aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / tests / setup_secret_test.c
| 1 | /* setup_secret_test.c -- the setup wizard must never send its API key to the courier, and a key |
| 2 | * test must fail locally and erase the key. |
| 3 | * |
| 4 | * The real wizard (svc/setup_wizard.c) and the real iMac adapter (x86/setup_x86.c) are compiled |
| 5 | * into this file. The courier is a mock that counts every line it is asked to send and keeps |
| 6 | * a copy; the screen and the disk record are stubs. The wizard is put at its key-testing step |
| 7 | * with a key held (as the real key typing leaves it), then the adapter's step function runs the |
| 8 | * way the kernel's loop calls it. Built for x86-64 (the adapter reads rdtsc). |
| 9 | * Prints "SETUP_SECRET: PASS" or "SETUP_SECRET: FAIL <why>". */ |
| 10 | #include <stdarg.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include SETUP_X86_C |
| 16 | |
| 17 | /* ---- stubs for what the adapter calls outside the wizard ---- */ |
| 18 | int ck_snprintf(char *buf, size_t n, const char *fmt, ...) |
| 19 | { |
| 20 | va_list ap; |
| 21 | va_start(ap, fmt); |
| 22 | int r = vsnprintf(buf, n, fmt, ap); |
| 23 | va_end(ap); |
| 24 | return r; |
| 25 | } |
| 26 | void ck_printf(const char *fmt, ...) { (void)fmt; } |
| 27 | void ck_puts(const char *s) { (void)s; } |
| 28 | int ck_setup_save(ck_setup_store *st, const char *t, unsigned n) |
| 29 | { |
| 30 | (void)st, (void)t, (void)n; |
| 31 | return 0; |
| 32 | } |
| 33 | int ck_setup_load(ck_setup_store *st, const disk_dev *p, char *t, unsigned cap, unsigned *n) |
| 34 | { |
| 35 | (void)st, (void)p, (void)t, (void)cap, (void)n; |
| 36 | return 1; |
| 37 | } |
| 38 | void ck_allen_ui_input(const char *buf, unsigned len, uint64_t now) { (void)buf, (void)len, (void)now; } |
| 39 | void ck_allen_ui_said(const char *line, const char *reply, uint64_t now) { (void)line, (void)reply, (void)now; } |
| 40 | void ck_allen_ui_panel(const ck_allen_ui_panel_view *p, uint64_t now) { (void)p, (void)now; } |
| 41 | const disk_dev *ck_x86_memory_part(void) { return 0; } |
| 42 | int ck_allen_ui_formed(uint64_t now) |
| 43 | { |
| 44 | (void)now; |
| 45 | return 1; |
| 46 | } |
| 47 | int ck_x86_fwcfg_file(uint64_t rsdp, const char *name, char *buf, unsigned cap, unsigned *n) |
| 48 | { |
| 49 | (void)rsdp, (void)name, (void)buf, (void)cap, (void)n; |
| 50 | return -1; |
| 51 | } |
| 52 | int ck_x86_pci_fn(unsigned i, uint16_t *v, uint16_t *d, uint32_t *c) |
| 53 | { |
| 54 | (void)i, (void)v, (void)d, (void)c; |
| 55 | return -1; |
| 56 | } |
| 57 | const disk_dev *ck_x86_ahci_dev(unsigned i, const char **model) |
| 58 | { |
| 59 | (void)i, (void)model; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* ---- the courier mock ---- */ |
| 64 | static unsigned n_sent; |
| 65 | static char sent_copy[8][512]; |
| 66 | static int m_ready(void) { return 1; } |
| 67 | static int m_send(const char *line, unsigned n) |
| 68 | { |
| 69 | if (n_sent < 8) { |
| 70 | unsigned k = n < sizeof sent_copy[0] - 1 ? n : (unsigned)sizeof sent_copy[0] - 1; |
| 71 | memcpy(sent_copy[n_sent], line, k); |
| 72 | sent_copy[n_sent][k] = 0; |
| 73 | } |
| 74 | n_sent++; |
| 75 | return 0; /* the wire took it */ |
| 76 | } |
| 77 | static int m_take(char *out, unsigned cap) |
| 78 | { |
| 79 | (void)out, (void)cap; |
| 80 | return 0; |
| 81 | } |
| 82 | static const ck_courier mock_wire = {m_ready, m_send, m_take}; |
| 83 | |
| 84 | static unsigned failures; |
| 85 | #define EXPECT(cond, ...) \ |
| 86 | do { \ |
| 87 | if (!(cond)) { \ |
| 88 | failures++; \ |
| 89 | printf("SETUP_SECRET: FAIL "); \ |
| 90 | printf(__VA_ARGS__); \ |
| 91 | printf("\n"); \ |
| 92 | } \ |
| 93 | } while (0) |
| 94 | |
| 95 | #define SECRET "sk-live-0123456789abcdef" |
| 96 | |
| 97 | static int key_bytes_left(const sw_wizard *w) |
| 98 | { |
| 99 | for (unsigned i = 0; i < sizeof w->key; i++) |
| 100 | if (w->key[i]) |
| 101 | return 1; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* The wizard just after the user pressed Enter on a typed key: testing, a request wanted. */ |
| 106 | static void at_key_test(int service, const char *url) |
| 107 | { |
| 108 | static si_inv inv; |
| 109 | memset(&inv, 0, sizeof inv); |
| 110 | memset(&wiz, 0, sizeof wiz); |
| 111 | sw_start(&wiz, &inv, 1, 0, 0, 0); |
| 112 | wiz.step = SW_TESTING; |
| 113 | wiz.test_out = 1; |
| 114 | wiz.service = service; |
| 115 | strcpy(wiz.llm_url, url); |
| 116 | strcpy(wiz.key, SECRET); |
| 117 | wiz.nkey = (unsigned)strlen(SECRET); |
| 118 | n_sent = 0; |
| 119 | memset(sent_copy, 0, sizeof sent_copy); |
| 120 | wire = &mock_wire; |
| 121 | on = 1; |
| 122 | pending = 0; |
| 123 | t0 = ck_rdtsc(); |
| 124 | hz = 1000000000u; |
| 125 | } |
| 126 | |
| 127 | static void run_service(int service, const char *url, const char *name) |
| 128 | { |
| 129 | at_key_test(service, url); |
| 130 | EXPECT(wiz.nkey == strlen(SECRET) && key_bytes_left(&wiz), "%s: setup did not hold the key", name); |
| 131 | ck_x86_setup_step(); |
| 132 | EXPECT(n_sent == 0, "%s: the adapter sent %u line(s) to the courier (first: '%.12s...')", name, n_sent, |
| 133 | sent_copy[0]); |
| 134 | for (unsigned i = 0; i < n_sent && i < 8; i++) |
| 135 | EXPECT(!strstr(sent_copy[i], SECRET) && strncmp(sent_copy[i], "KEYTEST", 7), "%s: a key test line went out", name); |
| 136 | EXPECT(wiz.nkey == 0 && !key_bytes_left(&wiz), "%s: the key is still held after the local refusal (nkey %u)", name, wiz.nkey); |
| 137 | EXPECT(sw_step_of(&wiz) == SW_TEST_FAILED, "%s: step is %s, want TEST_FAILED", name, sw_step_name(sw_step_of(&wiz))); |
| 138 | EXPECT(wiz.test_out == 0, "%s: a test is still marked outstanding", name); |
| 139 | const sw_view *v = sw_view_of(&wiz); |
| 140 | EXPECT(!strstr(v->say, SECRET) && !strstr(v->typed, SECRET), "%s: the screen shows the key", name); |
| 141 | EXPECT(wiz.why[0] != 0, "%s: no reason is shown for the refusal", name); |
| 142 | /* the key is not in the wizard struct at all */ |
| 143 | EXPECT(memmem(&wiz, sizeof wiz, SECRET, sizeof SECRET - 1) == 0, "%s: the key is somewhere in the wizard state", name); |
| 144 | /* a second pass does nothing more */ |
| 145 | ck_x86_setup_step(); |
| 146 | EXPECT(n_sent == 0, "%s: a later pass sent a line", name); |
| 147 | } |
| 148 | |
| 149 | static void late_replies(void) |
| 150 | { |
| 151 | /* an old KEYOK / KEYBAD from a host helper must not change the wizard or leak: it is taken and dropped */ |
| 152 | at_key_test(SW_LLM_OPENAI, "http://127.0.0.1:18091"); |
| 153 | wiz.test_out = 2; /* a test counted as outstanding */ |
| 154 | static sw_wizard before; |
| 155 | memcpy(&before, &wiz, sizeof wiz); |
| 156 | EXPECT(ck_x86_setup_line("KEYOK 1 some-model") == 1, "a KEYOK line was passed on as an ordinary answer"); |
| 157 | EXPECT(ck_x86_setup_line("KEYBAD 1 nope") == 1, "a KEYBAD line was passed on as an ordinary answer"); |
| 158 | EXPECT(!memcmp(&before, &wiz, sizeof wiz), "an old key-test reply changed the wizard"); |
| 159 | EXPECT(ck_x86_setup_line("SAY hello") == 0, "an ordinary answer line was swallowed"); |
| 160 | } |
| 161 | |
| 162 | int main(void) |
| 163 | { |
| 164 | run_service(SW_LLM_OPENAI, "http://127.0.0.1:18091", "openai-style"); |
| 165 | run_service(SW_LLM_CLAUDE, "https://api.anthropic.com", "claude"); |
| 166 | run_service(SW_LLM_OTHER, "http://192.168.1.50:8000", "another service"); |
| 167 | late_replies(); |
| 168 | if (failures) |
| 169 | return 1; |
| 170 | printf("SETUP_SECRET: PASS\n"); |
| 171 | return 0; |
| 172 | } |