With GB18030, prevent SIGSEGV from reading past end of allocation.
authorNoah Misch <noah@leadboat.com>
Mon, 5 May 2025 11:52:04 +0000 (04:52 -0700)
committerNoah Misch <noah@leadboat.com>
Mon, 5 May 2025 11:52:08 +0000 (04:52 -0700)
commitcbadeaca9271a1bade8ef9790bae09dc92e0ed30
tree3d7a9a7b0b36a56d104dbd2b24e58be659f1f387
parent7279e58205813305fc09d2cc9977731c05014b84
With GB18030, prevent SIGSEGV from reading past end of allocation.

With GB18030 as source encoding, applications could crash the server via
SQL functions convert() or convert_from().  Applications themselves
could crash after passing unterminated GB18030 input to libpq functions
PQescapeLiteral(), PQescapeIdentifier(), PQescapeStringConn(), or
PQescapeString().  Extension code could crash by passing unterminated
GB18030 input to jsonapi.h functions.  All those functions have been
intended to handle untrusted, unterminated input safely.

A crash required allocating the input such that the last byte of the
allocation was the last byte of a virtual memory page.  Some malloc()
implementations take measures against that, making the SIGSEGV hard to
reach.  Back-patch to v13 (all supported versions).

Author: Noah Misch <noah@leadboat.com>
Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Backpatch-through: 13
Security: CVE-2025-4207
src/backend/utils/mb/mbutils.c
src/common/jsonapi.c
src/common/wchar.c
src/include/mb/pg_wchar.h
src/interfaces/libpq/fe-exec.c
src/interfaces/libpq/fe-misc.c
src/test/modules/test_escape/test_escape.c