Fix integer-overflow problem in scram_SaltedPassword()
authorRichard Guo
Wed, 26 Mar 2025 08:46:51 +0000 (17:46 +0900)
committerRichard Guo
Wed, 26 Mar 2025 08:46:51 +0000 (17:46 +0900)
Setting the iteration count for SCRAM secret generation to INT_MAX
will cause an infinite loop in scram_SaltedPassword() due to integer
overflow, as the loop uses the "i <= iterations" comparison.  To fix,
use "i < iterations" instead.

Back-patch to v16 where the user-settable GUC scram_iterations has
been added.

Author: Kevin K Biju 
Reviewed-by: Richard Guo
Reviewed-by: Michael Paquier
Discussion: https://postgr.es/m/CAM45KeEMm8hnxdTOxA98qhfZ9CzGDdgy3mxgJmy0c+2WwjA6Zg@mail.gmail.com

src/common/scram-common.c

index 400900c51c58410f75a67c6d156eba67312042cc..bfd2a340016cf22bf2d68c8d1a70a27dc63a831c 100644 (file)
@@ -74,7 +74,7 @@ scram_SaltedPassword(const char *password,
    memcpy(result, Ui_prev, key_length);
 
    /* Subsequent iterations */
-   for (i = 2; i <= iterations; i++)
+   for (i = 1; i < iterations; i++)
    {
 #ifndef FRONTEND
        /*