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