Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/artifact/admission.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / artifact / admission.c` 184 lines ยท 6799 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 /* admission.c -- the boot admission policy and its evaluation (port of 18 * aienos-kernel admission.rs and the boot policy of artifact_loader.rs). */ 19 #include "ck_artifact.h" 20 #include "sha256.h" 21 22 #define POLICY_HEADER 64u 23 24 void cka_boot_policy(struct cka_policy *pol, const uint8_t (*anchors)[32], unsigned nanchors) 25 { 26 struct cka_limits l = {64, 64, 16, 16, 0, 0, 1000000000ull, 1000000000ull, 65536}; 27 struct cka_cap rule = {CKA_KIND_OBJECT, 1, 1, 1, 16, 32, 0, 32}; 28 pol->limits = l; 29 pol->nrules = 1; 30 pol->rules[0] = rule; 31 if (nanchors > CKA_POLICY_MAX_SIGNERS) 32 nanchors = CKA_POLICY_MAX_SIGNERS; 33 pol->nsigners = nanchors; 34 for (unsigned i = 0; i < nanchors; i++) 35 sha256_hash(anchors[i], 32, pol->signers[i]); 36 } 37 38 size_t cka_policy_canonical(const struct cka_policy *pol, uint8_t *o, size_t cap) 39 { 40 size_t need = POLICY_HEADER + (size_t)CKA_CAP_SIZE * pol->nrules + 32u * pol->nsigners; 41 if (cap < need) 42 return 0; 43 for (size_t i = 0; i < POLICY_HEADER; i++) 44 o[i] = 0; 45 static const char magic[8] = "AIENPOL"; 46 for (int i = 0; i < 8; i++) 47 o[i] = (uint8_t)magic[i]; 48 cka_wr16(o + 10, POLICY_HEADER); 49 cka_wr16(o + 12, CKA_CAP_SIZE); 50 cka_wr16(o + 14, (uint16_t)pol->nrules); 51 const struct cka_limits *l = &pol->limits; 52 cka_wr32(o + 16, l->code_pages); 53 cka_wr32(o + 20, l->data_pages); 54 cka_wr32(o + 24, l->stack_pages); 55 cka_wr32(o + 28, l->max_caps); 56 cka_wr32(o + 32, l->ipc_msgs); 57 cka_wr32(o + 36, l->ipc_bytes); 58 cka_wr64(o + 40, l->cpu); 59 cka_wr64(o + 48, l->elapsed); 60 cka_wr32(o + 56, l->syscalls); 61 cka_wr16(o + 60, (uint16_t)pol->nsigners); 62 size_t off = POLICY_HEADER; 63 for (unsigned i = 0; i < pol->nrules; i++, off += CKA_CAP_SIZE) 64 cka_cap_encode(&pol->rules[i], o + off); 65 for (unsigned i = 0; i < pol->nsigners; i++, off += 32) 66 for (int k = 0; k < 32; k++) 67 o[off + k] = pol->signers[i][k]; 68 return off; 69 } 70 71 void cka_policy_digest(const struct cka_policy *pol, uint8_t out[32]) 72 { 73 uint8_t buf[POLICY_HEADER + CKA_CAP_SIZE * CKA_POLICY_MAX_RULES + 32 * CKA_POLICY_MAX_SIGNERS]; 74 size_t n = cka_policy_canonical(pol, buf, sizeof buf); 75 static const char dom[] = "AIENOS-ADMISSION-POLICY-V1"; 76 cka_digest_domain(dom, sizeof dom, buf, n, out); 77 } 78 79 static int over(const struct cka_env *e, const struct cka_limits *l) 80 { 81 return e->code_pages > l->code_pages || e->data_pages > l->data_pages || 82 e->stack_pages > l->stack_pages || (uint32_t)e->max_caps > l->max_caps || 83 e->ipc_msgs > l->ipc_msgs || e->ipc_bytes > l->ipc_bytes || e->cpu > l->cpu || 84 e->elapsed > l->elapsed || e->syscalls > l->syscalls; 85 } 86 87 /* intersect_request: 1 = grant written, 0 = skip, <0 = -error */ 88 static int intersect(const struct cka_cap *req, const struct cka_cap *rule, struct cka_cap *g) 89 { 90 uint32_t rights = req->rights & rule->rights; 91 if (!rights) 92 return 0; 93 uint32_t ops = req->max_ops < rule->max_ops ? req->max_ops : rule->max_ops; 94 uint64_t bytes = req->max_bytes < rule->max_bytes ? req->max_bytes : rule->max_bytes; 95 uint64_t off, len; 96 if (req->bounds == 1) { 97 uint64_t rend = req->off + req->len, pend = rule->off + rule->len; 98 if (rend < req->off || pend < rule->off) 99 return -CKA_LENGTH_OVERFLOW; 100 uint64_t start = req->off > rule->off ? req->off : rule->off; 101 uint64_t end = rend < pend ? rend : pend; 102 if (start >= end) 103 return 0; 104 off = start; 105 len = end - start; 106 if (len < bytes) 107 bytes = len; 108 } else if (req->bounds == 2) { 109 off = len = 0; 110 } else { 111 return -CKA_MALFORMED_CAP; 112 } 113 if (!ops || !bytes) 114 return 0; 115 g->kind = req->kind; 116 g->id = req->id; 117 g->rights = rights; 118 g->bounds = req->bounds; 119 g->max_ops = ops; 120 g->max_bytes = bytes; 121 g->off = off; 122 g->len = len; 123 int rc = cka_cap_validate(g); 124 return rc ? -rc : 1; 125 } 126 127 int cka_evaluate(const struct cka_policy *pol, const struct cka_parsed *p, 128 const uint8_t signer_fp[32], const struct cka_limits *avail, 129 struct cka_decision *d) 130 { 131 d->ngrants = 0; 132 if (p->target != CKA_TARGET_AARCH64_LE) 133 return CKA_WRONG_TARGET; 134 if (p->abi != CKA_ABI_V1) 135 return CKA_WRONG_ABI; 136 int known = 0; 137 for (unsigned i = 0; i < pol->nsigners && !known; i++) { 138 uint8_t acc = 0; 139 for (int k = 0; k < 32; k++) 140 acc |= (uint8_t)(pol->signers[i][k] ^ signer_fp[k]); 141 known = acc == 0; 142 } 143 if (!known) 144 return CKA_UNTRUSTED_SIGNER; 145 if (p->entry & 3) 146 return CKA_BAD_ENTRY; 147 if (p->entry > 0xffffffffu - 4) 148 return CKA_LENGTH_OVERFLOW; 149 if (p->entry + 4 > p->code.flen) 150 return CKA_BAD_ENTRY; 151 if (p->payload_len > CKA_MAX_PAYLOAD) 152 return CKA_RESOURCE_LIMIT; 153 int rc = cka_env_validate(&p->env, p->ncaps); 154 if (rc) 155 return rc; 156 if (over(&p->env, &pol->limits)) 157 return CKA_RESOURCE_LIMIT; 158 if (p->env.code_pages > 0xffffffffu / 4096 || p->env.data_pages > 0xffffffffu / 4096 || 159 p->env.stack_pages > 0xffffffffu / 4096) 160 return CKA_LENGTH_OVERFLOW; 161 if (over(&p->env, avail)) 162 return CKA_RESOURCE_LIMIT; 163 for (unsigned i = 0; i < p->ncaps; i++) { 164 const struct cka_cap *req = &p->caps[i], *rule = 0; 165 for (unsigned r = 0; r < pol->nrules && !rule; r++) 166 if (pol->rules[r].kind == req->kind && pol->rules[r].id == req->id) 167 rule = &pol->rules[r]; 168 if (!rule) 169 continue; 170 struct cka_cap g; 171 int got = intersect(req, rule, &g); 172 if (got < 0) 173 return -got; 174 if (!got) 175 continue; 176 if (!cka_cap_is_attenuation_of(&g, req) || !cka_cap_is_attenuation_of(&g, rule)) 177 return CKA_RIGHTS_ESCALATION; 178 if (g.rights > 0xff) 179 return CKA_RIGHTS_ESCALATION; 180 d->grants[d->ngrants++] = g; 181 } 182 cka_grants_digest(d->grants, d->ngrants, d->granted_digest); 183 return CKA_OK; 184 }