Overview
A race condition occurs when two or more threads can access shared data and try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you need to consider the order in which the threads will attempt to access the shared data. The result of the change in data is dependent on the thread scheduling algorithm - both threads are “racing” to access/change the data.Shopping Cart Theft Attack
Exploiting race conditions for ‘Shopping cart theft’ is common when inadequate session management has been implemented. The threat actor will send quick succession POST requests with a discount voucher. Because the session management has been poorly designed, the voucher can be applied to the shopping cart multiple times, and goods can be purchased with unintended discounts.Vulnerable Algorithm
Consider the following algorithm run in parallel threads for a shopping cart discount voucher system:| Processor | Thread 1 | Thread 2 |
|---|---|---|
| 01 | BEGIN apply_voucher(v, cart) | |
| 02 | BEGIN apply_voucher(v, cart) | |
| 03 | IF GET voucher_applied() = TRUE | |
| 04 | RETURN | |
| 05 | ENDIF | |
| 06 | IF GET voucher_applied() = TRUE | |
| 07 | RETURN | |
| 08 | ENDIF | |
| 09 | apply_disc(calc_disc(v), cart) | |
| 10 | SET voucher_applied(TRUE) | |
| 11 | apply_disc(calc_disc(v), cart) | |
| 12 | SET voucher_applied(TRUE) | |
| 13 | RETURN render_front_end() | |
| 14 | END apply_voucher | |
| 15 | RETURN render_front_end() | |
| 16 | END apply_voucher |
Secure Implementation
Timing Attack (Advanced)
This complex side channel attack vector involves both race conditions and broken authentication/session management vulnerabilities. Unmanaged race conditions and poor session management allow the successful return token for a public user to be confused with the unsuccessful return token for an administrator user, granting the threat actor authentication with escalated user authorization.DoS Attack
Perform a reverse DNS search and place the infrastructure under load through a DoS attack. Send repeated GET requests at short intervals for all sites hosted on the webserver.
Countermeasures
1. Thread-Safe Design
Consider multithreading in any shared resource process, including:- Discount applications
- Login processes
- Session ID creation
- Payment processing
- Inventory management
2. Implement Proper Locking
3. Secure Session Management
Implement unique session IDs which cannot be brute forced or calculated:4. CSRF Protection
Encrypt all form inputs asynchronously:5. Rate Limiting
Implement rate limiting so a session can only make 1 request per 5 seconds:Best Practices
Race Condition Prevention Checklist
Race Condition Prevention Checklist
- Identify Shared Resources: Map all shared resources (database records, session data, files)
- Implement Proper Locking: Use thread-safe locks with minimal processing time between check and set
- Use Atomic Operations: Leverage database transactions and atomic operations where possible
- Add Rate Limiting: Prevent rapid-fire requests that exploit timing windows
- Secure Sessions: Use cryptographically secure session IDs
- Monitor Unusual Patterns: Watch for repeated requests from the same session/IP
- Test Under Load: Conduct concurrent testing to identify race conditions
Related Vulnerabilities
- Broken Authentication and Session Management
- Cross-Site Request Forgery (CSRF)
- Side-Channel Timing Attacks
- DoS/DDoS Attacks
