aboutsummaryrefslogtreecommitdiffstats
path: root/samples/seccomp/bpf-helper.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-helper.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-helper.c')
-rw-r--r--samples/seccomp/bpf-helper.c89
1 files changed, 89 insertions, 0 deletions
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}