pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bug in DefineRange() with multiranges
5+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2021-10-13 5b0e7fe1d6 922e15c476  Fix use-after-free with multirange types in CREATE TYPE
* Bug in DefineRange() with multiranges
@ 2021-10-04 17:09 Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  2021-10-10 17:12 ` Re: Bug in DefineRange() with multiranges Peter Eisentraut <peter.eisentraut@enterprisedb.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Sergey Shinderuk @ 2021-10-04 17:09 UTC (permalink / raw)
  To: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; +Cc: Alex Kozhemyakin <a.kozhemyakin@postgrespro.ru>

Hi,

My colleague, Alex Kozhemyakin, stumbled upon a bug in DefineRange().
The problem is here:

     @@ -1707,7 +1707,6 @@ DefineRange(ParseState *pstate, 
CreateRangeStmt *stmt)
         /* Create cast from the range type to its multirange type */
         CastCreate(typoid, multirangeOid, castFuncOid, 'e', 'f', 
DEPENDENCY_INTERNAL);

     -   pfree(multirangeTypeName);
         pfree(multirangeArrayName);

         return address;


Given a query

     create type textrange1 as range(subtype=text, 
multirange_type_name=multirange_of_text, collation="C");

the string "multirange_of_text" in the parse tree is erroneously
pfree'd.  The corrupted parse tree is then passed to event triggers.

There is another branch in DefineRange() that genereates a multirange
type name which is fine to free.

I wonder what is the proper fix.  Just drop pfree() altogether or add
pstrdup() instead?  I see that makeMultirangeTypeName() doesn't bother
freeing its buf.

Here is a gdb session demonstating the bug:

Breakpoint 1, ProcessUtilitySlow (pstate=0x5652e80c7730, 
pstmt=0x5652e80a6a40, queryString=0x5652e80a5790 "create type textrange1 
as range(subtype=text, multirange_type_name=multirange_of_text, 
collation=\"C\");",
     context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, 
qc=0x7ffe835b4be0, dest=<optimized out>) at 
/pgwork/REL_14_STABLE/src/src/backend/tcop/utility.c:1621
1621                    address = DefineRange((CreateRangeStmt *) 
parsetree);
(gdb) p *(Value *)((TypeName *)((DefElem *)((CreateRangeStmt 
*)parsetree)->params->elements[1].ptr_value)->arg)->names->elements[0].ptr_value
$1 = {type = T_String, val = {ival = -401972176, str = 0x5652e80a6430 
"multirange_of_text"}}
(gdb) n
1900            if (!commandCollected)
(gdb) p *(Value *)((TypeName *)((DefElem *)((CreateRangeStmt 
*)parsetree)->params->elements[1].ptr_value)->arg)->names->elements[0].ptr_value
$2 = {type = T_String, val = {ival = -401972176, str = 0x5652e80a6430 
'\177' <repeats 32 times>, "\020"}}


Regards,

-- 
Sergey Shinderuk		https://postgrespro.com/





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bug in DefineRange() with multiranges
  2021-10-04 17:09 Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
@ 2021-10-10 17:12 ` Peter Eisentraut <peter.eisentraut@enterprisedb.com>
  2021-10-12 05:52   ` Re: Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  0 siblings, 1 reply; 5+ messages in thread

From: Peter Eisentraut @ 2021-10-10 17:12 UTC (permalink / raw)
  To: Sergey Shinderuk <s.shinderuk@postgrespro.ru>; PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; +Cc: Alex Kozhemyakin <a.kozhemyakin@postgrespro.ru>

On 04.10.21 19:09, Sergey Shinderuk wrote:
> I wonder what is the proper fix.  Just drop pfree() altogether or add
> pstrdup() instead?  I see that makeMultirangeTypeName() doesn't bother
> freeing its buf.

I think removing the pfree()s is a correct fix.






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bug in DefineRange() with multiranges
  2021-10-04 17:09 Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  2021-10-10 17:12 ` Re: Bug in DefineRange() with multiranges Peter Eisentraut <peter.eisentraut@enterprisedb.com>
@ 2021-10-12 05:52   ` Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  2021-10-13 04:21     ` Re: Bug in DefineRange() with multiranges Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 5+ messages in thread

From: Sergey Shinderuk @ 2021-10-12 05:52 UTC (permalink / raw)
  To: Peter Eisentraut <peter.eisentraut@enterprisedb.com>; PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; +Cc: Alex Kozhemyakin <a.kozhemyakin@postgrespro.ru>

On 10.10.2021 20:12, Peter Eisentraut wrote:
> On 04.10.21 19:09, Sergey Shinderuk wrote:
>> I wonder what is the proper fix.  Just drop pfree() altogether or add
>> pstrdup() instead?  I see that makeMultirangeTypeName() doesn't bother
>> freeing its buf.
> 
> I think removing the pfree()s is a correct fix.
> 

Thanks, here is a patch.


-- 
Sergey Shinderuk		https://postgrespro.com/

Attachments:

0001-Fix-premature-pfree-of-multirange_type_name-in-Defin.patch (text/plain, 991B) download From 6c548f07d2a254da46cd0b6f6e99b7ed24a6b811 Mon Sep 17 00:00:00 2001 From: Sergey Shinderuk <s.shinderuk@postgrespro.ru> Date: Tue, 12 Oct 2021 07:57:42 +0300 Subject: [PATCH] Fix premature pfree() of multirange_type_name in DefineRange() If the mutlirange_type_name parameter is given in the query, this would erroneously pfree() the string in the parse tree. Oversight in 6df7a9698bb0. --- src/backend/commands/typecmds.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index b290629a450..9ab40341793 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -1707,7 +1707,6 @@ DefineRange(ParseState *pstate, CreateRangeStmt *stmt) /* Create cast from the range type to its multirange type */ CastCreate(typoid, multirangeOid, castFuncOid, 'e', 'f', DEPENDENCY_INTERNAL); - pfree(multirangeTypeName); pfree(multirangeArrayName); return address; -- 2.24.3 (Apple Git-128)
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Bug in DefineRange() with multiranges
  2021-10-04 17:09 Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  2021-10-10 17:12 ` Re: Bug in DefineRange() with multiranges Peter Eisentraut <peter.eisentraut@enterprisedb.com>
  2021-10-12 05:52   ` Re: Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
@ 2021-10-13 04:21     ` Michael Paquier <michael@paquier.xyz>
  2021-10-13 08:35       ` Re: Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  0 siblings, 1 reply; 5+ messages in thread

From: Michael Paquier @ 2021-10-13 04:21 UTC (permalink / raw)
  To: Sergey Shinderuk <s.shinderuk@postgrespro.ru>; +Cc: Peter Eisentraut <peter.eisentraut@enterprisedb.com>; PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; Alex Kozhemyakin <a.kozhemyakin@postgrespro.ru>

On Tue, Oct 12, 2021 at 08:52:29AM +0300, Sergey Shinderuk wrote:
> Thanks, here is a patch.

Looks fine seen from here, so I'll apply shortly.  I was initially
tempted to do pstrdup() on the object name returned by
QualifiedNameGetCreationNamespace(), but just removing the pfree() is
simpler.   

I got to wonder about similar mistakes from the other callers of
QualifiedNameGetCreationNamespace(), so I have double-checked but
nothing looks wrong.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bug in DefineRange() with multiranges
  2021-10-04 17:09 Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  2021-10-10 17:12 ` Re: Bug in DefineRange() with multiranges Peter Eisentraut <peter.eisentraut@enterprisedb.com>
  2021-10-12 05:52   ` Re: Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  2021-10-13 04:21     ` Re: Bug in DefineRange() with multiranges Michael Paquier <michael@paquier.xyz>
@ 2021-10-13 08:35       ` Sergey Shinderuk <s.shinderuk@postgrespro.ru>
  0 siblings, 0 replies; 5+ messages in thread

From: Sergey Shinderuk @ 2021-10-13 08:35 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Peter Eisentraut <peter.eisentraut@enterprisedb.com>; PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; Alex Kozhemyakin <a.kozhemyakin@postgrespro.ru>

On 13.10.2021 07:21, Michael Paquier wrote:
> Looks fine seen from here, so I'll apply shortly.

Thank you!

-- 
Sergey Shinderuk		https://postgrespro.com/





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2021-10-13 08:35 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-10-04 17:09 Bug in DefineRange() with multiranges Sergey Shinderuk <s.shinderuk@postgrespro.ru>
2021-10-10 17:12 ` Peter Eisentraut <peter.eisentraut@enterprisedb.com>
2021-10-12 05:52   ` Sergey Shinderuk <s.shinderuk@postgrespro.ru>
2021-10-13 04:21     ` Michael Paquier <michael@paquier.xyz>
2021-10-13 08:35       ` Sergey Shinderuk <s.shinderuk@postgrespro.ru>

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