1 /* $NetBSD: bpf_filter.c,v 1.41 2010/12/05 22:40:56 mrg 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.41 2010/12/05 22:40:56 mrg 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 int 465 bpf_validate(const struct bpf_insn *f, int signed_len) 466 { 467 u_int i, from, len; 468 const struct bpf_insn *p; 469 470 len = (u_int)signed_len; 471 if (len < 1) 472 return 0; 473 #if defined(KERNEL) || defined(_KERNEL) 474 if (len > BPF_MAXINSNS) 475 return 0; 476 #endif 477 478 for (i = 0; i < len; ++i) { 479 p = &f[i]; 480 switch (BPF_CLASS(p->code)) { 481 /* 482 * Check that memory operations use valid addresses. 483 */ 484 case BPF_LD: 485 case BPF_LDX: 486 switch (BPF_MODE(p->code)) { 487 case BPF_MEM: 488 /* 489 * There's no maximum packet data size 490 * in userland. The runtime packet length 491 * check suffices. 492 */ 493 #if defined(KERNEL) || defined(_KERNEL) 494 /* 495 * More strict check with actual packet length 496 * is done runtime. 497 */ 498 if (p->k >= BPF_MEMWORDS) 499 return 0; 500 #endif 501 break; 502 case BPF_ABS: 503 case BPF_IND: 504 case BPF_MSH: 505 case BPF_IMM: 506 case BPF_LEN: 507 break; 508 default: 509 return 0; 510 } 511 break; 512 case BPF_ST: 513 case BPF_STX: 514 if (p->k >= BPF_MEMWORDS) 515 return 0; 516 break; 517 case BPF_ALU: 518 switch (BPF_OP(p->code)) { 519 case BPF_ADD: 520 case BPF_SUB: 521 case BPF_MUL: 522 case BPF_OR: 523 case BPF_AND: 524 case BPF_LSH: 525 case BPF_RSH: 526 case BPF_NEG: 527 break; 528 case BPF_DIV: 529 /* 530 * Check for constant division by 0. 531 */ 532 if (BPF_SRC(p->code) == BPF_K && p->k == 0) 533 return 0; 534 break; 535 default: 536 return 0; 537 } 538 break; 539 case BPF_JMP: 540 /* 541 * Check that jumps are within the code block, 542 * and that unconditional branches don't go 543 * backwards as a result of an overflow. 544 * Unconditional branches have a 32-bit offset, 545 * so they could overflow; we check to make 546 * sure they don't. Conditional branches have 547 * an 8-bit offset, and the from address is <= 548 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS 549 * is sufficiently small that adding 255 to it 550 * won't overflow. 551 * 552 * We know that len is <= BPF_MAXINSNS, and we 553 * assume that BPF_MAXINSNS is < the maximum size 554 * of a u_int, so that i + 1 doesn't overflow. 555 * 556 * For userland, we don't know that the from 557 * or len are <= BPF_MAXINSNS, but we know that 558 * from <= len, and, except on a 64-bit system, 559 * it's unlikely that len, if it truly reflects 560 * the size of the program we've been handed, 561 * will be anywhere near the maximum size of 562 * a u_int. We also don't check for backward 563 * branches, as we currently support them in 564 * userland for the protochain operation. 565 */ 566 from = i + 1; 567 switch (BPF_OP(p->code)) { 568 case BPF_JA: 569 #if defined(KERNEL) || defined(_KERNEL) 570 if (from + p->k < from || from + p->k >= len) 571 #else 572 if (from + p->k >= len) 573 #endif 574 return 0; 575 break; 576 case BPF_JEQ: 577 case BPF_JGT: 578 case BPF_JGE: 579 case BPF_JSET: 580 if (from + p->jt >= len || from + p->jf >= len) 581 return 0; 582 break; 583 default: 584 return 0; 585 } 586 break; 587 case BPF_RET: 588 break; 589 case BPF_MISC: 590 break; 591 default: 592 return 0; 593 } 594 } 595 596 return BPF_CLASS(f[len - 1].code) == BPF_RET; 597 } 598