interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / runtime / rx_caproot.h

250 lines · 10007 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/*
18 * rx_caproot.h -- host reference capability root for the resident reaction
19 * runtime (ADR 0016, Part II ยง3; gate R7 is NOT claimed by this file).
20 *
21 * The rule: the shared world may hold references to capabilities, it may
22 * never invent them. Here that rule is enforced by the operating system, not by
23 * convention:
24 *
25 * - The capability table lives in a sealed memfd. The only writable mapping
26 * belongs to a separate, non-dumpable root process (the mint).
27 * - The reaction runtime receives a read-only mapping. The memfd carries
28 * F_SEAL_FUTURE_WRITE | F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL, so no
29 * new writable mapping, write(2), or resize can be obtained from any fd.
30 * - Minting, delegation (attenuation only), revocation, slot reclaim, lease
31 * clock and epoch changes are requests over a socket; the root enforces
32 * every rule and the runtime only observes the result.
33 *
34 * A capability binds: id, slot generation, issuer, subject, resource, rights,
35 * epoch, lease expiry, parent (delegation), delegable flag, revocation state.
36 * A CapabilityRef in the world is only {cap_id, generation}; validation reads
37 * the root's table, so a forged, stale, revoked, expired, wrong-subject,
38 * wrong-resource or amplified reference fails.
39 *
40 * Scope: this is the Linux-hosted development root. It is not the permanent
41 * root and it is not R7. On AIENOS the kernel capability authority is the root
42 * (ADR 0014). This host root only refuses to treat the control socket as
43 * authority:
44 * - mint, revoke, reclaim, epoch, clock and shutdown each require a
45 * capability this mint previously handed to this socket, carrying the
46 * matching right;
47 * - every such request must also present the office token delivered once at
48 * start. That token is not stored in the readable table, so reading the
49 * table and speaking on the socket is not enough;
50 * - revoke reaches a target only when the target's privileged rights are
51 * already held by the presented authority, and it marks descendants
52 * revoked (AIENOS-style cascade);
53 * - privileged rights cannot be delegated;
54 * - counter wrap and a slot whose generation is exhausted fail closed;
55 * - a new root starts above every generation an earlier root in this
56 * process could have reached (see take_boot_gen).
57 */
58
59#ifndef RX_CAPROOT_H
60#define RX_CAPROOT_H
61
62#include <stdatomic.h>
63#include <stdbool.h>
64#include <stdint.h>
65#include <sys/types.h>
66
67#define RX_CAP_MAX 256u
68#define RX_CAP_MAX_DEPTH 8u
69#define RX_CAP_TOKEN_LEN 32u
70#define RX_CAP_TABLE_MAGIC 0x50414358u /* 'XCAP' LE */
71
72/* Control operations. The socket carries these; it does not authorize them. */
73enum {
74 RX_OP_MINT = 1,
75 RX_OP_REVOKE,
76 RX_OP_RECLAIM,
77 RX_OP_CLOCK,
78 RX_OP_EPOCH,
79 RX_OP_SHUTDOWN
80};
81
82/* Rights */
83#define RX_RIGHT_READ 0x1u
84#define RX_RIGHT_WRITE 0x2u
85#define RX_RIGHT_EFFECT 0x4u
86#define RX_RIGHT_DELEGATE 0x8u
87/* Privileged rights. Never delegable. A root-issued cap may carry a subset of
88 * the presented authority's privileged rights, and never DELEGATE as well. */
89#define RX_RIGHT_MINT 0x10u
90#define RX_RIGHT_REVOKE 0x20u
91#define RX_RIGHT_RECLAIM 0x40u
92#define RX_RIGHT_EPOCH 0x80u
93#define RX_RIGHT_CLOCK 0x100u
94/* Lineage transition. The proposer of a candidate does not hold this.
95 * It cannot be combined with DELEGATE or passed on to a child. */
96#define RX_RIGHT_PROMOTE 0x200u
97#define RX_RIGHT_PRIVILEGED (RX_RIGHT_MINT | RX_RIGHT_REVOKE | RX_RIGHT_RECLAIM | \
98 RX_RIGHT_EPOCH | RX_RIGHT_CLOCK | RX_RIGHT_PROMOTE)
99#define RX_RIGHT_KNOWN (RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT | \
100 RX_RIGHT_DELEGATE | RX_RIGHT_PRIVILEGED)
101
102/* Resource id of the bootstrap authority office. Not a world object. */
103#define RX_CAP_RES_AUTHORITY 0ull
104
105/* Slot state */
106#define RX_CAP_FREE 0u
107#define RX_CAP_LIVE 1u
108#define RX_CAP_REVOKED 2u
109
110/* Validation / request results */
111#define RX_CAP_OK 0
112#define RX_CAP_ERR_BOUNDS -1
113#define RX_CAP_ERR_STALE_GEN -2
114#define RX_CAP_ERR_REVOKED -3
115#define RX_CAP_ERR_EPOCH -4
116#define RX_CAP_ERR_SUBJECT -5
117#define RX_CAP_ERR_RESOURCE -6
118#define RX_CAP_ERR_RIGHTS -7
119#define RX_CAP_ERR_EXPIRED -8
120#define RX_CAP_ERR_CHAIN -9
121#define RX_CAP_ERR_AMPLIFY -10
122#define RX_CAP_ERR_NOT_DELEGABLE -11
123#define RX_CAP_ERR_FULL -12
124#define RX_CAP_ERR_IO -13
125#define RX_CAP_ERR_STATE -14
126#define RX_CAP_ERR_UNAUTHORIZED -15
127#define RX_CAP_ERR_OVERFLOW -16
128#define RX_CAP_ERR_EXHAUSTED -17
129
130typedef struct {
131 uint32_t cap_id;
132 uint64_t generation;
133} RxCapRef;
134
135typedef struct {
136 uint32_t cap_id;
137 uint64_t generation;
138 uint32_t state;
139 uint32_t issuer;
140 uint32_t subject;
141 uint32_t rights;
142 uint64_t resource;
143 uint64_t epoch;
144 uint64_t lease_expiry; /* logical clock tick; 0 = no lease */
145 uint32_t parent_id; /* UINT32_MAX = root-issued */
146 uint64_t parent_generation;
147 uint32_t minted_by_id; /* capability that was allowed to create this one */
148 uint64_t minted_by_generation;
149} RxCapEntry;
150
151/* Shared table. Written only by the root process under a seqlock. */
152typedef struct {
153 uint32_t magic;
154 uint32_t capacity;
155 _Atomic uint64_t seq; /* odd while the root is writing */
156 _Atomic uint64_t epoch;
157 _Atomic uint64_t clock;
158 RxCapEntry entries[RX_CAP_MAX];
159} RxCapTable;
160
161/* Runtime validation handle. It intentionally contains no mint token, office
162 * capability, or control socket. Passing this to RxWorld cannot confer
163 * administrative authority. */
164typedef struct {
165 const RxCapTable *table; /* read-only mapping */
166 int ro_fd; /* sealed memfd, kept for attack tests */
167 pid_t root_pid;
168 bool running;
169} RxCapRoot;
170
171/* Privileged host-reference administration handle. Policy/setup code may hold
172 * this; the resident reaction world must not. This split is the host analogue
173 * of keeping intelligence separate from the authority root. */
174typedef struct {
175 int ctl_fd;
176 pid_t root_pid;
177 bool running;
178 RxCapRef office;
179 uint8_t token[RX_CAP_TOKEN_LEN];
180} RxCapAdmin;
181
182/* Mint request. parent.cap_id == UINT32_MAX means a root-issued capability.
183 * authority must be a capability this mint handed to this socket:
184 * root-issued: authority carries MINT
185 * delegation: authority is the parent itself
186 * The socket is transport. Holding it does not mint anything. */
187typedef struct {
188 uint32_t issuer;
189 uint32_t subject;
190 uint64_t resource;
191 uint32_t rights;
192 uint64_t lease_ticks; /* 0 = no lease */
193 RxCapRef parent;
194 RxCapRef authority;
195} RxCapMint;
196
197/* Wire request. Tests may forge one. The library fills `token` from the
198 * secret delivered at start; a caller who only read the table cannot. */
199typedef struct {
200 uint32_t op;
201 uint32_t pad;
202 RxCapMint mint;
203 RxCapRef ref;
204 RxCapRef authority;
205 uint64_t arg;
206 uint8_t token[RX_CAP_TOKEN_LEN];
207} RxCapRequest;
208
209/* Fail closed. A sum that would wrap is refused and *out is left unchanged. */
210static inline int rx_cap_add_u64(uint64_t a, uint64_t b, uint64_t *out) {
211 if (b > UINT64_MAX - a) return RX_CAP_ERR_OVERFLOW;
212 if (out) *out = a + b;
213 return RX_CAP_OK;
214}
215
216/* Match AIENOS: generations are 64 bits and a slot whose generation is
217 * already UINT64_MAX is retired. The old handle is not revived by wrapping. */
218static inline int rx_cap_generation_advance(uint64_t generation, uint64_t *out) {
219 if (generation == UINT64_MAX) return RX_CAP_ERR_EXHAUSTED;
220 if (out) *out = generation + 1u;
221 return RX_CAP_OK;
222}
223
224int rx_caproot_start(RxCapRoot *root, RxCapAdmin *admin);
225void rx_caproot_stop(RxCapRoot *root, RxCapAdmin *admin);
226RxCapRef rx_capadmin_office(const RxCapAdmin *admin);
227
228int rx_capadmin_mint(RxCapAdmin *admin, const RxCapMint *req, RxCapRef *out);
229int rx_capadmin_revoke(RxCapAdmin *admin, RxCapRef authority, RxCapRef ref);
230/* Return a revoked slot to the free pool; its generation advances, so every
231 * outstanding reference to the old occupant becomes stale. Generation
232 * UINT32_MAX refuses and leaves the slot revoked. */
233int rx_capadmin_reclaim(RxCapAdmin *admin, RxCapRef authority, uint32_t cap_id);
234int rx_capadmin_advance_clock(RxCapAdmin *admin, RxCapRef authority, uint64_t ticks);
235int rx_capadmin_bump_epoch(RxCapAdmin *admin, RxCapRef authority);
236
237/* Copy one table entry if the reference generation matches. Does not grant
238 * rights. Used to record who issued a capability into a causal crumb. */
239int rx_caproot_inspect(const RxCapRoot *root, RxCapRef ref, RxCapEntry *out);
240
241/* Validate a reference for (subject, resource, rights). Reads the root table
242 * with a seqlock and walks the delegation chain; any revoked ancestor fails.
243 * On success, *out_entry (optional) receives the entry snapshot. */
244int rx_caproot_validate(const RxCapRoot *root, RxCapRef ref,
245 uint32_t subject, uint64_t resource, uint32_t rights,
246 RxCapEntry *out_entry);
247
248const char *rx_cap_strerror(int code);
249
250#endif /* RX_CAPROOT_H */