blob: 1ef0f4d72898d9945a95fd78b501583fb18d1093 [file] [log] [blame]
Will Drewry8ac270d2012-04-12 16:48:04 -05001/*
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>
Kees Cook3a9af0b2015-02-17 13:47:58 -080013#include <stdlib.h>
Will Drewry8ac270d2012-04-12 16:48:04 -050014#include <string.h>
15
16#include "bpf-helper.h"
17
18int bpf_resolve_jumps(struct bpf_labels *labels,
19 struct sock_filter *filter, size_t count)
20{
Ricky Zhoud881d252016-10-13 10:37:28 -070021 size_t i;
Will Drewry8ac270d2012-04-12 16:48:04 -050022
Ricky Zhoud881d252016-10-13 10:37:28 -070023 if (count < 1 || count > BPF_MAXINSNS)
Will Drewry8ac270d2012-04-12 16:48:04 -050024 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 */
Ricky Zhoud881d252016-10-13 10:37:28 -070029 for (i = 0; i < count; ++i) {
30 size_t offset = count - i - 1;
31 struct sock_filter *instr = &filter[offset];
32 if (instr->code != (BPF_JMP+BPF_JA))
Will Drewry8ac270d2012-04-12 16:48:04 -050033 continue;
Ricky Zhoud881d252016-10-13 10:37:28 -070034 switch ((instr->jt<<8)|instr->jf) {
Will Drewry8ac270d2012-04-12 16:48:04 -050035 case (JUMP_JT<<8)|JUMP_JF:
Ricky Zhoud881d252016-10-13 10:37:28 -070036 if (labels->labels[instr->k].location == 0xffffffff) {
Will Drewry8ac270d2012-04-12 16:48:04 -050037 fprintf(stderr, "Unresolved label: '%s'\n",
Ricky Zhoud881d252016-10-13 10:37:28 -070038 labels->labels[instr->k].label);
Will Drewry8ac270d2012-04-12 16:48:04 -050039 return 1;
40 }
Ricky Zhoud881d252016-10-13 10:37:28 -070041 instr->k = labels->labels[instr->k].location -
42 (offset + 1);
43 instr->jt = 0;
44 instr->jf = 0;
Will Drewry8ac270d2012-04-12 16:48:04 -050045 continue;
46 case (LABEL_JT<<8)|LABEL_JF:
Ricky Zhoud881d252016-10-13 10:37:28 -070047 if (labels->labels[instr->k].location != 0xffffffff) {
Will Drewry8ac270d2012-04-12 16:48:04 -050048 fprintf(stderr, "Duplicate label use: '%s'\n",
Ricky Zhoud881d252016-10-13 10:37:28 -070049 labels->labels[instr->k].label);
Will Drewry8ac270d2012-04-12 16:48:04 -050050 return 1;
51 }
Ricky Zhoud881d252016-10-13 10:37:28 -070052 labels->labels[instr->k].location = offset;
53 instr->k = 0; /* fall through */
54 instr->jt = 0;
55 instr->jf = 0;
Will Drewry8ac270d2012-04-12 16:48:04 -050056 continue;
57 }
58 }
59 return 0;
60}
61
62/* Simple lookup table for labels. */
63__u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label)
64{
65 struct __bpf_label *begin = labels->labels, *end;
66 int id;
Kees Cook3a9af0b2015-02-17 13:47:58 -080067
68 if (labels->count == BPF_LABELS_MAX) {
69 fprintf(stderr, "Too many labels\n");
70 exit(1);
71 }
Will Drewry8ac270d2012-04-12 16:48:04 -050072 if (labels->count == 0) {
73 begin->label = label;
74 begin->location = 0xffffffff;
75 labels->count++;
76 return 0;
77 }
78 end = begin + labels->count;
79 for (id = 0; begin < end; ++begin, ++id) {
80 if (!strcmp(label, begin->label))
81 return id;
82 }
83 begin->label = label;
84 begin->location = 0xffffffff;
85 labels->count++;
86 return id;
87}
88
89void seccomp_bpf_print(struct sock_filter *filter, size_t count)
90{
91 struct sock_filter *end = filter + count;
92 for ( ; filter < end; ++filter)
93 printf("{ code=%u,jt=%u,jf=%u,k=%u },\n",
94 filter->code, filter->jt, filter->jf, filter->k);
95}