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 / courier_secret_test.c
| 1 | /* courier_secret_test.c -- the Ethernet courier must never carry or keep the wizard's API key. |
| 2 | * |
| 3 | * The real courier (x86/courier_udp_x86.c) is compiled into this file with -DCK_COURIER_HOST, |
| 4 | * so the test reads its static job buffer. The clock, the link and the frames are mocks: every |
| 5 | * frame the courier sends is unwrapped by the same net library the kernel uses and recorded; |
| 6 | * the frames it reads are built here. Checks: |
| 7 | * 1. a "KEYTEST" line (empty, with a space, or a whole key test) is refused before anything is |
| 8 | * sent, and leaves an earlier job's bytes, counters and resend timer untouched; |
| 9 | * 2. a normal job is sent, and sent again every 2 s until it is answered; |
| 10 | * 3. an ACK or a first LINE with the job's own number clears every byte of the job buffer and |
| 11 | * ends the resends; a wrong number, or another boot's run id, does not clear it; |
| 12 | * 4. a new init clears a job still held. |
| 13 | * Prints "COURIER_SECRET: PASS" or "COURIER_SECRET: FAIL <why>". */ |
| 14 | #define _GNU_SOURCE |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include COURIER_X86_C |
| 20 | |
| 21 | typedef struct { |
| 22 | uint64_t now; |
| 23 | unsigned sent, jobs, hellos, hits; /* frames out; JOB frames; HELLO frames; frames holding `needle` */ |
| 24 | const char *needle; |
| 25 | char in[8][1400]; /* datagrams waiting to be read, as wire text */ |
| 26 | size_t in_n[8]; |
| 27 | unsigned in_cnt, in_pos; |
| 28 | } t_dev; |
| 29 | |
| 30 | static t_dev dev; |
| 31 | |
| 32 | static int t_send(void *ctx, const uint8_t *fr, size_t n) |
| 33 | { |
| 34 | t_dev *d = ctx; |
| 35 | net_eth_frame e; |
| 36 | net_ipv4_header ih; |
| 37 | net_udp_header uh; |
| 38 | const uint8_t *ipp, *data; |
| 39 | size_t ipn, dn; |
| 40 | if (net_eth_parse(fr, n, &e) != NET_OK || net_ipv4_parse(e.payload, e.payload_len, &ih, &ipp, &ipn) != NET_OK || |
| 41 | net_udp_parse(ipp, ipn, ih.source, ih.destination, &uh, &data, &dn) != NET_OK) |
| 42 | return -1; |
| 43 | d->sent++; |
| 44 | if (dn > 48 && !memcmp(data + 38, " kind=JOB ", 10)) |
| 45 | d->jobs++; |
| 46 | if (dn > 48 && !memcmp(data + 38, " kind=HELLO ", 12)) |
| 47 | d->hellos++; |
| 48 | if (d->needle && memmem(data, dn, d->needle, strlen(d->needle))) |
| 49 | d->hits++; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static size_t t_recv(void *ctx, uint8_t *fr, size_t cap) |
| 54 | { |
| 55 | t_dev *d = ctx; |
| 56 | static uint8_t udp[1500], ipb[1600]; |
| 57 | if (d->in_pos >= d->in_cnt) |
| 58 | return 0; |
| 59 | size_t k = d->in_pos++, uw = 0, iw = 0, ew = 0; |
| 60 | const net_ipv4 src = {{192, 168, 1, 108}}, dst = {{255, 255, 255, 255}}; |
| 61 | net_udp_header uh = {CK_COURIER_PORT, CK_COURIER_PORT}; |
| 62 | net_ipv4_header ih = {0}; |
| 63 | ih.ttl = 64; |
| 64 | ih.protocol = NET_IPPROTO_UDP; |
| 65 | ih.source = src; |
| 66 | ih.destination = dst; |
| 67 | if (net_udp_build(&uh, (const uint8_t *)d->in[k], d->in_n[k], src, dst, udp, sizeof udp, &uw) != NET_OK || |
| 68 | net_ipv4_build(&ih, udp, uw, ipb, sizeof ipb, &iw) != NET_OK) |
| 69 | return 0; |
| 70 | net_eth_frame e; |
| 71 | memset(e.destination.b, 0xff, 6); |
| 72 | memset(e.source.b, 0x11, 6); |
| 73 | e.ethertype = NET_ETHERTYPE_IPV4; |
| 74 | e.payload = ipb; |
| 75 | e.payload_len = iw; |
| 76 | return net_eth_build(&e, fr, cap, &ew) == NET_OK ? ew : 0; |
| 77 | } |
| 78 | |
| 79 | static int t_link(void *ctx) |
| 80 | { |
| 81 | (void)ctx; |
| 82 | return 1; |
| 83 | } |
| 84 | static uint64_t t_now(void *ctx) { return ((t_dev *)ctx)->now; } |
| 85 | |
| 86 | static const ck_netio io_ = {&dev, t_send, t_recv, t_link, t_now}; |
| 87 | static const uint8_t t_mac[6] = {0x00, 0x1f, 0xf3, 0x12, 0x34, 0x56}, t_none[4] = {0, 0, 0, 0}, |
| 88 | t_peer[4] = {255, 255, 255, 255}; |
| 89 | static const ck_courier *c; |
| 90 | |
| 91 | static unsigned failures; |
| 92 | #define EXPECT(cond, ...) \ |
| 93 | do { \ |
| 94 | if (!(cond)) { \ |
| 95 | failures++; \ |
| 96 | printf("COURIER_SECRET: FAIL "); \ |
| 97 | printf(__VA_ARGS__); \ |
| 98 | printf("\n"); \ |
| 99 | } \ |
| 100 | } while (0) |
| 101 | |
| 102 | static void fresh(void) |
| 103 | { |
| 104 | memset(&dev, 0, sizeof dev); |
| 105 | dev.now = 1000; |
| 106 | c = ck_courier_udp_init(&io_, t_mac, t_none, t_peer, CK_COURIER_PORT, 1000); |
| 107 | } |
| 108 | |
| 109 | /* A datagram from the far side for this boot's run id (or run_override), read at the next pump. */ |
| 110 | static void inject(const char *kind, uint32_t seq, unsigned part, const char *body, uint64_t run_override) |
| 111 | { |
| 112 | char *p = dev.in[dev.in_cnt]; |
| 113 | int n = snprintf(p, sizeof dev.in[0], "ALLEN-COURIER v=1 run=%016llx kind=%s seq=%u part=%u/1 bytes=%zu\n%s", |
| 114 | (unsigned long long)(run_override ? run_override : run), kind, (unsigned)seq, part, strlen(body), body); |
| 115 | dev.in_n[dev.in_cnt++] = (size_t)n; |
| 116 | } |
| 117 | |
| 118 | static void pump_for(uint64_t ms) |
| 119 | { |
| 120 | dev.now += ms; |
| 121 | char line[CK_COURIER_LINE_MAX]; |
| 122 | c->take(line, sizeof line); |
| 123 | } |
| 124 | |
| 125 | static int all_zero(void) |
| 126 | { |
| 127 | for (unsigned i = 0; i < sizeof job; i++) |
| 128 | if (job[i]) |
| 129 | return 0; |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | #define SECRET "sk-live-0123456789abcdef" |
| 134 | |
| 135 | static void keytest_refused(void) |
| 136 | { |
| 137 | static const char *const forms[] = {"KEYTEST", "KEYTEST ", "KEYTEST 1 openai https://api.example " SECRET, |
| 138 | "KEYTEST 7 anthropic - " SECRET "\nmore", "KEYTESTS x", |
| 139 | "KEYTEST\t1 " SECRET, "KEYTEST\n1 " SECRET}; |
| 140 | /* with an earlier job held, and with nothing held */ |
| 141 | for (int held = 0; held < 2; held++) { |
| 142 | for (unsigned f = 0; f < sizeof forms / sizeof *forms; f++) { |
| 143 | fresh(); |
| 144 | if (held) |
| 145 | EXPECT(c->send("chat hello allen", 16) == 0, "the normal job was not taken"); |
| 146 | static char snap[sizeof job]; |
| 147 | memcpy(snap, job, sizeof job); |
| 148 | unsigned jn = job_n, jp = job_parts, so = st_out, sent = dev.sent; |
| 149 | uint32_t js = job_seq; |
| 150 | int jo = job_out, ja = job_acked; |
| 151 | dev.needle = SECRET; |
| 152 | int rc = c->send(forms[f], (unsigned)strlen(forms[f])); |
| 153 | EXPECT(rc != 0, "KEYTEST form %u (%s) was taken (rc %d)", f, held ? "job held" : "no job", rc); |
| 154 | EXPECT(dev.sent == sent && st_out == so, "KEYTEST form %u: a frame went out", f); |
| 155 | EXPECT(dev.hits == 0, "KEYTEST form %u: the key was on the wire", f); |
| 156 | EXPECT(!memcmp(snap, job, sizeof job), "KEYTEST form %u: the job buffer changed (%s)", f, |
| 157 | held ? "job held" : "no job"); |
| 158 | EXPECT(jn == job_n && jp == job_parts && js == job_seq && jo == job_out && ja == job_acked, |
| 159 | "KEYTEST form %u: job state changed", f); |
| 160 | pump_for(5000); |
| 161 | EXPECT(dev.hits == 0, "KEYTEST form %u: the key went out at a later resend", f); |
| 162 | EXPECT(memmem(job, sizeof job, SECRET, sizeof SECRET - 1) == 0, "KEYTEST form %u: the key is in the buffer", f); |
| 163 | } |
| 164 | } |
| 165 | /* Unrelated protocol lines keep their existing behavior. */ |
| 166 | fresh(); |
| 167 | EXPECT(c->send("STATUS x", 8) == 0 && c->send("keytest 1", 9) == 0, "an ordinary line starting like it was refused"); |
| 168 | } |
| 169 | |
| 170 | static void resend_and_clear(void) |
| 171 | { |
| 172 | /* resends before the answer */ |
| 173 | fresh(); |
| 174 | EXPECT(c->send("chat hello allen " SECRET, 17 + sizeof SECRET - 1) == 0, "job not taken"); |
| 175 | unsigned j0 = dev.jobs, r0 = st_resends; |
| 176 | EXPECT(j0 == 1, "the job went out %u times at send, want 1", j0); |
| 177 | pump_for(1000); |
| 178 | EXPECT(dev.jobs == j0, "resent after 1 s"); |
| 179 | pump_for(1200); |
| 180 | EXPECT(dev.jobs == j0 + 1 && st_resends == r0 + 1, "no resend after 2 s (jobs %u)", dev.jobs); |
| 181 | EXPECT(job_n && !all_zero(), "the job is not held before the answer"); |
| 182 | |
| 183 | /* a wrong number does not clear it: ACK, LINE, and another boot's run id */ |
| 184 | uint32_t seq = job_seq; |
| 185 | inject("ACK", seq + 5, 0, "", 0); |
| 186 | inject("LINE", seq + 5, 1, "SAY no", 0); |
| 187 | inject("ACK", seq, 0, "", run + 1); |
| 188 | pump_for(10); |
| 189 | EXPECT(job_n && !all_zero() && job_out, "a wrong-number or foreign-run ACK/LINE cleared the job"); |
| 190 | pump_for(2100); |
| 191 | EXPECT(dev.jobs == j0 + 2, "the resends stopped for a wrong-number answer (jobs %u)", dev.jobs); |
| 192 | |
| 193 | /* the matching ACK clears every byte and stops the resends */ |
| 194 | dev.in_cnt = dev.in_pos = 0; |
| 195 | inject("ACK", seq, 0, "", 0); |
| 196 | pump_for(10); |
| 197 | EXPECT(all_zero() && job_n == 0 && job_parts == 0 && job_out == 0, "the matching ACK left bytes in the job buffer"); |
| 198 | unsigned jobs = dev.jobs; |
| 199 | pump_for(10000); |
| 200 | EXPECT(dev.jobs == jobs, "the job was sent again after its ACK"); |
| 201 | EXPECT(c->send("chat next", 9) == 0 && job_seq == seq + 1 && dev.jobs == jobs + 1, "no new job after the ACK"); |
| 202 | |
| 203 | /* the first matching LINE does the same, and the line is still delivered */ |
| 204 | fresh(); |
| 205 | EXPECT(c->send("chat " SECRET, 5 + sizeof SECRET - 1) == 0, "job not taken"); |
| 206 | seq = job_seq; |
| 207 | dev.in_cnt = dev.in_pos = 0; |
| 208 | inject("LINE", seq, 1, "SAY hello", 0); |
| 209 | char line[CK_COURIER_LINE_MAX]; |
| 210 | int got = c->take(line, sizeof line); |
| 211 | EXPECT(all_zero() && job_n == 0 && job_out == 0, "the first matching LINE left bytes in the job buffer"); |
| 212 | EXPECT(got == 1 && !strcmp(line, "SAY hello"), "the answer line was lost (%d '%s')", got, line); |
| 213 | jobs = dev.jobs; |
| 214 | pump_for(10000); |
| 215 | EXPECT(dev.jobs == jobs, "the job was sent again after its LINE"); |
| 216 | |
| 217 | /* a LINE for another job number does not clear */ |
| 218 | fresh(); |
| 219 | EXPECT(c->send("chat " SECRET, 5 + sizeof SECRET - 1) == 0, "job not taken"); |
| 220 | dev.in_cnt = dev.in_pos = 0; |
| 221 | inject("LINE", job_seq + 3, 1, "SAY other", 0); |
| 222 | pump_for(10); |
| 223 | EXPECT(job_n && job_out && !all_zero(), "a LINE for another job cleared the buffer"); |
| 224 | } |
| 225 | |
| 226 | static void reinit_clears(void) |
| 227 | { |
| 228 | fresh(); |
| 229 | EXPECT(c->send("chat " SECRET, 5 + sizeof SECRET - 1) == 0, "job not taken"); |
| 230 | EXPECT(!all_zero(), "setup: job not held"); |
| 231 | c = ck_courier_udp_init(&io_, t_mac, t_none, t_peer, CK_COURIER_PORT, 1000); |
| 232 | EXPECT(all_zero() && job_n == 0 && job_parts == 0 && job_out == 0 && job_acked == 0 && job_seq == 0, |
| 233 | "init left the previous job in the buffer"); |
| 234 | EXPECT(memmem(job, sizeof job, SECRET, sizeof SECRET - 1) == 0, "the key survived init"); |
| 235 | } |
| 236 | |
| 237 | int main(void) |
| 238 | { |
| 239 | keytest_refused(); |
| 240 | resend_and_clear(); |
| 241 | reinit_clears(); |
| 242 | if (failures) |
| 243 | return 1; |
| 244 | printf("COURIER_SECRET: PASS\n"); |
| 245 | return 0; |
| 246 | } |