interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / artifact / osc_unit.h

149 lines · 7873 bytes Raw Blame History
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/* osc_unit.h -- admission of OSC unit containers (OSCUNIT, container v1).
18 *
19 * Contract: aien-spark/aien-protocols PR #17, specs/osc-unit-artifact/
20 * OSC_UNIT_ARTIFACT.md (section numbers below refer to it). This is a
21 * different format from Binary Artifact v0 (ck_artifact.h, ADR 0014); it
22 * reuses the in-tree SHA-256 (argus/sha256.c) and Ed25519 verify
23 * (sig/ed25519.c) and adds no crypto of its own.
24 *
25 * osc_unit_admit is a pure function of (bytes, policy): no allocation, no
26 * global state, no I/O, no unit code ever runs. Every offset and length is
27 * checked with 64-bit arithmetic on 32-bit fields before a byte it names is
28 * read. It returns 0 (accepted, *out filled) or the FIRST refusal code of the
29 * fixed check order of section 8.2. Admission is not launch: nothing here
30 * maps or runs code, and OWNER anchors are not provisioned until TRUST-1.
31 */
32#ifndef AIENOS_CK_OSC_UNIT_H
33#define AIENOS_CK_OSC_UNIT_H
34#include <stddef.h>
35#include <stdint.h>
36
37#define OSC_MAX_FUNCS 32u
38#define OSC_MAX_CAPS 16u
39#define OSC_UNIT_MAX_BYTES 2097152u
40
41/* Section 8.3 refusal codes (v1). 0 is "accepted". */
42enum osc_code {
43 OSC_OK = 0,
44 OSC_TRUNCATED = 1, OSC_BAD_MAGIC = 2, OSC_CONTAINER_VERSION = 3, OSC_HEADER_FIELD = 4,
45 OSC_UNIT_FORMAT = 5, OSC_ABI_VERSION = 6, OSC_UNKNOWN_FLAGS = 7, OSC_TRAILING_BYTES = 8,
46 OSC_TOTAL_LENGTH = 9, OSC_SECTION_TABLE = 10, OSC_SECTION_BOUNDS = 11, OSC_SECTION_OVERLAP = 12,
47 OSC_SECTION_LAYOUT = 13, OSC_LIMIT_EXCEEDED = 14, OSC_IR_HASH_MISMATCH = 15,
48 OSC_CODE_HASH_MISMATCH = 16, OSC_ENTRY_HASH_MISMATCH = 17, OSC_CAPS_HASH_MISMATCH = 18,
49 OSC_IR_MALFORMED = 19, OSC_ENTRY_TABLE = 20, OSC_CAPS_TABLE = 21, OSC_SIGNER_CLASS = 22,
50 OSC_SIGNATURE_ALGORITHM = 23, OSC_TEST_SIGNER_IN_RELEASE = 24, OSC_UNTRUSTED_SIGNER = 25,
51 OSC_BAD_SIGNATURE = 26, OSC_CAP_DOMAIN_UNSUPPORTED = 27, OSC_CAP_GEN_NOT_REPRESENTABLE = 28,
52 OSC_CAP_GENERATION_STALE = 29, OSC_RESOURCE_UNAVAILABLE = 30, OSC_ENTRY_NAME = 31,
53 OSC_ENTRY_NAME_HASH = 32, OSC_ENTRY_NAME_DUPLICATE = 33, OSC_CODE_INSTRUCTION = 34,
54 OSC_LAUNCH_BAD_ENTRY = 40, OSC_LAUNCH_ARG_SHAPE = 41
55};
56
57/* Stable spec name of a code ("?" for a number the spec does not define). */
58const char *osc_code_name(unsigned code);
59
60enum { OSC_SIGNER_TEST = 1, OSC_SIGNER_OWNER = 2 };
61
62struct osc_entry {
63 uint16_t fn_index;
64 uint8_t nregs, ret_kind;
65 uint8_t reg_kind[6];
66 uint32_t code_offset;
67 uint8_t name_hash[16];
68 uint8_t name_len;
69 char name[64]; /* NUL terminated copy of the name */
70};
71
72struct osc_cap {
73 uint16_t resource_kind; /* 2 channel, 3 object */
74 uint32_t resource_id, rights, bounds_kind, max_operations;
75 uint64_t max_bytes, bounds_offset, bounds_length;
76 uint16_t domain; /* 1 kernel IPC table (32-bit generation), 2 hosted authority */
77 uint64_t required_generation;
78};
79
80struct osc_accept {
81 uint8_t unit_digest[32]; /* UnitDigest, section 5.1: what execution and authorization bind to */
82 uint8_t ir_sha256[32]; /* program identity, display only (section 5.2) */
83 uint8_t code_sha256[32];
84 uint8_t signer_fp[32];
85 uint16_t unit_format, signer_class, function_count, pool_slots, cap_count;
86 uint32_t max_stack_bytes;
87 uint64_t cpu_ticks;
88 uint32_t ir_off, ir_len, code_off, code_len; /* into the admitted bytes */
89 struct osc_entry entry[OSC_MAX_FUNCS];
90 struct osc_cap cap[OSC_MAX_CAPS];
91};
92
93/* Loader inputs (section 8.1): local, never from the container. */
94struct osc_policy {
95 int release; /* 1 release build (TEST signers refused), 0 qualification */
96 const uint8_t (*test_anchors)[32]; /* raw Ed25519 public keys, class TEST */
97 unsigned n_test;
98 const uint8_t (*owner_anchors)[32]; /* class OWNER; empty until TRUST-1 provisions it */
99 unsigned n_owner;
100 uint32_t unit_formats; /* bit v set: unit_format_version v supported */
101 uint32_t abi_versions; /* bit v set: runtime_abi_version v provided */
102 uint32_t cap_domains; /* bit d set: capability domain d provided */
103 /* Loader state for step 15(c). Returns 0 and the resource's current generation, or nonzero
104 * when the loader has no such resource (then a pinned request is STALE). NULL: the loader
105 * assumes pinned generations match (host conformance profile only; the kernel passes a
106 * callback). */
107 int (*gen_lookup)(void *ctx, unsigned domain, unsigned kind, uint32_t id, uint64_t *current);
108 /* Staging maximum in bytes (0: none). A file longer than this is refused RESOURCE_UNAVAILABLE
109 * before step 1 (spec 8.2 step 16; the native Store holds at most CK_ART_MAX_BYTES). */
110 size_t staging_max;
111 /* Step 16: nonzero refuses with RESOURCE_UNAVAILABLE. NULL: reservation succeeds. */
112 int (*reserve)(void *ctx, const struct osc_accept *a);
113 void *ctx;
114 /* Realization receipts (artifact/osc_receipt.h, aienos#383): read only by osc_receipt_verify, never by
115 * osc_unit_admit. Local authority, never taken from a receipt. `release` above also refuses TEST receipts. */
116 const uint8_t (*rcpt_test_anchors)[32]; /* raw Ed25519 public keys, receipt class TEST */
117 unsigned n_rcpt_test;
118 const uint8_t (*rcpt_owner_anchors)[32]; /* receipt class OWNER (the kernel: the owner root, aienos#387) */
119 unsigned n_rcpt_owner;
120 const uint8_t (*rcpt_checkers)[32]; /* accepted checker2 identities; none: every receipt is refused */
121 unsigned n_rcpt_checkers;
122 const uint8_t (*rcpt_revoked_ids)[32]; /* revoked receipt ids (SHA-256 of the 416 bytes) */
123 unsigned n_rcpt_revoked_ids;
124 const uint8_t (*rcpt_revoked_keys)[32]; /* revoked signer key fingerprints (SHA-256 of the public key) */
125 unsigned n_rcpt_revoked_keys;
126 uint64_t rcpt_epoch_floor; /* receipts issued under an older policy epoch are replays */
127 uint64_t rcpt_epoch_now; /* the kernel's current policy epoch (no clock) */
128 const uint8_t *rcpt_target; /* 32-byte target hash; NULL: every receipt is WRONG_TARGET */
129 const uint8_t *rcpt_firmware; /* 32-byte firmware hash; NULL: none (all zero) */
130 const uint8_t *rcpt_producer; /* 32-byte producer identity; NULL: none (all zero) */
131};
132
133/* 0 accepted (*out filled), else the first refusal code. out may be NULL only if the caller
134 * wants the verdict alone; it is written only on acceptance. */
135unsigned osc_unit_admit(const uint8_t *f, size_t len, const struct osc_policy *pol, struct osc_accept *out);
136
137/* Section 6.1.1: exact, case-sensitive, equal-length name match; the hash is never consulted.
138 * Returns the fn_index, or -1 (the caller reports LAUNCH_BAD_ENTRY, 40). */
139int osc_unit_lookup(const struct osc_accept *a, const char *name, size_t name_len);
140
141/* Section 8.4 on one word: nonzero if the word is in the OSC-emitted A64 subset (class-mask
142 * match plus the decoder round trip; brk only with immediate 1..14). Exposed for tests. */
143int osc_a64_word_allowed(uint32_t w);
144
145/* Conformance profile of section 8.1 for unit format 1..5, ABI 1. */
146#define OSC_PROFILE_UNIT_FORMATS 0x3Eu /* bits 1..5 */
147#define OSC_PROFILE_ABI 0x2u /* bit 1 */
148
149#endif