^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sys/sysmacros.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <sys/user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <sys/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <sys/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/seccomp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int seccomp(unsigned int op, unsigned int flags, void *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return syscall(__NR_seccomp, op, flags, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int send_fd(int sock, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct msghdr msg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct cmsghdr *cmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct iovec io = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .iov_base = &c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .iov_len = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) msg.msg_iov = &io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) msg.msg_iovlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) msg.msg_control = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) msg.msg_controllen = sizeof(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) cmsg = CMSG_FIRSTHDR(&msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) cmsg->cmsg_level = SOL_SOCKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) cmsg->cmsg_type = SCM_RIGHTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) cmsg->cmsg_len = CMSG_LEN(sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *((int *)CMSG_DATA(cmsg)) = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) msg.msg_controllen = cmsg->cmsg_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (sendmsg(sock, &msg, 0) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) perror("sendmsg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int recv_fd(int sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct msghdr msg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct cmsghdr *cmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct iovec io = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .iov_base = &c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .iov_len = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) msg.msg_iov = &io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) msg.msg_iovlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) msg.msg_control = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) msg.msg_controllen = sizeof(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (recvmsg(sock, &msg, 0) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) perror("recvmsg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) cmsg = CMSG_FIRSTHDR(&msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return *((int *)CMSG_DATA(cmsg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int user_trap_syscall(int nr, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct sock_filter filter[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) offsetof(struct seccomp_data, nr)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, nr, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_USER_NOTIF),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct sock_fprog prog = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .len = (unsigned short)ARRAY_SIZE(filter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .filter = filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int handle_req(struct seccomp_notif *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct seccomp_notif_resp *resp, int listener)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) char path[PATH_MAX], source[PATH_MAX], target[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret = -1, mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) resp->id = req->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) resp->error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) resp->val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (req->data.nr != __NR_mount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) fprintf(stderr, "huh? trapped something besides mount? %d\n", req->data.nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Only allow bind mounts. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!(req->data.args[3] & MS_BIND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * Ok, let's read the task's memory to see where they wanted their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * mount to go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) snprintf(path, sizeof(path), "/proc/%d/mem", req->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) mem = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (mem < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) perror("open mem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * Now we avoid a TOCTOU: we referred to a pid by its pid, but since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * the pid that made the syscall may have died, we need to confirm that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * the pid is still valid after we open its /proc/pid/mem file. We can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * ask the listener fd this as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Note that this check should occur *after* any task-specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * resources are opened, to make sure that the task has not died and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * we're not wrongly reading someone else's state in order to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * decisions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &req->id) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) fprintf(stderr, "task died before we could map its memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Phew, we've got the right /proc/pid/mem. Now we can read it. Note
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * that to avoid another TOCTOU, we should read all of the pointer args
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * before we decide to allow the syscall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (lseek(mem, req->data.args[0], SEEK_SET) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) perror("seek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ret = read(mem, source, sizeof(source));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (lseek(mem, req->data.args[1], SEEK_SET) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) perror("seek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret = read(mem, target, sizeof(target));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * Our policy is to only allow bind mounts inside /tmp. This isn't very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * interesting, because we could do unprivlieged bind mounts with user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * namespaces already, but you get the idea.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!strncmp(source, "/tmp/", 5) && !strncmp(target, "/tmp/", 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (mount(source, target, NULL, req->data.args[3], NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) perror("actual mount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) resp->error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Even if we didn't allow it because of policy, generating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * response was be a success, because we want to tell the worker EPERM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) close(mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int sk_pair[2], ret = 1, status, listener;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) pid_t worker = 0 , tracer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) perror("socketpair");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) worker = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (worker < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) perror("fork");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) goto close_pair;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (worker == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) listener = user_trap_syscall(__NR_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) SECCOMP_FILTER_FLAG_NEW_LISTENER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (listener < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) perror("seccomp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * Drop privileges. We definitely can't mount as uid 1000.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (setuid(1000) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) perror("setuid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * Send the listener to the parent; also serves as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * synchronization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (send_fd(sk_pair[1], listener) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) close(listener);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (mkdir("/tmp/foo", 0755) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) perror("mkdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Try a bad mount just for grins.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (mount("/dev/sda", "/tmp/foo", NULL, 0, NULL) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) fprintf(stderr, "huh? mounted /dev/sda?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (errno != EPERM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) perror("bad error from mount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * Ok, we expect this one to succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (mount("/tmp/foo", "/tmp/foo", NULL, MS_BIND, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) perror("mount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Get the listener from the child.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) listener = recv_fd(sk_pair[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (listener < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) goto out_kill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * Fork a task to handle the requests. This isn't strictly necessary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * but it makes the particular writing of this sample easier, since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * can just wait ofr the tracee to exit and kill the tracer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) tracer = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (tracer < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) perror("fork");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) goto out_kill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (tracer == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct seccomp_notif *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct seccomp_notif_resp *resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct seccomp_notif_sizes sizes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (seccomp(SECCOMP_GET_NOTIF_SIZES, 0, &sizes) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) perror("seccomp(GET_NOTIF_SIZES)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) req = malloc(sizes.seccomp_notif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) resp = malloc(sizes.seccomp_notif_resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto out_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) memset(resp, 0, sizes.seccomp_notif_resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) memset(req, 0, sizes.seccomp_notif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) perror("ioctl recv");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto out_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (handle_req(req, resp, listener) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto out_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * ENOENT here means that the task may have gotten a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * signal and restarted the syscall. It's up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * handler to decide what to do in this case, but for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * the sample code, we just ignore it. Probably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * something better should happen, like undoing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * mount, or keeping track of the args to make sure we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * don't do it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, resp) < 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) errno != ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) perror("ioctl send");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) goto out_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) out_resp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) free(resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) out_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) close(listener);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) close(listener);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (waitpid(worker, &status, 0) != worker) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) perror("waitpid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) goto out_kill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (umount2("/tmp/foo", MNT_DETACH) < 0 && errno != EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) perror("umount2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto out_kill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (remove("/tmp/foo") < 0 && errno != ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) perror("remove");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!WIFEXITED(status) || WEXITSTATUS(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) fprintf(stderr, "worker exited nonzero\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto out_kill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) out_kill:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (tracer > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) kill(tracer, SIGKILL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (worker > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) kill(worker, SIGKILL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) close_pair:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) close(sk_pair[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) close(sk_pair[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }