1 /*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
4 * Author:
5 * Jay Schulist <jschlst@turbolinux.com>
6 *
7 * Based on the design of:
8 * - The Berkeley Packet Filter
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Andi Kleen - Fix a few bad bugs and races.
16 */
17
18 #include <linux/config.h>
19 #if defined(CONFIG_FILTER)
20
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_packet.h>
31 #include <net/ip.h>
32 #include <net/protocol.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <linux/errno.h>
36 #include <linux/timer.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <linux/filter.h>
40
41 /* No hurry in this branch */
42
43 static u8 *load_pointer(struct sk_buff *skb, int k)
44 {
45 u8 *ptr = NULL;
46
47 if (k>=SKF_NET_OFF)
48 ptr = skb->nh.raw + k - SKF_NET_OFF;
49 else if (k>=SKF_LL_OFF)
50 ptr = skb->mac.raw + k - SKF_LL_OFF;
51
52 if (ptr >= skb->head && ptr < skb->tail)
53 return ptr;
54 return NULL;
55 }
56
57 /**
58 * sk_run_filter - run a filter on a socket
59 * @skb: buffer to run the filter on
60 * @filter: filter to apply
61 * @flen: length of filter
62 *
63 * Decode and apply filter instructions to the skb->data.
64 * Return length to keep, 0 for none. skb is the data we are
65 * filtering, filter is the array of filter instructions, and
66 * len is the number of filter blocks in the array.
67 */
68
69 int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
70 {
71 unsigned char *data = skb->data;
72 /* len is UNSIGNED. Byte wide insns relies only on implicit
73 type casts to prevent reading arbitrary memory locations.
74 */
75 unsigned int len = skb->len;
76 struct sock_filter *fentry; /* We walk down these */
77 u32 A = 0; /* Accumulator */
78 u32 X = 0; /* Index Register */
79 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
80 int k;
81 int pc;
82
83 /*
84 * Process array of filter instructions.
85 */
86
87 for(pc = 0; pc < flen; pc++)
88 {
89 fentry = &filter[pc];
90
91 switch(fentry->code)
92 {
93 case BPF_ALU|BPF_ADD|BPF_X:
94 A += X;
95 continue;
96
97 case BPF_ALU|BPF_ADD|BPF_K:
98 A += fentry->k;
99 continue;
100
101 case BPF_ALU|BPF_SUB|BPF_X:
102 A -= X;
103 continue;
104
105 case BPF_ALU|BPF_SUB|BPF_K:
106 A -= fentry->k;
107 continue;
108
109 case BPF_ALU|BPF_MUL|BPF_X:
110 A *= X;
111 continue;
112
113 case BPF_ALU|BPF_MUL|BPF_K:
114 A *= fentry->k;
115 continue;
116
117 case BPF_ALU|BPF_DIV|BPF_X:
118 if(X == 0)
119 return (0);
120 A /= X;
121 continue;
122
123 case BPF_ALU|BPF_DIV|BPF_K:
124 if(fentry->k == 0)
125 return (0);
126 A /= fentry->k;
127 continue;
128
129 case BPF_ALU|BPF_AND|BPF_X:
130 A &= X;
131 continue;
132
133 case BPF_ALU|BPF_AND|BPF_K:
134 A &= fentry->k;
135 continue;
136
137 case BPF_ALU|BPF_OR|BPF_X:
138 A |= X;
139 continue;
140
141 case BPF_ALU|BPF_OR|BPF_K:
142 A |= fentry->k;
143 continue;
144
145 case BPF_ALU|BPF_LSH|BPF_X:
146 A <<= X;
147 continue;
148
149 case BPF_ALU|BPF_LSH|BPF_K:
150 A <<= fentry->k;
151 continue;
152
153 case BPF_ALU|BPF_RSH|BPF_X:
154 A >>= X;
155 continue;
156
157 case BPF_ALU|BPF_RSH|BPF_K:
158 A >>= fentry->k;
159 continue;
160
161 case BPF_ALU|BPF_NEG:
162 A = -A;
163 continue;
164
165 case BPF_JMP|BPF_JA:
166 pc += fentry->k;
167 continue;
168
169 case BPF_JMP|BPF_JGT|BPF_K:
170 pc += (A > fentry->k) ? fentry->jt : fentry->jf;
171 continue;
172
173 case BPF_JMP|BPF_JGE|BPF_K:
174 pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
175 continue;
176
177 case BPF_JMP|BPF_JEQ|BPF_K:
178 pc += (A == fentry->k) ? fentry->jt : fentry->jf;
179 continue;
180
181 case BPF_JMP|BPF_JSET|BPF_K:
182 pc += (A & fentry->k) ? fentry->jt : fentry->jf;
183 continue;
184
185 case BPF_JMP|BPF_JGT|BPF_X:
186 pc += (A > X) ? fentry->jt : fentry->jf;
187 continue;
188
189 case BPF_JMP|BPF_JGE|BPF_X:
190 pc += (A >= X) ? fentry->jt : fentry->jf;
191 continue;
192
193 case BPF_JMP|BPF_JEQ|BPF_X:
194 pc += (A == X) ? fentry->jt : fentry->jf;
195 continue;
196
197 case BPF_JMP|BPF_JSET|BPF_X:
198 pc += (A & X) ? fentry->jt : fentry->jf;
199 continue;
200
201 case BPF_LD|BPF_W|BPF_ABS:
202 k = fentry->k;
203 load_w:
204 if(k+sizeof(u32) <= len) {
205 A = ntohl(*(u32*)&data[k]);
206 continue;
207 }
208 if (k<0) {
209 u8 *ptr;
210
211 if (k>=SKF_AD_OFF)
212 break;
213 if ((ptr = load_pointer(skb, k)) != NULL) {
214 A = ntohl(*(u32*)ptr);
215 continue;
216 }
217 }
218 return 0;
219
220 case BPF_LD|BPF_H|BPF_ABS:
221 k = fentry->k;
222 load_h:
223 if(k + sizeof(u16) <= len) {
224 A = ntohs(*(u16*)&data[k]);
225 continue;
226 }
227 if (k<0) {
228 u8 *ptr;
229
230 if (k>=SKF_AD_OFF)
231 break;
232 if ((ptr = load_pointer(skb, k)) != NULL) {
233 A = ntohs(*(u16*)ptr);
234 continue;
235 }
236 }
237 return 0;
238
239 case BPF_LD|BPF_B|BPF_ABS:
240 k = fentry->k;
241 load_b:
242 if(k < len) {
243 A = data[k];
244 continue;
245 }
246 if (k<0) {
247 u8 *ptr;
248
249 if (k>=SKF_AD_OFF)
250 break;
251 if ((ptr = load_pointer(skb, k)) != NULL) {
252 A = *ptr;
253 continue;
254 }
255 }
256 return 0;
257
258 case BPF_LD|BPF_W|BPF_LEN:
259 A = len;
260 continue;
261
262 case BPF_LDX|BPF_W|BPF_LEN:
263 X = len;
264 continue;
265
266 case BPF_LD|BPF_W|BPF_IND:
267 k = X + fentry->k;
268 goto load_w;
269
270 case BPF_LD|BPF_H|BPF_IND:
271 k = X + fentry->k;
272 goto load_h;
273
274 case BPF_LD|BPF_B|BPF_IND:
275 k = X + fentry->k;
276 goto load_b;
277
278 case BPF_LDX|BPF_B|BPF_MSH:
279 k = fentry->k;
280 if(k >= len)
281 return (0);
282 X = (data[k] & 0xf) << 2;
283 continue;
284
285 case BPF_LD|BPF_IMM:
286 A = fentry->k;
287 continue;
288
289 case BPF_LDX|BPF_IMM:
290 X = fentry->k;
291 continue;
292
293 case BPF_LD|BPF_MEM:
294 A = mem[fentry->k];
295 continue;
296
297 case BPF_LDX|BPF_MEM:
298 X = mem[fentry->k];
299 continue;
300
301 case BPF_MISC|BPF_TAX:
302 X = A;
303 continue;
304
305 case BPF_MISC|BPF_TXA:
306 A = X;
307 continue;
308
309 case BPF_RET|BPF_K:
310 return ((unsigned int)fentry->k);
311
312 case BPF_RET|BPF_A:
313 return ((unsigned int)A);
314
315 case BPF_ST:
316 mem[fentry->k] = A;
317 continue;
318
319 case BPF_STX:
320 mem[fentry->k] = X;
321 continue;
322
323 default:
324 /* Invalid instruction counts as RET */
325 return (0);
326 }
327
328 /* Handle ancillary data, which are impossible
329 (or very difficult) to get parsing packet contents.
330 */
331 switch (k-SKF_AD_OFF) {
332 case SKF_AD_PROTOCOL:
333 A = htons(skb->protocol);
334 continue;
335 case SKF_AD_PKTTYPE:
336 A = skb->pkt_type;
337 continue;
338 case SKF_AD_IFINDEX:
339 A = skb->dev->ifindex;
340 continue;
341 default:
342 return 0;
343 }
344 }
345
346 return (0);
347 }
348
349 /**
350 * sk_chk_filter - verify socket filter code
351 * @filter: filter to verify
352 * @flen: length of filter
353 *
354 * Check the user's filter code. If we let some ugly
355 * filter code slip through kaboom! The filter must contain
356 * no references or jumps that are out of range, no illegal instructions
357 * and no backward jumps. It must end with a RET instruction
358 *
359 * Returns 0 if the rule set is legal or a negative errno code if not.
360 */
361
362 int sk_chk_filter(struct sock_filter *filter, int flen)
363 {
364 struct sock_filter *ftest;
365 int pc;
366
367 /*
368 * Check the filter code now.
369 */
370 for(pc = 0; pc < flen; pc++)
371 {
372 /*
373 * All jumps are forward as they are not signed
374 */
375
376 ftest = &filter[pc];
377 if(BPF_CLASS(ftest->code) == BPF_JMP)
378 {
379 /*
380 * But they mustn't jump off the end.
381 */
382 if(BPF_OP(ftest->code) == BPF_JA)
383 {
384 /* Note, the large ftest->k might cause
385 loops. Compare this with conditional
386 jumps below, where offsets are limited. --ANK (981016)
387 */
388 if (ftest->k >= (unsigned)(flen-pc-1))
389 return (-EINVAL);
390 }
391 else
392 {
393 /*
394 * For conditionals both must be safe
395 */
396 if(pc + ftest->jt +1 >= flen || pc + ftest->jf +1 >= flen)
397 return (-EINVAL);
398 }
399 }
400
401 /*
402 * Check that memory operations use valid addresses.
403 */
404
405 if (ftest->k >= BPF_MEMWORDS)
406 {
407 /*
408 * But it might not be a memory operation...
409 */
410 switch (ftest->code) {
411 case BPF_ST:
412 case BPF_STX:
413 case BPF_LD|BPF_MEM:
414 case BPF_LDX|BPF_MEM:
415 return -EINVAL;
416 }
417 }
418 }
419
420 /*
421 * The program must end with a return. We don't care where they
422 * jumped within the script (its always forwards) but in the
423 * end they _will_ hit this.
424 */
425
426 return (BPF_CLASS(filter[flen - 1].code) == BPF_RET)?0:-EINVAL;
427 }
428
429 /**
430 * sk_attach_filter - attach a socket filter
431 * @fprog: the filter program
432 * @sk: the socket to use
433 *
434 * Attach the user's filter code. We first run some sanity checks on
435 * it to make sure it does not explode on us later. If an error
436 * occurs or there is insufficient memory for the filter a negative
437 * errno code is returned. On success the return is zero.
438 */
439
440 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
441 {
442 struct sk_filter *fp;
443 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
444 int err;
445
446 /* Make sure new filter is there and in the right amounts. */
447 if (fprog->filter == NULL || fprog->len > BPF_MAXINSNS)
448 return (-EINVAL);
449
450 fp = (struct sk_filter *)sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
451 if(fp == NULL)
452 return (-ENOMEM);
453
454 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
455 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
456 return -EFAULT;
457 }
458
459 atomic_set(&fp->refcnt, 1);
460 fp->len = fprog->len;
461
462 if ((err = sk_chk_filter(fp->insns, fp->len))==0) {
463 struct sk_filter *old_fp;
464
465 spin_lock_bh(&sk->lock.slock);
466 old_fp = sk->filter;
467 sk->filter = fp;
468 spin_unlock_bh(&sk->lock.slock);
469 fp = old_fp;
470 }
471
472 if (fp)
473 sk_filter_release(sk, fp);
474
475 return (err);
476 }
477 #endif /* CONFIG_FILTER */
478
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.