|
| 1 | +#!/bin/sh |
| 2 | +# This file is part of KASLD - https://github.com/bcoles/kasld |
| 3 | +# |
| 4 | +# check-validators — no arithmetic-input validator accepts anything dangerous. |
| 5 | +# |
| 6 | +# extra/check-results and extra/ksymoff both feed parsed fields into shell |
| 7 | +# arithmetic, and `$(( x ))` evaluates embedded command substitutions and array |
| 8 | +# subscripts. A value like `a[$(cmd)]` reaching it runs cmd -- as ROOT in |
| 9 | +# check-results, whose documented use is `sudo check-results results.txt` and |
| 10 | +# `<component> | sudo check-results -`. Each script therefore validates every |
| 11 | +# arithmetic input first. |
| 12 | +# |
| 13 | +# The guard exists because that validator is DUPLICATED. Neither script can |
| 14 | +# source a shared library: ksymoff is installed to $PREFIX/bin as a standalone |
| 15 | +# program, and check-results is meant to be copied to a target and run there. So |
| 16 | +# there are four implementations of one security property, and a correction to |
| 17 | +# one does not reach the others. |
| 18 | +# |
| 19 | +# What is asserted is REJECTION, not sameness. The four legitimately differ in |
| 20 | +# what they accept -- is_hex refuses an 0x prefix because its callers strip one |
| 21 | +# first, and is_dec is decimal-only because --page-shift feeds a decimal range |
| 22 | +# check. Diffing them would fail on correct code. The invariant that actually |
| 23 | +# matters is that none of them lets a shell metacharacter through. |
| 24 | +# |
| 25 | +# Also asserted: |
| 26 | +# - each validator ACCEPTS a known-good value, so one that rejected |
| 27 | +# everything could not pass the corpus vacuously while breaking the tool; |
| 28 | +# - the number of @arith-validator markers in the two scripts equals the |
| 29 | +# number exercised here, so a validator added later cannot quietly escape |
| 30 | +# the corpus. |
| 31 | +# |
| 32 | +# The definitions are extracted rather than sourced: both scripts run top-level |
| 33 | +# code and cannot be sourced. Extraction also fails loudly if a validator stops |
| 34 | +# being self-contained -- which is exactly when a shared helper has crept in. |
| 35 | +# --- |
| 36 | +# <bcoles@gmail.com> |
| 37 | + |
| 38 | +set -u |
| 39 | +ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) |
| 40 | + |
| 41 | +if [ -t 1 ]; then |
| 42 | + RED=$(printf '\033[31m') |
| 43 | + GREEN=$(printf '\033[32m') |
| 44 | + RESET=$(printf '\033[0m') |
| 45 | +else |
| 46 | + RED= |
| 47 | + GREEN= |
| 48 | + RESET= |
| 49 | +fi |
| 50 | + |
| 51 | +fail=0 |
| 52 | +note() { |
| 53 | + printf '%scheck-validators: FAIL%s — %s\n' "$RED" "$RESET" "$1" >&2 |
| 54 | + fail=1 |
| 55 | +} |
| 56 | + |
| 57 | +# Pull one shell function definition out of a script, by brace depth so a |
| 58 | +# one-liner and a multi-line body both come out whole. |
| 59 | +extract() { |
| 60 | + awk -v fn="$2" ' |
| 61 | + index($0, fn "()") == 1 { inb = 1 } |
| 62 | + inb { |
| 63 | + print |
| 64 | + t = $0; o = gsub(/\{/, "{", t) |
| 65 | + t = $0; c = gsub(/\}/, "}", t) |
| 66 | + depth += o - c |
| 67 | + if (o > 0) seen = 1 |
| 68 | + if (seen && depth <= 0) exit |
| 69 | + } |
| 70 | + ' "$1" |
| 71 | +} |
| 72 | + |
| 73 | +# Inputs no validator may ever accept. Anything here reaching `$(( ))` is |
| 74 | +# command execution, or arithmetic the caller never intended. |
| 75 | +corpus=$( |
| 76 | + cat <<'ADVERSARIAL' |
| 77 | +a[$(touch /tmp/kasld-pwn)] |
| 78 | +$(id) |
| 79 | +`id` |
| 80 | +$((1)) |
| 81 | +${IFS} |
| 82 | +0x1$(id) |
| 83 | +x[0] |
| 84 | +1+1 |
| 85 | +1-1 |
| 86 | +1;id |
| 87 | +1 2 |
| 88 | +-1 |
| 89 | ++1 |
| 90 | +0xg |
| 91 | +zz |
| 92 | +1.0 |
| 93 | +0x |
| 94 | +* |
| 95 | +? |
| 96 | +../etc/passwd |
| 97 | +ADVERSARIAL |
| 98 | +) |
| 99 | + |
| 100 | +# validator | script | convention | a value it MUST accept |
| 101 | +# echo — accepted when it echoes something back (rejection is empty output) |
| 102 | +# exit — accepted when it returns 0 |
| 103 | +checks=" |
| 104 | +numeric|extra/check-results|echo|deadbeef |
| 105 | +hex16|extra/check-results|echo|deadbeef |
| 106 | +is_hex|extra/ksymoff|exit|deadbeef |
| 107 | +is_dec|extra/ksymoff|exit|1234 |
| 108 | +" |
| 109 | + |
| 110 | +n_checked=0 |
| 111 | +tmp=$(mktemp) || exit 1 |
| 112 | +trap 'rm -f "$tmp"' EXIT INT TERM |
| 113 | + |
| 114 | +# Read into positional args so the counters below survive (a `while read` on the |
| 115 | +# far side of a pipe runs in a subshell). |
| 116 | +oldifs=$IFS |
| 117 | +IFS=' |
| 118 | +' |
| 119 | +# shellcheck disable=SC2086 # deliberate split: one row per newline |
| 120 | +set -- $checks |
| 121 | +IFS=$oldifs |
| 122 | + |
| 123 | +for row in "$@"; do |
| 124 | + [ -n "$row" ] || continue |
| 125 | + fn=$(printf '%s' "$row" | cut -d'|' -f1) |
| 126 | + script=$(printf '%s' "$row" | cut -d'|' -f2) |
| 127 | + conv=$(printf '%s' "$row" | cut -d'|' -f3) |
| 128 | + good=$(printf '%s' "$row" | cut -d'|' -f4) |
| 129 | + src="$ROOT/$script" |
| 130 | + |
| 131 | + if [ ! -f "$src" ]; then |
| 132 | + note "$script is missing" |
| 133 | + continue |
| 134 | + fi |
| 135 | + def=$(extract "$src" "$fn") |
| 136 | + if [ -z "$def" ]; then |
| 137 | + note "$script: cannot extract $fn() -- is it still a self-contained function?" |
| 138 | + continue |
| 139 | + fi |
| 140 | + n_checked=$((n_checked + 1)) |
| 141 | + |
| 142 | + # Non-vacuity first: a validator that rejects everything would sail through |
| 143 | + # the corpus below while breaking the tool it guards. |
| 144 | + if [ "$conv" = echo ]; then |
| 145 | + got=$(printf '%s\n' "$def" "out=\$($fn '$good'); [ -n \"\$out\" ]" | sh 2>/dev/null && echo yes) |
| 146 | + else |
| 147 | + got=$(printf '%s\n' "$def" "$fn '$good'" | sh 2>/dev/null && echo yes) |
| 148 | + fi |
| 149 | + [ "${got:-}" = yes ] || |
| 150 | + note "$script: $fn() rejects '$good', which it must accept" |
| 151 | + |
| 152 | + # Every adversarial input must be refused. |
| 153 | + printf '%s\n' "$corpus" | while IFS= read -r bad; do |
| 154 | + if [ "$conv" = echo ]; then |
| 155 | + out=$(printf '%s\n' "$def" "$fn \"\$1\"" | sh -s -- "$bad" 2>/dev/null) |
| 156 | + [ -z "$out" ] || printf 'ACCEPTED %s\n' "$bad" |
| 157 | + else |
| 158 | + printf '%s\n' "$def" "$fn \"\$1\"" | sh -s -- "$bad" 2>/dev/null && |
| 159 | + printf 'ACCEPTED %s\n' "$bad" |
| 160 | + fi |
| 161 | + done >"$tmp" 2>/dev/null |
| 162 | + while IFS= read -r line; do |
| 163 | + note "$script: $fn() accepted ${line#ACCEPTED }" |
| 164 | + done <"$tmp" |
| 165 | +done |
| 166 | + |
| 167 | +# A validator added later must not escape the corpus. The marker sits at each |
| 168 | +# definition, so registering one is the same act as writing it. |
| 169 | +markers=$(cat "$ROOT/extra/check-results" "$ROOT/extra/ksymoff" | |
| 170 | + grep -c '@arith-validator') |
| 171 | +if [ "${markers:-0}" -ne "$n_checked" ]; then |
| 172 | + note "$markers @arith-validator markers but $n_checked exercised -- register the new one in this guard" |
| 173 | +fi |
| 174 | + |
| 175 | +if [ "$fail" -ne 0 ]; then |
| 176 | + exit 1 |
| 177 | +fi |
| 178 | +printf '%scheck-validators: OK%s (%s arithmetic-input validators, none accepts the adversarial corpus)\n' \ |
| 179 | + "$GREEN" "$RESET" "$n_checked" |
0 commit comments