From: Alexander Kuznetsov <kuznetsovam@altlinux.org>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: pgsql-hackers@lists.postgresql.org
Cc: nickel@altlinux.org
Cc: egori@altlinux.org
Subject: Re: Possible null pointer dereference in afterTriggerAddEvent()
Date: Fri, 26 Jul 2024 12:16:00 +0300
Message-ID: <8ddb8e6a-e899-4218-870f-8b25da3a9f3f@altlinux.org> (raw)
In-Reply-To: <202407251707.tkkrcs7c4eso@alvherre.pgsql>
References: <202407251707.tkkrcs7c4eso@alvherre.pgsql>
25.07.2024 20:07, Alvaro Herrera wrote:
> Maybe for sanity (and perhaps for Svace compliance) we could do it the
> other way around, i.e. by testing events->tail for nullness instead of
> events->head, then add the assertion:
>
> if (events->tail == NULL)
> {
> Assert(events->head == NULL);
> events->head = chunk;
> }
> else
> events->tail->next = chunk;
>
> This way, it's not wholly redundant.
Thanks for your response!
I agree with the proposed changes and have updated the patch accordingly. Version 2 is attached.
> That said, I'm not sure we actually *need* to change this.
I understand and partly agree. But it appears that with these changes, the dereference of a null pointer is impossible even in builds where assertions are disabled. Previously, this issue could theoretically occur. Consequently, these changes slightly enhance overall security.
--
Best regards,
Alexander Kuznetsov
Attachments:v2-0001-Add-assertion-of-an-empty-list-in-afterTriggerAdd.patch (text/x-patch, 1.1K) download
From 63a3a19fe67bfd1f427b21d53ff0ef642aed89c4 Mon Sep 17 00:00:00 2001
From: Alexander Kuznetsov <kuznetsovam@altlinux.org>
Date: Fri, 26 Jul 2024 11:55:53 +0300
Subject: [PATCH v2] Add assertion of an empty list in afterTriggerAddEvent()
The unwritten assumption is that events->tail and events->head are either
both NULL or neither is NULL.
Change the order of checks to ensure that we never try
to dereference events->tail if it is NULL.
This was found by ALT Linux Team with Svace.
---
src/backend/commands/trigger.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.cindex 170360edda..e039d62b0b 100644--- a/src/backend/commands/trigger.c+++ b/src/backend/commands/trigger.c@@ -4096,8 +4096,11 @@ afterTriggerAddEvent(AfterTriggerEventList *events,
chunk->endptr = chunk->endfree = (char *) chunk + chunksize;
Assert(chunk->endfree - chunk->freeptr >= needed);
- if (events->head == NULL)+ if (events->tail == NULL)+ {+ Assert(events->head == NULL);
events->head = chunk;
+ }
else
events->tail->next = chunk;
events->tail = chunk;
--
2.42.2
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: kuznetsovam@altlinux.org, alvherre@alvh.no-ip.org, nickel@altlinux.org, egori@altlinux.org
Subject: Re: Possible null pointer dereference in afterTriggerAddEvent()
In-Reply-To: <8ddb8e6a-e899-4218-870f-8b25da3a9f3f@altlinux.org>
References: <202407251707.tkkrcs7c4eso@alvherre.pgsql> <8ddb8e6a-e899-4218-870f-8b25da3a9f3f@altlinux.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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