[PATCH] Fix two client-side NULL dereferences on malformed result sets (libpq, pg_receivewal)

Started by s1ko15 days ago5 messageshackers
Jump to latest
#1s1ko
s1ko@riseup.net

Hi,

Two client programs crash when the server answers an internal query with a
result set of an unexpected shape. Both are plain NULL dereferences: the
code
checks part of the shape, then calls PQgetvalue() on a cell that does not
exist. (First sent to security@, Noah pointed me here since the project does
not treat these as security issues.)

0001 - libpq, fe-connect.c. PQconnectPoll gates the target_session_attrs
probes ("SHOW transaction_read_only" and "SELECT pg_is_in_recovery()") on
PQntuples(res) == 1 but never on PQnfields(res), then does
strncmp(PQgetvalue(res, 0, 0), ...). One row and zero columns gives
strncmp(NULL, ...). The patch requires PQnfields(res) == 1 too, so a bad
reply
takes the existing "Something went wrong" error path and the connection
attempt fails.

0002 - pg_basebackup, receivelog.c. The TIMELINE_HISTORY shape check logs a
warning and falls through to writeTimeLineHistoryFile(PQgetvalue(res, 0, 0),
PQgetvalue(res, 0, 1)), which calls strlen() on the NULL right away. The
patch
makes the check fatal (pg_log_error, PQclear, return false), matching the
other error returns in that block. It has been like this since cc8d4151172.

Tested on master at 0607aa6109 with clang 21 and
-fsanitize=address,undefined
against a small fake server on loopback: both crash before the patch, both
report a clean error after it, and the well-formed control paths still work
(libpq connects, pg_receivewal writes the history file and moves on to
START_REPLICATION). Both fixes apply cleanly to the back branches as far as
I checked, but I have only run them on master.

Regards,
s1ko

Attachments:

t258363_1
v1-0001-libpq-check-column-count-before-reading-target_se.patchtext/x-patch; charset=UTF-8; name=v1-0001-libpq-check-column-count-before-reading-target_se.patchDownload+2-3
v1-0002-pg_basebackup-fail-on-malformed-TIMELINE_HISTORY-.patchtext/x-patch; charset=UTF-8; name=v1-0002-pg_basebackup-fail-on-malformed-TIMELINE_HISTORY-.patchDownload+4-3
#2Chao Li
li.evan.chao@gmail.com
In reply to: s1ko (#1)
Re: [PATCH] Fix two client-side NULL dereferences on malformed result sets (libpq, pg_receivewal)

On Nov 9, 2026, at 01:59, s1ko <s1ko@riseup.net> wrote:

Hi,

Two client programs crash when the server answers an internal query with a
result set of an unexpected shape. Both are plain NULL dereferences: the code
checks part of the shape, then calls PQgetvalue() on a cell that does not
exist. (First sent to security@, Noah pointed me here since the project does
not treat these as security issues.)

0001 - libpq, fe-connect.c. PQconnectPoll gates the target_session_attrs
probes ("SHOW transaction_read_only" and "SELECT pg_is_in_recovery()") on
PQntuples(res) == 1 but never on PQnfields(res), then does
strncmp(PQgetvalue(res, 0, 0), ...). One row and zero columns gives
strncmp(NULL, ...). The patch requires PQnfields(res) == 1 too, so a bad reply
takes the existing "Something went wrong" error path and the connection
attempt fails.

0002 - pg_basebackup, receivelog.c. The TIMELINE_HISTORY shape check logs a
warning and falls through to writeTimeLineHistoryFile(PQgetvalue(res, 0, 0),
PQgetvalue(res, 0, 1)), which calls strlen() on the NULL right away. The patch
makes the check fatal (pg_log_error, PQclear, return false), matching the
other error returns in that block. It has been like this since cc8d4151172.

Tested on master at 0607aa6109 with clang 21 and -fsanitize=address,undefined
against a small fake server on loopback: both crash before the patch, both
report a clean error after it, and the well-formed control paths still work
(libpq connects, pg_receivewal writes the history file and moves on to
START_REPLICATION). Both fixes apply cleanly to the back branches as far as
I checked, but I have only run them on master.

Regards,
s1ko<v1-0001-libpq-check-column-count-before-reading-target_se.patch><v1-0002-pg_basebackup-fail-on-malformed-TIMELINE_HISTORY-.patch>

For 0001, the change is in libpq. I think we should accept the patch to make libpq more robust, for the following reasons:

1) PG allows a query to return zero columns, for example:
```
evantest=# select;
--
(1 row)
```

2) libpq can communicate with different PG protocol implementations, such as a normal PG server, a PG-compatible database server, or a pooler like PgBouncer. A buggy or malicious implementation could return an unexpected result.

3) With 0001, if either of the two internal queries returns zero columns, execution falls through to the existing error-handling path, which calls libpq_append_conn_error(), instead of crashing.

So 0001 looks good to me.

For 0002, since the existing code already checks if PQnfields(res) != 2, we should definitely not proceed to call writeTimeLineHistoryFile() when the response has an unexpected number of columns, as that function needs to read the first two columns.

So 0002 also looks good to me.

One related issue I noticed while reviewing 0002 is that writeTimeLineHistoryFile() returns a bool result for success or failure, but ReceiveXlogStream() ignores the return value. This might be outside the scope of this patch. I'll investigate it further and may start a separate thread to discuss the issue.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#2)
Re: [PATCH] Fix two client-side NULL dereferences on malformed result sets (libpq, pg_receivewal)

On Tue, Nov 10, 2026 at 5:57 PM Chao Li <li.evan.chao@gmail.com> wrote:

So 0002 also looks good to me.

Both patches look good to me too.
We should backpatch them to all supported versions?

BTW, there seem similar missing column-count checks in BaseBackup()
in pg_basebackup.c:

- The backup start and end position results are checked for one row,
but not for at least one column, before passing column 0 to strlcpy().
- The tablespace header is checked for rows, but not for at least
three columns, before passing column 2 to atoll().

Malformed responses could therefore cause NULL dereferences here too.
We should fix these separately?

Regards,

--
Fujii Masao

#4Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#3)
Re: [PATCH] Fix two client-side NULL dereferences on malformed result sets (libpq, pg_receivewal)

On Nov 11, 2026, at 14:46, Fujii Masao <masao.fujii@gmail.com> wrote:

On Tue, Nov 10, 2026 at 5:57 PM Chao Li <li.evan.chao@gmail.com> wrote:

So 0002 also looks good to me.

Both patches look good to me too.
We should backpatch them to all supported versions?

I think that would be great.

BTW, there seem similar missing column-count checks in BaseBackup()
in pg_basebackup.c:

- The backup start and end position results are checked for one row,
but not for at least one column, before passing column 0 to strlcpy().
- The tablespace header is checked for rows, but not for at least
three columns, before passing column 2 to atoll().

Malformed responses could therefore cause NULL dereferences here too.
We should fix these separately?

I just checked BaseBackup(). There are 3 places to add the check. How about the attached fix? After adding the column count check, we need to change the original error message in form of “got %d rows and %d fields, expected %d rows and %d fields”.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

nocfbot_basebackup.diffapplication/octet-stream; name=nocfbot_basebackup.diff; x-unix-mode=0644Download+9-7
#5Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#4)
Re: [PATCH] Fix two client-side NULL dereferences on malformed result sets (libpq, pg_receivewal)

On Wed, Nov 11, 2026 at 5:54 PM Chao Li <li.evan.chao@gmail.com> wrote:

How about the attached fix?

Thanks for the patch!
I've incorporated it into s1ko's 0002 patch and pushed all the patches.

Regards,

--
Fujii Masao