pg.ddx.io  pgpool-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tatsuo Ishii <ishii@postgresql.org>
To: pgpool-committers@lists.postgresql.org
Subject: pgpool: Reject negative read length in pool_read2.
Date: Tue, 29 Sep 2026 10:50:48 +0000
Message-ID: <E1xBVQ8-00000006DqV-2bmo@gothos.postgresql.org> (raw)

Reject negative read length in pool_read2.

pool_read2() allocates buf2 sized to cp->len + len, consumes that many
bytes from the pending buffer, then loops reading from the socket
while len > 0. When len is zero or negative the loop never executes a
fresh read and the function falls straight through to "return
cp->buf2", handing the caller a pointer to whatever the previous
pool_read2() invocation happened to leave in the per-connection
buffer.

Callers compute len from on-the-wire length fields, e.g.
len = ntohl(hdr) - sizeof(hdr);
and several do so without bounds-checking. A backend that ships a
malformed message with a length smaller than the header (or a
negative value once cast to int32) drives pool_read2() into the
stale-buffer path. The stale bytes are then forwarded to the
frontend as ParameterStatus / ErrorResponse / etc. payloads,
leaking whatever the previous reply contained on this same
backend connection.

Add a guard at the top of pool_read2() that logs and returns NULL when
len < 0, before any buffer setup runs.

When len == 0, just retuns cp->buf2, rather than returning NULL to not
break existing callers relying on the behavior.

Reported-by: Emond Papegaaij <emond.papegaaij@gmail.com>
Reported-by: Claude code
Author: Tatsuo Ishii <ishii@postgresql.org>
Discussion: https://github.com/pgpool/pgpool2/issues/168
Backpatch-through: v4.3

Branch
------
V4_3_STABLE

Details
-------
https://git.postgresql.org/gitweb?p=pgpool2.git;a=commitdiff;h=5b5eafae9bd85e73fa5dd01ce180655af93fc...

Modified Files
--------------
src/utils/pool_stream.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)



view thread (6+ messages)  latest in thread

Message-ID: <E1xBVQ8-00000006DqV-2bmo@gothos.postgresql.org>
Permalink:  ../E1xBVQ8-00000006DqV-2bmo@gothos.postgresql.org/
Also on:    postgresql.org/message-id/E1xBVQ8-00000006DqV-2bmo@gothos.postgresql.org

 · 

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: pgpool-committers@postgresql.org
  Cc: ishii@postgresql.org
  Subject: Re: pgpool: Reject negative read length in pool_read2.
  In-Reply-To: <E1xBVQ8-00000006DqV-2bmo@gothos.postgresql.org>
  References: <E1xBVQ8-00000006DqV-2bmo@gothos.postgresql.org>

* 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