Skip to content

Commit 44f6173

Browse files
sean-jackc
authored andcommitted
pgconn: cap server-supplied SCRAM iteration count
recvServerFirstMessage parsed the server-supplied i= value with strconv.Atoi and accepted any positive value. clientFinalMessage then calls pbkdf2.Key(sha256.New, password, salt, sc.iterations, 32) with that value. A malicious server can send i=2000000000, pinning a CPU core for several minutes per connection attempt before any context deadline check (PBKDF2 is not interruptible). Reject iteration counts above 10,000,000 (~2500x PostgreSQL's default of 4096). This makes pgx stricter than libpq, which accepts any positive value. Reported by CrowdStrike (F03).
1 parent 1a976f7 commit 44f6173

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎pgconn/auth_scram.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ func (sc *scramClient) recvServerFirstMessage(serverFirstMessage []byte) error {
284284
if err != nil || sc.iterations <= 0 {
285285
return fmt.Errorf("invalid SCRAM iteration count received from server: %w", err)
286286
}
287+
// Bound server-supplied iteration count to prevent a malicious server from forcing the client
288+
// to spend unbounded CPU in PBKDF2. PostgreSQL's scram_iterations defaults to 4096; this ceiling
289+
// is ~2500x that.
290+
const maxScramIterations = 10_000_000
291+
if sc.iterations > maxScramIterations {
292+
return fmt.Errorf("SCRAM iteration count from server too high: %d (max %d)", sc.iterations, maxScramIterations)
293+
}
287294

288295
if !bytes.HasPrefix(sc.clientAndServerNonce, sc.clientNonce) {
289296
return errors.New("invalid SCRAM nonce: did not start with client nonce")

0 commit comments

Comments
 (0)