Dispatch pre-auth cancel requests until _exit(1) handlers are disarmed

BackendInitialize() collects the startup packet with two facilities able
to exit() early:
- StartupPacketTimeoutHandler(), timeout enabled for the startup
process, via StartupPacketTimeoutHandler()
- process_startup_packet_die(), for SIGTERM

Both assume that shared memory is never touched, meaning that a hard
exit() is fine, as no cleanup action need to be taken.

9d9b9d46f3c5 broke this assumption by making the cancel request handling
happen earlier in the pre-authentication process: SendCancelRequest()
can acquire the spinlock "pss_mutex" to compare cancel keys.  If SIGALRM
(authentication_timeout) or SIGTERM is received while "pss_mutex" is
acquired, the process would exit with a spinlock still held.  The next
process touching the same proc slot as the one that has exited earlier
would wait indefinitely for the spin lock, crashing the server on PANIC
after one minute of wait.

This commit changes the cancel request handling so as a request received
is stashed instead of immediately processed.  Its processing is delayed
once it is safe to do so, where the startup timeout and SIGTERM handler
are cleared.

Reported-by: Anthropic OSS program
Author: Michael Paquier <[email protected]>
Reviewed-by: Heikki Linnakangas <[email protected]>
Backpatch-through: 18
Security: CVE-2026-116322

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/e1cc4a89318d919d3875443fd63704634aad21d4
Author: Michael Paquier <[email protected]>

Modified Files
--------------
src/backend/tcop/backend_startup.c | 54 +++++++++++++++++++++++++++++---------
src/tools/pgindent/typedefs.list   |  1 +
2 files changed, 43 insertions(+), 12 deletions(-)

Reply via email to