]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | */ | |
17 | ||
18 | #include <linux/module.h> | |
19 | #include <linux/types.h> | |
20 | #include <linux/sched.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 <net/ip.h> | |
29 | #include <net/protocol.h> | |
30 | #include <linux/skbuff.h> | |
31 | #include <net/sock.h> | |
32 | #include <linux/errno.h> | |
33 | #include <linux/timer.h> | |
34 | #include <asm/system.h> | |
35 | #include <asm/uaccess.h> | |
36 | #include <linux/filter.h> | |
37 | ||
38 | /* No hurry in this branch */ | |
39 | static u8 *load_pointer(struct sk_buff *skb, int k) | |
40 | { | |
41 | u8 *ptr = NULL; | |
42 | ||
43 | if (k >= SKF_NET_OFF) | |
44 | ptr = skb->nh.raw + k - SKF_NET_OFF; | |
45 | else if (k >= SKF_LL_OFF) | |
46 | ptr = skb->mac.raw + k - SKF_LL_OFF; | |
47 | ||
48 | if (ptr >= skb->head && ptr < skb->tail) | |
49 | return ptr; | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | /** | |
54 | * sk_run_filter - run a filter on a socket | |
55 | * @skb: buffer to run the filter on | |
56 | * @filter: filter to apply | |
57 | * @flen: length of filter | |
58 | * | |
59 | * Decode and apply filter instructions to the skb->data. | |
60 | * Return length to keep, 0 for none. skb is the data we are | |
61 | * filtering, filter is the array of filter instructions, and | |
62 | * len is the number of filter blocks in the array. | |
63 | */ | |
64 | ||
65 | int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen) | |
66 | { | |
67 | unsigned char *data = skb->data; | |
68 | /* len is UNSIGNED. Byte wide insns relies only on implicit | |
69 | type casts to prevent reading arbitrary memory locations. | |
70 | */ | |
71 | unsigned int len = skb->len-skb->data_len; | |
72 | struct sock_filter *fentry; /* We walk down these */ | |
73 | u32 A = 0; /* Accumulator */ | |
74 | u32 X = 0; /* Index Register */ | |
75 | u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */ | |
76 | int k; | |
77 | int pc; | |
78 | ||
79 | /* | |
80 | * Process array of filter instructions. | |
81 | */ | |
82 | for (pc = 0; pc < flen; pc++) { | |
83 | fentry = &filter[pc]; | |
84 | ||
85 | switch (fentry->code) { | |
86 | case BPF_ALU|BPF_ADD|BPF_X: | |
87 | A += X; | |
88 | continue; | |
89 | case BPF_ALU|BPF_ADD|BPF_K: | |
90 | A += fentry->k; | |
91 | continue; | |
92 | case BPF_ALU|BPF_SUB|BPF_X: | |
93 | A -= X; | |
94 | continue; | |
95 | case BPF_ALU|BPF_SUB|BPF_K: | |
96 | A -= fentry->k; | |
97 | continue; | |
98 | case BPF_ALU|BPF_MUL|BPF_X: | |
99 | A *= X; | |
100 | continue; | |
101 | case BPF_ALU|BPF_MUL|BPF_K: | |
102 | A *= fentry->k; | |
103 | continue; | |
104 | case BPF_ALU|BPF_DIV|BPF_X: | |
105 | if (X == 0) | |
106 | return 0; | |
107 | A /= X; | |
108 | continue; | |
109 | case BPF_ALU|BPF_DIV|BPF_K: | |
110 | if (fentry->k == 0) | |
111 | return 0; | |
112 | A /= fentry->k; | |
113 | continue; | |
114 | case BPF_ALU|BPF_AND|BPF_X: | |
115 | A &= X; | |
116 | continue; | |
117 | case BPF_ALU|BPF_AND|BPF_K: | |
118 | A &= fentry->k; | |
119 | continue; | |
120 | case BPF_ALU|BPF_OR|BPF_X: | |
121 | A |= X; | |
122 | continue; | |
123 | case BPF_ALU|BPF_OR|BPF_K: | |
124 | A |= fentry->k; | |
125 | continue; | |
126 | case BPF_ALU|BPF_LSH|BPF_X: | |
127 | A <<= X; | |
128 | continue; | |
129 | case BPF_ALU|BPF_LSH|BPF_K: | |
130 | A <<= fentry->k; | |
131 | continue; | |
132 | case BPF_ALU|BPF_RSH|BPF_X: | |
133 | A >>= X; | |
134 | continue; | |
135 | case BPF_ALU|BPF_RSH|BPF_K: | |
136 | A >>= fentry->k; | |
137 | continue; | |
138 | case BPF_ALU|BPF_NEG: | |
139 | A = -A; | |
140 | continue; | |
141 | case BPF_JMP|BPF_JA: | |
142 | pc += fentry->k; | |
143 | continue; | |
144 | case BPF_JMP|BPF_JGT|BPF_K: | |
145 | pc += (A > fentry->k) ? fentry->jt : fentry->jf; | |
146 | continue; | |
147 | case BPF_JMP|BPF_JGE|BPF_K: | |
148 | pc += (A >= fentry->k) ? fentry->jt : fentry->jf; | |
149 | continue; | |
150 | case BPF_JMP|BPF_JEQ|BPF_K: | |
151 | pc += (A == fentry->k) ? fentry->jt : fentry->jf; | |
152 | continue; | |
153 | case BPF_JMP|BPF_JSET|BPF_K: | |
154 | pc += (A & fentry->k) ? fentry->jt : fentry->jf; | |
155 | continue; | |
156 | case BPF_JMP|BPF_JGT|BPF_X: | |
157 | pc += (A > X) ? fentry->jt : fentry->jf; | |
158 | continue; | |
159 | case BPF_JMP|BPF_JGE|BPF_X: | |
160 | pc += (A >= X) ? fentry->jt : fentry->jf; | |
161 | continue; | |
162 | case BPF_JMP|BPF_JEQ|BPF_X: | |
163 | pc += (A == X) ? fentry->jt : fentry->jf; | |
164 | continue; | |
165 | case BPF_JMP|BPF_JSET|BPF_X: | |
166 | pc += (A & X) ? fentry->jt : fentry->jf; | |
167 | continue; | |
168 | case BPF_LD|BPF_W|BPF_ABS: | |
169 | k = fentry->k; | |
170 | load_w: | |
171 | if (k >= 0 && (unsigned int)(k+sizeof(u32)) <= len) { | |
172 | A = ntohl(*(u32*)&data[k]); | |
173 | continue; | |
174 | } | |
175 | if (k < 0) { | |
176 | u8 *ptr; | |
177 | ||
178 | if (k >= SKF_AD_OFF) | |
179 | break; | |
180 | ptr = load_pointer(skb, k); | |
181 | if (ptr) { | |
182 | A = ntohl(*(u32*)ptr); | |
183 | continue; | |
184 | } | |
185 | } else { | |
186 | u32 _tmp, *p; | |
187 | p = skb_header_pointer(skb, k, 4, &_tmp); | |
188 | if (p != NULL) { | |
189 | A = ntohl(*p); | |
190 | continue; | |
191 | } | |
192 | } | |
193 | return 0; | |
194 | case BPF_LD|BPF_H|BPF_ABS: | |
195 | k = fentry->k; | |
196 | load_h: | |
197 | if (k >= 0 && (unsigned int)(k + sizeof(u16)) <= len) { | |
198 | A = ntohs(*(u16*)&data[k]); | |
199 | continue; | |
200 | } | |
201 | if (k < 0) { | |
202 | u8 *ptr; | |
203 | ||
204 | if (k >= SKF_AD_OFF) | |
205 | break; | |
206 | ptr = load_pointer(skb, k); | |
207 | if (ptr) { | |
208 | A = ntohs(*(u16*)ptr); | |
209 | continue; | |
210 | } | |
211 | } else { | |
212 | u16 _tmp, *p; | |
213 | p = skb_header_pointer(skb, k, 2, &_tmp); | |
214 | if (p != NULL) { | |
215 | A = ntohs(*p); | |
216 | continue; | |
217 | } | |
218 | } | |
219 | return 0; | |
220 | case BPF_LD|BPF_B|BPF_ABS: | |
221 | k = fentry->k; | |
222 | load_b: | |
223 | if (k >= 0 && (unsigned int)k < len) { | |
224 | A = data[k]; | |
225 | continue; | |
226 | } | |
227 | if (k < 0) { | |
228 | u8 *ptr; | |
229 | ||
230 | if (k >= SKF_AD_OFF) | |
231 | break; | |
232 | ptr = load_pointer(skb, k); | |
233 | if (ptr) { | |
234 | A = *ptr; | |
235 | continue; | |
236 | } | |
237 | } else { | |
238 | u8 _tmp, *p; | |
239 | p = skb_header_pointer(skb, k, 1, &_tmp); | |
240 | if (p != NULL) { | |
241 | A = *p; | |
242 | continue; | |
243 | } | |
244 | } | |
245 | return 0; | |
246 | case BPF_LD|BPF_W|BPF_LEN: | |
247 | A = len; | |
248 | continue; | |
249 | case BPF_LDX|BPF_W|BPF_LEN: | |
250 | X = len; | |
251 | continue; | |
252 | case BPF_LD|BPF_W|BPF_IND: | |
253 | k = X + fentry->k; | |
254 | goto load_w; | |
255 | case BPF_LD|BPF_H|BPF_IND: | |
256 | k = X + fentry->k; | |
257 | goto load_h; | |
258 | case BPF_LD|BPF_B|BPF_IND: | |
259 | k = X + fentry->k; | |
260 | goto load_b; | |
261 | case BPF_LDX|BPF_B|BPF_MSH: | |
262 | if (fentry->k >= len) | |
263 | return 0; | |
264 | X = (data[fentry->k] & 0xf) << 2; | |
265 | continue; | |
266 | case BPF_LD|BPF_IMM: | |
267 | A = fentry->k; | |
268 | continue; | |
269 | case BPF_LDX|BPF_IMM: | |
270 | X = fentry->k; | |
271 | continue; | |
272 | case BPF_LD|BPF_MEM: | |
273 | A = mem[fentry->k]; | |
274 | continue; | |
275 | case BPF_LDX|BPF_MEM: | |
276 | X = mem[fentry->k]; | |
277 | continue; | |
278 | case BPF_MISC|BPF_TAX: | |
279 | X = A; | |
280 | continue; | |
281 | case BPF_MISC|BPF_TXA: | |
282 | A = X; | |
283 | continue; | |
284 | case BPF_RET|BPF_K: | |
285 | return ((unsigned int)fentry->k); | |
286 | case BPF_RET|BPF_A: | |
287 | return ((unsigned int)A); | |
288 | case BPF_ST: | |
289 | mem[fentry->k] = A; | |
290 | continue; | |
291 | case BPF_STX: | |
292 | mem[fentry->k] = X; | |
293 | continue; | |
294 | default: | |
295 | /* Invalid instruction counts as RET */ | |
296 | return 0; | |
297 | } | |
298 | ||
299 | /* | |
300 | * Handle ancillary data, which are impossible | |
301 | * (or very difficult) to get parsing packet contents. | |
302 | */ | |
303 | switch (k-SKF_AD_OFF) { | |
304 | case SKF_AD_PROTOCOL: | |
305 | A = htons(skb->protocol); | |
306 | continue; | |
307 | case SKF_AD_PKTTYPE: | |
308 | A = skb->pkt_type; | |
309 | continue; | |
310 | case SKF_AD_IFINDEX: | |
311 | A = skb->dev->ifindex; | |
312 | continue; | |
313 | default: | |
314 | return 0; | |
315 | } | |
316 | } | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | /** | |
322 | * sk_chk_filter - verify socket filter code | |
323 | * @filter: filter to verify | |
324 | * @flen: length of filter | |
325 | * | |
326 | * Check the user's filter code. If we let some ugly | |
327 | * filter code slip through kaboom! The filter must contain | |
328 | * no references or jumps that are out of range, no illegal instructions | |
329 | * and no backward jumps. It must end with a RET instruction | |
330 | * | |
331 | * Returns 0 if the rule set is legal or a negative errno code if not. | |
332 | */ | |
333 | int sk_chk_filter(struct sock_filter *filter, int flen) | |
334 | { | |
335 | struct sock_filter *ftest; | |
336 | int pc; | |
337 | ||
338 | if (((unsigned int)flen >= (~0U / sizeof(struct sock_filter))) || flen == 0) | |
339 | return -EINVAL; | |
340 | ||
341 | /* check the filter code now */ | |
342 | for (pc = 0; pc < flen; pc++) { | |
343 | /* all jumps are forward as they are not signed */ | |
344 | ftest = &filter[pc]; | |
345 | if (BPF_CLASS(ftest->code) == BPF_JMP) { | |
346 | /* but they mustn't jump off the end */ | |
347 | if (BPF_OP(ftest->code) == BPF_JA) { | |
348 | /* | |
349 | * Note, the large ftest->k might cause loops. | |
350 | * Compare this with conditional jumps below, | |
351 | * where offsets are limited. --ANK (981016) | |
352 | */ | |
353 | if (ftest->k >= (unsigned)(flen-pc-1)) | |
354 | return -EINVAL; | |
355 | } else { | |
356 | /* for conditionals both must be safe */ | |
357 | if (pc + ftest->jt +1 >= flen || | |
358 | pc + ftest->jf +1 >= flen) | |
359 | return -EINVAL; | |
360 | } | |
361 | } | |
362 | ||
363 | /* check that memory operations use valid addresses. */ | |
364 | if (ftest->k >= BPF_MEMWORDS) { | |
365 | /* but it might not be a memory operation... */ | |
366 | switch (ftest->code) { | |
367 | case BPF_ST: | |
368 | case BPF_STX: | |
369 | case BPF_LD|BPF_MEM: | |
370 | case BPF_LDX|BPF_MEM: | |
371 | return -EINVAL; | |
372 | } | |
373 | } | |
374 | } | |
375 | ||
376 | /* | |
377 | * The program must end with a return. We don't care where they | |
378 | * jumped within the script (its always forwards) but in the end | |
379 | * they _will_ hit this. | |
380 | */ | |
381 | return (BPF_CLASS(filter[flen - 1].code) == BPF_RET) ? 0 : -EINVAL; | |
382 | } | |
383 | ||
384 | /** | |
385 | * sk_attach_filter - attach a socket filter | |
386 | * @fprog: the filter program | |
387 | * @sk: the socket to use | |
388 | * | |
389 | * Attach the user's filter code. We first run some sanity checks on | |
390 | * it to make sure it does not explode on us later. If an error | |
391 | * occurs or there is insufficient memory for the filter a negative | |
392 | * errno code is returned. On success the return is zero. | |
393 | */ | |
394 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) | |
395 | { | |
396 | struct sk_filter *fp; | |
397 | unsigned int fsize = sizeof(struct sock_filter) * fprog->len; | |
398 | int err; | |
399 | ||
400 | /* Make sure new filter is there and in the right amounts. */ | |
401 | if (fprog->filter == NULL || fprog->len > BPF_MAXINSNS) | |
402 | return -EINVAL; | |
403 | ||
404 | fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL); | |
405 | if (!fp) | |
406 | return -ENOMEM; | |
407 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { | |
408 | sock_kfree_s(sk, fp, fsize+sizeof(*fp)); | |
409 | return -EFAULT; | |
410 | } | |
411 | ||
412 | atomic_set(&fp->refcnt, 1); | |
413 | fp->len = fprog->len; | |
414 | ||
415 | err = sk_chk_filter(fp->insns, fp->len); | |
416 | if (!err) { | |
417 | struct sk_filter *old_fp; | |
418 | ||
419 | spin_lock_bh(&sk->sk_lock.slock); | |
420 | old_fp = sk->sk_filter; | |
421 | sk->sk_filter = fp; | |
422 | spin_unlock_bh(&sk->sk_lock.slock); | |
423 | fp = old_fp; | |
424 | } | |
425 | ||
426 | if (fp) | |
427 | sk_filter_release(sk, fp); | |
428 | return err; | |
429 | } | |
430 | ||
431 | EXPORT_SYMBOL(sk_chk_filter); | |
432 | EXPORT_SYMBOL(sk_run_filter); |