pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Geoghegan <pg@heroku.com>
To: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: David Rowley <david.rowley@2ndquadrant.com>
Cc: Michael Paquier <michael.paquier@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Regina Obe <lr@pcorp.us>
Cc: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14344: string_agg(DISTINCT ..) crash
Date: Wed, 12 Oct 2016 17:02:11 -0700
Message-ID: <CAM3SWZSAPkKmbAFUHC3e6qszfMTnsUzv137Fi2Mcyg-rfEebKQ@mail.gmail.com> (raw)
In-Reply-To: <CAM3SWZQ6+C7xJKRNGxyRJLadaRBADsq4=0dOkbLmVCTHoXBV9w@mail.gmail.com>
References: <20160929035538.20224.39628@wrigleys.postgresql.org>
	<12479.1475122250@sss.pgh.pa.us>
	<000001d21a09$dd22a9f0$9767fdd0$@pcorp.us>
	<CAB7nPqSF-xbTAraFJqbjZLin7AUOyfvMDwq1p=CEGSv3Y7Vf_w@mail.gmail.com>
	<CAKJS1f9mu3NpZaYhD2z5Cr1fTx=dYqXqi8BW1NCnfXF17E=Hvw@mail.gmail.com>
	<CAM3SWZSJyUvakbibG6cP_3gLVnVDaPKzHPjyh43Ap54Za9xDxg@mail.gmail.com>
	<CAKJS1f9V=OQb_JyZ8HmeLonJMhQFJe9PFzE77F2KTED1AqTXNw@mail.gmail.com>
	<CAM3SWZQWjMkRbH=D0Yp-i0Jaj2vmWupN-3E_f8keAh-7UQeRFw@mail.gmail.com>
	<CAKJS1f_4aw+Lfm=xUxfGQe-LQiNJc=OrfBYkg58By2Gn-aL5Zg@mail.gmail.com>
	<CAM3SWZRF5fuuBnk6jBtsjND6LY8g1xhX=3QKBwhq=bw7sG_BGw@mail.gmail.com>
	<CAM3SWZQxeF5r4=U9-ikgn86Zun+LbcRfv6gQ0SWdWPQ5XVC6BQ@mail.gmail.com>
	<22270.1475163646@sss.pgh.pa.us>
	<CAM3SWZTQcmnutjza3GdLLC-Y4WmF_FKAfAxLF4EMVK_dS3RsEQ@mail.gmail.com>
	<ce6f5ca3-1576-43dd-e02d-e23f344ee316@iki.fi>
	<CAM3SWZQaUAowLqNE9xr+5ovFaEoU_SW9GVVUYthUnhDCvgOehw@mail.gmail.com>
	<CAM3SWZQ6+C7xJKRNGxyRJLadaRBADsq4=0dOkbLmVCTHoXBV9w@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On Wed, Oct 12, 2016 at 4:36 PM, Peter Geoghegan <pg@heroku.com> wrote:
> This fix has us copy the MinimalTuple into sortcontext palloc() memory
> within tuplesort_gettupleslot() (based on commit 25bf7f8b). This still
> differs a little from tuplestore_gettupleslot(), which explicitly uses
> current context of caller, but we've always done things that way for
> tuplesort.c.

Actually, it's only true that tuplesort sortcontext context is used
when copy isn't needed, which is not predictable to caller, so the new
comment is a bit inaccurate. The inconsistency seems inconsequential,
since we've always assume that caller tuples allocated within
sortcontext may be "owned" by caller (when should_free = true),
despite not being in caller's own memory context.

Attached is revision with tiny tweak to relevant comment.

-- 
Peter Geoghegan

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

0001-Fix-use-after-free-around-DISTINCT-transition-functi.patch (text/x-patch, 1.9K) download From d44e635a4d81e94bb313a76d37c8e1f90666135a Mon Sep 17 00:00:00 2001 From: Peter Geoghegan <pg@bowt.ie> Date: Wed, 12 Oct 2016 15:59:58 -0700 Subject: [PATCH] Fix use-after-free around DISTINCT transition function calls. Have tuplesort_gettupleslot() copy the contents of its current table slot as needed. This is based on an approach taken by tuplestore_gettupleslot(). In the future, tuplesort_gettupleslot() may also be taught to avoid copying the tuple where caller can determine that that is safe (the tuplestore_gettupleslot() interface already offers this option to callers). Per bug #14344 from Regina Obe. Report: <20160929035538.20224.39628@wrigleys.postgresql.org> Backpatch-through: 9.6 --- src/backend/utils/sort/tuplesort.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index f6eb30c..46512cf 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -2084,6 +2084,10 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward, * determination of "non-equal tuple" based on simple binary inequality. A * NULL value in leading attribute will set abbreviated value to zeroed * representation, which caller may rely on in abbreviated inequality check. + * + * The slot receives a copied tuple (sometimes allocated in caller memory + * context) that will stay valid regardless of future manipulations of the + * tuplesort's state. */ bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, @@ -2104,6 +2108,11 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward, if (state->sortKeys->abbrev_converter && abbrev) *abbrev = stup.datum1; + if (!should_free) + { + stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple); + should_free = true; + } ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, should_free); return true; } -- 2.7.4

view thread (30+ messages)  latest in thread

Message-ID: <CAM3SWZSAPkKmbAFUHC3e6qszfMTnsUzv137Fi2Mcyg-rfEebKQ@mail.gmail.com>
Permalink:  ../CAM3SWZSAPkKmbAFUHC3e6qszfMTnsUzv137Fi2Mcyg-rfEebKQ@mail.gmail.com/
Also on:    postgresql.org/message-id/CAM3SWZSAPkKmbAFUHC3e6qszfMTnsUzv137Fi2Mcyg-rfEebKQ@mail.gmail.com

 ·  · 

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-bugs@postgresql.org
  Cc: pg@heroku.com, hlinnaka@iki.fi, tgl@sss.pgh.pa.us, david.rowley@2ndquadrant.com, michael.paquier@gmail.com, robertmhaas@gmail.com, lr@pcorp.us
  Subject: Re: BUG #14344: string_agg(DISTINCT ..) crash
  In-Reply-To: <CAM3SWZSAPkKmbAFUHC3e6qszfMTnsUzv137Fi2Mcyg-rfEebKQ@mail.gmail.com>
  References: <20160929035538.20224.39628@wrigleys.postgresql.org> <CAB7nPqSF-xbTAraFJqbjZLin7AUOyfvMDwq1p=CEGSv3Y7Vf_w@mail.gmail.com> <CAKJS1f9mu3NpZaYhD2z5Cr1fTx=dYqXqi8BW1NCnfXF17E=Hvw@mail.gmail.com> <CAM3SWZSJyUvakbibG6cP_3gLVnVDaPKzHPjyh43Ap54Za9xDxg@mail.gmail.com> <CAKJS1f9V=OQb_JyZ8HmeLonJMhQFJe9PFzE77F2KTED1AqTXNw@mail.gmail.com> <CAM3SWZQWjMkRbH=D0Yp-i0Jaj2vmWupN-3E_f8keAh-7UQeRFw@mail.gmail.com> <CAKJS1f_4aw+Lfm=xUxfGQe-LQiNJc=OrfBYkg58By2Gn-aL5Zg@mail.gmail.com> <CAM3SWZRF5fuuBnk6jBtsjND6LY8g1xhX=3QKBwhq=bw7sG_BGw@mail.gmail.com> <CAM3SWZQxeF5r4=U9-ikgn86Zun+LbcRfv6gQ0SWdWPQ5XVC6BQ@mail.gmail.com> <22270.1475163646@sss.pgh.pa.us> <CAM3SWZTQcmnutjza3GdLLC-Y4WmF_FKAfAxLF4EMVK_dS3RsEQ@mail.gmail.com> <ce6f5ca3-1576-43dd-e02d-e23f344ee316@iki.fi> <CAM3SWZQaUAowLqNE9xr+5ovFaEoU_SW9GVVUYthUnhDCvgOehw@mail.gmail.com> <CAM3SWZQ6+C7xJKRNGxyRJLadaRBADsq4=0dOkbLmVCTHoXBV9w@mail.gmail.com> <CAM3SWZSAPkKmbAFUHC3e6qszfMTnsUzv137Fi2Mcyg-rfEebKQ@mail.gmail.com>

* 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