|
18 | 18 | int bpf_resolve_jumps(struct bpf_labels *labels, |
19 | 19 | struct sock_filter *filter, size_t count) |
20 | 20 | { |
21 | | - struct sock_filter *begin = filter; |
22 | | - __u8 insn = count - 1; |
| 21 | + size_t i; |
23 | 22 |
|
24 | | - if (count < 1) |
| 23 | + if (count < 1 || count > BPF_MAXINSNS) |
25 | 24 | return -1; |
26 | 25 | /* |
27 | 26 | * Walk it once, backwards, to build the label table and do fixups. |
28 | 27 | * Since backward jumps are disallowed by BPF, this is easy. |
29 | 28 | */ |
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)) |
33 | 33 | continue; |
34 | | - switch ((filter->jt<<8)|filter->jf) { |
| 34 | + switch ((instr->jt<<8)|instr->jf) { |
35 | 35 | case (JUMP_JT<<8)|JUMP_JF: |
36 | | - if (labels->labels[filter->k].location == 0xffffffff) { |
| 36 | + if (labels->labels[instr->k].location == 0xffffffff) { |
37 | 37 | fprintf(stderr, "Unresolved label: '%s'\n", |
38 | | - labels->labels[filter->k].label); |
| 38 | + labels->labels[instr->k].label); |
39 | 39 | return 1; |
40 | 40 | } |
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; |
45 | 45 | continue; |
46 | 46 | case (LABEL_JT<<8)|LABEL_JF: |
47 | | - if (labels->labels[filter->k].location != 0xffffffff) { |
| 47 | + if (labels->labels[instr->k].location != 0xffffffff) { |
48 | 48 | fprintf(stderr, "Duplicate label use: '%s'\n", |
49 | | - labels->labels[filter->k].label); |
| 49 | + labels->labels[instr->k].label); |
50 | 50 | return 1; |
51 | 51 | } |
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; |
56 | 56 | continue; |
57 | 57 | } |
58 | 58 | } |
|
0 commit comments