O_TMPFILE Removes the Filename, Not the File-Descriptor Risk
O_TMPFILE creates an unnamed regular file in a directory. The file remains usable through its open descriptor and disappears when the last descriptor closes unless it is linked into the filesystem. This is useful for temporary secrets, upload staging, and crash-safe handoffs that do not need a global pathname. It is not a guarantee that the data is protected from every other process.
The Linux open manual documents the required directory argument and the O_RDWR or O_WRONLY flag. It also explains that the file is lost when the final descriptor closes, unless the application gives it a name. linkat with an empty path, or a path through /proc/self/fd, can make the inode permanent when the filesystem and flags permit it. O_EXCL prevents that linking step.
The descriptor is the authority
An unnamed file has no directory entry to search for, but a process with the descriptor can read, write, seek, or pass it through a Unix socket. Inheritance across fork or exec can broaden access beyond the component that created it. So can a supervisor that scans /proc/<pid>/fd or a debugger with sufficient privilege.
The design should decide whether the file is a private scratch area or a handoff object. Private scratch data should be closed by the process that owns it and should not be passed to helpers. A handoff should use an explicit descriptor protocol with credential checks and a documented lifetime. Do not assume that removal of the pathname makes the content safe to leave open indefinitely.
Keep the filesystem assumptions visible
O_TMPFILE requires support from the underlying filesystem. A fallback path such as a randomized file in the same directory may be needed, but it has different failure and cleanup behavior. When the application links the file into place, the directory permissions, mount options, quotas, and available space all become part of the operation.
The unnamed state also does not encrypt the data. Pages may be written to storage and may remain in the page cache until reclaimed. If a loss of power or a process crash can leave remnants, the threat model should rely on full-disk encryption, filesystem-level policy, or a separate secret-management design rather than the absence of a filename.
Test the complete lifecycle
Write a small test that creates the file, closes an unrelated descriptor, passes the expected descriptor to a worker, links the file when the workflow requires persistence, and confirms the resulting mode and owner. Test the error path where the filesystem rejects O_TMPFILE or where a linking operation is denied. Verify that logs do not print a descriptor path or file content.
For service code, review the close-on-exec flag, inherited descriptor limits, and signal handlers. A leaked descriptor can keep data alive after the workflow appears complete. A descriptor that is closed too early can turn an intended atomic handoff into a partial write.
Limits
Unnamed temporary files are a useful primitive for reducing path-based races. They do not provide authorization, confidentiality after a crash, or a complete deletion guarantee. Use them where the application can define descriptor ownership precisely, and pair them with a storage and secret-management model that covers the data outside the process lifetime.

