Secure code review

Auditing concurrency, shared state and race conditions

Trusted records: terminal, document, eye arranged to illustrate auditing concurrency, shared state and race conditions.

Secure coding requires deliberate trust boundaries, defensive behaviour and safe handling of shared state. Understanding concurrency and shared state and race conditions helps establish which security property the feature needs and where that property is actually enforced.

What does this involve?

Business logic security checks whether the application preserves the intended rules across a complete sequence of actions. State transitions, ownership, approval and retry handling can matter more than the validity of any individual input.

Concurrency issues such as race conditions, time-of-check to time-of-use (TOCTOU) vulnerabilities, deadlocks, livelocks, thread starvation, and improper synchronisation can lead to unpredictable behaviour and security risks. This section includes various techniques and strategies to help mitigate these risks.

Why it matters

Consider a realistic failure: two individually valid code paths combine to violate an important application permission or transaction assumption.

Legitimate features can cause harm when used out of sequence, repeatedly or under an identity that should not control the current state. Financial loss, inventory disruption and unauthorised changes depend on the particular workflow.

A code audit connects a stated requirement to the implementation responsible for enforcing it. Source review and targeted behavioural checks should explain the same boundary and any relevant exceptions.

How penetration testers check for it

Review the specific control expectations below against the code, deployment and observable application behaviour.

  • Confirm that shared objects in multi-threaded code are accessed safely by using thread-safe types and synchronisation mechanisms like locks or semaphores to avoid race conditions and data corruption.
  • Confirm that checks on a resource's state, such as its existence or permissions, and the actions that depend on them are performed as a single atomic operation to prevent time-of-check to time-of-use (TOCTOU) race conditions. For example, checking if a file exists before opening it, or verifying a user’s access before granting it.
  • Confirm that locks are used consistently to avoid threads getting stuck, whether by waiting on each other or retrying endlessly, and that locking logic stays within the code responsible for managing the resource to ensure locks cannot be inadvertently or maliciously modified by external classes or code.
  • Confirm that resource allocation policies prevent thread starvation by ensuring fair access to resources, such as by leveraging thread pools, allowing lower-priority threads to proceed within a reasonable timeframe.
  • Compare ownership and approval across related operations.
  • Inspect committed outcomes and background side effects.

How to fix it

Implement the required boundary in the component that actually performs the sensitive operation and retain focused regression coverage.

  • Represent important rules explicitly on the server.
  • Bind approvals and state changes to trusted values.
  • Use atomic operations where competing requests affect the same decision.
  • Retain regression cases for invalid transitions and retries.

A behaviour is not automatically a vulnerability because it is unusual. The finding needs a clear violated business rule and a meaningful unauthorised consequence.

Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.

Read about our methodology or contact pentested.com to discuss the security of your application code.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo