bpf_filter.c revision 1.44 1 /* $NetBSD: bpf_filter.c,v 1.44 2011/02/19 04:10:47 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from the Stanford/CMU enet packet filter,
8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Berkeley Laboratory.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: bpf_filter.c,v 1.44 2011/02/19 04:10:47 christos Exp $");
41
42 #if 0
43 #if !(defined(lint) || defined(KERNEL))
44 static const char rcsid[] =
45 "@(#) Header: bpf_filter.c,v 1.33 97/04/26 13:37:18 leres Exp (LBL)";
46 #endif
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/kmem.h>
52 #include <sys/endian.h>
53
54 #define EXTRACT_SHORT(p) be16dec(p)
55 #define EXTRACT_LONG(p) be32dec(p)
56
57 #ifdef _KERNEL
58 #include <sys/mbuf.h>
59 #define MINDEX(len, m, k) \
60 { \
61 len = m->m_len; \
62 while (k >= len) { \
63 k -= len; \
64 m = m->m_next; \
65 if (m == 0) \
66 return 0; \
67 len = m->m_len; \
68 } \
69 }
70
71 static int m_xword (const struct mbuf *, uint32_t, int *);
72 static int m_xhalf (const struct mbuf *, uint32_t, int *);
73
74 static int
75 m_xword(const struct mbuf *m, uint32_t k, int *err)
76 {
77 int len;
78 u_char *cp, *np;
79 struct mbuf *m0;
80
81 *err = 1;
82 MINDEX(len, m, k);
83 cp = mtod(m, u_char *) + k;
84 if (len >= k + 4) {
85 *err = 0;
86 return EXTRACT_LONG(cp);
87 }
88 m0 = m->m_next;
89 if (m0 == 0 || m0->m_len + len - k < 4)
90 return 0;
91 *err = 0;
92 np = mtod(m0, u_char *);
93 switch (len - k) {
94
95 case 1:
96 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
97
98 case 2:
99 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
100
101 default:
102 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
103 }
104 }
105
106 static int
107 m_xhalf(const struct mbuf *m, uint32_t k, int *err)
108 {
109 int len;
110 u_char *cp;
111 struct mbuf *m0;
112
113 *err = 1;
114 MINDEX(len, m, k);
115 cp = mtod(m, u_char *) + k;
116 if (len >= k + 2) {
117 *err = 0;
118 return EXTRACT_SHORT(cp);
119 }
120 m0 = m->m_next;
121 if (m0 == 0)
122 return 0;
123 *err = 0;
124 return (cp[0] << 8) | mtod(m0, u_char *)[0];
125 }
126 #else /* _KERNEL */
127 #include <stdlib.h>
128 #endif /* !_KERNEL */
129
130 #include <net/bpf.h>
131
132 /*
133 * Execute the filter program starting at pc on the packet p
134 * wirelen is the length of the original packet
135 * buflen is the amount of data present
136 */
137 u_int
138 bpf_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen,
139 u_int buflen)
140 {
141 uint32_t A, X, k;
142 uint32_t mem[BPF_MEMWORDS];
143
144 if (pc == 0)
145 /*
146 * No filter means accept all.
147 */
148 return (u_int)-1;
149 A = 0;
150 X = 0;
151 --pc;
152 /* CONSTCOND */
153 while (1) {
154 ++pc;
155 switch (pc->code) {
156
157 default:
158 #ifdef _KERNEL
159 return 0;
160 #else
161 abort();
162 #endif
163 case BPF_RET|BPF_K:
164 return (u_int)pc->k;
165
166 case BPF_RET|BPF_A:
167 return (u_int)A;
168
169 case BPF_LD|BPF_W|BPF_ABS:
170 k = pc->k;
171 if (k + sizeof(int32_t) > buflen) {
172 #ifdef _KERNEL
173 int merr = 0; /* XXX: GCC */
174
175 if (buflen != 0)
176 return 0;
177 A = m_xword((const struct mbuf *)p, k, &merr);
178 if (merr != 0)
179 return 0;
180 continue;
181 #else
182 return 0;
183 #endif
184 }
185 A = EXTRACT_LONG(&p[k]);
186 continue;
187
188 case BPF_LD|BPF_H|BPF_ABS:
189 k = pc->k;
190 if (k + sizeof(int16_t) > buflen) {
191 #ifdef _KERNEL
192 int merr;
193
194 if (buflen != 0)
195 return 0;
196 A = m_xhalf((const struct mbuf *)p, k, &merr);
197 if (merr != 0)
198 return 0;
199 continue;
200 #else
201 return 0;
202 #endif
203 }
204 A = EXTRACT_SHORT(&p[k]);
205 continue;
206
207 case BPF_LD|BPF_B|BPF_ABS:
208 k = pc->k;
209 if (k >= buflen) {
210 #ifdef _KERNEL
211 const struct mbuf *m;
212 int len;
213
214 if (buflen != 0)
215 return 0;
216 m = (const struct mbuf *)p;
217 MINDEX(len, m, k);
218 A = mtod(m, u_char *)[k];
219 continue;
220 #else
221 return 0;
222 #endif
223 }
224 A = p[k];
225 continue;
226
227 case BPF_LD|BPF_W|BPF_LEN:
228 A = wirelen;
229 continue;
230
231 case BPF_LDX|BPF_W|BPF_LEN:
232 X = wirelen;
233 continue;
234
235 case BPF_LD|BPF_W|BPF_IND:
236 k = X + pc->k;
237 if (k + sizeof(int32_t) > buflen) {
238 #ifdef _KERNEL
239 int merr = 0; /* XXX: GCC */
240
241 if (buflen != 0)
242 return 0;
243 A = m_xword((const struct mbuf *)p, k, &merr);
244 if (merr != 0)
245 return 0;
246 continue;
247 #else
248 return 0;
249 #endif
250 }
251 A = EXTRACT_LONG(&p[k]);
252 continue;
253
254 case BPF_LD|BPF_H|BPF_IND:
255 k = X + pc->k;
256 if (k + sizeof(int16_t) > buflen) {
257 #ifdef _KERNEL
258 int merr = 0; /* XXX: GCC */
259
260 if (buflen != 0)
261 return 0;
262 A = m_xhalf((const struct mbuf *)p, k, &merr);
263 if (merr != 0)
264 return 0;
265 continue;
266 #else
267 return 0;
268 #endif
269 }
270 A = EXTRACT_SHORT(&p[k]);
271 continue;
272
273 case BPF_LD|BPF_B|BPF_IND:
274 k = X + pc->k;
275 if (k >= buflen) {
276 #ifdef _KERNEL
277 const struct mbuf *m;
278 int len;
279
280 if (buflen != 0)
281 return 0;
282 m = (const struct mbuf *)p;
283 MINDEX(len, m, k);
284 A = mtod(m, u_char *)[k];
285 continue;
286 #else
287 return 0;
288 #endif
289 }
290 A = p[k];
291 continue;
292
293 case BPF_LDX|BPF_MSH|BPF_B:
294 k = pc->k;
295 if (k >= buflen) {
296 #ifdef _KERNEL
297 const struct mbuf *m;
298 int len;
299
300 if (buflen != 0)
301 return 0;
302 m = (const struct mbuf *)p;
303 MINDEX(len, m, k);
304 X = (mtod(m, char *)[k] & 0xf) << 2;
305 continue;
306 #else
307 return 0;
308 #endif
309 }
310 X = (p[pc->k] & 0xf) << 2;
311 continue;
312
313 case BPF_LD|BPF_IMM:
314 A = pc->k;
315 continue;
316
317 case BPF_LDX|BPF_IMM:
318 X = pc->k;
319 continue;
320
321 case BPF_LD|BPF_MEM:
322 A = mem[pc->k];
323 continue;
324
325 case BPF_LDX|BPF_MEM:
326 X = mem[pc->k];
327 continue;
328
329 case BPF_ST:
330 mem[pc->k] = A;
331 continue;
332
333 case BPF_STX:
334 mem[pc->k] = X;
335 continue;
336
337 case BPF_JMP|BPF_JA:
338 pc += pc->k;
339 continue;
340
341 case BPF_JMP|BPF_JGT|BPF_K:
342 pc += (A > pc->k) ? pc->jt : pc->jf;
343 continue;
344
345 case BPF_JMP|BPF_JGE|BPF_K:
346 pc += (A >= pc->k) ? pc->jt : pc->jf;
347 continue;
348
349 case BPF_JMP|BPF_JEQ|BPF_K:
350 pc += (A == pc->k) ? pc->jt : pc->jf;
351 continue;
352
353 case BPF_JMP|BPF_JSET|BPF_K:
354 pc += (A & pc->k) ? pc->jt : pc->jf;
355 continue;
356
357 case BPF_JMP|BPF_JGT|BPF_X:
358 pc += (A > X) ? pc->jt : pc->jf;
359 continue;
360
361 case BPF_JMP|BPF_JGE|BPF_X:
362 pc += (A >= X) ? pc->jt : pc->jf;
363 continue;
364
365 case BPF_JMP|BPF_JEQ|BPF_X:
366 pc += (A == X) ? pc->jt : pc->jf;
367 continue;
368
369 case BPF_JMP|BPF_JSET|BPF_X:
370 pc += (A & X) ? pc->jt : pc->jf;
371 continue;
372
373 case BPF_ALU|BPF_ADD|BPF_X:
374 A += X;
375 continue;
376
377 case BPF_ALU|BPF_SUB|BPF_X:
378 A -= X;
379 continue;
380
381 case BPF_ALU|BPF_MUL|BPF_X:
382 A *= X;
383 continue;
384
385 case BPF_ALU|BPF_DIV|BPF_X:
386 if (X == 0)
387 return 0;
388 A /= X;
389 continue;
390
391 case BPF_ALU|BPF_AND|BPF_X:
392 A &= X;
393 continue;
394
395 case BPF_ALU|BPF_OR|BPF_X:
396 A |= X;
397 continue;
398
399 case BPF_ALU|BPF_LSH|BPF_X:
400 A <<= X;
401 continue;
402
403 case BPF_ALU|BPF_RSH|BPF_X:
404 A >>= X;
405 continue;
406
407 case BPF_ALU|BPF_ADD|BPF_K:
408 A += pc->k;
409 continue;
410
411 case BPF_ALU|BPF_SUB|BPF_K:
412 A -= pc->k;
413 continue;
414
415 case BPF_ALU|BPF_MUL|BPF_K:
416 A *= pc->k;
417 continue;
418
419 case BPF_ALU|BPF_DIV|BPF_K:
420 A /= pc->k;
421 continue;
422
423 case BPF_ALU|BPF_AND|BPF_K:
424 A &= pc->k;
425 continue;
426
427 case BPF_ALU|BPF_OR|BPF_K:
428 A |= pc->k;
429 continue;
430
431 case BPF_ALU|BPF_LSH|BPF_K:
432 A <<= pc->k;
433 continue;
434
435 case BPF_ALU|BPF_RSH|BPF_K:
436 A >>= pc->k;
437 continue;
438
439 case BPF_ALU|BPF_NEG:
440 A = -A;
441 continue;
442
443 case BPF_MISC|BPF_TAX:
444 X = A;
445 continue;
446
447 case BPF_MISC|BPF_TXA:
448 A = X;
449 continue;
450 }
451 }
452 }
453
454 /*
455 * Return true if the 'fcode' is a valid filter program.
456 * The constraints are that each jump be forward and to a valid
457 * code, that memory accesses are within valid ranges (to the
458 * extent that this can be checked statically; loads of packet
459 * data have to be, and are, also checked at run time), and that
460 * the code terminates with either an accept or reject.
461 *
462 * The kernel needs to be able to verify an application's filter code.
463 * Otherwise, a bogus program could easily crash the system.
464 */
465 __CTASSERT(BPF_MEMWORDS == sizeof(uint16_t) * NBBY);
466
467 int
468 bpf_validate(const struct bpf_insn *f, int signed_len)
469 {
470 u_int i, from, len, ok = 0, size;
471 const struct bpf_insn *p;
472 #if defined(KERNEL) || defined(_KERNEL)
473 uint16_t *mem, invalid;
474 #endif
475
476 len = (u_int)signed_len;
477 if (len < 1)
478 return 0;
479 #if defined(KERNEL) || defined(_KERNEL)
480 if (len > BPF_MAXINSNS)
481 return 0;
482 #endif
483 if (BPF_CLASS(f[len - 1].code) != BPF_RET)
484 return 0;
485
486 #if defined(KERNEL) || defined(_KERNEL)
487 mem = kmem_zalloc(size = sizeof(*mem) * len, KM_SLEEP);
488 invalid = ~0; /* All is invalid on startup */
489 #endif
490
491 for (i = 0; i < len; ++i) {
492 #if defined(KERNEL) || defined(_KERNEL)
493 /* blend in any invalid bits for current pc */
494 invalid |= mem[i];
495 #endif
496 p = &f[i];
497 switch (BPF_CLASS(p->code)) {
498 /*
499 * Check that memory operations use valid addresses.
500 */
501 case BPF_LD:
502 case BPF_LDX:
503 switch (BPF_MODE(p->code)) {
504 case BPF_MEM:
505 /*
506 * There's no maximum packet data size
507 * in userland. The runtime packet length
508 * check suffices.
509 */
510 #if defined(KERNEL) || defined(_KERNEL)
511 /*
512 * More strict check with actual packet length
513 * is done runtime.
514 */
515 if (p->k >= BPF_MEMWORDS)
516 goto out;
517 /* check for current memory invalid */
518 if (invalid & (1 << p->k))
519 goto out;
520 #endif
521 break;
522 case BPF_ABS:
523 case BPF_IND:
524 case BPF_MSH:
525 case BPF_IMM:
526 case BPF_LEN:
527 break;
528 default:
529 goto out;
530 }
531 break;
532 case BPF_ST:
533 case BPF_STX:
534 if (p->k >= BPF_MEMWORDS)
535 goto out;
536 #if defined(KERNEL) || defined(_KERNEL)
537 /* validate the memory word */
538 invalid &= ~(1 << p->k);
539 #endif
540 break;
541 case BPF_ALU:
542 switch (BPF_OP(p->code)) {
543 case BPF_ADD:
544 case BPF_SUB:
545 case BPF_MUL:
546 case BPF_OR:
547 case BPF_AND:
548 case BPF_LSH:
549 case BPF_RSH:
550 case BPF_NEG:
551 break;
552 case BPF_DIV:
553 /*
554 * Check for constant division by 0.
555 */
556 if (BPF_SRC(p->code) == BPF_K && p->k == 0)
557 goto out;
558 break;
559 default:
560 goto out;
561 }
562 break;
563 case BPF_JMP:
564 /*
565 * Check that jumps are within the code block,
566 * and that unconditional branches don't go
567 * backwards as a result of an overflow.
568 * Unconditional branches have a 32-bit offset,
569 * so they could overflow; we check to make
570 * sure they don't. Conditional branches have
571 * an 8-bit offset, and the from address is <=
572 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
573 * is sufficiently small that adding 255 to it
574 * won't overflow.
575 *
576 * We know that len is <= BPF_MAXINSNS, and we
577 * assume that BPF_MAXINSNS is < the maximum size
578 * of a u_int, so that i + 1 doesn't overflow.
579 *
580 * For userland, we don't know that the from
581 * or len are <= BPF_MAXINSNS, but we know that
582 * from <= len, and, except on a 64-bit system,
583 * it's unlikely that len, if it truly reflects
584 * the size of the program we've been handed,
585 * will be anywhere near the maximum size of
586 * a u_int. We also don't check for backward
587 * branches, as we currently support them in
588 * userland for the protochain operation.
589 */
590 from = i + 1;
591 switch (BPF_OP(p->code)) {
592 case BPF_JA:
593 if (from + p->k >= len)
594 goto out;
595 #if defined(KERNEL) || defined(_KERNEL)
596 if (from + p->k < from)
597 goto out;
598 /*
599 * mark the currently invalid bits for the
600 * destination
601 */
602 mem[from + p->k] |= invalid;
603 invalid = 0;
604 #endif
605 break;
606 case BPF_JEQ:
607 case BPF_JGT:
608 case BPF_JGE:
609 case BPF_JSET:
610 if (from + p->jt >= len || from + p->jf >= len)
611 goto out;
612 #if defined(KERNEL) || defined(_KERNEL)
613 /*
614 * mark the currently invalid bits for both
615 * possible jump destinations
616 */
617 mem[from + p->jt] |= invalid;
618 mem[from + p->jf] |= invalid;
619 invalid = 0;
620 #endif
621 break;
622 default:
623 goto out;
624 }
625 break;
626 case BPF_RET:
627 break;
628 case BPF_MISC:
629 break;
630 default:
631 goto out;
632 }
633 }
634 ok = 1;
635 out:
636 #if defined(KERNEL) || defined(_KERNEL)
637 kmem_free(mem, size);
638 #endif
639 return ok;
640 }
641