/* * The ack mechanism: a request is a file, an answer is a file, and any process * may write it. Silence is not consent. */ #include #include #include #include #include #include #include #include #include "ack.h" #include "splinter.h" static int checks, fails; #define CHECK(cond, what) do { \ checks++; \ if (!(cond)) { \ fails++; \ printf("FAIL %s (line %d)\n", what, __LINE__); \ } else { \ printf("ok %s\n", what); \ } \ } while (0) static int answer_id; static int answer_yes; /* * Two threads each ask sp_ack_dir() for the path and keep the pointer they got. * A per-thread buffer gives two different addresses; one shared static gives the * same address twice, which is the bug this guards. */ static struct { const char *want; unsigned long got; /* the address this thread was handed */ } two_dirs[2]; static void * dir_holder(void *arg) { int idx = (int)(long)arg; const char *p; p = sp_ack_dir(); /* the address only: the thread's own buffer dies with the thread */ if (p != NULL && strcmp(p, two_dirs[idx].want) == 0) two_dirs[idx].got = (unsigned long)(uintptr_t)p; return NULL; } static void * answerer(void *arg) { (void)arg; { struct timespec ts = { 0, 300000000L }; (void)nanosleep(&ts, NULL); } (void)sp_ack_answer(answer_id, answer_yes, "test-phone"); return NULL; } int main(void) { char out[2048], who[64]; char dir[] = "/var/tmp/splinter-ack-XXXXXX"; char open_dir[] = "/var/tmp/splinter-ack-open-XXXXXX"; struct sp_buf log; int id, yes; pthread_t th; CHECK(mkdtemp(dir) != NULL, "a private ack directory"); (void)setenv("SPLINTER_ACK_DIR", dir, 1); id = sp_ack_request("run", "{\"command\":\"ls\"}", "mutate", 5); CHECK(id > 0, "request is written and returns an id"); /* the answer comes from another thread, like another process would */ answer_id = id; answer_yes = 1; (void)pthread_create(&th, NULL, answerer, NULL); yes = sp_ack_wait(id, 10, who, sizeof(who)); (void)pthread_join(th, NULL); CHECK(yes == 1, "an answer of yes is read back"); CHECK(strcmp(who, "test-phone") == 0, "the answer says who gave it"); id = sp_ack_request("run", "{\"command\":\"rm -rf /\"}", "destructive", 1); answer_id = id; answer_yes = 0; (void)pthread_create(&th, NULL, answerer, NULL); yes = sp_ack_wait(id, 10, who, sizeof(who)); (void)pthread_join(th, NULL); CHECK(yes == 0, "an answer of no is read back as no"); id = sp_ack_request("run", "slow", "mutate", 1); CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0, "a timeout is a no, not a yes"); CHECK(sp_ack_answer(999999, 1, "nobody") != 0, "answering a request that does not exist fails"); id = sp_ack_request("edit", "{\"path\":\"/etc/passwd\"}", "mutate", 30); if (sp_buf_init(&log) == 0) { (void)sp_ack_list(&log); snprintf(out, sizeof(out), "%s", log.p != NULL ? log.p : ""); sp_buf_free(&log); CHECK(strstr(out, "\"path\":\"/etc/passwd\"") != NULL, "the pending list shows what is being asked"); } (void)sp_ack_answer(id, 0, "test"); /* * ---- an answer belongs to one request, once, in time ---- * The id used to be the pid: a "yes" that came too late stayed on disk * and approved the next request of the same process unseen. */ { int a, b; a = sp_ack_request("run", "first", "destructive", 1); b = sp_ack_request("run", "second", "destructive", 1); CHECK(a > 0 && b > 0 && a != b, "two requests of one process get two ids"); CHECK(a != (int)getpid(), "the id is not the pid"); (void)sp_ack_answer(b, 0, "test"); (void)sp_ack_wait(b, 1, who, sizeof(who)); CHECK(sp_ack_wait(a, 1, who, sizeof(who)) == 0, "an unanswered request times out as no"); CHECK(sp_ack_answer(a, 1, "late") != 0, "an answer after the timeout is refused"); id = sp_ack_request("run", "third", "destructive", 1); CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0, "a late yes does not approve the next request"); id = sp_ack_request("run", "fourth", "destructive", 5); CHECK(sp_ack_answer(id, 0, "first") == 0, "the first answer counts"); CHECK(sp_ack_answer(id, 1, "second") != 0, "a second answer to the same request is refused"); CHECK(sp_ack_wait(id, 5, who, sizeof(who)) == 0 && strcmp(who, "first") == 0, "the first answer wins"); } /* * ---- the channel message says what is approved, and is only data ---- * It used to be "Zustimmung " alone, so a phone said yes to a * number. */ { char chan[700], got[700], mark[700], args[1200]; FILE *f; char msg[1024] = ""; snprintf(chan, sizeof(chan), "%s/chan.sh", dir); snprintf(got, sizeof(got), "%s/chan.out", dir); snprintf(mark, sizeof(mark), "%s/pwned", dir); f = fopen(chan, "w"); if (f != NULL) { fprintf(f, "#!/bin/sh\nprintf '%%s' \"$1\" > %s\n", got); fclose(f); } (void)chmod(chan, 0700); (void)setenv("SPLINTER_ACK_CHANNELS", chan, 1); snprintf(args, sizeof(args), "{\"command\":\"x'; touch %s; '\"}", mark); id = sp_ack_request("run", args, "destructive", 5); CHECK(sp_ack_notify(id, NULL) == 1, "the channel was fired"); f = fopen(got, "r"); if (f != NULL) { size_t n = fread(msg, 1, sizeof(msg) - 1, f); msg[n] = '\0'; fclose(f); } CHECK(strstr(msg, "run") != NULL && strstr(msg, "touch") != NULL, "the message names the tool and its arguments"); CHECK(access(mark, F_OK) != 0, "a quote in the arguments runs nothing"); (void)sp_ack_answer(id, 0, "test"); (void)sp_ack_wait(id, 1, who, sizeof(who)); (void)unsetenv("SPLINTER_ACK_CHANNELS"); } /* * ---- the ack root is per thread ---- * sp_ack_dir() used to return one static buffer, sprintf'd by whoever * asked last, so a waiter in one thread and an answerer in another (the * gateway does both) read each other's path. The contract is now one * buffer per thread; two threads returning the same address means they * share it again, and the value one of them reads can change under it. */ { pthread_t ta, tb; two_dirs[0].want = dir; two_dirs[1].want = dir; (void)pthread_create(&ta, NULL, dir_holder, (void *)0L); (void)pthread_create(&tb, NULL, dir_holder, (void *)1L); (void)pthread_join(ta, NULL); (void)pthread_join(tb, NULL); CHECK(two_dirs[0].got != 0 && two_dirs[1].got != 0, "both threads get an ack path"); CHECK(two_dirs[0].got != two_dirs[1].got, "the two threads do not share one buffer"); } /* ---- a directory others may write into is not trusted with a yes ---- */ CHECK(mkdtemp(open_dir) != NULL, "a directory for the permission test"); (void)chmod(open_dir, 0777); (void)setenv("SPLINTER_ACK_DIR", open_dir, 1); CHECK(sp_ack_request("run", "x", "destructive", 1) < 0, "a world-writable ack directory is refused"); { char cmd[700]; snprintf(cmd, sizeof(cmd), "rm -rf '%s' '%s'", dir, open_dir); (void)system(cmd); } printf("\n%d checks, %d failures\n", checks, fails); return fails == 0 ? 0 : 1; }