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 / local_wire.c
| 1 | /* A closed, bounded data protocol over the existing untrusted courier. |
| 2 | * CALL request invocation tool total offset hex (chunks <=96 bytes, '-' empty). |
| 3 | * POLL request invocation and CANCEL request invocation require a live request. |
| 4 | * RESULT request invocation state reason hex. Only the three build-owned tools |
| 5 | * are reachable. No address, path, executable or resource limit comes off-wire. */ |
| 6 | #include "local_wire.h" |
| 7 | #include "local_tools.h" |
| 8 | #include "../core/ck_internal.h" |
| 9 | static uint32_t current, assembling, tool, total, received; |
| 10 | static uint8_t bytes[CK_LOCAL_INPUT]; |
| 11 | static const struct ck_local_result *pending; |
| 12 | |
| 13 | static int number(const char **p, uint32_t *out) |
| 14 | { |
| 15 | const char *s = *p; |
| 16 | if (*s < '0' || *s > '9') return 0; |
| 17 | uint32_t v = 0; |
| 18 | do { |
| 19 | unsigned d = (unsigned)(*s++ - '0'); |
| 20 | if (v > (UINT32_MAX - d) / 10) return 0; |
| 21 | v = v * 10 + d; |
| 22 | } while (*s >= '0' && *s <= '9'); |
| 23 | if (*s && *s != ' ') return 0; |
| 24 | *p = *s ? s + 1 : s; *out = v; |
| 25 | return 1; |
| 26 | } |
| 27 | static int nibble(char c) |
| 28 | { |
| 29 | if (c >= '0' && c <= '9') return c - '0'; |
| 30 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 31 | return -1; |
| 32 | } |
| 33 | void ck_local_wire_begin(uint32_t request) |
| 34 | { |
| 35 | ck_local_begin(request); |
| 36 | current = request; assembling = received = total = tool = 0; pending = 0; |
| 37 | memset(bytes, 0, sizeof bytes); |
| 38 | } |
| 39 | int ck_local_wire_line(const char *line) |
| 40 | { |
| 41 | int call = strlen(line) >= 5 && !memcmp(line, "CALL ", 5); |
| 42 | int poll = strlen(line) >= 5 && !memcmp(line, "POLL ", 5); |
| 43 | int cancel = strlen(line) >= 7 && !memcmp(line, "CANCEL ", 7); |
| 44 | if (!call && !poll && !cancel) return 0; |
| 45 | const char *p = line + (cancel ? 7 : 5); |
| 46 | uint32_t req, inv, id, n, off; |
| 47 | if (!number(&p, &req) || !number(&p, &inv) || !current || req != current || !inv) return 1; |
| 48 | if (!call) { |
| 49 | if (*p) return 1; |
| 50 | const struct ck_local_result *r = ck_local_status(req, inv); |
| 51 | if (r) { |
| 52 | if (cancel && r->state == CK_LOCAL_RUNNING) ck_local_cancel(); |
| 53 | pending = r; |
| 54 | } |
| 55 | return 1; |
| 56 | } |
| 57 | if (!number(&p, &id) || !number(&p, &n) || !number(&p, &off) || id < 1 || id > 3 || |
| 58 | n > sizeof bytes || off > n) return 1; |
| 59 | if (pending && pending->state == CK_LOCAL_RUNNING && pending->invocation != inv) return 1; |
| 60 | uint8_t chunk[96]; unsigned count = 0; |
| 61 | if (*p == '-' && !p[1]) p++; |
| 62 | while (*p) { |
| 63 | if (!p[1] || count == sizeof chunk) return 1; |
| 64 | int a = nibble(p[0]), b = nibble(p[1]); |
| 65 | if (a < 0 || b < 0) return 1; |
| 66 | chunk[count++] = (uint8_t)((a << 4) | b); p += 2; |
| 67 | } |
| 68 | if (count > n - off || (!count && n)) return 1; |
| 69 | if (!assembling) { |
| 70 | if (off) return 1; |
| 71 | assembling = inv; tool = id; total = n; received = 0; |
| 72 | } |
| 73 | if (inv != assembling || tool != id || total != n || off > received) return 1; |
| 74 | if (off < received) { /* Retransmitted chunks must be byte-for-byte identical. */ |
| 75 | if (count > received - off || memcmp(bytes + off, chunk, count)) return 1; |
| 76 | } else { |
| 77 | memcpy(bytes + off, chunk, count); received += count; |
| 78 | } |
| 79 | if (received == total) { |
| 80 | pending = ck_local_start(req, inv, id, bytes, total); |
| 81 | memset(bytes, 0, sizeof bytes); assembling = received = total = tool = 0; |
| 82 | } |
| 83 | return 1; |
| 84 | } |
| 85 | void ck_local_wire_step(const ck_courier *courier) |
| 86 | { |
| 87 | if (!courier || !pending || pending->state == CK_LOCAL_RUNNING) return; |
| 88 | /* Current tool schemas return 8, 24 or 40 bytes. Do not silently truncate |
| 89 | * an unexpected output or turn it into a success. */ |
| 90 | unsigned state = pending->state, reason = pending->reason, len = pending->length; |
| 91 | if (len > 40) { state = CK_LOCAL_ABNORMAL; reason = CK_LOCAL_BAD_RETURN; len = 0; } |
| 92 | char line[192]; |
| 93 | int at = ck_snprintf(line, sizeof line, "RESULT %u %u %u %u ", |
| 94 | (unsigned)pending->request, (unsigned)pending->invocation, state, reason); |
| 95 | static const char hex[] = "0123456789abcdef"; |
| 96 | for (unsigned i = 0; i < len; i++) { |
| 97 | line[at++] = hex[pending->output[i] >> 4]; line[at++] = hex[pending->output[i] & 15]; |
| 98 | } |
| 99 | if (!len) line[at++] = '-'; |
| 100 | line[at] = 0; |
| 101 | if (!courier->send(line, (unsigned)at)) pending = 0; |
| 102 | } |