1 // SPDX-License-Identifier: GPL-2.0
3 * Naive system call dropper built on seccomp_filter.
5 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
6 * Author: Will Drewry <wad@chromium.org>
8 * The code may be used by anyone for any purpose,
9 * and can serve as a starting point for developing
10 * applications using prctl(PR_SET_SECCOMP, 2, ...).
12 * When run, returns the specified errno for the specified
13 * system call number against the given architecture.
18 #include <linux/audit.h>
19 #include <linux/filter.h>
20 #include <linux/seccomp.h>
21 #include <linux/unistd.h>
25 #include <sys/prctl.h>
28 static int install_filter(int arch, int nr, int error)
30 struct sock_filter filter[] = {
31 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
32 (offsetof(struct seccomp_data, arch))),
33 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, arch, 0, 3),
34 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
35 (offsetof(struct seccomp_data, nr))),
36 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, nr, 0, 1),
37 BPF_STMT(BPF_RET+BPF_K,
38 SECCOMP_RET_ERRNO|(error & SECCOMP_RET_DATA)),
39 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
41 struct sock_fprog prog = {
42 .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
46 struct sock_filter kill = BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL);
49 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
50 perror("prctl(NO_NEW_PRIVS)");
53 if (prctl(PR_SET_SECCOMP, 2, &prog)) {
54 perror("prctl(PR_SET_SECCOMP)");
60 int main(int argc, char **argv)
63 fprintf(stderr, "Usage:\n"
64 "dropper <arch> <syscall_nr> <errno> <prog> [<args>]\n"
65 "Hint: AUDIT_ARCH_I386: 0x%X\n"
66 " AUDIT_ARCH_X86_64: 0x%X\n"
67 " errno == -1 means SECCOMP_RET_KILL\n"
68 "\n", AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
71 if (install_filter(strtol(argv[1], NULL, 0), strtol(argv[2], NULL, 0),
72 strtol(argv[3], NULL, 0)))
74 execv(argv[4], &argv[4]);
75 printf("Failed to execv\n");