bug: prevent tmpsh temporary-directory symlink escape - #93
Merged
Conversation
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.
Bug
.local/bin/tmpshcreated its working directory at predictable/tmp/$$.tmpshwithmkdir -p. A pre-created symlink at that path was accepted and followed, so the shell could start inside an attacker-selected directory instead of a private temporary directory.Severity and impact
This is a local security and correctness bug. On a multi-user system another process can predict or race the PID-derived path; even on a single-user machine, a stale or deliberate symlink can redirect a supposedly disposable shell into unrelated files.
Evidence
Relevant code:
.local/bin/tmpsh.The parent implementation performed:
mkdir -psucceeds when the path already resolves through a symlink to an existing directory, andcdfollows that symlink. The unsafe behavior is deterministic when the symlink is created beforetmpshstarts; no successful race is required.Reproduce
Use this temporary, uncommitted procedure:
The
execpreserves the wrapper PID, ensuring the symlink name exactly matches the path used by the parent implementation.Actual result before the fix: the helper starts in
$sandbox/victim, seessentinel, and exits nonzero.Expected result:
tmpshcreates a unique private directory and never enters the pre-created symlink target.Root cause
The temporary directory name was derived directly from a predictable process ID and was created non-atomically with
mkdir -p, which permits reuse of an existing symlink-to-directory path.Fix
Use:
mktemp -datomically creates a unique directory and refuses to reuse an attacker-controlled path. The existing cleanup trap remains in place.Validation
The temporary harness above was run before the committed test artifact was removed:
shellcheckwas not run because it was unavailable in the validation environment.GitHub comparison against
masternow shows only.local/bin/tmpshchanged (2 additions, 2 deletions). No test or reproduction artifact is included in the PR.Scope
Only temporary-directory creation changes. Shell selection, cleanup semantics, command tracing, dependencies, and unrelated scripts are intentionally unchanged. No tests, fixtures, snapshots, repro scripts, logs, or evidence-only files are added or modified.