pg.ddx.io pgaudit/pgaudit GitHub issues and pull requests
help / color / mirror / Atom feedRe: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
10+ messages / 2 participants
[nested] [flat]
* [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-06-26 01:17 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
0 siblings, 0 replies; 10+ messages in thread
From: dwsteele (@dwsteele) @ 2026-06-26 01:17 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
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.patch
diff --git a/expected/pgaudit.out b/expected/pgaudit.out
index 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.c
index 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.sql
index 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';
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-06-26 01:17 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: dwsteele (@dwsteele) @ 2026-06-26 01:17 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele assigned @dwsteele.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-06-26 01:18 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: dwsteele (@dwsteele) @ 2026-06-26 01:18 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele applied label "bug".
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-07-02 19:24 ` "pgguru (@pgguru)" <noreply+pgguru@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: pgguru (@pgguru) @ 2026-07-02 19:24 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
(on expected/pgaudit.out:2745)
does `CREATE SCHEMA ... AUTHORIZATION` hit these same paths?
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-07-02 19:33 ` "pgguru (@pgguru)" <noreply+pgguru@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: pgguru (@pgguru) @ 2026-07-02 19:33 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
(on pgaudit.c:1887)
So this doesn't change anything about the context that pieces were allocated in, it only suppresses the actual later inspection of the now invalid data?
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-07-03 03:38 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-03 03:38 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
(on pgaudit.c:1887)
Correct, this just keeps us from touching possible stale references later. We shouldn't be logging again anyway, so this is the correct behavior.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-07-03 04:07 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-03 04:07 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
(on expected/pgaudit.out:2745)
Yes and I added a test for that in bdb1591cc01a.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322 (approved): Fix use-after-free reading object name/type after event trigger.
@ 2026-07-20 14:40 ` "pgguru (@pgguru)" <noreply+pgguru@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: pgguru (@pgguru) @ 2026-07-20 14:40 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-07-21 03:22 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-21 03:22 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele closed this.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger.
@ 2026-07-21 03:22 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-21 03:22 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele merged this (commit 9352026ab84e).
^ permalink raw [nested|flat] 10+ messages in thread
end of thread, other threads:[~2026-07-21 03:22 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-26 01:17 [pgaudit/pgaudit] PR #322: Fix use-after-free reading object name/type after event trigger. "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-06-26 01:17 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-06-26 01:18 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-02 19:24 ` "pgguru (@pgguru)" <noreply+pgguru@github.com>
2026-07-02 19:33 ` "pgguru (@pgguru)" <noreply+pgguru@github.com>
2026-07-03 03:38 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-03 04:07 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-20 14:40 ` Re: [pgaudit/pgaudit] PR #322 (approved): Fix use-after-free reading object name/type after event trigger. "pgguru (@pgguru)" <noreply+pgguru@github.com>
2026-07-21 03:22 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-21 03:22 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox