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_operator_authority_misuse.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_operator_authority_misuse.c -- the R16 operator control capability |
| 19 | * against the locked native Aienux authority (aienos#266, aienos#272). |
| 20 | * |
| 21 | * The operator's capability is minted as the R13 program mints it |
| 22 | * (tests/runtime/rx_r13_living.c: RX_OPERATOR_SUBJ, RX_WORLD_RES_CONTROL, |
| 23 | * RX_WORLD_RIGHT_HALT, issued by the office). It is the epoch right on the |
| 24 | * control resource (docs/r16-operator-control.md section 6.1). This test |
| 25 | * proves, by execution against $(AIENOS_CAP_LIB): |
| 26 | * (a) the authority's office still mints, revokes, reclaims, advances the |
| 27 | * clock and bumps the epoch; |
| 28 | * (b) the control capability, and one carrying every privileged right on |
| 29 | * the control resource, are refused by every authority operation with |
| 30 | * RX_CAP_ERR_RESOURCE, and nothing changes; |
| 31 | * (c) the world still accepts the control capability for the halt check |
| 32 | * (rx_world_validate_cap on RX_WORLD_RES_CONTROL with |
| 33 | * RX_WORLD_RIGHT_HALT), the check stop/status/resume run through. |
| 34 | * Against an aienos.lock without the resource check (bbad5e4) part (b) fails. |
| 35 | */ |
| 36 | #include <stdint.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "../../src/runtime/aienos_cap.h" |
| 41 | #include "../../src/runtime/rx_caproot.h" |
| 42 | #include "../../src/runtime/rx_operator.h" |
| 43 | #include "../../src/runtime/rx_world.h" |
| 44 | |
| 45 | static int g_fail, g_pass; |
| 46 | |
| 47 | static void expect(const char *what, int got, int want) { |
| 48 | if (got == want) { |
| 49 | g_pass++; |
| 50 | printf("ok %s (%d)\n", what, got); |
| 51 | } else { |
| 52 | g_fail++; |
| 53 | printf("FAIL %s: got %d, want %d\n", what, got, want); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static AienosCapRef office_of(AienosCapAdmin *admin) { |
| 58 | AienosCapRef o = {UINT32_MAX, 0}; |
| 59 | aienos_cap_office(admin, &o); |
| 60 | return o; |
| 61 | } |
| 62 | |
| 63 | static int mint(AienosCapAdmin *admin, uint32_t subject, uint64_t resource, uint32_t rights, |
| 64 | AienosCapRef *out) { |
| 65 | AienosCapMint m = {3, subject, resource, rights, 0, {UINT32_MAX, 0}, office_of(admin)}; |
| 66 | return aienos_cap_mint(admin, &m, out); |
| 67 | } |
| 68 | |
| 69 | static RxCapRef rx(AienosCapRef r) { return (RxCapRef){r.cap_id, r.generation}; } |
| 70 | |
| 71 | int main(void) { |
| 72 | AienosCapAdmin *admin = NULL; |
| 73 | AienosCapView *view = NULL; |
| 74 | if (aienos_cap_start(&admin, &view) != RX_CAP_OK) { |
| 75 | printf("FAIL native authority start\n"); |
| 76 | return 1; |
| 77 | } |
| 78 | RxWorld world; |
| 79 | if (rx_world_init_native(&world, view, 2, 1u << 12) != RX_OK) { |
| 80 | printf("FAIL native world init\n"); |
| 81 | aienos_cap_stop(admin, view); |
| 82 | return 1; |
| 83 | } |
| 84 | const uint32_t all_priv = RX_RIGHT_MINT | RX_RIGHT_REVOKE | RX_RIGHT_RECLAIM | |
| 85 | RX_RIGHT_EPOCH | RX_RIGHT_CLOCK; |
| 86 | AienosCapRef halt, wide, victim, victim2, minted; |
| 87 | expect("mint operator control capability (as R13)", |
| 88 | mint(admin, RX_OPERATOR_SUBJ, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT, &halt), RX_CAP_OK); |
| 89 | expect("mint every privileged right on the control resource", |
| 90 | mint(admin, RX_OPERATOR_SUBJ, RX_WORLD_RES_CONTROL, all_priv, &wide), RX_CAP_OK); |
| 91 | expect("mint a victim", mint(admin, 2, 0x77, RX_RIGHT_READ, &victim), RX_CAP_OK); |
| 92 | |
| 93 | /* (b) misuse: the control capability operates no authority entry point. |
| 94 | * The halt cap holds only the epoch right, so its epoch bump is the hole |
| 95 | * aienos#266 named (refused on the resource since aienos#272); its other |
| 96 | * operations lack the right and stay refused as before. The wide cap holds |
| 97 | * every right each operation needs, so every refusal is the resource. */ |
| 98 | expect("halt cap: bump epoch refused on the resource", aienos_cap_bump_epoch(admin, halt), |
| 99 | RX_CAP_ERR_RESOURCE); |
| 100 | expect("halt cap: advance clock refused (no clock right)", |
| 101 | aienos_cap_advance_clock(admin, halt, 1), RX_CAP_ERR_UNAUTHORIZED); |
| 102 | expect("halt cap: revoke refused (no revoke right)", aienos_cap_revoke(admin, halt, victim), |
| 103 | RX_CAP_ERR_UNAUTHORIZED); |
| 104 | AienosCapMint mh = {3, 2, 0x78, RX_RIGHT_READ, 0, {UINT32_MAX, 0}, halt}; |
| 105 | expect("halt cap: mint refused (no mint right)", aienos_cap_mint(admin, &mh, &minted), |
| 106 | RX_CAP_ERR_UNAUTHORIZED); |
| 107 | expect("wide control cap: bump epoch refused on the resource", |
| 108 | aienos_cap_bump_epoch(admin, wide), RX_CAP_ERR_RESOURCE); |
| 109 | expect("wide control cap: advance clock refused on the resource", |
| 110 | aienos_cap_advance_clock(admin, wide, 1), RX_CAP_ERR_RESOURCE); |
| 111 | expect("wide control cap: revoke refused on the resource", |
| 112 | aienos_cap_revoke(admin, wide, victim), RX_CAP_ERR_RESOURCE); |
| 113 | expect("wide control cap: reclaim refused on the resource", |
| 114 | aienos_cap_reclaim(admin, wide, victim.cap_id), RX_CAP_ERR_RESOURCE); |
| 115 | AienosCapMint mw = {3, 2, 0x78, RX_RIGHT_READ, 0, {UINT32_MAX, 0}, wide}; |
| 116 | expect("wide control cap: mint refused on the resource", aienos_cap_mint(admin, &mw, &minted), |
| 117 | RX_CAP_ERR_RESOURCE); |
| 118 | /* Nothing moved: the victim and both control capabilities are still live |
| 119 | * at the same epoch (a bump would have staled every one of them). */ |
| 120 | expect("victim still valid", rx_world_validate_cap(&world, rx(victim), 2, 0x77, RX_RIGHT_READ, NULL), |
| 121 | RX_CAP_OK); |
| 122 | |
| 123 | /* (c) the halt check the operator commands run through still passes. */ |
| 124 | expect("world accepts control cap for the halt check", |
| 125 | rx_world_validate_cap(&world, rx(halt), RX_OPERATOR_SUBJ, RX_WORLD_RES_CONTROL, |
| 126 | RX_WORLD_RIGHT_HALT, NULL), RX_CAP_OK); |
| 127 | expect("world refuses control cap on another resource", |
| 128 | rx_world_validate_cap(&world, rx(halt), RX_OPERATOR_SUBJ, 0x77, RX_WORLD_RIGHT_HALT, NULL), |
| 129 | RX_CAP_ERR_RESOURCE); |
| 130 | |
| 131 | /* (a) legitimate authority operations by the office still succeed. */ |
| 132 | AienosCapRef office = office_of(admin); |
| 133 | expect("office advances the clock", aienos_cap_advance_clock(admin, office, 1), RX_CAP_OK); |
| 134 | expect("office revokes", aienos_cap_revoke(admin, office, victim), RX_CAP_OK); |
| 135 | expect("office reclaims", aienos_cap_reclaim(admin, office, victim.cap_id), RX_CAP_OK); |
| 136 | expect("office mints", mint(admin, 2, 0x79, RX_RIGHT_READ, &victim2), RX_CAP_OK); |
| 137 | AienosCapRef clock_cap; |
| 138 | expect("authority-resource clock cap minted", |
| 139 | mint(admin, RX_OPERATOR_SUBJ, 0 /* AIENOS_CAP_RES_AUTHORITY */, RX_RIGHT_CLOCK, &clock_cap), |
| 140 | RX_CAP_OK); |
| 141 | expect("authority-resource clock cap advances the clock", |
| 142 | aienos_cap_advance_clock(admin, clock_cap, 1), RX_CAP_OK); |
| 143 | expect("office bumps the epoch", aienos_cap_bump_epoch(admin, office), RX_CAP_OK); |
| 144 | expect("control cap stale after the office's epoch bump", |
| 145 | rx_world_validate_cap(&world, rx(halt), RX_OPERATOR_SUBJ, RX_WORLD_RES_CONTROL, |
| 146 | RX_WORLD_RIGHT_HALT, NULL), RX_CAP_ERR_EPOCH); |
| 147 | |
| 148 | rx_world_destroy(&world); |
| 149 | aienos_cap_stop(admin, view); |
| 150 | printf("rx_operator_authority_misuse: %d passed, %d failed\n", g_pass, g_fail); |
| 151 | if (g_fail) return 1; |
| 152 | printf("OMEGA_OPERATOR_AUTHORITY_MISUSE_REFUSED PASS\n"); |
| 153 | return 0; |
| 154 | } |