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