Fix infinite loop from setting scram_iterations to INT_MAX

Kevin K Biju <kevinkbiju@gmail.com>

From: Kevin K Biju <kevinkbiju@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Date: 2025-03-23T13:41:10Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix integer-overflow problem in scram_SaltedPassword()

Attachments

Hi,

I stumbled upon a problem with the scram_iterations GUC where setting
scram_iterations to INT_MAX and then creating a user causes the command to
hang indefinitely.

postgres=# SET scram_iterations=2147483647;
SET
postgres=# CREATE ROLE maxscram WITH PASSWORD 'forever';
<hangs>

I looked into the relevant code and found the issue. Each SCRAM iteration
after the first is done in a loop with the following condition:

int i;
...
for (i = 2; i <= iterations; i++)
{
    ...
}

For iterations = INT_MAX, the loop will never terminate since the condition
is <= and adding 1 to INT_MAX will lead to i wrapping around to INT_MIN.

I've fixed this by modifying the loop condition to be i < iterations. I've
attached a patch with the fix. I considered adding a test as well, but
since generating a password with a high number of iterations is very
time-consuming, I'm not sure if that would be practical.

I also tried adding this to the current CommitFest, but my account hasn't
passed the cooldown period yet.

Thanks,
Kevin