Skip to content

fix(file): refuse device and kernel pseudo-filesystem writes - #159306

Open
c2h5oh wants to merge 3 commits into
mainfrom
fix/156063-pseudo-fs-writes
Open

c2h5oh wants to merge 3 commits into
mainfrom
fix/156063-pseudo-fs-writes

Conversation

@c2h5oh

@c2h5oh c2h5oh commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

write_file, patch and the V4A delete/move paths could write into or unlink /dev, /proc and /sys entries, including through a symlink. The read side already refused these (is_blocked_device_path); the write side had no matching guard. This is §3.2 hardening: the terminal can still do it, but a file tool shouldn't do it by accident or under a prompt injection.

Both write-guard layers (agent/file_safety.py and tools/file_tools_write_guards.py) now refuse a device or kernel path, judged on the resolved target, before any backend I/O. /dev/shm stays writable because it's ordinary tmpfs scratch. The exemption compares the path exactly, without the case-folding the deny list uses, and covers nothing else: /dev/shmx, /dev/shm/../null, a link inside /dev/shm that points at a device, and /dev/mqueue are all refused. The refusal message points to the terminal tool.

The fix is the commit by @kvnloo, which carries GauravPatil2515's report and first fix; authorship is kept and Gaurav is credited as co-author. The follow-up adds the /dev/shm exemption, the terminal hint, and tests for moving into a directory symlinked to /dev and for links inside /dev/shm. The new tests fail on main.

Non-local backends (docker, ssh) are judged on the path text only, as the existing credential guards are, so a symlink already planted inside the container still defeats the guard. Fixing that needs resolution on the backend's side and is out of scope here.

Fixes #156063

kvnloo and others added 2 commits November 23, 2026 18:18
Block /dev, /proc, and /sys in both file-write guard layers, including
symlink-resolved targets, before write/patch/delete/move reach backend I/O.
Add a non-destructive regression with backend I/O deliberately disabled.

Refs #156063
Reported and first fix proposed by GauravPatil2515.

(cherry picked from commit 2acbcba)
(cherry picked from commit ccd24b7)
The device guard refused /dev/shm, ordinary tmpfs scratch that programs
write files into. The exemption compares the path exactly (no case fold)
and covers nothing else under /dev. The write-guard refusal now points at
the terminal like the /etc one, and the test covers a move into a
directory symlinked to /dev.

Fixes #156063

Co-authored-by: GauravPatil2515 <155764126+GauravPatil2515@users.noreply.github.com>
@c2h5oh

c2h5oh commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator Author

@greptileai review

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

૮ >ﻌ< ა ci review

ran on 7799dbc — test(file): pin the path-text half of the device guard (#156

ℹ️ Info

CI caches · View report · View job

25 / 25 cache hits (0 misses)
4155.48 MiB restored
0 B not restored.

per-job cache results

hits

JobStepLogsRestoredΔ vs main
Python tests / Run tests (3/3)Set up locked Python and test dependencies / Restore verified PM tools without savingview step154.75 MiB+0 B
Python tests / Run tests (3/3)Set up locked Python and test dependencies / Restore uv dependency downloads and buildsview step43.14 MiB+0 B
Python tests / Run tests (3/3)Run testsview step0.10 MiB+65 B
Python tests / Run tests (1/3)Set up locked Python and test dependencies / Restore verified PM tools without savingview step154.75 MiB+0 B
Python tests / Run tests (1/3)Set up locked Python and test dependencies / Restore uv dependency downloads and buildsview step43.14 MiB+0 B
Python tests / Run tests (1/3)Run testsview step0.10 MiB-23 B
Repository guards / Repository guardsRun ./.github/actions/setup-pm / Restore verified PM tools without savingview step156.11 MiB+0 B
Repository guards / Repository guardsRun ./.github/actions/setup-pm / Restore uv dependency downloads and buildsview step43.14 MiB+0 B
Repository guards / Repository guardsRun ./.github/actions/setup-pm / Restore node dependency downloadsview step180.90 MiB+0 B
Python tests / Run tests (2/3)Set up locked Python and test dependencies / Restore verified PM tools without savingview step154.75 MiB+0 B
Python tests / Run tests (2/3)Set up locked Python and test dependencies / Restore uv dependency downloads and buildsview step43.14 MiB+0 B
Python tests / Run tests (2/3)Run testsview step0.10 MiBunknown
Python lints / Python lintsRun ./.github/actions/setup-pm / Restore verified PM tools without savingview step83.12 MiB+0 B
OS-specific tests / Windows-only testsSet up locked Python and test dependencies / Restore verified PM tools without savingview step236.13 MiB+0 B
OS-specific tests / Windows-only testsSet up locked Python and test dependencies / Restore uv dependency downloads and buildsview step102.91 MiB+0 B
OS-specific tests / macOS-only tests (1/2)Set up locked Python and test dependencies / Restore verified PM tools without savingview step107.16 MiB+0 B
OS-specific tests / macOS-only tests (1/2)Set up locked Python and test dependencies / Restore uv dependency downloads and buildsview step167.62 MiB+0 B
OS-specific tests / macOS-only tests (2/2)Set up locked Python and test dependencies / Restore verified PM tools without savingview step107.16 MiB+0 B
OS-specific tests / macOS-only tests (2/2)Set up locked Python and test dependencies / Restore uv dependency downloads and buildsview step167.62 MiB+0 B
OS-specific tests / Windows-only tests (arm64, 1/2)Set up native ARM64 build dependencies / Cache ARM64 OpenSSL development filesview step26.23 MiB+0 B
OS-specific tests / Windows-only tests (arm64, 1/2)Set up locked Python and test dependencies / Restore verified PM tools without savingview step222.55 MiB+0 B
OS-specific tests / Windows-only tests (arm64, 1/2)Set up locked Python and test dependencies / Restore uv dependency downloads and buildsview step856.03 MiB+0 B
OS-specific tests / Windows-only tests (arm64, 2/2)Set up native ARM64 build dependencies / Cache ARM64 OpenSSL development filesview step26.23 MiB+0 B
OS-specific tests / Windows-only tests (arm64, 2/2)Set up locked Python and test dependencies / Restore verified PM tools without savingview step222.55 MiB+0 B
OS-specific tests / Windows-only tests (arm64, 2/2)Set up locked Python and test dependencies / Restore uv dependency downloads and buildsview step856.03 MiB+0 B

misses

none

Compressed archive bytes. Not-restored bytes use the main-size reference; missing sizes remain unknown.


debug info

CI timings

CI timings · View report · View job

Wall time 6m52s vs 6m23s (+7.6%). 8 job(s) slower, 3 faster, 1 unchanged.

  • Python lints / Python lints: +100.0s
  • OS-specific tests / Windows-only tests (arm64, 2/2): +28.0s
  • OS-specific tests / Windows-only tests: -26.0s
  • Python tests / Run tests (1/3): +25.0s
  • Python tests / Run tests (3/3): +22.0s

@alt-glitch alt-glitch added P2 Medium — degraded but workaround exists tool/file File tools (read, write, patch, search) type/security Security vulnerability or hardening labels Nov 23, 2026
@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issue was found.

Summary

The PR blocks file-tool changes under /dev, /proc, and /sys, while keeping ordinary /dev/shm scratch files writable.

  • File writes stop at device and kernel pseudo-filesystems.

Reviews (2) · Last reviewed commit: "test(file): pin the path-text half of th..." · Reviewed by Greptile

BearHuddleston
BearHuddleston previously approved these changes Nov 23, 2026

@BearHuddleston BearHuddleston left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: No blockers from me on this head. Three optional nits: a test for the path-text checks, a docs row, and a pre-existing V4A preflight gap for a follow-up.

# Finding Severity Where Status
1 The path-text check in both guard layers has no test (4 of 12 mutants survive) Nit tests/tools/test_file_write_safety.py:1248 Open
2 The security docs' protected-paths table doesn't list the new category (outside the diff) Nit (optional) agent/file_safety.py:99 Open
3 Not a regression: V4A preflight doesn't vet Delete/Move entry coordinates, so an entry refusal can land after earlier ops of the patch applied Nit (optional) tools/file_tools.py:1015 Open
  • Finding 3: a V4A patch that updates a.txt and then deletes <tmp>/devdir/fd/N (devdir -> /dev, fd N open on an ordinary file) passes preflight, because _check_sensitive_path finds no pseudo path in the text or in the followed target. The entry /proc/<pid>/fd/N is refused only at apply time, after a.txt was updated. Nothing is written or unlinked under /proc, and main ends in the same state (there the unlink fails with EACCES). Credential entries have had the same apply-time-only entry check, so this belongs in a follow-up: vetting the entry coordinates (_resolve_entry_for_task) with get_write_denied_error(..., entry=True) during preflight would close it for both.

Thanks for the PR. CI on this head is green: the three Python test shards, Python lints, repository guards, and the macOS-only and Windows-only lanes. The new TestPseudoFilesystemWriteGuard fails 11 of 12 on main, each on an assertion; the twelfth, test_dev_shm_stays_writable, passes on main by construction and fails on 802d3d7, before the exemption. A base-vs-head probe of both guard layers on Linux: main allows every case, and this head refuses /dev/null, /dev, /dev/shmx, /proc/sysrq-trigger, /sys/kernel/debug, a link to /dev/null, and a link to /dev plus a new name inside it, while /dev/shm, /dev/shm/x, /devel/x, /system/x and /sysx stay writable.

Beyond the diff: the ACP file bridge, the dashboard's local Files save and the TTS output_path check get the same refusal through the shared guard. Relative spellings (../../proc/self/cwd/x) and caller-resolved paths skip the text check, but every writer is handed the resolved path (tools/file_tools.py:931, :1032, :1037-1038), so they write the ordinary file they resolve to, same as main and as "judged on the resolved target" says. Three Sol rounds (gpt-6.2-sol) ended in agreement. Besides finding 3 it kept one follow-up: the shared guard resolves a Docker or SSH spelling on the host, so a host path at the same spelling that links into /dev, /proc or /sys would be over-refused, as the credential checks already would.

Details are in the inline comments.


Written and posted by Hermes Agent, Bear's AI agent, without Bear reading it first. An approval from it is never on behalf of hermes-agent-core: where that team's review is requested or required, the approval comes from Bear himself.

finally:
shutil.rmtree(scratch)

@pytest.mark.platforms("posix")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path-text half of both guards has no test: 4 of the 12 mutants I ran survive the new tests.

A /dev or /proc path whose realpath leaves the pseudo roots is refused only by the text check: /dev/fd/N resolves to the open file, /proc/self/cwd/x to <cwd>/x, /dev/stdout to wherever stdout is redirected. Every case in TestPseudoFilesystemWriteGuard resolves into /dev, /proc or /sys, so the resolved-target check alone passes all of them. Mutation run of tests/tools/test_file_write_safety.py on a copy of this head:

  • With the PR's tests: agent/file_safety.py 6 of 9 mutants caught, tools/file_tools_write_guards.py 2 of 3. The survivors drop the text check in _classify_write_denial (lines 394-395), drop the "/" + ….lstrip("/") re-rooting, drop posixpath.normpath, and check only candidates[0] (the resolved path) in _check_sensitive_path.
  • With the test below: 9 of 9 and 3 of 3, 100 passed unmutated, scripts/check --only ruff,health clean.

The precondition asserts the realpath once, on the plain /dev/fd/N spelling, so the /dev/shm/../fd/N row still passes on a host whose /dev/shm is a symlink out of /dev (its text normalizes to /dev/fd/N). Optional:

Suggested change
@pytest.mark.platforms("posix")
@pytest.mark.platforms("linux")
@pytest.mark.parametrize("spelling", ("/dev/fd/{fd}", "//dev/fd/{fd}", "/dev/shm/../fd/{fd}"))
def test_a_dev_path_that_resolves_outside_dev_is_refused_by_its_text(self, tmp_path: Path, spelling):
"""/dev/fd/N resolves to the open file itself, so only the path text can refuse it."""
from tools.file_tools_write_guards import _check_sensitive_path
regular = tmp_path / "regular.txt"
regular.write_text("")
with regular.open() as fh:
assert os.path.realpath(f"/dev/fd/{fh.fileno()}") == os.path.realpath(regular)
path = spelling.format(fd=fh.fileno())
assert _is_write_denied(path)
assert "device or kernel pseudo-filesystem" in (_check_sensitive_path(path) or "")
@pytest.mark.platforms("posix")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as test_a_dev_path_that_resolves_outside_dev_is_refused_by_its_text in 7799dbc, with your three spellings. test_file_write_safety.py passes locally (100 tests).

Comment thread agent/file_safety.py
return target == root or target.startswith(root.rstrip(os.sep) + os.sep)


_PSEUDO_FILESYSTEM_WRITE_ROOTS = ("/dev", "/proc", "/sys")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: the "Protected paths (always blocked)" table in website/docs/user-guide/security.md doesn't list this category or the /dev/shm exemption. That table is where a user looks up why a write was refused, and it already lists the credential stores and the NT-namespace paths. The docs file is outside the diff, so here's a patch instead of a suggestion (git apply --check clean on this head):

diff --git a/website/docs/user-guide/security.md b/website/docs/user-guide/security.md
index a2f53ee935..3415365cd7 100644
--- a/website/docs/user-guide/security.md
+++ b/website/docs/user-guide/security.md
@@ -424,6 +424,7 @@ These categories are always denied, even when `HERMES_WRITE_SAFE_ROOT` is unset:
 | Category | Examples |
 |----------|----------|
 | OS credential stores | `~/.ssh/` (keys, `authorized_keys`), `~/.aws/`, `~/.kube/`, `/etc/sudoers`, `~/.netrc` |
+| Device and kernel pseudo-filesystems | `/dev/`, `/proc/`, `/sys/`, judged on the path as given and on the file it resolves to, so a symlink to `/dev/null` is refused too (on Docker and SSH backends a symlink on the remote side is not followed). `/dev/shm` stays writable as ordinary scratch space. Use the terminal tool when a device or kernel file really has to be written. |
 | Hermes secret stores | `.env`, `.anthropic_oauth.json`, `auth/google_oauth.json`, the google-workspace skill's `google_token.json`, `google_client_secret.json` and `google_oauth_pending.json`, Bitwarden and 1Password caches (`bws_cache.json`, `bws_cache.enc.json`, `op_cache.json`), the Photon sidecar token (`photon-sidecar.json`), `vault/`, `browser-profile/`, `mcp-tokens/`, `pairing/` under HERMES_HOME: the active profile, every profile under `profiles/`, and the global root. Control files (`auth.json`, `config.yaml`, `webhook_subscriptions.json`) are read-denied but stay writable. |
 | Windows NT/device-namespace paths | `\??\...`, `\\.\...`, `\\?\UNC\...`, `\\?\GLOBALROOT...` — rejected for both reads and writes on every platform. On Windows, merely *resolving* such a path (e.g. `\??\UNC\host\share`) triggers outbound SMB authentication and can leak the user's NTLM hash; the prefixes also bypass normal path normalization. Ordinary extended-length local paths (`\\?\C:\...`) and plain UNC shares (`\\server\share`) are unaffected. |
 

@BearHuddleston BearHuddleston left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: Re-reviewed at 7799dbc: finding 1 is fixed, and findings 2 and 3 stay open as optional nits. No blockers from me on this head.

# Finding Severity Where Status
1 The path-text check in both guard layers had no test Nit tests/tools/test_file_write_safety.py:1248 Fixed in 7799dbc: 12 of 12 mutants caught
2 The security docs' protected-paths table doesn't list the new category Nit (optional) agent/file_safety.py:99 Still open; the patch still applies to this head
3 Not a regression: V4A preflight doesn't vet Delete/Move entry coordinates (body-only in my first review) Nit (optional) tools/file_tools.py:1015 Still open, a follow-up for another PR

7799dbc adds exactly the test from finding 1, and agent/file_safety.py and tools/file_tools_write_guards.py haven't changed since 9bef203, so the rest of my first review carries over. I reran its probes on this head. With the head's own test file, all 12 mutants of the two guard files are caught (100 tests pass unmutated). TestPseudoFilesystemWriteGuard fails 14 of 15 on main, including all three new rows; the one pass is test_dev_shm_stays_writable, which passes on main by construction. The base-vs-head guard probe and the V4A partial-apply probe match the first review case for case. CI on this head is green: the three Python test shards, Python lints, repository guards, and the macOS-only and Windows-only lanes, both arm64 jobs included. A fourth Sol round (gpt-6.2-sol) on the new commit approved it; besides findings 2 and 3 it kept the Docker/SSH host-resolution follow-up from my first review.

No new inline comments.


Written and posted by Hermes Agent, Bear's AI agent, without Bear reading it first. An approval from it is never on behalf of hermes-agent-core: where that team's review is requested or required, the approval comes from Bear himself.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Medium — degraded but workaround exists tool/file File tools (read, write, patch, search) type/security Security vulnerability or hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: write_file and delete_file permit writing to and unlinking /dev nodes and /proc /sys pseudo-files

4 participants