Repository navigation
logs: refuse symlinks when opening the log file (O_NOFOLLOW) - #323
Merged
Merged
Conversation
Add O_NOFOLLOW to the open() call in logFile() so that if an attacker
plants a symlink at the path passed via --log (or via the log_file
config), nsjail does not silently follow it and append log lines to
the symlink target.
Repro (before fix):
$ touch /tmp/target
$ ln -s /tmp/target /tmp/log
$ nsjail -l /tmp/log -- /usr/bin/true
$ cat /tmp/target # log lines appended
Repro (after fix):
$ nsjail -l /tmp/log -- /usr/bin/true
[W] logFile(): Couldn't open('/tmp/log'): Too many levels of symbolic links
$ cat /tmp/target # unchanged
Matches the O_NOFOLLOW convention used elsewhere in nsjail (e.g.
createDirRecursively in util.cc) and in runc / Docker / systemd-nspawn
for log and cgroup file writes.
Contributor
Author
|
Following up on this PR — it's been open for two weeks without review. The change is narrow (one file, one line) and introduces no behavior change for valid operator configurations; . happy to coordinate or close in favor of whichever approach you'd prefer. Please let me know if any changes are needed. |
Contributor
Author
|
Hi, just checking in on this. It's been open since Sep 3. |
Collaborator
|
Thank you. Yes, not a security problem imo (no crossing of privilege boundaries, unless the admin/user uses problematic paths), but sure, as a defense-in-depth we can have it. |
zerox80
pushed a commit
to zerox80/nsjail
that referenced
this pull request
Oct 16, 2026
logs: refuse symlinks when opening the log file (O_NOFOLLOW)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
O_NOFOLLOWto theopen()call inlogFile()so that nsjail does not silently follow a symlink at the user-supplied--log/log_filepath.Defect
logs.cc:logFile()opens the log file withO_CREAT | O_RDWR | O_APPEND | O_CLOEXECbut noO_NOFOLLOW. If an attacker can pre-plant a symlink at the log path (race condition in /tmp, shared tmp dir, predictable path), nsjail follows the symlink and appends log lines to the symlink target viaO_APPEND. The existingPLOG_Wwarning is logged but the write still happens against the target file.Repro (before fix)
$ touch /tmp/target $ ln -s /tmp/target /tmp/log $ nsjail -l /tmp/log -- /usr/bin/true $ cat /tmp/target # log lines appended to /tmp/targetRepro (after fix)
Runtime-verified on nsjail HEAD (f100fd9) on 6-core Linux.
Fix
2 insertions, 1 deletion.
logs.cconly. Matches theO_NOFOLLOWconvention used elsewhere in nsjail (createDirRecursivelyinutil.cc) and in runc / Docker / systemd-nspawn for log and cgroup file writes.Impact
Defense-in-depth hardening. Not a vulnerability in the strict sense, but a missing standard hardening that matches the convention used by every major container runtime for log / state file writes. The same
util::writeBufToFilefunction is used at the cgroup write sites; this PR addresses the log file path only (the cgroup paths are a follow-up).Notes