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 / svc / continuity_recovery.c

334 lines · 12221 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/* continuity_recovery.c -- see continuity_recovery.h. */
18#include <stdio.h>
19#include <string.h>
20
21#include "continuity_recovery.h"
22
23#define UNIT SV1_UNIT
24
25static int is_zero(const uint8_t *p, size_t n)
26{
27 uint8_t acc = 0;
28 for (size_t i = 0; i < n; i++) acc |= p[i];
29 return acc == 0;
30}
31
32/* Sealed mount refusals (store_sealed.h SS_E_*, -301..-314). */
33static int is_sealed_error(int rc) { return rc <= SS_E_ARG && rc >= SS_E_FORMAT_VERSION; }
34
35int rc_inspect(const struct rc_env *e, struct rc_record *rec)
36{
37 if (!e || !rec || !e->dev || !e->anchor || !e->keys || !e->ws || !e->store || !e->w || !e->view ||
38 !e->dev->read_unit)
39 return -1;
40 static uint8_t raw[2][UNIT]; /* single-threaded kernel scratch, like cr_work */
41 memset(rec, 0, sizeof *rec);
42 for (unsigned s = 0; s < 2; s++) {
43 memset(raw[s], 0, UNIT);
44 rec->slot[s] = RC_SLOT_READ_ERROR;
45 if (e->dev->read_unit(e->dev->ctx, s, raw[s]) != 0) {
46 memset(raw[s], 0, UNIT); /* recovery_core.rs:109-133: an unreadable unit hashes as zero */
47 continue;
48 }
49 if (is_zero(raw[s], UNIT)) {
50 rec->slot[s] = RC_SLOT_ZERO;
51 continue;
52 }
53 sv1_superblock sb;
54 if (sv1_superblock_decode(raw[s], UNIT, s, &sb) == 0) {
55 rec->slot[s] = RC_SLOT_SUPERBLOCK;
56 rec->slot_generation[s] = sb.generation;
57 if (!rec->have_uuid) {
58 rec->have_uuid = 1;
59 memcpy(rec->uuid, sb.store_uuid, 16);
60 }
61 } else {
62 rec->slot[s] = RC_SLOT_UNDECODABLE;
63 }
64 }
65 cr_state_digest(raw[0], raw[1], rec->state_digest);
66
67 /* Mount without writing (ss_open never writes). */
68 memset(e->store, 0, sizeof *e->store);
69 int rc = ss_open(e->store, e->dev, e->anchor, e->anchor_lba, e->keys, e->ws);
70 if (rc != 0) {
71 rec->mount_rc = rc;
72 rec->reason = rc == ST_M_UNFORMATTED ? RC_STORE_UNFORMATTED
73 : is_sealed_error(rc) ? RC_SEALED_REFUSAL
74 : RC_STORE_MOUNT;
75 return 0;
76 }
77 rec->have_mount = 1;
78 rec->mount_state = e->store->st.state;
79 rec->peer = e->store->st.peer;
80 rec->generation = ss_generation(e->store);
81
82 struct cr_source src;
83 cr_bind_sealed(&src, e->store);
84 uint32_t n = src.count(src.ctx);
85 rec->have_catalog = 1;
86 for (uint32_t i = 0; i < n; i++) {
87 uint16_t kind = 0, ver = 0;
88 if (src.entry(src.ctx, i, &kind, &ver) != 0) continue;
89 if (kind == CC_KIND_AGENT_ROOT)
90 rec->n_roots++;
91 else if (kind == CC_KIND_MANIFEST)
92 rec->n_manifests++;
93 else
94 rec->n_other++;
95 }
96
97 const char *why = NULL;
98 int srcrc = 0;
99 int o = cr_resolve(&src, e->w, e->view, &why, &srcrc);
100#ifdef RC_MUTANT_DEGRADED_IS_UNPROVISIONED
101 /* MR-5 (the fe2c2bd bug): continuity decides first, a degraded mount that resolves no identity
102 * reads as Unprovisioned. */
103 if (o == CR_UNPROVISIONED) {
104 rec->reason = RC_UNPROVISIONED;
105 rec->have_identity = 0;
106 return 0;
107 }
108#endif
109 if (rec->mount_state == ST_DEGRADED_RECOVERY) {
110 /* Degraded takes precedence (INV-14): what the valid (older) root says about
111 * continuity is not the whole story. */
112 rec->reason = RC_DEGRADED;
113 } else if (o == CR_RESOLVED) {
114 rec->reason = RC_REASON_NONE;
115 } else if (o == CR_UNPROVISIONED) {
116 rec->reason = RC_UNPROVISIONED;
117 } else if (o == CR_CONFLICT) {
118 rec->reason = RC_CONFLICT;
119 } else if (o == CR_CORRUPT && why) {
120 rec->reason = RC_CONTINUITY_CORRUPT;
121 rec->why = why;
122 } else {
123 rec->reason = RC_CONTINUITY_CORRUPT;
124 rec->why = "continuity unreadable";
125 }
126 if (o == CR_RESOLVED) {
127 rec->have_identity = 1;
128 memcpy(rec->agent_id, e->view->root.agent_id, 32);
129 }
130#ifdef RC_MUTANT_INSPECT_WRITES
131 /* MR-1: "repair on inspect". */
132 if (rec->reason == RC_DEGRADED && rec->peer == ST_PEER_MALFORMED && e->dev->write_unit) {
133 static const uint8_t zero[UNIT];
134 (void)e->dev->write_unit(e->dev->ctx, 1u - st_active_slot(&e->store->st), zero);
135 }
136#endif
137 return 0;
138}
139
140int rc_applicable(const struct rc_record *rec)
141{
142 if (!rec || !rec->have_mount) return 0;
143 switch (rec->reason) {
144 case RC_DEGRADED:
145#ifdef RC_MUTANT_REPAIR_NO_IDENTITY
146 if (rec->peer == ST_PEER_MALFORMED) return CR_ACTION_REPAIR_DEGRADED_PEER;
147#else
148 if (rec->peer == ST_PEER_MALFORMED && rec->have_identity) return CR_ACTION_REPAIR_DEGRADED_PEER;
149#endif
150 return 0;
151 case RC_UNPROVISIONED:
152#ifdef RC_MUTANT_DEGRADED_IS_UNPROVISIONED
153 return CR_ACTION_PROVISION_IDENTITY;
154#else
155 if (rec->mount_state == ST_VALID) return CR_ACTION_PROVISION_IDENTITY;
156 return 0;
157#endif
158 default:
159 return 0;
160 }
161}
162
163int rc_challenge(const struct rc_record *rec, uint8_t action, uint8_t out[32])
164{
165 if (!rec || !rec->have_uuid || !rec->have_mount) return 0;
166 cr_challenge(rec->uuid, rec->generation, action, rec->state_digest, out);
167 return 1;
168}
169
170const char *rc_outcome_name(int rc)
171{
172 switch (rc) {
173 case RC_OK: return "Ok";
174 case RC_NOT_APPLICABLE: return "NotApplicable";
175 case RC_UNAUTHORISED: return "Unauthorised";
176 case RC_IO: return "Io";
177 case RC_CONTINUITY: return "Continuity";
178 case RC_E_ARG: return "BadArgument";
179 default: return "?";
180 }
181}
182
183/* recovery_core.rs:219-235: applicable first, then the operator response. */
184static int authorise(const struct rc_record *rec, uint8_t action, const uint8_t key[32],
185 const uint8_t response[32])
186{
187 if (rc_applicable(rec) != action) return RC_NOT_APPLICABLE;
188 uint8_t ch[32];
189 if (!rc_challenge(rec, action, ch)) return RC_NOT_APPLICABLE;
190 if (!cr_operator_verify(key, ch, response)) return RC_UNAUTHORISED;
191 return RC_OK;
192}
193
194static int env_ok(const struct rc_env *e) { return e && e->tw && e->dev && e->store; }
195
196int rc_repair_degraded_peer(const struct rc_env *e, const uint8_t operator_key[32],
197 const uint8_t response[32], struct rc_record *after)
198{
199 if (!env_ok(e) || !operator_key || !response || !e->dev->write_unit || !e->dev->flush) return RC_E_ARG;
200 struct rc_record rec;
201 if (rc_inspect(e, &rec) != 0) return RC_E_ARG;
202 int a = authorise(&rec, CR_ACTION_REPAIR_DEGRADED_PEER, operator_key, response);
203 if (a != RC_OK) return a;
204 /* The valid root is the slot the engine mounted; the other one goes (INV-17). */
205 uint32_t active = st_active_slot(&e->store->st);
206#ifdef RC_MUTANT_REPAIR_ACTIVE
207 uint32_t victim = active; /* MR-6 */
208#else
209 uint32_t victim = 1u - active;
210#endif
211 if (active > 1u) return RC_IO;
212 static const uint8_t zero[UNIT];
213 if (e->dev->write_unit(e->dev->ctx, victim, zero) != 0) return RC_IO;
214 if (e->dev->flush(e->dev->ctx) != 0) return RC_IO;
215 if (after) (void)rc_inspect(e, after);
216 return RC_OK;
217}
218
219int rc_provision_identity(const struct rc_env *e, const uint8_t operator_key[32],
220 const uint8_t response[32], struct ck_rng *rng, struct cr_view *out,
221 int *cr_outcome, const char **why, int *store_rc)
222{
223 if (!env_ok(e) || !e->w || !operator_key || !response || !rng || !out) return RC_E_ARG;
224 struct rc_record rec;
225 if (rc_inspect(e, &rec) != 0) return RC_E_ARG;
226 int a = authorise(&rec, CR_ACTION_PROVISION_IDENTITY, operator_key, response);
227 if (a != RC_OK) return a;
228 if (!rec.have_uuid) return RC_NOT_APPLICABLE;
229 /* inspection left the Store open at exactly the state that was authorised */
230 struct cr_source src;
231 struct cr_sink snk;
232 struct cr_sealed_sink sk = {e->store, NULL, NULL};
233 cr_bind_sealed(&src, e->store);
234 cr_bind_sealed_sink(&snk, &sk);
235#ifdef RC_MUTANT_PROVISION_QUALIFICATION
236 uint8_t source = CC_SOURCE_QUALIFICATION; /* MR-9 */
237#else
238 uint8_t source = CC_SOURCE_OPERATOR; /* INV-18 */
239#endif
240 int o = cr_provision(&src, &snk, e->w, e->tw, rng, rec.uuid, source, out, why, store_rc);
241 if (cr_outcome) *cr_outcome = o;
242 return o == CR_RESOLVED ? RC_OK : RC_CONTINUITY;
243}
244
245/* ---- markers ---- */
246
247const char *rc_peer_name(int peer)
248{
249 switch (peer) {
250 case ST_PEER_ZERO: return "Zero";
251 case ST_PEER_VALID: return "Valid";
252 case ST_PEER_MALFORMED: return "Malformed";
253 case ST_PEER_GRAPH_BAD_NEWER: return "GraphBadNewer";
254 case ST_PEER_GRAPH_BAD_OLDER: return "GraphBadOlder";
255 default: return "?";
256 }
257}
258
259static size_t fin(char *out, size_t cap, int n)
260{
261 if (n < 0 || (size_t)n >= cap) {
262 if (cap) out[0] = 0;
263 return 0;
264 }
265 return (size_t)n;
266}
267
268size_t rc_reason_text(char *out, size_t cap, const struct rc_record *r)
269{
270 if (!out || !cap || !r) return 0;
271 switch (r->reason) {
272 case RC_STORE_UNFORMATTED: return fin(out, cap, snprintf(out, cap, "StoreUnformatted"));
273 case RC_STORE_MOUNT: return fin(out, cap, snprintf(out, cap, "StoreMount(%d)", r->mount_rc));
274 case RC_SEALED_REFUSAL: return fin(out, cap, snprintf(out, cap, "SealedRefusal(%d)", r->mount_rc));
275 case RC_UNPROVISIONED: return fin(out, cap, snprintf(out, cap, "Unprovisioned"));
276 case RC_CONFLICT: return fin(out, cap, snprintf(out, cap, "Conflict"));
277 case RC_CONTINUITY_CORRUPT:
278 return fin(out, cap, snprintf(out, cap, "ContinuityCorrupt(\"%s\")", r->why ? r->why : ""));
279 case RC_DEGRADED: return fin(out, cap, snprintf(out, cap, "Degraded(%s)", rc_peer_name(r->peer)));
280 default: return fin(out, cap, snprintf(out, cap, "None"));
281 }
282}
283
284size_t rc_marker_entry(char *out, size_t cap, const struct rc_record *r)
285{
286 if (!out || !cap || !r) return 0;
287 if (r->reason == RC_REASON_NONE) return fin(out, cap, snprintf(out, cap, "RECOVERY_CORE: NOT_NEEDED"));
288 char t[160];
289 if (!rc_reason_text(t, sizeof t, r)) return 0;
290 return fin(out, cap, snprintf(out, cap, "RECOVERY_CORE: ENTERED reason=%s", t));
291}
292
293static const char *action_name(uint8_t a)
294{
295 return a == CR_ACTION_REPAIR_DEGRADED_PEER ? "repair-degraded-peer"
296 : a == CR_ACTION_PROVISION_IDENTITY ? "provision-identity"
297 : "?";
298}
299
300static void hex(const uint8_t *p, size_t n, char *o)
301{
302 static const char H[] = "0123456789abcdef";
303 for (size_t i = 0; i < n; i++) {
304 o[2 * i] = H[p[i] >> 4];
305 o[2 * i + 1] = H[p[i] & 15];
306 }
307 o[2 * n] = 0;
308}
309
310size_t rc_marker_challenge(char *out, size_t cap, const struct rc_record *r, uint8_t action)
311{
312 uint8_t ch[32];
313 char h[65];
314 if (!out || !cap || !rc_challenge(r, action, ch)) return 0;
315 hex(ch, 32, h);
316 return fin(out, cap, snprintf(out, cap, "RECOVERY_CHALLENGE: action=%s challenge=%s", action_name(action), h));
317}
318
319size_t rc_marker_refused(char *out, size_t cap, int rc, int cr_outcome)
320{
321 if (!out || !cap) return 0;
322 if (rc == RC_CONTINUITY)
323 return fin(out, cap, snprintf(out, cap, "RECOVERY_REFUSED (Continuity(%s))", cr_outcome_name(cr_outcome)));
324 return fin(out, cap, snprintf(out, cap, "RECOVERY_REFUSED (%s)", rc_outcome_name(rc)));
325}
326
327size_t rc_marker_done(char *out, size_t cap, uint8_t action, const uint8_t *agent)
328{
329 char h[65];
330 if (!out || !cap) return 0;
331 if (!agent) return fin(out, cap, snprintf(out, cap, "RECOVERY_ACTION: %s DONE", action_name(action)));
332 hex(agent, 32, h);
333 return fin(out, cap, snprintf(out, cap, "RECOVERY_ACTION: %s DONE agent=%s", action_name(action), h));
334}