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 / src / visor / visor_evidence.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 Visor lane 4: evidence (receipt) inspection |
| 3 | note (replaces src/visor/visor_evidence.c). Depends on visor.om. |
| 4 | |
| 5 | note Scope constants: 0=UNKNOWN, 1=SIMULATED, 2=HOST, 3=QEMU, 4=SILICON |
| 6 | const VEV_SCOPE_UNKNOWN ulong 0 |
| 7 | const VEV_SCOPE_SIMULATED ulong 1 |
| 8 | const VEV_SCOPE_HOST ulong 2 |
| 9 | const VEV_SCOPE_QEMU ulong 3 |
| 10 | const VEV_SCOPE_SILICON ulong 4 |
| 11 | |
| 12 | text vev_s_unk "UNKNOWN" |
| 13 | text vev_s_sim "SIMULATED" |
| 14 | text vev_s_host "HOST" |
| 15 | text vev_s_qemu "QEMU" |
| 16 | text vev_s_sil "SILICON" |
| 17 | text vev_k_sil_true "\"silicon_observed\": true" |
| 18 | text vev_k_sil_true2 "\"silicon_observed\":true" |
| 19 | text vev_k_sil_false "\"silicon_observed\": false" |
| 20 | text vev_k_sil_false2 "\"silicon_observed\":false" |
| 21 | text vev_k_qemu "\"qemu_target\"" |
| 22 | text vev_k_hscope "\"hardware_scope\"" |
| 23 | text vev_k_host_lo "host" |
| 24 | text vev_k_sim_lo "simulat" |
| 25 | text vev_k_qemu_lo "qemu" |
| 26 | text vev_k_gpu_uuid "\"gpu_uuid\"" |
| 27 | text vev_k_comp_cls "\"compute_class\"" |
| 28 | text vev_k_dirty_t "\"tree_dirty\": true" |
| 29 | text vev_k_dirty_t2 "\"tree_dirty\":true" |
| 30 | text vev_k_dirty_f "\"tree_dirty\": false" |
| 31 | text vev_k_dirty_f2 "\"tree_dirty\":false" |
| 32 | text vev_t_noev "no evidence\n" |
| 33 | text vev_t_hdr "evidence: scanned=" |
| 34 | text vev_t_mat " matched=" |
| 35 | text vev_t_unp " UNPARSED=" |
| 36 | text vev_t_scope "\n scope " |
| 37 | text vev_t_rid "\n receipt_id sha256:" |
| 38 | text vev_t_path " path " |
| 39 | text vev_t_j1 "{\"scanned\":" |
| 40 | text vev_t_j2 ",\"matched\":" |
| 41 | text vev_t_j3 ",\"unparsed\":" |
| 42 | text vev_t_j4 ",\"items\":[" |
| 43 | text vev_t_ji1 "{\"path\":" |
| 44 | text vev_t_ji2 ",\"scope\":\"" |
| 45 | text vev_t_ji3 "\"}" |
| 46 | text vev_t_jend "]}" |
| 47 | |
| 48 | global vev_fbuf uchar 262144 |
| 49 | global vev_flen ulong 1 |
| 50 | global vev_dbuf uchar 32768 |
| 51 | global vev_pbuf uchar 512 |
| 52 | |
| 53 | note Loaded single record |
| 54 | global vev_rec_scope ulong 1 |
| 55 | global vev_rec_dirty ulong 1 |
| 56 | global vev_rec_dknow ulong 1 |
| 57 | global vev_rec_id uchar 32 |
| 58 | global vev_rec_nmatch long 1 |
| 59 | |
| 60 | note Scan view (up to 16 kept items + 8 unparsed paths) |
| 61 | global vev_cnt ulong 1 |
| 62 | global vev_scanned ulong 1 |
| 63 | global vev_matched ulong 1 |
| 64 | global vev_unparsed ulong 1 |
| 65 | global vev_trunc ulong 1 |
| 66 | global vev_it_path uchar 4096 |
| 67 | global vev_it_plen ulong 16 |
| 68 | global vev_it_scope ulong 16 |
| 69 | global vev_it_id uchar 512 |
| 70 | global vev_up_path uchar 2048 |
| 71 | global vev_up_plen ulong 8 |
| 72 | global vev_up_cnt ulong 1 |
| 73 | global vev_filter_hex uchar 64 |
| 74 | global vev_filter_on ulong 1 |
| 75 | |
| 76 | note Directory recursion stack (up to 8 levels, 256 bytes per dir path) |
| 77 | global vev_dstack uchar 2048 |
| 78 | global vev_dslen ulong 8 |
| 79 | global vev_dtop ulong 1 |
| 80 | |
| 81 | fn vev_buf_has n ulong needle view uchar * to bool |
| 82 | nl = len needle |
| 83 | if or eq nl 0 gt nl n |
| 84 | ret false |
| 85 | end |
| 86 | lim = add sub n nl 1 |
| 87 | for i ulong lim bound 262144 |
| 88 | ok = true |
| 89 | for j ulong nl bound 64 |
| 90 | if ne get vev_fbuf add i j get needle j |
| 91 | ok = false |
| 92 | break |
| 93 | end |
| 94 | end |
| 95 | if ok |
| 96 | ret true |
| 97 | end |
| 98 | end |
| 99 | ret false |
| 100 | end |
| 101 | |
| 102 | fn vev_buf_has_hex64 n ulong to bool |
| 103 | if lt n 64 |
| 104 | ret false |
| 105 | end |
| 106 | lim = add sub n 64 1 |
| 107 | for i ulong lim bound 262144 |
| 108 | ok = true |
| 109 | for j ulong 64 |
| 110 | if ne get vev_fbuf add i j get vev_filter_hex j |
| 111 | ok = false |
| 112 | break |
| 113 | end |
| 114 | end |
| 115 | if ok |
| 116 | ret true |
| 117 | end |
| 118 | end |
| 119 | ret false |
| 120 | end |
| 121 | |
| 122 | fn vev_json_basic_valid n ulong to bool |
| 123 | if lt n 2 |
| 124 | ret false |
| 125 | end |
| 126 | p = ulong 0 |
| 127 | while lt p n bound 262144 |
| 128 | c = ulong get vev_fbuf p |
| 129 | if or eq c 32 or eq c 9 or eq c 10 eq c 13 |
| 130 | p = add p 1 |
| 131 | else |
| 132 | break |
| 133 | end |
| 134 | end |
| 135 | if ge p n |
| 136 | ret false |
| 137 | end |
| 138 | if ne get vev_fbuf p uchar 123 |
| 139 | ret false |
| 140 | end |
| 141 | e = n |
| 142 | while gt e p bound 262144 |
| 143 | c = ulong get vev_fbuf sub e 1 |
| 144 | if or eq c 32 or eq c 9 or eq c 10 eq c 13 |
| 145 | e = sub e 1 |
| 146 | else |
| 147 | break |
| 148 | end |
| 149 | end |
| 150 | if le e p |
| 151 | ret false |
| 152 | end |
| 153 | if ne get vev_fbuf sub e 1 uchar 125 |
| 154 | ret false |
| 155 | end |
| 156 | note Balanced braces outside strings |
| 157 | depth = long 0 |
| 158 | instr = false |
| 159 | esc = false |
| 160 | for i ulong sub e p bound 262144 |
| 161 | c = ulong get vev_fbuf add p i |
| 162 | if instr |
| 163 | if esc |
| 164 | esc = false |
| 165 | else |
| 166 | if eq c 92 |
| 167 | esc = true |
| 168 | else |
| 169 | if eq c 34 |
| 170 | instr = false |
| 171 | end |
| 172 | end |
| 173 | end |
| 174 | else |
| 175 | if eq c 34 |
| 176 | instr = true |
| 177 | else |
| 178 | if eq c 123 |
| 179 | depth = add depth 1 |
| 180 | else |
| 181 | if eq c 125 |
| 182 | depth = sub depth 1 |
| 183 | if lt depth 0 |
| 184 | ret false |
| 185 | end |
| 186 | end |
| 187 | end |
| 188 | end |
| 189 | end |
| 190 | end |
| 191 | if or instr ne depth 0 |
| 192 | ret false |
| 193 | end |
| 194 | ret true |
| 195 | end |
| 196 | |
| 197 | fn vev_classify_scope n ulong to ulong |
| 198 | note Lowest scope wins; silicon_observed=false caps at HOST |
| 199 | sil_false = or vev_buf_has n vev_k_sil_false vev_buf_has n vev_k_sil_false2 |
| 200 | sil_true = or vev_buf_has n vev_k_sil_true vev_buf_has n vev_k_sil_true2 |
| 201 | has_qemu_key = vev_buf_has n vev_k_qemu |
| 202 | has_hscope = vev_buf_has n vev_k_hscope |
| 203 | has_gpu_uuid = or vev_buf_has n vev_k_gpu_uuid vev_buf_has n vev_k_comp_cls |
| 204 | if sil_false |
| 205 | ret VEV_SCOPE_HOST |
| 206 | end |
| 207 | if has_qemu_key |
| 208 | ret VEV_SCOPE_QEMU |
| 209 | end |
| 210 | if has_hscope |
| 211 | if vev_buf_has n vev_k_sim_lo |
| 212 | ret VEV_SCOPE_SIMULATED |
| 213 | end |
| 214 | if vev_buf_has n vev_k_host_lo |
| 215 | ret VEV_SCOPE_HOST |
| 216 | end |
| 217 | if vev_buf_has n vev_k_qemu_lo |
| 218 | ret VEV_SCOPE_QEMU |
| 219 | end |
| 220 | end |
| 221 | if or sil_true has_gpu_uuid |
| 222 | ret VEV_SCOPE_SILICON |
| 223 | end |
| 224 | ret VEV_SCOPE_UNKNOWN |
| 225 | end |
| 226 | |
| 227 | fn vev_load_pbuf plen ulong to long |
| 228 | if or eq plen 0 gt plen 511 |
| 229 | ret -1 |
| 230 | end |
| 231 | fd = fopen vev_pbuf 0 plen 0 0 |
| 232 | if lt fd 0 |
| 233 | ret -1 |
| 234 | end |
| 235 | tot = ulong 0 |
| 236 | while lt tot 262144 bound 4096 |
| 237 | k = read int fd vev_fbuf tot sub 262144 tot |
| 238 | if lt k 0 |
| 239 | u = fclose int fd |
| 240 | ret -1 |
| 241 | end |
| 242 | if eq k 0 |
| 243 | break |
| 244 | end |
| 245 | tot = add tot ulong k |
| 246 | end |
| 247 | u = fclose int fd |
| 248 | if or eq tot 0 ge tot 262144 |
| 249 | ret -1 |
| 250 | end |
| 251 | put vev_flen 0 tot |
| 252 | if not vev_json_basic_valid tot |
| 253 | ret -1 |
| 254 | end |
| 255 | for i ulong tot bound 262144 |
| 256 | put v_sha_in i get vev_fbuf i |
| 257 | end |
| 258 | sha256_hash tot |
| 259 | for i ulong 32 |
| 260 | put vev_rec_id i get v_sha_out i |
| 261 | end |
| 262 | put vev_rec_scope 0 vev_classify_scope tot |
| 263 | if or vev_buf_has tot vev_k_dirty_t vev_buf_has tot vev_k_dirty_t2 |
| 264 | put vev_rec_dknow 0 1 |
| 265 | put vev_rec_dirty 0 1 |
| 266 | else |
| 267 | if or vev_buf_has tot vev_k_dirty_f vev_buf_has tot vev_k_dirty_f2 |
| 268 | put vev_rec_dknow 0 1 |
| 269 | put vev_rec_dirty 0 0 |
| 270 | else |
| 271 | put vev_rec_dknow 0 0 |
| 272 | put vev_rec_dirty 0 0 |
| 273 | end |
| 274 | end |
| 275 | put vev_rec_nmatch 0 -1 |
| 276 | ret 0 |
| 277 | end |
| 278 | |
| 279 | fn vev_record_unparsed plen ulong to void |
| 280 | put vev_unparsed 0 add get vev_unparsed 0 1 |
| 281 | uc = get vev_up_cnt 0 |
| 282 | if lt uc 8 |
| 283 | put vev_up_cnt 0 add uc 1 |
| 284 | cl = plen |
| 285 | if gt cl 255 |
| 286 | cl = 255 |
| 287 | end |
| 288 | put vev_up_plen uc cl |
| 289 | base = mul uc 256 |
| 290 | for i ulong cl bound 256 |
| 291 | put vev_up_path add base i get vev_pbuf i |
| 292 | end |
| 293 | end |
| 294 | end |
| 295 | |
| 296 | fn vev_process_file plen ulong to void |
| 297 | put vev_scanned 0 add get vev_scanned 0 1 |
| 298 | rc = vev_load_pbuf plen |
| 299 | if lt rc 0 |
| 300 | if ne get vev_filter_on 0 0 |
| 301 | if not vev_buf_has_hex64 get vev_flen 0 |
| 302 | ret |
| 303 | end |
| 304 | end |
| 305 | vev_record_unparsed plen |
| 306 | ret |
| 307 | end |
| 308 | if ne get vev_filter_on 0 0 |
| 309 | if not vev_buf_has_hex64 get vev_flen 0 |
| 310 | ret |
| 311 | end |
| 312 | end |
| 313 | put vev_matched 0 add get vev_matched 0 1 |
| 314 | cnt = get vev_cnt 0 |
| 315 | if ge cnt 16 |
| 316 | put vev_trunc 0 1 |
| 317 | ret |
| 318 | end |
| 319 | put vev_cnt 0 add cnt 1 |
| 320 | cl = plen |
| 321 | if gt cl 255 |
| 322 | cl = 255 |
| 323 | end |
| 324 | put vev_it_plen cnt cl |
| 325 | put vev_it_scope cnt get vev_rec_scope 0 |
| 326 | pb = mul cnt 256 |
| 327 | for i ulong cl bound 256 |
| 328 | put vev_it_path add pb i get vev_pbuf i |
| 329 | end |
| 330 | ib = mul cnt 32 |
| 331 | for i ulong 32 |
| 332 | put vev_it_id add ib i get vev_rec_id i |
| 333 | end |
| 334 | end |
| 335 | |
| 336 | fn vev_push_pbuf n ulong to void |
| 337 | top = get vev_dtop 0 |
| 338 | if or ge top 8 gt n 255 |
| 339 | ret |
| 340 | end |
| 341 | put vev_dtop 0 add top 1 |
| 342 | put vev_dslen top n |
| 343 | base = mul top 256 |
| 344 | for i ulong n bound 256 |
| 345 | put vev_dstack add base i get vev_pbuf i |
| 346 | end |
| 347 | end |
| 348 | |
| 349 | fn vev_handle_dirent dbase ulong dlen ulong pos ulong rlen ulong to void |
| 350 | dtype = ulong get vev_dbuf add pos 18 |
| 351 | noff = add pos 19 |
| 352 | nlen = ulong 0 |
| 353 | lim = add pos rlen |
| 354 | while lt add noff nlen lim bound 256 |
| 355 | if eq get vev_dbuf add noff nlen uchar 0 |
| 356 | break |
| 357 | end |
| 358 | nlen = add nlen 1 |
| 359 | end |
| 360 | if eq nlen 0 |
| 361 | ret |
| 362 | end |
| 363 | if eq nlen 1 |
| 364 | if eq get vev_dbuf noff uchar 46 |
| 365 | ret |
| 366 | end |
| 367 | end |
| 368 | if eq nlen 2 |
| 369 | if eq get vev_dbuf noff uchar 46 |
| 370 | if eq get vev_dbuf add noff 1 uchar 46 |
| 371 | ret |
| 372 | end |
| 373 | end |
| 374 | end |
| 375 | for i ulong dlen bound 256 |
| 376 | put vev_pbuf i get vev_dstack add dbase i |
| 377 | end |
| 378 | put vev_pbuf dlen uchar 47 |
| 379 | for i ulong nlen bound 255 |
| 380 | dst = add add dlen 1 i |
| 381 | if lt dst 511 |
| 382 | put vev_pbuf dst get vev_dbuf add noff i |
| 383 | end |
| 384 | end |
| 385 | full = add add dlen 1 nlen |
| 386 | if eq dtype 4 |
| 387 | vev_push_pbuf full |
| 388 | else |
| 389 | vev_process_file full |
| 390 | end |
| 391 | end |
| 392 | |
| 393 | fn vev_dirent_rlen pos ulong to ulong |
| 394 | lo = ulong get vev_dbuf add pos 16 |
| 395 | hi = ulong get vev_dbuf add pos 17 |
| 396 | ret or lo shl hi 8 |
| 397 | end |
| 398 | |
| 399 | fn vev_scan_dir_pop to long |
| 400 | top = get vev_dtop 0 |
| 401 | if eq top 0 |
| 402 | ret 0 |
| 403 | end |
| 404 | idx = sub top 1 |
| 405 | put vev_dtop 0 idx |
| 406 | dlen = get vev_dslen idx |
| 407 | dbase = mul idx 256 |
| 408 | for i ulong dlen bound 256 |
| 409 | put vev_pbuf i get vev_dstack add dbase i |
| 410 | end |
| 411 | fd = fopen vev_pbuf 0 dlen 16384 0 |
| 412 | if lt fd 0 |
| 413 | ret -1 |
| 414 | end |
| 415 | while true bound 256 |
| 416 | nread = getdnts int fd vev_dbuf 0 32768 |
| 417 | if le nread 0 |
| 418 | break |
| 419 | end |
| 420 | pos = ulong 0 |
| 421 | nr = ulong nread |
| 422 | while lt pos nr bound 4096 |
| 423 | rlen = vev_dirent_rlen pos |
| 424 | if eq rlen 0 |
| 425 | break |
| 426 | end |
| 427 | vev_handle_dirent dbase dlen pos rlen |
| 428 | pos = add pos rlen |
| 429 | end |
| 430 | end |
| 431 | u = fclose int fd |
| 432 | ret 1 |
| 433 | end |
| 434 | |
| 435 | fn vev_scan_sbuf rlen ulong to long |
| 436 | put vev_cnt 0 0 |
| 437 | put vev_scanned 0 0 |
| 438 | put vev_matched 0 0 |
| 439 | put vev_unparsed 0 0 |
| 440 | put vev_trunc 0 0 |
| 441 | put vev_up_cnt 0 0 |
| 442 | put vev_dtop 0 0 |
| 443 | if or eq rlen 0 gt rlen 255 |
| 444 | ret -1 |
| 445 | end |
| 446 | for i ulong rlen bound 256 |
| 447 | put vev_pbuf i get v_sbuf i |
| 448 | end |
| 449 | vev_push_pbuf rlen |
| 450 | first = vev_scan_dir_pop |
| 451 | if lt first 0 |
| 452 | ret -1 |
| 453 | end |
| 454 | while gt get vev_dtop 0 0 bound 256 |
| 455 | u = vev_scan_dir_pop |
| 456 | end |
| 457 | ret 0 |
| 458 | end |
| 459 | |
| 460 | fn vev_put_scope s ulong to void |
| 461 | if eq s VEV_SCOPE_SILICON |
| 462 | ob_puts vev_s_sil |
| 463 | ret |
| 464 | end |
| 465 | if eq s VEV_SCOPE_QEMU |
| 466 | ob_puts vev_s_qemu |
| 467 | ret |
| 468 | end |
| 469 | if eq s VEV_SCOPE_HOST |
| 470 | ob_puts vev_s_host |
| 471 | ret |
| 472 | end |
| 473 | if eq s VEV_SCOPE_SIMULATED |
| 474 | ob_puts vev_s_sim |
| 475 | ret |
| 476 | end |
| 477 | ob_puts vev_s_unk |
| 478 | end |
| 479 | |
| 480 | fn vev_format_text cap ulong to long |
| 481 | ob_reset cap |
| 482 | cnt = get vev_cnt 0 |
| 483 | if and eq cnt 0 eq get vev_unparsed 0 0 |
| 484 | ob_puts vev_t_noev |
| 485 | else |
| 486 | ob_puts vev_t_hdr |
| 487 | ob_put_u64 get vev_scanned 0 |
| 488 | ob_puts vev_t_mat |
| 489 | ob_put_u64 get vev_matched 0 |
| 490 | ob_puts vev_t_unp |
| 491 | ob_put_u64 get vev_unparsed 0 |
| 492 | ob_putc uchar 10 |
| 493 | for i ulong cnt bound 16 |
| 494 | ob_puts vev_t_path |
| 495 | pl = get vev_it_plen i |
| 496 | pb = mul i 256 |
| 497 | for k ulong pl bound 256 |
| 498 | ob_putc get vev_it_path add pb k |
| 499 | end |
| 500 | ob_puts vev_t_scope |
| 501 | vev_put_scope get vev_it_scope i |
| 502 | ob_puts vev_t_rid |
| 503 | ib = mul i 32 |
| 504 | for b ulong 32 |
| 505 | ob_put_hex_byte ulong get vev_it_id add ib b |
| 506 | end |
| 507 | ob_putc uchar 10 |
| 508 | end |
| 509 | end |
| 510 | if ne get v_ob_ovf 0 0 |
| 511 | ret -1 |
| 512 | end |
| 513 | ret long get v_ob_len 0 |
| 514 | end |
| 515 | |
| 516 | fn vev_format_json cap ulong to long |
| 517 | ob_reset cap |
| 518 | ob_puts vev_t_j1 |
| 519 | ob_put_u64 get vev_scanned 0 |
| 520 | ob_puts vev_t_j2 |
| 521 | ob_put_u64 get vev_matched 0 |
| 522 | ob_puts vev_t_j3 |
| 523 | ob_put_u64 get vev_unparsed 0 |
| 524 | ob_puts vev_t_j4 |
| 525 | cnt = get vev_cnt 0 |
| 526 | for i ulong cnt bound 16 |
| 527 | if gt i 0 |
| 528 | ob_putc uchar 44 |
| 529 | end |
| 530 | ob_puts vev_t_ji1 |
| 531 | pl = get vev_it_plen i |
| 532 | pb = mul i 256 |
| 533 | for k ulong pl bound 256 |
| 534 | put v_sbuf k get vev_it_path add pb k |
| 535 | end |
| 536 | ob_put_json_sbuf pl |
| 537 | ob_puts vev_t_ji2 |
| 538 | vev_put_scope get vev_it_scope i |
| 539 | ob_puts vev_t_ji3 |
| 540 | end |
| 541 | ob_puts vev_t_jend |
| 542 | if ne get v_ob_ovf 0 0 |
| 543 | ret -1 |
| 544 | end |
| 545 | ret long get v_ob_len 0 |
| 546 | end |