Skip to content

OM-SEC-22: Isolate OCaml cleanup authorization - #10217

Merged
ErikMelton merged 3 commits into
quattrofrom
security/ocaml-removal-no-update
Sep 22, 2026
Merged

ErikMelton merged 3 commits into
quattrofrom
security/ocaml-removal-no-update

Conversation

@ErikMelton

Copy link
Copy Markdown
Member

Summary

Fix OM-SEC-22 without changing the package-management workflows carried by #9476.

User-controlled opam code previously ran before a normal sudo cleanup. A detached child could wait for that authentication and reuse the resulting timestamp.

The OCaml removal path now:

  • invalidates any existing sudo credential before running opam
  • verifies that sudo supports --no-update
  • runs only the fixed /usr/local/bin/opam cleanup through sudo -N
  • invalidates credentials on success, failure, and handled signals

The boundary is installed only for OCaml removal. Other development-environment removals retain their existing non-sudo behavior.

Validation

  • ./test/cli
  • test/shell.d/remove-dev-env-sudo-security-test.sh
  • bash -n on changed shell files
  • git diff --check

The focused namespace test covers the detached polling exploit, unsupported sudo -N, root cleanup failure, signal cleanup, and a non-OCaml removal that must not invoke sudo.

Supersedes #9476.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Moderate gaps in credential-invalidation coverage and subordinate-ID mapping must be addressed.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Secures OCaml removal by isolating privileged cleanup from user-controlled opam execution.

Changes:

  • Adds a cold, non-caching sudo boundary for OCaml cleanup.
  • Adds namespace-based security regression coverage.

Review findings:

  • Moderate: Success, failure, and signal tests do not verify final credential invalidation (lines 201, 217, 241).
  • Moderate: Namespace mappings assume 65,536 subordinate IDs instead of parsing authorized counts (line 23).
  • Nit: Use arithmetic conditionals for numeric tests (line 28).
  • Nits: Quote string literals in [[ ]] comparisons (lines 11, 147).
File summaries
File Description
test/shell.d/remove-dev-env-sudo-security-test.sh Tests sudo isolation, cleanup, and failure paths.
bin/omarchy-remove-dev-env Secures privileged OCaml cleanup.
Review details

Suppressed comments (5)

test/shell.d/remove-dev-env-sudo-security-test.sh:217

  • reset_case already removes the token, and the failing cleanup stub never creates one, so this assertion passes even if the EXIT invalidation is removed. Check for the second invalidate event to cover the claimed failure-path cleanup.
[[ ! -e $token ]] || fail "failed OCaml cleanup left a reusable sudo credential"

test/shell.d/remove-dev-env-sudo-security-test.sh:241

  • The token present at the start of this case is removed before opam signals readiness, so its absence after TERM does not exercise the signal-triggered EXIT cleanup. Verify the final invalidate call explicitly; otherwise the signal trap could stop invalidating credentials without failing this test.
[[ ! -e $token ]] || fail "terminated OCaml removal left a reusable sudo credential"

test/shell.d/remove-dev-env-sudo-security-test.sh:28

  • Repository shell style requires numeric tests to use arithmetic conditionals rather than [[ ]] (AGENTS.md:26-28).
[[ $(id -u) == 0 ]] || fail "OCaml sudo proof did not enter its root namespace"

test/shell.d/remove-dev-env-sudo-security-test.sh:11

  • Repository shell style requires string literals in [[ ]] comparisons to be quoted (AGENTS.md:27).
if [[ ${OMARCHY_REMOVE_DEV_ENV_SECURITY_NS:-0} != 1 ]]; then

test/shell.d/remove-dev-env-sudo-security-test.sh:147

  • Repository shell style requires string literals in [[ ]] comparisons to be quoted (AGENTS.md:27).
if [[ ${TEST_OPAM_BLOCK:-0} == 1 ]]; then
  • Files reviewed: 1/2 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@ErikMelton
ErikMelton force-pushed the security/ocaml-removal-no-update branch from 0e559ef to 236a24e Compare September 4, 2026 18:29
@ErikMelton
ErikMelton force-pushed the security/ocaml-removal-no-update branch from 236a24e to 73a0969 Compare September 4, 2026 18:34
@sayem314

sayem314 commented Sep 5, 2026

Copy link
Copy Markdown

@robosayem review this pr

@robosayem robosayem left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed the diff and cross-checked the sudo semantics against the sudo(8) docs. The core mechanism is sound.

Why this closes the window

sudo -N only stops creating or updating the timestamp record. Per sudo(8), with -N an existing valid credential is still used. That makes the cold start the load-bearing part: the initial sudo -k before opam runs is what removes anything a detached poller could reuse, and -N on the cleanup command is what keeps the authentication from creating a new record the poller could grab. The implementation gets both halves right, and the ordering is fail-closed (the -N -V capability probe runs before any user-controlled code).

Test quality

The namespace harness is a genuine regression test, not a mock parade: pre-placing the token file means the success-path test fails if the cold-start sudo -k is ever dropped, and the bind-mounted setuid stub reproduces the detached-poller reuse attempt for real. The node case correctly pins the boundary to OCaml only, matching the branch reality — no other dev-env branch pairs user-controlled commands with a direct sudo call.

Copilot's round-1 findings look addressed by 73a0969: assert_ocaml_invalidations now covers success, failure, signal, and unsupported-sudo paths (the exit trap's second invalidate event is what distinguishes a real cleanup from a reset), and the subuid/subgid mapping parses the authorized counts with a >= 1000 guard instead of assuming 65,536.

Two small notes, no blocker

  1. exec unshare ... turns an unshare failure into a hard test error instead of a skip. On hosts where unprivileged user namespaces are restricted (containers with the default seccomp profile, Ubuntu 24.04+ AppArmor policy) the suite reports a failure from a capability gap. Consider probing first and degrading to a skip the way the subuid guard already does.
  2. Behavior change worth a release-note line: OCaml removal now always prompts for the sudo password (cold start), where it previously rode a cached credential.

Out of scope, for the record

A hostile process that outlives the script, or a concurrent sudo authentication in another terminal during the removal window, can still ride a timestamp created later, and -N will happily use an existing valid credential. That is inherent to sudo's session-wide caching model and not fixable inside this script, so no change requested.

@AFOliveira AFOliveira left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two minor test portability observations.

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.

6 participants