Skip to content

fix(containment): refuse Linux terminal input injection - #126

Merged
kevin-orellana merged 2 commits into
mainfrom
feature/linux-refuse-terminal-injection
Oct 20, 2026
Merged

kevin-orellana merged 2 commits into
mainfrom
feature/linux-refuse-terminal-injection

Conversation

@kevin-orellana

@kevin-orellana kevin-orellana commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Human readable section

Problem statement: Sandboxed processes can use a system call ioctl to relay messages to a terminal's feed. This can be abused to run arbitrary commands once the sandboxed process runs.

Solution: Remove ability to relay synthetic input/output to this terminal channel.

Tradeoff: For a few, niche terminal programs that rely on this, they may lose ability to relay signals/messages to the terminal.

Description

The Linux filter permits ioctl without checking its request number. A workload that inherits a controlling terminal can therefore request synthetic input through TIOCSTI when the surrounding kernel allows it.

Add an argument-scoped refusal for TIOCSTI. The filter returns EPERM before the kernel looks up the descriptor. Match the request's low 32 bits, as Linux does, so nonzero upper bits cannot bypass the rule. Other ioctl requests retain their existing behavior.

Programs that use TIOCSTI to simulate terminal input will fail inside Box. The rule has no policy override. This change covers this request only; it does not isolate the shared terminal or claim complete terminal safety.

Rationale

A workload must not make the operator's shell run commands by pretending that the operator typed them. Ordinary terminal input, output, settings, and job control do not require this request. Block it in Box so protection does not depend on the surrounding kernel's configuration.

The compatibility cost applies to specific features, not to all uses of an application. Examples include hstr's command insertion and Emacs returning queued text when it suspends. hstr also supplies shell integration without TIOCSTI. These are source findings, not application compatibility results.

Refuse only TIOCSTI: blocking all terminal-control requests would also break legitimate terminal operations. Keep the refusal below policy, without an override that restores synthetic input to the operator's terminal. Shared-terminal risks outside this request remain outside this PR.

Related Issues

Related to #51. This PR covers only its terminal-input injection item.

Type of Change

Bug fix

Testing

GitHub CI passed for commit 98dca00f59345e754c82b7f600077e46247d4e9f, including Linux/macOS unit tests, lint, deterministic containment, and the CI Gate. Claude Code and Codex compatibility checks also passed on this exact commit, with the launch-mode limit below.

Review follow-up dee28bbbfcd0937f4aa47609fba443158b811932 changes only the compatibility test. It checks all four window-size fields using distinct, nonzero pixel dimensions. Both Linux ARM64 terminal tests, containment lint, formatting, and independent review passed. Zeroing either returned pixel field independently makes the test fail; restoring it passes. Production code remains identical to the application-tested commit above. Continuous integration is running for this follow-up.

Claude Code and Codex compatibility

Both programs ran inside the patched Box on ARM64 Linux (7.0.12-linuxkit) in a disposable terminal. The outer Docker container enabled the privileges required to create Box's namespaces. Each program made real model calls through Box's gateway.

  • Claude Code 2.1.304 with Claude Opus 5: headless file-edit and shell task passed in 14.65 seconds. Interactive typed input, resizing from 80×24 to 100×30, a shell task, and Ctrl-C exit passed in 25.07 seconds.
  • Codex 0.163.0 with openai.gpt-5.6-terra: headless file-edit and shell task passed in 8.34 seconds. The interactive scenario passed in 17.21 seconds with --no-alt-screen --no-daemon.
  • All four runs exited with status 0. The external test driver checked exact file contents. No timeout or forced termination was needed for the passing runs.

Codex launch-mode limit: default daemon mode reported an incomplete local package under the test's isolated filesystem grants. The documented --no-daemon mode passed. This is not a claim that default daemon startup works inside Box.

The Box binary SHA-256 was d764db39cfa0726f456b859f42d10aaca073b36ae9285003f326c9238c9251a8; the containment trampoline SHA-256 was 6c1f1bbc9c18599787acadd58a316b56e91a019ffb886e495cb25d4c618999b2.

Filter and workspace checks

ARM64 Linux, Docker Desktop kernel 7.0.12-linuxkit, Rust 1.98.1:

  • cargo test --locked -p strands-box-containment --all-features --lib terminal_ -- --nocapture: 2 passed, no skips. Injection requests use descriptor -1, so the test cannot insert input. The control returns EBADF; the filter returns EPERM. Requests with nonzero upper bits are included.
  • The compatibility test creates a private terminal pair and checks window size and terminal settings. It does not attach that terminal to the operator's shell.
  • cargo test --locked -p strands-box-containment --all-features --lib backend::linux::namespace::syscall::tests: 22 passed, no skips.
  • Mutation checks: removing the refusal fails the injection test; refusing all ioctl calls fails the compatibility test. Restoring the implementation passes both tests.
  • cargo clippy --locked -p strands-box-containment --all-features --all-targets -- -D warnings -A dead_code -A clippy::chunks_exact_to_as_chunks: passed. These allowances match the repository gate.
  • cargo build --locked -p strands-box-containment --no-default-features: passed, with warnings from existing platform code.
  • cargo check --locked -p strands-box-containment --lib --target aarch64-unknown-linux-gnu: passed.

No manual end-to-end terminal-injection reproduction was run. The application results cover the scenarios above, not every shell/editor feature or plugin. No Linux 6.1 runtime result is claimed. Independent adversarial review found no code defect.

macOS ARM64: just check passed (format, workspace clippy with repository allowances, and workspace tests with all features). Existing ignored tests remain ignored.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and workspace clippy through just check, with repository lint allowances

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

## Problem

The Linux filter permits ioctl requests without checking the request number.
A workload can request synthetic input on an inherited terminal when the
surrounding kernel permits TIOCSTI.

## Solution

Refuse TIOCSTI through the existing argument-scoped filter. Compare the low
32 request bits, as Linux does, so upper bits cannot bypass the refusal.
Keep other ioctl requests available. Legacy programs that use TIOCSTI fail
with EPERM inside Box. No public interface or configuration key changes.

## Tests

ARM64 Linux: terminal_input_injection_is_refused_before_descriptor_lookup
and terminal_queries_still_work_under_the_filters pass without skips.
The former uses an invalid descriptor and covers nonzero upper request bits.
Removing the refusal fails the first test. Refusing every ioctl fails the
second test. Restoring the implementation passes both tests.

All 22 syscall table tests pass. Containment clippy with repository lint
allowances and the no-default-features build pass. The ARM64 Linux library
cross-check passes. macOS just check passes, including workspace formatting,
linting, and tests with all features. Independent adversarial review found
no code defect.

No end-to-end terminal injection reproduction or full interactive shell
compatibility test was run. Linux runtime checks used kernel 7.0.12-linuxkit;
no Linux 6.1 runtime result is claimed.
@github-actions

Copy link
Copy Markdown

Assessment: Approve

A focused, well-reasoned security fix that closes the TIOCSTI input-injection vector (first item of #51). It reuses the established argument-scoped pattern, and the tests directly address the "refusal read as success" trap called out in the issue.

Review notes
  • Correctness: The two-filter design (permit allows ioctl, restrictions refuses the TIOCSTI request number) is correct — seccomp keeps the most restrictive result, so listing ioctl in both tables is intentional. The Dword match on arg 1 mirrors the kernel's unsigned int truncation and defeats the high-bit bypass.
  • Testing: The fd = -1 positive control (unfiltered EBADF vs filtered EPERM) cleanly proves the refusal lands before descriptor lookup and never touches the operator's real terminal. The compatibility test confirms legitimate terminal queries still pass. One minor suggestion inline on tightening the winsize assertion.
  • Scope: Matches the PR's stated boundary; incomplete terminal safety is acknowledged rather than overclaimed.

Clear commit message, thorough mutation testing, and the design documents its reasoning well.

@kevin-orellana
kevin-orellana enabled auto-merge (squash) October 20, 2026 21:38
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.

4 participants