9 #include <sys/sysmacros.h>
10 #include <sys/types.h>
12 #include <sys/socket.h>
15 #include <sys/syscall.h>
17 #include <sys/ioctl.h>
18 #include <sys/ptrace.h>
19 #include <sys/mount.h>
20 #include <linux/limits.h>
21 #include <linux/filter.h>
22 #include <linux/seccomp.h>
24 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
26 static int seccomp(unsigned int op, unsigned int flags, void *args)
29 return syscall(__NR_seccomp, op, flags, args);
32 static int send_fd(int sock, int fd)
34 struct msghdr msg = {};
37 char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
45 msg.msg_control = buf;
46 msg.msg_controllen = sizeof(buf);
47 cmsg = CMSG_FIRSTHDR(&msg);
48 cmsg->cmsg_level = SOL_SOCKET;
49 cmsg->cmsg_type = SCM_RIGHTS;
50 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
51 fd_ptr = (int *)CMSG_DATA(cmsg);
53 msg.msg_controllen = cmsg->cmsg_len;
55 if (sendmsg(sock, &msg, 0) < 0) {
63 static int recv_fd(int sock)
65 struct msghdr msg = {};
68 char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
76 msg.msg_control = buf;
77 msg.msg_controllen = sizeof(buf);
79 if (recvmsg(sock, &msg, 0) < 0) {
84 cmsg = CMSG_FIRSTHDR(&msg);
85 fd_ptr = (int *)CMSG_DATA(cmsg);
90 static int user_trap_syscall(int nr, unsigned int flags)
92 struct sock_filter filter[] = {
93 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
94 offsetof(struct seccomp_data, nr)),
95 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, nr, 0, 1),
96 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_USER_NOTIF),
97 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
100 struct sock_fprog prog = {
101 .len = (unsigned short)ARRAY_SIZE(filter),
105 return seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog);
108 static int handle_req(struct seccomp_notif *req,
109 struct seccomp_notif_resp *resp, int listener)
111 char path[PATH_MAX], source[PATH_MAX], target[PATH_MAX];
115 resp->error = -EPERM;
118 if (req->data.nr != __NR_mount) {
119 fprintf(stderr, "huh? trapped something besides mount? %d\n", req->data.nr);
123 /* Only allow bind mounts. */
124 if (!(req->data.args[3] & MS_BIND))
128 * Ok, let's read the task's memory to see where they wanted their
131 snprintf(path, sizeof(path), "/proc/%d/mem", req->pid);
132 mem = open(path, O_RDONLY);
139 * Now we avoid a TOCTOU: we referred to a pid by its pid, but since
140 * the pid that made the syscall may have died, we need to confirm that
141 * the pid is still valid after we open its /proc/pid/mem file. We can
142 * ask the listener fd this as follows.
144 * Note that this check should occur *after* any task-specific
145 * resources are opened, to make sure that the task has not died and
146 * we're not wrongly reading someone else's state in order to make
149 if (ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &req->id) < 0) {
150 fprintf(stderr, "task died before we could map its memory\n");
155 * Phew, we've got the right /proc/pid/mem. Now we can read it. Note
156 * that to avoid another TOCTOU, we should read all of the pointer args
157 * before we decide to allow the syscall.
159 if (lseek(mem, req->data.args[0], SEEK_SET) < 0) {
164 ret = read(mem, source, sizeof(source));
170 if (lseek(mem, req->data.args[1], SEEK_SET) < 0) {
175 ret = read(mem, target, sizeof(target));
182 * Our policy is to only allow bind mounts inside /tmp. This isn't very
183 * interesting, because we could do unprivlieged bind mounts with user
184 * namespaces already, but you get the idea.
186 if (!strncmp(source, "/tmp/", 5) && !strncmp(target, "/tmp/", 5)) {
187 if (mount(source, target, NULL, req->data.args[3], NULL) < 0) {
189 perror("actual mount");
195 /* Even if we didn't allow it because of policy, generating the
196 * response was be a success, because we want to tell the worker EPERM.
207 int sk_pair[2], ret = 1, status, listener;
208 pid_t worker = 0 , tracer = 0;
210 if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) {
211 perror("socketpair");
222 listener = user_trap_syscall(__NR_mount,
223 SECCOMP_FILTER_FLAG_NEW_LISTENER);
230 * Drop privileges. We definitely can't mount as uid 1000.
232 if (setuid(1000) < 0) {
238 * Send the listener to the parent; also serves as
241 if (send_fd(sk_pair[1], listener) < 0)
245 if (mkdir("/tmp/foo", 0755) < 0) {
251 * Try a bad mount just for grins.
253 if (mount("/dev/sda", "/tmp/foo", NULL, 0, NULL) != -1) {
254 fprintf(stderr, "huh? mounted /dev/sda?\n");
258 if (errno != EPERM) {
259 perror("bad error from mount");
264 * Ok, we expect this one to succeed.
266 if (mount("/tmp/foo", "/tmp/foo", NULL, MS_BIND, NULL) < 0) {
275 * Get the listener from the child.
277 listener = recv_fd(sk_pair[0]);
282 * Fork a task to handle the requests. This isn't strictly necessary,
283 * but it makes the particular writing of this sample easier, since we
284 * can just wait ofr the tracee to exit and kill the tracer.
293 struct seccomp_notif *req;
294 struct seccomp_notif_resp *resp;
295 struct seccomp_notif_sizes sizes;
297 if (seccomp(SECCOMP_GET_NOTIF_SIZES, 0, &sizes) < 0) {
298 perror("seccomp(GET_NOTIF_SIZES)");
302 req = malloc(sizes.seccomp_notif);
306 resp = malloc(sizes.seccomp_notif_resp);
309 memset(resp, 0, sizes.seccomp_notif_resp);
312 memset(req, 0, sizes.seccomp_notif);
313 if (ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, req)) {
314 perror("ioctl recv");
318 if (handle_req(req, resp, listener) < 0)
322 * ENOENT here means that the task may have gotten a
323 * signal and restarted the syscall. It's up to the
324 * handler to decide what to do in this case, but for
325 * the sample code, we just ignore it. Probably
326 * something better should happen, like undoing the
327 * mount, or keeping track of the args to make sure we
330 if (ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, resp) < 0 &&
332 perror("ioctl send");
347 if (waitpid(worker, &status, 0) != worker) {
352 if (umount2("/tmp/foo", MNT_DETACH) < 0 && errno != EINVAL) {
357 if (remove("/tmp/foo") < 0 && errno != ENOENT) {
362 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
363 fprintf(stderr, "worker exited nonzero\n");
371 kill(tracer, SIGKILL);
373 kill(worker, SIGKILL);