/* * krang -- offline test of the two one-way layers. * * Copyright (c) 2026 Konrad Lother * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * This does not chroot and does not need root. It applies the real Landlock * and seccomp layers to a child, then proves: * - a write under the workspace is allowed; * - a write under $HOME (writable by the user, outside the allowed set) is * denied by the path layer; * - mount(2) is denied by the syscall layer; * - fork/execve still work, because the run tool needs them. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "krang.h" static int network_ok(void) { struct addrinfo hints, *res = NULL; struct sockaddr_in sa; socklen_t sl = sizeof(sa); int l, c, a; char byte = 'x'; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo("localhost", "80", &hints, &res) != 0 || res == NULL) return 0; freeaddrinfo(res); l = socket(AF_INET, SOCK_STREAM, 0); if (l < 0) return 0; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); if (bind(l, (struct sockaddr *)&sa, sizeof(sa)) != 0 || listen(l, 4) != 0 || getsockname(l, (struct sockaddr *)&sa, &sl) != 0) { close(l); return 0; } c = socket(AF_INET, SOCK_STREAM, 0); if (c < 0 || connect(c, (struct sockaddr *)&sa, sizeof(sa)) != 0) { if (c >= 0) close(c); close(l); return 0; } a = accept(l, NULL, NULL); if (a < 0 || write(c, &byte, 1) != 1 || read(a, &byte, 1) != 1) { if (a >= 0) close(a); close(c); close(l); return 0; } close(a); close(c); close(l); return 1; } static int write_ok(const char *path) { int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); if (fd < 0) return 0; (void)write(fd, "x", 1); close(fd); return 1; } int main(void) { char root[] = "/tmp/krang-sandbox-XXXXXX"; char ws[600], st[600], tmp[600], allowed[700], home[700]; char err[512]; struct stat sb; pid_t pid; int status; if (mkdtemp(root) == NULL) { perror("mkdtemp"); return 1; } snprintf(ws, sizeof(ws), "%s/workspace", root); snprintf(st, sizeof(st), "%s/state", root); snprintf(tmp, sizeof(tmp), "%s/tmp", root); (void)mkdir(ws, 0700); (void)mkdir(st, 0700); (void)mkdir(tmp, 0700); snprintf(allowed, sizeof(allowed), "%s/ok", ws); snprintf(home, sizeof(home), "%s/krang-denied-%ld", getenv("HOME") != NULL ? getenv("HOME") : "/tmp", (long)getpid()); pid = fork(); if (pid < 0) { perror("fork"); return 1; } if (pid == 0) { if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { perror("no_new_privs"); _exit(1); } if (ksandbox_apply_landlock(ws, st, tmp, 1, err, sizeof(err)) < 0) { fprintf(stderr, "landlock: %s\n", err); _exit(1); } if (!write_ok(allowed)) { fprintf(stderr, "workspace write denied: %s\n", strerror(errno)); _exit(1); } if (write_ok(home)) { fprintf(stderr, "HOME write was not denied\n"); _exit(1); } if (ksandbox_apply_seccomp(1, err, sizeof(err)) < 0) { fprintf(stderr, "seccomp: %s\n", err); _exit(1); } errno = 0; if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) == 0) { fprintf(stderr, "mount was not denied\n"); _exit(1); } if (errno != EPERM && errno != EACCES) { fprintf(stderr, "mount: unexpected errno %d\n", errno); _exit(1); } if (!network_ok()) { fprintf(stderr, "network after seccomp failed\n"); _exit(1); } if (!write_ok(allowed)) { fprintf(stderr, "workspace write after seccomp: %s\n", strerror(errno)); _exit(1); } { pid_t c = fork(); if (c < 0) { fprintf(stderr, "fork after seccomp: %s\n", strerror(errno)); _exit(1); } if (c == 0) { execl("/bin/true", "true", (char *)NULL); _exit(127); } if (waitpid(c, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { fprintf(stderr, "exec after seccomp failed\n"); _exit(1); } } _exit(0); } if (waitpid(pid, &status, 0) < 0) { perror("waitpid"); return 1; } if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { fprintf(stderr, "test_sandbox: child failed (%d)\n", status); return 1; } (void)sb; printf("test_sandbox: ok\n"); return 0; }