[PATCH] pgcrypto: Fix sha-crypt rounds= values that do not fit in int (BUG #19743)

Started by Prashant Kumar1 day ago1 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t254356
psql -h localhost -U postgres

Built from patchset v1 (message #1), October 24, 2026 at 02:40 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t254356_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t254356_1 && git checkout t254356_1

Patchset v1 (message #1) is on t254356_1

Jump to latest
#1Prashant Kumar
crprashant@hotmail.com

Hi hackers,

Attached is a patch for BUG #19743, reported by Ke Han [1]/messages/by-id/19743-c48782d257b89819@postgresql.org:
pgcrypto's crypt() parses the rounds= option of a sha256crypt /
sha512crypt salt with strtoint() and never checks errno, so a
value that does not fit in an int is silently reduced modulo
2^32 before the supported-range clamping runs. For example,
rounds=4294968296 produces a hash computed with the minimum of
1000 rounds and no NOTICE at all, and rounds=4294967295 is
reported as "rounds=-1".

I reproduced this on current master (20devel) before fixing it.

The patch checks errno around the strtoint() call in
px_crypt_shacrypt(). On ERANGE the value is out of range in
the direction of its sign, so it is clamped to
PX_SHACRYPT_ROUNDS_MIN or PX_SHACRYPT_ROUNDS_MAX, matching the
existing clamp-with-NOTICE behaviour for in-range values, and
the NOTICEs now report the value as supplied instead of the
truncated conversion result.

Regression tests are added to crypt-shacrypt: negative overflow
cases for both $5$ and $6$, checked against full hashes, and
positive overflow cases run under a short statement_timeout so
the tests never actually compute ~1e9 rounds. The pgcrypto
regression suite (25 tests) and the core regression suite
(239 tests) both pass with the patch applied.

This is my first patch submission; feedback is welcome.
Testing was done with AI assistance.

[1]: /messages/by-id/19743-c48782d257b89819@postgresql.org

Thanks,
Prashant Chinnam

Attachments:

t254356_1
v1-0001-pgcrypto-Fix-sha-crypt-rounds-values-that-do-not-fit.patchapplication/octet-stream; name=v1-0001-pgcrypto-Fix-sha-crypt-rounds-values-that-do-not-fit.patchDownload+139-3