Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[~shefty/rdma-dev.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, filter->len);
93
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_S_ALU_ADD_X:
132                         A += X;
133                         continue;
134                 case BPF_S_ALU_ADD_K:
135                         A += fentry->k;
136                         continue;
137                 case BPF_S_ALU_SUB_X:
138                         A -= X;
139                         continue;
140                 case BPF_S_ALU_SUB_K:
141                         A -= fentry->k;
142                         continue;
143                 case BPF_S_ALU_MUL_X:
144                         A *= X;
145                         continue;
146                 case BPF_S_ALU_MUL_K:
147                         A *= fentry->k;
148                         continue;
149                 case BPF_S_ALU_DIV_X:
150                         if (X == 0)
151                                 return 0;
152                         A /= X;
153                         continue;
154                 case BPF_S_ALU_DIV_K:
155                         A /= fentry->k;
156                         continue;
157                 case BPF_S_ALU_AND_X:
158                         A &= X;
159                         continue;
160                 case BPF_S_ALU_AND_K:
161                         A &= fentry->k;
162                         continue;
163                 case BPF_S_ALU_OR_X:
164                         A |= X;
165                         continue;
166                 case BPF_S_ALU_OR_K:
167                         A |= fentry->k;
168                         continue;
169                 case BPF_S_ALU_LSH_X:
170                         A <<= X;
171                         continue;
172                 case BPF_S_ALU_LSH_K:
173                         A <<= fentry->k;
174                         continue;
175                 case BPF_S_ALU_RSH_X:
176                         A >>= X;
177                         continue;
178                 case BPF_S_ALU_RSH_K:
179                         A >>= fentry->k;
180                         continue;
181                 case BPF_S_ALU_NEG:
182                         A = -A;
183                         continue;
184                 case BPF_S_JMP_JA:
185                         pc += fentry->k;
186                         continue;
187                 case BPF_S_JMP_JGT_K:
188                         pc += (A > fentry->k) ? fentry->jt : fentry->jf;
189                         continue;
190                 case BPF_S_JMP_JGE_K:
191                         pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
192                         continue;
193                 case BPF_S_JMP_JEQ_K:
194                         pc += (A == fentry->k) ? fentry->jt : fentry->jf;
195                         continue;
196                 case BPF_S_JMP_JSET_K:
197                         pc += (A & fentry->k) ? fentry->jt : fentry->jf;
198                         continue;
199                 case BPF_S_JMP_JGT_X:
200                         pc += (A > X) ? fentry->jt : fentry->jf;
201                         continue;
202                 case BPF_S_JMP_JGE_X:
203                         pc += (A >= X) ? fentry->jt : fentry->jf;
204                         continue;
205                 case BPF_S_JMP_JEQ_X:
206                         pc += (A == X) ? fentry->jt : fentry->jf;
207                         continue;
208                 case BPF_S_JMP_JSET_X:
209                         pc += (A & X) ? fentry->jt : fentry->jf;
210                         continue;
211                 case BPF_S_LD_W_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_S_LD_H_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_S_LD_B_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_S_LD_W_LEN:
239                         A = skb->len;
240                         continue;
241                 case BPF_S_LDX_W_LEN:
242                         X = skb->len;
243                         continue;
244                 case BPF_S_LD_W_IND:
245                         k = X + fentry->k;
246                         goto load_w;
247                 case BPF_S_LD_H_IND:
248                         k = X + fentry->k;
249                         goto load_h;
250                 case BPF_S_LD_B_IND:
251                         k = X + fentry->k;
252                         goto load_b;
253                 case BPF_S_LDX_B_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_S_LD_IMM:
261                         A = fentry->k;
262                         continue;
263                 case BPF_S_LDX_IMM:
264                         X = fentry->k;
265                         continue;
266                 case BPF_S_LD_MEM:
267                         A = mem[fentry->k];
268                         continue;
269                 case BPF_S_LDX_MEM:
270                         X = mem[fentry->k];
271                         continue;
272                 case BPF_S_MISC_TAX:
273                         X = A;
274                         continue;
275                 case BPF_S_MISC_TXA:
276                         A = X;
277                         continue;
278                 case BPF_S_RET_K:
279                         return fentry->k;
280                 case BPF_S_RET_A:
281                         return A;
282                 case BPF_S_ST:
283                         mem[fentry->k] = A;
284                         continue;
285                 case BPF_S_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                         if (!skb->dev)
306                                 return 0;
307                         A = skb->dev->ifindex;
308                         continue;
309                 case SKF_AD_MARK:
310                         A = skb->mark;
311                         continue;
312                 case SKF_AD_QUEUE:
313                         A = skb->queue_mapping;
314                         continue;
315                 case SKF_AD_HATYPE:
316                         if (!skb->dev)
317                                 return 0;
318                         A = skb->dev->type;
319                         continue;
320                 case SKF_AD_NLATTR: {
321                         struct nlattr *nla;
322
323                         if (skb_is_nonlinear(skb))
324                                 return 0;
325                         if (A > skb->len - sizeof(struct nlattr))
326                                 return 0;
327
328                         nla = nla_find((struct nlattr *)&skb->data[A],
329                                        skb->len - A, X);
330                         if (nla)
331                                 A = (void *)nla - (void *)skb->data;
332                         else
333                                 A = 0;
334                         continue;
335                 }
336                 case SKF_AD_NLATTR_NEST: {
337                         struct nlattr *nla;
338
339                         if (skb_is_nonlinear(skb))
340                                 return 0;
341                         if (A > skb->len - sizeof(struct nlattr))
342                                 return 0;
343
344                         nla = (struct nlattr *)&skb->data[A];
345                         if (nla->nla_len > A - skb->len)
346                                 return 0;
347
348                         nla = nla_find_nested(nla, X);
349                         if (nla)
350                                 A = (void *)nla - (void *)skb->data;
351                         else
352                                 A = 0;
353                         continue;
354                 }
355                 default:
356                         return 0;
357                 }
358         }
359
360         return 0;
361 }
362 EXPORT_SYMBOL(sk_run_filter);
363
364 /**
365  *      sk_chk_filter - verify socket filter code
366  *      @filter: filter to verify
367  *      @flen: length of filter
368  *
369  * Check the user's filter code. If we let some ugly
370  * filter code slip through kaboom! The filter must contain
371  * no references or jumps that are out of range, no illegal
372  * instructions, and must end with a RET instruction.
373  *
374  * All jumps are forward as they are not signed.
375  *
376  * Returns 0 if the rule set is legal or -EINVAL if not.
377  */
378 int sk_chk_filter(struct sock_filter *filter, int flen)
379 {
380         struct sock_filter *ftest;
381         int pc;
382
383         if (flen == 0 || flen > BPF_MAXINSNS)
384                 return -EINVAL;
385
386         /* check the filter code now */
387         for (pc = 0; pc < flen; pc++) {
388                 ftest = &filter[pc];
389
390                 /* Only allow valid instructions */
391                 switch (ftest->code) {
392                 case BPF_ALU|BPF_ADD|BPF_K:
393                         ftest->code = BPF_S_ALU_ADD_K;
394                         break;
395                 case BPF_ALU|BPF_ADD|BPF_X:
396                         ftest->code = BPF_S_ALU_ADD_X;
397                         break;
398                 case BPF_ALU|BPF_SUB|BPF_K:
399                         ftest->code = BPF_S_ALU_SUB_K;
400                         break;
401                 case BPF_ALU|BPF_SUB|BPF_X:
402                         ftest->code = BPF_S_ALU_SUB_X;
403                         break;
404                 case BPF_ALU|BPF_MUL|BPF_K:
405                         ftest->code = BPF_S_ALU_MUL_K;
406                         break;
407                 case BPF_ALU|BPF_MUL|BPF_X:
408                         ftest->code = BPF_S_ALU_MUL_X;
409                         break;
410                 case BPF_ALU|BPF_DIV|BPF_X:
411                         ftest->code = BPF_S_ALU_DIV_X;
412                         break;
413                 case BPF_ALU|BPF_AND|BPF_K:
414                         ftest->code = BPF_S_ALU_AND_K;
415                         break;
416                 case BPF_ALU|BPF_AND|BPF_X:
417                         ftest->code = BPF_S_ALU_AND_X;
418                         break;
419                 case BPF_ALU|BPF_OR|BPF_K:
420                         ftest->code = BPF_S_ALU_OR_K;
421                         break;
422                 case BPF_ALU|BPF_OR|BPF_X:
423                         ftest->code = BPF_S_ALU_OR_X;
424                         break;
425                 case BPF_ALU|BPF_LSH|BPF_K:
426                         ftest->code = BPF_S_ALU_LSH_K;
427                         break;
428                 case BPF_ALU|BPF_LSH|BPF_X:
429                         ftest->code = BPF_S_ALU_LSH_X;
430                         break;
431                 case BPF_ALU|BPF_RSH|BPF_K:
432                         ftest->code = BPF_S_ALU_RSH_K;
433                         break;
434                 case BPF_ALU|BPF_RSH|BPF_X:
435                         ftest->code = BPF_S_ALU_RSH_X;
436                         break;
437                 case BPF_ALU|BPF_NEG:
438                         ftest->code = BPF_S_ALU_NEG;
439                         break;
440                 case BPF_LD|BPF_W|BPF_ABS:
441                         ftest->code = BPF_S_LD_W_ABS;
442                         break;
443                 case BPF_LD|BPF_H|BPF_ABS:
444                         ftest->code = BPF_S_LD_H_ABS;
445                         break;
446                 case BPF_LD|BPF_B|BPF_ABS:
447                         ftest->code = BPF_S_LD_B_ABS;
448                         break;
449                 case BPF_LD|BPF_W|BPF_LEN:
450                         ftest->code = BPF_S_LD_W_LEN;
451                         break;
452                 case BPF_LD|BPF_W|BPF_IND:
453                         ftest->code = BPF_S_LD_W_IND;
454                         break;
455                 case BPF_LD|BPF_H|BPF_IND:
456                         ftest->code = BPF_S_LD_H_IND;
457                         break;
458                 case BPF_LD|BPF_B|BPF_IND:
459                         ftest->code = BPF_S_LD_B_IND;
460                         break;
461                 case BPF_LD|BPF_IMM:
462                         ftest->code = BPF_S_LD_IMM;
463                         break;
464                 case BPF_LDX|BPF_W|BPF_LEN:
465                         ftest->code = BPF_S_LDX_W_LEN;
466                         break;
467                 case BPF_LDX|BPF_B|BPF_MSH:
468                         ftest->code = BPF_S_LDX_B_MSH;
469                         break;
470                 case BPF_LDX|BPF_IMM:
471                         ftest->code = BPF_S_LDX_IMM;
472                         break;
473                 case BPF_MISC|BPF_TAX:
474                         ftest->code = BPF_S_MISC_TAX;
475                         break;
476                 case BPF_MISC|BPF_TXA:
477                         ftest->code = BPF_S_MISC_TXA;
478                         break;
479                 case BPF_RET|BPF_K:
480                         ftest->code = BPF_S_RET_K;
481                         break;
482                 case BPF_RET|BPF_A:
483                         ftest->code = BPF_S_RET_A;
484                         break;
485
486                 /* Some instructions need special checks */
487
488                         /* check for division by zero */
489                 case BPF_ALU|BPF_DIV|BPF_K:
490                         if (ftest->k == 0)
491                                 return -EINVAL;
492                         ftest->code = BPF_S_ALU_DIV_K;
493                         break;
494
495                 /* check for invalid memory addresses */
496                 case BPF_LD|BPF_MEM:
497                         if (ftest->k >= BPF_MEMWORDS)
498                                 return -EINVAL;
499                         ftest->code = BPF_S_LD_MEM;
500                         break;
501                 case BPF_LDX|BPF_MEM:
502                         if (ftest->k >= BPF_MEMWORDS)
503                                 return -EINVAL;
504                         ftest->code = BPF_S_LDX_MEM;
505                         break;
506                 case BPF_ST:
507                         if (ftest->k >= BPF_MEMWORDS)
508                                 return -EINVAL;
509                         ftest->code = BPF_S_ST;
510                         break;
511                 case BPF_STX:
512                         if (ftest->k >= BPF_MEMWORDS)
513                                 return -EINVAL;
514                         ftest->code = BPF_S_STX;
515                         break;
516
517                 case BPF_JMP|BPF_JA:
518                         /*
519                          * Note, the large ftest->k might cause loops.
520                          * Compare this with conditional jumps below,
521                          * where offsets are limited. --ANK (981016)
522                          */
523                         if (ftest->k >= (unsigned)(flen-pc-1))
524                                 return -EINVAL;
525                         ftest->code = BPF_S_JMP_JA;
526                         break;
527
528                 case BPF_JMP|BPF_JEQ|BPF_K:
529                         ftest->code = BPF_S_JMP_JEQ_K;
530                         break;
531                 case BPF_JMP|BPF_JEQ|BPF_X:
532                         ftest->code = BPF_S_JMP_JEQ_X;
533                         break;
534                 case BPF_JMP|BPF_JGE|BPF_K:
535                         ftest->code = BPF_S_JMP_JGE_K;
536                         break;
537                 case BPF_JMP|BPF_JGE|BPF_X:
538                         ftest->code = BPF_S_JMP_JGE_X;
539                         break;
540                 case BPF_JMP|BPF_JGT|BPF_K:
541                         ftest->code = BPF_S_JMP_JGT_K;
542                         break;
543                 case BPF_JMP|BPF_JGT|BPF_X:
544                         ftest->code = BPF_S_JMP_JGT_X;
545                         break;
546                 case BPF_JMP|BPF_JSET|BPF_K:
547                         ftest->code = BPF_S_JMP_JSET_K;
548                         break;
549                 case BPF_JMP|BPF_JSET|BPF_X:
550                         ftest->code = BPF_S_JMP_JSET_X;
551                         break;
552
553                 default:
554                         return -EINVAL;
555                 }
556
557                         /* for conditionals both must be safe */
558                 switch (ftest->code) {
559                 case BPF_S_JMP_JEQ_K:
560                 case BPF_S_JMP_JEQ_X:
561                 case BPF_S_JMP_JGE_K:
562                 case BPF_S_JMP_JGE_X:
563                 case BPF_S_JMP_JGT_K:
564                 case BPF_S_JMP_JGT_X:
565                 case BPF_S_JMP_JSET_X:
566                 case BPF_S_JMP_JSET_K:
567                         if (pc + ftest->jt + 1 >= flen ||
568                             pc + ftest->jf + 1 >= flen)
569                                 return -EINVAL;
570                 }
571         }
572
573         /* last instruction must be a RET code */
574         switch (filter[flen - 1].code) {
575         case BPF_S_RET_K:
576         case BPF_S_RET_A:
577                 return 0;
578                 break;
579                 default:
580                         return -EINVAL;
581                 }
582 }
583 EXPORT_SYMBOL(sk_chk_filter);
584
585 /**
586  *      sk_filter_rcu_release: Release a socket filter by rcu_head
587  *      @rcu: rcu_head that contains the sk_filter to free
588  */
589 static void sk_filter_rcu_release(struct rcu_head *rcu)
590 {
591         struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
592
593         sk_filter_release(fp);
594 }
595
596 static void sk_filter_delayed_uncharge(struct sock *sk, struct sk_filter *fp)
597 {
598         unsigned int size = sk_filter_len(fp);
599
600         atomic_sub(size, &sk->sk_omem_alloc);
601         call_rcu_bh(&fp->rcu, sk_filter_rcu_release);
602 }
603
604 /**
605  *      sk_attach_filter - attach a socket filter
606  *      @fprog: the filter program
607  *      @sk: the socket to use
608  *
609  * Attach the user's filter code. We first run some sanity checks on
610  * it to make sure it does not explode on us later. If an error
611  * occurs or there is insufficient memory for the filter a negative
612  * errno code is returned. On success the return is zero.
613  */
614 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
615 {
616         struct sk_filter *fp, *old_fp;
617         unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
618         int err;
619
620         /* Make sure new filter is there and in the right amounts. */
621         if (fprog->filter == NULL)
622                 return -EINVAL;
623
624         fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
625         if (!fp)
626                 return -ENOMEM;
627         if (copy_from_user(fp->insns, fprog->filter, fsize)) {
628                 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
629                 return -EFAULT;
630         }
631
632         atomic_set(&fp->refcnt, 1);
633         fp->len = fprog->len;
634
635         err = sk_chk_filter(fp->insns, fp->len);
636         if (err) {
637                 sk_filter_uncharge(sk, fp);
638                 return err;
639         }
640
641         old_fp = rcu_dereference_protected(sk->sk_filter,
642                                            sock_owned_by_user(sk));
643         rcu_assign_pointer(sk->sk_filter, fp);
644
645         if (old_fp)
646                 sk_filter_delayed_uncharge(sk, old_fp);
647         return 0;
648 }
649 EXPORT_SYMBOL_GPL(sk_attach_filter);
650
651 int sk_detach_filter(struct sock *sk)
652 {
653         int ret = -ENOENT;
654         struct sk_filter *filter;
655
656         filter = rcu_dereference_protected(sk->sk_filter,
657                                            sock_owned_by_user(sk));
658         if (filter) {
659                 rcu_assign_pointer(sk->sk_filter, NULL);
660                 sk_filter_delayed_uncharge(sk, filter);
661                 ret = 0;
662         }
663         return ret;
664 }
665 EXPORT_SYMBOL_GPL(sk_detach_filter);