1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28ac270d1SWill Drewry /*
38ac270d1SWill Drewry * Seccomp filter example for x86 (32-bit and 64-bit) with BPF macros
48ac270d1SWill Drewry *
58ac270d1SWill Drewry * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
68ac270d1SWill Drewry * Author: Will Drewry <wad@chromium.org>
78ac270d1SWill Drewry *
88ac270d1SWill Drewry * The code may be used by anyone for any purpose,
98ac270d1SWill Drewry * and can serve as a starting point for developing
108ac270d1SWill Drewry * applications using prctl(PR_SET_SECCOMP, 2, ...).
118ac270d1SWill Drewry */
12561381a1SWill Drewry #if defined(__i386__) || defined(__x86_64__)
13561381a1SWill Drewry #define SUPPORTED_ARCH 1
14561381a1SWill Drewry #endif
15561381a1SWill Drewry
16561381a1SWill Drewry #if defined(SUPPORTED_ARCH)
178ac270d1SWill Drewry #define __USE_GNU 1
188ac270d1SWill Drewry #define _GNU_SOURCE 1
198ac270d1SWill Drewry
208ac270d1SWill Drewry #include <linux/types.h>
218ac270d1SWill Drewry #include <linux/filter.h>
228ac270d1SWill Drewry #include <linux/seccomp.h>
238ac270d1SWill Drewry #include <linux/unistd.h>
248ac270d1SWill Drewry #include <signal.h>
258ac270d1SWill Drewry #include <stdio.h>
268ac270d1SWill Drewry #include <stddef.h>
278ac270d1SWill Drewry #include <string.h>
288ac270d1SWill Drewry #include <sys/prctl.h>
298ac270d1SWill Drewry #include <unistd.h>
308ac270d1SWill Drewry
318ac270d1SWill Drewry #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
328ac270d1SWill Drewry #define syscall_nr (offsetof(struct seccomp_data, nr))
338ac270d1SWill Drewry
348ac270d1SWill Drewry #if defined(__i386__)
358ac270d1SWill Drewry #define REG_RESULT REG_EAX
368ac270d1SWill Drewry #define REG_SYSCALL REG_EAX
378ac270d1SWill Drewry #define REG_ARG0 REG_EBX
388ac270d1SWill Drewry #define REG_ARG1 REG_ECX
398ac270d1SWill Drewry #define REG_ARG2 REG_EDX
408ac270d1SWill Drewry #define REG_ARG3 REG_ESI
418ac270d1SWill Drewry #define REG_ARG4 REG_EDI
428ac270d1SWill Drewry #define REG_ARG5 REG_EBP
438ac270d1SWill Drewry #elif defined(__x86_64__)
448ac270d1SWill Drewry #define REG_RESULT REG_RAX
458ac270d1SWill Drewry #define REG_SYSCALL REG_RAX
468ac270d1SWill Drewry #define REG_ARG0 REG_RDI
478ac270d1SWill Drewry #define REG_ARG1 REG_RSI
488ac270d1SWill Drewry #define REG_ARG2 REG_RDX
498ac270d1SWill Drewry #define REG_ARG3 REG_R10
508ac270d1SWill Drewry #define REG_ARG4 REG_R8
518ac270d1SWill Drewry #define REG_ARG5 REG_R9
528ac270d1SWill Drewry #endif
538ac270d1SWill Drewry
548ac270d1SWill Drewry #ifndef PR_SET_NO_NEW_PRIVS
558ac270d1SWill Drewry #define PR_SET_NO_NEW_PRIVS 38
568ac270d1SWill Drewry #endif
578ac270d1SWill Drewry
588ac270d1SWill Drewry #ifndef SYS_SECCOMP
598ac270d1SWill Drewry #define SYS_SECCOMP 1
608ac270d1SWill Drewry #endif
618ac270d1SWill Drewry
emulator(int nr,siginfo_t * info,void * void_context)628ac270d1SWill Drewry static void emulator(int nr, siginfo_t *info, void *void_context)
638ac270d1SWill Drewry {
648ac270d1SWill Drewry ucontext_t *ctx = (ucontext_t *)(void_context);
658ac270d1SWill Drewry int syscall;
668ac270d1SWill Drewry char *buf;
678ac270d1SWill Drewry ssize_t bytes;
688ac270d1SWill Drewry size_t len;
698ac270d1SWill Drewry if (info->si_code != SYS_SECCOMP)
708ac270d1SWill Drewry return;
718ac270d1SWill Drewry if (!ctx)
728ac270d1SWill Drewry return;
738ac270d1SWill Drewry syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
748ac270d1SWill Drewry buf = (char *) ctx->uc_mcontext.gregs[REG_ARG1];
758ac270d1SWill Drewry len = (size_t) ctx->uc_mcontext.gregs[REG_ARG2];
768ac270d1SWill Drewry
778ac270d1SWill Drewry if (syscall != __NR_write)
788ac270d1SWill Drewry return;
798ac270d1SWill Drewry if (ctx->uc_mcontext.gregs[REG_ARG0] != STDERR_FILENO)
808ac270d1SWill Drewry return;
818ac270d1SWill Drewry /* Redirect stderr messages to stdout. Doesn't handle EINTR, etc */
828ac270d1SWill Drewry ctx->uc_mcontext.gregs[REG_RESULT] = -1;
838ac270d1SWill Drewry if (write(STDOUT_FILENO, "[ERR] ", 6) > 0) {
848ac270d1SWill Drewry bytes = write(STDOUT_FILENO, buf, len);
858ac270d1SWill Drewry ctx->uc_mcontext.gregs[REG_RESULT] = bytes;
868ac270d1SWill Drewry }
878ac270d1SWill Drewry return;
888ac270d1SWill Drewry }
898ac270d1SWill Drewry
install_emulator(void)908ac270d1SWill Drewry static int install_emulator(void)
918ac270d1SWill Drewry {
928ac270d1SWill Drewry struct sigaction act;
938ac270d1SWill Drewry sigset_t mask;
948ac270d1SWill Drewry memset(&act, 0, sizeof(act));
958ac270d1SWill Drewry sigemptyset(&mask);
968ac270d1SWill Drewry sigaddset(&mask, SIGSYS);
978ac270d1SWill Drewry
988ac270d1SWill Drewry act.sa_sigaction = &emulator;
998ac270d1SWill Drewry act.sa_flags = SA_SIGINFO;
1008ac270d1SWill Drewry if (sigaction(SIGSYS, &act, NULL) < 0) {
1018ac270d1SWill Drewry perror("sigaction");
1028ac270d1SWill Drewry return -1;
1038ac270d1SWill Drewry }
1048ac270d1SWill Drewry if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
1058ac270d1SWill Drewry perror("sigprocmask");
1068ac270d1SWill Drewry return -1;
1078ac270d1SWill Drewry }
1088ac270d1SWill Drewry return 0;
1098ac270d1SWill Drewry }
1108ac270d1SWill Drewry
install_filter(void)1118ac270d1SWill Drewry static int install_filter(void)
1128ac270d1SWill Drewry {
1138ac270d1SWill Drewry struct sock_filter filter[] = {
1148ac270d1SWill Drewry /* Grab the system call number */
1158ac270d1SWill Drewry BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_nr),
1168ac270d1SWill Drewry /* Jump table for the allowed syscalls */
1178ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_rt_sigreturn, 0, 1),
1188ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
1198ac270d1SWill Drewry #ifdef __NR_sigreturn
1208ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_sigreturn, 0, 1),
1218ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
1228ac270d1SWill Drewry #endif
1238ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_exit_group, 0, 1),
1248ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
1258ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_exit, 0, 1),
1268ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
1278ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_read, 1, 0),
1288ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_write, 3, 2),
1298ac270d1SWill Drewry
1308ac270d1SWill Drewry /* Check that read is only using stdin. */
1318ac270d1SWill Drewry BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_arg(0)),
1328ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDIN_FILENO, 4, 0),
1338ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
1348ac270d1SWill Drewry
1358ac270d1SWill Drewry /* Check that write is only using stdout */
1368ac270d1SWill Drewry BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_arg(0)),
1378ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDOUT_FILENO, 1, 0),
1388ac270d1SWill Drewry /* Trap attempts to write to stderr */
1398ac270d1SWill Drewry BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDERR_FILENO, 1, 2),
1408ac270d1SWill Drewry
1418ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
1428ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRAP),
1438ac270d1SWill Drewry BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
1448ac270d1SWill Drewry };
1458ac270d1SWill Drewry struct sock_fprog prog = {
1468ac270d1SWill Drewry .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
1478ac270d1SWill Drewry .filter = filter,
1488ac270d1SWill Drewry };
1498ac270d1SWill Drewry
1508ac270d1SWill Drewry if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
1518ac270d1SWill Drewry perror("prctl(NO_NEW_PRIVS)");
1528ac270d1SWill Drewry return 1;
1538ac270d1SWill Drewry }
1548ac270d1SWill Drewry
1558ac270d1SWill Drewry
1568ac270d1SWill Drewry if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
1578ac270d1SWill Drewry perror("prctl");
1588ac270d1SWill Drewry return 1;
1598ac270d1SWill Drewry }
1608ac270d1SWill Drewry return 0;
1618ac270d1SWill Drewry }
1628ac270d1SWill Drewry
1638ac270d1SWill Drewry #define payload(_c) (_c), sizeof((_c))
main(int argc,char ** argv)1648ac270d1SWill Drewry int main(int argc, char **argv)
1658ac270d1SWill Drewry {
1668ac270d1SWill Drewry char buf[4096];
1678ac270d1SWill Drewry ssize_t bytes = 0;
1688ac270d1SWill Drewry if (install_emulator())
1698ac270d1SWill Drewry return 1;
1708ac270d1SWill Drewry if (install_filter())
1718ac270d1SWill Drewry return 1;
1728ac270d1SWill Drewry syscall(__NR_write, STDOUT_FILENO,
1738ac270d1SWill Drewry payload("OHAI! WHAT IS YOUR NAME? "));
1748ac270d1SWill Drewry bytes = syscall(__NR_read, STDIN_FILENO, buf, sizeof(buf));
1758ac270d1SWill Drewry syscall(__NR_write, STDOUT_FILENO, payload("HELLO, "));
1768ac270d1SWill Drewry syscall(__NR_write, STDOUT_FILENO, buf, bytes);
1778ac270d1SWill Drewry syscall(__NR_write, STDERR_FILENO,
1788ac270d1SWill Drewry payload("Error message going to STDERR\n"));
1798ac270d1SWill Drewry return 0;
1808ac270d1SWill Drewry }
181561381a1SWill Drewry #else /* SUPPORTED_ARCH */
182561381a1SWill Drewry /*
183561381a1SWill Drewry * This sample is x86-only. Since kernel samples are compiled with the
184561381a1SWill Drewry * host toolchain, a non-x86 host will result in using only the main()
185561381a1SWill Drewry * below.
186561381a1SWill Drewry */
main(void)187561381a1SWill Drewry int main(void)
188561381a1SWill Drewry {
189561381a1SWill Drewry return 1;
190561381a1SWill Drewry }
191561381a1SWill Drewry #endif /* SUPPORTED_ARCH */
192