Skip to content

fix(cli): bound Unix secret and capability fd reads - #219

Open
nillikechatchat wants to merge 5 commits into
higress-group:mainfrom
nillikechatchat:fix/cli-protected-fd-bounds
Open

nillikechatchat wants to merge 5 commits into
higress-group:mainfrom
nillikechatchat:fix/cli-protected-fd-bounds

Conversation

@nillikechatchat

@nillikechatchat nillikechatchat commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Unix --secret-fd and non-task --capability-fd already rejected decoded material over 512 bytes, but std::fs::read waited for EOF and loaded the whole file first. A FIFO that never closed, /dev/zero, or a huge regular file could hang or exhaust memory.

Change

  • Read inherited Unix descriptors with poll on /dev/fd/N.
  • Do not change inherited file-status flags. macOS /dev/fd/N follows dup semantics, so opening with O_NONBLOCK does not make a blocking pipe nonblocking.
  • Accept the documented regular-file redirect (3< KEY_FILE) and inherited pipes.
  • Reject stdio and non-file/fifo types (devices).
  • Stop at 512 bytes or 2s, then apply the existing UTF-8 / NUL / trailing-newline checks.
  • Watchdog subprocesses cover empty and partial pipes whose writer stays open.

work-plans / tasks capability material stays pipe-only.

Fixes #216

Validation

Unix --secret-fd and non-task --capability-fd already reject decoded
material over 512 bytes, but std::fs::read waited for EOF and loaded
the whole file first. A FIFO that never closed, /dev/zero, or a huge
regular file could hang or exhaust memory.

Keep the documented 3< KEY_FILE regular-file redirect and inherited
pipes, reject stdio and non-file/fifo types, and stop at 512 bytes
or 2s.

Fixes higress-group#216

Signed-off-by: nillikechatchat <yuanhenglizhen2050@163.com>
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
@nillikechatchat

Copy link
Copy Markdown
Contributor Author

@johnlanni ready for review.

Fixes #216. Candidate SHA a706c7fe. backend-ci and windows-headless-compile passed.

@johnlanni

Copy link
Copy Markdown
Contributor

Review: valid input-bounding defect; macOS timeout needs correction

Reviewed commit: a706c7fe2ee9beada71a0804701054dbc2482bd0.

Importance of the original issue: medium (P2). The original reader consumes the entire input before enforcing the 512-byte limit. A large file or endless device can consume excessive memory, and a pipe whose writer stays open can hang the CLI and its calling automation. This is worth fixing in the normal maintenance cycle. The report and current evidence do not establish a production incident or remotely triggered daemon outage. Preserving documented regular-file redirection (3< KEY_FILE) is correct; accepting that input is not itself a defect.

Change needed before merge: the new timeout still does not work for inherited blocking pipes on macOS. In read_protected_fd_unix, opening /dev/fd/N with O_NONBLOCK does not make the duplicated descriptor nonblocking on macOS. read() can therefore block before the loop gets another opportunity to check its deadline. Apple's descriptor-opening implementation follows descriptor-duplication semantics here.

An isolated syscall probe matching this PR's open flags and deadline loop confirmed this on macOS 26.3.2 arm64: both an empty pipe with its writer retained and a pipe containing synthetic credential bytes without EOF remained blocked beyond an external 3-second watchdog. The same cases rejected after about 2 seconds on Linux. This is OS-level reproduction, not an execution of the candidate CLI on macOS; the existing green CI does not cover this behavior.

Please:

  1. Use a platform-correct bounded descriptor-reading strategy on both Linux and macOS. If changing descriptor flags, account for shared file-status flags and preserve the caller's descriptor ownership and original state on every exit path.
  2. Add timeout regressions for both empty and partially written pipes with the write end kept open. Run them on macOS as well as Linux, with an independent subprocess watchdog so a regression cannot hang the test runner.
  3. Retain regular-file/closed-pipe success, the 512-byte boundary, device/stdio rejection, and the existing UTF-8, NUL and trailing-newline behavior.

The size/type checks are useful improvements. Recommendation: keep the fix, address the macOS deadline gap, then re-review. The same deadline issue also applies to #220, so a shared bounded reader would help keep the behavior consistent.

macOS /dev/fd/N follows dup semantics, so opening with O_NONBLOCK
does not make an inherited blocking pipe nonblocking. poll waits
for readiness without changing file-status flags.

Keep the 512-byte / 2s bound, regular-file and pipe acceptance, and
stdio/device rejection. Watchdog subprocesses cover empty and
partial pipes whose writer stays open.

Signed-off-by: nillikechatchat <yuanhenglizhen2050@163.com>
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
@nillikechatchat

Copy link
Copy Markdown
Contributor Author

@johnlanni follow-up for the macOS deadline gap is on 8d857ced.

Inherited Unix secret/capability reads now use poll on /dev/fd/N and do not change file-status flags. Watchdog subprocesses cover an empty pipe and a partial secret whose writer stays open. Regular-file/closed-pipe success, the 512-byte bound, stdio/device rejection, and UTF-8/NUL/trailing-newline checks remain.

Linux focused tests: cargo test --locked -p hiroute-cli --lib -- unix_bounded_fd protected_fd — 8 passed.

Hosted on this SHA:

This checkout has no macOS validation target, so the watchdog tests have not been executed on macOS here. The same tests are in the candidate.

Shared reader: crates/cli/src/unix_bounded_fd.rs, also used by #220.

@johnlanni

Copy link
Copy Markdown
Contributor

Follow-up review (2026-10-29): macOS fix verified; Linux FIFO opening still needs a bound

Reviewed commit: 20f83d740d298648c67d127da9c4d42727d590f0.

The previous macOS pipe-read finding is addressed for the tested scenarios. I ran cargo test --locked -p hiroute-cli --lib -- unix_bounded_fd protected_fd on macOS arm64 at this exact head: 8 passed, including the empty-pipe and partial-secret-without-EOF watchdog cases. Keeping the shared reader and its subprocess watchdogs is a useful improvement.

Remaining P2 finding: read_unix_fd_bounded, line 31 calls blocking File::open("/dev/fd/N") before it creates the deadline. On Linux, reopening an inherited named FIFO this way waits for a writer. The poll loop is never reached, so the documented 2-second bound does not cover descriptor acquisition.

I reproduced this with the actual public CLI built from this head, using a private runtime and empty synthetic input:

  1. Create a named FIFO, open its read end with os.O_RDONLY | os.O_NONBLOCK, and keep that descriptor open with no writer.
  2. Inherit that descriptor into hiroute protected-input register --candidate candidate/review --secret-fd N --output json.
  3. The CLI remains blocked and needs an external watchdog to kill it after 3 seconds. As a control, the ordinary anonymous-pipe case with a retained writer returns INVALID_ARGUMENTS after about 2 seconds.

Please make the descriptor-acquisition step nonblocking/bounded as well, while preserving the caller's descriptor ownership and file-status flags on both operating systems. Add subprocess-watchdog regressions for named FIFOs with no writer and with a writer that has already closed; retain the passing macOS cases. #220 contains the identical helper and should receive the same correction.

Importance and merge recommendation: this remains a medium-priority CLI/automation reliability issue, suitable for normal maintenance. The macOS repair and size/type bounds are improvements, but this supported FIFO input still violates the promised deadline, so I recommend correcting it before merge. Current CI is green; GitHub also reports this branch behind main, so refresh the base and satisfy the required checks and code-owner approval on the final candidate.

nillikechatchat and others added 2 commits October 29, 2026 01:45
…d-boundsCo-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
Linux File::open("/dev/fd/N") can stall on a named FIFO before poll
sees the 2s deadline. Reopen with O_NONBLOCK so acquisition stays
bounded; inherited file-status flags are unchanged.

Watchdog coverage includes an empty named FIFO and a closed writer
that must finish inside the deadline.

Signed-off-by: nillikechatchat <yuanhenglizhen2050@163.com>
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
@nillikechatchat

Copy link
Copy Markdown
Contributor Author

@johnlanni Linux named-FIFO follow-up is on 94372aaa (rebased onto upstream/main 5eb39d29 via merge cffca33d).

--secret-fd / --capability-fd now reopen /dev/fd/N with O_RDONLY|O_CLOEXEC|O_NONBLOCK. That flag applies to the new description; the inherited descriptor's file-status flags stay unchanged. macOS /dev/fd/N still follows dup semantics, so poll remains the read deadline.

Watchdog coverage:

  • empty named FIFO without a writer times out inside 2s
  • hung-up named FIFO with a buffered payload completes inside the deadline

Linux focused tests at 94372aaa: cargo test --locked -p hiroute-cli --lib -- unix_bounded_fd protected_fd — 10 passed.

This checkout has no macOS validation target, so the watchdog tests have not been executed on macOS here. Hosted CI after this push is the remaining remote evidence. Human review remains the default.

Shared reader: crates/cli/src/unix_bounded_fd.rs. The same helper will be copied onto #220; the PRs stay unstacked.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Unix CLI secret and capability fds read without pipe, size, or time bounds

2 participants