TOCTOU
PronunciationTOK-too
Definition
TOCTOU (time-of-check to time-of-use) is a race-condition bug where something is verified, then changes before it is used, for example a file that is checked as safe and swapped before it is opened.
Where you hear it
In secure coding guides, file handling code, security audits and CVE write-ups.
Examples
if os.path.exists(f): open(f)has a TOCTOU gap.Open the file once and act on the handle; don't check by path first.
The check of the file owner happens before the open, which creates a TOCTOU gap.
Common mistake
Checking permissions or existence first and acting afterwards. Make the check and the action one atomic step.
Don't confuse with
A general race condition. TOCTOU is the security-relevant case where the gap lets an attacker change the object.
Say it at work
- In a meeting or chat
That's a TOCTOU bug.
- In an email or ticket
Use
O_EXCLso creation and check are one operation.