pgsql: Make cancel request keys longer

From: Heikki Linnakangas <heikki(dot)linnakangas(at)iki(dot)fi>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Make cancel request keys longer
Date: 2025-04-02 13:43:17
Message-ID: E1tzyNB-002IXa-0i@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

Make cancel request keys longer

Currently, the cancel request key is a 32-bit token, which isn't very
much entropy. If you want to cancel another session's query, you can
brute-force it. In most environments, an unauthorized cancellation of
a query isn't very serious, but it nevertheless would be nice to have
more protection from it. Hence make the key longer, to make it harder
to guess.

The longer cancellation keys are generated when using the new protocol
version 3.2. For connections using version 3.0, short 4-bytes keys are
still used.

The new longer key length is not hardcoded in the protocol anymore,
the client is expected to deal with variable length keys, up to 256
bytes. This flexibility allows e.g. a connection pooler to add more
information to the cancel key, which might be useful for finding the
connection.

Reviewed-by: Jelte Fennema-Nio <postgres(at)jeltef(dot)nl>
Reviewed-by: Robert Haas <robertmhaas(at)gmail(dot)com> (earlier versions)
Discussion: https://www.postgresql.org/message-id/508d0505-8b7a-4864-a681-e7e5edfe32aa@iki.fi

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a460251f0a1ac987f0225203ff9593704da0b1a9

Modified Files
--------------
doc/src/sgml/protocol.sgml | 29 +++++-
src/backend/storage/ipc/procsignal.c | 23 ++---
src/backend/tcop/backend_startup.c | 55 ++++++-----
src/backend/tcop/postgres.c | 15 ++-
src/backend/utils/init/globals.c | 5 +-
src/backend/utils/init/postinit.c | 2 +-
src/include/libpq/pqcomm.h | 8 +-
src/include/miscadmin.h | 4 +-
src/include/storage/procsignal.h | 14 ++-
src/interfaces/libpq/fe-cancel.c | 102 +++++++++++++++++----
src/interfaces/libpq/fe-connect.c | 15 ++-
src/interfaces/libpq/fe-protocol3.c | 45 ++++++++-
src/interfaces/libpq/libpq-int.h | 7 +-
.../modules/libpq_pipeline/t/001_libpq_pipeline.pl | 12 ++-
14 files changed, 252 insertions(+), 84 deletions(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2025-04-02 15:13:18 pgsql: Need to do CommandCounterIncrement after StoreAttrMissingVal.
Previous Message Peter Eisentraut 2025-04-02 12:47:19 pgsql: Fix code comment

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2025-04-02 13:48:46 Re: Make query cancellation keys longer
Previous Message George MacKerron 2025-04-02 13:39:06 Re: Making sslrootcert=system work on Windows psql