pgsql: Fix handling of invalidly encoded data in escaping functions

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix handling of invalidly encoded data in escaping functions
Date: 2025-02-10 15:09:49
Message-ID: E1thVPw-006NyD-R2@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix handling of invalidly encoded data in escaping functions

Previously invalidly encoded input to various escaping functions could lead to
the escaped string getting incorrectly parsed by psql. To be safe, escaping
functions need to ensure that neither invalid nor incomplete multi-byte
characters can be used to "escape" from being quoted.

Functions which can report errors now return an error in more cases than
before. Functions that cannot report errors now replace invalid input bytes
with a byte sequence that cannot be used to escape the quotes and that is
guaranteed to error out when a query is sent to the server.

The following functions are fixed by this commit:
- PQescapeLiteral()
- PQescapeIdentifier()
- PQescapeString()
- PQescapeStringConn()
- fmtId()
- appendStringLiteral()

Reported-by: Stephen Fewer <stephen_fewer(at)rapid7(dot)com>
Reviewed-by: Noah Misch <noah(at)leadboat(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Backpatch-through: 13
Security: CVE-2025-1094

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/370c94d4cce179736f9a255779eb8e653e1edfb2

Modified Files
--------------
src/fe_utils/string_utils.c | 170 ++++++++++++++++++++++++++++++++---------
src/interfaces/libpq/fe-exec.c | 135 +++++++++++++++++++++++---------
2 files changed, 237 insertions(+), 68 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Andres Freund 2025-02-10 17:30:44 pgsql: Fix type in test_escape test
Previous Message Andres Freund 2025-02-10 15:09:48 pgsql: Specify the encoding of input to fmtId()