bpf_filter.c revision 1.47 1 /* $NetBSD: bpf_filter.c,v 1.47 2011/07/14 10:43:55 drochner 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.47 2011/07/14 10:43:55 drochner 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 memset(mem, 0, sizeof(mem));
152 --pc;
153 for (;;) {
154 ++pc;
155 switch (pc->code) {
156
157 default:
158 #ifdef _KERNEL
159 return 0;
160 #else
161 abort();
162 /*NOTREACHED*/
163 #endif
164 case BPF_RET|BPF_K:
165 return (u_int)pc->k;
166
167 case BPF_RET|BPF_A:
168 return (u_int)A;
169
170 case BPF_LD|BPF_W|BPF_ABS:
171 k = pc->k;
172 if (k + sizeof(int32_t) > buflen) {
173 #ifdef _KERNEL
174 int merr = 0; /* XXX: GCC */
175
176 if (buflen != 0)
177 return 0;
178 A = m_xword((const struct mbuf *)p, k, &merr);
179 if (merr != 0)
180 return 0;
181 continue;
182 #else
183 return 0;
184 #endif
185 }
186 A = EXTRACT_LONG(&p[k]);
187 continue;
188
189 case BPF_LD|BPF_H|BPF_ABS:
190 k = pc->k;
191 if (k + sizeof(int16_t) > buflen) {
192 #ifdef _KERNEL
193 int merr;
194
195 if (buflen != 0)
196 return 0;
197 A = m_xhalf((const struct mbuf *)p, k, &merr);
198 if (merr != 0)
199 return 0;
200 continue;
201 #else
202 return 0;
203 #endif
204 }
205 A = EXTRACT_SHORT(&p[k]);
206 continue;
207
208 case BPF_LD|BPF_B|BPF_ABS:
209 k = pc->k;
210 if (k >= buflen) {
211 #ifdef _KERNEL
212 const struct mbuf *m;
213 int len;
214
215 if (buflen != 0)
216 return 0;
217 m = (const struct mbuf *)p;
218 MINDEX(len, m, k);
219 A = mtod(m, u_char *)[k];
220 continue;
221 #else
222 return 0;
223 #endif
224 }
225 A = p[k];
226 continue;
227
228 case BPF_LD|BPF_W|BPF_LEN:
229 A = wirelen;
230 continue;
231
232 case BPF_LDX|BPF_W|BPF_LEN:
233 X = wirelen;
234 continue;
235
236 case BPF_LD|BPF_W|BPF_IND:
237 k = X + pc->k;
238 if (k + sizeof(int32_t) > buflen) {
239 #ifdef _KERNEL
240 int merr = 0; /* XXX: GCC */
241
242 if (buflen != 0)
243 return 0;
244 A = m_xword((const struct mbuf *)p, k, &merr);
245 if (merr != 0)
246 return 0;
247 continue;
248 #else
249 return 0;
250 #endif
251 }
252 A = EXTRACT_LONG(&p[k]);
253 continue;
254
255 case BPF_LD|BPF_H|BPF_IND:
256 k = X + pc->k;
257 if (k + sizeof(int16_t) > buflen) {
258 #ifdef _KERNEL
259 int merr = 0; /* XXX: GCC */
260
261 if (buflen != 0)
262 return 0;
263 A = m_xhalf((const struct mbuf *)p, k, &merr);
264 if (merr != 0)
265 return 0;
266 continue;
267 #else
268 return 0;
269 #endif
270 }
271 A = EXTRACT_SHORT(&p[k]);
272 continue;
273
274 case BPF_LD|BPF_B|BPF_IND:
275 k = X + pc->k;
276 if (k >= buflen) {
277 #ifdef _KERNEL
278 const struct mbuf *m;
279 int len;
280
281 if (buflen != 0)
282 return 0;
283 m = (const struct mbuf *)p;
284 MINDEX(len, m, k);
285 A = mtod(m, u_char *)[k];
286 continue;
287 #else
288 return 0;
289 #endif
290 }
291 A = p[k];
292 continue;
293
294 case BPF_LDX|BPF_MSH|BPF_B:
295 k = pc->k;
296 if (k >= buflen) {
297 #ifdef _KERNEL
298 const struct mbuf *m;
299 int len;
300
301 if (buflen != 0)
302 return 0;
303 m = (const struct mbuf *)p;
304 MINDEX(len, m, k);
305 X = (mtod(m, char *)[k] & 0xf) << 2;
306 continue;
307 #else
308 return 0;
309 #endif
310 }
311 X = (p[pc->k] & 0xf) << 2;
312 continue;
313
314 case BPF_LD|BPF_IMM:
315 A = pc->k;
316 continue;
317
318 case BPF_LDX|BPF_IMM:
319 X = pc->k;
320 continue;
321
322 case BPF_LD|BPF_MEM:
323 A = mem[pc->k];
324 continue;
325
326 case BPF_LDX|BPF_MEM:
327 X = mem[pc->k];
328 continue;
329
330 case BPF_ST:
331 mem[pc->k] = A;
332 continue;
333
334 case BPF_STX:
335 mem[pc->k] = X;
336 continue;
337
338 case BPF_JMP|BPF_JA:
339 pc += pc->k;
340 continue;
341
342 case BPF_JMP|BPF_JGT|BPF_K:
343 pc += (A > pc->k) ? pc->jt : pc->jf;
344 continue;
345
346 case BPF_JMP|BPF_JGE|BPF_K:
347 pc += (A >= pc->k) ? pc->jt : pc->jf;
348 continue;
349
350 case BPF_JMP|BPF_JEQ|BPF_K:
351 pc += (A == pc->k) ? pc->jt : pc->jf;
352 continue;
353
354 case BPF_JMP|BPF_JSET|BPF_K:
355 pc += (A & pc->k) ? pc->jt : pc->jf;
356 continue;
357
358 case BPF_JMP|BPF_JGT|BPF_X:
359 pc += (A > X) ? pc->jt : pc->jf;
360 continue;
361
362 case BPF_JMP|BPF_JGE|BPF_X:
363 pc += (A >= X) ? pc->jt : pc->jf;
364 continue;
365
366 case BPF_JMP|BPF_JEQ|BPF_X:
367 pc += (A == X) ? pc->jt : pc->jf;
368 continue;
369
370 case BPF_JMP|BPF_JSET|BPF_X:
371 pc += (A & X) ? pc->jt : pc->jf;
372 continue;
373
374 case BPF_ALU|BPF_ADD|BPF_X:
375 A += X;
376 continue;
377
378 case BPF_ALU|BPF_SUB|BPF_X:
379 A -= X;
380 continue;
381
382 case BPF_ALU|BPF_MUL|BPF_X:
383 A *= X;
384 continue;
385
386 case BPF_ALU|BPF_DIV|BPF_X:
387 if (X == 0)
388 return 0;
389 A /= X;
390 continue;
391
392 case BPF_ALU|BPF_AND|BPF_X:
393 A &= X;
394 continue;
395
396 case BPF_ALU|BPF_OR|BPF_X:
397 A |= X;
398 continue;
399
400 case BPF_ALU|BPF_LSH|BPF_X:
401 A <<= X;
402 continue;
403
404 case BPF_ALU|BPF_RSH|BPF_X:
405 A >>= X;
406 continue;
407
408 case BPF_ALU|BPF_ADD|BPF_K:
409 A += pc->k;
410 continue;
411
412 case BPF_ALU|BPF_SUB|BPF_K:
413 A -= pc->k;
414 continue;
415
416 case BPF_ALU|BPF_MUL|BPF_K:
417 A *= pc->k;
418 continue;
419
420 case BPF_ALU|BPF_DIV|BPF_K:
421 A /= pc->k;
422 continue;
423
424 case BPF_ALU|BPF_AND|BPF_K:
425 A &= pc->k;
426 continue;
427
428 case BPF_ALU|BPF_OR|BPF_K:
429 A |= pc->k;
430 continue;
431
432 case BPF_ALU|BPF_LSH|BPF_K:
433 A <<= pc->k;
434 continue;
435
436 case BPF_ALU|BPF_RSH|BPF_K:
437 A >>= pc->k;
438 continue;
439
440 case BPF_ALU|BPF_NEG:
441 A = -A;
442 continue;
443
444 case BPF_MISC|BPF_TAX:
445 X = A;
446 continue;
447
448 case BPF_MISC|BPF_TXA:
449 A = X;
450 continue;
451 }
452 }
453 }
454
455 /*
456 * Return true if the 'fcode' is a valid filter program.
457 * The constraints are that each jump be forward and to a valid
458 * code, that memory accesses are within valid ranges (to the
459 * extent that this can be checked statically; loads of packet
460 * data have to be, and are, also checked at run time), and that
461 * the code terminates with either an accept or reject.
462 *
463 * The kernel needs to be able to verify an application's filter code.
464 * Otherwise, a bogus program could easily crash the system.
465 */
466 __CTASSERT(BPF_MEMWORDS == sizeof(uint16_t) * NBBY);
467
468 int
469 bpf_validate(const struct bpf_insn *f, int signed_len)
470 {
471 u_int i, from, len, ok = 0;
472 const struct bpf_insn *p;
473 #if defined(KERNEL) || defined(_KERNEL)
474 uint16_t *mem, invalid;
475 size_t size;
476 #endif
477
478 len = (u_int)signed_len;
479 if (len < 1)
480 return 0;
481 #if defined(KERNEL) || defined(_KERNEL)
482 if (len > BPF_MAXINSNS)
483 return 0;
484 #endif
485 if (BPF_CLASS(f[len - 1].code) != BPF_RET)
486 return 0;
487
488 #if defined(KERNEL) || defined(_KERNEL)
489 mem = kmem_zalloc(size = sizeof(*mem) * len, KM_SLEEP);
490 invalid = ~0; /* All is invalid on startup */
491 #endif
492
493 for (i = 0; i < len; ++i) {
494 #if defined(KERNEL) || defined(_KERNEL)
495 /* blend in any invalid bits for current pc */
496 invalid |= mem[i];
497 #endif
498 p = &f[i];
499 switch (BPF_CLASS(p->code)) {
500 /*
501 * Check that memory operations use valid addresses.
502 */
503 case BPF_LD:
504 case BPF_LDX:
505 switch (BPF_MODE(p->code)) {
506 case BPF_MEM:
507 /*
508 * There's no maximum packet data size
509 * in userland. The runtime packet length
510 * check suffices.
511 */
512 #if defined(KERNEL) || defined(_KERNEL)
513 /*
514 * More strict check with actual packet length
515 * is done runtime.
516 */
517 if (p->k >= BPF_MEMWORDS)
518 goto out;
519 /* check for current memory invalid */
520 if (invalid & (1 << p->k))
521 goto out;
522 #endif
523 break;
524 case BPF_ABS:
525 case BPF_IND:
526 case BPF_MSH:
527 case BPF_IMM:
528 case BPF_LEN:
529 break;
530 default:
531 goto out;
532 }
533 break;
534 case BPF_ST:
535 case BPF_STX:
536 if (p->k >= BPF_MEMWORDS)
537 goto out;
538 #if defined(KERNEL) || defined(_KERNEL)
539 /* validate the memory word */
540 invalid &= ~(1 << p->k);
541 #endif
542 break;
543 case BPF_ALU:
544 switch (BPF_OP(p->code)) {
545 case BPF_ADD:
546 case BPF_SUB:
547 case BPF_MUL:
548 case BPF_OR:
549 case BPF_AND:
550 case BPF_LSH:
551 case BPF_RSH:
552 case BPF_NEG:
553 break;
554 case BPF_DIV:
555 /*
556 * Check for constant division by 0.
557 */
558 if (BPF_SRC(p->code) == BPF_K && p->k == 0)
559 goto out;
560 break;
561 default:
562 goto out;
563 }
564 break;
565 case BPF_JMP:
566 /*
567 * Check that jumps are within the code block,
568 * and that unconditional branches don't go
569 * backwards as a result of an overflow.
570 * Unconditional branches have a 32-bit offset,
571 * so they could overflow; we check to make
572 * sure they don't. Conditional branches have
573 * an 8-bit offset, and the from address is <=
574 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
575 * is sufficiently small that adding 255 to it
576 * won't overflow.
577 *
578 * We know that len is <= BPF_MAXINSNS, and we
579 * assume that BPF_MAXINSNS is < the maximum size
580 * of a u_int, so that i + 1 doesn't overflow.
581 *
582 * For userland, we don't know that the from
583 * or len are <= BPF_MAXINSNS, but we know that
584 * from <= len, and, except on a 64-bit system,
585 * it's unlikely that len, if it truly reflects
586 * the size of the program we've been handed,
587 * will be anywhere near the maximum size of
588 * a u_int. We also don't check for backward
589 * branches, as we currently support them in
590 * userland for the protochain operation.
591 */
592 from = i + 1;
593 switch (BPF_OP(p->code)) {
594 case BPF_JA:
595 if (from + p->k >= len)
596 goto out;
597 #if defined(KERNEL) || defined(_KERNEL)
598 if (from + p->k < from)
599 goto out;
600 /*
601 * mark the currently invalid bits for the
602 * destination
603 */
604 mem[from + p->k] |= invalid;
605 invalid = 0;
606 #endif
607 break;
608 case BPF_JEQ:
609 case BPF_JGT:
610 case BPF_JGE:
611 case BPF_JSET:
612 if (from + p->jt >= len || from + p->jf >= len)
613 goto out;
614 #if defined(KERNEL) || defined(_KERNEL)
615 /*
616 * mark the currently invalid bits for both
617 * possible jump destinations
618 */
619 mem[from + p->jt] |= invalid;
620 mem[from + p->jf] |= invalid;
621 invalid = 0;
622 #endif
623 break;
624 default:
625 goto out;
626 }
627 break;
628 case BPF_RET:
629 break;
630 case BPF_MISC:
631 break;
632 default:
633 goto out;
634 }
635 }
636 ok = 1;
637 out:
638 #if defined(KERNEL) || defined(_KERNEL)
639 kmem_free(mem, size);
640 #endif
641 return ok;
642 }
643