Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDhA5-00000000efI-1DmJ for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 11:47:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xDhA4-00000009uDq-1Ok5 for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 11:47:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDEVB-0000000434E-0LLA for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:11:09 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDEV8-00000000JNU-32eg for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:11:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=G7nuvGvlTgbVLiX5rS/fyAj6hss2HtRhSxMypvHZfK8=; b=Y0NYLjcmFaDiXoroNkYx0wnKjR c/NDYAtrGjz+X4pq+Kts4XPGGVUOXiWjR67mlbJDFSC0dY/TghM0O0VkWz5jW9mESzXfSOEtPvXqG QZl3fbJsYXQ2Gz9DqOo8seNDNyvjMFqq5Y9CYIi6HU+QABXs6C0WGUb+9l3wYfhp1HkSXDZcEYqoT aUv6bb6ouSGcStY/iuBFs1aYVe8yEKnBTP4Q9YmNWArMnutoLEsqoDdWhYj8APPP33h/CnV1DXOcA v7Tqeei/DG+j3/5pvHlHSVkF9hYYyAyWdmz7gR3SbXRdfX7wFu/7kUlRdXavqgXFjBBCD7AG1Q+9n J81AViYg==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDEV7-00000000uZ9-1GdT for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:11:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xDEV5-00000002RUK-2JXc for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:11:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19743: pgcrypto crypt(): sha-crypt rounds= silently wraps modulo 2^32 To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: kehan5800@gmail.com Reply-To: kehan5800@gmail.com, pgsql-bugs@lists.postgresql.org Date: Sun, 04 Oct 2026 05:10:34 +0000 Message-ID: <19743-c48782d257b89819@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19743 Logged by: Ke Email address: kehan5800@gmail.com PostgreSQL version: 18.6 Operating system: ubuntu 22 Description: =20 px_crypt_shacrypt() (contrib/pgcrypto/crypt-sha.c:189 on master) parses the rounds=3D option of a caller-supplied sha256crypt/sha512crypt salt with int srounds =3D strtoint(num, &endp, 10); if (*endp !=3D '$') ereport(ERROR, ... "could not parse salt options"); strtoint() (src/common/string.c) signals overflow only by setting errno to ERANGE and returns the value cast to int anyway. This caller never looks at errno, so srounds is the request after a signed 32-bit truncation, and the clamp-with-NOTICE block that follows (crypt-sha.c:209-226) operates on that truncated number. The comment above that block says "We don't do this silently and print a NOTICE in such a case", but for inputs >=3D 2^32 the "exceeds maximum" branch can never fire. Reproduction (any build with --with-openssl): CREATE EXTENSION pgcrypto; -- 2^32 + 1000: computed with 1000 rounds, no NOTICE at all SELECT crypt('password', '$5$rounds=3D4294968296$saltsalt$'); crypt --------------------------------------------------------------------- $5$rounds=3D1000$saltsalt$azOwbpkvuuBKkE82dQPwTsQE8JyT9Fflpr9aKid3aT9 SELECT crypt('password', '$5$rounds=3D1000$saltsalt$') =3D crypt('password', '$5$rounds=3D4294968296$saltsalt$') AS same_= hash; same_hash ----------- t -- 2^32 - 1: the NOTICE names a number the caller never wrote SELECT crypt('password', '$5$rounds=3D4294967295$saltsalt$'); NOTICE: rounds=3D-1 is below supported value (1000), using 1000 instead -- control: a value that fits in int is clamped and warned about SET statement_timeout =3D '2s'; SELECT crypt('password', '$5$rounds=3D1000000000$saltsalt$'); NOTICE: rounds=3D1000000000 exceeds maximum supported value (999999999= ), using 999999999 instead The effective count is read back from the returned hash string, which records the rounds actually used. Walking the 2^32 boundary on master: requested NOTICE effective 4294966296 "rounds=3D-1000 is below supported value" 1000 4294967295 "rounds=3D-1 is below supported value" 1000 4294967296 "rounds=3D0 is below supported value" 1000 4294967297 "rounds=3D1 is below supported value" 1000 4294968295 "rounds=3D999 is below supported value" 1000 4294968296 (none) 1000 4294968300 (none) 1004 8589936592 (none) 2000 Expected: a rounds=3D value that does not fit in int is treated as out of range, i.e. clamped to PX_SHACRYPT_ROUNDS_MAX (999999999) with the existing "exceeds maximum supported value" NOTICE (or rejected), and the NOTICEs report the value the caller supplied. Actual: the value is reduced modulo 2^32; when the remainder lands inside [1000, 999999999] the hash is computed at that work factor with no diagnostic at all, so rounds=3D4294968296 yields a 1000-round hash, the minimum. gen_salt('sha256crypt', n) cannot produce such a salt (its argument is int4), so the path is a hand-written or imported salt string, i.e. the path used to verify or re-hash a hash produced elsewhere. Drepper's reference implementation, which the comment says is being kept compatible, parses rounds with strtoul() and clamps; libxcrypt (Ubuntu 22.04's crypt(3)) refuses this setting string and returns the failure token "*0". Neither reduces it modulo 2^32. Suggested fix: check errno around the call, as other strtoint() callers do: errno =3D 0; srounds =3D strtoint(num, &endp, 10); if (*endp !=3D '$') ereport(ERROR, ...); if (errno =3D=3D ERANGE) srounds =3D (num[0] =3D=3D '-') ? PX_SHACRYPT_ROUNDS_MIN : PX_SHACRYPT_ROUNDS_MAX; (or parse into a wider type) and print the supplied string rather than the truncated int in the two NOTICEs. Related: the integer options of pgp_sym_encrypt() (s2k-count, s2k-mode, compress-level, ...) are parsed with atoi() and wrap the same way; that is already being discussed under BUG #19714, so it is only mentioned here.