2 * Linux Socket Filter - Kernel level socket filtering
5 * Jay Schulist <jschlst@samba.org>
7 * Based on the design of:
8 * - The Berkeley Packet Filter
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.
15 * Andi Kleen - Fix a few bad bugs and races.
16 * Kris Katterjohn - Added many additional checks in sk_chk_filter()
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/sched.h>
23 #include <linux/fcntl.h>
24 #include <linux/socket.h>
26 #include <linux/inet.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_packet.h>
30 #include <net/protocol.h>
31 #include <linux/skbuff.h>
33 #include <linux/errno.h>
34 #include <linux/timer.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/filter.h>
39 /* No hurry in this branch */
40 static void *__load_pointer(struct sk_buff *skb, int k)
45 ptr = skb->nh.raw + k - SKF_NET_OFF;
46 else if (k >= SKF_LL_OFF)
47 ptr = skb->mac.raw + k - SKF_LL_OFF;
49 if (ptr >= skb->head && ptr < skb->tail)
54 static inline void *load_pointer(struct sk_buff *skb, int k,
55 unsigned int size, void *buffer)
58 return skb_header_pointer(skb, k, size, buffer);
62 return __load_pointer(skb, k);
67 * sk_run_filter - run a filter on a socket
68 * @skb: buffer to run the filter on
69 * @filter: filter to apply
70 * @flen: length of filter
72 * Decode and apply filter instructions to the skb->data.
73 * Return length to keep, 0 for none. skb is the data we are
74 * filtering, filter is the array of filter instructions, and
75 * len is the number of filter blocks in the array.
78 int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
80 struct sock_filter *fentry; /* We walk down these */
82 u32 A = 0; /* Accumulator */
83 u32 X = 0; /* Index Register */
84 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
90 * Process array of filter instructions.
92 for (pc = 0; pc < flen; pc++) {
95 switch (fentry->code) {
96 case BPF_ALU|BPF_ADD|BPF_X:
99 case BPF_ALU|BPF_ADD|BPF_K:
102 case BPF_ALU|BPF_SUB|BPF_X:
105 case BPF_ALU|BPF_SUB|BPF_K:
108 case BPF_ALU|BPF_MUL|BPF_X:
111 case BPF_ALU|BPF_MUL|BPF_K:
114 case BPF_ALU|BPF_DIV|BPF_X:
119 case BPF_ALU|BPF_DIV|BPF_K:
122 case BPF_ALU|BPF_AND|BPF_X:
125 case BPF_ALU|BPF_AND|BPF_K:
128 case BPF_ALU|BPF_OR|BPF_X:
131 case BPF_ALU|BPF_OR|BPF_K:
134 case BPF_ALU|BPF_LSH|BPF_X:
137 case BPF_ALU|BPF_LSH|BPF_K:
140 case BPF_ALU|BPF_RSH|BPF_X:
143 case BPF_ALU|BPF_RSH|BPF_K:
146 case BPF_ALU|BPF_NEG:
152 case BPF_JMP|BPF_JGT|BPF_K:
153 pc += (A > fentry->k) ? fentry->jt : fentry->jf;
155 case BPF_JMP|BPF_JGE|BPF_K:
156 pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
158 case BPF_JMP|BPF_JEQ|BPF_K:
159 pc += (A == fentry->k) ? fentry->jt : fentry->jf;
161 case BPF_JMP|BPF_JSET|BPF_K:
162 pc += (A & fentry->k) ? fentry->jt : fentry->jf;
164 case BPF_JMP|BPF_JGT|BPF_X:
165 pc += (A > X) ? fentry->jt : fentry->jf;
167 case BPF_JMP|BPF_JGE|BPF_X:
168 pc += (A >= X) ? fentry->jt : fentry->jf;
170 case BPF_JMP|BPF_JEQ|BPF_X:
171 pc += (A == X) ? fentry->jt : fentry->jf;
173 case BPF_JMP|BPF_JSET|BPF_X:
174 pc += (A & X) ? fentry->jt : fentry->jf;
176 case BPF_LD|BPF_W|BPF_ABS:
179 ptr = load_pointer(skb, k, 4, &tmp);
181 A = ntohl(*(u32 *)ptr);
185 case BPF_LD|BPF_H|BPF_ABS:
188 ptr = load_pointer(skb, k, 2, &tmp);
190 A = ntohs(*(u16 *)ptr);
194 case BPF_LD|BPF_B|BPF_ABS:
197 ptr = load_pointer(skb, k, 1, &tmp);
203 case BPF_LD|BPF_W|BPF_LEN:
206 case BPF_LDX|BPF_W|BPF_LEN:
209 case BPF_LD|BPF_W|BPF_IND:
212 case BPF_LD|BPF_H|BPF_IND:
215 case BPF_LD|BPF_B|BPF_IND:
218 case BPF_LDX|BPF_B|BPF_MSH:
219 ptr = load_pointer(skb, fentry->k, 1, &tmp);
221 X = (*(u8 *)ptr & 0xf) << 2;
228 case BPF_LDX|BPF_IMM:
234 case BPF_LDX|BPF_MEM:
237 case BPF_MISC|BPF_TAX:
240 case BPF_MISC|BPF_TXA:
244 return ((unsigned int)fentry->k);
246 return ((unsigned int)A);
259 * Handle ancillary data, which are impossible
260 * (or very difficult) to get parsing packet contents.
262 switch (k-SKF_AD_OFF) {
263 case SKF_AD_PROTOCOL:
264 A = htons(skb->protocol);
270 A = skb->dev->ifindex;
281 * sk_chk_filter - verify socket filter code
282 * @filter: filter to verify
283 * @flen: length of filter
285 * Check the user's filter code. If we let some ugly
286 * filter code slip through kaboom! The filter must contain
287 * no references or jumps that are out of range, no illegal
288 * instructions, and must end with a RET instruction.
290 * Returns 0 if the rule set is legal or a negative errno code if not.
292 int sk_chk_filter(struct sock_filter *filter, int flen)
294 struct sock_filter *ftest;
297 if (flen == 0 || flen > BPF_MAXINSNS)
300 /* check the filter code now */
301 for (pc = 0; pc < flen; pc++) {
302 /* all jumps are forward as they are not signed */
305 /* Only allow valid instructions */
306 switch (ftest->code) {
307 case BPF_ALU|BPF_ADD|BPF_K:
308 case BPF_ALU|BPF_ADD|BPF_X:
309 case BPF_ALU|BPF_SUB|BPF_K:
310 case BPF_ALU|BPF_SUB|BPF_X:
311 case BPF_ALU|BPF_MUL|BPF_K:
312 case BPF_ALU|BPF_MUL|BPF_X:
313 case BPF_ALU|BPF_DIV|BPF_X:
314 case BPF_ALU|BPF_AND|BPF_K:
315 case BPF_ALU|BPF_AND|BPF_X:
316 case BPF_ALU|BPF_OR|BPF_K:
317 case BPF_ALU|BPF_OR|BPF_X:
318 case BPF_ALU|BPF_LSH|BPF_K:
319 case BPF_ALU|BPF_LSH|BPF_X:
320 case BPF_ALU|BPF_RSH|BPF_K:
321 case BPF_ALU|BPF_RSH|BPF_X:
322 case BPF_ALU|BPF_NEG:
323 case BPF_LD|BPF_W|BPF_ABS:
324 case BPF_LD|BPF_H|BPF_ABS:
325 case BPF_LD|BPF_B|BPF_ABS:
326 case BPF_LD|BPF_W|BPF_LEN:
327 case BPF_LD|BPF_W|BPF_IND:
328 case BPF_LD|BPF_H|BPF_IND:
329 case BPF_LD|BPF_B|BPF_IND:
331 case BPF_LDX|BPF_W|BPF_LEN:
332 case BPF_LDX|BPF_B|BPF_MSH:
333 case BPF_LDX|BPF_IMM:
334 case BPF_MISC|BPF_TAX:
335 case BPF_MISC|BPF_TXA:
340 /* Some instructions need special checks */
342 case BPF_ALU|BPF_DIV|BPF_K:
343 /* check for division by zero */
349 case BPF_LDX|BPF_MEM:
352 /* check for invalid memory addresses */
353 if (ftest->k >= BPF_MEMWORDS)
359 * Note, the large ftest->k might cause loops.
360 * Compare this with conditional jumps below,
361 * where offsets are limited. --ANK (981016)
363 if (ftest->k >= (unsigned)(flen-pc-1))
367 case BPF_JMP|BPF_JEQ|BPF_K:
368 case BPF_JMP|BPF_JEQ|BPF_X:
369 case BPF_JMP|BPF_JGE|BPF_K:
370 case BPF_JMP|BPF_JGE|BPF_X:
371 case BPF_JMP|BPF_JGT|BPF_K:
372 case BPF_JMP|BPF_JGT|BPF_X:
373 case BPF_JMP|BPF_JSET|BPF_K:
374 case BPF_JMP|BPF_JSET|BPF_X:
375 /* for conditionals both must be safe */
376 if (pc + ftest->jt + 1 >= flen ||
377 pc + ftest->jf + 1 >= flen)
387 * The program must end with a return. We don't care where they
388 * jumped within the script (its always forwards) but in the end
389 * they _will_ hit this.
391 return (BPF_CLASS(filter[flen - 1].code) == BPF_RET) ? 0 : -EINVAL;
395 * sk_attach_filter - attach a socket filter
396 * @fprog: the filter program
397 * @sk: the socket to use
399 * Attach the user's filter code. We first run some sanity checks on
400 * it to make sure it does not explode on us later. If an error
401 * occurs or there is insufficient memory for the filter a negative
402 * errno code is returned. On success the return is zero.
404 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
406 struct sk_filter *fp;
407 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
410 /* Make sure new filter is there and in the right amounts. */
411 if (fprog->filter == NULL)
414 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
417 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
418 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
422 atomic_set(&fp->refcnt, 1);
423 fp->len = fprog->len;
425 err = sk_chk_filter(fp->insns, fp->len);
427 struct sk_filter *old_fp;
429 spin_lock_bh(&sk->sk_lock.slock);
430 old_fp = sk->sk_filter;
432 spin_unlock_bh(&sk->sk_lock.slock);
437 sk_filter_release(sk, fp);
441 EXPORT_SYMBOL(sk_chk_filter);
442 EXPORT_SYMBOL(sk_run_filter);