Skip to content

Commit 51189f9

Browse files
committed
Disallow converting a table to a view within an outer SQL command.
We have long disallowed all forms of ALTER TABLE if the table is already opened by some outer SQL command in the same session. This has the same purpose as obtaining AccessExclusiveLock, but since a session's own locks don't conflict the lock only blocks use of the table by other sessions, not our own. Without this check, the ALTER might confuse the outer SQL command since any previous inspection of the table would potentially become invalid. However, the RelisBecomingView code path in DefineQueryRewrite never got that memo, and assumed that AccessExclusiveLock is sufficient for performing something morally equivalent to a rather invasive ALTER TABLE. Unsurprisingly, this can confuse an outer command that is trying to do something with the table. This was submitted as a security issue, but the security team has been unable to identify any consequence worse than a null pointer dereference (from trying to access rd_tableam methods that the relation no longer has). Therefore, in accordance with our usual policy, it's not security material and should just be fixed as a routine bug. Fix by disallowing the operation if the table is open locally, exactly as ALTER TABLE does it. Per an anonymous security researcher, via Bundesamt für Sicherheit in der Informationstechnik. Patch v12-v15 only. In v16 and later, we removed this code altogether (cf. commit b23cd18), so that there's no issue.
1 parent 2ca19aa commit 51189f9

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/backend/rewrite/rewriteDefine.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "catalog/pg_rewrite.h"
3030
#include "catalog/storage.h"
3131
#include "commands/policy.h"
32+
#include "commands/tablecmds.h"
3233
#include "miscadmin.h"
3334
#include "nodes/nodeFuncs.h"
3435
#include "parser/parse_utilcmd.h"
@@ -421,6 +422,9 @@ DefineQueryRewrite(const char *rulename,
421422
* whole business of converting relations to views is just an obsolete
422423
* kluge to allow dump/reload of views that participate in circular
423424
* dependencies.)
425+
*
426+
* Also ensure the relation isn't being manipulated in any outer SQL
427+
* command of our own session.
424428
*/
425429
if (event_relation->rd_rel->relkind != RELKIND_VIEW &&
426430
event_relation->rd_rel->relkind != RELKIND_MATVIEW)
@@ -429,6 +433,8 @@ DefineQueryRewrite(const char *rulename,
429433
Snapshot snapshot;
430434
TupleTableSlot *slot;
431435

436+
CheckTableNotInUse(event_relation, "CREATE RULE");
437+
432438
if (event_relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
433439
ereport(ERROR,
434440
(errcode(ERRCODE_WRONG_OBJECT_TYPE),

0 commit comments

Comments
 (0)