filter: constify sk_run_filter()
[linux-3.10.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 #include <linux/reciprocal_div.h>
41
42 enum {
43         BPF_S_RET_K = 1,
44         BPF_S_RET_A,
45         BPF_S_ALU_ADD_K,
46         BPF_S_ALU_ADD_X,
47         BPF_S_ALU_SUB_K,
48         BPF_S_ALU_SUB_X,
49         BPF_S_ALU_MUL_K,
50         BPF_S_ALU_MUL_X,
51         BPF_S_ALU_DIV_X,
52         BPF_S_ALU_AND_K,
53         BPF_S_ALU_AND_X,
54         BPF_S_ALU_OR_K,
55         BPF_S_ALU_OR_X,
56         BPF_S_ALU_LSH_K,
57         BPF_S_ALU_LSH_X,
58         BPF_S_ALU_RSH_K,
59         BPF_S_ALU_RSH_X,
60         BPF_S_ALU_NEG,
61         BPF_S_LD_W_ABS,
62         BPF_S_LD_H_ABS,
63         BPF_S_LD_B_ABS,
64         BPF_S_LD_W_LEN,
65         BPF_S_LD_W_IND,
66         BPF_S_LD_H_IND,
67         BPF_S_LD_B_IND,
68         BPF_S_LD_IMM,
69         BPF_S_LDX_W_LEN,
70         BPF_S_LDX_B_MSH,
71         BPF_S_LDX_IMM,
72         BPF_S_MISC_TAX,
73         BPF_S_MISC_TXA,
74         BPF_S_ALU_DIV_K,
75         BPF_S_LD_MEM,
76         BPF_S_LDX_MEM,
77         BPF_S_ST,
78         BPF_S_STX,
79         BPF_S_JMP_JA,
80         BPF_S_JMP_JEQ_K,
81         BPF_S_JMP_JEQ_X,
82         BPF_S_JMP_JGE_K,
83         BPF_S_JMP_JGE_X,
84         BPF_S_JMP_JGT_K,
85         BPF_S_JMP_JGT_X,
86         BPF_S_JMP_JSET_K,
87         BPF_S_JMP_JSET_X,
88 };
89
90 /* No hurry in this branch */
91 static void *__load_pointer(const struct sk_buff *skb, int k)
92 {
93         u8 *ptr = NULL;
94
95         if (k >= SKF_NET_OFF)
96                 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
97         else if (k >= SKF_LL_OFF)
98                 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
99
100         if (ptr >= skb->head && ptr < skb_tail_pointer(skb))
101                 return ptr;
102         return NULL;
103 }
104
105 static inline void *load_pointer(const struct sk_buff *skb, int k,
106                                  unsigned int size, void *buffer)
107 {
108         if (k >= 0)
109                 return skb_header_pointer(skb, k, size, buffer);
110         else {
111                 if (k >= SKF_AD_OFF)
112                         return NULL;
113                 return __load_pointer(skb, k);
114         }
115 }
116
117 /**
118  *      sk_filter - run a packet through a socket filter
119  *      @sk: sock associated with &sk_buff
120  *      @skb: buffer to filter
121  *
122  * Run the filter code and then cut skb->data to correct size returned by
123  * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
124  * than pkt_len we keep whole skb->data. This is the socket level
125  * wrapper to sk_run_filter. It returns 0 if the packet should
126  * be accepted or -EPERM if the packet should be tossed.
127  *
128  */
129 int sk_filter(struct sock *sk, struct sk_buff *skb)
130 {
131         int err;
132         struct sk_filter *filter;
133
134         err = security_sock_rcv_skb(sk, skb);
135         if (err)
136                 return err;
137
138         rcu_read_lock_bh();
139         filter = rcu_dereference_bh(sk->sk_filter);
140         if (filter) {
141                 unsigned int pkt_len = sk_run_filter(skb, filter->insns);
142
143                 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
144         }
145         rcu_read_unlock_bh();
146
147         return err;
148 }
149 EXPORT_SYMBOL(sk_filter);
150
151 /**
152  *      sk_run_filter - run a filter on a socket
153  *      @skb: buffer to run the filter on
154  *      @filter: filter to apply
155  *
156  * Decode and apply filter instructions to the skb->data.
157  * Return length to keep, 0 for none. @skb is the data we are
158  * filtering, @filter is the array of filter instructions.
159  * Because all jumps are guaranteed to be before last instruction,
160  * and last instruction guaranteed to be a RET, we dont need to check
161  * flen. (We used to pass to this function the length of filter)
162  */
163 unsigned int sk_run_filter(const struct sk_buff *skb,
164                            const struct sock_filter *fentry)
165 {
166         void *ptr;
167         u32 A = 0;                      /* Accumulator */
168         u32 X = 0;                      /* Index Register */
169         u32 mem[BPF_MEMWORDS];          /* Scratch Memory Store */
170         u32 tmp;
171         int k;
172
173         /*
174          * Process array of filter instructions.
175          */
176         for (;; fentry++) {
177 #if defined(CONFIG_X86_32)
178 #define K (fentry->k)
179 #else
180                 const u32 K = fentry->k;
181 #endif
182
183                 switch (fentry->code) {
184                 case BPF_S_ALU_ADD_X:
185                         A += X;
186                         continue;
187                 case BPF_S_ALU_ADD_K:
188                         A += K;
189                         continue;
190                 case BPF_S_ALU_SUB_X:
191                         A -= X;
192                         continue;
193                 case BPF_S_ALU_SUB_K:
194                         A -= K;
195                         continue;
196                 case BPF_S_ALU_MUL_X:
197                         A *= X;
198                         continue;
199                 case BPF_S_ALU_MUL_K:
200                         A *= K;
201                         continue;
202                 case BPF_S_ALU_DIV_X:
203                         if (X == 0)
204                                 return 0;
205                         A /= X;
206                         continue;
207                 case BPF_S_ALU_DIV_K:
208                         A = reciprocal_divide(A, K);
209                         continue;
210                 case BPF_S_ALU_AND_X:
211                         A &= X;
212                         continue;
213                 case BPF_S_ALU_AND_K:
214                         A &= K;
215                         continue;
216                 case BPF_S_ALU_OR_X:
217                         A |= X;
218                         continue;
219                 case BPF_S_ALU_OR_K:
220                         A |= K;
221                         continue;
222                 case BPF_S_ALU_LSH_X:
223                         A <<= X;
224                         continue;
225                 case BPF_S_ALU_LSH_K:
226                         A <<= K;
227                         continue;
228                 case BPF_S_ALU_RSH_X:
229                         A >>= X;
230                         continue;
231                 case BPF_S_ALU_RSH_K:
232                         A >>= K;
233                         continue;
234                 case BPF_S_ALU_NEG:
235                         A = -A;
236                         continue;
237                 case BPF_S_JMP_JA:
238                         fentry += K;
239                         continue;
240                 case BPF_S_JMP_JGT_K:
241                         fentry += (A > K) ? fentry->jt : fentry->jf;
242                         continue;
243                 case BPF_S_JMP_JGE_K:
244                         fentry += (A >= K) ? fentry->jt : fentry->jf;
245                         continue;
246                 case BPF_S_JMP_JEQ_K:
247                         fentry += (A == K) ? fentry->jt : fentry->jf;
248                         continue;
249                 case BPF_S_JMP_JSET_K:
250                         fentry += (A & K) ? fentry->jt : fentry->jf;
251                         continue;
252                 case BPF_S_JMP_JGT_X:
253                         fentry += (A > X) ? fentry->jt : fentry->jf;
254                         continue;
255                 case BPF_S_JMP_JGE_X:
256                         fentry += (A >= X) ? fentry->jt : fentry->jf;
257                         continue;
258                 case BPF_S_JMP_JEQ_X:
259                         fentry += (A == X) ? fentry->jt : fentry->jf;
260                         continue;
261                 case BPF_S_JMP_JSET_X:
262                         fentry += (A & X) ? fentry->jt : fentry->jf;
263                         continue;
264                 case BPF_S_LD_W_ABS:
265                         k = K;
266 load_w:
267                         ptr = load_pointer(skb, k, 4, &tmp);
268                         if (ptr != NULL) {
269                                 A = get_unaligned_be32(ptr);
270                                 continue;
271                         }
272                         break;
273                 case BPF_S_LD_H_ABS:
274                         k = K;
275 load_h:
276                         ptr = load_pointer(skb, k, 2, &tmp);
277                         if (ptr != NULL) {
278                                 A = get_unaligned_be16(ptr);
279                                 continue;
280                         }
281                         break;
282                 case BPF_S_LD_B_ABS:
283                         k = K;
284 load_b:
285                         ptr = load_pointer(skb, k, 1, &tmp);
286                         if (ptr != NULL) {
287                                 A = *(u8 *)ptr;
288                                 continue;
289                         }
290                         break;
291                 case BPF_S_LD_W_LEN:
292                         A = skb->len;
293                         continue;
294                 case BPF_S_LDX_W_LEN:
295                         X = skb->len;
296                         continue;
297                 case BPF_S_LD_W_IND:
298                         k = X + K;
299                         goto load_w;
300                 case BPF_S_LD_H_IND:
301                         k = X + K;
302                         goto load_h;
303                 case BPF_S_LD_B_IND:
304                         k = X + K;
305                         goto load_b;
306                 case BPF_S_LDX_B_MSH:
307                         ptr = load_pointer(skb, K, 1, &tmp);
308                         if (ptr != NULL) {
309                                 X = (*(u8 *)ptr & 0xf) << 2;
310                                 continue;
311                         }
312                         return 0;
313                 case BPF_S_LD_IMM:
314                         A = K;
315                         continue;
316                 case BPF_S_LDX_IMM:
317                         X = K;
318                         continue;
319                 case BPF_S_LD_MEM:
320                         A = mem[K];
321                         continue;
322                 case BPF_S_LDX_MEM:
323                         X = mem[K];
324                         continue;
325                 case BPF_S_MISC_TAX:
326                         X = A;
327                         continue;
328                 case BPF_S_MISC_TXA:
329                         A = X;
330                         continue;
331                 case BPF_S_RET_K:
332                         return K;
333                 case BPF_S_RET_A:
334                         return A;
335                 case BPF_S_ST:
336                         mem[K] = A;
337                         continue;
338                 case BPF_S_STX:
339                         mem[K] = X;
340                         continue;
341                 default:
342                         WARN_ON(1);
343                         return 0;
344                 }
345
346                 /*
347                  * Handle ancillary data, which are impossible
348                  * (or very difficult) to get parsing packet contents.
349                  */
350                 switch (k-SKF_AD_OFF) {
351                 case SKF_AD_PROTOCOL:
352                         A = ntohs(skb->protocol);
353                         continue;
354                 case SKF_AD_PKTTYPE:
355                         A = skb->pkt_type;
356                         continue;
357                 case SKF_AD_IFINDEX:
358                         if (!skb->dev)
359                                 return 0;
360                         A = skb->dev->ifindex;
361                         continue;
362                 case SKF_AD_MARK:
363                         A = skb->mark;
364                         continue;
365                 case SKF_AD_QUEUE:
366                         A = skb->queue_mapping;
367                         continue;
368                 case SKF_AD_HATYPE:
369                         if (!skb->dev)
370                                 return 0;
371                         A = skb->dev->type;
372                         continue;
373                 case SKF_AD_RXHASH:
374                         A = skb->rxhash;
375                         continue;
376                 case SKF_AD_CPU:
377                         A = raw_smp_processor_id();
378                         continue;
379                 case SKF_AD_NLATTR: {
380                         struct nlattr *nla;
381
382                         if (skb_is_nonlinear(skb))
383                                 return 0;
384                         if (A > skb->len - sizeof(struct nlattr))
385                                 return 0;
386
387                         nla = nla_find((struct nlattr *)&skb->data[A],
388                                        skb->len - A, X);
389                         if (nla)
390                                 A = (void *)nla - (void *)skb->data;
391                         else
392                                 A = 0;
393                         continue;
394                 }
395                 case SKF_AD_NLATTR_NEST: {
396                         struct nlattr *nla;
397
398                         if (skb_is_nonlinear(skb))
399                                 return 0;
400                         if (A > skb->len - sizeof(struct nlattr))
401                                 return 0;
402
403                         nla = (struct nlattr *)&skb->data[A];
404                         if (nla->nla_len > A - skb->len)
405                                 return 0;
406
407                         nla = nla_find_nested(nla, X);
408                         if (nla)
409                                 A = (void *)nla - (void *)skb->data;
410                         else
411                                 A = 0;
412                         continue;
413                 }
414                 default:
415                         return 0;
416                 }
417         }
418
419         return 0;
420 }
421 EXPORT_SYMBOL(sk_run_filter);
422
423 /*
424  * Security :
425  * A BPF program is able to use 16 cells of memory to store intermediate
426  * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
427  * As we dont want to clear mem[] array for each packet going through
428  * sk_run_filter(), we check that filter loaded by user never try to read
429  * a cell if not previously written, and we check all branches to be sure
430  * a malicious user doesnt try to abuse us.
431  */
432 static int check_load_and_stores(struct sock_filter *filter, int flen)
433 {
434         u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
435         int pc, ret = 0;
436
437         BUILD_BUG_ON(BPF_MEMWORDS > 16);
438         masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
439         if (!masks)
440                 return -ENOMEM;
441         memset(masks, 0xff, flen * sizeof(*masks));
442
443         for (pc = 0; pc < flen; pc++) {
444                 memvalid &= masks[pc];
445
446                 switch (filter[pc].code) {
447                 case BPF_S_ST:
448                 case BPF_S_STX:
449                         memvalid |= (1 << filter[pc].k);
450                         break;
451                 case BPF_S_LD_MEM:
452                 case BPF_S_LDX_MEM:
453                         if (!(memvalid & (1 << filter[pc].k))) {
454                                 ret = -EINVAL;
455                                 goto error;
456                         }
457                         break;
458                 case BPF_S_JMP_JA:
459                         /* a jump must set masks on target */
460                         masks[pc + 1 + filter[pc].k] &= memvalid;
461                         memvalid = ~0;
462                         break;
463                 case BPF_S_JMP_JEQ_K:
464                 case BPF_S_JMP_JEQ_X:
465                 case BPF_S_JMP_JGE_K:
466                 case BPF_S_JMP_JGE_X:
467                 case BPF_S_JMP_JGT_K:
468                 case BPF_S_JMP_JGT_X:
469                 case BPF_S_JMP_JSET_X:
470                 case BPF_S_JMP_JSET_K:
471                         /* a jump must set masks on targets */
472                         masks[pc + 1 + filter[pc].jt] &= memvalid;
473                         masks[pc + 1 + filter[pc].jf] &= memvalid;
474                         memvalid = ~0;
475                         break;
476                 }
477         }
478 error:
479         kfree(masks);
480         return ret;
481 }
482
483 /**
484  *      sk_chk_filter - verify socket filter code
485  *      @filter: filter to verify
486  *      @flen: length of filter
487  *
488  * Check the user's filter code. If we let some ugly
489  * filter code slip through kaboom! The filter must contain
490  * no references or jumps that are out of range, no illegal
491  * instructions, and must end with a RET instruction.
492  *
493  * All jumps are forward as they are not signed.
494  *
495  * Returns 0 if the rule set is legal or -EINVAL if not.
496  */
497 int sk_chk_filter(struct sock_filter *filter, int flen)
498 {
499         /*
500          * Valid instructions are initialized to non-0.
501          * Invalid instructions are initialized to 0.
502          */
503         static const u8 codes[] = {
504                 [BPF_ALU|BPF_ADD|BPF_K]  = BPF_S_ALU_ADD_K,
505                 [BPF_ALU|BPF_ADD|BPF_X]  = BPF_S_ALU_ADD_X,
506                 [BPF_ALU|BPF_SUB|BPF_K]  = BPF_S_ALU_SUB_K,
507                 [BPF_ALU|BPF_SUB|BPF_X]  = BPF_S_ALU_SUB_X,
508                 [BPF_ALU|BPF_MUL|BPF_K]  = BPF_S_ALU_MUL_K,
509                 [BPF_ALU|BPF_MUL|BPF_X]  = BPF_S_ALU_MUL_X,
510                 [BPF_ALU|BPF_DIV|BPF_X]  = BPF_S_ALU_DIV_X,
511                 [BPF_ALU|BPF_AND|BPF_K]  = BPF_S_ALU_AND_K,
512                 [BPF_ALU|BPF_AND|BPF_X]  = BPF_S_ALU_AND_X,
513                 [BPF_ALU|BPF_OR|BPF_K]   = BPF_S_ALU_OR_K,
514                 [BPF_ALU|BPF_OR|BPF_X]   = BPF_S_ALU_OR_X,
515                 [BPF_ALU|BPF_LSH|BPF_K]  = BPF_S_ALU_LSH_K,
516                 [BPF_ALU|BPF_LSH|BPF_X]  = BPF_S_ALU_LSH_X,
517                 [BPF_ALU|BPF_RSH|BPF_K]  = BPF_S_ALU_RSH_K,
518                 [BPF_ALU|BPF_RSH|BPF_X]  = BPF_S_ALU_RSH_X,
519                 [BPF_ALU|BPF_NEG]        = BPF_S_ALU_NEG,
520                 [BPF_LD|BPF_W|BPF_ABS]   = BPF_S_LD_W_ABS,
521                 [BPF_LD|BPF_H|BPF_ABS]   = BPF_S_LD_H_ABS,
522                 [BPF_LD|BPF_B|BPF_ABS]   = BPF_S_LD_B_ABS,
523                 [BPF_LD|BPF_W|BPF_LEN]   = BPF_S_LD_W_LEN,
524                 [BPF_LD|BPF_W|BPF_IND]   = BPF_S_LD_W_IND,
525                 [BPF_LD|BPF_H|BPF_IND]   = BPF_S_LD_H_IND,
526                 [BPF_LD|BPF_B|BPF_IND]   = BPF_S_LD_B_IND,
527                 [BPF_LD|BPF_IMM]         = BPF_S_LD_IMM,
528                 [BPF_LDX|BPF_W|BPF_LEN]  = BPF_S_LDX_W_LEN,
529                 [BPF_LDX|BPF_B|BPF_MSH]  = BPF_S_LDX_B_MSH,
530                 [BPF_LDX|BPF_IMM]        = BPF_S_LDX_IMM,
531                 [BPF_MISC|BPF_TAX]       = BPF_S_MISC_TAX,
532                 [BPF_MISC|BPF_TXA]       = BPF_S_MISC_TXA,
533                 [BPF_RET|BPF_K]          = BPF_S_RET_K,
534                 [BPF_RET|BPF_A]          = BPF_S_RET_A,
535                 [BPF_ALU|BPF_DIV|BPF_K]  = BPF_S_ALU_DIV_K,
536                 [BPF_LD|BPF_MEM]         = BPF_S_LD_MEM,
537                 [BPF_LDX|BPF_MEM]        = BPF_S_LDX_MEM,
538                 [BPF_ST]                 = BPF_S_ST,
539                 [BPF_STX]                = BPF_S_STX,
540                 [BPF_JMP|BPF_JA]         = BPF_S_JMP_JA,
541                 [BPF_JMP|BPF_JEQ|BPF_K]  = BPF_S_JMP_JEQ_K,
542                 [BPF_JMP|BPF_JEQ|BPF_X]  = BPF_S_JMP_JEQ_X,
543                 [BPF_JMP|BPF_JGE|BPF_K]  = BPF_S_JMP_JGE_K,
544                 [BPF_JMP|BPF_JGE|BPF_X]  = BPF_S_JMP_JGE_X,
545                 [BPF_JMP|BPF_JGT|BPF_K]  = BPF_S_JMP_JGT_K,
546                 [BPF_JMP|BPF_JGT|BPF_X]  = BPF_S_JMP_JGT_X,
547                 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
548                 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
549         };
550         int pc;
551
552         if (flen == 0 || flen > BPF_MAXINSNS)
553                 return -EINVAL;
554
555         /* check the filter code now */
556         for (pc = 0; pc < flen; pc++) {
557                 struct sock_filter *ftest = &filter[pc];
558                 u16 code = ftest->code;
559
560                 if (code >= ARRAY_SIZE(codes))
561                         return -EINVAL;
562                 code = codes[code];
563                 if (!code)
564                         return -EINVAL;
565                 /* Some instructions need special checks */
566                 switch (code) {
567                 case BPF_S_ALU_DIV_K:
568                         /* check for division by zero */
569                         if (ftest->k == 0)
570                                 return -EINVAL;
571                         ftest->k = reciprocal_value(ftest->k);
572                         break;
573                 case BPF_S_LD_MEM:
574                 case BPF_S_LDX_MEM:
575                 case BPF_S_ST:
576                 case BPF_S_STX:
577                         /* check for invalid memory addresses */
578                         if (ftest->k >= BPF_MEMWORDS)
579                                 return -EINVAL;
580                         break;
581                 case BPF_S_JMP_JA:
582                         /*
583                          * Note, the large ftest->k might cause loops.
584                          * Compare this with conditional jumps below,
585                          * where offsets are limited. --ANK (981016)
586                          */
587                         if (ftest->k >= (unsigned)(flen-pc-1))
588                                 return -EINVAL;
589                         break;
590                 case BPF_S_JMP_JEQ_K:
591                 case BPF_S_JMP_JEQ_X:
592                 case BPF_S_JMP_JGE_K:
593                 case BPF_S_JMP_JGE_X:
594                 case BPF_S_JMP_JGT_K:
595                 case BPF_S_JMP_JGT_X:
596                 case BPF_S_JMP_JSET_X:
597                 case BPF_S_JMP_JSET_K:
598                         /* for conditionals both must be safe */
599                         if (pc + ftest->jt + 1 >= flen ||
600                             pc + ftest->jf + 1 >= flen)
601                                 return -EINVAL;
602                         break;
603                 }
604                 ftest->code = code;
605         }
606
607         /* last instruction must be a RET code */
608         switch (filter[flen - 1].code) {
609         case BPF_S_RET_K:
610         case BPF_S_RET_A:
611                 return check_load_and_stores(filter, flen);
612         }
613         return -EINVAL;
614 }
615 EXPORT_SYMBOL(sk_chk_filter);
616
617 /**
618  *      sk_filter_rcu_release - Release a socket filter by rcu_head
619  *      @rcu: rcu_head that contains the sk_filter to free
620  */
621 static void sk_filter_rcu_release(struct rcu_head *rcu)
622 {
623         struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
624
625         sk_filter_release(fp);
626 }
627
628 static void sk_filter_delayed_uncharge(struct sock *sk, struct sk_filter *fp)
629 {
630         unsigned int size = sk_filter_len(fp);
631
632         atomic_sub(size, &sk->sk_omem_alloc);
633         call_rcu_bh(&fp->rcu, sk_filter_rcu_release);
634 }
635
636 /**
637  *      sk_attach_filter - attach a socket filter
638  *      @fprog: the filter program
639  *      @sk: the socket to use
640  *
641  * Attach the user's filter code. We first run some sanity checks on
642  * it to make sure it does not explode on us later. If an error
643  * occurs or there is insufficient memory for the filter a negative
644  * errno code is returned. On success the return is zero.
645  */
646 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
647 {
648         struct sk_filter *fp, *old_fp;
649         unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
650         int err;
651
652         /* Make sure new filter is there and in the right amounts. */
653         if (fprog->filter == NULL)
654                 return -EINVAL;
655
656         fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
657         if (!fp)
658                 return -ENOMEM;
659         if (copy_from_user(fp->insns, fprog->filter, fsize)) {
660                 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
661                 return -EFAULT;
662         }
663
664         atomic_set(&fp->refcnt, 1);
665         fp->len = fprog->len;
666
667         err = sk_chk_filter(fp->insns, fp->len);
668         if (err) {
669                 sk_filter_uncharge(sk, fp);
670                 return err;
671         }
672
673         old_fp = rcu_dereference_protected(sk->sk_filter,
674                                            sock_owned_by_user(sk));
675         rcu_assign_pointer(sk->sk_filter, fp);
676
677         if (old_fp)
678                 sk_filter_delayed_uncharge(sk, old_fp);
679         return 0;
680 }
681 EXPORT_SYMBOL_GPL(sk_attach_filter);
682
683 int sk_detach_filter(struct sock *sk)
684 {
685         int ret = -ENOENT;
686         struct sk_filter *filter;
687
688         filter = rcu_dereference_protected(sk->sk_filter,
689                                            sock_owned_by_user(sk));
690         if (filter) {
691                 rcu_assign_pointer(sk->sk_filter, NULL);
692                 sk_filter_delayed_uncharge(sk, filter);
693                 ret = 0;
694         }
695         return ret;
696 }
697 EXPORT_SYMBOL_GPL(sk_detach_filter);