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 / runtime / rx_r7_native.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * rx_r7_native.c -- same attacks against the Linux mint and the native |
| 19 | * Aienux authority. The world is then pointed at the native view only. |
| 20 | * The Linux oracle is not removed. |
| 21 | */ |
| 22 | #include "omega_evidence.h" |
| 23 | #include "runtime/aienos_cap.h" |
| 24 | #include "runtime/rx_world.h" |
| 25 | #include "sha256.h" |
| 26 | |
| 27 | #include <signal.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/utsname.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | static int g_fail; |
| 36 | |
| 37 | typedef struct { |
| 38 | char name[48]; /* copied: some names are built on the caller's stack */ |
| 39 | int code; |
| 40 | } DiffResult; |
| 41 | |
| 42 | static DiffResult g_diff[96]; |
| 43 | static int g_ndiff; |
| 44 | |
| 45 | static void expect_eq(const char *name, int linux_rc, int native_rc) { |
| 46 | if (linux_rc != native_rc) { |
| 47 | fprintf(stderr, "FAIL %s: linux %s (%d), native %s (%d)\n", name, |
| 48 | rx_cap_strerror(linux_rc), linux_rc, rx_cap_strerror(native_rc), native_rc); |
| 49 | g_fail++; |
| 50 | return; |
| 51 | } |
| 52 | if (g_ndiff < (int)(sizeof g_diff / sizeof g_diff[0])) { |
| 53 | snprintf(g_diff[g_ndiff].name, sizeof g_diff[g_ndiff].name, "%s", name); |
| 54 | g_diff[g_ndiff].code = linux_rc; |
| 55 | g_ndiff++; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | typedef struct { |
| 60 | RxCapRoot root; |
| 61 | RxCapAdmin admin; |
| 62 | AienosCapAdmin *native_admin; |
| 63 | AienosCapView *native_view; |
| 64 | /* Each authority starts its table at its own boot generation (time |
| 65 | * seeded), so the two agree on generations relative to their office. */ |
| 66 | uint64_t linux_base; |
| 67 | uint64_t native_base; |
| 68 | } Pair; |
| 69 | |
| 70 | static int refs_match(const Pair *p, RxCapRef linux_ref, AienosCapRef native_ref) { |
| 71 | return linux_ref.cap_id == native_ref.cap_id && |
| 72 | linux_ref.generation - p->linux_base == native_ref.generation - p->native_base; |
| 73 | } |
| 74 | |
| 75 | /* The native reference for a Linux one: same slot, same offset from boot. |
| 76 | * Forged generations map one to one, so they stay forged. */ |
| 77 | static AienosCapRef to_native(const Pair *p, RxCapRef ref) { |
| 78 | return (AienosCapRef){ ref.cap_id, ref.generation - p->linux_base + p->native_base }; |
| 79 | } |
| 80 | |
| 81 | static int pair_start(Pair *p) { |
| 82 | memset(p, 0, sizeof(*p)); |
| 83 | int linux_rc = rx_caproot_start(&p->root, &p->admin); |
| 84 | int native_rc = aienos_cap_start(&p->native_admin, &p->native_view); |
| 85 | expect_eq("start", linux_rc, native_rc); |
| 86 | if (linux_rc == RX_CAP_OK && native_rc == RX_CAP_OK) { |
| 87 | AienosCapRef office; |
| 88 | aienos_cap_office(p->native_admin, &office); |
| 89 | p->linux_base = p->admin.office.generation; |
| 90 | p->native_base = office.generation; |
| 91 | } |
| 92 | return linux_rc == RX_CAP_OK && native_rc == RX_CAP_OK ? 0 : -1; |
| 93 | } |
| 94 | |
| 95 | static void pair_stop(Pair *p) { |
| 96 | rx_caproot_stop(&p->root, &p->admin); |
| 97 | aienos_cap_stop(p->native_admin, p->native_view); |
| 98 | p->native_admin = NULL; |
| 99 | p->native_view = NULL; |
| 100 | } |
| 101 | |
| 102 | static void fill_mint(const Pair *p, RxCapMint *linux_mint, AienosCapMint *native_mint, |
| 103 | uint32_t subject, |
| 104 | uint64_t resource, uint32_t rights, uint64_t lease, RxCapRef parent) { |
| 105 | memset(linux_mint, 0, sizeof(*linux_mint)); |
| 106 | memset(native_mint, 0, sizeof(*native_mint)); |
| 107 | linux_mint->issuer = 3; |
| 108 | native_mint->issuer = 3; |
| 109 | linux_mint->subject = subject; |
| 110 | native_mint->subject = subject; |
| 111 | linux_mint->resource = resource; |
| 112 | native_mint->resource = resource; |
| 113 | linux_mint->rights = rights; |
| 114 | native_mint->rights = rights; |
| 115 | linux_mint->lease_ticks = lease; |
| 116 | native_mint->lease_ticks = lease; |
| 117 | linux_mint->parent = parent; |
| 118 | native_mint->parent = parent.cap_id == UINT32_MAX ? (AienosCapRef){ UINT32_MAX, 0 } |
| 119 | : to_native(p, parent); |
| 120 | } |
| 121 | |
| 122 | static int mint_both(Pair *p, const char *name, uint32_t subject, uint64_t resource, |
| 123 | uint32_t rights, uint64_t lease, RxCapRef parent, int parent_is_authority, |
| 124 | RxCapRef *out) { |
| 125 | RxCapMint linux_mint; |
| 126 | AienosCapMint native_mint; |
| 127 | fill_mint(p, &linux_mint, &native_mint, subject, resource, rights, lease, parent); |
| 128 | if (parent_is_authority) { |
| 129 | linux_mint.authority = parent; |
| 130 | native_mint.authority = native_mint.parent; |
| 131 | } else { |
| 132 | linux_mint.authority = p->admin.office; |
| 133 | AienosCapRef office; |
| 134 | aienos_cap_office(p->native_admin, &office); |
| 135 | native_mint.authority = office; |
| 136 | } |
| 137 | RxCapRef linux_out = { UINT32_MAX, 0 }; |
| 138 | AienosCapRef native_out = { UINT32_MAX, 0 }; |
| 139 | int linux_rc = rx_capadmin_mint(&p->admin, &linux_mint, &linux_out); |
| 140 | int native_rc = aienos_cap_mint(p->native_admin, &native_mint, &native_out); |
| 141 | expect_eq(name, linux_rc, native_rc); |
| 142 | if (linux_rc == RX_CAP_OK || native_rc == RX_CAP_OK) { |
| 143 | if (!refs_match(p, linux_out, native_out)) { |
| 144 | fprintf(stderr, "FAIL %s: ref linux %u/%llu native %u/%llu\n", name, |
| 145 | linux_out.cap_id, (unsigned long long)linux_out.generation, |
| 146 | native_out.cap_id, (unsigned long long)native_out.generation); |
| 147 | g_fail++; |
| 148 | return -1; |
| 149 | } |
| 150 | } |
| 151 | if (out && linux_rc == RX_CAP_OK) *out = linux_out; |
| 152 | return linux_rc == native_rc ? 0 : -1; |
| 153 | } |
| 154 | |
| 155 | static void validate_both(Pair *p, const char *name, RxCapRef cap, uint32_t subject, |
| 156 | uint64_t resource, uint32_t rights) { |
| 157 | AienosCapRef native = to_native(p, cap); |
| 158 | int linux_rc = rx_caproot_validate(&p->root, cap, subject, resource, rights, NULL); |
| 159 | int native_rc = aienos_cap_validate(p->native_view, native, subject, resource, rights, NULL); |
| 160 | expect_eq(name, linux_rc, native_rc); |
| 161 | } |
| 162 | |
| 163 | static int corpus(void) { |
| 164 | Pair p; |
| 165 | if (pair_start(&p) != 0) return -1; |
| 166 | RxCapRef linux_office = p.admin.office; |
| 167 | AienosCapRef native_office; |
| 168 | aienos_cap_office(p.native_admin, &native_office); |
| 169 | if (!refs_match(&p, linux_office, native_office)) { |
| 170 | fprintf(stderr, "FAIL office identity diverged\n"); |
| 171 | g_fail++; |
| 172 | } |
| 173 | |
| 174 | RxCapRef none = { UINT32_MAX, 0 }; |
| 175 | RxCapRef read_cap; |
| 176 | if (mint_both(&p, "mint read", 1, 0x10, RX_RIGHT_READ, 0, none, 0, &read_cap) != 0) { |
| 177 | pair_stop(&p); |
| 178 | return -1; |
| 179 | } |
| 180 | validate_both(&p, "valid read", read_cap, 1, 0x10, RX_RIGHT_READ); |
| 181 | validate_both(&p, "forged handle", (RxCapRef){ 200, 7 }, 1, 0x10, RX_RIGHT_READ); |
| 182 | validate_both(&p, "out of range handle", (RxCapRef){ 300, 1 }, 1, 0x10, RX_RIGHT_READ); |
| 183 | validate_both(&p, "wrong subject", read_cap, 7, 0x10, RX_RIGHT_READ); |
| 184 | validate_both(&p, "wrong resource", read_cap, 1, 0x30, RX_RIGHT_READ); |
| 185 | validate_both(&p, "missing rights", read_cap, 1, 0x10, RX_RIGHT_WRITE); |
| 186 | |
| 187 | uint64_t wide = (1ull << 32) | 0x51u; |
| 188 | RxCapRef wide_cap; |
| 189 | mint_both(&p, "mint wide resource", 1, wide, RX_RIGHT_READ, 0, none, 0, &wide_cap); |
| 190 | validate_both(&p, "wide resource kept", wide_cap, 1, wide, RX_RIGHT_READ); |
| 191 | validate_both(&p, "truncated resource refused", wide_cap, 1, 0x51, RX_RIGHT_READ); |
| 192 | |
| 193 | RxCapRef revoked; |
| 194 | mint_both(&p, "mint to revoke", 1, 0x20, RX_RIGHT_WRITE, 0, none, 0, &revoked); |
| 195 | expect_eq("revoke", rx_capadmin_revoke(&p.admin, linux_office, revoked), |
| 196 | aienos_cap_revoke(p.native_admin, native_office, |
| 197 | to_native(&p, revoked))); |
| 198 | validate_both(&p, "revoked", revoked, 1, 0x20, RX_RIGHT_WRITE); |
| 199 | expect_eq("reclaim", rx_capadmin_reclaim(&p.admin, linux_office, revoked.cap_id), |
| 200 | aienos_cap_reclaim(p.native_admin, native_office, revoked.cap_id)); |
| 201 | RxCapRef reused; |
| 202 | mint_both(&p, "remint reclaimed", 7, 0x20, RX_RIGHT_WRITE, 0, none, 0, &reused); |
| 203 | if (reused.cap_id != revoked.cap_id || reused.generation != revoked.generation + 1) { |
| 204 | fprintf(stderr, "FAIL reclaim did not advance generation\n"); |
| 205 | g_fail++; |
| 206 | } |
| 207 | validate_both(&p, "stale generation", revoked, 1, 0x20, RX_RIGHT_WRITE); |
| 208 | |
| 209 | RxCapRef parent; |
| 210 | mint_both(&p, "delegable parent", 1, 0x20, RX_RIGHT_READ | RX_RIGHT_DELEGATE, 0, none, 0, |
| 211 | &parent); |
| 212 | mint_both(&p, "rights amplification", 2, 0x20, RX_RIGHT_READ | RX_RIGHT_WRITE, 0, parent, 1, |
| 213 | NULL); |
| 214 | mint_both(&p, "resource change", 2, 0x30, RX_RIGHT_READ, 0, parent, 1, NULL); |
| 215 | mint_both(&p, "not delegable", 2, 0x10, RX_RIGHT_READ, 0, read_cap, 1, NULL); |
| 216 | RxCapRef child; |
| 217 | mint_both(&p, "attenuated child", 2, 0x20, RX_RIGHT_READ, 0, parent, 1, &child); |
| 218 | validate_both(&p, "child valid", child, 2, 0x20, RX_RIGHT_READ); |
| 219 | expect_eq("revoke ancestor", rx_capadmin_revoke(&p.admin, linux_office, parent), |
| 220 | aienos_cap_revoke(p.native_admin, native_office, |
| 221 | to_native(&p, parent))); |
| 222 | validate_both(&p, "revoked ancestor", child, 2, 0x20, RX_RIGHT_READ); |
| 223 | |
| 224 | RxCapRef leased; |
| 225 | mint_both(&p, "leased", 1, 0x10, RX_RIGHT_READ, 5, none, 0, &leased); |
| 226 | expect_eq("advance clock", rx_capadmin_advance_clock(&p.admin, linux_office, 10), |
| 227 | aienos_cap_advance_clock(p.native_admin, native_office, 10)); |
| 228 | validate_both(&p, "expired lease", leased, 1, 0x10, RX_RIGHT_READ); |
| 229 | |
| 230 | RxCapRef before_epoch; |
| 231 | mint_both(&p, "before epoch", 1, 0x10, RX_RIGHT_READ, 0, none, 0, &before_epoch); |
| 232 | expect_eq("bump epoch", rx_capadmin_bump_epoch(&p.admin, linux_office), |
| 233 | aienos_cap_bump_epoch(p.native_admin, native_office)); |
| 234 | validate_both(&p, "stale epoch", before_epoch, 1, 0x10, RX_RIGHT_READ); |
| 235 | expect_eq("stale office epoch", rx_capadmin_bump_epoch(&p.admin, linux_office), |
| 236 | aienos_cap_bump_epoch(p.native_admin, native_office)); |
| 237 | |
| 238 | pair_stop(&p); |
| 239 | if (pair_start(&p) != 0) return -1; |
| 240 | linux_office = p.admin.office; |
| 241 | aienos_cap_office(p.native_admin, &native_office); |
| 242 | mint_both(&p, "privileged plus delegate", 1, 0, RX_RIGHT_REVOKE | RX_RIGHT_DELEGATE, 0, none, |
| 243 | 0, NULL); |
| 244 | RxCapRef revoker; |
| 245 | mint_both(&p, "limited revoker", 1, 0, RX_RIGHT_REVOKE, 0, none, 0, &revoker); |
| 246 | mint_both(&p, "delegate admin right", 2, 0, RX_RIGHT_REVOKE, 0, revoker, 1, NULL); |
| 247 | expect_eq("narrow revoker vs office", |
| 248 | rx_capadmin_revoke(&p.admin, revoker, linux_office), |
| 249 | aienos_cap_revoke(p.native_admin, to_native(&p, revoker), |
| 250 | native_office)); |
| 251 | validate_both(&p, "office still live", linux_office, 0, 0, RX_RIGHT_MINT); |
| 252 | /* aienos#266/#272: a privileged right on a resource other than the |
| 253 | * authority (the R16 operator's epoch right on the control resource) is |
| 254 | * refused by every authority operation, in both authorities. */ |
| 255 | RxCapRef control; |
| 256 | mint_both(&p, "control-resource privileged cap", 70, 0x906, |
| 257 | RX_RIGHT_EPOCH | RX_RIGHT_CLOCK | RX_RIGHT_REVOKE | RX_RIGHT_RECLAIM | RX_RIGHT_MINT, |
| 258 | 0, none, 0, &control); |
| 259 | expect_eq("control cap bumps the epoch", rx_capadmin_bump_epoch(&p.admin, control), |
| 260 | aienos_cap_bump_epoch(p.native_admin, to_native(&p, control))); |
| 261 | expect_eq("control cap advances the clock", |
| 262 | rx_capadmin_advance_clock(&p.admin, control, 1), |
| 263 | aienos_cap_advance_clock(p.native_admin, to_native(&p, control), 1)); |
| 264 | expect_eq("control cap revokes", rx_capadmin_revoke(&p.admin, control, revoker), |
| 265 | aienos_cap_revoke(p.native_admin, to_native(&p, control), to_native(&p, revoker))); |
| 266 | validate_both(&p, "control cap still live", control, 70, 0x906, RX_RIGHT_EPOCH); |
| 267 | |
| 268 | RxCapRef cur; |
| 269 | mint_both(&p, "depth base", 1, 0x20, RX_RIGHT_READ | RX_RIGHT_DELEGATE, 0, none, 0, &cur); |
| 270 | for (int i = 0; i < 8; i++) { |
| 271 | char name[32]; |
| 272 | snprintf(name, sizeof(name), "depth %d", i + 1); |
| 273 | if (mint_both(&p, name, 2, 0x20, RX_RIGHT_READ | RX_RIGHT_DELEGATE, 0, cur, 1, &cur) != 0) |
| 274 | break; |
| 275 | } |
| 276 | mint_both(&p, "delegation too deep", 2, 0x20, RX_RIGHT_READ | RX_RIGHT_DELEGATE, 0, cur, 1, |
| 277 | NULL); |
| 278 | |
| 279 | uint64_t advanced = 7; |
| 280 | expect_eq("generation wrap", rx_cap_generation_advance(UINT64_MAX, &advanced), |
| 281 | aienos_cap_generation_advance(UINT64_MAX, &advanced)); |
| 282 | if (advanced != 7) { |
| 283 | fprintf(stderr, "FAIL generation wrap wrote a new generation\n"); |
| 284 | g_fail++; |
| 285 | } |
| 286 | expect_eq("clock near the top", |
| 287 | rx_capadmin_advance_clock(&p.admin, linux_office, UINT64_MAX - 8), |
| 288 | aienos_cap_advance_clock(p.native_admin, native_office, UINT64_MAX - 8)); |
| 289 | expect_eq("clock wrap", rx_capadmin_advance_clock(&p.admin, linux_office, 100), |
| 290 | aienos_cap_advance_clock(p.native_admin, native_office, 100)); |
| 291 | |
| 292 | uint64_t before_clock = aienos_cap_clock(p.native_view); |
| 293 | AienosCapMint sneak; |
| 294 | memset(&sneak, 0, sizeof(sneak)); |
| 295 | sneak.issuer = 1; |
| 296 | sneak.subject = 1; |
| 297 | sneak.resource = 1; |
| 298 | sneak.rights = RX_RIGHT_MINT; |
| 299 | sneak.parent.cap_id = UINT32_MAX; |
| 300 | sneak.authority = native_office; |
| 301 | uint8_t guessed[32]; |
| 302 | memset(guessed, 0, sizeof(guessed)); |
| 303 | int native_cog = aienos_cap_cognition_mint(p.native_view, &sneak, guessed); |
| 304 | RxCapRequest forged; |
| 305 | memset(&forged, 0, sizeof(forged)); |
| 306 | forged.op = 1; /* mint */ |
| 307 | forged.mint.issuer = 1; |
| 308 | forged.mint.subject = 1; |
| 309 | forged.mint.resource = 1; |
| 310 | forged.mint.rights = RX_RIGHT_MINT; |
| 311 | forged.mint.parent.cap_id = UINT32_MAX; |
| 312 | forged.mint.authority = linux_office; |
| 313 | forged.authority = linux_office; |
| 314 | int wrote = write(p.admin.ctl_fd, &forged, sizeof(forged)) == (ssize_t)sizeof(forged); |
| 315 | struct { int32_t status; uint32_t pad; RxCapRef ref; } reply; |
| 316 | memset(&reply, 0, sizeof(reply)); |
| 317 | int got = read(p.admin.ctl_fd, &reply, sizeof(reply)) == (ssize_t)sizeof(reply); |
| 318 | if (!wrote || !got) { |
| 319 | fprintf(stderr, "FAIL could not complete the forged linux mint\n"); |
| 320 | g_fail++; |
| 321 | } else { |
| 322 | expect_eq("cognition mint", reply.status, native_cog); |
| 323 | } |
| 324 | int native_admin_try = aienos_cap_cognition_admin(p.native_view, 2, native_office, native_office); |
| 325 | expect_eq("cognition admin", RX_CAP_ERR_UNAUTHORIZED, native_admin_try); |
| 326 | if (aienos_cap_clock(p.native_view) != before_clock) { |
| 327 | fprintf(stderr, "FAIL cognition moved the clock\n"); |
| 328 | g_fail++; |
| 329 | } |
| 330 | |
| 331 | RxCapRef old; |
| 332 | mint_both(&p, "before restart", 1, 0x10, RX_RIGHT_READ, 0, none, 0, &old); |
| 333 | RxCapRef old_office = linux_office; |
| 334 | if (kill(p.root.root_pid, SIGKILL) != 0) { |
| 335 | fprintf(stderr, "FAIL could not stop the linux mint\n"); |
| 336 | g_fail++; |
| 337 | } |
| 338 | int status = 0; |
| 339 | waitpid(p.root.root_pid, &status, 0); |
| 340 | expect_eq("kill native writer", RX_CAP_OK, aienos_cap_kill(p.native_admin)); |
| 341 | RxCapMint dead; |
| 342 | memset(&dead, 0, sizeof(dead)); |
| 343 | dead.issuer = 3; |
| 344 | dead.subject = 1; |
| 345 | dead.resource = 0x10; |
| 346 | dead.rights = RX_RIGHT_READ; |
| 347 | dead.parent = none; |
| 348 | dead.authority = old_office; |
| 349 | AienosCapMint dead_native; |
| 350 | memset(&dead_native, 0, sizeof(dead_native)); |
| 351 | dead_native.issuer = 3; |
| 352 | dead_native.subject = 1; |
| 353 | dead_native.resource = 0x10; |
| 354 | dead_native.rights = RX_RIGHT_READ; |
| 355 | dead_native.parent.cap_id = UINT32_MAX; |
| 356 | dead_native.authority = native_office; |
| 357 | expect_eq("mint after death", rx_capadmin_mint(&p.admin, &dead, NULL), |
| 358 | aienos_cap_mint(p.native_admin, &dead_native, NULL)); |
| 359 | rx_caproot_stop(&p.root, &p.admin); |
| 360 | int linux_restart = rx_caproot_start(&p.root, &p.admin); |
| 361 | int native_restart = aienos_cap_restart(p.native_admin); |
| 362 | expect_eq("restart", linux_restart, native_restart); |
| 363 | validate_both(&p, "restart drops old capability", old, 1, 0x10, RX_RIGHT_READ); |
| 364 | validate_both(&p, "restart drops old office", old_office, 0, 0, RX_RIGHT_MINT); |
| 365 | pair_stop(&p); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int native_exhaustion(void) { |
| 370 | AienosCapAdmin *admin = NULL; |
| 371 | AienosCapView *view = NULL; |
| 372 | if (aienos_cap_start(&admin, &view) != RX_CAP_OK) { |
| 373 | fprintf(stderr, "FAIL native exhaustion start\n"); |
| 374 | g_fail++; |
| 375 | return -1; |
| 376 | } |
| 377 | AienosCapRef office; |
| 378 | aienos_cap_office(admin, &office); |
| 379 | AienosCapMint mint; |
| 380 | memset(&mint, 0, sizeof(mint)); |
| 381 | mint.issuer = 3; |
| 382 | mint.subject = 1; |
| 383 | mint.resource = 0x10; |
| 384 | mint.rights = RX_RIGHT_READ; |
| 385 | mint.parent.cap_id = UINT32_MAX; |
| 386 | mint.authority = office; |
| 387 | AienosCapRef cap; |
| 388 | int rc = aienos_cap_mint(admin, &mint, &cap); |
| 389 | if (rc != RX_CAP_OK) { |
| 390 | fprintf(stderr, "FAIL exhaustion mint\n"); |
| 391 | g_fail++; |
| 392 | } |
| 393 | rc = aienos_cap_revoke(admin, office, cap); |
| 394 | if (rc != RX_CAP_OK) { |
| 395 | fprintf(stderr, "FAIL exhaustion revoke\n"); |
| 396 | g_fail++; |
| 397 | } |
| 398 | expect_eq("force exhausted generation", RX_CAP_OK, |
| 399 | aienos_cap_force_generation(admin, cap.cap_id, UINT64_MAX)); |
| 400 | expect_eq("reclaim refuses generation wrap", RX_CAP_ERR_EXHAUSTED, |
| 401 | aienos_cap_reclaim(admin, office, cap.cap_id)); |
| 402 | expect_eq("wrapped generation does not validate", RX_CAP_ERR_STALE_GEN, |
| 403 | aienos_cap_validate(view, cap, 1, 0x10, RX_RIGHT_READ, NULL)); |
| 404 | aienos_cap_stop(admin, view); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int table_full(void) { |
| 409 | Pair p; |
| 410 | if (pair_start(&p) != 0) return -1; |
| 411 | RxCapRef none = { UINT32_MAX, 0 }; |
| 412 | int linux_full = -1; |
| 413 | int native_full = -1; |
| 414 | int n = 0; |
| 415 | for (uint32_t i = 0; i < RX_CAP_MAX + 2; i++) { |
| 416 | RxCapMint linux_mint; |
| 417 | AienosCapMint native_mint; |
| 418 | fill_mint(&p, &linux_mint, &native_mint, 1, 0x70, RX_RIGHT_READ, 0, none); |
| 419 | linux_mint.authority = p.admin.office; |
| 420 | AienosCapRef office; |
| 421 | aienos_cap_office(p.native_admin, &office); |
| 422 | native_mint.authority = office; |
| 423 | int linux_rc = rx_capadmin_mint(&p.admin, &linux_mint, NULL); |
| 424 | int native_rc = aienos_cap_mint(p.native_admin, &native_mint, NULL); |
| 425 | if (linux_rc != RX_CAP_OK && linux_full < 0) linux_full = linux_rc; |
| 426 | if (native_rc != RX_CAP_OK && native_full < 0) native_full = native_rc; |
| 427 | if (linux_rc == RX_CAP_OK && native_rc == RX_CAP_OK) n++; |
| 428 | if (linux_rc != RX_CAP_OK && native_rc != RX_CAP_OK) break; |
| 429 | } |
| 430 | expect_eq("table full", linux_full, native_full); |
| 431 | if (n != (int)RX_CAP_MAX - 1) { |
| 432 | fprintf(stderr, "FAIL filled %d slots\n", n); |
| 433 | g_fail++; |
| 434 | } |
| 435 | pair_stop(&p); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /* The old defect: a reclaim moved a slot to boot + 1 and the next root also |
| 440 | * started at boot + 1, so a reference from a stopped root validated against |
| 441 | * a fresh mint of the same slot in the next one. */ |
| 442 | static int restart_generations(void) { |
| 443 | RxCapRef none = { UINT32_MAX, 0 }; |
| 444 | Pair a; |
| 445 | if (pair_start(&a) != 0) return -1; |
| 446 | RxCapRef first, second; |
| 447 | mint_both(&a, "restart: first", 1, 0x40, RX_RIGHT_READ, 0, none, 0, &first); |
| 448 | AienosCapRef native_office; |
| 449 | aienos_cap_office(a.native_admin, &native_office); |
| 450 | expect_eq("restart: revoke", rx_capadmin_revoke(&a.admin, a.admin.office, first), |
| 451 | aienos_cap_revoke(a.native_admin, native_office, to_native(&a, first))); |
| 452 | expect_eq("restart: reclaim", rx_capadmin_reclaim(&a.admin, a.admin.office, first.cap_id), |
| 453 | aienos_cap_reclaim(a.native_admin, native_office, first.cap_id)); |
| 454 | mint_both(&a, "restart: second", 1, 0x40, RX_RIGHT_READ, 0, none, 0, &second); |
| 455 | AienosCapRef native_second = to_native(&a, second); |
| 456 | if (second.cap_id != first.cap_id || second.generation != first.generation + 1) { |
| 457 | fprintf(stderr, "FAIL restart: reclaim did not reuse the slot one generation up\n"); |
| 458 | g_fail++; |
| 459 | } |
| 460 | if (a.linux_base <= UINT32_MAX || a.native_base <= UINT32_MAX) { |
| 461 | fprintf(stderr, "FAIL restart: boot generation still fits in 32 bits\n"); |
| 462 | g_fail++; |
| 463 | } |
| 464 | uint64_t a_linux = a.linux_base, a_native = a.native_base; |
| 465 | pair_stop(&a); |
| 466 | |
| 467 | Pair b; |
| 468 | if (pair_start(&b) != 0) return -1; |
| 469 | if (b.linux_base <= second.generation || b.native_base <= native_second.generation || |
| 470 | b.linux_base <= a_linux || b.native_base <= a_native) { |
| 471 | fprintf(stderr, "FAIL restart: new root did not start above the old one\n"); |
| 472 | g_fail++; |
| 473 | } |
| 474 | RxCapRef fresh; |
| 475 | mint_both(&b, "restart: fresh", 1, 0x40, RX_RIGHT_READ, 0, none, 0, &fresh); |
| 476 | if (fresh.cap_id != second.cap_id) { |
| 477 | fprintf(stderr, "FAIL restart: fresh mint used another slot\n"); |
| 478 | g_fail++; |
| 479 | } |
| 480 | expect_eq("restart: old ref stale (linux)", RX_CAP_ERR_STALE_GEN, |
| 481 | rx_caproot_validate(&b.root, second, 1, 0x40, RX_RIGHT_READ, NULL)); |
| 482 | expect_eq("restart: old ref stale (native)", RX_CAP_ERR_STALE_GEN, |
| 483 | aienos_cap_validate(b.native_view, native_second, 1, 0x40, RX_RIGHT_READ, NULL)); |
| 484 | validate_both(&b, "restart: fresh valid", fresh, 1, 0x40, RX_RIGHT_READ); |
| 485 | RxCapRef truncated = { fresh.cap_id, (uint32_t)fresh.generation }; |
| 486 | validate_both(&b, "restart: truncated generation refused", truncated, 1, 0x40, |
| 487 | RX_RIGHT_READ); |
| 488 | expect_eq("restart: truncated generation stale", RX_CAP_ERR_STALE_GEN, |
| 489 | rx_caproot_validate(&b.root, truncated, 1, 0x40, RX_RIGHT_READ, NULL)); |
| 490 | |
| 491 | /* In-process restart of the native table: same promise. */ |
| 492 | AienosCapRef native_fresh = to_native(&b, fresh); |
| 493 | expect_eq("restart: native restart", RX_CAP_OK, aienos_cap_restart(b.native_admin)); |
| 494 | AienosCapRef after; |
| 495 | aienos_cap_office(b.native_admin, &after); |
| 496 | if (after.generation <= native_fresh.generation) { |
| 497 | fprintf(stderr, "FAIL restart: native restart did not move above the old table\n"); |
| 498 | g_fail++; |
| 499 | } |
| 500 | expect_eq("restart: pre-restart native ref stale", RX_CAP_ERR_STALE_GEN, |
| 501 | aienos_cap_validate(b.native_view, native_fresh, 1, 0x40, RX_RIGHT_READ, NULL)); |
| 502 | pair_stop(&b); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static AienosCapView *g_view; |
| 507 | static int g_cognition; |
| 508 | |
| 509 | static int reaction_tries_to_mint(RxCtx *ctx) { |
| 510 | (void)ctx; |
| 511 | AienosCapMint mint; |
| 512 | memset(&mint, 0, sizeof(mint)); |
| 513 | mint.subject = 1; |
| 514 | mint.resource = 1; |
| 515 | mint.rights = RX_RIGHT_MINT | RX_RIGHT_REVOKE | RX_RIGHT_CLOCK | RX_RIGHT_EPOCH; |
| 516 | mint.parent.cap_id = UINT32_MAX; |
| 517 | g_cognition = aienos_cap_cognition_mint(g_view, &mint, NULL); |
| 518 | AienosCapRef none = { UINT32_MAX, 0 }; |
| 519 | int admin = aienos_cap_cognition_admin(g_view, 2, none, none); |
| 520 | if (g_cognition == RX_CAP_ERR_UNAUTHORIZED && admin == RX_CAP_ERR_UNAUTHORIZED) |
| 521 | g_cognition = RX_CAP_ERR_UNAUTHORIZED; |
| 522 | else |
| 523 | g_cognition = -100; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int world_uses_native(void) { |
| 528 | AienosCapAdmin *admin = NULL; |
| 529 | AienosCapView *view = NULL; |
| 530 | if (aienos_cap_start(&admin, &view) != RX_CAP_OK) { |
| 531 | fprintf(stderr, "FAIL world authority start\n"); |
| 532 | g_fail++; |
| 533 | return -1; |
| 534 | } |
| 535 | g_view = view; |
| 536 | RxWorld world; |
| 537 | if (rx_world_init_native(&world, view, 2, 1u << 12) != RX_OK) { |
| 538 | fprintf(stderr, "FAIL native world init\n"); |
| 539 | g_fail++; |
| 540 | aienos_cap_stop(admin, view); |
| 541 | return -1; |
| 542 | } |
| 543 | world.external_subject = 100; |
| 544 | uint64_t init[RX_MAX_FIELDS] = { 0 }; |
| 545 | RxObjRef sensor; |
| 546 | if (rx_world_create(&world, 1, RX_PERSIST_RESIDENT, 0x10, init, &sensor) != RX_OK) { |
| 547 | fprintf(stderr, "FAIL create sensor\n"); |
| 548 | g_fail++; |
| 549 | } |
| 550 | AienosCapRef office; |
| 551 | aienos_cap_office(admin, &office); |
| 552 | AienosCapMint mint; |
| 553 | memset(&mint, 0, sizeof(mint)); |
| 554 | mint.issuer = 3; |
| 555 | mint.subject = 100; |
| 556 | mint.resource = 0x10; |
| 557 | mint.rights = RX_RIGHT_WRITE; |
| 558 | mint.parent.cap_id = UINT32_MAX; |
| 559 | mint.authority = office; |
| 560 | AienosCapRef ext; |
| 561 | if (aienos_cap_mint(admin, &mint, &ext) != RX_CAP_OK) { |
| 562 | fprintf(stderr, "FAIL external mint\n"); |
| 563 | g_fail++; |
| 564 | } |
| 565 | RxCapRef forged = { 200, 7 }; |
| 566 | RxMutation mutation = { sensor, 0, 5 }; |
| 567 | if (rx_world_publish_external(&world, forged, &mutation, 1) != RX_ERR_AUTHORITY) { |
| 568 | fprintf(stderr, "FAIL world accepted a forged handle\n"); |
| 569 | g_fail++; |
| 570 | } |
| 571 | RxObject object; |
| 572 | rx_world_read(&world, sensor, &object); |
| 573 | if (object.field[0] != 0) { |
| 574 | fprintf(stderr, "FAIL forged write changed the object\n"); |
| 575 | g_fail++; |
| 576 | } |
| 577 | RxCapRef good = { ext.cap_id, ext.generation }; |
| 578 | if (rx_world_publish_external(&world, good, &mutation, 1) < 0) { |
| 579 | fprintf(stderr, "FAIL world rejected a good native capability\n"); |
| 580 | g_fail++; |
| 581 | } |
| 582 | mint.subject = 1; |
| 583 | mint.rights = RX_RIGHT_READ; |
| 584 | mint.resource = 0x10; |
| 585 | AienosCapRef reader; |
| 586 | aienos_cap_mint(admin, &mint, &reader); |
| 587 | mint.resource = 0x20; |
| 588 | mint.rights = RX_RIGHT_WRITE; |
| 589 | AienosCapRef writer; |
| 590 | aienos_cap_mint(admin, &mint, &writer); |
| 591 | RxObjRef belief; |
| 592 | uint64_t belief_init[RX_MAX_FIELDS] = { 0 }; |
| 593 | rx_world_create(&world, 1, RX_PERSIST_RESIDENT, 0x20, belief_init, &belief); |
| 594 | RxReactionDesc desc; |
| 595 | memset(&desc, 0, sizeof(desc)); |
| 596 | desc.name = "cognition.tries.mint"; |
| 597 | desc.faculty = RX_FACULTY_AIEN; |
| 598 | desc.subject = 1; |
| 599 | desc.fn = reaction_tries_to_mint; |
| 600 | desc.n_triggers = 1; |
| 601 | desc.triggers[0].obj = sensor; |
| 602 | desc.triggers[0].mask = RX_FIELD(0); |
| 603 | desc.n_writes = 1; |
| 604 | desc.writes[0].obj = belief; |
| 605 | desc.writes[0].mask = RX_FIELD(0); |
| 606 | desc.n_caps = 2; |
| 607 | desc.caps[0].ref = (RxCapRef){ reader.cap_id, reader.generation }; |
| 608 | desc.caps[0].resource = 0x10; |
| 609 | desc.caps[0].rights = RX_RIGHT_READ; |
| 610 | desc.caps[1].ref = (RxCapRef){ writer.cap_id, writer.generation }; |
| 611 | desc.caps[1].resource = 0x20; |
| 612 | desc.caps[1].rights = RX_RIGHT_WRITE; |
| 613 | uint32_t id = 0; |
| 614 | if (rx_world_add_reaction(&world, &desc, &id) != RX_OK) { |
| 615 | fprintf(stderr, "FAIL add cognition reaction\n"); |
| 616 | g_fail++; |
| 617 | } |
| 618 | uint64_t clock_before = aienos_cap_clock(view); |
| 619 | mutation.value = 9; |
| 620 | if (rx_world_publish_external(&world, good, &mutation, 1) < 0) { |
| 621 | fprintf(stderr, "FAIL stimulus\n"); |
| 622 | g_fail++; |
| 623 | } |
| 624 | rx_world_wait_quiescent(&world, 5000); |
| 625 | expect_eq("reaction cannot mint or administer", RX_CAP_ERR_UNAUTHORIZED, g_cognition); |
| 626 | if (aienos_cap_clock(view) != clock_before) { |
| 627 | fprintf(stderr, "FAIL reaction moved the authority clock\n"); |
| 628 | g_fail++; |
| 629 | } |
| 630 | if (aienos_cap_validate(view, office, 0, 0, RX_RIGHT_MINT, NULL) != RX_CAP_OK) { |
| 631 | fprintf(stderr, "FAIL office changed after cognition\n"); |
| 632 | g_fail++; |
| 633 | } |
| 634 | rx_world_destroy(&world); |
| 635 | aienos_cap_stop(admin, view); |
| 636 | g_view = NULL; |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static void binary_digest(char out[65]) { |
| 641 | strcpy(out, "unavailable"); |
| 642 | FILE *f = fopen("/proc/self/exe", "rb"); |
| 643 | if (!f) return; |
| 644 | sha256_ctx c; |
| 645 | sha256_init(&c); |
| 646 | uint8_t buf[65536]; |
| 647 | size_t n; |
| 648 | while ((n = fread(buf, 1, sizeof buf, f)) > 0) sha256_update(&c, buf, n); |
| 649 | fclose(f); |
| 650 | uint8_t d[32]; |
| 651 | sha256_final(&c, d); |
| 652 | for (int i = 0; i < 32; i++) sprintf(&out[i * 2], "%02x", d[i]); |
| 653 | } |
| 654 | |
| 655 | static void write_receipt(void) { |
| 656 | char path[512]; |
| 657 | if (omega_evidence_path("R7/rx_native_authority_receipt.json", path, sizeof path) != 0) |
| 658 | return; |
| 659 | FILE *f = fopen(path, "w"); |
| 660 | if (!f) return; |
| 661 | char commit[41]; |
| 662 | memset(commit, 0, sizeof commit); |
| 663 | if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8); |
| 664 | const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT"); |
| 665 | const char *aienos = getenv("AIENOS_COMMIT"); |
| 666 | int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && |
| 667 | !omega_evidence_tree_dirty(); |
| 668 | char digest[65]; |
| 669 | binary_digest(digest); |
| 670 | struct utsname u; |
| 671 | memset(&u, 0, sizeof u); |
| 672 | uname(&u); |
| 673 | fprintf(f, |
| 674 | "{\n" |
| 675 | " \"schema\": \"AIEN_RX_R7_NATIVE_AUTHORITY_V1\",\n" |
| 676 | " \"run_id\": \"%s\",\n" |
| 677 | " \"candidate_commit\": %s%s%s,\n" |
| 678 | " \"candidate_bound\": %s,\n" |
| 679 | " \"run_commit\": \"%s\",\n" |
| 680 | " \"tree_dirty\": %s,\n" |
| 681 | " \"aienos_commit\": %s%s%s,\n" |
| 682 | " \"test_binary_sha256\": \"%s\",\n" |
| 683 | " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\"},\n" |
| 684 | " \"hardware_scope\": \"host CPU only; Linux mint kept as the oracle; no graphics processor\",\n" |
| 685 | " \"gates\": {\n" |
| 686 | " \"R7_NATIVE_AUTHORITY\": \"PASS (host; same outcomes as the Linux oracle; oracle retained)\",\n" |
| 687 | " \"not_claimed\": [\"R8\", \"R9\", \"R10\", \"R11\", \"R12 silicon\", \"R13\"]\n" |
| 688 | " },\n" |
| 689 | " \"differential\": [\n", |
| 690 | omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null", |
| 691 | candidate ? "\"" : "", bound ? "true" : "false", commit, |
| 692 | omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "", |
| 693 | aienos ? aienos : "null", aienos ? "\"" : "", digest, u.sysname, u.release, u.machine); |
| 694 | for (int i = 0; i < g_ndiff; i++) { |
| 695 | fprintf(f, " {\"name\": \"%s\", \"linux\": %d, \"native\": %d}%s\n", g_diff[i].name, |
| 696 | g_diff[i].code, g_diff[i].code, i + 1 < g_ndiff ? "," : ""); |
| 697 | } |
| 698 | fprintf(f, " ]\n}\n"); |
| 699 | fclose(f); |
| 700 | printf("receipt: %s\n", path); |
| 701 | printf("candidate bound: %s\n", bound ? "yes" : "no"); |
| 702 | } |
| 703 | |
| 704 | int main(void) { |
| 705 | signal(SIGPIPE, SIG_IGN); |
| 706 | corpus(); |
| 707 | table_full(); |
| 708 | native_exhaustion(); |
| 709 | restart_generations(); |
| 710 | world_uses_native(); |
| 711 | if (g_fail) { |
| 712 | fprintf(stderr, "%d native-authority mismatches\n", g_fail); |
| 713 | return 1; |
| 714 | } |
| 715 | write_receipt(); |
| 716 | printf("native authority matched the linux oracle, and the world checks the native view\n"); |
| 717 | return 0; |
| 718 | } |