Skip to content

fix(minvmd): name a guest's bytes in a log line bounded and escaped - #3164

Merged
bryan-minimal merged 1 commit into
mainfrom
fix/minvmd-escape-guest-log
Nov 16, 2026
Merged

bryan-minimal merged 1 commit into
mainfrom
fix/minvmd-escape-guest-log

Conversation

@bryan-minimal

@bryan-minimal bryan-minimal commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

A refused request head's target and a guest-door parse refusal reached minvmd's log as the guest spelled them. A CR, NUL or other control byte in a guest's request line went into the warn line raw.

The bug

  • refuse_head (crates/minvmd/src/net/egress_gate.rs) logged request_target = %String::from_utf8_lossy(..). It was capped at 64 bytes but not escaped. The request line splits on LF only and strip_cr drops just a trailing CR, so a guest's target (or a line with no target at all) can carry CR, NUL, ESC, other C0 bytes or non-UTF-8 bytes.
  • tracing-subscriber's fmt writer (0.3.23) already escapes ESC, BEL, BS, FF, DEL and C1, but not CR, LF, NUL, TAB or the rest of C0. The OTel layer gets the attribute raw. In the file log the real hazard is CR (it overwrites the line in a terminal tailing the log) and NUL; ESC matters only to raw consumers.
  • A second site: serve_connection in crates/minvmd/src/control.rs logs a parse refusal's serde_json_lenient error at debug. serde's unknown variant `{}` and unknown field `{}` messages quote the guest's word with Display, so on the guest report door the line is the guest's.

The change

  • New crates/minvmd/src/loggable.rs:
    • text_within(&str, max) -> Cow<str>: printable ASCII (space included) within max passes borrowed. Anything else is {:?}-escaped, cut on a char boundary at max, with a ... (N bytes in all) marker.
    • bytes_within(&[u8], max): the same, but it escapes with escape_ascii in quotes, so a non-UTF-8 byte is named as \xNN, not U+FFFD.
  • RefusedHead::named() goes through bytes_within with a bound of the escaped worst case of the 64-byte cap, so the target is never cut a second time (no misleading "64 bytes in all" marker).
  • control.rs: the parse refusal's debug line goes through text_within(&error, MAX_LOGGED_PARSE_ERROR) (512). The reply to the client is unchanged; JSON escapes it.

Reviewed and left alone: dns_pins.rs's name = %key.name (hickory's Name Display escapes non-printable label bytes), control.rs's mint/resume lines (a registered name, or ?), render_record and warn_interim_publish (already {:?} with a bound), and the other from_utf8_lossy uses in minvmd (test code or host-side input).

Tests

  • loggable: honest values pass borrowed; CR, LF, TAB, NUL, BEL, BS, ESC, DEL, U+0085, U+2028 and U+2029 are escaped (both functions); non-UTF-8 bytes; a 7000-byte line cut with its marker; 120 CRs cut; a char-boundary sweep.
  • egress_gate::a_refused_head_names_its_target_escaped: an honest target, a CR/ESC/NUL target, a no-target line with CR and 0xff, and a full head of ESC named whole at the 64-byte cap.
  • egress_gate::a_refused_head_cannot_break_its_refusal_line: through the gate harness, the warn line holds request_target="/a\rWARN\x1b[2J\x00forged=1" and no raw CR, ESC or NUL.

With #3151

#3151 (fix/1180-egress-gate-parse) fixes refuse_request, a different site, with a local helper, loggable_what. The two touch different functions and should merge without a textual conflict. loggable_what has the same semantics as text_within with an explicit bound: once both merge, #3151's loggable_what(what) should switch to crate::loggable::text_within(what, MAX_LOGGED_WHAT) and its local helper go. Until #3151 merges, refuse_request on main still logs what raw.

Verification

  • prepush (CI's exact commands: fmt, clippy -D warnings on minvmd, nextest): green at 6dad730
  • No lab scenario: no decision changes, only how a refusal line spells the guest's bytes; the unit tests above cover it.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Diagnostic logs now limit the length of parse-error messages and safely format unusual request targets, helping keep log entries readable. Error details returned to clients remain complete.

A refused request head's target and a guest-door parse refusal were logged with `%` as the
guest spelled them. The file appender escapes ESC and a few other terminal controls, but not
CR, LF, NUL or the rest of C0, and an exporter's attribute is written raw: a CR in a request
line could overwrite the warn line in a terminal tailing the log.

A new crate module, loggable, names a value as it is when it is printable ASCII within a
bound, and otherwise quoted and escaped, cut on a char boundary with a marker naming its full
length. The egress gate's refused-head line names its 64-byte target through it, byte by byte,
never cut a second time; the control door's parse refusal names the parser's message through
it, since an unknown verb or field is quoted as spelled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bryan-minimal
bryan-minimal requested a review from a team as a code owner November 8, 2026 00:37
@coderabbitai

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

The change adds bounded formatting helpers for text and bytes, then uses them in parse-error and refused-head logs. Parse failures still return the full error to the client.

Changes: Bounded log values

Layer / File(s) Summary
Log-value formatting helpers
crates/minvmd/src/lib.rs, crates/minvmd/src/loggable.rs
Adds text_within and bytes_within helpers. They escape non-printable or non-ASCII values and truncate over-limit output. Tests cover escaping, invalid UTF-8, length markers, and character-boundary truncation.
Bounded parse-error and refused-head logs
crates/minvmd/src/control.rs, crates/minvmd/src/net/egress_gate.rs
Parse-error logs use a 512-byte limit while client replies retain the full error. Refused-head logs use bounded, escaped request targets. Tests cover printable, control, non-UTF-8, and long targets.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: norrietaylor

Merge Risk: Minimal · up to 6dad7

The changed refusal logs escape guest-controlled values, and no material issue remains identified before merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title follows the Conventional Commit form type(scope): summary and names the main change: guest bytes in a minvmd log line are now bounded and escaped. The changeset adds loggable.rs, applies i…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is thorough and on-topic. It explains the bug (raw guest-controlled control and non-UTF-8 bytes in refused-head target and parse-refusal log lines), the fix (new loggable helpers, boun…
Full details: Title check

Explanation

The title follows the Conventional Commit form type(scope): summary and names the main change: guest bytes in a minvmd log line are now bounded and escaped. The changeset adds loggable.rs, applies it to refused-head targets in egress_gate.rs, and applies it to parse-refusal errors in control.rs, so the title matches the primary change. It is 67 characters, within a reasonable length for a title.

Full details: Description check

Explanation

The description is thorough and on-topic. It explains the bug (raw guest-controlled control and non-UTF-8 bytes in refused-head target and parse-refusal log lines), the fix (new loggable helpers, bounded escaping, 512-byte limit for parse errors), tests added, a dependency on PR #3151, and verification (prepush green at 6dad730, no lab scenario). The template's Summary, Testing and Checklist sections are substantively present. The title is not in the template's Conventional Commit form in the provided text, but the title is outside the description body. The Checklist items (docs updated if behavior changed, BREAKING CHANGE footer) are not ticked or addressed. Behavior changed in logs, so the author should state whether docs were updated and confirm that no breaking change exists.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks the bytes at play
And keeps raw control marks away
The logs stay bounded, neat, and bright
While full errors reach their client right
The rabbit hops through tests today

@minimal-foundry

minimal-foundry Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Approved: no findings at 6dad730db4f6.

Nothing to do.

Clobber: none seen.

@minimal-foundry minimal-foundry Bot 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.

minimal-review — advisory review

Verdict COMMENT — No findings.

Panel: 1 of 3 personas ran (correctness); did not complete: security, supply-chain.

@minimal-foundry minimal-foundry Bot added the review:approved minimal-review approved the current head label Nov 8, 2026

@coderabbitai coderabbitai Bot 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.

Additional comments: 1

crates/minvmd/src/loggable.rs: 1

51-57: Trivial · Maintainability & Code Quality · Low value

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Truncation can cut an escape sequence in the middle.

bounded cuts the escaped string at max bytes on a char boundary. For bytes_within, the cut can split a \xNN escape or remove the closing quote. The result is still safe, with no raw control bytes. A reader may see a partial escape such as \x1. The MAX_NAMED_TARGET_ESCAPED bound avoids this for refused heads. Direct callers with a small max do not get that protection.

Document that behavior on bytes_within. The truncation marker already tells the reader that the value is cut.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 22aaf86d-a944-422b-9fd7-cfee7555789b
Commits

Reviewing files that changed from the base of the PR and between 78a60b2 and 6dad730.

Files selected for processing: 4

  • crates/minvmd/src/control.rs
  • crates/minvmd/src/lib.rs
  • crates/minvmd/src/loggable.rs
  • crates/minvmd/src/net/egress_gate.rs

Included review availability: This review used your included allowance. Your plan provides up to 5 included reviews per hour; 1 remain after this review.

@agent-137 agent-137 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.

Review on Karl's behalf, at 6dad730db. This is a comment, not an approval. Nothing was built for it: every point comes from reading the code, and each finding says how it was checked.

  • CI at head 6dad730db: all green. Every lane aggregator passed.
  • Existing reviews: CodeRabbit COMMENTED at head with a real review: one trivial, unverified point that a cut can split a \xNN escape. I am not repeating it. minimal-review gave COMMENT with no findings, but only 1 of 3 personas ran (security did not complete). No humans have reviewed.

You asked whether the fix covers every guest-controlled string minvmd logs. It does not. The two fixed sites are correct, but two other places still log guest-chosen strings without escaping or without a bound. I swept these files for guest-sourced log fields:

  • control.rs: every ControlDoor::GuestReports handler and the host_ip reconcile.
  • egress_gate.rs: every log site outside the tests.
  • guest_telemetry.rs.
  • rpc_client.rs.
  • cmd/stop.rs.

Should-fix

  1. The guest's session-list reply reaches a warn line unescaped. crates/minvmd/src/control.rs:1150-1155.
    • The host_ip reconcile logs error = %format!("{error:#}") when read_guest_box_states fails.
    • That error comes from rpc_client::call_oneshot (rpc_client.rs:189): serde_json_lenient::from_slice(..).with_context(|| "decode response for ListSessions"). The {:#} format prints the whole chain, including serde's own message.
    • The guest's minimald writes the response. ListSessionsEntry.status is boxes::BoxState, a rename_all = "snake_case" enum with no #[serde(other)]. So a reply with "status":"x\r\u001b[2J..." produces serde's unknown variant `x<CR><ESC>[2J...` , with the guest's word quoted through Display.
    • That is the same hazard this PR fixes at serve_connection, here at warn level rather than debug. It fires on every switch from success to failure, and the read runs at least every 30 s and on every guest box event.
    • uuid parse errors on id quote the offending character the same way.
    • Fix: pass this field through crate::loggable::text_within(&format!("{error:#}"), MAX_LOGGED_PARSE_ERROR), and do the same for the debug line at :1159.
    • How I verified: I read the code path and the BoxState derive (crates/boxes/src/lib.rs:955-958). I did not build or run it.
  2. The guest's host_ip_name is escaped but has no bound. crates/minvmd/src/control.rs:1663 (mint_box_id_reply).
    • The success line tracing::info!(…, host_ip_name = ?request.host_ip_name, …, "minted the box id for an in-VM create") logs a guest-door field with ?. That escapes it, but nothing bounds its length.
    • A MintBoxId with no switch address and a host_ip_name that is not registered takes the (None, None) fresh-mint arm and always reaches this line.
    • So root in the VM can send {"verb":"mint_box_id","host_ip_name":"<~64 KiB>"} in a loop. Each request writes a ~64 KiB info line (the request-line cap is MAX_REQUEST_LINE), with no rate limit on this path.
    • The PR body lists "control.rs's mint/resume lines (… or ?)" as reviewed and left alone. ? covers the escaping, but not the length.
    • Fix: host_ip_name = %request.host_ip_name.as_deref().map(|n| loggable::text_within(n, 128)).unwrap_or_default() or similar.
    • How I verified: I read mint_box_id_reply, MintBoxIdRequest (minimald-rpc/src/lib.rs:937-959) and read_request_line. I did not build it.

Nit

  1. The "printable" shortcut lets a guest forge fields inside the line. crates/minvmd/src/loggable.rs:45-46, plus the module doc at :14, which says "cannot break the line or forge one".
    • is_plain passes space, ", = and \ through raw and unquoted. The fmt layer writes a % field as-is.
    • Example: a request line with four words, such as GET / x rule_matched="allow". of_head refuses it as RefusedHead::new(line) (egress_gate.rs:1074-1077), so the whole line is the "target". It is printable, so it is logged as request_target=GET / x rule_matched="allow", and a grep for rule_matched= now matches a field the guest wrote.
    • Separately, a guest that literally sends "/a\rWARN" (backslash, r) logs output identical to the escaped form of a real CR. A reader cannot tell the two apart.
    • Neither case breaks the line, so this is not a should-fix. Narrowing the plain set (no space, ", \ or =), or always quoting, would make the doc's claim true.
    • How I verified: I read is_plain, of_head and refuse_head.
  2. Context, not a new finding. refuse_request (egress_gate.rs:4277, port_or_name = %what) still logs raw on main until #3151 merges. The PR body already says so. Once both are in, make sure #3151 switches to loggable::text_within.

Checked and found fine:

  • text_within and bytes_within. Debug and escape_ascii both escape " and \, so a value on the escaped path cannot close its own quotes.
  • MAX_NAMED_TARGET_ESCAPED (64×4+2) means the bounded target is never cut a second time.
  • The has_no_raw_control tests.
  • audit_refused_egress already runs proto and rule through clamp_guest_text, which keeps printable ASCII only.
  • audit_expose_decision logs enums only.
  • admit_report and withdraw_report log host-held names and addresses.
  • guest_telemetry logs counts and its own errors, and none of the guest's record text.
  • In cmd/stop.rs, error = %e on the shutdown RPC uses the non-alternate format, so it prints only the top context and no serde text. ?resp is escaped, and the response size is capped (MAX_RESPONSE_BYTES).

Verdict: needs changes. Should-fix 1 is the same class of bug this PR fixes, at a higher log level. Should-fix 2 is a small missing bound.

@bryan-minimal
bryan-minimal added this pull request to the merge queue Nov 16, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 16, 2026
@bryan-minimal
bryan-minimal added this pull request to the merge queue Nov 16, 2026
Merged via the queue into main with commit 3cd06b2 Nov 16, 2026
36 checks passed
@bryan-minimal
bryan-minimal deleted the fix/minvmd-escape-guest-log branch November 16, 2026 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review:approved minimal-review approved the current head

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants