diff options
author | Alexander Korotkov | 2024-08-22 06:50:48 +0000 |
---|---|---|
committer | Alexander Korotkov | 2024-08-22 06:50:48 +0000 |
commit | 04158e7fa37c2dda9c3421ca922d02807b86df19 (patch) | |
tree | aa1c8d1d48c1336275b764418e9dcf039aefe7e2 | |
parent | 9bb842f95ef3384f0822c386a4c569780e613e4e (diff) |
Avoid repeated table name lookups in createPartitionTable()
Currently, createPartitionTable() opens newly created table using its name.
This approach is prone to privilege escalation attack, because we might end
up opening another table than we just created.
This commit address the issue above by opening newly created table by its
OID. It appears to be tricky to get a relation OID out of ProcessUtility().
We have to extend TableLikeClause with new newRelationOid field, which is
filled within ProcessUtility() to be further accessed by caller.
Security: CVE-2014-0062
Reported-by: Noah Misch
Discussion: https://postgr.es/m/20240808171351.a9.nmisch%40google.com
Reviewed-by: Pavel Borisov, Dmitry Koval
-rw-r--r-- | src/backend/commands/tablecmds.c | 3 | ||||
-rw-r--r-- | src/backend/parser/gram.y | 1 | ||||
-rw-r--r-- | src/backend/tcop/utility.c | 6 | ||||
-rw-r--r-- | src/include/nodes/parsenodes.h | 1 |
4 files changed, 10 insertions, 1 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 13bb881120..52ce6b0c92 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -20383,6 +20383,7 @@ createPartitionTable(RangeVar *newPartName, Relation modelRel, tlc->options = CREATE_TABLE_LIKE_ALL & ~(CREATE_TABLE_LIKE_INDEXES | CREATE_TABLE_LIKE_IDENTITY | CREATE_TABLE_LIKE_STATISTICS); tlc->relationOid = InvalidOid; + tlc->newRelationOid = InvalidOid; createStmt->tableElts = lappend(createStmt->tableElts, tlc); /* Need to make a wrapper PlannedStmt. */ @@ -20406,7 +20407,7 @@ createPartitionTable(RangeVar *newPartName, Relation modelRel, * Open the new partition with no lock, because we already have * AccessExclusiveLock placed there after creation. */ - newRel = table_openrv(newPartName, NoLock); + newRel = table_open(tlc->newRelationOid, NoLock); /* * We intended to create the partition with the same persistence as the diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index c3f25582c3..b7d98eb9f0 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -4138,6 +4138,7 @@ TableLikeClause: n->relation = $2; n->options = $3; n->relationOid = InvalidOid; + n->newRelationOid = InvalidOid; $$ = (Node *) n; } ; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index b2ea8125c9..b385175e7a 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1225,6 +1225,12 @@ ProcessUtilitySlow(ParseState *pstate, morestmts = expandTableLikeClause(table_rv, like); stmts = list_concat(morestmts, stmts); + + /* + * Store the OID of newly created relation to the + * TableLikeClause for the caller to use it. + */ + like->newRelationOid = address.objectId; } else { diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 85a62b538e..577c4bfef7 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -754,6 +754,7 @@ typedef struct TableLikeClause RangeVar *relation; bits32 options; /* OR of TableLikeOption flags */ Oid relationOid; /* If table has been looked up, its OID */ + Oid newRelationOid; /* OID of newly created table */ } TableLikeClause; typedef enum TableLikeOption |