omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / one_layer / acceptance / ola_runner.c
| 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 | /* |
| 18 | * ola_runner.c -- the sandboxed half of the one-layer stage-1 acceptance harness (omega#443). |
| 19 | * |
| 20 | * The trusted parent (ola.c) fork+execs this program for every candidate and every isolation probe, |
| 21 | * with rlimits already set (CPU, address space, file size, core) and only these descriptors open: |
| 22 | * fd 0 inputs: u32 LE line count n, then n * nparams u64 LE canonical arguments |
| 23 | * fd 1 transcript (a memfd the parent reads after exit) |
| 24 | * fd 2 diagnostics (a memfd) |
| 25 | * fd 3 candidate bytes (osc / oscir / omegaprog only) |
| 26 | * It never receives a path to hidden/, LABELS, the corpus tarball, the evidence tree or a key. |
| 27 | * |
| 28 | * ola_runner osc <nonce> <scratch> <entry> <ret> <param kinds...> |
| 29 | * ola_runner oscir <nonce> <scratch> <entry> <ret> <param kinds...> (v2: OSC binary IR bytes, osc_ir_decode) |
| 30 | * ola_runner omegaprog <nonce> <scratch> <width> <program_id hex | -> |
| 31 | * ola_runner probe <nonce> <scratch> <control 0|1> <name> [target] |
| 32 | * |
| 33 | * Sandbox (enter_sandbox): no_new_privs, Landlock (no filesystem access except the scratch |
| 34 | * directory; no TCP bind/connect where the kernel ABI has it; scoped signals/abstract sockets where |
| 35 | * it has that), then a seccomp allow-list whose default action traps: the SIGSYS handler writes |
| 36 | * "SANDBOX syscall=<nr>" to fd 2 and exits OLA_EXIT_SANDBOX. Decoding, compiling and executing the |
| 37 | * candidate all happen after the sandbox is entered (the compiler sees hostile bytes). |
| 38 | * A probe with control=1 skips the sandbox: the negative control that proves the probe notices. |
| 39 | * |
| 40 | * Transcript: "OLA1 <nonce>", then either "REFUSE <reason> <detail>" or one line per input |
| 41 | * ("R <decimal u64>" = returned value, raw 64 bits; "T <code>" = OSC trap), then "END <nonce> <n>". |
| 42 | */ |
| 43 | #include <errno.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <inttypes.h> |
| 46 | #include <linux/audit.h> |
| 47 | #include <linux/filter.h> |
| 48 | #include <linux/landlock.h> |
| 49 | #include <linux/seccomp.h> |
| 50 | #include <netinet/in.h> |
| 51 | #include <arpa/inet.h> |
| 52 | #include <signal.h> |
| 53 | #include <stddef.h> |
| 54 | #include <stdio.h> |
| 55 | #include <stdlib.h> |
| 56 | #include <string.h> |
| 57 | #include <sys/mman.h> |
| 58 | #include <sys/prctl.h> |
| 59 | #include <sys/socket.h> |
| 60 | #include <sys/syscall.h> |
| 61 | #include "sha256.h" |
| 62 | #include <sys/utsname.h> |
| 63 | #include <sys/wait.h> |
| 64 | #include <time.h> |
| 65 | #include <unistd.h> |
| 66 | |
| 67 | #include "osc_cg.h" |
| 68 | #include "osc_front.h" |
| 69 | #include "osc_native.h" |
| 70 | #include "omega_program.h" |
| 71 | #include "omega_program_ir.h" |
| 72 | |
| 73 | #define OLA_EXIT_SANDBOX 97 /* a syscall outside the allow-list was attempted */ |
| 74 | #define OLA_EXIT_MEMORY 98 /* an allocation failed (RLIMIT_AS) */ |
| 75 | #define OLA_EXIT_UNAVAIL 96 /* the sandbox could not be built: the parent fails closed */ |
| 76 | #define OLA_EXIT_USAGE 95 |
| 77 | |
| 78 | /* The seccomp allow-list below uses aarch64 syscall numbers and admits only this audit arch. */ |
| 79 | #define OLA_FILTER_ARCH AUDIT_ARCH_AARCH64 /* OLA:filter-arch */ |
| 80 | #if defined(__aarch64__) |
| 81 | #define OLA_BUILD_ARCH AUDIT_ARCH_AARCH64 |
| 82 | #elif defined(__x86_64__) |
| 83 | #define OLA_BUILD_ARCH AUDIT_ARCH_X86_64 |
| 84 | #else |
| 85 | #define OLA_BUILD_ARCH 0u |
| 86 | #endif |
| 87 | |
| 88 | static const char *g_nonce = "-"; |
| 89 | static int g_header; /* "OLA1 <nonce>" already printed */ |
| 90 | |
| 91 | static void sigsys_handler(int sig, siginfo_t *si, void *uc) |
| 92 | { |
| 93 | (void)sig; (void)uc; |
| 94 | char b[64] = "SANDBOX syscall="; |
| 95 | size_t n = strlen(b); |
| 96 | unsigned v = (unsigned)si->si_syscall; |
| 97 | char d[12]; int k = 0; |
| 98 | do { d[k++] = (char)('0' + v % 10); v /= 10; } while (v && k < 11); |
| 99 | while (k) b[n++] = d[--k]; |
| 100 | b[n++] = '\n'; |
| 101 | ssize_t w = write(2, b, n); |
| 102 | (void)w; |
| 103 | _exit(OLA_EXIT_SANDBOX); |
| 104 | } |
| 105 | |
| 106 | static int landlock_abi(void) |
| 107 | { |
| 108 | return (int)syscall(SYS_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION); |
| 109 | } |
| 110 | |
| 111 | /* Filesystem rights Landlock knows at each ABI (linux/landlock.h). */ |
| 112 | static uint64_t fs_rights(int abi) |
| 113 | { |
| 114 | uint64_t r = LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_READ_FILE | |
| 115 | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_REMOVE_DIR | LANDLOCK_ACCESS_FS_REMOVE_FILE | |
| 116 | LANDLOCK_ACCESS_FS_MAKE_CHAR | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | |
| 117 | LANDLOCK_ACCESS_FS_MAKE_SOCK | LANDLOCK_ACCESS_FS_MAKE_FIFO | LANDLOCK_ACCESS_FS_MAKE_BLOCK | |
| 118 | LANDLOCK_ACCESS_FS_MAKE_SYM; |
| 119 | if (abi >= 2) r |= LANDLOCK_ACCESS_FS_REFER; |
| 120 | if (abi >= 3) r |= LANDLOCK_ACCESS_FS_TRUNCATE; |
| 121 | #ifdef LANDLOCK_ACCESS_FS_IOCTL_DEV |
| 122 | if (abi >= 5) r |= LANDLOCK_ACCESS_FS_IOCTL_DEV; |
| 123 | #endif |
| 124 | return r; |
| 125 | } |
| 126 | |
| 127 | static int enter_landlock(const char *scratch) |
| 128 | { |
| 129 | int abi = landlock_abi(); |
| 130 | if (abi < 1) { dprintf(2, "SANDBOX_UNAVAILABLE landlock abi=%d errno=%d\n", abi, errno); return -1; } |
| 131 | struct landlock_ruleset_attr ra; |
| 132 | memset(&ra, 0, sizeof ra); |
| 133 | size_t rsz = offsetof(struct landlock_ruleset_attr, handled_access_net); |
| 134 | ra.handled_access_fs = fs_rights(abi); |
| 135 | if (abi >= 4) { /* OLA:landlock-net */ |
| 136 | ra.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP; |
| 137 | rsz = offsetof(struct landlock_ruleset_attr, handled_access_net) + sizeof ra.handled_access_net; |
| 138 | } |
| 139 | #ifdef LANDLOCK_SCOPE_SIGNAL |
| 140 | if (abi >= 6) { |
| 141 | ra.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL; |
| 142 | rsz = sizeof ra; |
| 143 | } |
| 144 | #endif |
| 145 | int rs = (int)syscall(SYS_landlock_create_ruleset, &ra, rsz, 0); |
| 146 | if (rs < 0) { dprintf(2, "SANDBOX_UNAVAILABLE landlock_create_ruleset errno=%d\n", errno); return -1; } |
| 147 | int dfd = open(scratch, O_PATH | O_DIRECTORY | O_CLOEXEC); |
| 148 | if (dfd < 0) { dprintf(2, "SANDBOX_UNAVAILABLE scratch errno=%d\n", errno); return -1; } |
| 149 | struct landlock_path_beneath_attr pb = { |
| 150 | .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_READ_DIR | |
| 151 | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REMOVE_FILE | |
| 152 | (abi >= 3 ? LANDLOCK_ACCESS_FS_TRUNCATE : 0), |
| 153 | .parent_fd = dfd, |
| 154 | }; |
| 155 | if (syscall(SYS_landlock_add_rule, rs, LANDLOCK_RULE_PATH_BENEATH, &pb, 0)) { |
| 156 | dprintf(2, "SANDBOX_UNAVAILABLE landlock_add_rule errno=%d\n", errno); |
| 157 | return -1; |
| 158 | } |
| 159 | close(dfd); |
| 160 | if (syscall(SYS_landlock_restrict_self, rs, 0)) { /* OLA:landlock-restrict */ |
| 161 | dprintf(2, "SANDBOX_UNAVAILABLE landlock_restrict_self errno=%d\n", errno); |
| 162 | return -1; |
| 163 | } |
| 164 | close(rs); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | #define ALLOW(nr) BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, (nr), 0, 1), BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW) |
| 169 | |
| 170 | static int enter_seccomp(void) |
| 171 | { |
| 172 | struct sigaction sa; |
| 173 | memset(&sa, 0, sizeof sa); |
| 174 | sa.sa_sigaction = sigsys_handler; |
| 175 | sa.sa_flags = SA_SIGINFO; |
| 176 | if (sigaction(SIGSYS, &sa, NULL)) return -1; |
| 177 | struct sock_filter f[] = { |
| 178 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)), |
| 179 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, OLA_FILTER_ARCH, 1, 0), |
| 180 | BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS), |
| 181 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)), |
| 182 | ALLOW(__NR_read), ALLOW(__NR_write), ALLOW(__NR_close), ALLOW(__NR_lseek), |
| 183 | ALLOW(__NR_fstat), ALLOW(__NR_newfstatat), ALLOW(__NR_openat), |
| 184 | ALLOW(__NR_mmap), ALLOW(__NR_munmap), ALLOW(__NR_mprotect), ALLOW(__NR_mremap), ALLOW(__NR_brk), |
| 185 | ALLOW(__NR_madvise), ALLOW(__NR_getrandom), |
| 186 | ALLOW(__NR_rt_sigreturn), ALLOW(__NR_rt_sigprocmask), ALLOW(__NR_clock_gettime), |
| 187 | ALLOW(__NR_exit), ALLOW(__NR_exit_group), |
| 188 | BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP), /* OLA:seccomp-default */ |
| 189 | }; |
| 190 | struct sock_fprog prog = { .len = (unsigned short)(sizeof f / sizeof f[0]), .filter = f }; |
| 191 | if (syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog)) { |
| 192 | dprintf(2, "SANDBOX_UNAVAILABLE seccomp errno=%d\n", errno); |
| 193 | return -1; |
| 194 | } |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static void enter_sandbox(const char *scratch) |
| 199 | { |
| 200 | /* A machine the allow-list was not written for would have every syscall killed and every case |
| 201 | * reported as a sandbox violation: refuse the whole run instead (the parent fails closed). */ |
| 202 | struct utsname un; |
| 203 | if (OLA_BUILD_ARCH != OLA_FILTER_ARCH || uname(&un) || strcmp(un.machine, "aarch64")) { /* OLA:arch-check */ |
| 204 | dprintf(2, "SANDBOX_UNAVAILABLE arch: the seccomp allow-list is aarch64-only; this machine or build is not aarch64\n"); |
| 205 | _exit(OLA_EXIT_UNAVAIL); |
| 206 | } |
| 207 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { dprintf(2, "SANDBOX_UNAVAILABLE no_new_privs\n"); _exit(OLA_EXIT_UNAVAIL); } |
| 208 | if (enter_landlock(scratch)) _exit(OLA_EXIT_UNAVAIL); /* OLA:enter-landlock */ |
| 209 | if (enter_seccomp()) _exit(OLA_EXIT_UNAVAIL); /* OLA:enter-seccomp */ |
| 210 | } |
| 211 | |
| 212 | /* ---- inputs and candidate ----------------------------------------------------------------- */ |
| 213 | |
| 214 | static uint8_t *read_all(int fd, size_t max, size_t *len) |
| 215 | { |
| 216 | size_t cap = 4096, n = 0; |
| 217 | uint8_t *b = malloc(cap); |
| 218 | if (!b) return NULL; |
| 219 | for (;;) { |
| 220 | if (n == cap) { |
| 221 | if (cap > max) { free(b); errno = EFBIG; return NULL; } |
| 222 | uint8_t *nb = realloc(b, cap * 2); |
| 223 | if (!nb) { free(b); return NULL; } |
| 224 | b = nb; cap *= 2; |
| 225 | } |
| 226 | ssize_t r = read(fd, b + n, cap - n); |
| 227 | if (r < 0) { if (errno == EINTR) continue; free(b); return NULL; } |
| 228 | if (r == 0) break; |
| 229 | n += (size_t)r; |
| 230 | } |
| 231 | if (n > max) { free(b); errno = EFBIG; return NULL; } |
| 232 | *len = n; |
| 233 | return b; |
| 234 | } |
| 235 | |
| 236 | static uint64_t le64(const uint8_t *p) |
| 237 | { |
| 238 | uint64_t v = 0; |
| 239 | for (int i = 7; i >= 0; i--) v = v << 8 | p[i]; |
| 240 | return v; |
| 241 | } |
| 242 | |
| 243 | static void nomem(const char *where) |
| 244 | { |
| 245 | printf("MEM %s\n", where); |
| 246 | fflush(stdout); |
| 247 | _exit(OLA_EXIT_MEMORY); |
| 248 | } |
| 249 | |
| 250 | static const uint64_t *load_inputs(unsigned nparams, uint32_t *nlines) |
| 251 | { |
| 252 | size_t len = 0; |
| 253 | uint8_t *b = read_all(0, (size_t)4 + 4096u * 6u * 8u, &len); |
| 254 | if (!b) nomem("inputs"); |
| 255 | if (len < 4) { dprintf(2, "inputs: short\n"); _exit(OLA_EXIT_USAGE); } |
| 256 | uint32_t n = (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24; |
| 257 | if (n < 1 || n > 4096 || len != 4 + (size_t)n * nparams * 8) { dprintf(2, "inputs: bad length\n"); _exit(OLA_EXIT_USAGE); } |
| 258 | uint64_t *v = malloc(sizeof *v * ((size_t)n * nparams + 1)); |
| 259 | if (!v) nomem("inputs"); |
| 260 | for (size_t i = 0; i < (size_t)n * nparams; i++) v[i] = le64(b + 4 + 8 * i); |
| 261 | free(b); |
| 262 | *nlines = n; |
| 263 | return v; |
| 264 | } |
| 265 | |
| 266 | static void refuse(const char *reason, const char *detail) |
| 267 | { |
| 268 | if (!g_header) printf("OLA1 %s\n", g_nonce); |
| 269 | printf("REFUSE %s %s\nEND %s 0\n", reason, detail, g_nonce); |
| 270 | fflush(stdout); |
| 271 | _exit(0); |
| 272 | } |
| 273 | |
| 274 | /* Detail text is one token: no spaces or control bytes (receipts are key=value lines). */ |
| 275 | static void token(char *s) |
| 276 | { |
| 277 | for (; *s; s++) |
| 278 | if ((unsigned char)*s <= ' ' || (unsigned char)*s >= 127) *s = '_'; |
| 279 | } |
| 280 | |
| 281 | static int kind_of(const char *s) |
| 282 | { |
| 283 | static const char *const nm[] = {"bool", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64"}; |
| 284 | static const int k[] = {OSC_T_BOOL, OSC_T_U8, OSC_T_U16, OSC_T_U32, OSC_T_U64, OSC_T_I8, OSC_T_I16, OSC_T_I32, OSC_T_I64}; |
| 285 | for (int i = 0; i < 9; i++) if (!strcmp(s, nm[i])) return k[i]; |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | static int run_osc(int argc, char **argv, int ir) |
| 290 | { |
| 291 | /* argv: osc|oscir nonce scratch entry ret k... (oscir: the candidate is OSC binary IR bytes, v2) */ |
| 292 | if (argc < 6 || argc > 12) return OLA_EXIT_USAGE; |
| 293 | const char *entry = argv[4]; |
| 294 | int ret_kind = kind_of(argv[5]); |
| 295 | unsigned np = (unsigned)(argc - 6); |
| 296 | int pk[6]; |
| 297 | if (ret_kind < 0) return OLA_EXIT_USAGE; |
| 298 | for (unsigned i = 0; i < np; i++) if ((pk[i] = kind_of(argv[6 + i])) < 0) return OLA_EXIT_USAGE; |
| 299 | uint32_t n = 0; |
| 300 | const uint64_t *in = load_inputs(np, &n); |
| 301 | size_t slen = 0; |
| 302 | uint8_t *src = read_all(3, 1u << 20, &slen); |
| 303 | if (!src) refuse("oversize", "candidate"); |
| 304 | OscUnit *u = malloc(sizeof *u); |
| 305 | OscRt *rt = malloc(sizeof *rt); |
| 306 | if (!u || !rt) nomem("unit"); |
| 307 | enter_sandbox(argv[3]); |
| 308 | printf("OLA1 %s\n", g_nonce); |
| 309 | g_header = 1; |
| 310 | if (ir) { /* v2 kind oscir: the strict reader, inside the sandbox; then exactly the osc path */ |
| 311 | int rc = osc_ir_decode(src, slen, u); |
| 312 | if (rc) { /* OLA:oscir-decode */ |
| 313 | char det[64]; |
| 314 | snprintf(det, sizeof det, "class=%s", osc_ir_refusal_name(rc)); |
| 315 | refuse("decode_refused", det); |
| 316 | } |
| 317 | } else { |
| 318 | OscDiag d; |
| 319 | memset(&d, 0, sizeof d); |
| 320 | if (osc_compile((const char *)src, slen, u, &d, NULL)) { |
| 321 | char det[96]; |
| 322 | snprintf(det, sizeof det, "kind=%s,line=%u", osc_diag_kind_name(d.kind), d.line); |
| 323 | token(det); |
| 324 | refuse("compile_refused", det); |
| 325 | } |
| 326 | } |
| 327 | int fi = -1; |
| 328 | for (unsigned i = 0; i < u->nfuncs; i++) |
| 329 | if (!strcmp(u->funcs[i].name, entry)) { fi = (int)i; break; } |
| 330 | if (fi < 0) refuse("signature_mismatch", "no_entry_function"); |
| 331 | const OscFunc *fn = &u->funcs[fi]; |
| 332 | if (fn->nparams != np) refuse("signature_mismatch", "param_count"); |
| 333 | for (unsigned i = 0; i < np; i++) |
| 334 | if ((int)fn->vtype[i].s != pk[i] || fn->vtype[i].ref != OSC_REF_NONE) refuse("signature_mismatch", "param_kind"); |
| 335 | if ((int)fn->ret.s != ret_kind || fn->ret.ref != OSC_REF_NONE) refuse("signature_mismatch", "ret_kind"); |
| 336 | OscCode code; |
| 337 | memset(&code, 0, sizeof code); |
| 338 | char err[160]; |
| 339 | if (osc_cg_compile(u, &code, err, sizeof err)) { token(err); refuse("compile_refused", "backend"); } |
| 340 | OscNative nm; |
| 341 | if (osc_native_map(&nm, code.code, code.len)) refuse("realize_refused", "native_map"); |
| 342 | osc_rt_init(rt); |
| 343 | for (uint32_t i = 0; i < n; i++) { |
| 344 | uint64_t r = 0; |
| 345 | osc_rt_reset(rt); |
| 346 | int t = osc_native_call(u, fi, &nm, code.entry[fi], rt, in + (size_t)i * np, np, &r); |
| 347 | if (t < 0) refuse("realize_refused", "native_call_refused"); |
| 348 | if (t > 0) printf("T %d\n", t); |
| 349 | else printf("R %" PRIu64 "\n", r); |
| 350 | } |
| 351 | printf("END %s %" PRIu32 "\n", g_nonce, n); |
| 352 | fflush(stdout); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static int hexval(int c) { return c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1; } |
| 357 | |
| 358 | static int run_omegaprog(int argc, char **argv) |
| 359 | { |
| 360 | /* argv: omegaprog nonce scratch width program_id|- */ |
| 361 | if (argc != 6) return OLA_EXIT_USAGE; |
| 362 | unsigned w = (unsigned)atoi(argv[4]); |
| 363 | if (w != 8 && w != 16 && w != 32 && w != 64) return OLA_EXIT_USAGE; |
| 364 | uint8_t want_id[32]; |
| 365 | int has_id = strcmp(argv[5], "-") != 0; |
| 366 | if (has_id) { |
| 367 | if (strlen(argv[5]) != 64) return OLA_EXIT_USAGE; |
| 368 | for (int i = 0; i < 32; i++) { |
| 369 | int a = hexval(argv[5][2 * i]), b = hexval(argv[5][2 * i + 1]); |
| 370 | if (a < 0 || b < 0) return OLA_EXIT_USAGE; |
| 371 | want_id[i] = (uint8_t)(a << 4 | b); |
| 372 | } |
| 373 | } |
| 374 | uint32_t n = 0; |
| 375 | const uint64_t *in = load_inputs(1, &n); |
| 376 | size_t blen = 0; |
| 377 | uint8_t *bytes = read_all(3, 1u << 20, &blen); |
| 378 | if (!bytes) refuse("oversize", "candidate"); |
| 379 | OmegaProgram *p = malloc(sizeof *p); |
| 380 | if (!p) nomem("program"); |
| 381 | enter_sandbox(argv[3]); |
| 382 | printf("OLA1 %s\n", g_nonce); |
| 383 | g_header = 1; |
| 384 | if (omega_program_ir_decode(bytes, blen, p)) refuse("decode_refused", "omega_program_ir_decode"); |
| 385 | if (p->contract.input_type != TYPE_UNSIGNED_INT || p->contract.output_type != TYPE_UNSIGNED_INT || |
| 386 | p->contract.input_width != w || p->contract.output_width != w) |
| 387 | refuse("signature_mismatch", "type_or_width"); |
| 388 | if (has_id && memcmp(p->program_id.bytes, want_id, 32)) refuse("signature_mismatch", "program_id"); |
| 389 | char why[160] = ""; |
| 390 | if (omega_program_realize_ex(p, why, sizeof why)) { token(why); refuse("realize_refused", why[0] ? why : "-"); } |
| 391 | for (uint32_t i = 0; i < n; i++) { |
| 392 | uint64_t y = 0; |
| 393 | if (omega_program_exec(p, in[i], &y)) refuse("realize_refused", "exec_refused"); |
| 394 | printf("R %" PRIu64 "\n", y); |
| 395 | } |
| 396 | printf("END %s %" PRIu32 "\n", g_nonce, n); |
| 397 | fflush(stdout); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /* ---- isolation probes ------------------------------------------------------------------------ |
| 402 | * Each probe attempts one forbidden action and reports what happened: |
| 403 | * ESCAPED <what> the action succeeded (the sandbox did not stop it) |
| 404 | * BLOCKED errno=<e> the kernel refused it |
| 405 | * or it dies: seccomp (exit 97 "SANDBOX syscall=..."), time limit, memory limit. The parent |
| 406 | * classifies. scratch_ok is the positive control inside the sandbox (must be ALLOWED). */ |
| 407 | |
| 408 | static double now_s(void) |
| 409 | { |
| 410 | struct timespec ts; |
| 411 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 412 | return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; |
| 413 | } |
| 414 | |
| 415 | static int probe_open(const char *path, int flags, const char *what) |
| 416 | { |
| 417 | int fd = open(path, flags | O_CLOEXEC, 0600); |
| 418 | if (fd < 0) { printf("BLOCKED errno=%d\n", errno); return 0; } |
| 419 | if ((flags & O_ACCMODE) == O_RDONLY) { |
| 420 | char c; |
| 421 | ssize_t r = read(fd, &c, 1); |
| 422 | if (r < 0) { printf("BLOCKED errno=%d read\n", errno); return 0; } |
| 423 | } |
| 424 | printf("ESCAPED %s\n", what); /* OLA:probe-report */ |
| 425 | close(fd); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int run_probe(int argc, char **argv) |
| 430 | { |
| 431 | /* argv: probe nonce scratch control name [target] */ |
| 432 | if (argc < 6) return OLA_EXIT_USAGE; |
| 433 | const char *scratch = argv[3], *name = argv[5], *target = argc > 6 ? argv[6] : ""; |
| 434 | int control = !strcmp(argv[4], "1"); |
| 435 | if (!control) enter_sandbox(scratch); /* OLA:probe-sandbox */ |
| 436 | setvbuf(stdout, NULL, _IONBF, 0); |
| 437 | if (!strcmp(name, "read_hidden_inputs") || !strcmp(name, "read_expected") || !strcmp(name, "read_labels") || |
| 438 | !strcmp(name, "read_corpus_tar") || !strcmp(name, "read_key")) |
| 439 | return probe_open(target, O_RDONLY, "read"); |
| 440 | if (!strcmp(name, "write_evidence") || !strcmp(name, "write_scorer") || !strcmp(name, "change_rules")) |
| 441 | return probe_open(target, O_WRONLY, "opened_for_write"); /* no write, no truncation: nothing changes */ |
| 442 | if (!strcmp(name, "write_outside_scratch")) return probe_open(target, O_WRONLY | O_CREAT | O_EXCL, "created"); |
| 443 | if (!strcmp(name, "scratch_ok")) { |
| 444 | char p[4096]; |
| 445 | snprintf(p, sizeof p, "%s/scratch_ok", scratch); |
| 446 | int fd = open(p, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); |
| 447 | if (fd < 0) { printf("BLOCKED errno=%d\n", errno); return 0; } |
| 448 | ssize_t w = write(fd, "ok\n", 3); |
| 449 | close(fd); |
| 450 | if (w != 3) { printf("BLOCKED errno=%d write\n", errno); return 0; } |
| 451 | printf("ESCAPED wrote_scratch\n"); |
| 452 | return 0; |
| 453 | } |
| 454 | if (!strcmp(name, "network")) { |
| 455 | int port = atoi(target); |
| 456 | int s = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 457 | if (s < 0) { printf("BLOCKED errno=%d socket\n", errno); return 0; } |
| 458 | struct sockaddr_in a; |
| 459 | memset(&a, 0, sizeof a); |
| 460 | a.sin_family = AF_INET; |
| 461 | a.sin_port = htons((uint16_t)port); |
| 462 | a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 463 | if (connect(s, (struct sockaddr *)&a, sizeof a)) { printf("BLOCKED errno=%d connect\n", errno); return 0; } |
| 464 | printf("ESCAPED connected\n"); |
| 465 | return 0; |
| 466 | } |
| 467 | if (!strcmp(name, "outlive_time")) { |
| 468 | double limit = atof(target), t0 = now_s(); |
| 469 | volatile uint64_t x = 0; |
| 470 | while (now_s() - t0 < limit + 2.0) x++; |
| 471 | printf("ESCAPED outlived\n"); |
| 472 | return 0; |
| 473 | } |
| 474 | if (!strcmp(name, "outlive_fork")) { |
| 475 | pid_t c = fork(); |
| 476 | if (c < 0) { printf("BLOCKED errno=%d\n", errno); return 0; } |
| 477 | if (c == 0) _exit(0); |
| 478 | waitpid(c, NULL, 0); |
| 479 | printf("ESCAPED forked\n"); |
| 480 | return 0; |
| 481 | } |
| 482 | if (!strcmp(name, "exceed_memory")) { |
| 483 | size_t mb = (size_t)atol(target); |
| 484 | void *m = mmap(NULL, mb * 2u << 20, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); |
| 485 | if (m == MAP_FAILED) { printf("BLOCKED errno=%d\n", errno); return 0; } |
| 486 | munmap(m, mb * 2u << 20); |
| 487 | printf("ESCAPED mapped\n"); |
| 488 | return 0; |
| 489 | } |
| 490 | if (!strcmp(name, "replay_verdict")) { |
| 491 | /* A replayed transcript: framed with a nonce recorded from an earlier execution (target). */ |
| 492 | printf("OLA1 %s\nR 0\nEND %s 1\n", target, target); |
| 493 | return 0; |
| 494 | } |
| 495 | return OLA_EXIT_USAGE; |
| 496 | } |
| 497 | |
| 498 | /* The Omega-native hash binding (src/omega_om_units.c) reserves its thread stack and its pthread key on |
| 499 | * first use, and glibc reaches a futex on that path; the sandbox allow-list below does not admit futex, so |
| 500 | * a first use inside the sandbox is judged a sandbox violation (receipt detail=syscall_98). Warm the |
| 501 | * binding once, before any sandbox is entered. */ |
| 502 | static void warm_native_binding(void) |
| 503 | { |
| 504 | uint8_t d[32]; |
| 505 | sha256_hash((const uint8_t *)"", 0, d); |
| 506 | } |
| 507 | int main(int argc, char **argv) |
| 508 | { |
| 509 | if (argc < 4) { fprintf(stderr, "usage: ola_runner osc|omegaprog|probe <nonce> <scratch> ...\n"); return OLA_EXIT_USAGE; } |
| 510 | g_nonce = argv[2]; |
| 511 | warm_native_binding(); |
| 512 | if (!strcmp(argv[1], "osc")) return run_osc(argc, argv, 0); |
| 513 | if (!strcmp(argv[1], "oscir")) return run_osc(argc, argv, 1); /* v2 */ |
| 514 | if (!strcmp(argv[1], "omegaprog")) return run_omegaprog(argc, argv); |
| 515 | if (!strcmp(argv[1], "probe")) return run_probe(argc, argv); |
| 516 | return OLA_EXIT_USAGE; |
| 517 | } |