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 / store / store_v1.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 | * store_v1.c -- System Store v1 format primitives, C twin of |
| 19 | * crates/aienos-kernel/src/store/v1.rs. See store_v1.h. |
| 20 | * |
| 21 | * Lines tagged GUARD:<name> are refusal checks that `make mutants` deletes |
| 22 | * one at a time; the test suite must fail for every such mutant. |
| 23 | */ |
| 24 | #include "store_v1.h" |
| 25 | |
| 26 | static const uint8_t OBJECT_DOMAIN[23] = "AIENOS-STORE-OBJECT-V1"; /* + NUL */ |
| 27 | static const uint8_t CATALOG_MAGIC[8] = {'A', 'I', 'E', 'N', 'C', 'A', 'T', '1'}; |
| 28 | static const uint8_t COMMIT_MAGIC[8] = {'A', 'I', 'E', 'N', 'C', 'M', 'T', '1'}; |
| 29 | static const uint8_t SUPERBLOCK_MAGIC[8] = {'A', 'I', 'E', 'N', 'S', 'T', 'R', '1'}; |
| 30 | |
| 31 | const char *sv1_strerror(int e) |
| 32 | { |
| 33 | switch (e) { |
| 34 | case SV1_OK: return "Ok"; |
| 35 | case SV1_E_INVALID_LENGTH: return "InvalidLength"; |
| 36 | case SV1_E_INVALID_OBJECT: return "InvalidObject"; |
| 37 | case SV1_E_OBJECT_TOO_LARGE: return "ObjectTooLarge"; |
| 38 | case SV1_E_LENGTH_OVERFLOW: return "LengthOverflow"; |
| 39 | case SV1_E_MALFORMED_DESCRIPTOR: return "MalformedDescriptor"; |
| 40 | case SV1_E_OUT_OF_BOUNDS: return "OutOfBounds"; |
| 41 | case SV1_E_OVERLAP: return "Overlap"; |
| 42 | case SV1_E_CATALOG_TOO_LARGE: return "CatalogTooLarge"; |
| 43 | case SV1_E_CATALOG_ORDER: return "CatalogOrder"; |
| 44 | case SV1_E_DUPLICATE_OBJECT: return "DuplicateObject"; |
| 45 | case SV1_E_BAD_CATALOG_MAGIC: return "BadCatalogMagic"; |
| 46 | case SV1_E_UNSUPPORTED_VERSION: return "UnsupportedVersion"; |
| 47 | case SV1_E_MALFORMED_COMMIT: return "MalformedCommitRecord"; |
| 48 | case SV1_E_MALFORMED_SUPERBLOCK: return "MalformedSuperblock"; |
| 49 | case SV1_E_BAD_SUPERBLOCK_MAGIC: return "BadSuperblockMagic"; |
| 50 | case SV1_E_BAD_SUPERBLOCK_CRC: return "BadSuperblockCrc"; |
| 51 | case SV1_E_NONZERO_RESERVED: return "NonzeroReserved"; |
| 52 | case SV1_E_WRONG_SUPERBLOCK_SLOT: return "WrongSuperblockSlot"; |
| 53 | case SV1_E_UNSUPPORTED_FEATURES: return "UnsupportedFeatures"; |
| 54 | case SV1_E_INTEGRITY: return "IntegrityFailure"; |
| 55 | case SV1_E_INVALID_GENERATION: return "InvalidGeneration"; |
| 56 | case SV1_E_CONFLICTING_ROOTS: return "ConflictingRoots"; |
| 57 | case SV1_E_INCONSISTENT_HISTORY: return "InconsistentHistory"; |
| 58 | case SV1_E_ARG: return "Arg"; |
| 59 | default: return "Unknown"; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* ---- byte helpers ---- */ |
| 64 | void sv1_put16(uint8_t *b, uint16_t v) |
| 65 | { |
| 66 | b[0] = (uint8_t)v; |
| 67 | b[1] = (uint8_t)(v >> 8); |
| 68 | } |
| 69 | void sv1_put32(uint8_t *b, uint32_t v) |
| 70 | { |
| 71 | for (int i = 0; i < 4; i++) |
| 72 | b[i] = (uint8_t)(v >> (8 * i)); |
| 73 | } |
| 74 | void sv1_put64(uint8_t *b, uint64_t v) |
| 75 | { |
| 76 | for (int i = 0; i < 8; i++) |
| 77 | b[i] = (uint8_t)(v >> (8 * i)); |
| 78 | } |
| 79 | uint16_t sv1_get16(const uint8_t *b) { return (uint16_t)(b[0] | (b[1] << 8)); } |
| 80 | uint32_t sv1_get32(const uint8_t *b) |
| 81 | { |
| 82 | return (uint32_t)b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | |
| 83 | ((uint32_t)b[3] << 24); |
| 84 | } |
| 85 | uint64_t sv1_get64(const uint8_t *b) |
| 86 | { |
| 87 | uint64_t v = 0; |
| 88 | for (int i = 7; i >= 0; i--) |
| 89 | v = (v << 8) | b[i]; |
| 90 | return v; |
| 91 | } |
| 92 | void sv1_copy(void *dst, const void *src, size_t n) |
| 93 | { |
| 94 | uint8_t *d = dst; |
| 95 | const uint8_t *s = src; |
| 96 | for (size_t i = 0; i < n; i++) |
| 97 | d[i] = s[i]; |
| 98 | } |
| 99 | void sv1_zero(void *dst, size_t n) |
| 100 | { |
| 101 | volatile uint8_t *d = dst; |
| 102 | for (size_t i = 0; i < n; i++) |
| 103 | d[i] = 0; |
| 104 | } |
| 105 | int sv1_equal(const void *a, const void *b, size_t n) |
| 106 | { |
| 107 | const uint8_t *x = a, *y = b; |
| 108 | for (size_t i = 0; i < n; i++) |
| 109 | if (x[i] != y[i]) |
| 110 | return 0; |
| 111 | return 1; |
| 112 | } |
| 113 | int sv1_all_zero(const void *a, size_t n) |
| 114 | { |
| 115 | const uint8_t *x = a; |
| 116 | for (size_t i = 0; i < n; i++) |
| 117 | if (x[i]) |
| 118 | return 0; |
| 119 | return 1; |
| 120 | } |
| 121 | int sv1_cmp_id(const uint8_t a[32], const uint8_t b[32]) |
| 122 | { |
| 123 | for (int i = 0; i < 32; i++) |
| 124 | if (a[i] != b[i]) |
| 125 | return a[i] < b[i] ? -1 : 1; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | uint32_t sv1_crc32c(const uint8_t *bytes, size_t len) |
| 130 | { |
| 131 | uint32_t crc = 0xffffffffu; |
| 132 | for (size_t i = 0; i < len; i++) { |
| 133 | crc ^= bytes[i]; |
| 134 | for (int k = 0; k < 8; k++) { |
| 135 | uint32_t mask = 0u - (crc & 1u); |
| 136 | crc = (crc >> 1) ^ (0x82f63b78u & mask); |
| 137 | } |
| 138 | } |
| 139 | return ~crc; |
| 140 | } |
| 141 | |
| 142 | /* ---- ObjectId ---- */ |
| 143 | int sv1_object_id_begin(sha256_ctx *ctx, uint16_t kind, uint16_t version, uint64_t len) |
| 144 | { |
| 145 | if (kind == 0 || version == 0 || len == 0) return SV1_E_INVALID_OBJECT; /* GUARD:id-shape */ |
| 146 | if (len > SV1_MAX_OBJECT_BYTES) |
| 147 | return SV1_E_OBJECT_TOO_LARGE; |
| 148 | uint8_t d[12]; |
| 149 | sv1_put16(d, kind); |
| 150 | sv1_put16(d + 2, version); |
| 151 | sv1_put64(d + 4, len); |
| 152 | sha256_init(ctx); |
| 153 | sha256_update(ctx, OBJECT_DOMAIN, sizeof OBJECT_DOMAIN); |
| 154 | sha256_update(ctx, d, sizeof d); |
| 155 | return SV1_OK; |
| 156 | } |
| 157 | |
| 158 | int sv1_object_id(uint16_t kind, uint16_t version, const uint8_t *bytes, uint64_t len, |
| 159 | uint8_t out[32]) |
| 160 | { |
| 161 | sha256_ctx ctx; |
| 162 | int e = sv1_object_id_begin(&ctx, kind, version, len); |
| 163 | if (e) |
| 164 | return e; |
| 165 | sha256_update(&ctx, bytes, (size_t)len); |
| 166 | sha256_final(&ctx, out); |
| 167 | return SV1_OK; |
| 168 | } |
| 169 | |
| 170 | int sv1_unit_count(uint64_t byte_length, uint32_t *units) |
| 171 | { |
| 172 | if (byte_length == 0) |
| 173 | return SV1_E_INVALID_OBJECT; |
| 174 | if (byte_length > SV1_MAX_OBJECT_BYTES) |
| 175 | return SV1_E_OBJECT_TOO_LARGE; |
| 176 | uint64_t u = (byte_length + SV1_UNIT - 1) / SV1_UNIT; /* cannot overflow below 2^26 */ |
| 177 | if (u > SV1_MAX_OBJECT_UNITS) |
| 178 | return SV1_E_OBJECT_TOO_LARGE; |
| 179 | *units = (uint32_t)u; |
| 180 | return SV1_OK; |
| 181 | } |
| 182 | |
| 183 | int sv1_validate_extent(const uint8_t id[32], uint16_t kind, uint16_t version, |
| 184 | uint64_t byte_length, const uint8_t *extent, size_t extent_len) |
| 185 | { |
| 186 | uint32_t units; |
| 187 | int e = sv1_unit_count(byte_length, &units); |
| 188 | if (e) |
| 189 | return e; |
| 190 | if ((uint64_t)extent_len != (uint64_t)units * SV1_UNIT) |
| 191 | return SV1_E_INVALID_LENGTH; |
| 192 | if (!sv1_all_zero(extent + byte_length, extent_len - (size_t)byte_length)) return SV1_E_NONZERO_RESERVED; /* GUARD:extent-padding */ |
| 193 | uint8_t got[32]; |
| 194 | e = sv1_object_id(kind, version, extent, byte_length, got); |
| 195 | if (e) |
| 196 | return e; |
| 197 | if (!sv1_equal(got, id, 32)) return SV1_E_INTEGRITY; /* GUARD:extent-hash */ |
| 198 | return SV1_OK; |
| 199 | } |
| 200 | |
| 201 | /* ---- CatalogEntry ---- */ |
| 202 | static int entry_validate_shape(const sv1_entry *e) |
| 203 | { |
| 204 | if (e->kind < 3 || e->version == 0 || e->flags != 0) return SV1_E_MALFORMED_DESCRIPTOR; /* GUARD:entry-shape */ |
| 205 | uint32_t u; |
| 206 | int r = sv1_unit_count(e->byte_length, &u); |
| 207 | if (r) |
| 208 | return r; |
| 209 | if (u != e->unit_count) return SV1_E_MALFORMED_DESCRIPTOR; /* GUARD:entry-units */ |
| 210 | return SV1_OK; |
| 211 | } |
| 212 | |
| 213 | int sv1_entry_encode(const sv1_entry *e, uint8_t out[64]) |
| 214 | { |
| 215 | int r = entry_validate_shape(e); |
| 216 | if (r) |
| 217 | return r; |
| 218 | sv1_zero(out, 64); |
| 219 | sv1_copy(out, e->object_id, 32); |
| 220 | sv1_put16(out + 32, e->kind); |
| 221 | sv1_put16(out + 34, e->version); |
| 222 | sv1_put64(out + 36, e->first_unit); |
| 223 | sv1_put64(out + 44, e->byte_length); |
| 224 | sv1_put32(out + 52, e->unit_count); |
| 225 | sv1_put16(out + 56, e->flags); |
| 226 | return SV1_OK; |
| 227 | } |
| 228 | |
| 229 | int sv1_entry_decode(const uint8_t *b, size_t len, sv1_entry *out) |
| 230 | { |
| 231 | if (len != SV1_CATALOG_ENTRY) |
| 232 | return SV1_E_INVALID_LENGTH; |
| 233 | if (!sv1_all_zero(b + 58, 6)) return SV1_E_NONZERO_RESERVED; /* GUARD:entry-reserved */ |
| 234 | sv1_copy(out->object_id, b, 32); |
| 235 | out->kind = sv1_get16(b + 32); |
| 236 | out->version = sv1_get16(b + 34); |
| 237 | out->first_unit = sv1_get64(b + 36); |
| 238 | out->byte_length = sv1_get64(b + 44); |
| 239 | out->unit_count = sv1_get32(b + 52); |
| 240 | out->flags = sv1_get16(b + 56); |
| 241 | return entry_validate_shape(out); |
| 242 | } |
| 243 | |
| 244 | int sv1_entry_validate_bounds(const sv1_entry *e, uint64_t region_units, |
| 245 | uint64_t catalog_first_unit, uint64_t hw) |
| 246 | { |
| 247 | int r = entry_validate_shape(e); |
| 248 | if (r) |
| 249 | return r; |
| 250 | if (region_units < 4 || region_units > SV1_MAX_REGION_UNITS || hw < 4 || hw > region_units) |
| 251 | return SV1_E_OUT_OF_BOUNDS; |
| 252 | if (e->first_unit > UINT64_MAX - e->unit_count) |
| 253 | return SV1_E_LENGTH_OVERFLOW; |
| 254 | uint64_t end = e->first_unit + e->unit_count; |
| 255 | int out_of_bounds = e->first_unit < 2 || end > catalog_first_unit || end > hw || end > region_units; |
| 256 | if (out_of_bounds) return SV1_E_OUT_OF_BOUNDS; /* GUARD:entry-bounds */ |
| 257 | return SV1_OK; |
| 258 | } |
| 259 | |
| 260 | int sv1_entry_equal(const sv1_entry *a, const sv1_entry *b) |
| 261 | { |
| 262 | return sv1_equal(a->object_id, b->object_id, 32) && a->kind == b->kind && |
| 263 | a->version == b->version && a->first_unit == b->first_unit && |
| 264 | a->byte_length == b->byte_length && a->unit_count == b->unit_count && |
| 265 | a->flags == b->flags; |
| 266 | } |
| 267 | |
| 268 | /* ---- Catalog ---- */ |
| 269 | uint64_t sv1_catalog_len(uint64_t n) { return SV1_CATALOG_HEADER + n * SV1_CATALOG_ENTRY; } |
| 270 | |
| 271 | static int check_order(const sv1_entry *entries, size_t n) |
| 272 | { |
| 273 | for (size_t i = 0; i + 1 < n; i++) { |
| 274 | int c = sv1_cmp_id(entries[i].object_id, entries[i + 1].object_id); |
| 275 | if (c == 0) return SV1_E_DUPLICATE_OBJECT; /* GUARD:catalog-duplicate */ |
| 276 | if (c > 0) return SV1_E_CATALOG_ORDER; /* GUARD:catalog-order */ |
| 277 | } |
| 278 | return SV1_OK; |
| 279 | } |
| 280 | |
| 281 | int sv1_catalog_encode(const sv1_entry *entries, size_t n, uint8_t *out, size_t cap, |
| 282 | size_t *out_len) |
| 283 | { |
| 284 | if (n > SV1_MAX_CATALOG_ENTRIES) |
| 285 | return SV1_E_CATALOG_TOO_LARGE; |
| 286 | int r = check_order(entries, n); |
| 287 | if (r) |
| 288 | return r; |
| 289 | uint64_t len = sv1_catalog_len(n); |
| 290 | if (!out || len > cap) |
| 291 | return SV1_E_ARG; |
| 292 | sv1_copy(out, CATALOG_MAGIC, 8); |
| 293 | sv1_put16(out + 8, 1); |
| 294 | sv1_put16(out + 10, SV1_CATALOG_ENTRY); |
| 295 | sv1_put32(out + 12, (uint32_t)n); |
| 296 | for (size_t i = 0; i < n; i++) { |
| 297 | r = sv1_entry_encode(&entries[i], out + SV1_CATALOG_HEADER + i * SV1_CATALOG_ENTRY); |
| 298 | if (r) |
| 299 | return r; |
| 300 | } |
| 301 | *out_len = (size_t)len; |
| 302 | return SV1_OK; |
| 303 | } |
| 304 | |
| 305 | int sv1_catalog_decode(const uint8_t *b, size_t len, sv1_entry *entries, size_t cap, |
| 306 | uint32_t *n_out) |
| 307 | { |
| 308 | if (len < SV1_CATALOG_HEADER) |
| 309 | return SV1_E_INVALID_LENGTH; |
| 310 | if (!sv1_equal(b, CATALOG_MAGIC, 8)) return SV1_E_BAD_CATALOG_MAGIC; /* GUARD:catalog-magic */ |
| 311 | if (sv1_get16(b + 8) != 1 || sv1_get16(b + 10) != SV1_CATALOG_ENTRY) |
| 312 | return SV1_E_UNSUPPORTED_VERSION; |
| 313 | uint32_t count = sv1_get32(b + 12); |
| 314 | if (count > SV1_MAX_CATALOG_ENTRIES) |
| 315 | return SV1_E_CATALOG_TOO_LARGE; |
| 316 | if ((uint64_t)len != sv1_catalog_len(count)) return SV1_E_INVALID_LENGTH; /* GUARD:catalog-length */ |
| 317 | if (count > cap) |
| 318 | return SV1_E_ARG; |
| 319 | for (uint32_t i = 0; i < count; i++) { |
| 320 | int r = sv1_entry_decode(b + SV1_CATALOG_HEADER + (size_t)i * SV1_CATALOG_ENTRY, |
| 321 | SV1_CATALOG_ENTRY, &entries[i]); |
| 322 | if (r) |
| 323 | return r; |
| 324 | } |
| 325 | int r = check_order(entries, count); |
| 326 | if (r) |
| 327 | return r; |
| 328 | *n_out = count; |
| 329 | return SV1_OK; |
| 330 | } |
| 331 | |
| 332 | static int extent_less(const sv1_extent *a, const sv1_extent *b) |
| 333 | { |
| 334 | return a->first < b->first || (a->first == b->first && a->end < b->end); |
| 335 | } |
| 336 | |
| 337 | static void sift_extents(sv1_extent *x, size_t start, size_t n) |
| 338 | { |
| 339 | size_t root = start; |
| 340 | for (;;) { |
| 341 | size_t child = 2 * root + 1; |
| 342 | if (child >= n) |
| 343 | return; |
| 344 | if (child + 1 < n && extent_less(&x[child], &x[child + 1])) |
| 345 | child++; |
| 346 | if (!extent_less(&x[root], &x[child])) |
| 347 | return; |
| 348 | sv1_extent t = x[root]; |
| 349 | x[root] = x[child]; |
| 350 | x[child] = t; |
| 351 | root = child; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void sv1_sort_extents(sv1_extent *x, size_t n) |
| 356 | { |
| 357 | if (n < 2) |
| 358 | return; |
| 359 | for (size_t i = n / 2; i-- > 0;) |
| 360 | sift_extents(x, i, n); |
| 361 | for (size_t end = n - 1; end > 0; end--) { |
| 362 | sv1_extent t = x[0]; |
| 363 | x[0] = x[end]; |
| 364 | x[end] = t; |
| 365 | sift_extents(x, 0, end); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | int sv1_catalog_validate_extents(const sv1_entry *entries, size_t n, uint64_t region_units, |
| 370 | uint64_t catalog_first_unit, uint64_t hw, sv1_extent *scratch) |
| 371 | { |
| 372 | if (region_units < 4 || region_units > SV1_MAX_REGION_UNITS || hw < 4 || |
| 373 | hw > region_units || catalog_first_unit < 2 || catalog_first_unit >= region_units) |
| 374 | return SV1_E_OUT_OF_BOUNDS; |
| 375 | for (size_t i = 0; i < n; i++) { |
| 376 | int r = sv1_entry_validate_bounds(&entries[i], region_units, catalog_first_unit, hw); |
| 377 | if (r) |
| 378 | return r; |
| 379 | scratch[i].first = entries[i].first_unit; |
| 380 | scratch[i].end = entries[i].first_unit + entries[i].unit_count; |
| 381 | } |
| 382 | sv1_sort_extents(scratch, n); |
| 383 | for (size_t i = 0; i + 1 < n; i++) |
| 384 | if (scratch[i].end > scratch[i + 1].first) return SV1_E_OVERLAP; /* GUARD:catalog-overlap */ |
| 385 | return SV1_OK; |
| 386 | } |
| 387 | |
| 388 | long sv1_catalog_find(const sv1_entry *entries, size_t n, const uint8_t id[32]) |
| 389 | { |
| 390 | size_t lo = 0, hi = n; |
| 391 | while (lo < hi) { |
| 392 | size_t mid = lo + (hi - lo) / 2; |
| 393 | int c = sv1_cmp_id(entries[mid].object_id, id); |
| 394 | if (c == 0) |
| 395 | return (long)mid; |
| 396 | if (c < 0) |
| 397 | lo = mid + 1; |
| 398 | else |
| 399 | hi = mid; |
| 400 | } |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | static void swap_entries(sv1_entry *a, sv1_entry *b) |
| 405 | { |
| 406 | sv1_entry t = *a; |
| 407 | *a = *b; |
| 408 | *b = t; |
| 409 | } |
| 410 | |
| 411 | static void sift_entries(sv1_entry *x, size_t root, size_t n) |
| 412 | { |
| 413 | for (;;) { |
| 414 | size_t child = 2 * root + 1; |
| 415 | if (child >= n) |
| 416 | return; |
| 417 | if (child + 1 < n && sv1_cmp_id(x[child].object_id, x[child + 1].object_id) < 0) |
| 418 | child++; |
| 419 | if (sv1_cmp_id(x[root].object_id, x[child].object_id) >= 0) |
| 420 | return; |
| 421 | swap_entries(&x[root], &x[child]); |
| 422 | root = child; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void sv1_sort_entries(sv1_entry *x, size_t n) |
| 427 | { |
| 428 | if (n < 2) |
| 429 | return; |
| 430 | for (size_t i = n / 2; i-- > 0;) |
| 431 | sift_entries(x, i, n); |
| 432 | for (size_t end = n - 1; end > 0; end--) { |
| 433 | swap_entries(&x[0], &x[end]); |
| 434 | sift_entries(x, 0, end); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* ---- CommitRecord ---- */ |
| 439 | int sv1_commit_validate(const sv1_commit *c) |
| 440 | { |
| 441 | if (c->region_units < 4 || c->region_units > SV1_MAX_REGION_UNITS || c->generation == 0) return SV1_E_MALFORMED_COMMIT; /* GUARD:commit-region */ |
| 442 | uint64_t expected = sv1_catalog_len(c->catalog_entry_count); |
| 443 | if (c->catalog_entry_count > SV1_MAX_CATALOG_ENTRIES || c->catalog_byte_length != expected) |
| 444 | return SV1_E_MALFORMED_COMMIT; |
| 445 | uint32_t u; |
| 446 | int r = sv1_unit_count(c->catalog_byte_length, &u); |
| 447 | if (r) |
| 448 | return r; |
| 449 | if (u != c->catalog_unit_count) |
| 450 | return SV1_E_MALFORMED_COMMIT; |
| 451 | if (c->catalog_first_unit > UINT64_MAX - c->catalog_unit_count) |
| 452 | return SV1_E_LENGTH_OVERFLOW; |
| 453 | uint64_t end = c->catalog_first_unit + c->catalog_unit_count; |
| 454 | int bad_layout = c->catalog_first_unit < 2 || end >= c->region_units || |
| 455 | c->committed_high_water != end + 1 || c->committed_high_water > c->region_units; |
| 456 | if (bad_layout) return SV1_E_MALFORMED_COMMIT; /* GUARD:commit-layout */ |
| 457 | if (c->generation == 1) { |
| 458 | int bad_link = c->previous_generation != 0 || !sv1_all_zero(c->previous_commit_id, 32) || |
| 459 | !sv1_all_zero(c->previous_catalog_id, 32); |
| 460 | if (bad_link) return SV1_E_INVALID_GENERATION; /* GUARD:commit-genesis-link */ |
| 461 | } else { |
| 462 | int bad_link = c->previous_generation == UINT64_MAX || |
| 463 | c->previous_generation + 1 != c->generation || |
| 464 | sv1_all_zero(c->previous_commit_id, 32) || |
| 465 | sv1_all_zero(c->previous_catalog_id, 32); |
| 466 | if (bad_link) return SV1_E_INVALID_GENERATION; /* GUARD:commit-link */ |
| 467 | } |
| 468 | return SV1_OK; |
| 469 | } |
| 470 | |
| 471 | int sv1_commit_encode(const sv1_commit *c, uint8_t out[SV1_COMMIT_BYTES]) |
| 472 | { |
| 473 | int r = sv1_commit_validate(c); |
| 474 | if (r) |
| 475 | return r; |
| 476 | sv1_zero(out, SV1_COMMIT_BYTES); |
| 477 | sv1_copy(out, COMMIT_MAGIC, 8); |
| 478 | sv1_put16(out + 8, 1); |
| 479 | sv1_put16(out + 10, SV1_COMMIT_BYTES); |
| 480 | sv1_put16(out + 12, 1); |
| 481 | sv1_put16(out + 14, 0); |
| 482 | sv1_copy(out + 32, c->store_uuid, 16); |
| 483 | sv1_put64(out + 48, c->region_units); |
| 484 | sv1_put64(out + 56, c->generation); |
| 485 | sv1_put64(out + 64, c->previous_generation); |
| 486 | sv1_copy(out + 72, c->previous_commit_id, 32); |
| 487 | sv1_copy(out + 104, c->previous_catalog_id, 32); |
| 488 | sv1_copy(out + 136, c->catalog_id, 32); |
| 489 | sv1_put64(out + 168, c->catalog_first_unit); |
| 490 | sv1_put64(out + 176, c->catalog_byte_length); |
| 491 | sv1_put32(out + 184, c->catalog_unit_count); |
| 492 | sv1_put32(out + 188, c->catalog_entry_count); |
| 493 | sv1_put64(out + 192, c->committed_high_water); |
| 494 | return SV1_OK; |
| 495 | } |
| 496 | |
| 497 | int sv1_commit_decode(const uint8_t *b, size_t len, sv1_commit *c) |
| 498 | { |
| 499 | if (len != SV1_COMMIT_BYTES || !sv1_equal(b, COMMIT_MAGIC, 8)) return SV1_E_MALFORMED_COMMIT; /* GUARD:commit-magic */ |
| 500 | if (sv1_get16(b + 8) != 1 || sv1_get16(b + 10) != SV1_COMMIT_BYTES) |
| 501 | return SV1_E_UNSUPPORTED_VERSION; |
| 502 | if (sv1_get16(b + 12) != 1 || sv1_get16(b + 14) != 0) |
| 503 | return SV1_E_UNSUPPORTED_VERSION; |
| 504 | if (sv1_get64(b + 16) != 0 || sv1_get64(b + 24) != 0) return SV1_E_UNSUPPORTED_FEATURES; /* GUARD:commit-features */ |
| 505 | sv1_copy(c->store_uuid, b + 32, 16); |
| 506 | c->region_units = sv1_get64(b + 48); |
| 507 | c->generation = sv1_get64(b + 56); |
| 508 | c->previous_generation = sv1_get64(b + 64); |
| 509 | sv1_copy(c->previous_commit_id, b + 72, 32); |
| 510 | sv1_copy(c->previous_catalog_id, b + 104, 32); |
| 511 | sv1_copy(c->catalog_id, b + 136, 32); |
| 512 | c->catalog_first_unit = sv1_get64(b + 168); |
| 513 | c->catalog_byte_length = sv1_get64(b + 176); |
| 514 | c->catalog_unit_count = sv1_get32(b + 184); |
| 515 | c->catalog_entry_count = sv1_get32(b + 188); |
| 516 | c->committed_high_water = sv1_get64(b + 192); |
| 517 | return sv1_commit_validate(c); |
| 518 | } |
| 519 | |
| 520 | int sv1_commit_object_id(const sv1_commit *c, uint8_t out[32]) |
| 521 | { |
| 522 | uint8_t b[SV1_COMMIT_BYTES]; |
| 523 | int r = sv1_commit_encode(c, b); |
| 524 | if (r) |
| 525 | return r; |
| 526 | return sv1_object_id(SV1_KIND_COMMIT, SV1_VERSION, b, SV1_COMMIT_BYTES, out); |
| 527 | } |
| 528 | |
| 529 | int sv1_commit_validates_catalog(const sv1_commit *c, const uint8_t catalog_id[32], |
| 530 | const sv1_entry *entries, size_t n) |
| 531 | { |
| 532 | /* Rust: semantic = catalog.encode()?; then the comparisons. The |
| 533 | * re-encoding is hashed as a stream, so no catalog-sized buffer. */ |
| 534 | if (n > SV1_MAX_CATALOG_ENTRIES) |
| 535 | return SV1_E_CATALOG_TOO_LARGE; |
| 536 | int r = check_order(entries, n); |
| 537 | if (r) |
| 538 | return r; |
| 539 | uint8_t enc[SV1_CATALOG_ENTRY]; |
| 540 | for (size_t i = 0; i < n; i++) { |
| 541 | r = sv1_entry_encode(&entries[i], enc); |
| 542 | if (r) |
| 543 | return r; |
| 544 | } |
| 545 | uint64_t sem_len = sv1_catalog_len(n); |
| 546 | int mismatch = !sv1_equal(c->catalog_id, catalog_id, 32) || c->catalog_entry_count != n || |
| 547 | c->catalog_byte_length != sem_len; |
| 548 | if (mismatch) return SV1_E_INTEGRITY; /* GUARD:commit-catalog */ |
| 549 | uint32_t u; |
| 550 | r = sv1_unit_count(sem_len, &u); |
| 551 | if (r) |
| 552 | return r; |
| 553 | if (u != c->catalog_unit_count) |
| 554 | return SV1_E_INTEGRITY; |
| 555 | sha256_ctx ctx; |
| 556 | r = sv1_object_id_begin(&ctx, SV1_KIND_CATALOG, SV1_VERSION, sem_len); |
| 557 | if (r) |
| 558 | return r; |
| 559 | uint8_t hdr[SV1_CATALOG_HEADER]; |
| 560 | sv1_copy(hdr, CATALOG_MAGIC, 8); |
| 561 | sv1_put16(hdr + 8, 1); |
| 562 | sv1_put16(hdr + 10, SV1_CATALOG_ENTRY); |
| 563 | sv1_put32(hdr + 12, (uint32_t)n); |
| 564 | sha256_update(&ctx, hdr, sizeof hdr); |
| 565 | for (size_t i = 0; i < n; i++) { |
| 566 | (void)sv1_entry_encode(&entries[i], enc); |
| 567 | sha256_update(&ctx, enc, sizeof enc); |
| 568 | } |
| 569 | uint8_t id[32]; |
| 570 | sha256_final(&ctx, id); |
| 571 | if (!sv1_equal(id, catalog_id, 32)) return SV1_E_INTEGRITY; /* GUARD:catalog-id */ |
| 572 | return SV1_OK; |
| 573 | } |
| 574 | |
| 575 | int sv1_commit_identifies_predecessor(const sv1_commit *s, const sv1_commit *p, |
| 576 | const uint8_t prev_id[32]) |
| 577 | { |
| 578 | return s->generation != 0 && s->generation - 1 == p->generation && |
| 579 | s->previous_generation == p->generation && |
| 580 | sv1_equal(s->previous_commit_id, prev_id, 32) && |
| 581 | sv1_equal(s->previous_catalog_id, p->catalog_id, 32) && |
| 582 | sv1_equal(s->store_uuid, p->store_uuid, 16) && s->region_units == p->region_units && |
| 583 | s->catalog_first_unit >= p->committed_high_water; |
| 584 | } |
| 585 | |
| 586 | /* ---- Superblock ---- */ |
| 587 | static int superblock_validate_shape(const sv1_superblock *s) |
| 588 | { |
| 589 | int bad_shape = s->slot_id > 1 || s->region_units < 4 || s->region_units > SV1_MAX_REGION_UNITS || |
| 590 | s->generation == 0 || s->committed_high_water > s->region_units || |
| 591 | s->committed_high_water < 4; |
| 592 | if (bad_shape) return SV1_E_MALFORMED_SUPERBLOCK; /* GUARD:sb-shape */ |
| 593 | uint64_t expected = sv1_catalog_len(s->catalog_entry_count); |
| 594 | if (s->catalog_first_unit > UINT64_MAX - s->catalog_unit_count) |
| 595 | return SV1_E_LENGTH_OVERFLOW; |
| 596 | uint64_t end = s->catalog_first_unit + s->catalog_unit_count; |
| 597 | if (s->catalog_entry_count > SV1_MAX_CATALOG_ENTRIES || s->catalog_byte_length != expected) |
| 598 | return SV1_E_MALFORMED_SUPERBLOCK; |
| 599 | uint32_t u; |
| 600 | int r = sv1_unit_count(s->catalog_byte_length, &u); |
| 601 | if (r) |
| 602 | return r; |
| 603 | if (u != s->catalog_unit_count || s->catalog_first_unit < 2 || |
| 604 | end != s->commit_record_unit || s->commit_record_unit == UINT64_MAX || |
| 605 | s->commit_record_unit + 1 != s->committed_high_water) |
| 606 | return SV1_E_MALFORMED_SUPERBLOCK; |
| 607 | return SV1_OK; |
| 608 | } |
| 609 | |
| 610 | int sv1_superblock_encode(const sv1_superblock *s, uint8_t out[SV1_UNIT]) |
| 611 | { |
| 612 | int r = superblock_validate_shape(s); |
| 613 | if (r) |
| 614 | return r; |
| 615 | sv1_zero(out, SV1_UNIT); |
| 616 | sv1_copy(out, SUPERBLOCK_MAGIC, 8); |
| 617 | sv1_put16(out + 8, 1); |
| 618 | sv1_put16(out + 10, 0); |
| 619 | sv1_copy(out + 28, s->store_uuid, 16); |
| 620 | sv1_put32(out + 44, s->slot_id); |
| 621 | sv1_put64(out + 48, s->region_units); |
| 622 | sv1_put64(out + 56, s->generation); |
| 623 | sv1_copy(out + 64, s->commit_record_id, 32); |
| 624 | sv1_put64(out + 96, s->commit_record_unit); |
| 625 | sv1_copy(out + 104, s->catalog_id, 32); |
| 626 | sv1_put64(out + 136, s->catalog_first_unit); |
| 627 | sv1_put64(out + 144, s->catalog_byte_length); |
| 628 | sv1_put32(out + 152, s->catalog_unit_count); |
| 629 | sv1_put32(out + 156, s->catalog_entry_count); |
| 630 | sv1_put64(out + 160, s->committed_high_water); |
| 631 | sv1_put32(out + SV1_SB_CRC_OFFSET, sv1_crc32c(out, SV1_UNIT)); |
| 632 | return SV1_OK; |
| 633 | } |
| 634 | |
| 635 | /* CRC of a superblock unit with the CRC field read as zero. */ |
| 636 | static uint32_t sb_crc(const uint8_t *b) |
| 637 | { |
| 638 | uint32_t crc = 0xffffffffu; |
| 639 | for (size_t i = 0; i < SV1_UNIT; i++) { |
| 640 | crc ^= (i >= SV1_SB_CRC_OFFSET && i < SV1_SB_CRC_OFFSET + 4) ? 0u : b[i]; |
| 641 | for (int k = 0; k < 8; k++) { |
| 642 | uint32_t mask = 0u - (crc & 1u); |
| 643 | crc = (crc >> 1) ^ (0x82f63b78u & mask); |
| 644 | } |
| 645 | } |
| 646 | return ~crc; |
| 647 | } |
| 648 | |
| 649 | /* 1 if the CRC field matches (engine helper crc_valid). */ |
| 650 | int sv1_superblock_crc_ok(const uint8_t b[SV1_UNIT]) |
| 651 | { |
| 652 | #ifdef CK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC |
| 653 | /* TEST-ONLY mutant of the C kernel crash gate (native/kernel full image |
| 654 | * with CK_TEST_STORE_CRASH=1 CK_TEST_STORE_CRASH_MUTANT=accept_bad_root_crc): |
| 655 | * every superblock CRC is accepted. Never defined otherwise. */ |
| 656 | (void)sb_crc; |
| 657 | (void)b; |
| 658 | return 1; |
| 659 | #else |
| 660 | return sb_crc(b) == sv1_get32(b + SV1_SB_CRC_OFFSET); |
| 661 | #endif |
| 662 | } |
| 663 | |
| 664 | int sv1_superblock_decode(const uint8_t *b, size_t len, uint32_t expected_slot, |
| 665 | sv1_superblock *s) |
| 666 | { |
| 667 | if (len != SV1_UNIT || !sv1_equal(b, SUPERBLOCK_MAGIC, 8)) return SV1_E_BAD_SUPERBLOCK_MAGIC; /* GUARD:sb-magic */ |
| 668 | #ifndef CK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC |
| 669 | if (sb_crc(b) != sv1_get32(b + SV1_SB_CRC_OFFSET)) return SV1_E_BAD_SUPERBLOCK_CRC; /* GUARD:sb-crc */ |
| 670 | #endif |
| 671 | if (!sv1_all_zero(b + SV1_SB_RESERVED_OFFSET, SV1_UNIT - SV1_SB_RESERVED_OFFSET)) return SV1_E_NONZERO_RESERVED; /* GUARD:sb-reserved */ |
| 672 | if (sv1_get16(b + 8) != 1 || sv1_get16(b + 10) != 0) return SV1_E_UNSUPPORTED_VERSION; /* GUARD:sb-version */ |
| 673 | if (sv1_get64(b + 12) != 0 || sv1_get64(b + 20) != 0) |
| 674 | return SV1_E_UNSUPPORTED_FEATURES; |
| 675 | uint32_t slot = sv1_get32(b + 44); |
| 676 | if (slot != expected_slot || slot > 1) return SV1_E_WRONG_SUPERBLOCK_SLOT; /* GUARD:sb-slot */ |
| 677 | sv1_copy(s->store_uuid, b + 28, 16); |
| 678 | s->slot_id = slot; |
| 679 | s->region_units = sv1_get64(b + 48); |
| 680 | s->generation = sv1_get64(b + 56); |
| 681 | sv1_copy(s->commit_record_id, b + 64, 32); |
| 682 | s->commit_record_unit = sv1_get64(b + 96); |
| 683 | sv1_copy(s->catalog_id, b + 104, 32); |
| 684 | s->catalog_first_unit = sv1_get64(b + 136); |
| 685 | s->catalog_byte_length = sv1_get64(b + 144); |
| 686 | s->catalog_unit_count = sv1_get32(b + 152); |
| 687 | s->catalog_entry_count = sv1_get32(b + 156); |
| 688 | s->committed_high_water = sv1_get64(b + 160); |
| 689 | return superblock_validate_shape(s); |
| 690 | } |
| 691 | |
| 692 | int sv1_superblock_validate_commit(const sv1_superblock *s, const uint8_t commit_id[32], |
| 693 | const sv1_commit *c) |
| 694 | { |
| 695 | int mismatch = !sv1_equal(s->commit_record_id, commit_id, 32) || |
| 696 | !sv1_equal(s->store_uuid, c->store_uuid, 16) || s->region_units != c->region_units || |
| 697 | s->generation != c->generation || !sv1_equal(s->catalog_id, c->catalog_id, 32) || |
| 698 | s->catalog_first_unit != c->catalog_first_unit || |
| 699 | s->catalog_byte_length != c->catalog_byte_length || |
| 700 | s->catalog_unit_count != c->catalog_unit_count || |
| 701 | s->catalog_entry_count != c->catalog_entry_count || |
| 702 | s->committed_high_water != c->committed_high_water; |
| 703 | if (mismatch) return SV1_E_MALFORMED_SUPERBLOCK; /* GUARD:sb-commit-match */ |
| 704 | if (s->commit_record_unit == UINT64_MAX || |
| 705 | s->commit_record_unit + 1 != c->committed_high_water || |
| 706 | s->catalog_first_unit > UINT64_MAX - s->catalog_unit_count || |
| 707 | s->catalog_first_unit + s->catalog_unit_count != s->commit_record_unit) |
| 708 | return SV1_E_MALFORMED_SUPERBLOCK; |
| 709 | return SV1_OK; |
| 710 | } |
| 711 | |
| 712 | int sv1_superblock_equivalent(const sv1_superblock *a, const sv1_superblock *b) |
| 713 | { |
| 714 | return sv1_equal(a->store_uuid, b->store_uuid, 16) && a->region_units == b->region_units && |
| 715 | a->generation == b->generation && |
| 716 | sv1_equal(a->commit_record_id, b->commit_record_id, 32); |
| 717 | } |
| 718 | |
| 719 | /* ---- genesis ---- */ |
| 720 | int sv1_genesis_units(const uint8_t uuid[16], uint64_t region_units, uint8_t out[4][SV1_UNIT]) |
| 721 | { |
| 722 | uint8_t cat[SV1_CATALOG_HEADER]; |
| 723 | size_t cat_len; |
| 724 | int r = sv1_catalog_encode((const sv1_entry *)0, 0, cat, sizeof cat, &cat_len); |
| 725 | if (r) |
| 726 | return r; |
| 727 | sv1_commit c; |
| 728 | sv1_zero(&c, sizeof c); |
| 729 | sv1_copy(c.store_uuid, uuid, 16); |
| 730 | c.region_units = region_units; |
| 731 | c.generation = 1; |
| 732 | r = sv1_object_id(SV1_KIND_CATALOG, SV1_VERSION, cat, cat_len, c.catalog_id); |
| 733 | if (r) |
| 734 | return r; |
| 735 | c.catalog_first_unit = 2; |
| 736 | c.catalog_byte_length = cat_len; |
| 737 | c.catalog_unit_count = 1; |
| 738 | c.catalog_entry_count = 0; |
| 739 | c.committed_high_water = 4; |
| 740 | sv1_zero(out, 4 * (size_t)SV1_UNIT); |
| 741 | r = sv1_commit_encode(&c, out[3]); |
| 742 | if (r) |
| 743 | return r; |
| 744 | sv1_superblock s; |
| 745 | sv1_zero(&s, sizeof s); |
| 746 | sv1_copy(s.store_uuid, uuid, 16); |
| 747 | s.slot_id = 0; |
| 748 | s.region_units = region_units; |
| 749 | s.generation = 1; |
| 750 | r = sv1_commit_object_id(&c, s.commit_record_id); |
| 751 | if (r) |
| 752 | return r; |
| 753 | s.commit_record_unit = 3; |
| 754 | sv1_copy(s.catalog_id, c.catalog_id, 32); |
| 755 | s.catalog_first_unit = 2; |
| 756 | s.catalog_byte_length = cat_len; |
| 757 | s.catalog_unit_count = 1; |
| 758 | s.catalog_entry_count = 0; |
| 759 | s.committed_high_water = 4; |
| 760 | r = sv1_superblock_encode(&s, out[0]); |
| 761 | if (r) |
| 762 | return r; |
| 763 | sv1_copy(out[2], cat, cat_len); |
| 764 | return SV1_OK; |
| 765 | } |