/* * splinter -- builtin file tools. * * The tools are the part of the harness a model can actually break something * with, so each of them is tested against the failure it is supposed to refuse: * an ambiguous edit, a binary file, a write that is too large. */ #include #include #include #include #include #include #include #include #include "splinter.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static char dir[600]; static const struct sp_tool * tool(struct sp_registry *r, const char *name) { const struct sp_tool *t = sp_registry_find(r, name); CHECK(t != NULL); return t; } static void write_file(const char *path, const char *text) { FILE *f = fopen(path, "w"); CHECK(f != NULL); if (f != NULL) { fputs(text, f); fclose(f); } } /* * The context a tool was registered with: the tools keep their root there, and * a test that calls run() directly has to hand it over the way the agent does. */ static void * tool_ctx(struct sp_registry *r, const char *name) { const struct sp_tool *t = tool(r, name); return t != NULL ? t->ctx : NULL; } int main(void) { struct sp_registry r; struct sp_buf out; char path[700], big[1600]; const char *copy; const char *tmp = getenv("TMPDIR"); if (tmp == NULL || *tmp == '\0') tmp = "/var/tmp"; snprintf(dir, sizeof(dir), "%s/splinter-test-XXXXXX", tmp); CHECK(mkdtemp(dir) != NULL); memset(&r, 0, sizeof(r)); if (sp_buf_init(&out) != 0) return 1; CHECK(sp_builtin_register(&r, NULL) == 0); /* the five tools exist and declare their class */ CHECK(tool(&r, "read")->auth == SP_AUTH_READ); CHECK(tool(&r, "write")->auth == SP_AUTH_MUTATE); CHECK(tool(&r, "edit")->auth == SP_AUTH_MUTATE); CHECK(tool(&r, "search")->auth == SP_AUTH_READ); CHECK(tool(&r, "list")->auth == SP_AUTH_READ); CHECK(tool(&r, "run")->auth == SP_AUTH_DESTRUCTIVE); snprintf(path, sizeof(path), "%s/a.txt", dir); write_file(path, "erste zeile\nzweite zeile\ndritte zeile\n"); /* ---- read ---- */ sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path); CHECK(tool(&r, "read")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "zweite zeile") != NULL); CHECK(strstr(out.p, "1\terste") != NULL); /* offset and limit */ sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\",\"offset\":2,\"limit\":1}", path); CHECK(tool(&r, "read")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "zweite") != NULL); CHECK(strstr(out.p, "erste") == NULL); /* a missing file is an observation, not a crash */ sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s/nope\"}", dir); CHECK(tool(&r, "read")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "error:") != NULL); /* ---- write ---- */ sp_buf_clear(&out); snprintf(path, sizeof(path), "%s/neu.txt", dir); snprintf(big, sizeof(big), "{\"path\":\"%s\",\"content\":\"hallo\"}", path); CHECK(tool(&r, "write")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "wrote 5 bytes") != NULL); /* ---- edit: exactly one match ---- */ sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\",\"old\":\"hallo\",\"new\":\"welt\"}", path); CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "edited") != NULL); sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path); CHECK(tool(&r, "read")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "welt") != NULL); /* ---- edit: two matches are refused ---- */ write_file(path, "x x\n"); sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\",\"old\":\"x\",\"new\":\"y\"}", path); CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "occurs 2 times") != NULL); sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path); CHECK(tool(&r, "read")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "y") == NULL); /* nothing was written */ /* * ---- a write that cannot finish must not eat the old file ---- * * The tools used to truncate with fopen(path, "w") and then write, so a * short write (a full disk, RLIMIT_FSIZE) left the operator with an * empty file and no copy. RLIMIT_FSIZE makes the short write * deterministic; SIGXFSZ is ignored so the write returns EFBIG instead * of killing the test. */ { struct rlimit rl; struct sigaction sa, oldsa; char safe[700], big2[1600]; snprintf(safe, sizeof(safe), "%s/keep.txt", dir); write_file(safe, "wichtig\n"); memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0); CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0); { struct rlimit lim = rl; lim.rlim_cur = 4; /* fewer bytes than the new text */ CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0); } sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\",\"content\":\"0123456789\"}", safe); CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0); CHECK(strstr(out.p, "error:") != NULL); CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0); CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0); /* the original is untouched, byte for byte */ sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", safe); CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0); CHECK(strstr(out.p, "wichtig") != NULL); CHECK(strstr(out.p, "0123456789") == NULL); /* and no debris was left behind */ snprintf(big2, sizeof(big2), "%s.tmp", safe); CHECK(access(big2, F_OK) != 0); } /* * ---- a write through a symlink writes the target, not the link ---- * * The old fopen(path, "w") went through the link; a temp file plus * rename() replaces the link instead, which would silently detach a * dotfile setup. Both must agree: the link stays, the target changes. */ { char real[700], link[700], big2[1600]; struct stat st; snprintf(real, sizeof(real), "%s/real.conf", dir); snprintf(link, sizeof(link), "%s/link.conf", dir); write_file(real, "alt\n"); if (symlink(real, link) == 0) { sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\",\"content\":\"neu\\n\"}", link); CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0); CHECK(lstat(link, &st) == 0); CHECK(S_ISLNK(st.st_mode)); /* still a link */ sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", real); CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0); CHECK(strstr(out.p, "neu") != NULL); /* target written */ } } /* * ---- a read error is reported, not mistaken for the end ---- * * A directory opens fine on Linux and fails on the first read. read() * used to print nothing at all, which is indistinguishable from an * empty file; it now names the error. slurp() (behind edit/write) gets * the same guard: an error there would hand edit() a truncated prefix * to write back over the file. */ { char big2[1600]; sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", dir); CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0); CHECK(strstr(out.p, "read error") != NULL); /* the same failure mid-read, not at the very first byte */ sp_buf_clear(&out); CHECK(tool(&r, "read")->run("{\"path\":\"/proc/self/mem\"}", &out, NULL) == 0); CHECK(strstr(out.p, "error") != NULL || strstr(out.p, "read error") != NULL); sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\",\"old\":\"x\",\"new\":\"y\"}", dir); CHECK(tool(&r, "edit")->run(big2, &out, NULL) == 0); CHECK(strstr(out.p, "error:") != NULL); CHECK(strstr(out.p, "edited") == NULL); /* slurp() behind edit(): the EIO must not read as "empty" */ sp_buf_clear(&out); CHECK(tool(&r, "edit")->run( "{\"path\":\"/proc/self/mem\",\"old\":\"x\",\"new\":\"y\"}", &out, NULL) == 0); CHECK(strstr(out.p, "Input/output error") != NULL); CHECK(strstr(out.p, "is empty") == NULL); } /* * ---- the file's own mode still decides ---- * * A temp file plus rename() only needs write permission on the * directory, so without a check a 0444 file would be replaced while * fopen(path, "w") had always refused it. And a 0600 file must not * become 0644 just because a tool wrote it. */ { char ro[700], big2[1600]; struct stat st; snprintf(ro, sizeof(ro), "%s/ro.txt", dir); write_file(ro, "alt\n"); CHECK(chmod(ro, 0444) == 0); sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\",\"content\":\"neu\\n\"}", ro); CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0); CHECK(strstr(out.p, "Permission denied") != NULL); sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\"}", ro); CHECK(tool(&r, "read")->run(big2, &out, NULL) == 0); CHECK(strstr(out.p, "alt") != NULL); CHECK(chmod(ro, 0600) == 0); sp_buf_clear(&out); snprintf(big2, sizeof(big2), "{\"path\":\"%s\",\"content\":\"neu\\n\"}", ro); CHECK(tool(&r, "write")->run(big2, &out, NULL) == 0); CHECK(stat(ro, &st) == 0); CHECK((st.st_mode & 07777) == 0600); /* mode kept */ } /* ---- read refuses a binary file ---- */ { FILE *f; char bin[700]; snprintf(bin, sizeof(bin), "%s/bin.dat", dir); f = fopen(bin, "wb"); CHECK(f != NULL); if (f != NULL) { fwrite("abc\0def", 1, 7, f); fclose(f); } sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\"}", bin); CHECK(tool(&r, "read")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "binary") != NULL); } /* ---- search and list ---- */ sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"pattern\":\"zweite\",\"path\":\"%s\"}", dir); CHECK(tool(&r, "search")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "a.txt") != NULL); sp_buf_clear(&out); snprintf(big, sizeof(big), "{\"path\":\"%s\"}", dir); CHECK(tool(&r, "list")->run(big, &out, NULL) == 0); CHECK(strstr(out.p, "a.txt") != NULL); /* ---- missing arguments are reported, never guessed ---- */ sp_buf_clear(&out); CHECK(tool(&r, "read")->run("{}", &out, NULL) == 0); CHECK(strstr(out.p, "required") != NULL); /* * ---- webfetch: the URL is the model's, so it is data, never shell ---- * The quote used to close curl's '...' and run the rest as a command, * from a tool that needed nobody's consent. */ { char mark[700]; struct stat st; CHECK(tool(&r, "webfetch")->auth != SP_AUTH_READ); snprintf(mark, sizeof(mark), "%s/pwned", dir); snprintf(big, sizeof(big), "{\"url\":\"http://127.0.0.1:9/'; " "touch %s; echo '\"}", mark); sp_buf_clear(&out); CHECK(tool(&r, "webfetch")->run(big, &out, NULL) == 0); CHECK(stat(mark, &st) != 0); sp_buf_clear(&out); CHECK(tool(&r, "webfetch")->run("{\"url\":\"file:///etc/passwd\"}", &out, NULL) == 0); CHECK(strstr(out.p, "only http") != NULL); CHECK(strstr(out.p, "root:") == NULL); sp_buf_clear(&out); CHECK(tool(&r, "webfetch")->run("{\"url\":\"-K/etc/passwd\"}", &out, NULL) == 0); CHECK(strstr(out.p, "only http") != NULL); } copy = out.p; CHECK(copy != NULL); /* * ---- tools_root: the file tools stay inside it ---- * * Without a root a model with mutate rights can write anything the * process can: ~/.ssh/authorized_keys, the config that names the * plugins. The classes say *whether* a tool may run, not *where* - so * the root is the boundary, and it has to hold for every entry the * model can spell: an absolute path outside, a "../.." walk, and a * symlink inside that points outside. */ { struct sp_registry r2; struct sp_buf out2; char root[700], outside[700], link[700]; char req[1600]; struct stat st; snprintf(root, sizeof(root), "%s/rootdir", dir); snprintf(outside, sizeof(outside), "%s/outside.txt", dir); snprintf(link, sizeof(link), "%s/rootdir/escape", dir); CHECK(mkdir(root, 0700) == 0); write_file(outside, "geheim\n"); CHECK(symlink(outside, link) == 0); memset(&r2, 0, sizeof(r2)); if (sp_buf_init(&out2) != 0) return 1; CHECK(sp_builtin_register(&r2, root) == 0); /* inside: allowed, and it works */ sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"path\":\"%s/drin.txt\",\"content\":\"ok\"}", root); CHECK(tool(&r2, "write")->run(req, &out2, tool_ctx(&r2, "write")) == 0); CHECK(strstr(out2.p, "wrote") != NULL); /* absolute path outside: refused, and nothing was written */ sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"path\":\"%s/pwned.txt\",\"content\":\"x\"}", outside); CHECK(tool(&r2, "write")->run(req, &out2, tool_ctx(&r2, "write")) == 0); CHECK(strstr(out2.p, "outside the tool root") != NULL); CHECK(stat(outside, &st) == 0); CHECK(st.st_size == 7); /* "geheim\n" - not overwritten */ /* ../.. walk: refused */ sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"path\":\"%s/../../pwned2.txt\",\"content\":\"x\"}", root); CHECK(tool(&r2, "write")->run(req, &out2, tool_ctx(&r2, "write")) == 0); CHECK(strstr(out2.p, "outside the tool root") != NULL); /* a symlink inside that points outside: refused */ sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"path\":\"%s\"}", link); CHECK(tool(&r2, "read")->run(req, &out2, tool_ctx(&r2, "read")) == 0); CHECK(strstr(out2.p, "outside the tool root") != NULL); CHECK(strstr(out2.p, "geheim") == NULL); /* * A *dangling* link inside the root that points outside: realpath() * cannot resolve it (the target does not exist yet), so a check that * only looked at the directory would say "inside" - and the write * then follows the link out of the root. This is the case that made * the fence worthless, so it is tested separately. */ { char dangling[700], victim[700]; snprintf(dangling, sizeof(dangling), "%s/rootdir/dangling", dir); snprintf(victim, sizeof(victim), "%s/created-by-model.txt", dir); CHECK(symlink(victim, dangling) == 0); sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"path\":\"%s\",\"content\":\"pwned\\n\"}", dangling); CHECK(tool(&r2, "write")->run(req, &out2, tool_ctx(&r2, "write")) == 0); CHECK(strstr(out2.p, "outside the tool root") != NULL); CHECK(access(victim, F_OK) != 0); /* not created */ } /* read, search and list are fenced too */ sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"path\":\"%s\"}", outside); CHECK(tool(&r2, "read")->run(req, &out2, tool_ctx(&r2, "read")) == 0); CHECK(strstr(out2.p, "outside the tool root") != NULL); sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"pattern\":\"geheim\",\"path\":\"%s\"}", dir); CHECK(tool(&r2, "search")->run(req, &out2, tool_ctx(&r2, "search")) == 0); CHECK(strstr(out2.p, "outside the tool root") != NULL); sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"path\":\"%s\"}", dir); CHECK(tool(&r2, "list")->run(req, &out2, tool_ctx(&r2, "list")) == 0); CHECK(strstr(out2.p, "outside the tool root") != NULL); /* * search recurses, and stat() follows a directory symlink: a link * inside the root that points outside would be walked and its files * read. Guarding the argument alone is not enough, so every entry is * resolved too. */ { char outside_dir[700], link_out[700], secret[800]; char inner[700]; snprintf(outside_dir, sizeof(outside_dir), "%s/secretdir", dir); snprintf(secret, sizeof(secret), "%s/note.txt", outside_dir); if (mkdir(outside_dir, 0700) == 0) { write_file(secret, "GEHEIM-NOTE\n"); snprintf(link_out, sizeof(link_out), "%s/rootdir/linkout", dir); CHECK(symlink(outside_dir, link_out) == 0); sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"pattern\":\"GEHEIM\",\"path\":\"%s\"}", root); CHECK(tool(&r2, "search")->run(req, &out2, tool_ctx(&r2, "search")) == 0); CHECK(strstr(out2.p, "GEHEIM-NOTE") == NULL); } /* a search that stays inside still finds what is there */ snprintf(inner, sizeof(inner), "%s/rootdir/inner.txt", dir); write_file(inner, "TREFFER-DRINN\n"); sp_buf_clear(&out2); snprintf(req, sizeof(req), "{\"pattern\":\"TREFFER\",\"path\":\"%s\"}", root); CHECK(tool(&r2, "search")->run(req, &out2, tool_ctx(&r2, "search")) == 0); CHECK(strstr(out2.p, "TREFFER-DRINN") != NULL); } sp_registry_free(&r2); sp_buf_free(&out2); /* the tree outside the root was never touched by a refusal */ CHECK(access(outside, F_OK) == 0); } sp_registry_free(&r); sp_buf_free(&out); { char cmd[700]; snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir); if (system(cmd) != 0) fprintf(stderr, "cleanup failed: %s\n", strerror(errno)); } if (failures == 0) printf("test_builtin: ok\n"); return failures == 0 ? 0 : 1; }