include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6.git] / net / core / filter.c
1 /*
2  * Linux Socket Filter - Kernel level socket filtering
3  *
4  * Author:
5  *     Jay Schulist <jschlst@samba.org>
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  * Kris Katterjohn - Added many additional checks in sk_chk_filter()
17  */
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/mm.h>
22 #include <linux/fcntl.h>
23 #include <linux/socket.h>
24 #include <linux/in.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_packet.h>
28 #include <linux/gfp.h>
29 #include <net/ip.h>
30 #include <net/protocol.h>
31 #include <net/netlink.h>
32 #include <linux/skbuff.h>
33 #include <net/sock.h>
34 #include <linux/errno.h>
35 #include <linux/timer.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <asm/unaligned.h>
39 #include <linux/filter.h>
40
41 /* No hurry in this branch */
42 static void *__load_pointer(struct sk_buff *skb, int k)
43 {
44         u8 *ptr = NULL;
45
46         if (k >= SKF_NET_OFF)
47                 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
48         else if (k >= SKF_LL_OFF)
49                 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
50
51         if (ptr >= skb->head && ptr < skb_tail_pointer(skb))
52                 return ptr;
53         return NULL;
54 }
55
56 static inline void *load_pointer(struct sk_buff *skb, int k,
57                                  unsigned int size, void *buffer)
58 {
59         if (k >= 0)
60                 return skb_header_pointer(skb, k, size, buffer);
61         else {
62                 if (k >= SKF_AD_OFF)
63                         return NULL;
64                 return __load_pointer(skb, k);
65         }
66 }
67
68 /**
69  *      sk_filter - run a packet through a socket filter
70  *      @sk: sock associated with &sk_buff
71  *      @skb: buffer to filter
72  *
73  * Run the filter code and then cut skb->data to correct size returned by
74  * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
75  * than pkt_len we keep whole skb->data. This is the socket level
76  * wrapper to sk_run_filter. It returns 0 if the packet should
77  * be accepted or -EPERM if the packet should be tossed.
78  *
79  */
80 int sk_filter(struct sock *sk, struct sk_buff *skb)
81 {
82         int err;
83         struct sk_filter *filter;
84
85         err = security_sock_rcv_skb(sk, skb);
86         if (err)
87                 return err;
88
89         rcu_read_lock_bh();
90         filter = rcu_dereference_bh(sk->sk_filter);
91         if (filter) {
92                 unsigned int pkt_len = sk_run_filter(skb, filter->insns,
93                                 filter->len);
94                 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
95         }
96         rcu_read_unlock_bh();
97
98         return err;
99 }
100 EXPORT_SYMBOL(sk_filter);
101
102 /**
103  *      sk_run_filter - run a filter on a socket
104  *      @skb: buffer to run the filter on
105  *      @filter: filter to apply
106  *      @flen: length of filter
107  *
108  * Decode and apply filter instructions to the skb->data.
109  * Return length to keep, 0 for none. skb is the data we are
110  * filtering, filter is the array of filter instructions, and
111  * len is the number of filter blocks in the array.
112  */
113 unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
114 {
115         struct sock_filter *fentry;     /* We walk down these */
116         void *ptr;
117         u32 A = 0;                      /* Accumulator */
118         u32 X = 0;                      /* Index Register */
119         u32 mem[BPF_MEMWORDS];          /* Scratch Memory Store */
120         u32 tmp;
121         int k;
122         int pc;
123
124         /*
125          * Process array of filter instructions.
126          */
127         for (pc = 0; pc < flen; pc++) {
128                 fentry = &filter[pc];
129
130                 switch (fentry->code) {
131                 case BPF_ALU|BPF_ADD|BPF_X:
132                         A += X;
133                         continue;
134                 case BPF_ALU|BPF_ADD|BPF_K:
135                         A += fentry->k;
136                         continue;
137                 case BPF_ALU|BPF_SUB|BPF_X:
138                         A -= X;
139                         continue;
140                 case BPF_ALU|BPF_SUB|BPF_K:
141                         A -= fentry->k;
142                         continue;
143                 case BPF_ALU|BPF_MUL|BPF_X:
144                         A *= X;
145                         continue;
146                 case BPF_ALU|BPF_MUL|BPF_K:
147                         A *= fentry->k;
148                         continue;
149                 case BPF_ALU|BPF_DIV|BPF_X:
150                         if (X == 0)
151                                 return 0;
152                         A /= X;
153                         continue;
154                 case BPF_ALU|BPF_DIV|BPF_K:
155                         A /= fentry->k;
156                         continue;
157                 case BPF_ALU|BPF_AND|BPF_X:
158                         A &= X;
159                         continue;
160                 case BPF_ALU|BPF_AND|BPF_K:
161                         A &= fentry->k;
162                         continue;
163                 case BPF_ALU|BPF_OR|BPF_X:
164                         A |= X;
165                         continue;
166                 case BPF_ALU|BPF_OR|BPF_K:
167                         A |= fentry->k;
168                         continue;
169                 case BPF_ALU|BPF_LSH|BPF_X:
170                         A <<= X;
171                         continue;
172                 case BPF_ALU|BPF_LSH|BPF_K:
173                         A <<= fentry->k;
174                         continue;
175                 case BPF_ALU|BPF_RSH|BPF_X:
176                         A >>= X;
177                         continue;
178                 case BPF_ALU|BPF_RSH|BPF_K:
179                         A >>= fentry->k;
180                         continue;
181                 case BPF_ALU|BPF_NEG:
182                         A = -A;
183                         continue;
184                 case BPF_JMP|BPF_JA:
185                         pc += fentry->k;
186                         continue;
187                 case BPF_JMP|BPF_JGT|BPF_K:
188                         pc += (A > fentry->k) ? fentry->jt : fentry->jf;
189                         continue;
190                 case BPF_JMP|BPF_JGE|BPF_K:
191                         pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
192                         continue;
193                 case BPF_JMP|BPF_JEQ|BPF_K:
194                         pc += (A == fentry->k) ? fentry->jt : fentry->jf;
195                         continue;
196                 case BPF_JMP|BPF_JSET|BPF_K:
197                         pc += (A & fentry->k) ? fentry->jt : fentry->jf;
198                         continue;
199                 case BPF_JMP|BPF_JGT|BPF_X:
200                         pc += (A > X) ? fentry->jt : fentry->jf;
201                         continue;
202                 case BPF_JMP|BPF_JGE|BPF_X:
203                         pc += (A >= X) ? fentry->jt : fentry->jf;
204                         continue;
205                 case BPF_JMP|BPF_JEQ|BPF_X:
206                         pc += (A == X) ? fentry->jt : fentry->jf;
207                         continue;
208                 case BPF_JMP|BPF_JSET|BPF_X:
209                         pc += (A & X) ? fentry->jt : fentry->jf;
210                         continue;
211                 case BPF_LD|BPF_W|BPF_ABS:
212                         k = fentry->k;
213 load_w:
214                         ptr = load_pointer(skb, k, 4, &tmp);
215                         if (ptr != NULL) {
216                                 A = get_unaligned_be32(ptr);
217                                 continue;
218                         }
219                         break;
220                 case BPF_LD|BPF_H|BPF_ABS:
221                         k = fentry->k;
222 load_h:
223                         ptr = load_pointer(skb, k, 2, &tmp);
224                         if (ptr != NULL) {
225                                 A = get_unaligned_be16(ptr);
226                                 continue;
227                         }
228                         break;
229                 case BPF_LD|BPF_B|BPF_ABS:
230                         k = fentry->k;
231 load_b:
232                         ptr = load_pointer(skb, k, 1, &tmp);
233                         if (ptr != NULL) {
234                                 A = *(u8 *)ptr;
235                                 continue;
236                         }
237                         break;
238                 case BPF_LD|BPF_W|BPF_LEN:
239                         A = skb->len;
240                         continue;
241                 case BPF_LDX|BPF_W|BPF_LEN:
242                         X = skb->len;
243                         continue;
244                 case BPF_LD|BPF_W|BPF_IND:
245                         k = X + fentry->k;
246                         goto load_w;
247                 case BPF_LD|BPF_H|BPF_IND:
248                         k = X + fentry->k;
249                         goto load_h;
250                 case BPF_LD|BPF_B|BPF_IND:
251                         k = X + fentry->k;
252                         goto load_b;
253                 case BPF_LDX|BPF_B|BPF_MSH:
254                         ptr = load_pointer(skb, fentry->k, 1, &tmp);
255                         if (ptr != NULL) {
256                                 X = (*(u8 *)ptr & 0xf) << 2;
257                                 continue;
258                         }
259                         return 0;
260                 case BPF_LD|BPF_IMM:
261                         A = fentry->k;
262                         continue;
263                 case BPF_LDX|BPF_IMM:
264                         X = fentry->k;
265                         continue;
266                 case BPF_LD|BPF_MEM:
267                         A = mem[fentry->k];
268                         continue;
269                 case BPF_LDX|BPF_MEM:
270                         X = mem[fentry->k];
271                         continue;
272                 case BPF_MISC|BPF_TAX:
273                         X = A;
274                         continue;
275                 case BPF_MISC|BPF_TXA:
276                         A = X;
277                         continue;
278                 case BPF_RET|BPF_K:
279                         return fentry->k;
280                 case BPF_RET|BPF_A:
281                         return A;
282                 case BPF_ST:
283                         mem[fentry->k] = A;
284                         continue;
285                 case BPF_STX:
286                         mem[fentry->k] = X;
287                         continue;
288                 default:
289                         WARN_ON(1);
290                         return 0;
291                 }
292
293                 /*
294                  * Handle ancillary data, which are impossible
295                  * (or very difficult) to get parsing packet contents.
296                  */
297                 switch (k-SKF_AD_OFF) {
298                 case SKF_AD_PROTOCOL:
299                         A = ntohs(skb->protocol);
300                         continue;
301                 case SKF_AD_PKTTYPE:
302                         A = skb->pkt_type;
303                         continue;
304                 case SKF_AD_IFINDEX:
305                         A = skb->dev->ifindex;
306                         continue;
307                 case SKF_AD_MARK:
308                         A = skb->mark;
309                         continue;
310                 case SKF_AD_QUEUE:
311                         A = skb->queue_mapping;
312                         continue;
313                 case SKF_AD_NLATTR: {
314                         struct nlattr *nla;
315
316                         if (skb_is_nonlinear(skb))
317                                 return 0;
318                         if (A > skb->len - sizeof(struct nlattr))
319                                 return 0;
320
321                         nla = nla_find((struct nlattr *)&skb->data[A],
322                                        skb->len - A, X);
323                         if (nla)
324                                 A = (void *)nla - (void *)skb->data;
325                         else
326                                 A = 0;
327                         continue;
328                 }
329                 case SKF_AD_NLATTR_NEST: {
330                         struct nlattr *nla;
331
332                         if (skb_is_nonlinear(skb))
333                                 return 0;
334                         if (A > skb->len - sizeof(struct nlattr))
335                                 return 0;
336
337                         nla = (struct nlattr *)&skb->data[A];
338                         if (nla->nla_len > A - skb->len)
339                                 return 0;
340
341                         nla = nla_find_nested(nla, X);
342                         if (nla)
343                                 A = (void *)nla - (void *)skb->data;
344                         else
345                                 A = 0;
346                         continue;
347                 }
348                 default:
349                         return 0;
350                 }
351         }
352
353         return 0;
354 }
355 EXPORT_SYMBOL(sk_run_filter);
356
357 /**
358  *      sk_chk_filter - verify socket filter code
359  *      @filter: filter to verify
360  *      @flen: length of filter
361  *
362  * Check the user's filter code. If we let some ugly
363  * filter code slip through kaboom! The filter must contain
364  * no references or jumps that are out of range, no illegal
365  * instructions, and must end with a RET instruction.
366  *
367  * All jumps are forward as they are not signed.
368  *
369  * Returns 0 if the rule set is legal or -EINVAL if not.
370  */
371 int sk_chk_filter(struct sock_filter *filter, int flen)
372 {
373         struct sock_filter *ftest;
374         int pc;
375
376         if (flen == 0 || flen > BPF_MAXINSNS)
377                 return -EINVAL;
378
379         /* check the filter code now */
380         for (pc = 0; pc < flen; pc++) {
381                 ftest = &filter[pc];
382
383                 /* Only allow valid instructions */
384                 switch (ftest->code) {
385                 case BPF_ALU|BPF_ADD|BPF_K:
386                 case BPF_ALU|BPF_ADD|BPF_X:
387                 case BPF_ALU|BPF_SUB|BPF_K:
388                 case BPF_ALU|BPF_SUB|BPF_X:
389                 case BPF_ALU|BPF_MUL|BPF_K:
390                 case BPF_ALU|BPF_MUL|BPF_X:
391                 case BPF_ALU|BPF_DIV|BPF_X:
392                 case BPF_ALU|BPF_AND|BPF_K:
393                 case BPF_ALU|BPF_AND|BPF_X:
394                 case BPF_ALU|BPF_OR|BPF_K:
395                 case BPF_ALU|BPF_OR|BPF_X:
396                 case BPF_ALU|BPF_LSH|BPF_K:
397                 case BPF_ALU|BPF_LSH|BPF_X:
398                 case BPF_ALU|BPF_RSH|BPF_K:
399                 case BPF_ALU|BPF_RSH|BPF_X:
400                 case BPF_ALU|BPF_NEG:
401                 case BPF_LD|BPF_W|BPF_ABS:
402                 case BPF_LD|BPF_H|BPF_ABS:
403                 case BPF_LD|BPF_B|BPF_ABS:
404                 case BPF_LD|BPF_W|BPF_LEN:
405                 case BPF_LD|BPF_W|BPF_IND:
406                 case BPF_LD|BPF_H|BPF_IND:
407                 case BPF_LD|BPF_B|BPF_IND:
408                 case BPF_LD|BPF_IMM:
409                 case BPF_LDX|BPF_W|BPF_LEN:
410                 case BPF_LDX|BPF_B|BPF_MSH:
411                 case BPF_LDX|BPF_IMM:
412                 case BPF_MISC|BPF_TAX:
413                 case BPF_MISC|BPF_TXA:
414                 case BPF_RET|BPF_K:
415                 case BPF_RET|BPF_A:
416                         break;
417
418                 /* Some instructions need special checks */
419
420                 case BPF_ALU|BPF_DIV|BPF_K:
421                         /* check for division by zero */
422                         if (ftest->k == 0)
423                                 return -EINVAL;
424                         break;
425
426                 case BPF_LD|BPF_MEM:
427                 case BPF_LDX|BPF_MEM:
428                 case BPF_ST:
429                 case BPF_STX:
430                         /* check for invalid memory addresses */
431                         if (ftest->k >= BPF_MEMWORDS)
432                                 return -EINVAL;
433                         break;
434
435                 case BPF_JMP|BPF_JA:
436                         /*
437                          * Note, the large ftest->k might cause loops.
438                          * Compare this with conditional jumps below,
439                          * where offsets are limited. --ANK (981016)
440                          */
441                         if (ftest->k >= (unsigned)(flen-pc-1))
442                                 return -EINVAL;
443                         break;
444
445                 case BPF_JMP|BPF_JEQ|BPF_K:
446                 case BPF_JMP|BPF_JEQ|BPF_X:
447                 case BPF_JMP|BPF_JGE|BPF_K:
448                 case BPF_JMP|BPF_JGE|BPF_X:
449                 case BPF_JMP|BPF_JGT|BPF_K:
450                 case BPF_JMP|BPF_JGT|BPF_X:
451                 case BPF_JMP|BPF_JSET|BPF_K:
452                 case BPF_JMP|BPF_JSET|BPF_X:
453                         /* for conditionals both must be safe */
454                         if (pc + ftest->jt + 1 >= flen ||
455                             pc + ftest->jf + 1 >= flen)
456                                 return -EINVAL;
457                         break;
458
459                 default:
460                         return -EINVAL;
461                 }
462         }
463
464         return (BPF_CLASS(filter[flen - 1].code) == BPF_RET) ? 0 : -EINVAL;
465 }
466 EXPORT_SYMBOL(sk_chk_filter);
467
468 /**
469  *      sk_filter_rcu_release: Release a socket filter by rcu_head
470  *      @rcu: rcu_head that contains the sk_filter to free
471  */
472 static void sk_filter_rcu_release(struct rcu_head *rcu)
473 {
474         struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
475
476         sk_filter_release(fp);
477 }
478
479 static void sk_filter_delayed_uncharge(struct sock *sk, struct sk_filter *fp)
480 {
481         unsigned int size = sk_filter_len(fp);
482
483         atomic_sub(size, &sk->sk_omem_alloc);
484         call_rcu_bh(&fp->rcu, sk_filter_rcu_release);
485 }
486
487 /**
488  *      sk_attach_filter - attach a socket filter
489  *      @fprog: the filter program
490  *      @sk: the socket to use
491  *
492  * Attach the user's filter code. We first run some sanity checks on
493  * it to make sure it does not explode on us later. If an error
494  * occurs or there is insufficient memory for the filter a negative
495  * errno code is returned. On success the return is zero.
496  */
497 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
498 {
499         struct sk_filter *fp, *old_fp;
500         unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
501         int err;
502
503         /* Make sure new filter is there and in the right amounts. */
504         if (fprog->filter == NULL)
505                 return -EINVAL;
506
507         fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
508         if (!fp)
509                 return -ENOMEM;
510         if (copy_from_user(fp->insns, fprog->filter, fsize)) {
511                 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
512                 return -EFAULT;
513         }
514
515         atomic_set(&fp->refcnt, 1);
516         fp->len = fprog->len;
517
518         err = sk_chk_filter(fp->insns, fp->len);
519         if (err) {
520                 sk_filter_uncharge(sk, fp);
521                 return err;
522         }
523
524         rcu_read_lock_bh();
525         old_fp = rcu_dereference_bh(sk->sk_filter);
526         rcu_assign_pointer(sk->sk_filter, fp);
527         rcu_read_unlock_bh();
528
529         if (old_fp)
530                 sk_filter_delayed_uncharge(sk, old_fp);
531         return 0;
532 }
533 EXPORT_SYMBOL_GPL(sk_attach_filter);
534
535 int sk_detach_filter(struct sock *sk)
536 {
537         int ret = -ENOENT;
538         struct sk_filter *filter;
539
540         rcu_read_lock_bh();
541         filter = rcu_dereference_bh(sk->sk_filter);
542         if (filter) {
543                 rcu_assign_pointer(sk->sk_filter, NULL);
544                 sk_filter_delayed_uncharge(sk, filter);
545                 ret = 0;
546         }
547         rcu_read_unlock_bh();
548         return ret;
549 }
550 EXPORT_SYMBOL_GPL(sk_detach_filter);