Race Condition (Limit Overrun)

A limit is checked and then updated in separate steps, so requests sent at the same moment can all pass the check and exceed the limit.

SeverityMedium
CVSS 3.15.3 CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:N/I:H/A:N Open in the calculator
CVSS 4.06 CVSS:4.0/AV:N/AC:L/AT:P/PR:L/UI:N/VC:N/VI:H/VA:N/SC:N/SI:N/SA:N Open in the calculator
CWECWE-367: Time-of-check Time-of-use (TOCTOU) Race Condition
MITRE ATT&CKT1190 Exploit Public-Facing Application
CategoryInsecure Design

Description

The application enforces a limit by first checking it and then recording the action in a separate step, without making the two steps a single atomic operation. When several identical requests arrive at the same moment, each one passes the check before any of them records its effect, so the limit is exceeded. This is a time-of-check to time-of-use race condition, often called limit overrun.

An attacker can therefore obtain more than the application intends to allow. Typical targets are single-use vouchers and gift cards redeemed several times for repeated discounts, credit duplicated or a balance spent or withdrawn twice, one-per-user actions such as votes, trials, referral bonuses or reviews repeated, and counters that protect against brute force, such as limits on sign-in or one-time code attempts, bypassed to allow many more guesses at a password or one-time code than the lockout permits. The financial or integrity impact depends on the limit involved and on how many requests win the race, which is often several per attempt. Exploitation usually requires an account, and the duplicated actions can be hard to distinguish from genuine ones afterwards.

The window between check and update is often only milliseconds, but sending requests in parallel makes it practical to hit: techniques such as the single-packet attack over HTTP/2 make many requests arrive together, and tools such as Burp Suite's Repeater, sending a group of requests in parallel, and Turbo Intruder automate this. The weakness is identified by sending the same limited request many times at once and checking whether more than one succeeds, by comparing balances, counts or records before and after. Automated scanners rarely find it.

Remediation

Make the check and the update a single atomic operation, so that only one request can ever succeed:

  • Put the condition into the update itself and act only if it changed a row, for example a conditional UPDATE that marks a voucher used only where it is still unused, or deducts from a balance only where the balance is sufficient.
  • Otherwise, read and update within one database transaction that locks the row, for example with SELECT ... FOR UPDATE.
  • Use database constraints for one-per-user rules, such as a unique index on the user and the offer, so that duplicate inserts fail.
  • Use atomic counters, such as Redis INCR, for attempt limits, incrementing before the attempt is processed rather than after it fails.
  • Accept an idempotency key on operations that must happen once per request, and reject repeats.
-- Check and update in one statement; proceed only if one row changed UPDATE vouchers SET redeemed_at = CURRENT_TIMESTAMP, redeemed_by = ? WHERE code = ? AND redeemed_at IS NULL;

Confirm the fix by repeating the parallel requests and checking that exactly one succeeds and the others are refused.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding