2 * Seccomp BPF helper functions
4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
5 * Author: Will Drewry <wad@chromium.org>
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).
15 #include "bpf-helper.h"
17 int bpf_resolve_jumps(struct bpf_labels *labels,
18 struct sock_filter *filter, size_t count)
20 struct sock_filter *begin = filter;
21 __u8 insn = count - 1;
26 * Walk it once, backwards, to build the label table and do fixups.
27 * Since backward jumps are disallowed by BPF, this is easy.
30 for (; filter >= begin; --insn, --filter) {
31 if (filter->code != (BPF_JMP+BPF_JA))
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);
40 filter->k = labels->labels[filter->k].location -
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);
51 labels->labels[filter->k].location = insn;
52 filter->k = 0; /* fall through */
61 /* Simple lookup table for labels. */
62 __u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label)
64 struct __bpf_label *begin = labels->labels, *end;
66 if (labels->count == 0) {
68 begin->location = 0xffffffff;
72 end = begin + labels->count;
73 for (id = 0; begin < end; ++begin, ++id) {
74 if (!strcmp(label, begin->label))
78 begin->location = 0xffffffff;
83 void seccomp_bpf_print(struct sock_filter *filter, size_t count)
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);