|
| 1 | +#!/bin/sh |
| 2 | +# This file is part of KASLD - https://github.com/bcoles/kasld |
| 3 | +# |
| 4 | +# check-wire-text — a component cannot put an escape sequence on the operator's |
| 5 | +# terminal. |
| 6 | +# |
| 7 | +# Three wire fields carry text a component chooses: a result's `name`, and a |
| 8 | +# disposition's `gate` and `msg`. All three are rendered, `gate` and `msg` into |
| 9 | +# the hardening report, which is the output an operator forwards. A control byte |
| 10 | +# among them is not a cosmetic flaw: cursor-movement and erase-line sequences |
| 11 | +# redraw what was already printed, so a finding can be made to read as its |
| 12 | +# opposite by the report meant to expose it. |
| 13 | +# |
| 14 | +# Two paths carry such a byte and both are covered here. The parser rejects a |
| 15 | +# record whose free-text field leaves printable ASCII, so nothing reaches the |
| 16 | +# rendered readout. `--verbose` additionally echoes each component line before |
| 17 | +# any parser sees it, so that echo strips control bytes rather than relying on |
| 18 | +# the rejection. |
| 19 | +# |
| 20 | +# check-render-color proves KASLD's OWN escapes strip back to the plain |
| 21 | +# rendering. It says nothing about escapes arriving in data, which is the gap |
| 22 | +# this guard covers. |
| 23 | +# |
| 24 | +# The stubs are shell scripts: the orchestrator execs whatever is executable in |
| 25 | +# the component directory, so no compiler is needed and the guard runs anywhere |
| 26 | +# the host binary does. |
| 27 | +# --- |
| 28 | +# <bcoles@gmail.com> |
| 29 | + |
| 30 | +set -u |
| 31 | +ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) |
| 32 | + |
| 33 | +if [ -t 1 ] || [ -n "${KASLD_COLOR:-}" ]; then |
| 34 | + RED=$(printf '\033[31m'); GREEN=$(printf '\033[32m'); YELLOW=$(printf '\033[33m') |
| 35 | + RESET=$(printf '\033[0m') |
| 36 | +else |
| 37 | + RED=; GREEN=; YELLOW=; RESET= |
| 38 | +fi |
| 39 | + |
| 40 | +# Literal control bytes, built once. The textual form "\033" is not a portable |
| 41 | +# pattern -- some greps read it as an escape and some as four characters, so a |
| 42 | +# guard written that way matches nothing and reports success on every run. |
| 43 | +ESC=$(printf '\033') |
| 44 | +CR=$(printf '\r') |
| 45 | + |
| 46 | +HOST_TRIPLE=$(cc -dumpmachine 2>/dev/null || echo unknown) |
| 47 | +BIN="$ROOT/build/$HOST_TRIPLE/kasld" |
| 48 | +if [ ! -x "$BIN" ]; then |
| 49 | + printf '%swire-text guard: SKIP%s (no host binary at %s; run make first)\n' \ |
| 50 | + "$YELLOW" "$RESET" "$BIN" |
| 51 | + exit 0 |
| 52 | +fi |
| 53 | + |
| 54 | +WORK=$(mktemp -d "${TMPDIR:-/tmp}/kasld-wiretext.XXXXXX") || exit 1 |
| 55 | +trap 'rm -rf "$WORK"' EXIT INT TERM |
| 56 | +EMPTY="$WORK/sysroot" |
| 57 | +STUBS="$WORK/components" |
| 58 | +mkdir -p "$EMPTY" "$STUBS" || exit 1 |
| 59 | + |
| 60 | +# An empty sysroot keeps the run hermetic: no host fact is read, so the output |
| 61 | +# is the stubs' contribution and the architectural defaults. |
| 62 | + |
| 63 | +# A clean record, so a run that rejects everything cannot pass by accident. |
| 64 | +cat >"$STUBS/stub_clean" <<'STUB' |
| 65 | +#!/bin/sh |
| 66 | +echo "V kernel_text:cleanname pos=base conf=parsed lo=0xffffffff8b600000" |
| 67 | +STUB |
| 68 | + |
| 69 | +# One stub per hostile field. Each carries an erase-line and a colour sequence, |
| 70 | +# the pair that rewrites a rendered line in place. |
| 71 | +cat >"$STUBS/stub_name" <<'STUB' |
| 72 | +#!/bin/sh |
| 73 | +printf 'V module:ev\033[2K\rHOSTILENAME pos=base conf=parsed lo=0xffffffffc0abc000\n' |
| 74 | +STUB |
| 75 | + |
| 76 | +cat >"$STUBS/stub_gate" <<'STUB' |
| 77 | +#!/bin/sh |
| 78 | +printf 'R cat=mitigation gate=kpti\033[2K\rHOSTILEGATE msg="blocked"\n' |
| 79 | +exit 69 |
| 80 | +STUB |
| 81 | + |
| 82 | +cat >"$STUBS/stub_msg" <<'STUB' |
| 83 | +#!/bin/sh |
| 84 | +printf 'R cat=mitigation gate=kpti msg="blocked \033[7mHOSTILEMSG\033[0m here"\n' |
| 85 | +exit 69 |
| 86 | +STUB |
| 87 | + |
| 88 | +# A high byte, covering the other half of the rule. The admissible set stops at |
| 89 | +# 0x7E, not merely below 0x20: 0x80..0x9F is the C1 control range, which a |
| 90 | +# terminal in an 8-bit locale acts on with no ESC byte involved, and it cannot |
| 91 | +# be excluded byte-wise without also excluding UTF-8 continuation bytes. A guard |
| 92 | +# that only emitted ESC would keep passing if that half were dropped. |
| 93 | +cat >"$STUBS/stub_highbyte" <<'STUB' |
| 94 | +#!/bin/sh |
| 95 | +printf 'V module:hi\233HOSTILEHIGH pos=base conf=parsed lo=0xffffffffc0def000\n' |
| 96 | +STUB |
| 97 | + |
| 98 | +chmod +x "$STUBS"/stub_* || exit 1 |
| 99 | + |
| 100 | +fail=0 |
| 101 | +checked=0 |
| 102 | + |
| 103 | +# Every mode that renders component-supplied text: the default readout, the |
| 104 | +# verbose readout (which also echoes raw component lines), and the hardening |
| 105 | +# report (where a disposition's gate and msg are shown). |
| 106 | +for mode in "-q" "-q -v" "-q -H"; do |
| 107 | + # shellcheck disable=SC2086 # $mode is a deliberate word-split option list |
| 108 | + out=$(KASLD_SYSROOT="$EMPTY" KASLD_COMPONENT_DIR="$STUBS" \ |
| 109 | + "$BIN" $mode 2>&1) || true |
| 110 | + checked=$((checked + 1)) |
| 111 | + |
| 112 | + case $out in |
| 113 | + *"$ESC"*) |
| 114 | + printf '%s escape byte reached the terminal%s: kasld %s\n' \ |
| 115 | + "$RED" "$RESET" "$mode" |
| 116 | + fail=1 ;; |
| 117 | + esac |
| 118 | + case $out in |
| 119 | + *"$CR"*) |
| 120 | + printf '%s carriage return reached the terminal%s: kasld %s\n' \ |
| 121 | + "$RED" "$RESET" "$mode" |
| 122 | + fail=1 ;; |
| 123 | + esac |
| 124 | + |
| 125 | + # A record whose free-text field left printable ASCII must not RENDER, |
| 126 | + # whichever byte took it out of the set. Under --verbose the raw component |
| 127 | + # line is echoed on purpose, so the marker legitimately appears there with its |
| 128 | + # control bytes already neutralised; the rendered readout is every other line. |
| 129 | + # Dropping the echoed lines -- those that still carry a wire tag -- separates |
| 130 | + # the two without needing to know the readout's layout. |
| 131 | + # -a: the output can carry a high byte, and grep would otherwise report |
| 132 | + # "binary file matches" instead of the lines, leaving the check below with |
| 133 | + # nothing to search and passing vacuously. |
| 134 | + rendered=$(printf '%s\n' "$out" | LC_ALL=C grep -av '^[VPSR] ' || true) |
| 135 | + for marker in HOSTILENAME HOSTILEGATE HOSTILEMSG HOSTILEHIGH; do |
| 136 | + case $rendered in |
| 137 | + *"$marker"*) |
| 138 | + printf '%s a rejected record rendered anyway%s: %s in kasld %s\n' \ |
| 139 | + "$RED" "$RESET" "$marker" "$mode" |
| 140 | + fail=1 ;; |
| 141 | + esac |
| 142 | + done |
| 143 | + |
| 144 | + # The clean record must still render: a guard that passes because nothing at |
| 145 | + # all was accepted would report success on a parser that rejects everything. |
| 146 | + # Checked under --verbose, the mode that lists a record's name at all; the |
| 147 | + # default readout reports resolved quantities rather than per-record labels. |
| 148 | + case $mode in |
| 149 | + "-q -v") |
| 150 | + case $out in |
| 151 | + *cleanname*) ;; |
| 152 | + *) |
| 153 | + printf '%s the clean record did not render%s: kasld %s\n' \ |
| 154 | + "$RED" "$RESET" "$mode" |
| 155 | + fail=1 ;; |
| 156 | + esac ;; |
| 157 | + esac |
| 158 | +done |
| 159 | + |
| 160 | +if [ "$fail" -eq 0 ]; then |
| 161 | + printf '%swire-text guard: OK%s (%d modes, 4 hostile records, none rendered)\n' \ |
| 162 | + "$GREEN" "$RESET" "$checked" |
| 163 | +else |
| 164 | + printf '%swire-text guard: FAIL%s\n' "$RED" "$RESET" |
| 165 | +fi |
| 166 | +exit "$fail" |
0 commit comments