aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-05-21 23:27:36 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-05-21 23:27:36 -0400
commitcb60e3e65c1b96a4d6444a7a13dc7dd48bc15a2b (patch)
tree4322be35db678f6299348a76ad60a2023954af7d /samples
parent99262a3dafa3290866512ddfb32609198f8973e9 (diff)
parentff2bb047c4bce9742e94911eeb44b4d6ff4734ab (diff)
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull security subsystem updates from James Morris: "New notable features: - The seccomp work from Will Drewry - PR_{GET,SET}_NO_NEW_PRIVS from Andy Lutomirski - Longer security labels for Smack from Casey Schaufler - Additional ptrace restriction modes for Yama by Kees Cook" Fix up trivial context conflicts in arch/x86/Kconfig and include/linux/filter.h * 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (65 commits) apparmor: fix long path failure due to disconnected path apparmor: fix profile lookup for unconfined ima: fix filename hint to reflect script interpreter name KEYS: Don't check for NULL key pointer in key_validate() Smack: allow for significantly longer Smack labels v4 gfp flags for security_inode_alloc()? Smack: recursive tramsmute Yama: replace capable() with ns_capable() TOMOYO: Accept manager programs which do not start with / . KEYS: Add invalidation support KEYS: Do LRU discard in full keyrings KEYS: Permit in-place link replacement in keyring list KEYS: Perform RCU synchronisation on keys prior to key destruction KEYS: Announce key type (un)registration KEYS: Reorganise keys Makefile KEYS: Move the key config into security/keys/Kconfig KEYS: Use the compat keyctl() syscall wrapper on Sparc64 for Sparc32 compat Yama: remove an unused variable samples/seccomp: fix dependencies on arch macros Yama: add additional ptrace scopes ...
Diffstat (limited to 'samples')
-rw-r--r--samples/Makefile2
-rw-r--r--samples/seccomp/Makefile32
-rw-r--r--samples/seccomp/bpf-direct.c190
-rw-r--r--samples/seccomp/bpf-fancy.c102
-rw-r--r--samples/seccomp/bpf-helper.c89
-rw-r--r--samples/seccomp/bpf-helper.h238
-rw-r--r--samples/seccomp/dropper.c68
7 files changed, 720 insertions, 1 deletions
diff --git a/samples/Makefile b/samples/Makefile
index 2f75851ec629..5ef08bba96ce 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,4 +1,4 @@
1# Makefile for Linux samples code 1# Makefile for Linux samples code
2 2
3obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ tracepoints/ trace_events/ \ 3obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ tracepoints/ trace_events/ \
4 hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ 4 hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/
diff --git a/samples/seccomp/Makefile b/samples/seccomp/Makefile
new file mode 100644
index 000000000000..16aa2d424985
--- /dev/null
+++ b/samples/seccomp/Makefile
@@ -0,0 +1,32 @@
1# kbuild trick to avoid linker error. Can be omitted if a module is built.
2obj- := dummy.o
3
4hostprogs-$(CONFIG_SECCOMP_FILTER) := bpf-fancy dropper bpf-direct
5
6HOSTCFLAGS_bpf-fancy.o += -I$(objtree)/usr/include
7HOSTCFLAGS_bpf-fancy.o += -idirafter $(objtree)/include
8HOSTCFLAGS_bpf-helper.o += -I$(objtree)/usr/include
9HOSTCFLAGS_bpf-helper.o += -idirafter $(objtree)/include
10bpf-fancy-objs := bpf-fancy.o bpf-helper.o
11
12HOSTCFLAGS_dropper.o += -I$(objtree)/usr/include
13HOSTCFLAGS_dropper.o += -idirafter $(objtree)/include
14dropper-objs := dropper.o
15
16HOSTCFLAGS_bpf-direct.o += -I$(objtree)/usr/include
17HOSTCFLAGS_bpf-direct.o += -idirafter $(objtree)/include
18bpf-direct-objs := bpf-direct.o
19
20# Try to match the kernel target.
21ifeq ($(CONFIG_64BIT),)
22HOSTCFLAGS_bpf-direct.o += -m32
23HOSTCFLAGS_dropper.o += -m32
24HOSTCFLAGS_bpf-helper.o += -m32
25HOSTCFLAGS_bpf-fancy.o += -m32
26HOSTLOADLIBES_bpf-direct += -m32
27HOSTLOADLIBES_bpf-fancy += -m32
28HOSTLOADLIBES_dropper += -m32
29endif
30
31# Tell kbuild to always build the programs
32always := $(hostprogs-y)
diff --git a/samples/seccomp/bpf-direct.c b/samples/seccomp/bpf-direct.c
new file mode 100644
index 000000000000..151ec3f52189
--- /dev/null
+++ b/samples/seccomp/bpf-direct.c
@@ -0,0 +1,190 @@
1/*
2 * Seccomp filter example for x86 (32-bit and 64-bit) with BPF macros
3 *
4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
5 * Author: Will Drewry <wad@chromium.org>
6 *
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using prctl(PR_SET_SECCOMP, 2, ...).
10 */
11#if defined(__i386__) || defined(__x86_64__)
12#define SUPPORTED_ARCH 1
13#endif
14
15#if defined(SUPPORTED_ARCH)
16#define __USE_GNU 1
17#define _GNU_SOURCE 1
18
19#include <linux/types.h>
20#include <linux/filter.h>
21#include <linux/seccomp.h>
22#include <linux/unistd.h>
23#include <signal.h>
24#include <stdio.h>
25#include <stddef.h>
26#include <string.h>
27#include <sys/prctl.h>
28#include <unistd.h>
29
30#define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
31#define syscall_nr (offsetof(struct seccomp_data, nr))
32
33#if defined(__i386__)
34#define REG_RESULT REG_EAX
35#define REG_SYSCALL REG_EAX
36#define REG_ARG0 REG_EBX
37#define REG_ARG1 REG_ECX
38#define REG_ARG2 REG_EDX
39#define REG_ARG3 REG_ESI
40#define REG_ARG4 REG_EDI
41#define REG_ARG5 REG_EBP
42#elif defined(__x86_64__)
43#define REG_RESULT REG_RAX
44#define REG_SYSCALL REG_RAX
45#define REG_ARG0 REG_RDI
46#define REG_ARG1 REG_RSI
47#define REG_ARG2 REG_RDX
48#define REG_ARG3 REG_R10
49#define REG_ARG4 REG_R8
50#define REG_ARG5 REG_R9
51#endif
52
53#ifndef PR_SET_NO_NEW_PRIVS
54#define PR_SET_NO_NEW_PRIVS 38
55#endif
56
57#ifndef SYS_SECCOMP
58#define SYS_SECCOMP 1
59#endif
60
61static void emulator(int nr, siginfo_t *info, void *void_context)
62{
63 ucontext_t *ctx = (ucontext_t *)(void_context);
64 int syscall;
65 char *buf;
66 ssize_t bytes;
67 size_t len;
68 if (info->si_code != SYS_SECCOMP)
69 return;
70 if (!ctx)
71 return;
72 syscall = ctx->uc_mcontext.gregs[REG_SYSCALL];
73 buf = (char *) ctx->uc_mcontext.gregs[REG_ARG1];
74 len = (size_t) ctx->uc_mcontext.gregs[REG_ARG2];
75
76 if (syscall != __NR_write)
77 return;
78 if (ctx->uc_mcontext.gregs[REG_ARG0] != STDERR_FILENO)
79 return;
80 /* Redirect stderr messages to stdout. Doesn't handle EINTR, etc */
81 ctx->uc_mcontext.gregs[REG_RESULT] = -1;
82 if (write(STDOUT_FILENO, "[ERR] ", 6) > 0) {
83 bytes = write(STDOUT_FILENO, buf, len);
84 ctx->uc_mcontext.gregs[REG_RESULT] = bytes;
85 }
86 return;
87}
88
89static int install_emulator(void)
90{
91 struct sigaction act;
92 sigset_t mask;
93 memset(&act, 0, sizeof(act));
94 sigemptyset(&mask);
95 sigaddset(&mask, SIGSYS);
96
97 act.sa_sigaction = &emulator;
98 act.sa_flags = SA_SIGINFO;
99 if (sigaction(SIGSYS, &act, NULL) < 0) {
100 perror("sigaction");
101 return -1;
102 }
103 if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
104 perror("sigprocmask");
105 return -1;
106 }
107 return 0;
108}
109
110static int install_filter(void)
111{
112 struct sock_filter filter[] = {
113 /* Grab the system call number */
114 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_nr),
115 /* Jump table for the allowed syscalls */
116 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_rt_sigreturn, 0, 1),
117 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
118#ifdef __NR_sigreturn
119 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_sigreturn, 0, 1),
120 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
121#endif
122 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_exit_group, 0, 1),
123 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
124 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_exit, 0, 1),
125 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
126 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_read, 1, 0),
127 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_write, 3, 2),
128
129 /* Check that read is only using stdin. */
130 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_arg(0)),
131 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDIN_FILENO, 4, 0),
132 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
133
134 /* Check that write is only using stdout */
135 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_arg(0)),
136 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDOUT_FILENO, 1, 0),
137 /* Trap attempts to write to stderr */
138 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, STDERR_FILENO, 1, 2),
139
140 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
141 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRAP),
142 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
143 };
144 struct sock_fprog prog = {
145 .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
146 .filter = filter,
147 };
148
149 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
150 perror("prctl(NO_NEW_PRIVS)");
151 return 1;
152 }
153
154
155 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
156 perror("prctl");
157 return 1;
158 }
159 return 0;
160}
161
162#define payload(_c) (_c), sizeof((_c))
163int main(int argc, char **argv)
164{
165 char buf[4096];
166 ssize_t bytes = 0;
167 if (install_emulator())
168 return 1;
169 if (install_filter())
170 return 1;
171 syscall(__NR_write, STDOUT_FILENO,
172 payload("OHAI! WHAT IS YOUR NAME? "));
173 bytes = syscall(__NR_read, STDIN_FILENO, buf, sizeof(buf));
174 syscall(__NR_write, STDOUT_FILENO, payload("HELLO, "));
175 syscall(__NR_write, STDOUT_FILENO, buf, bytes);
176 syscall(__NR_write, STDERR_FILENO,
177 payload("Error message going to STDERR\n"));
178 return 0;
179}
180#else /* SUPPORTED_ARCH */
181/*
182 * This sample is x86-only. Since kernel samples are compiled with the
183 * host toolchain, a non-x86 host will result in using only the main()
184 * below.
185 */
186int main(void)
187{
188 return 1;
189}
190#endif /* SUPPORTED_ARCH */
diff --git a/samples/seccomp/bpf-fancy.c b/samples/seccomp/bpf-fancy.c
new file mode 100644
index 000000000000..8eb483aaec46
--- /dev/null
+++ b/samples/seccomp/bpf-fancy.c
@@ -0,0 +1,102 @@
1/*
2 * Seccomp BPF example using a macro-based generator.
3 *
4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
5 * Author: Will Drewry <wad@chromium.org>
6 *
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using prctl(PR_ATTACH_SECCOMP_FILTER).
10 */
11
12#include <linux/filter.h>
13#include <linux/seccomp.h>
14#include <linux/unistd.h>
15#include <stdio.h>
16#include <string.h>
17#include <sys/prctl.h>
18#include <unistd.h>
19
20#include "bpf-helper.h"
21
22#ifndef PR_SET_NO_NEW_PRIVS
23#define PR_SET_NO_NEW_PRIVS 38
24#endif
25
26int main(int argc, char **argv)
27{
28 struct bpf_labels l;
29 static const char msg1[] = "Please type something: ";
30 static const char msg2[] = "You typed: ";
31 char buf[256];
32 struct sock_filter filter[] = {
33 /* TODO: LOAD_SYSCALL_NR(arch) and enforce an arch */
34 LOAD_SYSCALL_NR,
35 SYSCALL(__NR_exit, ALLOW),
36 SYSCALL(__NR_exit_group, ALLOW),
37 SYSCALL(__NR_write, JUMP(&l, write_fd)),
38 SYSCALL(__NR_read, JUMP(&l, read)),
39 DENY, /* Don't passthrough into a label */
40
41 LABEL(&l, read),
42 ARG(0),
43 JNE(STDIN_FILENO, DENY),
44 ARG(1),
45 JNE((unsigned long)buf, DENY),
46 ARG(2),
47 JGE(sizeof(buf), DENY),
48 ALLOW,
49
50 LABEL(&l, write_fd),
51 ARG(0),
52 JEQ(STDOUT_FILENO, JUMP(&l, write_buf)),
53 JEQ(STDERR_FILENO, JUMP(&l, write_buf)),
54 DENY,
55
56 LABEL(&l, write_buf),
57 ARG(1),
58 JEQ((unsigned long)msg1, JUMP(&l, msg1_len)),
59 JEQ((unsigned long)msg2, JUMP(&l, msg2_len)),
60 JEQ((unsigned long)buf, JUMP(&l, buf_len)),
61 DENY,
62
63 LABEL(&l, msg1_len),
64 ARG(2),
65 JLT(sizeof(msg1), ALLOW),
66 DENY,
67
68 LABEL(&l, msg2_len),
69 ARG(2),
70 JLT(sizeof(msg2), ALLOW),
71 DENY,
72
73 LABEL(&l, buf_len),
74 ARG(2),
75 JLT(sizeof(buf), ALLOW),
76 DENY,
77 };
78 struct sock_fprog prog = {
79 .filter = filter,
80 .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
81 };
82 ssize_t bytes;
83 bpf_resolve_jumps(&l, filter, sizeof(filter)/sizeof(*filter));
84
85 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
86 perror("prctl(NO_NEW_PRIVS)");
87 return 1;
88 }
89
90 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
91 perror("prctl(SECCOMP)");
92 return 1;
93 }
94 syscall(__NR_write, STDOUT_FILENO, msg1, strlen(msg1));
95 bytes = syscall(__NR_read, STDIN_FILENO, buf, sizeof(buf)-1);
96 bytes = (bytes > 0 ? bytes : 0);
97 syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2));
98 syscall(__NR_write, STDERR_FILENO, buf, bytes);
99 /* Now get killed */
100 syscall(__NR_write, STDERR_FILENO, msg2, strlen(msg2)+2);
101 return 0;
102}
diff --git a/samples/seccomp/bpf-helper.c b/samples/seccomp/bpf-helper.c
new file mode 100644
index 000000000000..579cfe331886
--- /dev/null
+++ b/samples/seccomp/bpf-helper.c
@@ -0,0 +1,89 @@
1/*
2 * Seccomp BPF helper functions
3 *
4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
5 * Author: Will Drewry <wad@chromium.org>
6 *
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using prctl(PR_ATTACH_SECCOMP_FILTER).
10 */
11
12#include <stdio.h>
13#include <string.h>
14
15#include "bpf-helper.h"
16
17int bpf_resolve_jumps(struct bpf_labels *labels,
18 struct sock_filter *filter, size_t count)
19{
20 struct sock_filter *begin = filter;
21 __u8 insn = count - 1;
22
23 if (count < 1)
24 return -1;
25 /*
26 * Walk it once, backwards, to build the label table and do fixups.
27 * Since backward jumps are disallowed by BPF, this is easy.
28 */
29 filter += insn;
30 for (; filter >= begin; --insn, --filter) {
31 if (filter->code != (BPF_JMP+BPF_JA))
32 continue;
33 switch ((filter->jt<<8)|filter->jf) {
34 case (JUMP_JT<<8)|JUMP_JF:
35 if (labels->labels[filter->k].location == 0xffffffff) {
36 fprintf(stderr, "Unresolved label: '%s'\n",
37 labels->labels[filter->k].label);
38 return 1;
39 }
40 filter->k = labels->labels[filter->k].location -
41 (insn + 1);
42 filter->jt = 0;
43 filter->jf = 0;
44 continue;
45 case (LABEL_JT<<8)|LABEL_JF:
46 if (labels->labels[filter->k].location != 0xffffffff) {
47 fprintf(stderr, "Duplicate label use: '%s'\n",
48 labels->labels[filter->k].label);
49 return 1;
50 }
51 labels->labels[filter->k].location = insn;
52 filter->k = 0; /* fall through */
53 filter->jt = 0;
54 filter->jf = 0;
55 continue;
56 }
57 }
58 return 0;
59}
60
61/* Simple lookup table for labels. */
62__u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label)
63{
64 struct __bpf_label *begin = labels->labels, *end;
65 int id;
66 if (labels->count == 0) {
67 begin->label = label;
68 begin->location = 0xffffffff;
69 labels->count++;
70 return 0;
71 }
72 end = begin + labels->count;
73 for (id = 0; begin < end; ++begin, ++id) {
74 if (!strcmp(label, begin->label))
75 return id;
76 }
77 begin->label = label;
78 begin->location = 0xffffffff;
79 labels->count++;
80 return id;
81}
82
83void seccomp_bpf_print(struct sock_filter *filter, size_t count)
84{
85 struct sock_filter *end = filter + count;
86 for ( ; filter < end; ++filter)
87 printf("{ code=%u,jt=%u,jf=%u,k=%u },\n",
88 filter->code, filter->jt, filter->jf, filter->k);
89}
diff --git a/samples/seccomp/bpf-helper.h b/samples/seccomp/bpf-helper.h
new file mode 100644
index 000000000000..643279dd30fb
--- /dev/null
+++ b/samples/seccomp/bpf-helper.h
@@ -0,0 +1,238 @@
1/*
2 * Example wrapper around BPF macros.
3 *
4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
5 * Author: Will Drewry <wad@chromium.org>
6 *
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using prctl(PR_SET_SECCOMP, 2, ...).
10 *
11 * No guarantees are provided with respect to the correctness
12 * or functionality of this code.
13 */
14#ifndef __BPF_HELPER_H__
15#define __BPF_HELPER_H__
16
17#include <asm/bitsperlong.h> /* for __BITS_PER_LONG */
18#include <endian.h>
19#include <linux/filter.h>
20#include <linux/seccomp.h> /* for seccomp_data */
21#include <linux/types.h>
22#include <linux/unistd.h>
23#include <stddef.h>
24
25#define BPF_LABELS_MAX 256
26struct bpf_labels {
27 int count;
28 struct __bpf_label {
29 const char *label;
30 __u32 location;
31 } labels[BPF_LABELS_MAX];
32};
33
34int bpf_resolve_jumps(struct bpf_labels *labels,
35 struct sock_filter *filter, size_t count);
36__u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label);
37void seccomp_bpf_print(struct sock_filter *filter, size_t count);
38
39#define JUMP_JT 0xff
40#define JUMP_JF 0xff
41#define LABEL_JT 0xfe
42#define LABEL_JF 0xfe
43
44#define ALLOW \
45 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
46#define DENY \
47 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
48#define JUMP(labels, label) \
49 BPF_JUMP(BPF_JMP+BPF_JA, FIND_LABEL((labels), (label)), \
50 JUMP_JT, JUMP_JF)
51#define LABEL(labels, label) \
52 BPF_JUMP(BPF_JMP+BPF_JA, FIND_LABEL((labels), (label)), \
53 LABEL_JT, LABEL_JF)
54#define SYSCALL(nr, jt) \
55 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (nr), 0, 1), \
56 jt
57
58/* Lame, but just an example */
59#define FIND_LABEL(labels, label) seccomp_bpf_label((labels), #label)
60
61#define EXPAND(...) __VA_ARGS__
62/* Map all width-sensitive operations */
63#if __BITS_PER_LONG == 32
64
65#define JEQ(x, jt) JEQ32(x, EXPAND(jt))
66#define JNE(x, jt) JNE32(x, EXPAND(jt))
67#define JGT(x, jt) JGT32(x, EXPAND(jt))
68#define JLT(x, jt) JLT32(x, EXPAND(jt))
69#define JGE(x, jt) JGE32(x, EXPAND(jt))
70#define JLE(x, jt) JLE32(x, EXPAND(jt))
71#define JA(x, jt) JA32(x, EXPAND(jt))
72#define ARG(i) ARG_32(i)
73#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
74
75#elif __BITS_PER_LONG == 64
76
77/* Ensure that we load the logically correct offset. */
78#if __BYTE_ORDER == __LITTLE_ENDIAN
79#define ENDIAN(_lo, _hi) _lo, _hi
80#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
81#define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
82#elif __BYTE_ORDER == __BIG_ENDIAN
83#define ENDIAN(_lo, _hi) _hi, _lo
84#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
85#define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
86#else
87#error "Unknown endianness"
88#endif
89
90union arg64 {
91 struct {
92 __u32 ENDIAN(lo32, hi32);
93 };
94 __u64 u64;
95};
96
97#define JEQ(x, jt) \
98 JEQ64(((union arg64){.u64 = (x)}).lo32, \
99 ((union arg64){.u64 = (x)}).hi32, \
100 EXPAND(jt))
101#define JGT(x, jt) \
102 JGT64(((union arg64){.u64 = (x)}).lo32, \
103 ((union arg64){.u64 = (x)}).hi32, \
104 EXPAND(jt))
105#define JGE(x, jt) \
106 JGE64(((union arg64){.u64 = (x)}).lo32, \
107 ((union arg64){.u64 = (x)}).hi32, \
108 EXPAND(jt))
109#define JNE(x, jt) \
110 JNE64(((union arg64){.u64 = (x)}).lo32, \
111 ((union arg64){.u64 = (x)}).hi32, \
112 EXPAND(jt))
113#define JLT(x, jt) \
114 JLT64(((union arg64){.u64 = (x)}).lo32, \
115 ((union arg64){.u64 = (x)}).hi32, \
116 EXPAND(jt))
117#define JLE(x, jt) \
118 JLE64(((union arg64){.u64 = (x)}).lo32, \
119 ((union arg64){.u64 = (x)}).hi32, \
120 EXPAND(jt))
121
122#define JA(x, jt) \
123 JA64(((union arg64){.u64 = (x)}).lo32, \
124 ((union arg64){.u64 = (x)}).hi32, \
125 EXPAND(jt))
126#define ARG(i) ARG_64(i)
127
128#else
129#error __BITS_PER_LONG value unusable.
130#endif
131
132/* Loads the arg into A */
133#define ARG_32(idx) \
134 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, LO_ARG(idx))
135
136/* Loads hi into A and lo in X */
137#define ARG_64(idx) \
138 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, LO_ARG(idx)), \
139 BPF_STMT(BPF_ST, 0), /* lo -> M[0] */ \
140 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, HI_ARG(idx)), \
141 BPF_STMT(BPF_ST, 1) /* hi -> M[1] */
142
143#define JEQ32(value, jt) \
144 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (value), 0, 1), \
145 jt
146
147#define JNE32(value, jt) \
148 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (value), 1, 0), \
149 jt
150
151/* Checks the lo, then swaps to check the hi. A=lo,X=hi */
152#define JEQ64(lo, hi, jt) \
153 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
154 BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
155 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (lo), 0, 2), \
156 BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
157 jt, \
158 BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
159
160#define JNE64(lo, hi, jt) \
161 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 5, 0), \
162 BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
163 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (lo), 2, 0), \
164 BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
165 jt, \
166 BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
167
168#define JA32(value, jt) \
169 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, (value), 0, 1), \
170 jt
171
172#define JA64(lo, hi, jt) \
173 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, (hi), 3, 0), \
174 BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
175 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, (lo), 0, 2), \
176 BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
177 jt, \
178 BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
179
180#define JGE32(value, jt) \
181 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (value), 0, 1), \
182 jt
183
184#define JLT32(value, jt) \
185 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (value), 1, 0), \
186 jt
187
188/* Shortcut checking if hi > arg.hi. */
189#define JGE64(lo, hi, jt) \
190 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (hi), 4, 0), \
191 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
192 BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
193 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (lo), 0, 2), \
194 BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
195 jt, \
196 BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
197
198#define JLT64(lo, hi, jt) \
199 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, (hi), 0, 4), \
200 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
201 BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
202 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (lo), 2, 0), \
203 BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
204 jt, \
205 BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
206
207#define JGT32(value, jt) \
208 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (value), 0, 1), \
209 jt
210
211#define JLE32(value, jt) \
212 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (value), 1, 0), \
213 jt
214
215/* Check hi > args.hi first, then do the GE checking */
216#define JGT64(lo, hi, jt) \
217 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (hi), 4, 0), \
218 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 5), \
219 BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
220 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (lo), 0, 2), \
221 BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
222 jt, \
223 BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
224
225#define JLE64(lo, hi, jt) \
226 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (hi), 6, 0), \
227 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (hi), 0, 3), \
228 BPF_STMT(BPF_LD+BPF_MEM, 0), /* swap in lo */ \
229 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, (lo), 2, 0), \
230 BPF_STMT(BPF_LD+BPF_MEM, 1), /* passed: swap hi back in */ \
231 jt, \
232 BPF_STMT(BPF_LD+BPF_MEM, 1) /* failed: swap hi back in */
233
234#define LOAD_SYSCALL_NR \
235 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
236 offsetof(struct seccomp_data, nr))
237
238#endif /* __BPF_HELPER_H__ */
diff --git a/samples/seccomp/dropper.c b/samples/seccomp/dropper.c
new file mode 100644
index 000000000000..c69c347c7011
--- /dev/null
+++ b/samples/seccomp/dropper.c
@@ -0,0 +1,68 @@
1/*
2 * Naive system call dropper built on seccomp_filter.
3 *
4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
5 * Author: Will Drewry <wad@chromium.org>
6 *
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using prctl(PR_SET_SECCOMP, 2, ...).
10 *
11 * When run, returns the specified errno for the specified
12 * system call number against the given architecture.
13 *
14 * Run this one as root as PR_SET_NO_NEW_PRIVS is not called.
15 */
16
17#include <errno.h>
18#include <linux/audit.h>
19#include <linux/filter.h>
20#include <linux/seccomp.h>
21#include <linux/unistd.h>
22#include <stdio.h>
23#include <stddef.h>
24#include <stdlib.h>
25#include <sys/prctl.h>
26#include <unistd.h>
27
28static int install_filter(int nr, int arch, int error)
29{
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),
40 };
41 struct sock_fprog prog = {
42 .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
43 .filter = filter,
44 };
45 if (prctl(PR_SET_SECCOMP, 2, &prog)) {
46 perror("prctl");
47 return 1;
48 }
49 return 0;
50}
51
52int main(int argc, char **argv)
53{
54 if (argc < 5) {
55 fprintf(stderr, "Usage:\n"
56 "dropper <syscall_nr> <arch> <errno> <prog> [<args>]\n"
57 "Hint: AUDIT_ARCH_I386: 0x%X\n"
58 " AUDIT_ARCH_X86_64: 0x%X\n"
59 "\n", AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
60 return 1;
61 }
62 if (install_filter(strtol(argv[1], NULL, 0), strtol(argv[2], NULL, 0),
63 strtol(argv[3], NULL, 0)))
64 return 1;
65 execv(argv[4], &argv[4]);
66 printf("Failed to execv\n");
67 return 255;
68}