Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Seccomp BPF helper functions
4 *
5 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
6 * Author: Will Drewry <wad@chromium.org>
7 *
8 * The code may be used by anyone for any purpose,
9 * and can serve as a starting point for developing
10 * applications using prctl(PR_ATTACH_SECCOMP_FILTER).
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include "bpf-helper.h"
18
19int bpf_resolve_jumps(struct bpf_labels *labels,
20 struct sock_filter *filter, size_t count)
21{
22 size_t i;
23
24 if (count < 1 || count > BPF_MAXINSNS)
25 return -1;
26 /*
27 * Walk it once, backwards, to build the label table and do fixups.
28 * Since backward jumps are disallowed by BPF, this is easy.
29 */
30 for (i = 0; i < count; ++i) {
31 size_t offset = count - i - 1;
32 struct sock_filter *instr = &filter[offset];
33 if (instr->code != (BPF_JMP+BPF_JA))
34 continue;
35 switch ((instr->jt<<8)|instr->jf) {
36 case (JUMP_JT<<8)|JUMP_JF:
37 if (labels->labels[instr->k].location == 0xffffffff) {
38 fprintf(stderr, "Unresolved label: '%s'\n",
39 labels->labels[instr->k].label);
40 return 1;
41 }
42 instr->k = labels->labels[instr->k].location -
43 (offset + 1);
44 instr->jt = 0;
45 instr->jf = 0;
46 continue;
47 case (LABEL_JT<<8)|LABEL_JF:
48 if (labels->labels[instr->k].location != 0xffffffff) {
49 fprintf(stderr, "Duplicate label use: '%s'\n",
50 labels->labels[instr->k].label);
51 return 1;
52 }
53 labels->labels[instr->k].location = offset;
54 instr->k = 0; /* fall through */
55 instr->jt = 0;
56 instr->jf = 0;
57 continue;
58 }
59 }
60 return 0;
61}
62
63/* Simple lookup table for labels. */
64__u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label)
65{
66 struct __bpf_label *begin = labels->labels, *end;
67 int id;
68
69 if (labels->count == BPF_LABELS_MAX) {
70 fprintf(stderr, "Too many labels\n");
71 exit(1);
72 }
73 if (labels->count == 0) {
74 begin->label = label;
75 begin->location = 0xffffffff;
76 labels->count++;
77 return 0;
78 }
79 end = begin + labels->count;
80 for (id = 0; begin < end; ++begin, ++id) {
81 if (!strcmp(label, begin->label))
82 return id;
83 }
84 begin->label = label;
85 begin->location = 0xffffffff;
86 labels->count++;
87 return id;
88}
89
90void seccomp_bpf_print(struct sock_filter *filter, size_t count)
91{
92 struct sock_filter *end = filter + count;
93 for ( ; filter < end; ++filter)
94 printf("{ code=%u,jt=%u,jf=%u,k=%u },\n",
95 filter->code, filter->jt, filter->jf, filter->k);
96}
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}