Add object TRUNCATE hook All operations with acl permissions checks should have a corresponding hook so that, for example, mandatory access control (MAC) may be enforced by an extension. The command TRUNCATE is missing this hook, so add it. Patch by Yuli Khodorkovskiy with some editorialization by me. Based on the discussion not back-patched. A separate patch will exercise the hook in the sepgsql extension. Author: Yuli Khodorkovskiy Reviewed-by: Joe Conway Discussion: https://postgr.es/m/CAFL5wJcomybj1Xdw7qWmPJRpGuFukKgNrDb6uVBaCMgYS9dkaA%40mail.gmail.com --- diff --git a/src/backend/catalog/objectaccess.c b/src/backend/catalog/objectaccess.c index b1619b8..d51d01f 100644 *** a/src/backend/catalog/objectaccess.c --- b/src/backend/catalog/objectaccess.c *************** *** 11,16 **** --- 11,17 ---- #include "postgres.h" #include "catalog/objectaccess.h" + #include "catalog/pg_class.h" #include "catalog/pg_namespace.h" #include "catalog/pg_proc.h" *************** RunObjectDropHook(Oid classId, Oid objec *** 65,70 **** --- 66,87 ---- } /* + * RunObjectTruncateHook + * + * It is the entrypoint of OAT_TRUNCATE event + */ + void + RunObjectTruncateHook(Oid objectId) + { + /* caller should check, but just in case... */ + Assert(object_access_hook != NULL); + + (*object_access_hook) (OAT_TRUNCATE, + RelationRelationId, objectId, 0, + NULL); + } + + /* * RunObjectPostAlterHook * * It is entrypoint of OAT_POST_ALTER event diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 45aae59..5440eb9 100644 *** a/src/backend/commands/tablecmds.c --- b/src/backend/commands/tablecmds.c *************** truncate_check_rel(Oid relid, Form_pg_cl *** 1937,1942 **** --- 1937,1944 ---- (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), errmsg("permission denied: \"%s\" is a system catalog", relname))); + + InvokeObjectTruncateHook(relid); } /* diff --git a/src/include/catalog/objectaccess.h b/src/include/catalog/objectaccess.h index 0170f1f..9824adf 100644 *** a/src/include/catalog/objectaccess.h --- b/src/include/catalog/objectaccess.h *************** *** 37,42 **** --- 37,46 ---- * creation or altering, because OAT_POST_CREATE or OAT_POST_ALTER are * sufficient for extensions to track these kind of checks. * + * OAT_TRUNCATE should be invoked just before truncation of objects. This + * event is equivalent to truncate permission on a relation under the + * default access control mechanism. + * * Other types may be added in the future. */ typedef enum ObjectAccessType *************** typedef enum ObjectAccessType *** 45,51 **** OAT_DROP, OAT_POST_ALTER, OAT_NAMESPACE_SEARCH, ! OAT_FUNCTION_EXECUTE } ObjectAccessType; /* --- 49,56 ---- OAT_DROP, OAT_POST_ALTER, OAT_NAMESPACE_SEARCH, ! OAT_FUNCTION_EXECUTE, ! OAT_TRUNCATE } ObjectAccessType; /* *************** extern void RunObjectPostCreateHook(Oid *** 131,136 **** --- 136,142 ---- bool is_internal); extern void RunObjectDropHook(Oid classId, Oid objectId, int subId, int dropflags); + extern void RunObjectTruncateHook(Oid objectId); extern void RunObjectPostAlterHook(Oid classId, Oid objectId, int subId, Oid auxiliaryId, bool is_internal); extern bool RunNamespaceSearchHook(Oid objectId, bool ereport_on_violation); *************** extern void RunFunctionExecuteHook(Oid o *** 160,165 **** --- 166,177 ---- (dropflags)); \ } while(0) + #define InvokeObjectTruncateHook(objectId) \ + do { \ + if (object_access_hook) \ + RunObjectTruncateHook(objectId); \ + } while(0) + #define InvokeObjectPostAlterHook(classId,objectId,subId) \ InvokeObjectPostAlterHookArg((classId),(objectId),(subId), \ InvalidOid,false)