TOCTOU Attack

TOCTOU Attack Definition
A TOCTOU attack (short for “time-of-check to time-of-use”) is a type of security exploit. It involves an attacker taking advantage of the short delay between the system checking a resource (like a file or permission) and actually using it. In that small gap, the attacker can modify the targeted resource and trick the system into doing something unintended.
How TOCTOU Attacks Work
A TOCTOU attack involves a key flaw in processes like file handling, temporary file creation, or permission checks in operating systems. It relies on the fact that there’s a delay between two critical moments:
- Time of check (TOC): The system checks if a resource is available for use. For example, it checks whether a user has permission to access a file.
- Time of use (TOU): The system uses the checked resource. For example, it opens the file.
A TOCTOU attack takes place in the brief gap that happens between these two steps, where an attacker can quickly replace, modify, or redirect the resource. For example, an attacker may wait for the system program to finish a check and quickly swap the checked file with a malicious one. The program continues to open the malicious file, believing it’s the original file it checked.
Why Are TOCTOU Attacks Dangerous?
TOCTOU attacks exploit timing, which is really hard to predict and properly defend against. They can lead to serious security problems, including:
- Data corruption: Changes made to important files or system settings could cause data loss or even system crashes.
- Privilege escalation: A user might exploit a TOCTOU bug to gain admin-level permissions.
- Unauthorized access: Attackers can bypass security checks to read, modify, or delete files they shouldn’t have access to.
Read More
FAQ
By definition, any process that verifies a resource before using it is a potential target for a TOCTOU attack. Most commonly, attackers target file operations, temporary file handling, or privilege and authorization checks.
No, they can’t. Firewalls and antivirus software focus on blocking outside threats, like malware and suspicious traffic, not internal timing flaws TOCTOU attacks rely on. Preventing TOCTOU attacks requires fixing the timing issues directly, like requiring extra verification at important points in the process.
Yes, they can. If a program stores passwords in a configuration file, an attacker could compromise them with a TOCTOU attack. For example, they might replace the real password file with a fake one, tricking the system into saving new passwords where the attacker can see them.