From: Manu <manuelreyesbravo@gmail.com>
To: pgsql-bugs@lists.postgresql.org
Cc: John Naylor <johncnaylorls@gmail.com>
Subject: Re: use-after-free in jsonb_set_lax()
Date: Thu, 15 Oct 2026 03:21:43 -0300
Message-ID: <179204530357.2958899.15502080119117747086@gmail.com> (raw)
In-Reply-To: <CANWCAZYbYkOUPWb2snASVrpxQZc=FanrMHTJFJgMM7aS3LUdkg@mail.gmail.com>
References: <CANWCAZYbYkOUPWb2snASVrpxQZc=FanrMHTJFJgMM7aS3LUdkg@mail.gmail.com>
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
Attachments:repro_asan.sql.txt (text/plain, 408B) download
\echo == variant: arg0 computed per-row with ~5 kB padding, so the per-tuple context
\echo == spills out of its keeper block and the jsonb null lands in a block free()d on reset
SELECT jsonb_set_lax(j || jsonb_build_object('pad', repeat('x', 5000)), '{a}', NULL, true, t) - 'pad'
FROM (VALUES (jsonb_build_object('a','1'),'use_json_null'),
(jsonb_build_object('a','1'),'delete_key')) AS v(j,t);
asan-report.txt (text/plain, 11.9K) download
=================================================================
==666397==ERROR: AddressSanitizer: heap-use-after-free on address 0x7e1f71c1fce0 at pc 0x000001876f63 bp 0x7ffcb4285260 sp 0x7ffcb4285258
READ of size 1 at 0x7e1f71c1fce0 thread T0
#0 0x000001876f62 in VARATT_IS_EXTENDED ../../../../src/include/varatt.h:463
#1 0x000001876f62 in pg_detoast_datum /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/fmgr/fmgr.c:1800
#2 0x000001658fd2 in DatumGetJsonbP ../../../../src/include/utils/jsonb.h:403
#3 0x000001658fd2 in jsonb_set /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonfuncs.c:4859
#4 0x0000016599ce in jsonb_set_lax /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonfuncs.c:4917
#5 0x000000f4ce32 in ExecInterpExpr /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execExprInterp.c:936
#6 0x000000f7f889 in ExecEvalExprNoReturn ../../../src/include/executor/executor.h:431
#7 0x000000f7f889 in ExecEvalExprNoReturnSwitchContext ../../../src/include/executor/executor.h:472
#8 0x000000f7f889 in ExecProject ../../../src/include/executor/executor.h:504
#9 0x000000f7f889 in ExecScanExtended ../../../src/include/executor/execScan.h:236
#10 0x000000f7f889 in ExecScan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execScan.c:59
#11 0x0000010266c1 in ExecValuesScan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/nodeValuesscan.c:200
#12 0x000000f5c309 in ExecProcNode ../../../src/include/executor/executor.h:327
#13 0x000000f5c309 in ExecutePlan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:1766
#14 0x000000f5c309 in standard_ExecutorRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:377
#15 0x000000f5c622 in ExecutorRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:314
#16 0x0000014f5767 in PortalRunSelect /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/pquery.c:921
#17 0x0000014f931e in PortalRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/pquery.c:765
#18 0x0000014ef947 in exec_simple_query /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/postgres.c:1297
#19 0x0000014f14d3 in PostgresMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/postgres.c:4946
#20 0x0000014e631c in BackendMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/backend_startup.c:124
#21 0x0000013073a6 in postmaster_child_launch /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/launch_backend.c:268
#22 0x00000130ea20 in BackendStartup /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:3650
#23 0x00000130ea20 in ServerLoop /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:1727
#24 0x0000013116e3 in PostmasterMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:1414
#25 0x000001073f48 in main /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/main/main.c:227
#26 0x7f8f72d63300 in __libc_start_call_main (/lib64/libc.so.6+0x7d300) (BuildId: 02120880204b69a3b1f1e23a2eb47b2b12bc2636)
#27 0x7f8f72d63417 in __libc_start_main_impl (/lib64/libc.so.6+0x7d417) (BuildId: 02120880204b69a3b1f1e23a2eb47b2b12bc2636)
#28 0x0000009e7264 in _start (/home/manu/Proyectos/yggdrasil/.arboles/jslax-inst/master-asan/bin/postgres+0x9e7264) (BuildId: 937b858dcb7103a06735e294a004a9b447c26598)
0x7e1f71c1fce0 is located 15072 bytes inside of 16384-byte region [0x7e1f71c1c200,0x7e1f71c20200)
freed by thread T0 here:
#0 0x7f8f7316420f in free.part.0 (/lib64/libasan.so.8+0x16420f) (BuildId: 03bc53f1963ff3227be69a129d4fe48bb6a64b94)
#1 0x0000018b9636 in AllocSetReset /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/mmgr/aset.c:605
#2 0x0000018c7b9f in MemoryContextResetOnly /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/mmgr/mcxt.c:442
#3 0x0000018c8173 in MemoryContextReset /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/mmgr/mcxt.c:416
#4 0x000000f7ebac in ExecScanExtended ../../../src/include/executor/execScan.h:187
#5 0x000000f7ebac in ExecScan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execScan.c:59
#6 0x0000010266c1 in ExecValuesScan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/nodeValuesscan.c:200
#7 0x000000f5c309 in ExecProcNode ../../../src/include/executor/executor.h:327
#8 0x000000f5c309 in ExecutePlan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:1766
#9 0x000000f5c309 in standard_ExecutorRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:377
#10 0x000000f5c622 in ExecutorRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:314
#11 0x0000014f5767 in PortalRunSelect /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/pquery.c:921
#12 0x0000014f931e in PortalRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/pquery.c:765
#13 0x0000014ef947 in exec_simple_query /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/postgres.c:1297
#14 0x0000014f14d3 in PostgresMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/postgres.c:4946
#15 0x0000014e631c in BackendMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/backend_startup.c:124
#16 0x0000013073a6 in postmaster_child_launch /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/launch_backend.c:268
#17 0x00000130ea20 in BackendStartup /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:3650
#18 0x00000130ea20 in ServerLoop /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:1727
#19 0x0000013116e3 in PostmasterMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:1414
#20 0x000001073f48 in main /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/main/main.c:227
#21 0x7f8f72d63300 in __libc_start_call_main (/lib64/libc.so.6+0x7d300) (BuildId: 02120880204b69a3b1f1e23a2eb47b2b12bc2636)
#22 0x7f8f72d63417 in __libc_start_main_impl (/lib64/libc.so.6+0x7d417) (BuildId: 02120880204b69a3b1f1e23a2eb47b2b12bc2636)
#23 0x0000009e7264 in _start (/home/manu/Proyectos/yggdrasil/.arboles/jslax-inst/master-asan/bin/postgres+0x9e7264) (BuildId: 937b858dcb7103a06735e294a004a9b447c26598)
previously allocated by thread T0 here:
#0 0x7f8f7316515f in malloc (/lib64/libasan.so.8+0x16515f) (BuildId: 03bc53f1963ff3227be69a129d4fe48bb6a64b94)
#1 0x0000018b8bcf in AllocSetAllocFromNewBlock /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/mmgr/aset.c:950
#2 0x0000018b9ba0 in AllocSetAlloc /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/mmgr/aset.c:1096
#3 0x0000018ba1b9 in AllocSetRealloc /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/mmgr/aset.c:1473
#4 0x0000018c9683 in repalloc /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/mmgr/mcxt.c:1665
#5 0x000001926c51 in enlargeStringInfo /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/common/stringinfo.c:410
#6 0x00000163d666 in reserveFromBuffer /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:1610
#7 0x00000163d798 in appendToBuffer /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:1644
#8 0x00000163d957 in convertJsonbScalar /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:1954
#9 0x00000163eb0b in convertJsonbValue /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:1732
#10 0x00000163f297 in convertJsonbObject /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:1901
#11 0x00000163f297 in convertJsonbValue /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:1736
#12 0x00000163f5e8 in convertToJsonb /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:1690
#13 0x0000016428c1 in JsonbValueToJsonb /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb_util.c:118
#14 0x0000016339df in jsonb_build_object_worker /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb.c:1175
#15 0x000001633b47 in jsonb_build_object /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/utils/adt/jsonb.c:1195
#16 0x000000f4ce32 in ExecInterpExpr /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execExprInterp.c:936
#17 0x000000f3bafa in ExecInterpExprStillValid /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execExprInterp.c:2309
#18 0x000000f7f889 in ExecEvalExprNoReturn ../../../src/include/executor/executor.h:431
#19 0x000000f7f889 in ExecEvalExprNoReturnSwitchContext ../../../src/include/executor/executor.h:472
#20 0x000000f7f889 in ExecProject ../../../src/include/executor/executor.h:504
#21 0x000000f7f889 in ExecScanExtended ../../../src/include/executor/execScan.h:236
#22 0x000000f7f889 in ExecScan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execScan.c:59
#23 0x0000010266c1 in ExecValuesScan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/nodeValuesscan.c:200
#24 0x000000f75d43 in ExecProcNodeFirst /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execProcnode.c:469
#25 0x000000f5c309 in ExecProcNode ../../../src/include/executor/executor.h:327
#26 0x000000f5c309 in ExecutePlan /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:1766
#27 0x000000f5c309 in standard_ExecutorRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:377
#28 0x000000f5c622 in ExecutorRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/executor/execMain.c:314
#29 0x0000014f5767 in PortalRunSelect /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/pquery.c:921
#30 0x0000014f931e in PortalRun /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/pquery.c:765
#31 0x0000014ef947 in exec_simple_query /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/postgres.c:1297
#32 0x0000014f14d3 in PostgresMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/postgres.c:4946
#33 0x0000014e631c in BackendMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/tcop/backend_startup.c:124
#34 0x0000013073a6 in postmaster_child_launch /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/launch_backend.c:268
#35 0x00000130ea20 in BackendStartup /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:3650
#36 0x00000130ea20 in ServerLoop /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:1727
#37 0x0000013116e3 in PostmasterMain /home/manu/Proyectos/yggdrasil/.arboles/jslax-master-asan/src/backend/postmaster/postmaster.c:1414
SUMMARY: AddressSanitizer: heap-use-after-free ../../../../src/include/varatt.h:463 in VARATT_IS_EXTENDED
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: manuelreyesbravo@gmail.com, johncnaylorls@gmail.com
Subject: Re: use-after-free in jsonb_set_lax()
In-Reply-To: <179204530357.2958899.15502080119117747086@gmail.com>
References: <CANWCAZYbYkOUPWb2snASVrpxQZc=FanrMHTJFJgMM7aS3LUdkg@mail.gmail.com> <179204530357.2958899.15502080119117747086@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