A privileged upload service checks that the requesting user may read a file path, then opens that path moments later. A tester shows a user can swap the path for a symbolic link to a protected file between the two steps. Which three changes best remove the flaw? (Choose three.)
Choose 3 answers
Show the answer
Correct answersB. Keep the working files in a directory that unprivileged users cannot write toC. Open the file once and run the checks against the open handle, not the pathF. Drop to the requesting user's privileges before opening the file
AWhy not this: A clean file name does not stop a path from being replaced by a link after the check.
BWhy this is right: If attackers cannot write to the directory, they cannot place the link that wins the race.
CWhy this is right: Checking the open handle ties the check and the use to the same object, so a swap has no effect.
DWhy not this: Encryption protects confidentiality at rest but does not stop the path being swapped.
EWhy not this: Repeating the check still leaves a gap between check and use that an attacker can race.
FWhy this is right: Opening as the user makes the operating system enforce access at the moment of use.
TOCTOU is fixed by removing the gap or the attacker's ability to change the resource: bind the check to the opened object, let the OS enforce the user's rights, and deny write access to the location. A second check only narrows the window and the race remains.