pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row
5+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-11-06 23e2a332dd de51a0e294 c88c71a4b5 61b06d2ba6  Fix use-after-free in COPY (ON_ERROR)
* [Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row
@ 2026-10-28 10:12  Gowdham M <gowdham.m@zohocorp.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Gowdham M @ 2026-10-28 10:12 UTC (permalink / raw)
  To: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; +Cc: zlabs-cstore@zohocorp.com <zlabs-cstore@zohocorp.com>

Hello Developers,



We have discovered a heap use-after-free in COPY FROM ... WITH (ON_ERROR

ignore). It was introduced in PG v17 together with the ON_ERROR option, and it

is still present in v18, v19 and master.



What's the issue?



When ON_ERROR ignore skips a row because a column value cannot be converted,

NextCopyFrom() (CopyFromTextLikeOneRow() in v18 and later) returns from its

per-column loop without clearing cstate->cur_attname and cstate->cur_attval.

These are only cleared at the end of each loop iteration, which this early

return skips, and cur_attval normally points into cstate->attribute_buf.



When the next line is read, CopyReadAttributesText() / CopyReadAttributesCSV()

reset attribute_buf and, if the line does not fit in it (1024 bytes initially),

enlarge it with repalloc(), which may move it and free the old buffer. If that

line then fails before any of its columns is converted, for example with "extra

data after last expected column", CopyFromErrorCallback() still sees the stale

cur_attname/cur_attval and calls CopyLimitPrintoutLength() on the stale

pointer, which reads freed memory.



If the buffer is not enlarged, the pointer points into the reused buffer, and

the error is reported with a wrong CONTEXT that names a column of the skipped

row and shows a piece of the new line.



Steps to reproduce:



    CREATE TABLE copy_uaf_repro (a int, b int);

    \! python3 -c "print('1\tx'); print('1\t2\t' + 'z' * 1100)" > /tmp/copy_uaf_repro.txt

    COPY copy_uaf_repro FROM '/tmp/copy_uaf_repro.txt' WITH (FORMAT text, ON_ERROR ignore);



Row 1 is skipped because 'x' is not a valid integer. Row 2 is longer than 1024

bytes and has an extra column. With our malloc-based allocator (see below) and

AddressSanitizer, the backend aborts with a heap-use-after-free in

CopyLimitPrintoutLength() called from CopyFromErrorCallback(); the memory was

freed by repalloc() in enlargeStringInfo() called from

CopyReadAttributesText(). The full ASan report is attached

(crash_back_trace.txt). It was captured on our PG 17.11 based build; the

CStoreProcessUtility / CallPreviousProcessUtility frames belong to our

extension's ProcessUtility hook and are not involved.



With the default allocator the freed chunk stays readable, so instead of a

crash the error is reported with a wrong or garbled CONTEXT. A short row shows

the wrong context deterministically on any build:



    CREATE TABLE t (a int, b int);

    COPY t FROM STDIN WITH (ON_ERROR ignore);

    1	x

    1	2	3

    \.

    ERROR:  extra data after last expected column

    CONTEXT:  COPY t, line 2, column b: "2"



The expected CONTEXT is:  COPY t, line 2: "1	2	3"



Fix:



The attached patch clears cur_attname and cur_attval in NextCopyFromRawFields()

(NextCopyFromRawFieldsInternal() in v18 and later) before each new line is

read, that is, before attribute_buf can be reset or enlarged.



The patch also adds a regression test to copy2.sql that fails without the fix

(it reports line 2, column k: "2") and passes with it.



How was the bug detected?



We found this using our custom malloc-based memory context, which allocates

every chunk with its own malloc() call, without rounding up the requested size,

and releases it with free() on pfree() and context reset, instead of keeping it

in AllocSet freelists. This lets AddressSanitizer catch use-after-free bugs

that AllocSet hides. We shared this allocator earlier as malloc_allocator.patch

[1]. It has already helped find three other bugs, which have been fixed

[2][3][4]. That patch no longer applies to the current master; if there is

interest, we can send an updated version of it.



[1] https://www.postgresql.org/message-id/193261e2c4d.3dd3cd7c1842.871636075166132237%40zohocorp.com 

[2] 1fe5a347e3 Fix possible crash during WindowAgg evaluation

[3] ccc8194e42 Fix use-after-free in parallel_vacuum_reset_dead_items

[4] 54ab748651 Fix reuse-after-free hazard in dead_items_reset



Please let me know if you have any questions or would like further details.



Thanks & Regards,

Gowdham M

Member Technical Staff

ZOHO Corporation

Attachments:

copy_onerror_fix.patch (application/octet-stream, 2.4K) download From 63d1402a6a57536e40438f4017e40de782e8767b Mon Sep 17 00:00:00 2001 From: Gowdham M <gowdham.m@zohocorp.com> Date: Wed, 28 Oct 2026 15:07:51 +0530 Subject: [PATCH] Avoid usage of stale error context in COPY FROM after ON_ERROR ignore skips a row --- src/backend/commands/copyfromparse.c | 8 ++++++++ src/test/regress/expected/copy2.out | 4 ++++ src/test/regress/sql/copy2.sql | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c index 0645053577..f7fb8b3bdc 100644 --- a/src/backend/commands/copyfromparse.c +++ b/src/backend/commands/copyfromparse.c @@ -873,6 +873,14 @@ NextCopyFromRawFieldsInternal(CopyFromState cstate, char ***fields, int *nfields return false; } + /* + * Clear the current attribute for error messages. A row skipped by + * ON_ERROR leaves it set, with cur_attval pointing into attribute_buf, + * which is about to be reset and possibly enlarged. + */ + cstate->cur_attname = NULL; + cstate->cur_attval = NULL; + cstate->cur_lineno++; /* Actually read the line into memory here */ diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index df6fcd9179..353f54e600 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -883,6 +883,10 @@ CONTEXT: COPY hard_err, line 1, column foo: "1" COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: missing data for column "k" CONTEXT: COPY check_ign_err, line 1: "1 {1}" +-- test extra data after a skipped row: should fail, reporting the line +COPY check_ign_err FROM STDIN WITH (on_error ignore); +ERROR: extra data after last expected column +CONTEXT: COPY check_ign_err, line 2: "2 {2} 2 extra" -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: extra data after last expected column diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index ec6f5e8cfd..d2119b337f 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -669,6 +669,12 @@ COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} \. +-- test extra data after a skipped row: should fail, reporting the line +COPY check_ign_err FROM STDIN WITH (on_error ignore); +1 {1} a +2 {2} 2 extra +\. + -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} 3 abc -- 2.54.0
crash_back_trace.txt (text/plain, 11.1K) download ================================================================= ==72083==ERROR: AddressSanitizer: heap-use-after-free on address 0x61900007b2b2 at pc 0x000107eee804 bp 0x00016b4d8200 sp 0x00016b4d79d0 READ of size 2 at 0x61900007b2b2 thread T0 #0 0x000107eee800 in strlen+0x22c (/Library/Developer/CommandLineTools/usr/lib/clang/21/lib/darwin/libclang_rt.asan_osx_dynamic.dylib:arm64e+0x16800) #1 0x000104fce5d4 in CopyLimitPrintoutLength+0x14 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006ae5d4) #2 0x000104fce168 in CopyFromErrorCallback+0x3f0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006ae168) #3 0x000106232688 in errfinish+0x3b4 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x101912688) #4 0x000104fe1c50 in NextCopyFrom+0x828 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006c1c50) #5 0x000104fd1494 in CopyFrom+0x2e14 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006b1494) #6 0x000104fc6a94 in DoCopy+0x1cdc (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006a6a94) #7 0x000105be9c54 in standard_ProcessUtility+0xe08 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c9c54) #8 0x00012562b614 in CallPreviousProcessUtility(PlannedStmt*, char const*, bool, ProcessUtilityContext, ParamListInfoData*, QueryEnvironment*, _DestReceiver*, QueryCompletion*)+0x94 (/Users/gowdham-23370/projects/deb17/pg17d/lib/postgresql/cstore_fdw.dylib:arm64+0x27614) #9 0x00012562955c in CStoreProcessUtility(PlannedStmt*, char const*, bool, ProcessUtilityContext, ParamListInfoData*, QueryEnvironment*, _DestReceiver*, QueryCompletion*)+0x730 (/Users/gowdham-23370/projects/deb17/pg17d/lib/postgresql/cstore_fdw.dylib:arm64+0x2555c) #10 0x000105be8e0c in ProcessUtility+0x258 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c8e0c) #11 0x000105be7820 in PortalRunUtility+0x2ec (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c7820) #12 0x000105be5174 in PortalRunMulti+0x874 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c5174) #13 0x000105be31d4 in PortalRun+0x958 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c31d4) #14 0x000105bd63ec in exec_simple_query+0xbe0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012b63ec) #15 0x000105bd48f0 in PostgresMain+0x1880 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012b48f0) #16 0x000105bc0b70 in BackendMain+0x288 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012a0b70) #17 0x0001058b243c in postmaster_child_launch+0x2f0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f9243c) #18 0x0001058c06b8 in BackendStartup+0x5b4 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100fa06b8) #19 0x0001058bbcf0 in ServerLoop+0x454 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f9bcf0) #20 0x0001058b9db8 in PostmasterMain+0x2e24 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f99db8) #21 0x000105439858 in main+0x774 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100b19858) #22 0x00018f89c4e0 in start+0x1b4c (/usr/lib/dyld:arm64e+0x204e0) 0x61900007b2b2 is located 50 bytes inside of 1072-byte region [0x61900007b280,0x61900007b6b0) freed by thread T0 here: #0 0x000107f19460 in realloc+0x80 (/Library/Developer/CommandLineTools/usr/lib/clang/21/lib/darwin/libclang_rt.asan_osx_dynamic.dylib:arm64e+0x41460) #1 0x00010630bda0 in MallocContextRealloc+0x94 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1019ebda0) #2 0x000106314f1c in repalloc+0x244 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1019f4f1c) #3 0x0001063ce2b8 in enlargeStringInfo+0x490 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x101aae2b8) #4 0x000104fe026c in CopyReadAttributesText+0x35c (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006c026c) #5 0x000104fddae8 in NextCopyFromRawFields+0xa88 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006bdae8) #6 0x000104fe1ba0 in NextCopyFrom+0x778 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006c1ba0) #7 0x000104fd1494 in CopyFrom+0x2e14 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006b1494) #8 0x000104fc6a94 in DoCopy+0x1cdc (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006a6a94) #9 0x000105be9c54 in standard_ProcessUtility+0xe08 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c9c54) #10 0x00012562b614 in CallPreviousProcessUtility(PlannedStmt*, char const*, bool, ProcessUtilityContext, ParamListInfoData*, QueryEnvironment*, _DestReceiver*, QueryCompletion*)+0x94 (/Users/gowdham-23370/projects/deb17/pg17d/lib/postgresql/cstore_fdw.dylib:arm64+0x27614) #11 0x00012562955c in CStoreProcessUtility(PlannedStmt*, char const*, bool, ProcessUtilityContext, ParamListInfoData*, QueryEnvironment*, _DestReceiver*, QueryCompletion*)+0x730 (/Users/gowdham-23370/projects/deb17/pg17d/lib/postgresql/cstore_fdw.dylib:arm64+0x2555c) #12 0x000105be8e0c in ProcessUtility+0x258 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c8e0c) #13 0x000105be7820 in PortalRunUtility+0x2ec (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c7820) #14 0x000105be5174 in PortalRunMulti+0x874 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c5174) #15 0x000105be31d4 in PortalRun+0x958 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c31d4) #16 0x000105bd63ec in exec_simple_query+0xbe0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012b63ec) #17 0x000105bd48f0 in PostgresMain+0x1880 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012b48f0) #18 0x000105bc0b70 in BackendMain+0x288 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012a0b70) #19 0x0001058b243c in postmaster_child_launch+0x2f0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f9243c) #20 0x0001058c06b8 in BackendStartup+0x5b4 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100fa06b8) #21 0x0001058bbcf0 in ServerLoop+0x454 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f9bcf0) #22 0x0001058b9db8 in PostmasterMain+0x2e24 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f99db8) #23 0x000105439858 in main+0x774 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100b19858) #24 0x00018f89c4e0 in start+0x1b4c (/usr/lib/dyld:arm64e+0x204e0) previously allocated by thread T0 here: #0 0x000107f19274 in malloc+0x78 (/Library/Developer/CommandLineTools/usr/lib/clang/21/lib/darwin/libclang_rt.asan_osx_dynamic.dylib:arm64e+0x41274) #1 0x00010630b75c in MallocContextAlloc+0x48 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1019eb75c) #2 0x000106313c3c in palloc+0x404 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1019f3c3c) #3 0x0001063cd4bc in initStringInfo+0x2c (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x101aad4bc) #4 0x000104fd72e0 in BeginCopyFrom+0x2514 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006b72e0) #5 0x000104fc6a88 in DoCopy+0x1cd0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006a6a88) #6 0x000105be9c54 in standard_ProcessUtility+0xe08 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c9c54) #7 0x00012562b614 in CallPreviousProcessUtility(PlannedStmt*, char const*, bool, ProcessUtilityContext, ParamListInfoData*, QueryEnvironment*, _DestReceiver*, QueryCompletion*)+0x94 (/Users/gowdham-23370/projects/deb17/pg17d/lib/postgresql/cstore_fdw.dylib:arm64+0x27614) #8 0x00012562955c in CStoreProcessUtility(PlannedStmt*, char const*, bool, ProcessUtilityContext, ParamListInfoData*, QueryEnvironment*, _DestReceiver*, QueryCompletion*)+0x730 (/Users/gowdham-23370/projects/deb17/pg17d/lib/postgresql/cstore_fdw.dylib:arm64+0x2555c) #9 0x000105be8e0c in ProcessUtility+0x258 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c8e0c) #10 0x000105be7820 in PortalRunUtility+0x2ec (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c7820) #11 0x000105be5174 in PortalRunMulti+0x874 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c5174) #12 0x000105be31d4 in PortalRun+0x958 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012c31d4) #13 0x000105bd63ec in exec_simple_query+0xbe0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012b63ec) #14 0x000105bd48f0 in PostgresMain+0x1880 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012b48f0) #15 0x000105bc0b70 in BackendMain+0x288 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1012a0b70) #16 0x0001058b243c in postmaster_child_launch+0x2f0 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f9243c) #17 0x0001058c06b8 in BackendStartup+0x5b4 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100fa06b8) #18 0x0001058bbcf0 in ServerLoop+0x454 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f9bcf0) #19 0x0001058b9db8 in PostmasterMain+0x2e24 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100f99db8) #20 0x000105439858 in main+0x774 (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x100b19858) #21 0x00018f89c4e0 in start+0x1b4c (/usr/lib/dyld:arm64e+0x204e0) SUMMARY: AddressSanitizer: heap-use-after-free (/Users/gowdham-23370/projects/deb17/pg17d/bin/postgres:arm64+0x1006ae5d4) in CopyLimitPrintoutLength+0x14 Shadow bytes around the buggy address: 0x61900007b000: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x61900007b080: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x61900007b100: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x61900007b180: fd fd fd fd fd fd fa fa fa fa fa fa fa fa fa fa 0x61900007b200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa =>0x61900007b280: fd fd fd fd fd fd[fd]fd fd fd fd fd fd fd fd fd 0x61900007b300: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x61900007b380: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x61900007b400: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x61900007b480: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x61900007b500: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb ==72083==ABORTING
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: [Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row
@ 2026-11-03 03:40  shihao zhong <zhong950419@gmail.com>
  parent: Gowdham M <gowdham.m@zohocorp.com>
  0 siblings, 1 reply; 5+ messages in thread

From: shihao zhong @ 2026-11-03 03:40 UTC (permalink / raw)
  To: Gowdham M <gowdham.m@zohocorp.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; zlabs-cstore@zohocorp.com <zlabs-cstore@zohocorp.com>

Hi Gowdham,

Thanks for the report and the patch. I can reproduce it on 17 through
master. It also crashes a normal release build on Linux, if the next
row is a few MB long.

I think ON_ERROR set_null in 19 hits the same bug, the attached test
is a repro for it.

Thanks,
Shihao

Attachments:

nocfbot-0002-Test-stale-COPY-error-context-after-ON_ERROR-set_nul.txt (text/plain, 1.9K) download From 919492bd6f0781ddf2b9864e154bec89d252060a Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Mon, 2 Nov 2026 22:31:23 -0500 Subject: [PATCH 2/2] Test stale COPY error context after ON_ERROR set_null The previous commit also fixes set_null, which exists in v19 and later. --- src/test/regress/expected/copy2.out | 4 ++++ src/test/regress/sql/copy2.sql | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index 353f54e6005..690568db4be 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -830,6 +830,10 @@ CONTEXT: COPY t_on_error_null, line 1: "1,1" COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); ERROR: extra data after last expected column CONTEXT: COPY t_on_error_null, line 1: "1,2,3,4" +-- fail, extra data after a row whose last column was set to null +COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); +ERROR: extra data after last expected column +CONTEXT: COPY t_on_error_null, line 2: "1,2,3,4" COPY t_on_error_null FROM STDIN WITH (on_error set_null, log_verbosity verbose); -- ok NOTICE: setting to null due to data type incompatibility at line 1 for column "b": "x1" CONTEXT: COPY t_on_error_null diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index d2119b337f7..71b7e31dac5 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -628,6 +628,11 @@ COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); 1,2,3,4 \. +-- fail, extra data after a row whose last column was set to null +COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); +1,2,x +1,2,3,4 +\. COPY t_on_error_null FROM STDIN WITH (on_error set_null, log_verbosity verbose); -- ok 10 x1 yx -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: [Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row
@ 2026-11-03 06:50  Gowdham M <gowdham.m@zohocorp.com>
  parent: shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Gowdham M @ 2026-11-03 06:50 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; zlabs-cstore@zohocorp.com <zlabs-cstore@zohocorp.com>

Hi Shihao,



Thanks for testing, and for confirming the release-build crash.



Yes, set_null hits the same stale pointer, and the patch fixes it, since the

fields are cleared before each new line is read. Your test passes with the

patch on REL_19_STABLE and master, and fails without it, so it can go on top

of the patch for 19 and master.



Thanks & Regards,

Gowdham M







From: shihao zhong <zhong950419@gmail.com>
To: "Gowdham M"<gowdham.m@zohocorp.com>
Cc: "pgsql-bugs"<pgsql-bugs@lists.postgresql.org>, "zlabs-cstore@zohocorp.com"<zlabs-cstore@zohocorp.com>
Date: Tue, 03 Nov 2026 09:10:47 +0530
Subject: Re: [Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row



Hi Gowdham,



Thanks for the report and the patch. I can reproduce it on 17 through
master. It also crashes a normal release build on Linux, if the next
row is a few MB long.


I think ON_ERROR set_null in 19 hits the same bug, the attached test

is a repro for it.



Thanks,

Shihao

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row
@ 2026-11-05 04:18  Michael Paquier <michael@paquier.xyz>
  parent: Gowdham M <gowdham.m@zohocorp.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Michael Paquier @ 2026-11-05 04:18 UTC (permalink / raw)
  To: Gowdham M <gowdham.m@zohocorp.com>; +Cc: shihao zhong <zhong950419@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>; zlabs-cstore@zohocorp.com <zlabs-cstore@zohocorp.com>

On Tue, Nov 03, 2026 at 12:20:33PM +0530, Gowdham M wrote:
> Yes, set_null hits the same stale pointer, and the patch fixes it, since the
> fields are cleared before each new line is read. Your test passes with the
> patch on REL_19_STABLE and master, and fails without it, so it can go on top
> of the patch for 19 and master.

Confirmed issue here as well.  The test for SET_NULL is a good idea,
as you say down to v19 where this flavor has been added. 

@@ -873,6 +873,14 @@ NextCopyFromRawFieldsInternal(CopyFromState cstate, char ***fields, int *nfields
             return false;
     }
 
+    /*
+     * Clear the current attribute for error messages.  A row skipped by
+     * ON_ERROR leaves it set, with cur_attval pointing into attribute_buf,
+     * which is about to be reset and possibly enlarged.
+     */
+    cstate->cur_attname = NULL;
+    cstate->cur_attval = NULL;
+
     cstate->cur_lineno++;

Resetting these two at this location seems a bit weird to me?  What do
you think about the beginning of NextCopyFrom() instead?  That's also
where the fields for the row to NULL are reset.  I don't think that
this should be an issue for the COPY API portability or file_fdw.
--
Michael

Attachments:

0001-Fix-use-after-free-in-COPY-ON_ERROR.patch (text/plain, 4.0K) download From 8cc6745fc479748883e1d22b360e8509cf4b59a3 Mon Sep 17 00:00:00 2001 From: Michael Paquier <michael@paquier.xyz> Date: Thu, 5 Nov 2026 13:11:54 +0900 Subject: [PATCH] Fix use-after-free in COPY (ON_ERROR) When ON_ERROR handled a soft error, some of the contents set for the error context callback could remain set and pointing at areas that could be reset, leading to some use-after-frees depending on the input data provided through COPY. These contents are now reset at the beginning of NextCopyFrom(), before processing the next tuple. SET_NULL is affected down to v19, IGNORE down to v17. Author: Gowdham M <gowdham.m@zohocorp.com> Reviewed-by: Shihao Zhong <zhong950419@gmail.com> Discussion: https://postgr.es/m/1a181ff6be8.6d14feb261368.4036709598649280477@zohocorp.com Backpatch-through: 17 --- src/backend/commands/copyfromparse.c | 4 ++++ src/test/regress/expected/copy2.out | 8 ++++++++ src/test/regress/sql/copy2.sql | 11 +++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c index 06450535779b..e6cf907ad280 100644 --- a/src/backend/commands/copyfromparse.c +++ b/src/backend/commands/copyfromparse.c @@ -927,6 +927,10 @@ NextCopyFrom(CopyFromState cstate, ExprContext *econtext, MemSet(nulls, true, num_phys_attrs * sizeof(bool)); MemSet(cstate->defaults, false, num_phys_attrs * sizeof(bool)); + /* Clear the current attribute for error messages. */ + cstate->cur_attname = NULL; + cstate->cur_attval = NULL; + /* Get one row from source */ if (!cstate->routine->CopyFromOneRow(cstate, econtext, values, nulls)) return false; diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index df6fcd91799a..690568db4be8 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -830,6 +830,10 @@ CONTEXT: COPY t_on_error_null, line 1: "1,1" COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); ERROR: extra data after last expected column CONTEXT: COPY t_on_error_null, line 1: "1,2,3,4" +-- fail, extra data after a row whose last column was set to null +COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); +ERROR: extra data after last expected column +CONTEXT: COPY t_on_error_null, line 2: "1,2,3,4" COPY t_on_error_null FROM STDIN WITH (on_error set_null, log_verbosity verbose); -- ok NOTICE: setting to null due to data type incompatibility at line 1 for column "b": "x1" CONTEXT: COPY t_on_error_null @@ -883,6 +887,10 @@ CONTEXT: COPY hard_err, line 1, column foo: "1" COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: missing data for column "k" CONTEXT: COPY check_ign_err, line 1: "1 {1}" +-- test extra data after a skipped row: should fail, reporting the line +COPY check_ign_err FROM STDIN WITH (on_error ignore); +ERROR: extra data after last expected column +CONTEXT: COPY check_ign_err, line 2: "2 {2} 2 extra" -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: extra data after last expected column diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index ec6f5e8cfdc1..71b7e31dac56 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -628,6 +628,11 @@ COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); 1,2,3,4 \. +-- fail, extra data after a row whose last column was set to null +COPY t_on_error_null FROM STDIN WITH (delimiter ',', on_error set_null); +1,2,x +1,2,3,4 +\. COPY t_on_error_null FROM STDIN WITH (on_error set_null, log_verbosity verbose); -- ok 10 x1 yx @@ -669,6 +674,12 @@ COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} \. +-- test extra data after a skipped row: should fail, reporting the line +COPY check_ign_err FROM STDIN WITH (on_error ignore); +1 {1} a +2 {2} 2 extra +\. + -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} 3 abc -- 2.56.0
signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: [Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row
@ 2026-11-06 00:20  Michael Paquier <michael@paquier.xyz>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 5+ messages in thread

From: Michael Paquier @ 2026-11-06 00:20 UTC (permalink / raw)
  To: Gowdham M <gowdham.m@zohocorp.com>; +Cc: shihao zhong <zhong950419@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>; zlabs-cstore@zohocorp.com <zlabs-cstore@zohocorp.com>

On Thu, Nov 05, 2026 at 01:18:30PM +0900, Michael Paquier wrote:
> Resetting these two at this location seems a bit weird to me?  What do
> you think about the beginning of NextCopyFrom() instead?  That's also
> where the fields for the row to NULL are reset.  I don't think that
> this should be an issue for the COPY API portability or file_fdw.

This one has been applied down to v17 as of 23e2a332dd24.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-11-06 00:20 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-28 10:12 [Bug] Heap use-after-free in COPY FROM error context after ON_ERROR ignore skips a row Gowdham M <gowdham.m@zohocorp.com>
2026-11-03 03:40 ` shihao zhong <zhong950419@gmail.com>
2026-11-03 06:50   ` Gowdham M <gowdham.m@zohocorp.com>
2026-11-05 04:18     ` Michael Paquier <michael@paquier.xyz>
2026-11-06 00:20       ` Michael Paquier <michael@paquier.xyz>

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