Skip to content

fix: harden diagnostic command execution - #74

Merged
abruption merged 2 commits into
mainfrom
fix/command-execution-hardening
Sep 29, 2026
Merged

abruption merged 2 commits into
mainfrom
fix/command-execution-hardening

Conversation

@abruption

Copy link
Copy Markdown
Owner

Summary

  • security: inherited COLUMNS truncates ps args and hides shared app-server owners on Linux #52 (inherited terminal/format environment): runCommand no longer passes the parent environment. Diagnostic commands run with a fixed DIAGNOSTIC_COMMAND_ENV (LC_ALL=C, LANG=C, PATH=/usr/bin:/bin:/usr/sbin:/sbin), so COLUMNS, PS_FORMAT, PS_PERSONALITY, and similar variables cannot change ps output. Every ps call uses -ww. On Linux, ps arguments are reconciled with /proc/<pid>/cmdline: if the kernel argv cannot be read, or the ps value is a strict prefix of it, arguments is null with an arguments_unverified: / arguments_truncated: error and identity is incomplete. Shared-service classification is true when either source contains a service token.
  • security: resolve ps and lsof only from fixed system paths #61 (fixed tool paths): ps is resolved only from /bin/ps or /usr/bin/ps, and lsof only from /usr/sbin/lsof or /usr/bin/lsof (regular file with execute permission). There is no PATH fallback; a missing tool is a spawn_error and inspection becomes unknown.
  • fix: runCommand crashes on spawn failures such as EMFILE and loses post-signal evidence #66 (spawn failures, part 1): spawn is wrapped in try/catch; the error listener is attached before stdio is touched; a child without stdout/stderr (for example EMFILE) resolves as spawn_error instead of throwing a TypeError and crashing on an unhandled error event. child.kill is guarded. Converting failures after SIGTERM into an unlock result that keeps pid/signalSent is left for a follow-up PR.
  • fix: non-ASCII Codex home paths are escaped by lsof under LC_ALL=C and unlock always refuses #68 (non-ASCII lock paths): owner lock files are matched with lsof -a -p PID -F0Din by device (hex) and inode (decimal), parsed as BigInt and compared with lstat({ bigint: true }) of the intended lock. Non-matching candidates are resolved as before and fail closed with owner_lock_file_lookup_failed if unresolvable. A candidate whose path equals the intended canonical path but whose dev/ino differs is now an error.

Closes #52
Closes #61
Closes #68
Refs #66

Type

  • fix

Safety impact

Tightens owner identity and lock-file evidence; every new failure mode fails closed (unknown / refused). No change to the signaling path or post-signal verification. The JSON schema is unchanged (arguments was already string | null; errors are free-form strings).

Checklist

  • npm run check passes
  • npm run lint passes
  • npm test passes (95 pass, 0 fail)
  • npm run smoke:package passes
  • Commits follow Conventional Commits
  • Documentation is updated when behavior or output changed
  • No tokens, private paths, or unredacted process data are in the diff

Tests

  • doctor: a long-title app-server owner under COLUMNS=40/LINES/PS_FORMAT is refused as a shared service; fake ps/lsof earlier in PATH are never executed; a Codex home with a non-ASCII path yields ownerLockFiles=[lockPath] and unlocks the fixture owner; a second lock in another non-ASCII home is still refused.
  • util: environment is not inherited; synchronous spawn throw (EBADF); stdio-less child (EMFILE, no response); real descriptor exhaustion with ulimit -n 64.
  • process: parseLsofFiles, parseProcCommandLine, reconcileArguments.
  • Existing tests that intercepted ps via PATH now use a test-only override injected with node --import test/helpers/diagnostic-override.mjs; src never reads the override variables.
  • smoke:platform passes on macOS arm64.

Notes

  • Linux behavior is validated by the CI matrix rather than locally: the security: inherited COLUMNS truncates ps args and hides shared app-server owners on Linux #52 regression test only fails before this change on Linux (macOS ps does not truncate piped output), and ps -ww with -axo / -p ... -o on procps, /proc/<pid>/cmdline NUL padding for retitled processes, and lsof D encoding versus stat.dev were not measured locally. Unmatched D/i falls back to path resolution, which fails closed.
  • If procps renders non-ASCII argument bytes as ? under LC_ALL=C, the prefix comparison does not flag truncation; shared-service detection still uses the union of both sources.
  • If /proc/<pid>/cmdline is unreadable (for example hidepid), the owner identity is incomplete and unlock refuses.
  • Systems that install ps/lsof outside the fixed paths (for example NixOS) now fail closed.
  • processCwd on macOS still uses lsof names, so a non-ASCII cwd appears escaped; it is informational only and not used by policy.

@abruption abruption added this to the v0.4.1 milestone Sep 28, 2026
Comment thread src/util.ts Fixed
Run ps and lsof only from fixed system paths with a fixed environment
(LC_ALL=C, LANG=C, system PATH) instead of the caller's, so COLUMNS,
LINES, PS_FORMAT, PS_PERSONALITY, and locale variables cannot alter
process evidence. Invoke ps with -ww and, on Linux, cross-check its
arguments against /proc/<pid>/cmdline: unreadable or truncated argv
becomes unknown, and a shared-service token in either source marks the
owner as shared.

Match the owner's native lock to the intended lock by lsof device and
inode fields rather than the rendered name, which lsof escapes for
non-ASCII bytes in the C locale. Unresolvable other lock names still
fail closed.

runCommand now resolves spawn_error for synchronous spawn throws and
for EMFILE/ENFILE children without stdio pipes, attaching the error
listener before touching stdio. Converting post-SIGTERM failures into
an unlock result is left for a follow-up.

Tests inject failing ps through an in-process test seam instead of
PATH, and cover terminal-width variables, PATH shadowing, non-ASCII
Codex homes with real advisory locks, and spawn failure paths.

Closes #52
Closes #61
Closes #68
Refs #66
Linux lsof 4.9x can omit the `f` field in `-F0Din` output, so every file
set was dropped and the owner lock set came back empty on Linux. Request
`-F0fDin`, and also start a new file set when a `D`, `i`, or `n` field
repeats, so both the macOS and the Linux layouts parse.

Call `child_process.spawn` directly with `shell: false`. Spawn failures
are now injected through a module-level test-only override instead of a
runCommand parameter. Drop the descriptor-exhaustion test that ran
`/bin/sh -c` through runCommand; the stub tests cover the synchronous
throw and missing-stdio paths.

Refs #68
Refs #66
@abruption
abruption force-pushed the fix/command-execution-hardening branch from 0ffbc0f to 8bc97b5 Compare September 29, 2026 00:14
@abruption
abruption merged commit ce2105c into main Sep 29, 2026
24 checks passed
@abruption
abruption deleted the fix/command-execution-hardening branch September 29, 2026 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants