From: dwsteele (@dwsteele) <noreply+dwsteele@github.com>
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
Subject: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
Date: Fri, 26 Jun 2026 01:17:59 +0000
Message-ID: <gh-pgaudit-pgaudit-322@github.com> (raw)
The ddl_command_end and sql_drop event triggers store each object's name and type in a temporary memory context that is freed before the trigger returns. The audit event was only marked as logged when log_audit_event() actually emitted a record, so when the last command collected by the trigger belonged to a class that is not being logged, for example a GRANT inside CREATE SCHEMA while only DDL is logged, the event was left marked "not logged". The ProcessUtility hook then logged the statement a second time after the trigger returned, reading the object name and type from the already-freed context. Under AddressSanitizer this surfaced as a heap-buffer-overflow in append_valid_csv().
Mark the audit event as logged after each collected row is processed so the statement is not logged again with freed fields.
Reported-by: @nkPit
inline patch 1: expected/pgaudit.out, pgaudit.c, sql/pgaudit.sql (+103/-0 lines) download inline-1.patchdiff --git a/expected/pgaudit.out b/expected/pgaudit.outindex 17e8de1..eafe356 100644--- a/expected/pgaudit.out+++ b/expected/pgaudit.out@@ -2728,6 +2728,56 @@ NOTICE: AUDIT: SESSION,14,1,READ,SELECT,TABLE,public.graph_vertex,"SELECT name
DROP PROPERTY GRAPH graph_test;
DROP TABLE graph_vertex;
+--+-- Test that a role substatement (GRANT) collected as the last command of a DDL+-- statement does not cause the statement to be logged a second time by the+-- ProcessUtility hook, reading the object name/type after their memory context+-- has been freed, when only DDL is being logged.+SET pgaudit.log = 'none';+CREATE EXTENSION pgaudit;+SET pgaudit.log_client = on;+SET pgaudit.log_level = 'notice';+SET pgaudit.log_relation = off;+SET pgaudit.log = 'ddl';+CREATE TABLE schema_grant_tbl (id int);+NOTICE: AUDIT: SESSION,15,1,DDL,CREATE TABLE,TABLE,public.schema_grant_tbl,CREATE TABLE schema_grant_tbl (id int),<not logged>+CREATE ROLE regress_schema_grant;+CREATE SCHEMA schema_grant+ GRANT SELECT+ ON public.schema_grant_tbl+ TO regress_schema_grant;+NOTICE: AUDIT: SESSION,16,1,DDL,CREATE SCHEMA,SCHEMA,schema_grant,"CREATE SCHEMA schema_grant+ GRANT SELECT+ ON public.schema_grant_tbl+ TO regress_schema_grant",<not logged>+-- The same scenario written with the CREATE SCHEMA ... AUTHORIZATION form. Its+-- schema elements execute as the target role, so create the table as an element+-- (owned by that role) and grant on it; the trailing GRANT substatement+-- exercises the same path.+CREATE ROLE regress_schema_auth;+CREATE SCHEMA AUTHORIZATION regress_schema_auth+ CREATE TABLE schema_auth_tbl (id int)+ GRANT SELECT+ ON schema_auth_tbl+ TO PUBLIC;+NOTICE: AUDIT: SESSION,17,1,DDL,CREATE SCHEMA,SCHEMA,regress_schema_auth,"CREATE SCHEMA AUTHORIZATION regress_schema_auth+ CREATE TABLE schema_auth_tbl (id int)+ GRANT SELECT+ ON schema_auth_tbl+ TO PUBLIC",<not logged>+NOTICE: AUDIT: SESSION,17,1,DDL,CREATE TABLE,TABLE,regress_schema_auth.schema_auth_tbl,"CREATE SCHEMA AUTHORIZATION regress_schema_auth+ CREATE TABLE schema_auth_tbl (id int)+ GRANT SELECT+ ON schema_auth_tbl+ TO PUBLIC",<not logged>+SET pgaudit.log = 'none';+DROP SCHEMA schema_grant;+DROP SCHEMA regress_schema_auth CASCADE;+NOTICE: drop cascades to table regress_schema_auth.schema_auth_tbl+DROP TABLE schema_grant_tbl;+DROP ROLE regress_schema_grant;+DROP ROLE regress_schema_auth;+DROP EXTENSION pgaudit;
-- Cleanup
-- Set client_min_messages up to warning to avoid noise
SET client_min_messages = 'warning';
diff --git a/pgaudit.c b/pgaudit.cindex 0086883..c5cca00 100644--- a/pgaudit.c+++ b/pgaudit.c@@ -1879,6 +1879,12 @@ pgaudit_ddl_command_end(PG_FUNCTION_ARGS)
}
else
log_audit_event(auditEventStack);
++ /*+ * Mark the audit event as logged so it won't be logged again with fields+ * that have been freed.+ */+ auditEventStack->auditEvent.logged = true;
}
/* Complete the query */
@@ -1962,6 +1968,12 @@ pgaudit_sql_drop(PG_FUNCTION_ARGS)
auditEventStack->auditEvent.logged = false;
log_audit_event(auditEventStack);
++ /*+ * Mark the audit event as logged so it won't be logged again with fields+ * that have been freed.+ */+ auditEventStack->auditEvent.logged = true;
}
/* Complete the query */
diff --git a/sql/pgaudit.sql b/sql/pgaudit.sqlindex 9dfc571..a4f2169 100644--- a/sql/pgaudit.sql+++ b/sql/pgaudit.sql@@ -1750,6 +1750,47 @@ SELECT name
DROP PROPERTY GRAPH graph_test;
DROP TABLE graph_vertex;
+--+-- Test that a role substatement (GRANT) collected as the last command of a DDL+-- statement does not cause the statement to be logged a second time by the+-- ProcessUtility hook, reading the object name/type after their memory context+-- has been freed, when only DDL is being logged.+SET pgaudit.log = 'none';+CREATE EXTENSION pgaudit;++SET pgaudit.log_client = on;+SET pgaudit.log_level = 'notice';+SET pgaudit.log_relation = off;+SET pgaudit.log = 'ddl';++CREATE TABLE schema_grant_tbl (id int);+CREATE ROLE regress_schema_grant;++CREATE SCHEMA schema_grant+ GRANT SELECT+ ON public.schema_grant_tbl+ TO regress_schema_grant;++-- The same scenario written with the CREATE SCHEMA ... AUTHORIZATION form. Its+-- schema elements execute as the target role, so create the table as an element+-- (owned by that role) and grant on it; the trailing GRANT substatement+-- exercises the same path.+CREATE ROLE regress_schema_auth;++CREATE SCHEMA AUTHORIZATION regress_schema_auth+ CREATE TABLE schema_auth_tbl (id int)+ GRANT SELECT+ ON schema_auth_tbl+ TO PUBLIC;++SET pgaudit.log = 'none';+DROP SCHEMA schema_grant;+DROP SCHEMA regress_schema_auth CASCADE;+DROP TABLE schema_grant_tbl;+DROP ROLE regress_schema_grant;+DROP ROLE regress_schema_auth;+DROP EXTENSION pgaudit;+
-- Cleanup
-- Set client_min_messages up to warning to avoid noise
SET client_min_messages = 'warning';