fix(#25821): refuse to chmod top-level dirs from 5 token-storage call sites#27384
Closed
YonganZhang wants to merge 1 commit into
Closed
fix(#25821): refuse to chmod top-level dirs from 5 token-storage call sites#27384YonganZhang wants to merge 1 commit into
YonganZhang wants to merge 1 commit into
Conversation
…storage call sites Fixes NousResearch#25821. `os.chmod(path.parent, 0o700)` is called on a derived path at five token-storage sites without checking that `path.parent` is a sane directory. If anything makes the resolution land at `/` (e.g. `HERMES_HOME=/`, an env-var concat bug, or a path whose `.parent.parent == .parent`), chmod silently succeeds and strips traversal permission from the root inode, bricking the host: every non-root user fails any path lookup with `EACCES`, taking out systemd-resolved, systemd-networkd, syslog, every Docker container that drops privileges, and graceful reboot. Root keeps working (CAP_DAC_OVERRIDE) so the symptom cascades for hours before recovery via rescue mode. (See the issue body for the full incident timeline.) This adds `_secure_dir_safe(path)` to `hermes_cli/config.py` (next to the existing `_secure_dir`) that: * Resolves the path first (so symlink games can't smuggle `/` past a string check). * Refuses single-component paths (`len(parts) < 2`), the filesystem anchor (`Path("/")`, `C:\\`), and a blocklist of known system roots (`/etc`, `/var`, `/usr`, `/home`, `/root`, `/opt`, `/tmp`, `/sys`, `/proc`, `/dev`, `/boot`, `/lib`, `/lib64`, `/run`, `/srv`, `/mnt`, `/media`). * Always uses 0o700, no `HERMES_HOME_MODE` override — token stores must not be traversable regardless of deployment profile. * Honors `is_managed()` for parity with the existing `_secure_dir` (NixOS module sets group-readable permissions). Switches all five identified call sites to use the new helper: * `tools/mcp_oauth.py` (MCP OAuth token storage) * `agent/google_oauth.py` (Google/Gemini OAuth) * `hermes_cli/auth.py` ×3 (Hermes auth store, Qwen CLI tokens, `HERMES_SHARED_AUTH_DIR` nous shared token) A misresolved `HERMES_HOME` is still a bug worth diagnosing — strict-mode logger.error on top-level resolution is left as a follow-up. The priority here is to make the catastrophic outcome ("brick the host silently") unreachable from any of these five sites. `cron/jobs.py` has its own local `_secure_dir` helper that should probably be unified with this one, but is intentionally out of scope for this PR (its callers operate on `CRON_DIR`/`OUTPUT_DIR` which are not derived from user-controlled env vars, so the bricking trigger does not apply there today). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Collaborator
Author
|
Closing as duplicate. Apologies for not running I'll add an observation on #25841 in case the helper's blocklist is worth widening. |
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 #25821.
The original issue describes a production outage caused by
os.chmod(path.parent, 0o700)succeeding silently against/and stripping traversal permission from the root inode — bricking every non-root user on the host. Root cause took 5+ hours to isolate.There are five call sites with the same shape:
tools/mcp_oauth.py— MCP OAuth token storageagent/google_oauth.py— Google / Gemini OAuthhermes_cli/auth.py×3 — Hermes auth store, Qwen CLI tokens,HERMES_SHARED_AUTH_DIRshared tokenAll five become unsafe the moment a derived path resolves to a top-level directory (empty
HERMES_HOME, env-var concat bug, etc.).What this PR does
Adds
_secure_dir_safe(path)tohermes_cli/config.py, next to the existing_secure_dir. It:/past a string check.len(parts) < 2), the filesystem anchor (Path("/"),C:\), and a blocklist of known system roots:/etc /var /usr /home /root /opt /tmp /sys /proc /dev /boot /lib /lib64 /run /srv /mnt /media.0o700— noHERMES_HOME_MODEoverride, because token stores must not be group-readable regardless of deployment profile.is_managed()for parity with the existing helper (NixOS module sets group-readable perms intentionally).Switches all five call sites to use the new helper. They each lose the surrounding
try / except OSError / passboilerplate because_secure_dir_safealready swallowsOSError/NotImplementedErrorinternally.Behavior trip-tests (inline Python)
path.parentresolves to/home/alice/.hermes/auth//etc/tmp/x→//, refused ✅/home(single mid-level)/home/*for everyone 🔴C:\(Windows anchor)~/.hermes/authOSErrorswallowedScope
cron/jobs.pyhas its own local_secure_dirthat should probably be unified with the new safe variant, but is intentionally out of scope for this PR — its callers operate onCRON_DIR/OUTPUT_DIR, neither of which is derived from user-controlled env vars, so the bricking trigger doesn't apply there today. Happy to follow up with a unify-helpers PR if maintainers want it cleaned in one direction.Related
_secure_dir()work on~/.hermesitself; left the five sites above untouched.The issue author offered to send a PR — opening this one in case it's still useful. Happy to iterate on the helper signature or scope as needed.