1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Ensure ExecutorCheckPerms_hook is called from RI fast path

The fast path's ri_CheckPermissions() called ExecCheckOneRelPerms()
directly, so ExecutorCheckPerms_hook never saw the referenced-table
access that the SPI path's query exposed to it via ExecutorStart().
Modules such as sepgsql that rely on this hook therefore lost control
over FK checks against referenced tables that go through the fast
path.

So, build a one-entry range table and permission-info list and call
ExecCheckPermissions().  The hook now sees the same relation and
requiredPerms as before, once per check that goes through the fast
path.  This also drops the shortcut that skipped the column-level
check when table-level privileges sufficed, since the hook must run
regardless.  While at it, drop the explicit frees in the old code,
because both callers run ri_CheckPermissions() in a per-tuple context
that is reset after each check, so they were redundant.

Reported-by: Noah Misch <noah@leadboat.com>
Discussion: https://postgr.es/m/20260705210533.ee.noahmisch%40microsoft.com
Backpatch-through: 19
This commit is contained in:
Amit Langote
2026-09-24 19:28:32 +09:00
parent 46024c573b
commit 2cdd1950f2

View File

@@ -3613,14 +3613,18 @@ ri_check_fastpath_index(RI_ConstraintInfo *riinfo,
* ri_CheckPermissions
* Check permissions for the SELECT ... FOR KEY SHARE used by the SPI
* path, as the referenced table's owner.
*
* Go through ExecCheckPermissions() with a manufactured range table so that
* ExecutorCheckPerms_hook gets control, as it does for the query the SPI
* path executes.
*/
static void
ri_CheckPermissions(const RI_ConstraintInfo *riinfo, Relation query_rel)
{
AclResult aclresult;
AclMode requiredPerms = ACL_SELECT | ACL_SELECT_FOR_UPDATE;
RangeTblEntry *rte;
RTEPermissionInfo *perminfo;
bool result;
/* USAGE on schema. */
aclresult = object_aclcheck(NamespaceRelationId,
@@ -3630,16 +3634,10 @@ ri_CheckPermissions(const RI_ConstraintInfo *riinfo, Relation query_rel)
aclcheck_error(aclresult, OBJECT_SCHEMA,
get_namespace_name(RelationGetNamespace(query_rel)));
/* Avoid building the column bitmap when table privileges suffice. */
if (pg_class_aclmask(RelationGetRelid(query_rel), GetUserId(),
requiredPerms, ACLMASK_ALL) == requiredPerms)
return;
/*
* SELECT is needed only on the referenced key columns. FOR KEY SHARE
* also needs UPDATE privilege, which may be granted on any column. Use
* the executor's checks for both, leaving updatedCols empty as the SPI
* query does.
* also needs UPDATE privilege, which may be granted on any column; leave
* updatedCols empty as the SPI query does.
*/
perminfo = makeNode(RTEPermissionInfo);
perminfo->relid = RelationGetRelid(query_rel);
@@ -3651,12 +3649,14 @@ ri_CheckPermissions(const RI_ConstraintInfo *riinfo, Relation query_rel)
perminfo->selectedCols = bms_add_member(perminfo->selectedCols, attno);
}
result = ExecCheckOneRelPerms(perminfo);
bms_free(perminfo->selectedCols);
pfree(perminfo);
if (!result)
aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_TABLE,
RelationGetRelationName(query_rel));
rte = makeNode(RangeTblEntry);
rte->rtekind = RTE_RELATION;
rte->relid = RelationGetRelid(query_rel);
rte->relkind = query_rel->rd_rel->relkind;
rte->rellockmode = RowShareLock;
rte->perminfoindex = 1;
(void) ExecCheckPermissions(list_make1(rte), list_make1(perminfo), true);
}
/*