Fix handling of invalidly encoded data in escaping functions
authorAndres Freund <andres@anarazel.de>
Mon, 10 Feb 2025 15:03:39 +0000 (10:03 -0500)
committerAndres Freund <andres@anarazel.de>
Mon, 10 Feb 2025 15:03:39 +0000 (10:03 -0500)
commit92e4170f421832208a75645f4a4ea94494bdad4d
tree98fea6b80aa818e6073f0fed8b4a3be40fab5d7b
parent56aa2dcddeb299f62dad98ba50ad8db47cddaf97
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@rapid7.com>
Reviewed-by: Noah Misch <noah@leadboat.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Backpatch-through: 13
Security: CVE-2025-1094
src/fe_utils/string_utils.c
src/interfaces/libpq/fe-exec.c