Repository navigation
fix(tier3): stage over existing trees on older glibc and explain bwrap failures - #267
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: Narendran Raghavan <[email protected]>
a93f75d to
3dc9b05
Compare
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
left a comment
There was a problem hiding this comment.
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.
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]>
Summary
ValueError: chmod: cannot use dir_fd and follow_symlinks togetherwhen it replaces or merges into an existing directory or file. The twoos.chmod(..., dir_fd=..., follow_symlinks=False)calls insecure_copy.py(_remove_tree_atand_prepare_moved_backup) now go through a new_chmod_named_entryhelper. 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 withUnsafeStagingError. It then opens the entry withO_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC | O_NOCTTY(plusO_DIRECTORYfor a directory), checks that the descriptor's device and inode match that stat, and changes the mode withos.fchmodon 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 withEACCES/EPERM(for example one its owner cannot read) and for platforms withoutos.O_NOFOLLOW._bwrap_smoke_result()returns bubblewrap's last stderr line, the launch error, or a timeout note, anddetect()appends that detail to the failure reason. When the detail ispivot_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.SandboxUnavailablemessage now offersSKILLEVALUATOR_LOCAL_SANDBOX=preferalongsideoff, and suggests--env-mode dockeronly "where a Docker daemon is available".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
lchmodon Linux, so it mapsdir_fdplusfollow_symlinks=Falsetofchmodat(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/procis not mounted. CPython then raisesValueError, and the staging cleanup paths do not catch it, because they only catchOSErrorandUnsafeStagingError.Every publish over an existing destination reaches this call. That includes:
init-custom-graderandinit-harbor-taskover an existingevals/directorydirs_exist_okmerges of eval input filescopy_file_secureover an existing fileOn 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 thatdetect()reports in other cases. Users chased sysctls that were already fine. The remediation text also offered onlyoff, which turns off every guardrail, althoughprefer, which keeps the advisory guardrails, is available.Kept from public
follow_symlinks=False._chmod_named_entrytries the no-follow call first, so macOS, BSD and glibc 2.32+ hosts with/procbehave 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._is_linkpre-checks and the device/inode and mode re-checks after each chmod are unchanged. So are the same-UID boundary statements in the docstrings.detect()still resolvesbwrapto an absolute path, probes that path and stores it inSandboxPlan.executable, as onmain. The smoke-test detail is collected from the same resolved path.follow_symlinksis 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.pytest_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 forcopy_file_secureover an existing file.test_chmod_fallback_refuses_a_link_without_following_it: the fallback raisesUnsafeStagingErrorfor a symlink and leaves the link target's mode unchanged.main: the first two with theValueError, 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 byos.fchmodon 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 raisesUnsafeStagingError(symlink), makes no chmod by name orfchmod, 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 (orENOTDIR) 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: withoutos.O_NOFOLLOW(native Windows), the checked chmod by name is used.tests/test_harbor_local_sandbox.pypivot_root: Invalid argumentdetail does not trigger the seccomp claim.preferandoff._bwrap_smoke_resultreports a launch error for a missing binary and returns the last stderr line.main's launcher-pinning tests now patch_bwrap_smoke_result, whichdetect()calls.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 checkandruff format --checkon the changed Python files: clean.scripts/check_oss_boundary.py: passed.🤖 Generated with Claude Code