pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John Naylor <john.naylor@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix use-after-free in jsonb_set_lax for Const NULL new_value
Date: Mon, 19 Oct 2026 04:41:43 +0000
Message-ID: <E1xIfBv-00000002D9h-1IBm@gemulon.postgresql.org> (raw)

Fix use-after-free in jsonb_set_lax for Const NULL new_value

Commit a83586b5543 decided to scribble on the caller's fcinfo but failed
to take into account that const arguments are written once at expression
init and assumed never to change.

The use_json_null branch overwrote fcinfo->args[2] with a jsonb null
built in the per-tuple expression context.  In later rows,
args[2].isnull is still false and args[2].value still holds that
pointer, but the per-tuple context has been reset.  Later rows find
args[2].isnull = false, short-circuit past the NULL handling at the top
of the function, and jump straight to jsonb_set with the stale pointer
regardless of their own null_value_treatment argument, which is never
read.

To fix, call jsonb_set through its own call frame instead.  Arguments 0,
1 and 3 were already checked non-NULL above, so we can use
DirectFunctionCall4.

Add regression tests covering use_json_null followed by delete_key and
return_target, and a multi-row default-treatment query.

Reported-by: Evan Si <evsi@amazon.com>
Discussion: https://postgr.es/m/CANWCAZY-PS=ev8TiiV1j=NoR2xLopEL8fDq0fyQZahHiP5b+Pw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/42b320abe33907fc8449c63ed6c62ad9fba5bd90

Modified Files
--------------
src/backend/utils/adt/jsonfuncs.c   |  8 +++++---
src/test/regress/expected/jsonb.out | 28 ++++++++++++++++++++++++++++
src/test/regress/sql/jsonb.sql      | 10 ++++++++++
3 files changed, 43 insertions(+), 3 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1xIfBv-00000002D9h-1IBm@gemulon.postgresql.org>
Permalink:  ../E1xIfBv-00000002D9h-1IBm@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xIfBv-00000002D9h-1IBm@gemulon.postgresql.org

 · 

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-committers@postgresql.org
  Cc: john.naylor@postgresql.org
  Subject: Re: pgsql: Fix use-after-free in jsonb_set_lax for Const NULL new_value
  In-Reply-To: <E1xIfBv-00000002D9h-1IBm@gemulon.postgresql.org>
  References: <E1xIfBv-00000002D9h-1IBm@gemulon.postgresql.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