pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: John Naylor <johncnaylorls@gmail.com>
To: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Subject: Re: use-after-free in jsonb_set_lax()
Date: Thu, 15 Oct 2026 11:08:11 +0200
Message-ID: <4046596fd6b6af64d207e939ca86935f0503b361.camel@cybertec.at> (raw)
In-Reply-To: <CANWCAZY-PS=ev8TiiV1j=NoR2xLopEL8fDq0fyQZahHiP5b+Pw@mail.gmail.com>
References: <CANWCAZY-PS=ev8TiiV1j=NoR2xLopEL8fDq0fyQZahHiP5b+Pw@mail.gmail.com>

On Thu, 2026-10-15 at 09:44 +0700, John Naylor wrote:
> I received an off-list report of a crash in production using this
> function. It's possible to reproduce a crash in a non-assert build
> with a fuzzing script, but the easiest demo of the problem is (on an
> assert build):
> 
> =# SELECT jsonb_set_lax(j, '{a}', NULL, true, t)
> FROM (VALUES (jsonb_build_object('a', '1'), 'use_json_null'),
>              (jsonb_build_object('a', '1'), 'delete_key')) AS v(j, t);
> ERROR:  unknown type of jsonb container
> 
> I've attached a fix, which I plan to commit to all supported versions
> in a couple days barring objections.
> 
> I didn't find any other places in the code base with a similar bug,
> but of course I can't be certain.

Funny, I also received a bug report about a crash in jsonb_set_lax()
yesterday, and your patch fixes the problem.

The reproducer I got was

WITH RECURSIVE c AS (                                                                                                                                                   SELECT 1 n,
jsonb_set_lax('{"a":1}'::jsonb,'{a}',NULL,true,'use_json_null') j
  UNION ALL
  SELECT n+1, jsonb_set_lax(jsonb_build_object('n',n,'p',j,'x',repeat('A',500)),'{n}',NULL,true,
    CASE WHEN n%2=0 THEN 'delete_key' ELSE 'use_json_null' END)
  FROM c WHERE n<80
) SELECT n FROM c ORDER BY n DESC LIMIT 1;

So, +1 from me!

Yours,
Laurenz Albe






view thread (8+ messages)  latest in thread

Message-ID: <4046596fd6b6af64d207e939ca86935f0503b361.camel@cybertec.at>
Permalink:  ../4046596fd6b6af64d207e939ca86935f0503b361.camel@cybertec.at/
Also on:    postgresql.org/message-id/4046596fd6b6af64d207e939ca86935f0503b361.camel@cybertec.at

 · 

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: laurenz.albe@cybertec.at, johncnaylorls@gmail.com
  Subject: Re: use-after-free in jsonb_set_lax()
  In-Reply-To: <4046596fd6b6af64d207e939ca86935f0503b361.camel@cybertec.at>
  References: <CANWCAZY-PS=ev8TiiV1j=NoR2xLopEL8fDq0fyQZahHiP5b+Pw@mail.gmail.com> <4046596fd6b6af64d207e939ca86935f0503b361.camel@cybertec.at>

* 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