omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / visor / check_authority_link.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Omega Visor V1, lane 7: authority isolation check (objects and OM-2 sources). |
| 18 | # "The Visor can ask. The Visor cannot grant." |
| 19 | # |
| 20 | # Two halves: |
| 21 | # 1. Objects (nm): fails when a Visor C object in $BUILD/visor (the test-only |
| 22 | # runtime-linked adapters) references an authority-mutating symbol from |
| 23 | # forbidden_symbols.txt, references any runtime / Aienux / publish-gate |
| 24 | # symbol at all (^rx_ ^aienos_cap_ ^rc_), or DEFINES a forbidden name, and |
| 25 | # checks that visor_effect_request.o exports only its four public functions. |
| 26 | # 2. OM-2 sources: the omega binary is pure seeded OM-2 with no symbol table, |
| 27 | # so `nm` sees nothing in it. Its isolation is checked on the sources it is |
| 28 | # built from (every tools/omega*.om, om/visor/*.om, src/visor/*.om, |
| 29 | # src/language/*.om, om/aarch64/aarch64_om2.om, plus any file named in |
| 30 | # $OMEGA_OM_SRCS, which the mk rule passes). On every code line (note lines |
| 31 | # skipped, string literals blanked) no identifier may be a forbidden name or |
| 32 | # start with rx_, aienos_cap_ or rc_ (a call to, or a local definition of, |
| 33 | # an authority operation), and no identifier may contain `authorized` (the |
| 34 | # Visor holds no authorization state). In string literals an `authorized` |
| 35 | # key may only be followed by false. |
| 36 | # Host words: an ALLOWLIST. The host words are derived from the compiler's own |
| 37 | # keyword table, seed/omc/genkw.awk, as seed/effects/check.sh derives them |
| 38 | # (every keyword with code >= 100 except the non-host const, qty, fma, cpufence |
| 39 | # and cpurelax, plus read and write). A code line may use only these host words, |
| 40 | # the ones the omega sources use today (derived by scanning them, 2026-11-23): |
| 41 | # read stdin session lines (visor_console) and bytes of files opened read-only |
| 42 | # write stdout/stderr renderings (visor.om ob_write_fd/v_write_all, visor_console, |
| 43 | # tools/omega.om); it writes only to descriptors the parent passed in, as no |
| 44 | # allowed word creates a writable descriptor |
| 45 | # fopen open for reading only: the flags must be the literal 0 (O_RDONLY) or |
| 46 | # 16384 (O_RDONLY|O_DIRECTORY), checked below |
| 47 | # fclose close those read-only descriptors |
| 48 | # stat size and type of a file or directory before reading it |
| 49 | # lstat the evidence explorer refuses symbolic links (visor_evidence) |
| 50 | # getdnts list a directory opened read-only (the evidence explorer) |
| 51 | # argcnt the command-line argument count (tools/omega.om) |
| 52 | # argread a command-line argument (tools/omega.om) |
| 53 | # Any other host word fails (process, socket, mapping, device, file-system change, |
| 54 | # descriptor duplication, environment, timing, ...). An allowlisted word that no |
| 55 | # scanned source uses any more also fails, so the list stays what omega uses. |
| 56 | # Residual (not claimed): write on a descriptor the parent left open is not |
| 57 | # restricted further; the Visor cannot open, duplicate or create one. |
| 58 | # A bite control runs every time: each forbidden form is planted in a copy of |
| 59 | # a shipped source and the OM scan must fail on it; a planted note line and |
| 60 | # string literal must not trip it. A control that does not bite fails the check. |
| 61 | # Usage: tests/visor/check_authority_link.sh [build_dir] (default: build) |
| 62 | set -u |
| 63 | BUILD=${1:-build} |
| 64 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 65 | ROOT=$(cd "$HERE/../.." && pwd) |
| 66 | LIST="$HERE/forbidden_symbols.txt" |
| 67 | NM=${NM:-nm} |
| 68 | fail=0 |
| 69 | checked=0 |
| 70 | |
| 71 | [ -f "$LIST" ] || { echo "FAIL: missing $LIST"; exit 1; } |
| 72 | forbidden=$(sed -e 's/#.*//' -e 's/[[:space:]]//g' "$LIST" | grep -v '^$') |
| 73 | [ -n "$forbidden" ] || { echo "FAIL: forbidden list is empty"; exit 1; } |
| 74 | |
| 75 | set -- "$BUILD"/visor/*.o |
| 76 | if [ ! -e "$1" ]; then |
| 77 | echo "FAIL: no objects in $BUILD/visor (build the omega target first)" |
| 78 | echo "OMEGA_VISOR_AUTHORITY_ISOLATION_FAIL" |
| 79 | exit 1 |
| 80 | fi |
| 81 | |
| 82 | check_file() { |
| 83 | f=$1 |
| 84 | if [ ! -e "$f" ]; then |
| 85 | echo "skip: $f (not built)" |
| 86 | return 0 |
| 87 | fi |
| 88 | checked=$((checked + 1)) |
| 89 | undef=$($NM -u "$f" 2>/dev/null | awk '{print $NF}' | sed 's/@.*//') |
| 90 | defs=$($NM --defined-only "$f" 2>/dev/null | awk '{print $NF}') |
| 91 | for s in $forbidden; do |
| 92 | if printf '%s\n' "$undef" | grep -qx "$s"; then |
| 93 | echo "FAIL: $f references forbidden symbol $s"; fail=1 |
| 94 | fi |
| 95 | if printf '%s\n' "$defs" | grep -qx "$s"; then |
| 96 | echo "FAIL: $f defines forbidden symbol $s"; fail=1 |
| 97 | fi |
| 98 | done |
| 99 | bad=$(printf '%s\n' "$undef" | grep -E '^(rx_|aienos_cap_|rc_)' || true) |
| 100 | if [ -n "$bad" ]; then |
| 101 | for s in $bad; do echo "FAIL: $f references runtime/authority symbol $s"; done |
| 102 | fail=1 |
| 103 | fi |
| 104 | } |
| 105 | |
| 106 | for f in "$BUILD"/visor/*.o; do |
| 107 | check_file "$f" |
| 108 | done |
| 109 | nobj=$checked |
| 110 | |
| 111 | # The adapter exports exactly its four public functions: no setter, no submit. |
| 112 | ER="$BUILD/visor/visor_effect_request.o" |
| 113 | if [ -e "$ER" ]; then |
| 114 | exports=$($NM --defined-only -g "$ER" | awk '$2 ~ /^[TDBR]$/ {print $3}' | sort | tr '\n' ' ') |
| 115 | want="visor_effect_request_build visor_effect_request_classify visor_effect_request_format_json visor_effect_request_format_text " |
| 116 | if [ "$exports" != "$want" ]; then |
| 117 | echo "FAIL: $ER exports [$exports], expected [$want]"; fail=1 |
| 118 | fi |
| 119 | else |
| 120 | echo "FAIL: $ER missing (lane 7 adapter not built)"; fail=1 |
| 121 | fi |
| 122 | |
| 123 | # No Visor C source (the test-only adapters left in src/visor) assigns `authorized` |
| 124 | # anything but false. |
| 125 | [ -d "$ROOT/src/visor" ] || { echo "FAIL: $ROOT/src/visor missing"; fail=1; } |
| 126 | hits=$(grep -rnE --include='*.c' --include='*.h' '(\.|->)authorized[[:space:]]*=[^=]' "$ROOT/src/visor" \ |
| 127 | | grep -vE 'authorized[[:space:]]*=[[:space:]]*false[[:space:]]*;' || true) |
| 128 | if [ -n "$hits" ]; then |
| 129 | printf '%s\n' "$hits" |
| 130 | echo "FAIL: a Visor C source assigns 'authorized' other than false"; fail=1 |
| 131 | fi |
| 132 | |
| 133 | # ---- OM-2 sources of the omega binary ------------------------------------------ |
| 134 | TMPD=$(mktemp -d "${TMPDIR:-/tmp}/visor-auth.XXXXXX") || { echo "FAIL: mktemp"; exit 1; } |
| 135 | trap 'rm -rf "$TMPD"' EXIT INT TERM |
| 136 | printf '%s\n' $forbidden > "$TMPD/forbidden" |
| 137 | # Host words, from the compiler's keyword table (the derivation seed/effects/check.sh uses). |
| 138 | GENKW="$ROOT/seed/omc/genkw.awk" |
| 139 | [ -f "$GENKW" ] || { echo "FAIL: missing $GENKW (the compiler keyword table)"; exit 1; } |
| 140 | { grep 'split("[a-z]' "$GENKW" | grep -oE '[a-z0-9]+:[0-9]+' \ |
| 141 | | awk -F: '$2 >= 100 && $1 != "const" && $1 != "qty" && $1 != "cpufence" && $1 != "cpurelax" && $1 != "fma" { print $1 }' |
| 142 | printf '%s\n' read write; } | sort -u > "$TMPD/host" |
| 143 | nhost=$(grep -c . "$TMPD/host") |
| 144 | [ "$nhost" -ge 100 ] || { echo "FAIL: only $nhost host words derived from $GENKW (expected >= 100)"; exit 1; } |
| 145 | for w in fopen fork socknew mapnew execprog read write; do |
| 146 | grep -qx "$w" "$TMPD/host" || { echo "FAIL: host word $w not derived from $GENKW"; exit 1; } |
| 147 | done |
| 148 | ALLOW="read write fopen fclose stat lstat getdnts argcnt argread" |
| 149 | printf '%s\n' $ALLOW > "$TMPD/allow" |
| 150 | : > "$TMPD/used" |
| 151 | |
| 152 | # om_scan file...: prints one line per hit, exits 1 on any hit. Note lines are |
| 153 | # skipped; string literals are checked for an `authorized` key with a value |
| 154 | # other than false, then blanked before the identifiers are checked. |
| 155 | om_scan() { |
| 156 | awk -v flist="$TMPD/forbidden" -v hlist="$TMPD/host" -v alist="$TMPD/allow" -v used="${OM_SCAN_USED:-/dev/null}" ' |
| 157 | BEGIN { while ((getline l < flist) > 0) F[l] = 1; bad = 0 |
| 158 | while ((getline l < hlist) > 0) H[l] = 1 |
| 159 | while ((getline l < alist) > 0) A[l] = 1 } |
| 160 | function hit(msg) { print "FAIL: " FILENAME ":" FNR ": " msg ": " $0; bad = 1 } |
| 161 | /^[[:space:]]*note([[:space:]]|$)/ { next } |
| 162 | { |
| 163 | s = $0 |
| 164 | while (match(s, /"([^"\\]|\\.)*"/)) { |
| 165 | t = substr(s, RSTART, RLENGTH) |
| 166 | s = substr(s, RSTART + RLENGTH) |
| 167 | while (match(t, /(^|[^A-Za-z0-9_])authorized(\\")?[[:space:]]*[:=][[:space:]]*/)) { |
| 168 | t = substr(t, RSTART + RLENGTH) |
| 169 | if (t !~ /^false/) hit("string literal gives authorized a value other than false") |
| 170 | } |
| 171 | } |
| 172 | code = $0 |
| 173 | gsub(/"([^"\\]|\\.)*"/, "\"\"", code) |
| 174 | n = split(code, w, /[^A-Za-z0-9_]+/) |
| 175 | for (i = 1; i <= n; i++) { |
| 176 | if (w[i] == "") continue |
| 177 | if (w[i] in F) hit("names forbidden authority symbol " w[i]) |
| 178 | else if (w[i] ~ /^(rx_|aienos_cap_|rc_)/) hit("names runtime/authority symbol " w[i]) |
| 179 | if (w[i] ~ /authorized/) hit("identifier " w[i] " holds authorization state") |
| 180 | if (w[i] in H) { |
| 181 | if (!(w[i] in A)) hit("uses OM-2 host word " w[i] ", which is not on the allowlist of host words omega uses") |
| 182 | else print w[i] > used |
| 183 | } |
| 184 | if (w[i] == "fopen") { |
| 185 | # fopen path off len flags mode: only literal read-only flags (0, O_DIRECTORY 16384) |
| 186 | if (!(i + 4 <= n && (w[i + 4] == "0" || w[i + 4] == "16384"))) |
| 187 | hit("fopen whose flags are not literally read-only (0 or 16384)") |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | END { exit bad }' "$@" |
| 192 | } |
| 193 | |
| 194 | om_files="" |
| 195 | for f in "$ROOT"/tools/omega*.om "$ROOT"/om/visor/*.om "$ROOT"/src/visor/*.om \ |
| 196 | "$ROOT"/src/language/*.om "$ROOT"/om/aarch64/aarch64_om2.om; do |
| 197 | [ -e "$f" ] && om_files="$om_files $f" |
| 198 | done |
| 199 | # Every file the mk rule says omega is built from must be scanned too. |
| 200 | for f in ${OMEGA_OM_SRCS:-}; do |
| 201 | case $f in /*) ;; *) f="$ROOT/$f" ;; esac |
| 202 | [ -e "$f" ] || { echo "FAIL: OMEGA_OM_SRCS names $f, which does not exist"; fail=1; continue; } |
| 203 | case " $om_files " in *" $f "*) ;; *) om_files="$om_files $f" ;; esac |
| 204 | done |
| 205 | nom=$(printf '%s\n' $om_files | grep -c . || true) |
| 206 | if [ "$nom" -eq 0 ] || [ ! -e "$ROOT/tools/omega.om" ] || [ ! -e "$ROOT/tools/omega_ops.om" ]; then |
| 207 | echo "FAIL: the OM-2 sources of the omega binary were not found under $ROOT"; fail=1 |
| 208 | fi |
| 209 | # shellcheck disable=SC2086 |
| 210 | OM_SCAN_USED="$TMPD/used" om_scan $om_files || { echo "FAIL: an OM-2 source of the omega binary names an authority operation"; fail=1; } |
| 211 | for w in $ALLOW; do |
| 212 | grep -qx "$w" "$TMPD/used" || { echo "FAIL: allowlisted host word $w is used by no scanned source; remove it from the allowlist"; fail=1; } |
| 213 | done |
| 214 | |
| 215 | # Bite control: plant each forbidden form in a copy of a shipped source; the scan |
| 216 | # must fail on every one, and must stay clean on the unplanted copy and on a copy |
| 217 | # that carries the names only in a note line and a string literal. |
| 218 | BASE="$ROOT/src/visor/visor_effect_request.om" |
| 219 | bites=0 |
| 220 | nbite=0 |
| 221 | bite() { # bite <label> <planted line> |
| 222 | nbite=$((nbite + 1)) |
| 223 | cp "$BASE" "$TMPD/plant.om" && printf '%s\n' "$2" >> "$TMPD/plant.om" |
| 224 | if om_scan "$TMPD/plant.om" > "$TMPD/plant.out"; then |
| 225 | echo "FAIL: bite control '$1' was not caught by the OM source scan"; fail=1 |
| 226 | else |
| 227 | bites=$((bites + 1)) |
| 228 | echo "bite control '$1': caught ($(head -n 1 "$TMPD/plant.out" | sed -e 's/^FAIL: [^ ]* //' -e 's/: .*//'))" |
| 229 | fi |
| 230 | } |
| 231 | cp "$BASE" "$TMPD/plant.om" |
| 232 | om_scan "$TMPD/plant.om" > /dev/null || { echo "FAIL: bite control baseline (unplanted copy) is not clean"; fail=1; } |
| 233 | bite "local definition of a forbidden name" 'fn rx_capadmin_mint slot ulong to void' |
| 234 | bite "call to an Aienux mint" ' aienos_cap_mint 0 0' |
| 235 | bite "call to the publish gate" ' rc_produce 0' |
| 236 | bite "call to an unlisted runtime symbol" ' x = rx_world_poke 1' |
| 237 | bite "authorization state" 'global verq_authorized ulong 1' |
| 238 | bite "JSON literal authorized true" 'text verq_bad "\",\"authorized\":true}"' |
| 239 | bite "text literal authorized yes" 'text verq_bad2 " authorized: yes\n"' |
| 240 | bite "process start" ' pid = fork' |
| 241 | bite "program exec" ' r = execprog verq_bad 0 0' |
| 242 | bite "socket" ' s = socknew 2 1 0' |
| 243 | bite "device control" ' devctl 3 0 0' |
| 244 | bite "file system change" ' unlink verq_bad 0 0' |
| 245 | bite "descriptor duplication (not on the allowlist)" ' fd = dupfd 1 9 0' |
| 246 | bite "pipe (not on the allowlist)" ' r = pipefds verq_bad' |
| 247 | bite "event descriptor (not on the allowlist)" ' fd = eventfd 0 0' |
| 248 | bite "working directory change (not on the allowlist)" ' r = chdir verq_bad 0 0' |
| 249 | bite "umask (not on the allowlist)" ' r = umask 0' |
| 250 | bite "map read (not on the allowlist)" ' r = mapread 0 verq_bad 0 0' |
| 251 | bite "kfd control (in the compiler table, not in seed/ng/hostwords.tsv)" ' r = kfdctl 0 0 0' |
| 252 | bite "environment read (not on the allowlist)" ' r = envread 0 verq_bad 0 0' |
| 253 | bite "fopen for writing" ' fd = fopen verq_bad 0 8 577 420' |
| 254 | bite "fopen with computed flags" ' fd = fopen verq_bad 0 8 get verq_fl 0 0' |
| 255 | cp "$BASE" "$TMPD/plant.om" |
| 256 | printf '%s\n' 'note rx_capadmin_mint authorized aienos_cap_mint fork socknew' \ |
| 257 | 'text verq_ok "rx_capadmin_mint \",\"authorized\":false fork execprog dupfd umask"' \ |
| 258 | ' fd = fopen verq_ok 0 8 0 0' ' fd = fopen verq_ok 0 8 16384 0' \ |
| 259 | ' k = read 0 verq_ok 0 1' ' k = write 1 verq_ok 0 1' ' r = stat verq_ok 0 8 verq_ok 0 1' >> "$TMPD/plant.om" |
| 260 | om_scan "$TMPD/plant.om" > /dev/null || { echo "FAIL: bite control false positive (a note line or a literal tripped the scan)"; fail=1; } |
| 261 | |
| 262 | if [ "$fail" -ne 0 ]; then |
| 263 | echo "OMEGA_VISOR_AUTHORITY_ISOLATION_FAIL" |
| 264 | exit 1 |
| 265 | fi |
| 266 | echo "checked $nobj object(s) and $nom OM-2 source(s) against $(printf '%s\n' $forbidden | wc -l) forbidden symbols and $nhost host words (allowlist: $ALLOW); $bites/$nbite bite controls caught" |
| 267 | echo "OMEGA_VISOR_AUTHORITY_ISOLATION_PASS" |
| 268 | exit 0 |