Skip to content

Commit d881d25

Browse files
rickyzkees
authored andcommitted
samples/seccomp: Support programs with >256 instructions
Previously, the program size was incorrectly truncated to 8 bits, resulting in broken labels in large programs. Also changes the jump resolution loop to not rely on undefined behavior (making a pointer point before the filter array). Signed-off-by: Ricky Zhou <rickyz@chromium.org> Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 1ff1205 commit d881d25

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

‎samples/seccomp/bpf-helper.c‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,41 @@
1818
int bpf_resolve_jumps(struct bpf_labels *labels,
1919
struct sock_filter *filter, size_t count)
2020
{
21-
struct sock_filter *begin = filter;
22-
__u8 insn = count - 1;
21+
size_t i;
2322

24-
if (count < 1)
23+
if (count < 1 || count > BPF_MAXINSNS)
2524
return -1;
2625
/*
2726
* Walk it once, backwards, to build the label table and do fixups.
2827
* Since backward jumps are disallowed by BPF, this is easy.
2928
*/
30-
filter += insn;
31-
for (; filter >= begin; --insn, --filter) {
32-
if (filter->code != (BPF_JMP+BPF_JA))
29+
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))
3333
continue;
34-
switch ((filter->jt<<8)|filter->jf) {
34+
switch ((instr->jt<<8)|instr->jf) {
3535
case (JUMP_JT<<8)|JUMP_JF:
36-
if (labels->labels[filter->k].location == 0xffffffff) {
36+
if (labels->labels[instr->k].location == 0xffffffff) {
3737
fprintf(stderr, "Unresolved label: '%s'\n",
38-
labels->labels[filter->k].label);
38+
labels->labels[instr->k].label);
3939
return 1;
4040
}
41-
filter->k = labels->labels[filter->k].location -
42-
(insn + 1);
43-
filter->jt = 0;
44-
filter->jf = 0;
41+
instr->k = labels->labels[instr->k].location -
42+
(offset + 1);
43+
instr->jt = 0;
44+
instr->jf = 0;
4545
continue;
4646
case (LABEL_JT<<8)|LABEL_JF:
47-
if (labels->labels[filter->k].location != 0xffffffff) {
47+
if (labels->labels[instr->k].location != 0xffffffff) {
4848
fprintf(stderr, "Duplicate label use: '%s'\n",
49-
labels->labels[filter->k].label);
49+
labels->labels[instr->k].label);
5050
return 1;
5151
}
52-
labels->labels[filter->k].location = insn;
53-
filter->k = 0; /* fall through */
54-
filter->jt = 0;
55-
filter->jf = 0;
52+
labels->labels[instr->k].location = offset;
53+
instr->k = 0; /* fall through */
54+
instr->jt = 0;
55+
instr->jf = 0;
5656
continue;
5757
}
5858
}

0 commit comments

Comments
 (0)