...
POSIX defines the O_NONBLOCK flag to open(), which ensures that delayed operations on a file do not hang the program [Open Group 2004IEEE Std 1003.1:2013].
When opening a FIFO with
O_RDONLYorO_WRONLYset:
- If
O_NONBLOCKis set, anopen()for reading-only shall return without delay. Anopen()for writing-only shall return an error if no process currently has the file open for reading.- If
O_NONBLOCKis clear, anopen()for reading-only shall block the calling thread until a thread opens the file for writing. Anopen()for writing-only shall block the calling thread until a thread opens the file for reading.When opening a block special or character special file that supports non-blocking opens:
- If
O_NONBLOCKis set, theopen()function shall return without blocking for the device to be ready or available. Subsequent behavior of the device is device-specific.- If
O_NONBLOCKis clear, theopen()function shall block the calling thread until the device is ready or available before returning.Otherwise, the behavior of
O_NONBLOCKis unspecified.
...
This code contains an intractable TOCTOU (time-of-check, time-of-use) race condition under which an attacker can alter the file referenced by file_name following the call to lstat() but before the call to open(). The switch will be discovered after the file is opened, but opening the file cannot be prevented in the case where this action itself causes undesired behavior.
...
Although it may be tempting to use the Win32 GetFileType() function, it is a dangerous API to use in this case. If the file name given identifies a named pipe, and that pipe is currently blocking on a read request, the call to GetFileType() will block until the read request completes. An attacker can then effectively launch a denial-of-service attack on the application. Furthermore, the act of opening a file handle may cause further action to be taken, such as line states being set to their default voltage when opening a serial device.
...
| [Garfinkel 1996] | Section 5.6, "Device Files" | ||
| [IEEE Std 1003.1:2013] | XSH, System Interfaces, open | ||
| [Howard 2002] | Chapter 11, "Canonical Representation Issues" | [Open Group 2004] | open() |
...