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 / x86 / tests / boot_verify_test.c

270 lines · 11286 bytes Raw Blame History
1/* boot_verify_test.c -- the iMac kernel's boot-section install (memory_x86.c boot_section)
2 * must check the owner's signature before any disk write.
3 *
4 * The real memory_x86.c is compiled into this file, with the disk, the GPT, the FAT
5 * installer and the loader hand-over mocked, and the real ck_slot_verify, Ed25519 and
6 * SHA-512 linked. The mocks count: write windows opened (ck_x86_ahci_allow), GPT
7 * partitions added (ck_gpt_add) and installs (ck_fatb_install). Every object the owner
8 * did not sign must leave all three at zero, and so must an owner-signed object whose
9 * generation is below the installed kernel's policy_generation (a replayed old kernel)
10 * or any object when the installed manifest cannot be read. Built for x86-64 (the kernel uses rdtsc).
11 * Keys: the RFC 8032 test vectors 1 and 2, public. No real key, no disk.
12 *
13 * Prints "BOOT_VERIFY: PASS" or "BOOT_VERIFY: FAIL <why>". */
14#include <stdarg.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include MEMORY_X86_C
20
21#include "../../../argus/sha256.h"
22
23extern void bv_set_keys(const uint8_t (*keys)[32], unsigned n);
24
25/* RFC 8032 section 7.1, tests 1 and 2 */
26static const uint8_t SK1[32] = {0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4,
27 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60};
28static const uint8_t SK2[32] = {0x4c, 0xcd, 0x08, 0x9b, 0x28, 0xff, 0x96, 0xda, 0x9d, 0xb6, 0xc3, 0x46, 0xec, 0x11, 0x4e, 0x0f,
29 0x5b, 0x8a, 0x31, 0x9f, 0x35, 0xab, 0xa6, 0x24, 0xda, 0x8c, 0xf6, 0xed, 0x4f, 0xb8, 0xa6, 0xfb};
30
31/* ---- mocks --------------------------------------------------------------- */
32static unsigned n_allow, n_allow_none, n_add, n_install;
33static const uint8_t *inst_man, *inst_img;
34static uint32_t inst_len;
35static struct nb_pass pass;
36static int have_pass;
37static int part_rc; /* what ck_gpt_find_type answers: CK_GPT_OK (a boot partition is there) or CK_GPT_ENOPART */
38/* what ck_fatb_manifest_read answers: CK_FATB_ENOTOURS (nothing installed), CK_FATB_EIO, or
39 * CK_FATB_OK with a manifest of generation inst_gen and policy_generation inst_policy */
40static int man_rc = CK_FATB_ENOTOURS;
41static uint64_t inst_gen, inst_policy;
42static disk_dev fake_disk;
43const uint8_t ck_esp_part_type[16] = {0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11, 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b};
44
45void ck_printf(const char *fmt, ...) { (void)fmt; }
46void ck_puts(const char *s) { (void)s; }
47const struct nb_pass *ck_x86_netldr_pass(int *dropped)
48{
49 *dropped = 0;
50 return have_pass ? &pass : 0;
51}
52int ck_x86_ahci_allow(unsigned index, uint64_t first, uint64_t last)
53{
54 (void)index, (void)first, (void)last;
55 n_allow++;
56 return 0;
57}
58void ck_x86_ahci_allow_none(unsigned index)
59{
60 (void)index;
61 n_allow_none++;
62}
63int ck_gpt_find_type(const disk_dev *d, const uint8_t type[16], ck_part *p, ck_gpt_info *info)
64{
65 (void)d, (void)type;
66 memset(p, 0, sizeof *p);
67 memset(info, 0, sizeof *info);
68 info->first_usable = 34;
69 info->last_usable = 1000000;
70 info->last_lba = 1000033;
71 if (part_rc == CK_GPT_OK || n_add) { /* the disk's own boot partition, or the one just added */
72 p->first_lba = 4096;
73 p->blocks = 524288;
74 p->valid = 1;
75 return CK_GPT_OK;
76 }
77 return part_rc;
78}
79int ck_gpt_is_ours(const disk_dev *d, const ck_gpt_new *spec, uint32_t index)
80{
81 (void)d, (void)spec, (void)index;
82 return 1;
83}
84const char *ck_gpt_add_strerror(int rc)
85{
86 (void)rc;
87 return "mock";
88}
89int ck_gpt_add(const disk_dev *d, const ck_gpt_new *spec, uint64_t blocks, uint64_t align, ck_gpt_added *out)
90{
91 (void)d, (void)spec, (void)blocks, (void)align;
92 n_add++;
93 memset(out, 0, sizeof *out);
94 return CK_GPT_OK;
95}
96int ck_fatb_state_read(const disk_dev *p, uint32_t serial, ck_fatb_state *st)
97{
98 (void)p, (void)serial, (void)st;
99 return CK_FATB_EIO;
100}
101int ck_fatb_manifest_read(const disk_dev *p, uint32_t serial, uint8_t man[CK_FATB_MAN_BYTES])
102{
103 (void)p, (void)serial;
104 memset(man, 0, CK_FATB_MAN_BYTES);
105 if (man_rc != CK_FATB_OK)
106 return man_rc;
107 struct ck_slot_manifest *m = (struct ck_slot_manifest *)man;
108 memcpy(m->magic, CK_SLOT_MANIFEST_MAGIC, 8);
109 m->generation = inst_gen;
110 m->policy_generation = inst_policy;
111 return CK_FATB_OK;
112}
113const char *ck_fatb_strerror(int rc)
114{
115 (void)rc;
116 return "mock";
117}
118void ck_fatb_dos_time(uint64_t unix_s, uint16_t *date, uint16_t *time)
119{
120 (void)unix_s;
121 *date = *time = 0;
122}
123int ck_fatb_install(const disk_dev *p, uint32_t serial, int format_ok, const uint8_t *man, const uint8_t *img,
124 uint32_t img_len, uint16_t dd, uint16_t tt, ck_fatb_result *out)
125{
126 (void)p, (void)serial, (void)format_ok, (void)dd, (void)tt;
127 n_install++;
128 inst_man = man, inst_img = img, inst_len = img_len;
129 memset(out, 0, sizeof *out);
130 out->written = 1;
131 return CK_FATB_OK;
132}
133
134/* ---- objects ------------------------------------------------------------- */
135#define IMG_LEN 3000u
136static uint8_t object[CK_SLOT_UNIT + IMG_LEN];
137
138/* A well-formed manifest and image. sign_with: the secret key that signs it (0: leave the
139 * signature zero). claims: the public key whose id goes in signer_id. */
140static void build(const uint8_t *sign_with, const uint8_t claims_pk[32])
141{
142 struct ck_slot_manifest *m = (struct ck_slot_manifest *)object;
143 uint8_t *img = object + CK_SLOT_UNIT;
144 memset(object, 0, sizeof object);
145 for (unsigned i = 0; i < IMG_LEN; i++)
146 img[i] = (uint8_t)(i * 7u + 1u);
147 memcpy(m->magic, CK_SLOT_MANIFEST_MAGIC, 8);
148 m->version = CK_SLOT_MANIFEST_VERSION;
149 m->payload_kind = CK_SLOT_PAYLOAD_BOOTAA64;
150 m->loader_abi = CK_SLOT_LOADER_ABI;
151 m->kernel_abi = CK_SLOT_KERNEL_ABI;
152 m->generation = 5;
153 m->policy_generation = 5;
154 m->min_rollback_generation = 1;
155 m->image_len = IMG_LEN;
156 aienos_sha512(img, IMG_LEN, m->image_sha512);
157 memcpy(m->kernel_sha512, m->image_sha512, 64);
158 sha256_hash(claims_pk, 32, m->signer_id);
159 memset(m->commit, 'a', sizeof m->commit);
160 memcpy(m->provenance, "boot_verify_test", 16);
161 m->timestamp = 1;
162 if (sign_with && aienos_ed25519_sign(m->signature, object, CK_SLOT_TBS_BYTES, sign_with) != AIENOS_SIG_OK) {
163 printf("BOOT_VERIFY: FAIL the test signer failed\n");
164 exit(1);
165 }
166 pass.loader_tsc = 1, pass.start_tsc = 2;
167 pass.object = (uint64_t)(uintptr_t)object;
168 pass.object_len = sizeof object;
169 have_pass = 1;
170}
171
172static unsigned failures;
173#define EXPECT(cond, ...) \
174 do { \
175 if (!(cond)) { \
176 failures++; \
177 printf("BOOT_VERIFY: FAIL "); \
178 printf(__VA_ARGS__); \
179 printf("\n"); \
180 } \
181 } while (0)
182
183static void run(const char *label, int rc_part, int expect_install)
184{
185 n_allow = n_allow_none = n_add = n_install = 0;
186 inst_man = inst_img = 0;
187 part_rc = rc_part;
188 boot_section(&fake_disk);
189 const char *where = rc_part == CK_GPT_OK ? "existing boot partition" : "no boot partition yet";
190 if (!expect_install) {
191 EXPECT(n_allow == 0, "%s (%s): %u write window(s) opened, want 0", label, where, n_allow);
192 EXPECT(n_add == 0, "%s (%s): %u partition(s) added, want 0", label, where, n_add);
193 EXPECT(n_install == 0, "%s (%s): installed, want refused", label, where);
194 } else {
195 EXPECT(n_install == 1, "%s (%s): %u installs, want exactly 1", label, where, n_install);
196 EXPECT(n_allow >= 1 && n_allow_none >= 1, "%s (%s): window opened %u / closed %u", label, where, n_allow,
197 n_allow_none);
198 EXPECT(inst_man == object && inst_img == object + CK_SLOT_UNIT && inst_len == IMG_LEN,
199 "%s (%s): the installer got other bytes than the verified object", label, where);
200 }
201}
202
203int main(void)
204{
205 uint8_t pk1[32], pk2[32];
206 uint8_t both[2][32];
207 aienos_ed25519_public_key(pk1, SK1);
208 aienos_ed25519_public_key(pk2, SK2);
209 memcpy(both[0], pk1, 32);
210 memcpy(both[1], pk2, 32);
211 fake_disk.block_size = 512;
212 fake_disk.block_count = 2000000;
213
214 const int modes[2] = {CK_GPT_OK, CK_GPT_ENOPART};
215 for (unsigned k = 0; k < 2; k++) {
216 int pm = modes[k];
217 /* the key set holds the owner key 1 for every refusal below */
218 bv_set_keys(both, 1);
219
220 build(0, pk1); /* self-consistent, well formed, SHA-512 matches, never signed */
221 run("unsigned self-consistent manifest", pm, 0);
222
223 build(SK1, pk1);
224 ((uint8_t *)object)[CK_SLOT_UNIT + 17] ^= 0x40; /* image changed after signing */
225 run("tampered image", pm, 0);
226
227 build(SK1, pk1);
228 ((struct ck_slot_manifest *)object)->generation = 6; /* manifest field changed after signing */
229 run("tampered manifest field", pm, 0);
230
231 build(SK2, pk2); /* signed by a key that is not the owner's */
232 run("signed by the wrong owner", pm, 0);
233
234 build(SK2, pk1); /* claims the owner's id, signed by someone else */
235 run("wrong signature under the owner's id", pm, 0);
236
237 bv_set_keys(both, 0); /* an image built without owner keys */
238 build(SK1, pk1);
239 run("valid signature, empty key set", pm, 0);
240
241 bv_set_keys(both, 1);
242 build(SK1, pk1);
243 run("owner-signed", pm, 1);
244 if (pm == CK_GPT_ENOPART)
245 EXPECT(n_add == 1, "owner-signed with no boot partition yet: %u partitions added, want 1", n_add);
246 if (pm == CK_GPT_OK) { /* the object is generation 5, policy_generation 5 */
247 man_rc = CK_FATB_OK;
248 inst_gen = 7, inst_policy = 7;
249 build(SK1, pk1);
250 run("owner-signed but older than the installed floor (replay)", pm, 0);
251 inst_gen = 7, inst_policy = 6;
252 build(SK1, pk1);
253 run("owner-signed below the floor of an installed kernel with a lower floor", pm, 0);
254 inst_gen = 9, inst_policy = 5;
255 build(SK1, pk1);
256 run("owner-signed at the installed floor (rollback the owner allowed)", pm, 1);
257 inst_gen = 3, inst_policy = 3;
258 build(SK1, pk1);
259 run("owner-signed newer than the installed kernel", pm, 1);
260 man_rc = CK_FATB_EIO;
261 build(SK1, pk1);
262 run("installed manifest unreadable", pm, 0);
263 man_rc = CK_FATB_ENOTOURS;
264 }
265 }
266 if (failures)
267 return 1;
268 printf("BOOT_VERIFY: PASS\n");
269 return 0;
270}