Skip to content

fix(tier3): stage over existing trees on older glibc and explain bwrap failures - #267

Merged
rng1995 merged 10 commits into
mainfrom
naren/port-secure-copy-chmod-glibc
Nov 4, 2026
Merged

rng1995 merged 10 commits into
mainfrom
naren/port-secure-copy-chmod-glibc

Conversation

@rng1995

@rng1995 rng1995 commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Staging over an existing tree on older glibc. Tier 3 staging no longer fails with ValueError: chmod: cannot use dir_fd and follow_symlinks together when it replaces or merges into an existing directory or file. The two os.chmod(..., dir_fd=..., follow_symlinks=False) calls in secure_copy.py (_remove_tree_at and _prepare_moved_backup) now go through a new _chmod_named_entry helper. The helper keeps the no-follow call wherever the platform supports it. If the call is rejected, it re-stats the entry without following links and refuses a link with UnsafeStagingError. It then opens the entry with O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC | O_NOCTTY (plus O_DIRECTORY for a directory), checks that the descriptor's device and inode match that stat, and changes the mode with os.fchmod on the descriptor. A link swapped in after the stat makes the open fail, so it is refused instead of followed. A plain chmod by name, followed by a device/inode re-check, is left only for an entry that cannot be opened with EACCES/EPERM (for example one its owner cannot read) and for platforms without os.O_NOFOLLOW.
  • Clearer local-mode sandbox errors. A new _bwrap_smoke_result() returns bubblewrap's last stderr line, the launch error, or a timeout note, and detect() appends that detail to the failure reason. When the detail is pivot_root: Operation not permitted, the reason also says that the enclosing container's seccomp profile may be blocking bubblewrap. _bwrap_smoke_test() is kept as a boolean wrapper.
  • Remediation text. The require-mode SandboxUnavailable message now offers SKILLEVALUATOR_LOCAL_SANDBOX=prefer alongside off, and suggests --env-mode docker only "where a Docker daemon is available".
  • Docs and changelog. docs/agents-and-sandboxes.mdx (Local mode) explains the new failure detail and the seccomp signature. CHANGELOG has two entries under Unreleased → Fixed.

Why

The chmod failure. CPython has no lchmod on Linux, so it maps dir_fd plus follow_symlinks=False to fchmodat(AT_SYMLINK_NOFOLLOW). glibc before 2.32 rejects that flag, for example on RHEL/Rocky 8, Debian 10 and Ubuntu 20.04. Newer glibc also rejects it when /proc is not mounted. CPython then raises ValueError, and the staging cleanup paths do not catch it, because they only catch OSError and UnsafeStagingError.

Every publish over an existing destination reaches this call. That includes:

  • init-custom-grader and init-harbor-task over an existing evals/ directory
  • re-staging native Harbor tasks into an existing output directory
  • dirs_exist_ok merges of eval input files
  • copy_file_secure over an existing file

On affected hosts, all of these failed. The rollback restored the original, but nothing was staged. CI did not catch the bug because its Linux runners have a recent glibc and macOS has lchmod.

The sandbox message. When bubblewrap was installed but could not start, users saw only "failed its smoke test", and bubblewrap's stderr was thrown away. Inside a container whose seccomp profile blocks pivot_root, that message looks the same as the sysctl problems that detect() reports in other cases. Users chased sysctls that were already fine. The remediation text also offered only off, which turns off every guardrail, although prefer, which keeps the advisory guardrails, is available.

Kept from public

  • No-follow chmod where it works. I did not just drop follow_symlinks=False. _chmod_named_entry tries the no-follow call first, so macOS, BSD and glibc 2.32+ hosts with /proc behave exactly as before. The fallback runs only after the platform rejects the flag. On glibc 2.32+, that rejection also happens when the entry has become a symlink, and the fallback refuses it instead of following it.
  • Existing race checks. The _is_link pre-checks and the device/inode and mode re-checks after each chmod are unchanged. So are the same-UID boundary statements in the docstrings.
  • Platform guards. The current Seatbelt wording, the native-Windows fail-closed check and the per-platform remediation text stay as they are. Only the Linux bubblewrap reason and the shared remediation sentence changed.
  • Pinned bubblewrap launcher. detect() still resolves bwrap to an absolute path, probes that path and stores it in SandboxPlan.executable, as on main. The smoke-test detail is collected from the same resolved path.
  • Tests that do not depend on the host. No test asserts that follow_symlinks is absent from the source. That would conflict with keeping the no-follow call. Instead, the regression tests emulate the old glibc behaviour directly, so they fail without the fix on any CI host.

Tests

  • tests/test_harbor_secure_copy.py
    • test_existing_destination_publish_survives_chmod_without_nofollow_support[merge|replace]: publishes over a destination that contains a read-only (0o500) nested directory while no-follow chmod is rejected. It asserts that the publish succeeds and leaves no backup residue.
    • test_existing_file_replacement_survives_chmod_without_nofollow_support: the same check for copy_file_secure over an existing file.
    • test_chmod_fallback_refuses_a_link_without_following_it: the fallback raises UnsafeStagingError for a symlink and leaves the link target's mode unchanged.
    • All three fail against unmodified main: the first two with the ValueError, the third because the helper does not exist yet.
    • test_chmod_fallback_changes_mode_through_the_opened_descriptor[directory|file]: with no-follow chmod rejected, the mode is changed by os.fchmod on a descriptor for the same inode, and no chmod by name is made.
    • test_chmod_fallback_refuses_a_link_swapped_in_after_the_stat[directory|file]: the entry is replaced by a symlink right after the helper's no-follow stat. The helper raises UnsafeStagingError (symlink), makes no chmod by name or fchmod, and the link target's mode is unchanged.
    • test_chmod_fallback_refuses_an_entry_replaced_after_the_stat[directory|file]: the entry is replaced by another directory or a file after the stat. The device/inode check (or ENOTDIR) refuses it, and neither entry's mode changes.
    • test_chmod_fallback_keeps_checked_chmod_by_name_when_entry_cannot_be_opened[EACCES|EPERM]: when the open is refused for a permission reason, the helper falls back to the checked chmod by name and the mode is changed.
    • test_chmod_fallback_without_nofollow_open_keeps_checked_chmod_by_name: without os.O_NOFOLLOW (native Windows), the checked chmod by name is used.
    • The first three of these fail against a stat-then-chmod helper.
  • tests/test_harbor_local_sandbox.py
    • Bubblewrap's detail reaches the reason, together with the seccomp hint.
    • A pivot_root: Invalid argument detail does not trigger the seccomp claim.
    • Require mode offers both prefer and off.
    • _bwrap_smoke_result reports a launch error for a missing binary and returns the last stderr line.
    • The existing working-bubblewrap test and main's launcher-pinning tests now patch _bwrap_smoke_result, which detect() calls.
  • Local runs:
    • pytest tests/test_harbor_secure_copy.py tests/test_harbor_local_sandbox.py: 191 passed, 7 skipped.
    • pytest -n 4 tests/test_harbor_local_mode.py tests/test_harbor_local_environment.py tests/test_harbor_local_upload.py tests/test_local_mode_contract.py tests/test_harbor_adapter_safety.py tests/test_harbor_output_provenance.py tests/test_harbor_runner_status.py tests/test_harbor_secure_docker_environment.py tests/test_oss_packaging.py tests/test_tier3_public_runtime.py tests/test_ci_change_classifier.py: 1226 passed, 4 skipped, 7 failed. Two are the *sidecar_retains_* tests, which depend on host environment variables. The other five are process-timing tests (timeouts, descendant reaping, a streamed callback) run on a heavily loaded macOS host, and all five pass when rerun serially.
    • ruff check and ruff format --check on the changed Python files: clean.
    • scripts/check_oss_boundary.py: passed.

🤖 Generated with Claude Code

…p failures

Replacing or merging into an existing directory or file during Tier 3
staging made the moved original private, and later removable, with
os.chmod(dir_fd=..., follow_symlinks=False). CPython has no lchmod on
Linux and maps that call to fchmodat(AT_SYMLINK_NOFOLLOW), which glibc
before 2.32 (and newer glibc without /proc) rejects. CPython then raises
"ValueError: chmod: cannot use dir_fd and follow_symlinks together",
which the staging cleanup paths do not catch. On those hosts commands
such as init-custom-grader and init-harbor-task over an existing evals/
directory, and Harbor task re-staging, failed outright.

Route both call sites through _chmod_named_entry, which keeps the
no-follow call wherever the platform supports it and only falls back to
the plain call after a fresh no-follow stat confirms the entry is still
not a link; a link is refused with UnsafeStagingError. The existing link
pre-checks and the device/inode and mode re-checks afterwards are
unchanged.

Local mode on Linux also reported only "bubblewrap failed its smoke
test" and discarded bubblewrap's stderr, so a container seccomp profile
that blocks pivot_root looked identical to the sysctl causes. The probe
now returns bubblewrap's last stderr line (or the launch error), detect()
appends it to the reason, and a "pivot_root: Operation not permitted"
detail adds a hint about the enclosing container's seccomp profile. The
require-mode error now offers SKILLEVALUATOR_LOCAL_SANDBOX=prefer
alongside off and qualifies the --env-mode docker suggestion.
_bwrap_smoke_test stays as a boolean wrapper.

Tests emulate the older glibc by making the no-follow chmod raise, so
they fail without the fix on any host; CI runners support the flag.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@rng1995 rng1995 left a comment

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.

Reviewed with Claude Code. The fix is correct and well tested: no P1/P2 findings. Tests for secure_copy and local_sandbox pass locally (178 passed, 7 skipped) and ruff check is clean. Two P3 notes below, neither blocking.

Comment thread CHANGELOG.md Resolved
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Narendran Raghavan <[email protected]>
rng1995 and others added 5 commits November 2, 2026 10:47
Conflicts: CHANGELOG.md, src/skillevaluator/tier3/harbor/local_sandbox.py (main's bwrap pin tests now stub _bwrap_smoke_result).

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
When fchmodat(AT_SYMLINK_NOFOLLOW) is rejected, _chmod_named_entry no
longer stats the entry and then chmods it by name, a window in which a
symlink swapped in would be followed. It now opens the entry with
O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC (plus O_DIRECTORY for a
directory), checks the descriptor's device and inode against the
no-follow stat, and calls os.fchmod on that descriptor. ELOOP (and
macOS's ENOTDIR for a link) is refused as a symlink, as before.

The checked chmod by name, now followed by a device/inode re-check,
remains only when the open fails with EACCES/EPERM, such as an entry its
owner cannot read, and on platforms without os.O_NOFOLLOW.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflicts: CHANGELOG.md (kept main's claude-code background-task Fixed entry,
then this PR's chmod and bwrap-detail entries) and local_sandbox.py (detect()
resolves bwrap to an absolute path and pins it as main does, then probes it
via this PR's _bwrap_smoke_result for the failure detail). Main's pinning
tests now patch _bwrap_smoke_result, the seam detect() calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Narendran Raghavan <[email protected]>
Clean merge of the newer main (#281, #271, #293); CHANGELOG.md and
docs/agents-and-sandboxes.mdx auto-merged with no conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Narendran Raghavan <[email protected]>
…n/port-secure-copy-chmod-glibc

Conflicts: CHANGELOG.md (kept the descriptor-chmod wording for the staging entry).

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@chrisknvidia chrisknvidia 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.

I read _chmod_named_entry and _fchmod_opened_entry with their two callers (the staging cleanup and the moved-backup step) and ran the new chmod tests, which pass: a link swapped in after the stat is refused (ELOOP for files, ENOTDIR for directories, confirmed with a fresh no-follow stat), a replaced entry is caught by the device and inode check, and fchmod acts on the opened descriptor, so it cannot follow anything. The by-name chmod that remains, for entries the owner cannot open and for platforms without O_NOFOLLOW, is documented and still re-checks identity afterwards; it needs a same-user racer, which is the boundary the earlier comment already accepted. The bubblewrap smoke-result handling and the pivot_root hint are unchanged. Nothing blocking.

rng1995 and others added 3 commits November 3, 2026 09:00
Conflicted: CHANGELOG.md.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the CHANGELOG.md conflict with #269: keep main's BENCHMARK.md
score-transition entry, then this PR's two Fixed entries.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the CHANGELOG.md conflict: keep main's new Unreleased entry and
put this PR's entry after it.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rng1995
rng1995 merged commit 8d8dc25 into main Nov 4, 2026
21 checks passed
@rng1995
rng1995 deleted the naren/port-secure-copy-chmod-glibc branch November 4, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants