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