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>
22 #include <linux/fcntl.h>
23 #include <linux/socket.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_packet.h>
29 #include <net/protocol.h>
30 #include <linux/skbuff.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <asm/unaligned.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_network_header(skb) + k - SKF_NET_OFF;
46 else if (k >= SKF_LL_OFF)
47 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
49 if (ptr >= skb->head && ptr < skb_tail_pointer(skb))
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.
77 unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
79 struct sock_filter *fentry; /* We walk down these */
81 u32 A = 0; /* Accumulator */
82 u32 X = 0; /* Index Register */
83 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
89 * Process array of filter instructions.
91 for (pc = 0; pc < flen; pc++) {
94 switch (fentry->code) {
95 case BPF_ALU|BPF_ADD|BPF_X:
98 case BPF_ALU|BPF_ADD|BPF_K:
101 case BPF_ALU|BPF_SUB|BPF_X:
104 case BPF_ALU|BPF_SUB|BPF_K:
107 case BPF_ALU|BPF_MUL|BPF_X:
110 case BPF_ALU|BPF_MUL|BPF_K:
113 case BPF_ALU|BPF_DIV|BPF_X:
118 case BPF_ALU|BPF_DIV|BPF_K:
121 case BPF_ALU|BPF_AND|BPF_X:
124 case BPF_ALU|BPF_AND|BPF_K:
127 case BPF_ALU|BPF_OR|BPF_X:
130 case BPF_ALU|BPF_OR|BPF_K:
133 case BPF_ALU|BPF_LSH|BPF_X:
136 case BPF_ALU|BPF_LSH|BPF_K:
139 case BPF_ALU|BPF_RSH|BPF_X:
142 case BPF_ALU|BPF_RSH|BPF_K:
145 case BPF_ALU|BPF_NEG:
151 case BPF_JMP|BPF_JGT|BPF_K:
152 pc += (A > fentry->k) ? fentry->jt : fentry->jf;
154 case BPF_JMP|BPF_JGE|BPF_K:
155 pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
157 case BPF_JMP|BPF_JEQ|BPF_K:
158 pc += (A == fentry->k) ? fentry->jt : fentry->jf;
160 case BPF_JMP|BPF_JSET|BPF_K:
161 pc += (A & fentry->k) ? fentry->jt : fentry->jf;
163 case BPF_JMP|BPF_JGT|BPF_X:
164 pc += (A > X) ? fentry->jt : fentry->jf;
166 case BPF_JMP|BPF_JGE|BPF_X:
167 pc += (A >= X) ? fentry->jt : fentry->jf;
169 case BPF_JMP|BPF_JEQ|BPF_X:
170 pc += (A == X) ? fentry->jt : fentry->jf;
172 case BPF_JMP|BPF_JSET|BPF_X:
173 pc += (A & X) ? fentry->jt : fentry->jf;
175 case BPF_LD|BPF_W|BPF_ABS:
178 ptr = load_pointer(skb, k, 4, &tmp);
180 A = ntohl(get_unaligned((__be32 *)ptr));
184 case BPF_LD|BPF_H|BPF_ABS:
187 ptr = load_pointer(skb, k, 2, &tmp);
189 A = ntohs(get_unaligned((__be16 *)ptr));
193 case BPF_LD|BPF_B|BPF_ABS:
196 ptr = load_pointer(skb, k, 1, &tmp);
202 case BPF_LD|BPF_W|BPF_LEN:
205 case BPF_LDX|BPF_W|BPF_LEN:
208 case BPF_LD|BPF_W|BPF_IND:
211 case BPF_LD|BPF_H|BPF_IND:
214 case BPF_LD|BPF_B|BPF_IND:
217 case BPF_LDX|BPF_B|BPF_MSH:
218 ptr = load_pointer(skb, fentry->k, 1, &tmp);
220 X = (*(u8 *)ptr & 0xf) << 2;
227 case BPF_LDX|BPF_IMM:
233 case BPF_LDX|BPF_MEM:
236 case BPF_MISC|BPF_TAX:
239 case BPF_MISC|BPF_TXA:
258 * Handle ancillary data, which are impossible
259 * (or very difficult) to get parsing packet contents.
261 switch (k-SKF_AD_OFF) {
262 case SKF_AD_PROTOCOL:
263 A = ntohs(skb->protocol);
269 A = skb->dev->ifindex;
278 EXPORT_SYMBOL(sk_run_filter);
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 * All jumps are forward as they are not signed.
292 * Returns 0 if the rule set is legal or -EINVAL if not.
294 int sk_chk_filter(struct sock_filter *filter, int flen)
296 struct sock_filter *ftest;
299 if (flen == 0 || flen > BPF_MAXINSNS)
302 /* check the filter code now */
303 for (pc = 0; pc < flen; pc++) {
306 /* Only allow valid instructions */
307 switch (ftest->code) {
308 case BPF_ALU|BPF_ADD|BPF_K:
309 case BPF_ALU|BPF_ADD|BPF_X:
310 case BPF_ALU|BPF_SUB|BPF_K:
311 case BPF_ALU|BPF_SUB|BPF_X:
312 case BPF_ALU|BPF_MUL|BPF_K:
313 case BPF_ALU|BPF_MUL|BPF_X:
314 case BPF_ALU|BPF_DIV|BPF_X:
315 case BPF_ALU|BPF_AND|BPF_K:
316 case BPF_ALU|BPF_AND|BPF_X:
317 case BPF_ALU|BPF_OR|BPF_K:
318 case BPF_ALU|BPF_OR|BPF_X:
319 case BPF_ALU|BPF_LSH|BPF_K:
320 case BPF_ALU|BPF_LSH|BPF_X:
321 case BPF_ALU|BPF_RSH|BPF_K:
322 case BPF_ALU|BPF_RSH|BPF_X:
323 case BPF_ALU|BPF_NEG:
324 case BPF_LD|BPF_W|BPF_ABS:
325 case BPF_LD|BPF_H|BPF_ABS:
326 case BPF_LD|BPF_B|BPF_ABS:
327 case BPF_LD|BPF_W|BPF_LEN:
328 case BPF_LD|BPF_W|BPF_IND:
329 case BPF_LD|BPF_H|BPF_IND:
330 case BPF_LD|BPF_B|BPF_IND:
332 case BPF_LDX|BPF_W|BPF_LEN:
333 case BPF_LDX|BPF_B|BPF_MSH:
334 case BPF_LDX|BPF_IMM:
335 case BPF_MISC|BPF_TAX:
336 case BPF_MISC|BPF_TXA:
341 /* Some instructions need special checks */
343 case BPF_ALU|BPF_DIV|BPF_K:
344 /* check for division by zero */
350 case BPF_LDX|BPF_MEM:
353 /* check for invalid memory addresses */
354 if (ftest->k >= BPF_MEMWORDS)
360 * Note, the large ftest->k might cause loops.
361 * Compare this with conditional jumps below,
362 * where offsets are limited. --ANK (981016)
364 if (ftest->k >= (unsigned)(flen-pc-1))
368 case BPF_JMP|BPF_JEQ|BPF_K:
369 case BPF_JMP|BPF_JEQ|BPF_X:
370 case BPF_JMP|BPF_JGE|BPF_K:
371 case BPF_JMP|BPF_JGE|BPF_X:
372 case BPF_JMP|BPF_JGT|BPF_K:
373 case BPF_JMP|BPF_JGT|BPF_X:
374 case BPF_JMP|BPF_JSET|BPF_K:
375 case BPF_JMP|BPF_JSET|BPF_X:
376 /* for conditionals both must be safe */
377 if (pc + ftest->jt + 1 >= flen ||
378 pc + ftest->jf + 1 >= flen)
387 return (BPF_CLASS(filter[flen - 1].code) == BPF_RET) ? 0 : -EINVAL;
389 EXPORT_SYMBOL(sk_chk_filter);
392 * sk_filter_rcu_release: Release a socket filter by rcu_head
393 * @rcu: rcu_head that contains the sk_filter to free
395 static void sk_filter_rcu_release(struct rcu_head *rcu)
397 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
399 sk_filter_release(fp);
402 static void sk_filter_delayed_uncharge(struct sock *sk, struct sk_filter *fp)
404 unsigned int size = sk_filter_len(fp);
406 atomic_sub(size, &sk->sk_omem_alloc);
407 call_rcu_bh(&fp->rcu, sk_filter_rcu_release);
411 * sk_attach_filter - attach a socket filter
412 * @fprog: the filter program
413 * @sk: the socket to use
415 * Attach the user's filter code. We first run some sanity checks on
416 * it to make sure it does not explode on us later. If an error
417 * occurs or there is insufficient memory for the filter a negative
418 * errno code is returned. On success the return is zero.
420 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
422 struct sk_filter *fp, *old_fp;
423 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
426 /* Make sure new filter is there and in the right amounts. */
427 if (fprog->filter == NULL)
430 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
433 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
434 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
438 atomic_set(&fp->refcnt, 1);
439 fp->len = fprog->len;
441 err = sk_chk_filter(fp->insns, fp->len);
443 sk_filter_uncharge(sk, fp);
448 old_fp = rcu_dereference(sk->sk_filter);
449 rcu_assign_pointer(sk->sk_filter, fp);
450 rcu_read_unlock_bh();
453 sk_filter_delayed_uncharge(sk, old_fp);
457 int sk_detach_filter(struct sock *sk)
460 struct sk_filter *filter;
463 filter = rcu_dereference(sk->sk_filter);
465 rcu_assign_pointer(sk->sk_filter, NULL);
466 sk_filter_delayed_uncharge(sk, filter);
469 rcu_read_unlock_bh();