pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sivirilova Marija <m.sivirilova@ftdata.ru>
To: Zsolt Parragi <zsolt.parragi@percona.com>
Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Subject: Re: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
Date: Mon, 16 Nov 2026 06:11:32 +0000
Message-ID: <17abc54868f24c20ab58afaed6c3495e@localhost.localdomain> (raw)
In-Reply-To: <CAN4CZFPquwS1puJSHFU7p8Q6QRBVQSZWx6ciC-C4SYvqpL29sw@mail.gmail.com>
References: <e3a21340a7844ada81c38aa5a164c58b@localhost.localdomain>
	<5BDD13C9-D6C8-475D-9946-61A165C2C822@yesql.se>
	<f159cf94e6694f11af783616234683dc@localhost.localdomain>
	<10564694-3E72-46C4-AC09-C0C344D2A62F@yesql.se>
	<CAN4CZFP7edXdvBoLVrdkFoyM7O1cRpWtcCEbNnpwW-Hg4JgGJw@mail.gmail.com>
	<50cfda3ae9d34c11b1b929c317d7639e@localhost.localdomain>
	<CAN4CZFPquwS1puJSHFU7p8Q6QRBVQSZWx6ciC-C4SYvqpL29sw@mail.gmail.com>

Hello!
I’ve updated expected/pgtypeslib-dt_test2.c to match the modified test. No changes to stdout or stderr are needed.
The revised patch is attached.
Best regards,
Maria Sivirilova

Attachments:

Fix-heap-buffer-overflow.patch (application/octet-stream, 4.5K) download From 68f27bbbb946b5f5e2ffab8ccfb5d3aad2161142 Mon Sep 17 00:00:00 2001 From: Sivirilova Maria <m.sivirilova@ftdata.ru> Date: Mon, 16 Nov 2026 13:09:36 +0700 Subject: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan() When parsing format tokens '%D', '%r', '%R', or '%T', the function allocated memory for the temporary buffer based on the length of the remaining input string (pstr). However, it subsequently appended the remaining format string (pfmt) to that buffer via strcat(). If the tail of the format string happened to be longer than the remaining input string, a heap-buffer-overflow occurred. Fix this by calculating the required buffer size using the length of the format string (pfmt) instead of the input string (pstr). Author: Maria Sivirilova <m.sivirilova@ftdata.ru> Co-authored-by: Kanatbek Kanybekov <k.kanybekov@ftdata.ru> --- src/interfaces/ecpg/pgtypeslib/dt_common.c | 8 ++++---- src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c | 17 +++++++++++++++++ src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc | 17 +++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/interfaces/ecpg/pgtypeslib/dt_common.c b/src/interfaces/ecpg/pgtypeslib/dt_common.c index c4119ab7932..ff345f7712f 100644 --- a/src/interfaces/ecpg/pgtypeslib/dt_common.c +++ b/src/interfaces/ecpg/pgtypeslib/dt_common.c @@ -2660,7 +2660,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d, * find the end of the substitution */ pfmt++; - tmp = pgtypes_alloc(strlen("%m/%d/%y") + strlen(pstr) + 1); + tmp = pgtypes_alloc(strlen("%m/%d/%y") + strlen(pfmt) + 1); if (!tmp) return 1; strcpy(tmp, "%m/%d/%y"); @@ -2787,7 +2787,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d, break; case 'r': pfmt++; - tmp = pgtypes_alloc(strlen("%I:%M:%S %p") + strlen(pstr) + 1); + tmp = pgtypes_alloc(strlen("%I:%M:%S %p") + strlen(pfmt) + 1); if (!tmp) return 1; strcpy(tmp, "%I:%M:%S %p"); @@ -2797,7 +2797,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d, return err; case 'R': pfmt++; - tmp = pgtypes_alloc(strlen("%H:%M") + strlen(pstr) + 1); + tmp = pgtypes_alloc(strlen("%H:%M") + strlen(pfmt) + 1); if (!tmp) return 1; strcpy(tmp, "%H:%M"); @@ -2845,7 +2845,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d, break; case 'T': pfmt++; - tmp = pgtypes_alloc(strlen("%H:%M:%S") + strlen(pstr) + 1); + tmp = pgtypes_alloc(strlen("%H:%M:%S") + strlen(pfmt) + 1); if (!tmp) return 1; strcpy(tmp, "%H:%M:%S"); diff --git a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c index b3e1e7519df..dae7fe3e157 100644 --- a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c +++ b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c @@ -101,6 +101,9 @@ main(void) #line 67 "dt_test2.pgc" + char large_fmt[10000]; + const char *fmt_tokens = "DrRT"; + int i, j; char *endptr; @@ -182,5 +185,19 @@ main(void) PGTYPESinterval_free(i1); } + /* + * Test parsing a small string with a very large format string. + * These calls are meant to trigger ASAN or Valgrind reports + * if a heap-buffer-overflow regression occurs. + */ + memset(large_fmt, 'A', sizeof(large_fmt) - 1); + large_fmt[sizeof(large_fmt) - 1] = '\0'; + large_fmt[0] = '%'; + for (i = 0; fmt_tokens[i] != '\0'; i++) + { + large_fmt[1] = fmt_tokens[i]; + PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1); + } + return 0; } diff --git a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc index 62b934b07ee..20feaf577d3 100644 --- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc +++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc @@ -66,6 +66,9 @@ main(void) date *dc; exec sql end declare section; + char large_fmt[10000]; + const char *fmt_tokens = "DrRT"; + int i, j; char *endptr; @@ -147,5 +150,19 @@ main(void) PGTYPESinterval_free(i1); } + /* + * Test parsing a small string with a very large format string. + * These calls are meant to trigger ASAN or Valgrind reports + * if a heap-buffer-overflow regression occurs. + */ + memset(large_fmt, 'A', sizeof(large_fmt) - 1); + large_fmt[sizeof(large_fmt) - 1] = '\0'; + large_fmt[0] = '%'; + for (i = 0; fmt_tokens[i] != '\0'; i++) + { + large_fmt[1] = fmt_tokens[i]; + PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1); + } + return 0; } -- 2.53.0

view thread (9+ messages)  latest in thread

Message-ID: <17abc54868f24c20ab58afaed6c3495e@localhost.localdomain>
Permalink:  ../17abc54868f24c20ab58afaed6c3495e@localhost.localdomain/
Also on:    postgresql.org/message-id/17abc54868f24c20ab58afaed6c3495e@localhost.localdomain

 ·  · 

reply

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: m.sivirilova@ftdata.ru, zsolt.parragi@percona.com
  Subject: Re: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
  In-Reply-To: <17abc54868f24c20ab58afaed6c3495e@localhost.localdomain>
  References: <e3a21340a7844ada81c38aa5a164c58b@localhost.localdomain> <5BDD13C9-D6C8-475D-9946-61A165C2C822@yesql.se> <f159cf94e6694f11af783616234683dc@localhost.localdomain> <10564694-3E72-46C4-AC09-C0C344D2A62F@yesql.se> <CAN4CZFP7edXdvBoLVrdkFoyM7O1cRpWtcCEbNnpwW-Hg4JgGJw@mail.gmail.com> <50cfda3ae9d34c11b1b929c317d7639e@localhost.localdomain> <CAN4CZFPquwS1puJSHFU7p8Q6QRBVQSZWx6ciC-C4SYvqpL29sw@mail.gmail.com> <17abc54868f24c20ab58afaed6c3495e@localhost.localdomain>

* 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