fix: TOCTOU allowlist bypass + exec.cwd silently ignored - #101
Merged
Conversation
Bug #1 — TOCTOU symlink-swap allowlist bypass: handler_fs.go read/write operations used the raw p.Path instead of the canonical (symlink-resolved) path returned by checkAllowed. An attacker with filesystem write access could swap a symlink component between the allowlist check and the file operation, reading or writing files outside the permitted roots. Fix: pass canonical to Stat, ReadFile, WriteFile. Bug #2 — exec.cwd silently ignored: handler_exec.go passed the original cwd (or the shell's current cwd) to Shell.Run(callCtx, cwd, p.Command), but SessionAdapter.Run discarded the target parameter entirely. Commands ran in whatever directory the persistent bash session happened to be in. Fix: pass canonical (validated/resolved path) from handler to Shell.Run. SessionAdapter.Run now prepends when target is non-empty, so explicit cwd is honoured. Signed-off-by: Blasius Patrick <blasius.patrick@gmail.com>
Signed-off-by: Blasius Patrick <blasius.patrick@gmail.com>
Signed-off-by: Blasius Patrick <blasius.patrick@gmail.com>
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
Fixes two bugs from the deep peer review of
hermes-node.Bug #1 — TOCTOU symlink-swap allowlist bypass
handler_fs.go:197,223,340used the rawp.PathforStat/ReadFile/WriteFilecalls, butcheckAllowed()returns a symlink-resolvedcanonicalpath. An attacker with filesystem write access could swap a symlink component between the allowlist check and the file operation, bypassing the allowlist.Fix: Pass
canonical(the resolved path fromcheckAllowed) instead ofp.Pathto all three file I/O operations.Bug #2 —
exec.cwdsilently ignoredSessionAdapter.Rundiscarded thetarget/cwdparameter entirely. Commands ran in whatever directory the persistent bash session happened to be in, regardless of thecwdfield in the exec payload.Fix (two parts):
handler_exec.go:249— passescanonical(the resolved, validated working directory) instead of rawcwdtoShell.Run.adapter.go—SessionAdapter.Runnow prependscd <target>when target is non-empty, so the command executes in the caller-intended directory.Build: clean. Tests: 7/7 packages pass, no races.