postgresql/src/include
Dean Rasheed a85eddab23 Fix security checks in selectivity estimation functions.
Commit e2d4ef8de8 (the fix for CVE-2017-7484) added security checks
to the selectivity estimation functions to prevent them from running
user-supplied operators on data obtained from pg_statistic if the user
lacks privileges to select from the underlying table. In cases
involving inheritance/partitioning, those checks were originally
performed against the child RTE (which for plain inheritance might
actually refer to the parent table). Commit 553d2ec271 then extended
that to also check the parent RTE, allowing access if the user had
permissions on either the parent or the child. It turns out, however,
that doing any checks using the child RTE is incorrect, since
securityQuals is set to NULL when creating an RTE for an inheritance
child (whether it refers to the parent table or the child table), and
therefore such checks do not correctly account for any RLS policies or
security barrier views. Therefore, do the security checks using only
the parent RTE. This is consistent with how RLS policies are applied,
and the executor's ACL checks, both of which use only the parent
table's permissions/policies. Similar checks are performed in the
extended stats code, so update that in the same way, centralizing all
the checks in a new function.

In addition, note that these checks by themselves are insufficient to
ensure that the user has access to the table's data because, in a
query that goes via a view, they only check that the view owner has
permissions on the underlying table, not that the current user has
permissions on the view itself. In the selectivity estimation
functions, there is no easy way to navigate from underlying tables to
views, so add permissions checks for all views mentioned in the query
to the planner startup code. If the user lacks permissions on a view,
a permissions error will now be reported at planner-startup, and the
selectivity estimation functions will not be run.

Checking view permissions at planner-startup in this way is a little
ugly, since the same checks will be repeated at executor-startup.
Longer-term, it might be better to move all the permissions checks
from the executor to the planner so that permissions errors can be
reported sooner, instead of creating a plan that won't ever be run.
However, such a change seems too far-reaching to be back-patched.

Back-patch to all supported versions. In v13, there is the added
complication that UPDATEs and DELETEs on inherited target tables are
planned using inheritance_planner(), which plans each inheritance
child table separately, so that the selectivity estimation functions
do not know that they are dealing with a child table accessed via its
parent. Handle that by checking access permissions on the top parent
table at planner-startup, in the same way as we do for views. Any
securityQuals on the top parent table are moved down to the child
tables by inheritance_planner(), so they continue to be checked by the
selectivity estimation functions.

Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Noah Misch <noah@leadboat.com>
Backpatch-through: 13
Security: CVE-2025-8713
2025-08-11 09:09:12 +01:00
..
access doc: Inform about aminsertcleanup optional NULLness 2025-07-22 14:34:22 +09:00
archive Add macro for customizing an archiving WARNING message. 2024-03-04 15:41:42 -06:00
backup Align blocks in incremental backups to BLCKSZ 2024-04-05 16:30:01 +02:00
bootstrap Update copyright for 2024 2024-01-03 20:49:05 -05:00
catalog Fix a deadlock during ALTER SUBSCRIPTION ... DROP PUBLICATION. 2025-08-01 06:53:16 +00:00
commands Fix concurrent update trigger issues with MERGE in a CTE. 2025-07-18 10:01:31 +01:00
common Silence warning in older versions of Valgrind 2025-02-24 18:03:48 +07:00
datatype Avoid using timezone Asia/Manila in regression tests. 2025-01-20 15:47:53 -05:00
executor Fix security checks in selectivity estimation functions. 2025-08-11 09:09:12 +01:00
fe_utils Specify the encoding of input to fmtId() 2025-02-10 10:03:38 -05:00
foreign Clean up more indent breakage from 6377e12a5. 2024-04-16 13:00:40 -04:00
jit Monkey-patch LLVM code to fix ARM relocation bug. 2024-11-06 23:07:34 +13:00
lib radixtree: Fix crash when non-creator begins iteration over shared tree. 2025-02-06 11:35:51 -08:00
libpq Make the memory layout of Port struct independent of USE_OPENSSL 2024-11-22 17:43:34 +02:00
mb With GB18030, prevent SIGSEGV from reading past end of allocation. 2025-05-05 04:52:07 -07:00
nodes Update comment for IndexInfo.ii_NullsNotDistinct 2025-07-01 23:13:07 +02:00
optimizer Disallow collecting transition tuples from child foreign tables. 2025-08-08 10:50:02 +09:00
parser Handle default NULL insertion a little better. 2025-01-29 15:31:55 -05:00
partitioning Revert support for ALTER TABLE ... MERGE/SPLIT PARTITION(S) commands 2024-08-24 19:18:24 +03:00
pch Update copyright for 2024 2024-01-03 20:49:05 -05:00
port Fix build breakage on Solaris-alikes with late-model GCC. 2025-07-23 15:44:29 -04:00
portability Update copyright for 2024 2024-01-03 20:49:05 -05:00
postmaster Wait for WAL summarization to catch up before creating .partial file. 2024-07-26 14:51:10 -04:00
regex Avoid assertion due to disconnected NFA sub-graphs in regex parsing. 2024-11-15 18:23:38 -05:00
replication Fix re-distributing previously distributed invalidation messages during logical decoding. 2025-06-16 17:35:58 -07:00
rewrite Support MERGE into updatable views. 2024-02-29 15:56:59 +00:00
snowball Update copyright for 2024 2024-01-03 20:49:05 -05:00
statistics Update copyright for 2024 2024-01-03 20:49:05 -05:00
storage Restore smgrtruncate() prototype in back-branches. 2025-01-08 10:43:40 +13:00
tcop Restrict accesses to non-system views and foreign tables during pg_dump. 2024-08-05 06:05:30 -07:00
tsearch Update copyright for 2024 2024-01-03 20:49:05 -05:00
utils Fix security checks in selectivity estimation functions. 2025-08-11 09:09:12 +01:00
.gitignore Refactor dlopen() support 2018-09-06 11:33:04 +02:00
c.h Assume that <stdbool.h> conforms to the C standard. 2024-11-25 20:54:15 +13:00
fmgr.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
funcapi.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
getopt_long.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
Makefile Put genbki.pl output into src/include/catalog/ directly 2024-03-14 07:11:21 +01:00
meson.build meson: Restore implicit warning/debug/optimize flags for extensions 2024-06-07 09:36:26 +02:00
miscadmin.h Exclude parallel workers from connection privilege/limit checks. 2024-12-28 16:08:50 -05:00
pg_config.h.in Make our usage of memset_s() conform strictly to the C11 standard. 2025-05-18 12:45:55 -04:00
pg_config_ext.h.in Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_ext.h.meson meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
pg_config_manual.h Introduce a non-recursive JSON parser 2024-04-04 06:46:40 -04:00
pg_getopt.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_trace.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pgstat.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pgtar.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pgtime.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
port.h Avoid breaking SJIS encoding while de-backslashing Windows paths. 2025-01-29 14:24:36 -05:00
postgres.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
postgres_ext.h Move RelFileNumber declarations to common/relpath.h. 2022-09-27 12:01:57 -04:00
postgres_fe.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
varatt.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
windowapi.h Update copyright for 2024 2024-01-03 20:49:05 -05:00