Skip to content

fix(containment): restrict Linux terminal and console controls - #255

Merged
kevin-orellana merged 6 commits into
mainfrom
feature/linux-console-controls
Nov 2, 2026
Merged

kevin-orellana merged 6 commits into
mainfrom
feature/linux-console-controls

Conversation

@kevin-orellana

@kevin-orellana kevin-orellana commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Human readable section

Problem. An untrusted workload can inherit the operator's Linux terminal or console and interfere with input, keyboard, display, or switching state.

Solution. Refuse console paste, terminal input-processing changes, and twenty console mutations. Keep ordinary terminal input/output, settings, resizing, foreground interrupts, and separately numbered read-only console queries.

Trade-off. Console management programs lose these operations. Font queries share a request with font changes, so those queries are also refused. Ordinary settings and terminal escape sequences remain permitted; this is not complete terminal isolation.

Dependencies. The filter defines the missing request constants privately from Linux's published headers. The existing released libc 0.2.189 dependency, manifests, and lockfile remain unchanged. rust-lang/libc#5684 stays open as an optional upstream contribution; this Box fix does not depend on its merge or release.

Description

Refuse console paste and terminal input-processing changes. Add request-specific denials for keyboard/display modes, keyboard lock state and lights, accent tables, fonts, color palettes, screen/Unicode maps, console switching, deallocation, and console-wide resizing. Cover VT_SETACTIVATE, which combines activation with mode changes, beside the separate operations.

The filter compares the low 32 request bits, matching Linux's argument handling. It retains the input-injection refusal and includes the paste and input-processing refusals from #240. Read-only requests remain available when their request number distinguishes them. KDFONTOP carries its operation behind a pointer, so this scalar filter refuses the entire request. Ordinary resizing uses TIOCSWINSZ and remains permitted.

The protected side is the surrounding operating system and its resources. These restrictions apply to untrusted contained processes.

This PR supersedes #240 and includes its complete head, c47b6bd2c1dcb498b5a675e129cb237df9892667. Review the terminal and console restrictions together here. The earlier PR retains its discussion and validation evidence. This PR targets main and has no merge prerequisite on #240.

Current head: 37b44c66f7bf630f5a888ee805bac1b7701e0e9a. Independent adversarial code review at 3640621 found no implementation defect. The hosted reviewer then found conflicting instructions about constant sources. The documentation-only follow-up names the twenty-console-request exception while preserving the general rule for every other number and missing name. Issue #253 now states the same acceptance criterion. Kevin approved this scope and keeping upstream libc work optional. The hosted re-review at 37b44c6 approves the final wording and reports no open finding. Both addressed review threads are resolved. These reviews are not formal human approval.

Authored and reviewed with Codex assistance.

Related Issues

Addresses #253 and the terminal-control portion of #51. Supersedes #240 with all its changes included. This PR does not close the broader scope of #51. The optional upstream contribution rust-lang/libc#5684 remains open.

Type of Change

Bug fix

Testing

Console paste effect validation, November 2 UTC. A fresh disposable ARM64 console guest passed 54 cases: two identities, three filter modes, three upper-bit patterns, and selection-only, paste-only, and combined operations. An outside parent checked exact input bytes or preserved selection after child exit. Privileged unfiltered controls injected a harmless marker; the production filters prevented it. Removing only the TIOCLINUX comparison restored the forbidden effect in all nine privileged cases. Filtered ordinary-user input, output, settings, resize/query controls, and outside restoration passed.

The guest used Linux 6.12.113-138.209.amzn2023.aarch64, a dummy console, 512 MiB, one CPU, and no network. This kernel already refuses selection and paste to uid 1000 with no effective capabilities. The successful effect control used uid 0 with administrator capability inside the guest; it is not an ordinary-user exploit reproduction or root-launched Box qualification. The guest ran both retained production filters from #255 at 3640621, byte-identical at 37b44c6, rather than the full Box launcher. Its TIOCLINUX rule is the restriction introduced by #240. Full launcher evidence remains the separate host runs. No physical keyboard/display, minimum-kernel, or cloud qualification is claimed. Raw evidence is retained in the implementation workspace under evidence/console-paste/.

Both follow-up commits change only crates/containment/AGENTS.md. Documentation consistency and git diff --check passed; code and tests are byte-identical to 3640621. The hosted test gate passed for that code revision. The results below are retained code evidence, not newly executed tests for the documentation-only commit.

Current checks use the declared, released dependencies. Cargo metadata confirms registry libc 0.2.189, with no Cargo patch or fork dependency.

  • All seven focused terminal tests passed. The twenty new denials and fifteen permitted-query controls use independent Linux request values.
  • A native C compiler checked all 55 production/test values against installed Linux console headers.
  • The full Box launcher passed 82 disposable-terminal cases: 60 denials, 20 uncontained controls, and two successful interaction/restoration controls. An outside observer checked terminal settings, line discipline, and foreground ownership after exit.
  • Fresh independent review passed 552 containment tests, all-feature and no-default builds, strict containment lint, and formatting. Two deliberate mutations caused three expected failures: a wrong constant breaks denial and query controls; a full-width comparison breaks upper-bit refusal.
  • An independent filter evaluator checked 196,608 request values and found exactly the 23 intended denials.
  • The deterministic suite passed 124 of 133 cases, with nine macOS-only skips and no integrity errors.
  • scripts/test-all.sh passed 4,579 top-level tests across 138 groups, with zero failures and 17 ignored tests. One nested re-execution is excluded. The example build passed. No scripted examples exist; the manual SDK tutorial did not run.
  • Strict workspace lint passed with zero warnings using the repository's prescribed vendored-code allowances.

Broader-suite limits. Four runtime checks remain limited: missing credential daemon, missing installed socket-alias helper, removed cardinality probe, and an unavailable alias-read assertion. The performance probe reports the existing absolute latency-target miss in #150; passing relative assertions do not establish performance qualification.

The earlier 25-case disposable-console guest run used the previous constants overlay. The fresh registry build produces byte-identical filter instructions (SHA-256 d9d3d9b02354b5990b05befef0126eefa86ea6cbe569ba2f09b769885342e58e; 587 permit and 322 restriction instructions). The guest tested six mode/switching routes, including VT_SETACTIVATE, with an outside observer after child exit. It also tested console queries, settings/restoration, ordinary resizing, and output. These are retained guest results, not a newly executed guest run.

Environment limits. The ARM64 guest used Linux 6.12.113-138.209.amzn2023.aarch64, a dummy console, 512 MiB, one CPU, no network, and workload uid/gid 1000. It did not test a physical keyboard/framebuffer or the complete Box launcher in the guest. Fourteen other new mutations have filter-denial tests and source inspection without console-effect reproduction. Minimum kernels, root workloads, native macOS, and cloud qualification remain untested locally. Ordinary settings and terminal escape sequences remain permitted.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

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 inherited terminal remains a host resource. The existing TIOCSTI refusal leaves console controls and line-discipline changes reachable from an untrusted workload.
The baseline terminal_discipline_cannot_change_or_break_input test demonstrates that TIOCSETD can replace terminal input processing.

## Solution

Refuse TIOCLINUX and TIOCSETD beside TIOCSTI in the existing deny-only filter. Compare the low 32 request bits, as the kernel does.
Keep ordinary terminal operations permitted. Document the restricted requests and remaining terminal limits. No public API or policy grant changes.

## Tests

The containment suite passes 553 tests with all features. The five added tests cover truncated request arguments, unchanged terminal discipline and input queue, normal terminal operations, and foreground interrupts.
Baseline and separate TIOCLINUX/TIOCSETD removal controls fail the relevant denial tests. Successful-operation controls pass on both baseline and repaired builds.
A disposable full-CLI harness passes 17 scenarios, including input/output, resizing, interrupts, and restoration.
Native validation uses ARM64 Amazon Linux 2023 and kernel 6.12.111. A real virtual console is unavailable; this does not claim console effect coverage, full terminal isolation, or cloud qualification.
## Problem

An untrusted workload can inherit the operator console and change its keyboard,
display, or switching state. The existing terminal filter permits these requests.

## Solution

Add twenty request denials to the existing low-32-bit filter. Preserve ordinary
terminal interaction and separately numbered queries. KDFONTOP shares font
queries and changes, so both are refused. No public interface changes.

Request constants come from proposed rust-lang/libc#5684. The declared libc
0.2.189 dependency lacks them. This draft requires its backport and release,
then a real dependency update. Keep handwritten constants out of Box.

## Tests

With an isolated libc constants overlay, all seven terminal tests passed.
console_mutations_are_refused_before_device_dispatch detects each of twenty
removed denials and a wrong 64-bit comparison. The read-only query control
detects accidental KDGETMODE refusal. All 22 mutation checks detected the defect.
Full Box disposable-terminal checks passed 82 cases, including restoration and
foreground interrupts. The disposable console guest passed 25 cases, including
six mutation routes and an outside observer after child exit.

Containment passed 552 top-level tests, all-feature and no-default builds,
strict lint, and formatting. Full workspace tests and lint remain pending.
The declared-dependency check fails on the twenty missing libc constants.
No physical keyboard/display, minimum-kernel, root-workload, or cloud
qualification ran. The broad deterministic suite was not rerun for this draft.
@kevin-orellana
kevin-orellana marked this pull request as ready for review November 1, 2026 09:11
@kevin-orellana
kevin-orellana enabled auto-merge (squash) November 1, 2026 09:15
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Assessment: Request Changes

I reviewed head 3640621, only the companion commits after #240 (c47b6bd..HEAD). The filter logic is correct for every case I listed. The one open item is the inline comment: the hand-written request numbers conflict with a standing rule in crates/containment/AGENTS.md, and the PR does not reconcile them.

Assumptions and rules checked that held
  • I checked all 20 private constants against <linux/kd.h> and <linux/vt.h> with a C compiler, and they match.
  • The low-32-bit Dword comparison refuses each request when the upper bits are 0, 1<<32, or 0xffff_ffff_0000_0000. The 7 terminal tests pass locally.
  • The separately numbered read-only queries (KDGETMODE, VT_GETSTATE, …) still reach device dispatch. Only KDFONTOP get is refused, and the PR documents that as a trade-off.
  • I read vt_ioctl.c and keyboard.c at the pinned kernel commit. Requests that are not on the list (KDSKBENT, KDSKBSENT, KDSETKEYCODE, KDKBDREP, VT_LOCKSWITCH/UNLOCKSWITCH, KDSIGACCEPT) still need CAP_SYS_TTY_CONFIG or CAP_KILL in the initial namespace, so the documented claim about capability checks holds.
  • The legacy PIO_FONT, PIO_FONTX, and PIO_FONTRESET requests were removed in v5.12 and the removal was backported to linux-5.4.y and 5.10.y. The "fonts refused" claim is therefore true on maintained kernels without those numbers in the list.
  • TIOCSWINSZ and ordinary terminal settings are still permitted, as the PR states.
  • No frozen surface changes: MEDIATED changes only its reason string, and no pub use set changes.

Apart from the rule conflict, the change is narrow and well tested, and the permitted-query tests are a good guard against refusing too much.

Comment thread crates/containment/AGENTS.md Resolved
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Assessment: Comment (approve once the rule wording keeps its old scope)

Re-review at 7f1881f. Since 3640621 the only change is in crates/containment/AGENTS.md; the code and tests are identical. The conflict from my last review is fixed: line 797 now names the twenty-request exception, and #253's acceptance criterion says the same. The one item left is the inline suggestion. The reworded rule no longer covers the argument constants the filter reads from libc.

Checked and held
  • Code unchanged: git diff 3640621 7f1881f touches only AGENTS.md, so the earlier checks still apply: all 20 constants match the headers, the 7 terminal tests pass, and the upper-bit refusal works.
  • No contradiction left: line 797 now agrees with the terminal bullet at line 952. Both allow private UAPI constants only where libc lacks the names, and neither depends on linux: add virtual-console keyboard and display ioctl constants rust-lang/libc#5684.
  • Exception is narrow: it covers only the 20 requests in terminal_control_rules. TIOCSTI, TIOCLINUX, and TIOCSETD still come from libc.
  • Header source is pinned: the pinned UAPI source is the kd.h link at commit adc21867 in docs/design/linux-enforcement.md.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Assessment: Approve

Re-review at 37b44c6. The only change since 7f1881f is a 2-line edit to crates/containment/AGENTS.md. It brings back "Take every number from libc" and "never #[cfg] them away", and keeps the twenty-request console exception. That resolves my earlier suggestion. Nothing is left open.

Checked and held
  • Code unchanged: git diff 3640621 37b44c6 touches only AGENTS.md. The earlier verification still applies: the 20 constants match the headers, the 7 terminal tests pass, and the upper-bit refusal works.
  • Rule scope restored: AF_UNIX/AF_INET, PROT_*, AT_EMPTY_PATH, and EPERM are covered by "every number" again. The only private u32 constants in syscall.rs are the 20 console requests the exception names.
  • No contradictions: line 797 agrees with the terminal bullet at line 952 and with the acceptance criterion in Linux: restrict inherited virtual-console keyboard and display controls #253.

@kevin-orellana kevin-orellana changed the title fix(containment): refuse virtual-console state changes fix(containment): restrict Linux terminal and console controls Nov 2, 2026
@kevin-orellana
kevin-orellana merged commit 18d3704 into main Nov 2, 2026
26 checks passed
@kevin-orellana
kevin-orellana deleted the feature/linux-console-controls branch November 2, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area - Agent Containment bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants