diff --git a/tools/regression/bpf/bpf_filter/bpf_test.c b/tools/regression/bpf/bpf_filter/bpf_test.c index a9f077ecf7a1..cd16fd68e9e0 100644 --- a/tools/regression/bpf/bpf_filter/bpf_test.c +++ b/tools/regression/bpf/bpf_filter/bpf_test.c @@ -89,7 +89,7 @@ u_int bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int); #endif #ifdef BPF_VALIDATE -static u_short bpf_code_map[] = { +static const u_short bpf_code_map[] = { 0x10ff, /* 0x00-0x0f: 1111111100001000 */ 0x3070, /* 0x10-0x1f: 0000111000001100 */ 0x3131, /* 0x20-0x2f: 1000110010001100 */ @@ -108,6 +108,9 @@ static u_short bpf_code_map[] = { 0x0000 /* 0xf0-0xff: 0000000000000000 */ }; +#define BPF_VALIDATE_CODE(c) \ + ((c) <= 0xff && (bpf_code_map[(c) >> 4] & (1 << ((c) & 0xf))) != 0) + /* * XXX Copied from sys/net/bpf_filter.c and modified. * @@ -137,8 +140,7 @@ bpf_validate(const struct bpf_insn *f, int len) /* * Check that the code is valid. */ - if ((p->code & 0xff00) || - !(bpf_code_map[p->code >> 4] & (1 << (p->code & 0xf)))) + if (!BPF_VALIDATE_CODE(p->code)) return (0); /* * Check that that jumps are forward, and within @@ -147,23 +149,24 @@ bpf_validate(const struct bpf_insn *f, int len) if (BPF_CLASS(p->code) == BPF_JMP) { register u_int offset; - if (BPF_OP(p->code) == BPF_JA) + if (p->code == (BPF_JMP|BPF_JA)) offset = p->k; else offset = p->jt > p->jf ? p->jt : p->jf; if (offset >= (u_int)(len - i) - 1) return (0); + continue; } /* * Check that memory operations use valid addresses. */ - if ((BPF_CLASS(p->code) == BPF_ST || - BPF_CLASS(p->code) == BPF_STX || - ((BPF_CLASS(p->code) == BPF_LD || - BPF_CLASS(p->code) == BPF_LDX) && - (p->code & 0xe0) == BPF_MEM)) && - p->k >= BPF_MEMWORDS) - return (0); + if (p->code == BPF_ST || p->code == BPF_STX || + p->code == (BPF_LD|BPF_MEM) || + p->code == (BPF_LDX|BPF_MEM)) { + if (p->k >= BPF_MEMWORDS) + return (0); + continue; + } /* * Check for constant division by 0. */