aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / capability / tests / capability_test.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 | * capability_test.c -- rules of the native capability authority. |
| 19 | * |
| 20 | * Each case names the rule it holds. A failure prints the line and the |
| 21 | * process exits nonzero. |
| 22 | */ |
| 23 | #include "../aienos_capability.h" |
| 24 | |
| 25 | #include <pthread.h> |
| 26 | #include <stdbool.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | static int failures; |
| 32 | static int checks; |
| 33 | |
| 34 | #define CHECK(cond) \ |
| 35 | do { \ |
| 36 | checks++; \ |
| 37 | if (!(cond)) { \ |
| 38 | failures++; \ |
| 39 | fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ |
| 40 | } \ |
| 41 | } while (0) |
| 42 | |
| 43 | #define EQ(a, b) CHECK((a) == (b)) |
| 44 | |
| 45 | /* Entries are compared field by field, never with memcmp. AienosCapEntry |
| 46 | * has padding (after cap_id, parent_id and minted_by_id), and a struct copy |
| 47 | * such as `*out = *e` need not copy padding bytes, so memcmp can see stack |
| 48 | * garbage in the padding of two equal entries. How a compiler copies differs |
| 49 | * by target (the suspected cause of the x86-64 CI baseline failure). */ |
| 50 | static int same_entry(const AienosCapEntry *x, const AienosCapEntry *y) { |
| 51 | return x->cap_id == y->cap_id && x->generation == y->generation && x->state == y->state && |
| 52 | x->issuer == y->issuer && x->subject == y->subject && x->rights == y->rights && |
| 53 | x->resource == y->resource && x->epoch == y->epoch && |
| 54 | x->lease_expiry == y->lease_expiry && x->parent_id == y->parent_id && |
| 55 | x->parent_generation == y->parent_generation && |
| 56 | x->minted_by_id == y->minted_by_id && |
| 57 | x->minted_by_generation == y->minted_by_generation; |
| 58 | } |
| 59 | |
| 60 | static const AienosCapRef NONE = {AIENOS_CAP_PARENT_NONE, 0}; |
| 61 | |
| 62 | typedef struct { |
| 63 | AienosCapAdmin *admin; |
| 64 | AienosCapView *view; |
| 65 | } Auth; |
| 66 | |
| 67 | static Auth boot(void) { |
| 68 | Auth a = {0}; |
| 69 | if (aienos_cap_start(&a.admin, &a.view) != AIENOS_CAP_OK) { |
| 70 | fprintf(stderr, "start failed\n"); |
| 71 | exit(2); |
| 72 | } |
| 73 | return a; |
| 74 | } |
| 75 | |
| 76 | static AienosCapRef office(const Auth *a) { |
| 77 | AienosCapRef r; |
| 78 | aienos_cap_office(a->admin, &r); |
| 79 | return r; |
| 80 | } |
| 81 | |
| 82 | static int root_mint(Auth *a, uint32_t subject, uint64_t resource, uint32_t rights, |
| 83 | AienosCapRef *out) { |
| 84 | AienosCapMint m = {3, subject, resource, rights, 0, NONE, office(a)}; |
| 85 | return aienos_cap_mint(a->admin, &m, out); |
| 86 | } |
| 87 | |
| 88 | static int child_mint(Auth *a, AienosCapRef parent, uint32_t issuer, uint32_t subject, |
| 89 | uint64_t resource, uint32_t rights, AienosCapRef *out) { |
| 90 | AienosCapMint m = {issuer, subject, resource, rights, 0, parent, parent}; |
| 91 | return aienos_cap_mint(a->admin, &m, out); |
| 92 | } |
| 93 | |
| 94 | static int validate(Auth *a, AienosCapRef c, uint32_t subject, uint64_t resource, |
| 95 | uint32_t rights) { |
| 96 | return aienos_cap_validate(a->view, c, subject, resource, rights, NULL); |
| 97 | } |
| 98 | |
| 99 | static void high_half_resource_is_kept(void) { |
| 100 | Auth a = boot(); |
| 101 | uint64_t wide = (1ull << 32) | 0x51; |
| 102 | AienosCapRef cap; |
| 103 | EQ(root_mint(&a, 1, wide, AIENOS_CAP_RIGHT_READ, &cap), AIENOS_CAP_OK); |
| 104 | EQ(validate(&a, cap, 1, wide, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 105 | EQ(validate(&a, cap, 1, 0x51, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_RESOURCE); |
| 106 | aienos_cap_stop(a.admin, a.view); |
| 107 | } |
| 108 | |
| 109 | static void forged_stale_subject_resource_and_rights_fail_closed(void) { |
| 110 | Auth a = boot(); |
| 111 | AienosCapRef cap, reused, live; |
| 112 | EQ(root_mint(&a, 1, 0x20, AIENOS_CAP_RIGHT_WRITE, &cap), AIENOS_CAP_OK); |
| 113 | EQ(validate(&a, (AienosCapRef){200, 7}, 1, 0x20, AIENOS_CAP_RIGHT_WRITE), |
| 114 | AIENOS_CAP_ERR_STALE_GEN); |
| 115 | EQ(validate(&a, (AienosCapRef){300, 1}, 1, 0x20, AIENOS_CAP_RIGHT_WRITE), |
| 116 | AIENOS_CAP_ERR_BOUNDS); |
| 117 | EQ(aienos_cap_revoke(a.admin, office(&a), cap), AIENOS_CAP_OK); |
| 118 | EQ(aienos_cap_reclaim(a.admin, office(&a), cap.cap_id), AIENOS_CAP_OK); |
| 119 | EQ(root_mint(&a, 7, 0x20, AIENOS_CAP_RIGHT_WRITE, &reused), AIENOS_CAP_OK); |
| 120 | EQ(reused.cap_id, cap.cap_id); |
| 121 | EQ(reused.generation, cap.generation + 1); |
| 122 | EQ(validate(&a, cap, 1, 0x20, AIENOS_CAP_RIGHT_WRITE), AIENOS_CAP_ERR_STALE_GEN); |
| 123 | EQ(root_mint(&a, 1, 0x20, AIENOS_CAP_RIGHT_READ, &live), AIENOS_CAP_OK); |
| 124 | EQ(validate(&a, live, 9, 0x20, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_SUBJECT); |
| 125 | EQ(validate(&a, live, 1, 0x30, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_RESOURCE); |
| 126 | EQ(validate(&a, live, 1, 0x20, AIENOS_CAP_RIGHT_WRITE), AIENOS_CAP_ERR_RIGHTS); |
| 127 | aienos_cap_stop(a.admin, a.view); |
| 128 | } |
| 129 | |
| 130 | static void amplification_lease_epoch_and_revoked_ancestor_fail(void) { |
| 131 | Auth a = boot(); |
| 132 | AienosCapRef parent, child, out; |
| 133 | AienosCapEntry e; |
| 134 | EQ(root_mint(&a, 1, 0x20, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE, &parent), |
| 135 | AIENOS_CAP_OK); |
| 136 | EQ(child_mint(&a, parent, 1, 2, 0x20, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE, &out), |
| 137 | AIENOS_CAP_ERR_AMPLIFY); |
| 138 | EQ(child_mint(&a, parent, 1, 2, 0x20, AIENOS_CAP_RIGHT_READ, &child), AIENOS_CAP_OK); |
| 139 | EQ(aienos_cap_revoke(a.admin, office(&a), parent), AIENOS_CAP_OK); |
| 140 | EQ(aienos_cap_inspect(a.view, child, &e), AIENOS_CAP_OK); |
| 141 | EQ(e.state, AIENOS_CAP_STATE_REVOKED); |
| 142 | EQ(validate(&a, child, 2, 0x20, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_REVOKED); |
| 143 | aienos_cap_stop(a.admin, a.view); |
| 144 | |
| 145 | a = boot(); |
| 146 | AienosCapRef leased, fresh; |
| 147 | AienosCapMint m = {3, 1, 0x10, AIENOS_CAP_RIGHT_READ, 5, NONE, office(&a)}; |
| 148 | EQ(aienos_cap_mint(a.admin, &m, &leased), AIENOS_CAP_OK); |
| 149 | EQ(aienos_cap_advance_clock(a.admin, office(&a), 10), AIENOS_CAP_OK); |
| 150 | EQ(validate(&a, leased, 1, 0x10, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_EXPIRED); |
| 151 | EQ(root_mint(&a, 1, 0x10, AIENOS_CAP_RIGHT_READ, &fresh), AIENOS_CAP_OK); |
| 152 | EQ(aienos_cap_bump_epoch(a.admin, office(&a)), AIENOS_CAP_OK); |
| 153 | EQ(validate(&a, fresh, 1, 0x10, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_EPOCH); |
| 154 | /* The office itself was issued in the old epoch. */ |
| 155 | EQ(aienos_cap_bump_epoch(a.admin, office(&a)), AIENOS_CAP_ERR_EPOCH); |
| 156 | aienos_cap_stop(a.admin, a.view); |
| 157 | } |
| 158 | |
| 159 | static void child_lease_cannot_outlive_parent(void) { |
| 160 | Auth a = boot(); |
| 161 | AienosCapRef parent, out; |
| 162 | AienosCapMint m = {3, 1, 0x40, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE, 10, NONE, |
| 163 | office(&a)}; |
| 164 | EQ(aienos_cap_mint(a.admin, &m, &parent), AIENOS_CAP_OK); |
| 165 | AienosCapMint forever = {1, 2, 0x40, AIENOS_CAP_RIGHT_READ, 0, parent, parent}; |
| 166 | EQ(aienos_cap_mint(a.admin, &forever, &out), AIENOS_CAP_ERR_AMPLIFY); |
| 167 | AienosCapMint longer = {1, 2, 0x40, AIENOS_CAP_RIGHT_READ, 11, parent, parent}; |
| 168 | EQ(aienos_cap_mint(a.admin, &longer, &out), AIENOS_CAP_ERR_AMPLIFY); |
| 169 | AienosCapMint shorter = {1, 2, 0x40, AIENOS_CAP_RIGHT_READ, 10, parent, parent}; |
| 170 | EQ(aienos_cap_mint(a.admin, &shorter, &out), AIENOS_CAP_OK); |
| 171 | AienosCapMint overflow = {3, 1, 0x41, AIENOS_CAP_RIGHT_READ, 0, NONE, office(&a)}; |
| 172 | EQ(aienos_cap_advance_clock(a.admin, office(&a), 3), AIENOS_CAP_OK); |
| 173 | overflow.lease_ticks = UINT64_MAX; |
| 174 | EQ(aienos_cap_mint(a.admin, &overflow, &out), AIENOS_CAP_ERR_OVERFLOW); |
| 175 | aienos_cap_stop(a.admin, a.view); |
| 176 | } |
| 177 | |
| 178 | static void privileged_rights_cannot_be_delegated_and_office_stays(void) { |
| 179 | Auth a = boot(); |
| 180 | AienosCapRef revoker, out; |
| 181 | EQ(root_mint(&a, 1, AIENOS_CAP_RES_AUTHORITY, |
| 182 | AIENOS_CAP_RIGHT_REVOKE | AIENOS_CAP_RIGHT_DELEGATE, &out), |
| 183 | AIENOS_CAP_ERR_NOT_DELEGABLE); |
| 184 | EQ(root_mint(&a, 1, AIENOS_CAP_RES_AUTHORITY, AIENOS_CAP_RIGHT_REVOKE, &revoker), |
| 185 | AIENOS_CAP_OK); |
| 186 | EQ(child_mint(&a, revoker, 3, 2, AIENOS_CAP_RES_AUTHORITY, AIENOS_CAP_RIGHT_REVOKE, &out), |
| 187 | AIENOS_CAP_ERR_NOT_DELEGABLE); |
| 188 | EQ(aienos_cap_revoke(a.admin, revoker, office(&a)), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 189 | EQ(validate(&a, office(&a), 0, AIENOS_CAP_RES_AUTHORITY, AIENOS_CAP_RIGHT_MINT), |
| 190 | AIENOS_CAP_OK); |
| 191 | aienos_cap_stop(a.admin, a.view); |
| 192 | } |
| 193 | |
| 194 | static void promote_right_cannot_be_delegated(void) { |
| 195 | Auth a = boot(); |
| 196 | AienosCapRef promoter, out; |
| 197 | EQ(root_mint(&a, 4, 0x905, AIENOS_CAP_RIGHT_PROMOTE | AIENOS_CAP_RIGHT_DELEGATE, &out), |
| 198 | AIENOS_CAP_ERR_NOT_DELEGABLE); |
| 199 | EQ(root_mint(&a, 4, 0x905, AIENOS_CAP_RIGHT_PROMOTE, &promoter), AIENOS_CAP_OK); |
| 200 | EQ(child_mint(&a, promoter, 4, 5, 0x905, AIENOS_CAP_RIGHT_PROMOTE, &out), |
| 201 | AIENOS_CAP_ERR_NOT_DELEGABLE); |
| 202 | EQ(validate(&a, promoter, 4, 0x905, AIENOS_CAP_RIGHT_PROMOTE), AIENOS_CAP_OK); |
| 203 | EQ(validate(&a, promoter, 9, 0x905, AIENOS_CAP_RIGHT_PROMOTE), AIENOS_CAP_ERR_SUBJECT); |
| 204 | aienos_cap_stop(a.admin, a.view); |
| 205 | } |
| 206 | |
| 207 | static void generation_exhaustion_fails_closed(void) { |
| 208 | uint64_t next = 0; |
| 209 | EQ(aienos_cap_generation_advance(UINT64_MAX, &next), AIENOS_CAP_ERR_EXHAUSTED); |
| 210 | EQ(aienos_cap_generation_advance((uint64_t)UINT32_MAX, &next), AIENOS_CAP_OK); |
| 211 | EQ(next, (uint64_t)UINT32_MAX + 1); |
| 212 | Auth a = boot(); |
| 213 | AienosCapRef cap, replacement; |
| 214 | EQ(root_mint(&a, 1, 0x10, AIENOS_CAP_RIGHT_READ, &cap), AIENOS_CAP_OK); |
| 215 | EQ(aienos_cap_revoke(a.admin, office(&a), cap), AIENOS_CAP_OK); |
| 216 | EQ(aienos_cap_force_generation(a.admin, cap.cap_id, UINT64_MAX), AIENOS_CAP_OK); |
| 217 | EQ(aienos_cap_reclaim(a.admin, office(&a), cap.cap_id), AIENOS_CAP_ERR_EXHAUSTED); |
| 218 | EQ(validate(&a, cap, 1, 0x10, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_STALE_GEN); |
| 219 | EQ(root_mint(&a, 1, 0x11, AIENOS_CAP_RIGHT_READ, &replacement), AIENOS_CAP_OK); |
| 220 | CHECK(replacement.cap_id != cap.cap_id); |
| 221 | /* A live slot cannot be moved onto a chosen generation. */ |
| 222 | EQ(aienos_cap_force_generation(a.admin, replacement.cap_id, 99), AIENOS_CAP_ERR_STATE); |
| 223 | /* No start above an exhausted generation exists, so restart refuses |
| 224 | * rather than reuse one. */ |
| 225 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_ERR_EXHAUSTED); |
| 226 | aienos_cap_stop(a.admin, a.view); |
| 227 | } |
| 228 | |
| 229 | static void restart_invalidates_and_cognition_cannot_mint(void) { |
| 230 | Auth a = boot(); |
| 231 | AienosCapRef old, out; |
| 232 | EQ(root_mint(&a, 1, 0x12, AIENOS_CAP_RIGHT_READ, &old), AIENOS_CAP_OK); |
| 233 | AienosCapRef old_office = office(&a); |
| 234 | EQ(aienos_cap_kill(a.admin), AIENOS_CAP_OK); |
| 235 | EQ(root_mint(&a, 1, 0x13, AIENOS_CAP_RIGHT_READ, &out), AIENOS_CAP_ERR_IO); |
| 236 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_OK); |
| 237 | EQ(validate(&a, old, 1, 0x12, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_STALE_GEN); |
| 238 | CHECK(validate(&a, old_office, 0, AIENOS_CAP_RES_AUTHORITY, AIENOS_CAP_RIGHT_MINT) != |
| 239 | AIENOS_CAP_OK); |
| 240 | |
| 241 | uint64_t before = aienos_cap_clock(a.view); |
| 242 | AienosCapMint m = {1, 1, 1, AIENOS_CAP_RIGHT_READ, 0, NONE, office(&a)}; |
| 243 | uint8_t guess[AIENOS_CAP_TOKEN_LEN]; |
| 244 | memset(guess, 0, sizeof guess); |
| 245 | EQ(aienos_cap_cognition_mint(a.view, &m, guess), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 246 | EQ(aienos_cap_cognition_admin(a.view, 1, office(&a), old), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 247 | EQ(aienos_cap_clock(a.view), before); |
| 248 | EQ(aienos_cap_authorize(a.admin, guess), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 249 | aienos_cap_stop(a.admin, a.view); |
| 250 | } |
| 251 | |
| 252 | /* The old defect: a reclaim moved a slot to boot+1, and the restart started |
| 253 | * the new table at boot+1 too, so the pre-restart reference validated |
| 254 | * again once the same slot was minted for the same subject and resource. */ |
| 255 | static void reclaimed_reference_stays_dead_after_restart(void) { |
| 256 | Auth a = boot(); |
| 257 | AienosCapRef first, second, fresh; |
| 258 | EQ(root_mint(&a, 1, 0x40, AIENOS_CAP_RIGHT_READ, &first), AIENOS_CAP_OK); |
| 259 | EQ(aienos_cap_revoke(a.admin, office(&a), first), AIENOS_CAP_OK); |
| 260 | EQ(aienos_cap_reclaim(a.admin, office(&a), first.cap_id), AIENOS_CAP_OK); |
| 261 | EQ(root_mint(&a, 1, 0x40, AIENOS_CAP_RIGHT_READ, &second), AIENOS_CAP_OK); |
| 262 | EQ(second.cap_id, first.cap_id); |
| 263 | EQ(second.generation, first.generation + 1); |
| 264 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_OK); |
| 265 | EQ(root_mint(&a, 1, 0x40, AIENOS_CAP_RIGHT_READ, &fresh), AIENOS_CAP_OK); |
| 266 | EQ(fresh.cap_id, second.cap_id); |
| 267 | CHECK(fresh.generation > second.generation); |
| 268 | EQ(validate(&a, second, 1, 0x40, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_STALE_GEN); |
| 269 | EQ(validate(&a, first, 1, 0x40, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_STALE_GEN); |
| 270 | EQ(validate(&a, fresh, 1, 0x40, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 271 | aienos_cap_stop(a.admin, a.view); |
| 272 | } |
| 273 | |
| 274 | static void restarts_and_authorities_never_share_a_start(void) { |
| 275 | Auth a = boot(); |
| 276 | Auth b = boot(); |
| 277 | AienosCapRef a0 = office(&a); |
| 278 | AienosCapRef b0 = office(&b); |
| 279 | CHECK(b0.generation > a0.generation); |
| 280 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_OK); |
| 281 | AienosCapRef a1 = office(&a); |
| 282 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_OK); |
| 283 | AienosCapRef a2 = office(&a); |
| 284 | CHECK(a1.generation > b0.generation); |
| 285 | CHECK(a2.generation > a1.generation); |
| 286 | aienos_cap_stop(a.admin, a.view); |
| 287 | aienos_cap_stop(b.admin, b.view); |
| 288 | } |
| 289 | |
| 290 | static void generation_above_32_bits_is_kept(void) { |
| 291 | Auth a = boot(); |
| 292 | uint64_t high = ((uint64_t)1 << 32) + 5; |
| 293 | AienosCapRef cap; |
| 294 | /* Slot 1 is the first free slot after the office. */ |
| 295 | EQ(aienos_cap_force_generation(a.admin, 1, high), AIENOS_CAP_OK); |
| 296 | EQ(root_mint(&a, 1, 0x50, AIENOS_CAP_RIGHT_READ, &cap), AIENOS_CAP_OK); |
| 297 | EQ(cap.cap_id, 1u); |
| 298 | EQ(cap.generation, high); |
| 299 | EQ(validate(&a, cap, 1, 0x50, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 300 | AienosCapRef truncated = {cap.cap_id, (uint32_t)cap.generation}; |
| 301 | EQ(validate(&a, truncated, 1, 0x50, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_STALE_GEN); |
| 302 | AienosCapEntry e; |
| 303 | EQ(aienos_cap_inspect(a.view, cap, &e), AIENOS_CAP_OK); |
| 304 | EQ(e.generation, high); |
| 305 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_OK); |
| 306 | CHECK(office(&a).generation > high); |
| 307 | EQ(validate(&a, cap, 1, 0x50, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_STALE_GEN); |
| 308 | aienos_cap_stop(a.admin, a.view); |
| 309 | } |
| 310 | |
| 311 | static void ordinary_capability_cannot_administer_and_depth_stops(void) { |
| 312 | Auth a = boot(); |
| 313 | AienosCapRef world, cur, out; |
| 314 | EQ(root_mint(&a, 1, 0x10, AIENOS_CAP_RIGHT_READ, &world), AIENOS_CAP_OK); |
| 315 | EQ(aienos_cap_revoke(a.admin, world, world), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 316 | EQ(aienos_cap_advance_clock(a.admin, world, 1), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 317 | EQ(aienos_cap_bump_epoch(a.admin, world), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 318 | EQ(aienos_cap_reclaim(a.admin, world, world.cap_id), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 319 | EQ(root_mint(&a, 1, 0x20, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE, &cur), |
| 320 | AIENOS_CAP_OK); |
| 321 | for (int i = 0; i < 8; i++) { |
| 322 | EQ(child_mint(&a, cur, 3, 2, 0x20, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE, |
| 323 | &cur), |
| 324 | AIENOS_CAP_OK); |
| 325 | } |
| 326 | EQ(child_mint(&a, cur, 3, 2, 0x20, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE, &out), |
| 327 | AIENOS_CAP_ERR_CHAIN); |
| 328 | aienos_cap_stop(a.admin, a.view); |
| 329 | } |
| 330 | |
| 331 | static void table_fills_then_refuses(void) { |
| 332 | Auth a = boot(); |
| 333 | AienosCapRef out; |
| 334 | for (uint32_t i = 1; i < AIENOS_CAP_MAX; i++) |
| 335 | EQ(root_mint(&a, 1, i, AIENOS_CAP_RIGHT_READ, &out), AIENOS_CAP_OK); |
| 336 | EQ(root_mint(&a, 1, 0x999, AIENOS_CAP_RIGHT_READ, &out), AIENOS_CAP_ERR_FULL); |
| 337 | aienos_cap_stop(a.admin, a.view); |
| 338 | } |
| 339 | |
| 340 | /* ---- Observer: the authority announces its own transitions. ---- */ |
| 341 | |
| 342 | /* The observer gets AienosCapEntry and nothing else. Its layout is pinned |
| 343 | * here: 13 fields, 88 bytes, no room for the 32-byte office token. */ |
| 344 | _Static_assert(sizeof(AienosCapEntry) == 88, "AienosCapEntry layout changed"); |
| 345 | |
| 346 | #define REC_MAX 1024 |
| 347 | |
| 348 | typedef struct { |
| 349 | uint32_t op; |
| 350 | int rc; |
| 351 | int has_entry; |
| 352 | AienosCapEntry entry; |
| 353 | } Rec; |
| 354 | |
| 355 | typedef struct { |
| 356 | uint32_t n; |
| 357 | Rec r[REC_MAX]; |
| 358 | } Recorder; |
| 359 | |
| 360 | static void record(void *ctx, uint32_t op, const AienosCapEntry *entry, int result) { |
| 361 | Recorder *rec = ctx; |
| 362 | if (rec->n >= REC_MAX) return; |
| 363 | Rec *r = &rec->r[rec->n++]; |
| 364 | r->op = op; |
| 365 | r->rc = result; |
| 366 | r->has_entry = entry != NULL; |
| 367 | if (entry) r->entry = *entry; |
| 368 | else memset(&r->entry, 0, sizeof r->entry); |
| 369 | } |
| 370 | |
| 371 | /* The recorded entry is exactly what the view already shows for it. */ |
| 372 | static int matches_view(Auth *a, const Rec *r) { |
| 373 | AienosCapEntry now; |
| 374 | AienosCapRef ref = {r->entry.cap_id, r->entry.generation}; |
| 375 | if (aienos_cap_inspect(a->view, ref, &now) != AIENOS_CAP_OK) return 0; |
| 376 | return same_entry(&now, &r->entry); |
| 377 | } |
| 378 | |
| 379 | static void observer_sees_each_admin_operation_once(void) { |
| 380 | Auth a = boot(); |
| 381 | static Recorder rec; |
| 382 | memset(&rec, 0, sizeof rec); |
| 383 | EQ(aienos_cap_set_observer(NULL, record, &rec), AIENOS_CAP_ERR_STATE); |
| 384 | EQ(aienos_cap_set_observer(a.admin, record, &rec), AIENOS_CAP_OK); |
| 385 | |
| 386 | AienosCapRef cap; |
| 387 | EQ(root_mint(&a, 5, 0x77, AIENOS_CAP_RIGHT_READ, &cap), AIENOS_CAP_OK); |
| 388 | EQ(rec.n, 1u); |
| 389 | EQ(rec.r[0].op, AIENOS_CAP_OBS_MINT); |
| 390 | EQ(rec.r[0].rc, AIENOS_CAP_OK); |
| 391 | EQ(rec.r[0].has_entry, 1); |
| 392 | EQ(rec.r[0].entry.cap_id, cap.cap_id); |
| 393 | EQ(rec.r[0].entry.generation, cap.generation); |
| 394 | EQ(rec.r[0].entry.state, AIENOS_CAP_STATE_LIVE); |
| 395 | EQ(rec.r[0].entry.subject, 5u); |
| 396 | EQ(rec.r[0].entry.resource, 0x77ull); |
| 397 | EQ(rec.r[0].entry.minted_by_id, 0u); |
| 398 | CHECK(matches_view(&a, &rec.r[0])); |
| 399 | |
| 400 | EQ(aienos_cap_revoke(a.admin, office(&a), cap), AIENOS_CAP_OK); |
| 401 | EQ(rec.n, 2u); |
| 402 | EQ(rec.r[1].op, AIENOS_CAP_OBS_REVOKE); |
| 403 | EQ(rec.r[1].rc, AIENOS_CAP_OK); |
| 404 | EQ(rec.r[1].entry.cap_id, cap.cap_id); |
| 405 | EQ(rec.r[1].entry.state, AIENOS_CAP_STATE_REVOKED); |
| 406 | CHECK(matches_view(&a, &rec.r[1])); |
| 407 | |
| 408 | EQ(aienos_cap_reclaim(a.admin, office(&a), cap.cap_id), AIENOS_CAP_OK); |
| 409 | EQ(rec.n, 3u); |
| 410 | EQ(rec.r[2].op, AIENOS_CAP_OBS_RECLAIM); |
| 411 | EQ(rec.r[2].rc, AIENOS_CAP_OK); |
| 412 | EQ(rec.r[2].entry.cap_id, cap.cap_id); |
| 413 | EQ(rec.r[2].entry.state, AIENOS_CAP_STATE_FREE); |
| 414 | EQ(rec.r[2].entry.generation, cap.generation + 1); |
| 415 | |
| 416 | EQ(aienos_cap_advance_clock(a.admin, office(&a), 4), AIENOS_CAP_OK); |
| 417 | EQ(rec.n, 4u); |
| 418 | EQ(rec.r[3].op, AIENOS_CAP_OBS_CLOCK); |
| 419 | EQ(rec.r[3].rc, AIENOS_CAP_OK); |
| 420 | EQ(rec.r[3].entry.cap_id, 0u); |
| 421 | CHECK(matches_view(&a, &rec.r[3])); |
| 422 | |
| 423 | AienosCapRef before_epoch = office(&a); |
| 424 | EQ(aienos_cap_bump_epoch(a.admin, office(&a)), AIENOS_CAP_OK); |
| 425 | EQ(rec.n, 5u); |
| 426 | EQ(rec.r[4].op, AIENOS_CAP_OBS_EPOCH); |
| 427 | EQ(rec.r[4].rc, AIENOS_CAP_OK); |
| 428 | EQ(rec.r[4].entry.cap_id, before_epoch.cap_id); |
| 429 | EQ(rec.r[4].entry.generation, before_epoch.generation); |
| 430 | |
| 431 | EQ(aienos_cap_kill(a.admin), AIENOS_CAP_OK); |
| 432 | EQ(rec.n, 6u); |
| 433 | EQ(rec.r[5].op, AIENOS_CAP_OBS_KILL); |
| 434 | EQ(rec.r[5].rc, AIENOS_CAP_OK); |
| 435 | EQ(rec.r[5].has_entry, 0); |
| 436 | |
| 437 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_OK); |
| 438 | EQ(rec.n, 7u); |
| 439 | EQ(rec.r[6].op, AIENOS_CAP_OBS_RESTART); |
| 440 | EQ(rec.r[6].rc, AIENOS_CAP_OK); |
| 441 | EQ(rec.r[6].entry.cap_id, 0u); |
| 442 | EQ(rec.r[6].entry.generation, office(&a).generation); |
| 443 | CHECK(rec.r[6].entry.generation > before_epoch.generation); |
| 444 | CHECK(matches_view(&a, &rec.r[6])); |
| 445 | |
| 446 | /* The observer stays set across the restart. */ |
| 447 | EQ(root_mint(&a, 5, 0x78, AIENOS_CAP_RIGHT_READ, &cap), AIENOS_CAP_OK); |
| 448 | EQ(rec.n, 8u); |
| 449 | EQ(rec.r[7].op, AIENOS_CAP_OBS_MINT); |
| 450 | aienos_cap_stop(a.admin, a.view); |
| 451 | } |
| 452 | |
| 453 | static void observer_sees_cascade_ancestor_first(void) { |
| 454 | Auth a = boot(); |
| 455 | static Recorder rec; |
| 456 | memset(&rec, 0, sizeof rec); |
| 457 | uint32_t dr = AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE; |
| 458 | AienosCapRef top, x, y, mid, low, other; |
| 459 | EQ(root_mint(&a, 1, 0x60, dr, &top), AIENOS_CAP_OK); |
| 460 | EQ(root_mint(&a, 1, 0x61, AIENOS_CAP_RIGHT_READ, &x), AIENOS_CAP_OK); |
| 461 | EQ(root_mint(&a, 1, 0x62, AIENOS_CAP_RIGHT_READ, &y), AIENOS_CAP_OK); |
| 462 | EQ(child_mint(&a, top, 1, 2, 0x60, dr, &mid), AIENOS_CAP_OK); |
| 463 | /* Free x's slot so the grandchild lands below its parent's index: the |
| 464 | * cascade then needs a second pass to reach it. */ |
| 465 | EQ(aienos_cap_revoke(a.admin, office(&a), x), AIENOS_CAP_OK); |
| 466 | EQ(aienos_cap_reclaim(a.admin, office(&a), x.cap_id), AIENOS_CAP_OK); |
| 467 | EQ(child_mint(&a, mid, 2, 3, 0x60, AIENOS_CAP_RIGHT_READ, &low), AIENOS_CAP_OK); |
| 468 | CHECK(low.cap_id < mid.cap_id); |
| 469 | EQ(root_mint(&a, 1, 0x63, AIENOS_CAP_RIGHT_READ, &other), AIENOS_CAP_OK); |
| 470 | |
| 471 | EQ(aienos_cap_set_observer(a.admin, record, &rec), AIENOS_CAP_OK); |
| 472 | EQ(aienos_cap_revoke(a.admin, office(&a), top), AIENOS_CAP_OK); |
| 473 | EQ(rec.n, 3u); |
| 474 | for (uint32_t i = 0; i < 3; i++) { |
| 475 | EQ(rec.r[i].op, AIENOS_CAP_OBS_REVOKE); |
| 476 | EQ(rec.r[i].rc, AIENOS_CAP_OK); |
| 477 | EQ(rec.r[i].has_entry, 1); |
| 478 | EQ(rec.r[i].entry.state, AIENOS_CAP_STATE_REVOKED); |
| 479 | CHECK(matches_view(&a, &rec.r[i])); |
| 480 | } |
| 481 | EQ(rec.r[0].entry.cap_id, top.cap_id); |
| 482 | EQ(rec.r[1].entry.cap_id, mid.cap_id); |
| 483 | EQ(rec.r[1].entry.parent_id, top.cap_id); |
| 484 | EQ(rec.r[2].entry.cap_id, low.cap_id); |
| 485 | EQ(rec.r[2].entry.parent_id, mid.cap_id); |
| 486 | /* Unrelated entries are not announced. */ |
| 487 | for (uint32_t i = 0; i < rec.n; i++) { |
| 488 | CHECK(rec.r[i].entry.cap_id != other.cap_id); |
| 489 | CHECK(rec.r[i].entry.cap_id != y.cap_id); |
| 490 | } |
| 491 | aienos_cap_stop(a.admin, a.view); |
| 492 | } |
| 493 | |
| 494 | static void observer_sees_refusals_with_their_code(void) { |
| 495 | Auth a = boot(); |
| 496 | static Recorder rec; |
| 497 | memset(&rec, 0, sizeof rec); |
| 498 | AienosCapRef parent, world, out; |
| 499 | EQ(root_mint(&a, 1, 0x20, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE, &parent), |
| 500 | AIENOS_CAP_OK); |
| 501 | EQ(root_mint(&a, 1, 0x21, AIENOS_CAP_RIGHT_READ, &world), AIENOS_CAP_OK); |
| 502 | EQ(aienos_cap_set_observer(a.admin, record, &rec), AIENOS_CAP_OK); |
| 503 | |
| 504 | EQ(child_mint(&a, parent, 1, 2, 0x20, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE, &out), |
| 505 | AIENOS_CAP_ERR_AMPLIFY); |
| 506 | EQ(rec.n, 1u); |
| 507 | EQ(rec.r[0].op, AIENOS_CAP_OBS_MINT); |
| 508 | EQ(rec.r[0].rc, AIENOS_CAP_ERR_AMPLIFY); |
| 509 | EQ(rec.r[0].has_entry, 0); |
| 510 | |
| 511 | EQ(aienos_cap_revoke(a.admin, world, parent), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 512 | EQ(aienos_cap_reclaim(a.admin, office(&a), parent.cap_id), AIENOS_CAP_ERR_STATE); |
| 513 | EQ(aienos_cap_advance_clock(a.admin, world, 1), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 514 | EQ(aienos_cap_bump_epoch(a.admin, world), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 515 | EQ(rec.n, 5u); |
| 516 | EQ(rec.r[1].op, AIENOS_CAP_OBS_REVOKE); |
| 517 | EQ(rec.r[1].rc, AIENOS_CAP_ERR_UNAUTHORIZED); |
| 518 | EQ(rec.r[2].op, AIENOS_CAP_OBS_RECLAIM); |
| 519 | EQ(rec.r[2].rc, AIENOS_CAP_ERR_STATE); |
| 520 | EQ(rec.r[3].op, AIENOS_CAP_OBS_CLOCK); |
| 521 | EQ(rec.r[3].rc, AIENOS_CAP_ERR_UNAUTHORIZED); |
| 522 | EQ(rec.r[4].op, AIENOS_CAP_OBS_EPOCH); |
| 523 | EQ(rec.r[4].rc, AIENOS_CAP_ERR_UNAUTHORIZED); |
| 524 | for (uint32_t i = 1; i < 5; i++) EQ(rec.r[i].has_entry, 0); |
| 525 | |
| 526 | /* A dead writer refuses a mint; the refusal is announced too. */ |
| 527 | EQ(aienos_cap_kill(a.admin), AIENOS_CAP_OK); |
| 528 | EQ(root_mint(&a, 1, 0x22, AIENOS_CAP_RIGHT_READ, &out), AIENOS_CAP_ERR_IO); |
| 529 | EQ(rec.n, 7u); |
| 530 | EQ(rec.r[6].op, AIENOS_CAP_OBS_MINT); |
| 531 | EQ(rec.r[6].rc, AIENOS_CAP_ERR_IO); |
| 532 | aienos_cap_stop(a.admin, a.view); |
| 533 | } |
| 534 | |
| 535 | static void observer_is_silent_for_the_view_and_after_clear(void) { |
| 536 | Auth a = boot(); |
| 537 | static Recorder rec; |
| 538 | memset(&rec, 0, sizeof rec); |
| 539 | AienosCapRef cap, out; |
| 540 | AienosCapEntry e; |
| 541 | EQ(root_mint(&a, 1, 0x30, AIENOS_CAP_RIGHT_READ, &cap), AIENOS_CAP_OK); |
| 542 | EQ(aienos_cap_set_observer(a.admin, record, &rec), AIENOS_CAP_OK); |
| 543 | |
| 544 | /* Successful and denied validates, inspect, clock, and cognition's |
| 545 | * refused attempts: none of them is announced. */ |
| 546 | EQ(validate(&a, cap, 1, 0x30, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 547 | EQ(aienos_cap_validate(a.view, cap, 1, 0x30, AIENOS_CAP_RIGHT_READ, &e), AIENOS_CAP_OK); |
| 548 | EQ(validate(&a, cap, 9, 0x30, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_SUBJECT); |
| 549 | EQ(validate(&a, (AienosCapRef){300, 1}, 1, 0x30, AIENOS_CAP_RIGHT_READ), |
| 550 | AIENOS_CAP_ERR_BOUNDS); |
| 551 | EQ(aienos_cap_inspect(a.view, cap, &e), AIENOS_CAP_OK); |
| 552 | (void)aienos_cap_clock(a.view); |
| 553 | uint8_t guess[AIENOS_CAP_TOKEN_LEN] = {0}; |
| 554 | AienosCapMint m = {1, 1, 1, AIENOS_CAP_RIGHT_READ, 0, NONE, office(&a)}; |
| 555 | EQ(aienos_cap_cognition_mint(a.view, &m, guess), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 556 | EQ(aienos_cap_cognition_admin(a.view, 1, office(&a), cap), AIENOS_CAP_ERR_UNAUTHORIZED); |
| 557 | EQ(rec.n, 0u); |
| 558 | |
| 559 | EQ(aienos_cap_set_observer(a.admin, NULL, &rec), AIENOS_CAP_OK); |
| 560 | EQ(root_mint(&a, 1, 0x31, AIENOS_CAP_RIGHT_READ, &out), AIENOS_CAP_OK); |
| 561 | EQ(aienos_cap_revoke(a.admin, office(&a), out), AIENOS_CAP_OK); |
| 562 | EQ(aienos_cap_reclaim(a.admin, office(&a), out.cap_id), AIENOS_CAP_OK); |
| 563 | EQ(aienos_cap_advance_clock(a.admin, office(&a), 1), AIENOS_CAP_OK); |
| 564 | EQ(aienos_cap_bump_epoch(a.admin, office(&a)), AIENOS_CAP_OK); |
| 565 | EQ(aienos_cap_kill(a.admin), AIENOS_CAP_OK); |
| 566 | EQ(aienos_cap_restart(a.admin), AIENOS_CAP_OK); |
| 567 | EQ(rec.n, 0u); |
| 568 | aienos_cap_stop(a.admin, a.view); |
| 569 | } |
| 570 | |
| 571 | /* Seeded random operations against two authorities, one watched and one |
| 572 | * not. The watched one keeps a shadow table built only from the calls it |
| 573 | * receives. After every step: both return the same code and slot, and every |
| 574 | * shadow entry equals what the view shows. This is what ARGUS does with |
| 575 | * the calls, so it is checked the way ARGUS would use them. */ |
| 576 | typedef struct { |
| 577 | AienosCapEntry e[AIENOS_CAP_MAX]; |
| 578 | int known[AIENOS_CAP_MAX]; |
| 579 | int bad; |
| 580 | } Shadow; |
| 581 | |
| 582 | static void shadow_apply(void *ctx, uint32_t op, const AienosCapEntry *entry, int result) { |
| 583 | Shadow *sh = ctx; |
| 584 | if (op == AIENOS_CAP_OBS_RESTART && result == AIENOS_CAP_OK) { |
| 585 | memset(sh->known, 0, sizeof sh->known); |
| 586 | } |
| 587 | if (!entry) return; |
| 588 | if (result != AIENOS_CAP_OK || entry->cap_id >= AIENOS_CAP_MAX) { |
| 589 | sh->bad = 1; |
| 590 | return; |
| 591 | } |
| 592 | sh->e[entry->cap_id] = *entry; |
| 593 | sh->known[entry->cap_id] = 1; |
| 594 | } |
| 595 | |
| 596 | static uint64_t rng_next(uint64_t *x) { |
| 597 | *x ^= *x << 13; |
| 598 | *x ^= *x >> 7; |
| 599 | *x ^= *x << 17; |
| 600 | return *x; |
| 601 | } |
| 602 | |
| 603 | static int random_step(Auth *a, uint64_t *rng, AienosCapRef *pool, uint32_t *pool_n, |
| 604 | uint32_t *slot_out) { |
| 605 | uint32_t kind = (uint32_t)(rng_next(rng) % 100); |
| 606 | AienosCapRef pick = *pool_n ? pool[rng_next(rng) % *pool_n] : office(a); |
| 607 | AienosCapRef out = {AIENOS_CAP_PARENT_NONE, 0}; |
| 608 | int rc; |
| 609 | *slot_out = AIENOS_CAP_PARENT_NONE; |
| 610 | uint32_t rights = (uint32_t)(rng_next(rng) % 16) | AIENOS_CAP_RIGHT_READ; |
| 611 | if (kind < 30) { |
| 612 | AienosCapMint m = {3, (uint32_t)(rng_next(rng) % 4), rng_next(rng) % 4, rights, |
| 613 | rng_next(rng) % 3 ? 0 : rng_next(rng) % 20, NONE, office(a)}; |
| 614 | rc = aienos_cap_mint(a->admin, &m, &out); |
| 615 | } else if (kind < 55) { |
| 616 | AienosCapEntry pe; |
| 617 | uint64_t res = 0; |
| 618 | if (aienos_cap_inspect(a->view, pick, &pe) == AIENOS_CAP_OK) res = pe.resource; |
| 619 | AienosCapMint m = {1, (uint32_t)(rng_next(rng) % 4), res, rights & ~AIENOS_CAP_RIGHT_WRITE, |
| 620 | rng_next(rng) % 20, pick, pick}; |
| 621 | rc = aienos_cap_mint(a->admin, &m, &out); |
| 622 | } else if (kind < 72) { |
| 623 | rc = aienos_cap_revoke(a->admin, office(a), pick); |
| 624 | } else if (kind < 85) { |
| 625 | rc = aienos_cap_reclaim(a->admin, office(a), pick.cap_id); |
| 626 | } else if (kind < 93) { |
| 627 | rc = aienos_cap_advance_clock(a->admin, rng_next(rng) % 8 ? office(a) : pick, |
| 628 | rng_next(rng) % 5); |
| 629 | } else if (kind < 96) { |
| 630 | rc = aienos_cap_bump_epoch(a->admin, office(a)); |
| 631 | } else if (kind < 98) { |
| 632 | rc = aienos_cap_restart(a->admin); |
| 633 | } else { |
| 634 | rc = aienos_cap_validate(a->view, pick, 1, 1, AIENOS_CAP_RIGHT_READ, NULL); |
| 635 | } |
| 636 | if (rc == AIENOS_CAP_OK && out.cap_id != AIENOS_CAP_PARENT_NONE) { |
| 637 | *slot_out = out.cap_id; |
| 638 | if (*pool_n < 512) pool[(*pool_n)++] = out; |
| 639 | else pool[rng_next(rng) % 512] = out; |
| 640 | } |
| 641 | return rc; |
| 642 | } |
| 643 | |
| 644 | static int shadow_matches(Auth *a, const Shadow *sh) { |
| 645 | if (sh->bad) return 0; |
| 646 | for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) { |
| 647 | if (!sh->known[i]) continue; |
| 648 | AienosCapEntry now; |
| 649 | AienosCapRef ref = {i, sh->e[i].generation}; |
| 650 | int rc = aienos_cap_inspect(a->view, ref, &now); |
| 651 | if (sh->e[i].state == AIENOS_CAP_STATE_FREE) { |
| 652 | if (rc != AIENOS_CAP_ERR_STATE) return 0; |
| 653 | continue; |
| 654 | } |
| 655 | /* The epoch and clock move without touching entries; the entry |
| 656 | * itself must be exactly what the shadow holds. */ |
| 657 | if (rc != AIENOS_CAP_OK || !same_entry(&now, &sh->e[i])) return 0; |
| 658 | } |
| 659 | return 1; |
| 660 | } |
| 661 | |
| 662 | static void observer_shadow_matches_table_under_random_operations(uint64_t base_seed, |
| 663 | uint32_t seeds, |
| 664 | uint32_t steps) { |
| 665 | static Shadow shadow; |
| 666 | static AienosCapRef pool_w[512], pool_u[512]; |
| 667 | for (uint32_t s = 0; s < seeds; s++) { |
| 668 | uint64_t seed = base_seed + s * 0x9e3779b97f4a7c15ull; |
| 669 | if (seed == 0) seed = 1; |
| 670 | uint64_t rw = seed, ru = seed; |
| 671 | uint32_t nw = 0, nu = 0; |
| 672 | Auth w = boot(); |
| 673 | Auth u = boot(); |
| 674 | memset(&shadow, 0, sizeof shadow); |
| 675 | AienosCapEntry office_entry; |
| 676 | aienos_cap_inspect(w.view, office(&w), &office_entry); |
| 677 | shadow.e[0] = office_entry; |
| 678 | shadow.known[0] = 1; |
| 679 | EQ(aienos_cap_set_observer(w.admin, shadow_apply, &shadow), AIENOS_CAP_OK); |
| 680 | uint32_t same = 1, shadow_ok = 1; |
| 681 | for (uint32_t i = 0; i < steps; i++) { |
| 682 | uint32_t sw, su; |
| 683 | int rcw = random_step(&w, &rw, pool_w, &nw, &sw); |
| 684 | int rcu = random_step(&u, &ru, pool_u, &nu, &su); |
| 685 | if (rcw != rcu || sw != su) same = 0; |
| 686 | if (!shadow_matches(&w, &shadow)) shadow_ok = 0; |
| 687 | if (!same || !shadow_ok) { |
| 688 | fprintf(stderr, "seed %llu step %u: rc %d/%d slot %u/%u shadow %u\n", |
| 689 | (unsigned long long)seed, i, rcw, rcu, sw, su, shadow_ok); |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | CHECK(same); |
| 694 | CHECK(shadow_ok); |
| 695 | aienos_cap_stop(w.admin, w.view); |
| 696 | aienos_cap_stop(u.admin, u.view); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | |
| 701 | /* Rights are a superset check: a reference holding READ must not pass a |
| 702 | * request for READ|WRITE. Kills mutant validate_partial_rights (any-overlap |
| 703 | * instead of all-of). */ |
| 704 | static void validate_needs_every_requested_right(void) { |
| 705 | Auth a = boot(); |
| 706 | AienosCapRef cap; |
| 707 | EQ(root_mint(&a, 1, 0x60, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_EFFECT, &cap), |
| 708 | AIENOS_CAP_OK); |
| 709 | EQ(validate(&a, cap, 1, 0x60, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 710 | EQ(validate(&a, cap, 1, 0x60, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_EFFECT), AIENOS_CAP_OK); |
| 711 | EQ(validate(&a, cap, 1, 0x60, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE), |
| 712 | AIENOS_CAP_ERR_RIGHTS); |
| 713 | EQ(validate(&a, cap, 1, 0x60, |
| 714 | AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_EFFECT | AIENOS_CAP_RIGHT_DELEGATE), |
| 715 | AIENOS_CAP_ERR_RIGHTS); |
| 716 | aienos_cap_stop(a.admin, a.view); |
| 717 | } |
| 718 | |
| 719 | /* A lease of n ticks is good for clock values below start+n and dead at |
| 720 | * exactly start+n. Kills mutant lease_expiry_off_by_one (> instead of >=). */ |
| 721 | static void lease_ends_exactly_at_expiry_tick(void) { |
| 722 | Auth a = boot(); |
| 723 | AienosCapRef leased; |
| 724 | AienosCapMint m = {3, 1, 0x70, AIENOS_CAP_RIGHT_READ, 5, NONE, office(&a)}; |
| 725 | EQ(aienos_cap_mint(a.admin, &m, &leased), AIENOS_CAP_OK); |
| 726 | EQ(aienos_cap_advance_clock(a.admin, office(&a), 4), AIENOS_CAP_OK); |
| 727 | EQ(validate(&a, leased, 1, 0x70, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 728 | EQ(aienos_cap_advance_clock(a.admin, office(&a), 1), AIENOS_CAP_OK); |
| 729 | EQ(aienos_cap_clock(a.view), 5u); |
| 730 | EQ(validate(&a, leased, 1, 0x70, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_EXPIRED); |
| 731 | aienos_cap_stop(a.admin, a.view); |
| 732 | } |
| 733 | |
| 734 | /* Fault injection for the chain walk in validate. |
| 735 | * |
| 736 | * Through the public calls a live child of a non-live parent cannot exist: |
| 737 | * revoke cascades to every descendant, and reclaim and force_generation |
| 738 | * only touch non-live slots. So the "parent must be LIVE" step in validate |
| 739 | * is defense in depth, and only a corrupted table can reach it. This test |
| 740 | * corrupts the table on purpose: it flips the parent's state with no |
| 741 | * cascade and checks validate still refuses the child with CHAIN. |
| 742 | * |
| 743 | * It reaches the table through a mirror of the private layout in |
| 744 | * aienos_capability.c (CapShared/CapState). The mirror is checked against |
| 745 | * the public view before anything is written; any layout drift fails this |
| 746 | * test loudly instead of poking the wrong bytes. Kills mutant |
| 747 | * validate_parent_live. */ |
| 748 | typedef struct { |
| 749 | uint64_t boot_gen; |
| 750 | uint64_t epoch; |
| 751 | uint64_t clock; |
| 752 | AienosCapEntry entries[AIENOS_CAP_MAX]; |
| 753 | bool delivered[AIENOS_CAP_MAX]; |
| 754 | bool writer_alive; |
| 755 | } MirrorCapState; |
| 756 | |
| 757 | typedef struct { |
| 758 | pthread_mutex_t state_lock; |
| 759 | MirrorCapState state; |
| 760 | } MirrorCapShared; |
| 761 | |
| 762 | static void validate_refuses_child_of_non_live_parent_in_corrupt_table(void) { |
| 763 | Auth a = boot(); |
| 764 | AienosCapRef parent, child; |
| 765 | AienosCapEntry pe, ce, oe; |
| 766 | EQ(root_mint(&a, 1, 0x80, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE, &parent), |
| 767 | AIENOS_CAP_OK); |
| 768 | EQ(child_mint(&a, parent, 1, 2, 0x80, AIENOS_CAP_RIGHT_READ, &child), AIENOS_CAP_OK); |
| 769 | EQ(validate(&a, child, 2, 0x80, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 770 | EQ(aienos_cap_inspect(a.view, parent, &pe), AIENOS_CAP_OK); |
| 771 | EQ(aienos_cap_inspect(a.view, child, &ce), AIENOS_CAP_OK); |
| 772 | EQ(aienos_cap_inspect(a.view, office(&a), &oe), AIENOS_CAP_OK); |
| 773 | |
| 774 | MirrorCapShared *sh = *(MirrorCapShared *const *)a.admin; |
| 775 | int layout_ok = sh != NULL && sh->state.epoch == 1 && sh->state.clock == 0 && |
| 776 | sh->state.writer_alive && sh->state.delivered[child.cap_id] && |
| 777 | same_entry(&sh->state.entries[0], &oe) && |
| 778 | same_entry(&sh->state.entries[parent.cap_id], &pe) && |
| 779 | same_entry(&sh->state.entries[child.cap_id], &ce); |
| 780 | CHECK(layout_ok); |
| 781 | if (layout_ok) { |
| 782 | AienosCapEntry *p = &sh->state.entries[parent.cap_id]; |
| 783 | p->state = AIENOS_CAP_STATE_REVOKED; /* no cascade: child stays LIVE */ |
| 784 | EQ(aienos_cap_inspect(a.view, child, &ce), AIENOS_CAP_OK); |
| 785 | EQ(ce.state, AIENOS_CAP_STATE_LIVE); |
| 786 | EQ(validate(&a, child, 2, 0x80, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_CHAIN); |
| 787 | p->state = AIENOS_CAP_STATE_FREE; |
| 788 | EQ(validate(&a, child, 2, 0x80, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_ERR_CHAIN); |
| 789 | p->state = AIENOS_CAP_STATE_LIVE; |
| 790 | EQ(validate(&a, child, 2, 0x80, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 791 | } |
| 792 | aienos_cap_stop(a.admin, a.view); |
| 793 | } |
| 794 | |
| 795 | /* #266: a privileged right is an authority-office right. A capability that |
| 796 | * carries privileged rights on another resource (the operator CONTROL |
| 797 | * resource) is honored by validate for that resource, but never accepted by |
| 798 | * the authority operations. */ |
| 799 | static void control_resource_capability_cannot_operate_the_authority(void) { |
| 800 | Auth a = boot(); |
| 801 | const uint64_t control = 0xC0DEull; |
| 802 | AienosCapRef halt, victim, ok; |
| 803 | const uint32_t all_priv = AIENOS_CAP_RIGHT_PRIVILEGED & ~AIENOS_CAP_RIGHT_PROMOTE; |
| 804 | EQ(root_mint(&a, 1, control, AIENOS_CAP_RIGHT_EPOCH | AIENOS_CAP_RIGHT_READ, &halt), |
| 805 | AIENOS_CAP_OK); |
| 806 | EQ(root_mint(&a, 1, control, all_priv, &ok), AIENOS_CAP_OK); |
| 807 | EQ(root_mint(&a, 2, 0x77, AIENOS_CAP_RIGHT_READ, &victim), AIENOS_CAP_OK); |
| 808 | /* Negative: no authority operation accepts a control-resource capability. */ |
| 809 | EQ(aienos_cap_bump_epoch(a.admin, halt), AIENOS_CAP_ERR_RESOURCE); |
| 810 | EQ(aienos_cap_bump_epoch(a.admin, ok), AIENOS_CAP_ERR_RESOURCE); |
| 811 | EQ(aienos_cap_advance_clock(a.admin, ok, 1), AIENOS_CAP_ERR_RESOURCE); |
| 812 | EQ(aienos_cap_revoke(a.admin, ok, victim), AIENOS_CAP_ERR_RESOURCE); |
| 813 | EQ(aienos_cap_reclaim(a.admin, ok, victim.cap_id), AIENOS_CAP_ERR_RESOURCE); |
| 814 | AienosCapMint m = {3, 2, 0x78, AIENOS_CAP_RIGHT_READ, 0, NONE, ok}; |
| 815 | AienosCapRef out; |
| 816 | EQ(aienos_cap_mint(a.admin, &m, &out), AIENOS_CAP_ERR_RESOURCE); |
| 817 | /* Nothing moved: victim still live, epoch unchanged (office still works). */ |
| 818 | EQ(validate(&a, victim, 2, 0x77, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 819 | /* Regression: operator stop/status/resume checks on CONTROL still pass. */ |
| 820 | EQ(validate(&a, halt, 1, control, AIENOS_CAP_RIGHT_EPOCH), AIENOS_CAP_OK); |
| 821 | EQ(validate(&a, halt, 1, control, AIENOS_CAP_RIGHT_READ), AIENOS_CAP_OK); |
| 822 | EQ(validate(&a, ok, 1, control, AIENOS_CAP_RIGHT_CLOCK), AIENOS_CAP_OK); |
| 823 | /* Regression: the real office still performs every operation. */ |
| 824 | EQ(aienos_cap_advance_clock(a.admin, office(&a), 1), AIENOS_CAP_OK); |
| 825 | EQ(aienos_cap_revoke(a.admin, office(&a), victim), AIENOS_CAP_OK); |
| 826 | EQ(aienos_cap_reclaim(a.admin, office(&a), victim.cap_id), AIENOS_CAP_OK); |
| 827 | /* Authority-resource privileged delegate-free caps still work too. */ |
| 828 | AienosCapRef reclaimer; |
| 829 | EQ(root_mint(&a, 1, AIENOS_CAP_RES_AUTHORITY, AIENOS_CAP_RIGHT_CLOCK, &reclaimer), |
| 830 | AIENOS_CAP_OK); |
| 831 | EQ(aienos_cap_advance_clock(a.admin, reclaimer, 1), AIENOS_CAP_OK); |
| 832 | EQ(aienos_cap_bump_epoch(a.admin, office(&a)), AIENOS_CAP_OK); |
| 833 | aienos_cap_stop(a.admin, a.view); |
| 834 | } |
| 835 | |
| 836 | int main(int argc, char **argv) { |
| 837 | uint64_t base_seed = argc > 1 ? strtoull(argv[1], NULL, 0) : 0x5eedull; |
| 838 | control_resource_capability_cannot_operate_the_authority(); |
| 839 | high_half_resource_is_kept(); |
| 840 | forged_stale_subject_resource_and_rights_fail_closed(); |
| 841 | amplification_lease_epoch_and_revoked_ancestor_fail(); |
| 842 | child_lease_cannot_outlive_parent(); |
| 843 | privileged_rights_cannot_be_delegated_and_office_stays(); |
| 844 | promote_right_cannot_be_delegated(); |
| 845 | generation_exhaustion_fails_closed(); |
| 846 | restart_invalidates_and_cognition_cannot_mint(); |
| 847 | reclaimed_reference_stays_dead_after_restart(); |
| 848 | restarts_and_authorities_never_share_a_start(); |
| 849 | generation_above_32_bits_is_kept(); |
| 850 | ordinary_capability_cannot_administer_and_depth_stops(); |
| 851 | table_fills_then_refuses(); |
| 852 | validate_needs_every_requested_right(); |
| 853 | lease_ends_exactly_at_expiry_tick(); |
| 854 | validate_refuses_child_of_non_live_parent_in_corrupt_table(); |
| 855 | observer_sees_each_admin_operation_once(); |
| 856 | observer_sees_cascade_ancestor_first(); |
| 857 | observer_sees_refusals_with_their_code(); |
| 858 | observer_is_silent_for_the_view_and_after_clear(); |
| 859 | observer_shadow_matches_table_under_random_operations(base_seed, 20, 5000); |
| 860 | printf("capability: seed %llu\n", (unsigned long long)base_seed); |
| 861 | printf("capability: %d checks, %d failures\n", checks, failures); |
| 862 | return failures == 0 ? 0 : 1; |
| 863 | } |