use-after-free in jsonb_set_lax()
use-after-free in jsonb_set_lax()
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.
--
John Naylor
Amazon Web Services
Re: use-after-free in jsonb_set_lax()
От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Hi John,
> I didn't find any other places in the code base with a similar bug,
> but of course I can't be certain.
I swept for siblings and found none. Of the 444 writes to
fcinfo->args[] in the backend, all go into a frame the function built
itself or an executor/planner frame, except 15 into a caller-supplied
frame; jsonb_set_lax is the only SQL-callable one among those. Const
args are written once at expression init in the expected places
(ExecInitFunc, the JSON constructors). Same on 14-17. A differential
multi-row vs per-row fuzz over the jsonb family (132k cases, cassert
and ASan) only ever fires in jsonb_set_lax, and is clean with v1.
One note on severity, in case it affects the classification: the
read-after-free reaches the output, not just control flow. Under ASan
the thread's repro does not crash; it silently returns {"a": null}
instead of {}, since the freed null stays in the per-tuple keeper
block. The 3-argument form jsonb_set_lax(col, path, NULL) over two
rows returns bytes of freed memory inside the result value (on a
cassert build); on a non-assert build that is reused heap, so it looks
able to disclose memory contents, not only crash or return wrong data.
Allocating ~5 kB per row turns it into a clean heap-use-after-free at
the !PG_ARGISNULL(2) shortcut. v1 is clean on all of it.
Regards,
Manu
Re: use-after-free in jsonb_set_lax()
От:
John Naylor <johncnaylorls@gmail.com>
Дата:
On Thu, Oct 15, 2026 at 11:08 AM Manu wrote: > > I didn't find any other places in the code base with a similar bug, > > but of course I can't be certain. > > I swept for siblings and found none. Of the 444 writes to > fcinfo->args[] in the backend, all go into a frame the function built > itself or an executor/planner frame, except 15 into a caller-supplied > frame; jsonb_set_lax is the only SQL-callable one among those. Const > args are written once at expression init in the expected places > (ExecInitFunc, the JSON constructors). Same on 14-17. A differential > multi-row vs per-row fuzz over the jsonb family (132k cases, cassert > and ASan) only ever fires in jsonb_set_lax, and is clean with v1. Please don't copy and paste LLM output into emails. It's annoying. -- John Naylor Amazon Web Services
Re: use-after-free in jsonb_set_lax()
Hi John, > Please don't copy and paste LLM output into emails. It wasn't pasted; it's a real build here. The repro and the ASan report (with my build paths in it) are attached. It runs under an ASan build in a couple of seconds. The point, short: on a non-assert build this is not only a crash. The ASan report (attached) is heap-use-after-free, READ of size 1 at a freed address, in jsonb_set at jsonfuncs.c:4859 reached from jsonb_set_lax at 4917. The freed read lands in the value jsonb_set returns, so the result can carry reused heap, not just wrong data. That is what seemed worth a line for the back-branches. v1 is clean over the multi-row cases I ran. Regards, Manu
Re: use-after-free in jsonb_set_lax()
От:
Laurenz Albe <laurenz.albe@cybertec.at>
Дата:
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
Re: use-after-free in jsonb_set_lax()
От:
John Naylor <johncnaylorls@gmail.com>
Дата:
On Thu, Oct 15, 2026 at 1:21 PM Manu wrote: > > Please don't copy and paste LLM output into emails. > > It wasn't pasted; it's a real build here. What Claude model was used to compose this email? -- John Naylor Amazon Web Services
Re: use-after-free in jsonb_set_lax()
От:
Manuel Reyes Bravo <manuelreyesbravo@gmail.com>
Дата:
>What Claude model was used to compose this email? Claude, as a drafting aid. The sweep, the fuzzing and the ASan runs are mine and ran on my machines; that is what I meant by "not pasted", but I should have answered the actual question. Point taken on the style, I’ll keep future emails to the facts. Nothing further from me on this one; thanks for the fix. Regards, Manu
Re: use-after-free in jsonb_set_lax()
От:
John Naylor <johncnaylorls@gmail.com>
Дата:
On Thu, Oct 15, 2026 at 4:08 PM Laurenz Albe wrote: > Funny, I also received a bug report about a crash in jsonb_set_lax() > yesterday, and your patch fixes the problem. Glad to hear it! Pushed. (although I forgot to give you a tested-by credit..) -- John Naylor Amazon Web Services