aboutsummaryrefslogtreecommitdiffstats
path: root/samples/seccomp/bpf-fancy.c
diff options
context:
space:
mode:
authorWill Drewry <wad@chromium.org>2012-04-12 17:48:04 -0400
committerJames Morris <james.l.morris@oracle.com>2012-04-13 21:13:22 -0400
commit8ac270d1e29f0428228ab2b9a8ae5e1ed4a5cd84 (patch)
tree6deba4ed83da9ace758004b29d15aa0d2ec875a7 /samples/seccomp/bpf-fancy.c
parentc6cfbeb4029610c8c330c312dcf4d514cc067554 (diff)
Documentation: prctl/seccomp_filter
Documents how system call filtering using Berkeley Packet Filter programs works and how it may be used. Includes an example for x86 and a semi-generic example using a macro-based code generator. Acked-by: Eric Paris <eparis@redhat.com> Signed-off-by: Will Drewry <wad@chromium.org> Acked-by: Kees Cook <keescook@chromium.org> v18: - added acked by - update no new privs numbers v17: - remove @compat note and add Pitfalls section for arch checking (keescook@chromium.org) v16: - v15: - v14: - rebase/nochanges v13: - rebase on to 88ebdda6159ffc15699f204c33feb3e431bf9bdc v12: - comment on the ptrace_event use - update arch support comment - note the behavior of SECCOMP_RET_DATA when there are multiple filters (keescook@chromium.org) - lots of samples/ clean up incl 64-bit bpf-direct support (markus@chromium.org) - rebase to linux-next v11: - overhaul return value language, updates (keescook@chromium.org) - comment on do_exit(SIGSYS) v10: - update for SIGSYS - update for new seccomp_data layout - update for ptrace option use v9: - updated bpf-direct.c for SIGILL v8: - add PR_SET_NO_NEW_PRIVS to the samples. v7: - updated for all the new stuff in v7: TRAP, TRACE - only talk about PR_SET_SECCOMP now - fixed bad JLE32 check (coreyb@linux.vnet.ibm.com) - adds dropper.c: a simple system call disabler v6: - tweak the language to note the requirement of PR_SET_NO_NEW_PRIVS being called prior to use. (luto@mit.edu) v5: - update sample to use system call arguments - adds a "fancy" example using a macro-based generator - cleaned up bpf in the sample - update docs to mention arguments - fix prctl value (eparis@redhat.com) - language cleanup (rdunlap@xenotime.net) v4: - update for no_new_privs use - minor tweaks v3: - call out BPF <-> Berkeley Packet Filter (rdunlap@xenotime.net) - document use of tentative always-unprivileged - guard sample compilation for i386 and x86_64 v2: - move code to samples (corbet@lwn.net) Signed-off-by: James Morris <james.l.morris@oracle.com>
Diffstat (limited to 'samples/seccomp/bpf-fancy.c')
-rw-r--r--samples/seccomp/bpf-fancy.c102
1 files changed, 102 insertions, 0 deletions
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}