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. |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 16 | * Kris Katterjohn - Added many additional checks in sk_chk_filter() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Dmitry Mishin | 40daafc | 2006-04-18 14:50:10 -0700 | [diff] [blame] | 36 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/filter.h> |
| 38 | |
| 39 | /* No hurry in this branch */ |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 40 | static void *__load_pointer(struct sk_buff *skb, int k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | u8 *ptr = NULL; |
| 43 | |
| 44 | if (k >= SKF_NET_OFF) |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 45 | ptr = skb_network_header(skb) + k - SKF_NET_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | else if (k >= SKF_LL_OFF) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 47 | ptr = skb_mac_header(skb) + k - SKF_LL_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 49 | if (ptr >= skb->head && ptr < skb_tail_pointer(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return ptr; |
| 51 | return NULL; |
| 52 | } |
| 53 | |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 54 | static inline void *load_pointer(struct sk_buff *skb, int k, |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 55 | unsigned int size, void *buffer) |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 56 | { |
| 57 | if (k >= 0) |
| 58 | return skb_header_pointer(skb, k, size, buffer); |
| 59 | else { |
| 60 | if (k >= SKF_AD_OFF) |
| 61 | return NULL; |
| 62 | return __load_pointer(skb, k); |
| 63 | } |
| 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /** |
Kris Katterjohn | 2966b66 | 2006-01-23 16:26:16 -0800 | [diff] [blame] | 67 | * sk_run_filter - run a filter on a socket |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * @skb: buffer to run the filter on |
| 69 | * @filter: filter to apply |
| 70 | * @flen: length of filter |
| 71 | * |
| 72 | * Decode and apply filter instructions to the skb->data. |
| 73 | * Return length to keep, 0 for none. skb is the data we are |
| 74 | * filtering, filter is the array of filter instructions, and |
| 75 | * len is the number of filter blocks in the array. |
| 76 | */ |
Kris Katterjohn | 4bad4dc | 2006-01-06 13:08:20 -0800 | [diff] [blame] | 77 | unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Kris Katterjohn | 2966b66 | 2006-01-23 16:26:16 -0800 | [diff] [blame] | 81 | u32 A = 0; /* Accumulator */ |
| 82 | u32 X = 0; /* Index Register */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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]; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | A /= fentry->k; |
| 120 | continue; |
| 121 | case BPF_ALU|BPF_AND|BPF_X: |
| 122 | A &= X; |
| 123 | continue; |
| 124 | case BPF_ALU|BPF_AND|BPF_K: |
| 125 | A &= fentry->k; |
| 126 | continue; |
| 127 | case BPF_ALU|BPF_OR|BPF_X: |
| 128 | A |= X; |
| 129 | continue; |
| 130 | case BPF_ALU|BPF_OR|BPF_K: |
| 131 | A |= fentry->k; |
| 132 | continue; |
| 133 | case BPF_ALU|BPF_LSH|BPF_X: |
| 134 | A <<= X; |
| 135 | continue; |
| 136 | case BPF_ALU|BPF_LSH|BPF_K: |
| 137 | A <<= fentry->k; |
| 138 | continue; |
| 139 | case BPF_ALU|BPF_RSH|BPF_X: |
| 140 | A >>= X; |
| 141 | continue; |
| 142 | case BPF_ALU|BPF_RSH|BPF_K: |
| 143 | A >>= fentry->k; |
| 144 | continue; |
| 145 | case BPF_ALU|BPF_NEG: |
| 146 | A = -A; |
| 147 | continue; |
| 148 | case BPF_JMP|BPF_JA: |
| 149 | pc += fentry->k; |
| 150 | continue; |
| 151 | case BPF_JMP|BPF_JGT|BPF_K: |
| 152 | pc += (A > fentry->k) ? fentry->jt : fentry->jf; |
| 153 | continue; |
| 154 | case BPF_JMP|BPF_JGE|BPF_K: |
| 155 | pc += (A >= fentry->k) ? fentry->jt : fentry->jf; |
| 156 | continue; |
| 157 | case BPF_JMP|BPF_JEQ|BPF_K: |
| 158 | pc += (A == fentry->k) ? fentry->jt : fentry->jf; |
| 159 | continue; |
| 160 | case BPF_JMP|BPF_JSET|BPF_K: |
| 161 | pc += (A & fentry->k) ? fentry->jt : fentry->jf; |
| 162 | continue; |
| 163 | case BPF_JMP|BPF_JGT|BPF_X: |
| 164 | pc += (A > X) ? fentry->jt : fentry->jf; |
| 165 | continue; |
| 166 | case BPF_JMP|BPF_JGE|BPF_X: |
| 167 | pc += (A >= X) ? fentry->jt : fentry->jf; |
| 168 | continue; |
| 169 | case BPF_JMP|BPF_JEQ|BPF_X: |
| 170 | pc += (A == X) ? fentry->jt : fentry->jf; |
| 171 | continue; |
| 172 | case BPF_JMP|BPF_JSET|BPF_X: |
| 173 | pc += (A & X) ? fentry->jt : fentry->jf; |
| 174 | continue; |
| 175 | case BPF_LD|BPF_W|BPF_ABS: |
| 176 | k = fentry->k; |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 177 | load_w: |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 178 | ptr = load_pointer(skb, k, 4, &tmp); |
| 179 | if (ptr != NULL) { |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 180 | A = ntohl(get_unaligned((__be32 *)ptr)); |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 181 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
Herbert Xu | 1198ad0 | 2005-09-05 18:44:37 -0700 | [diff] [blame] | 183 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | case BPF_LD|BPF_H|BPF_ABS: |
| 185 | k = fentry->k; |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 186 | load_h: |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 187 | ptr = load_pointer(skb, k, 2, &tmp); |
| 188 | if (ptr != NULL) { |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 189 | A = ntohs(get_unaligned((__be16 *)ptr)); |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 190 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
Herbert Xu | 1198ad0 | 2005-09-05 18:44:37 -0700 | [diff] [blame] | 192 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | case BPF_LD|BPF_B|BPF_ABS: |
| 194 | k = fentry->k; |
| 195 | load_b: |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 196 | ptr = load_pointer(skb, k, 1, &tmp); |
| 197 | if (ptr != NULL) { |
| 198 | A = *(u8 *)ptr; |
| 199 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
Herbert Xu | 1198ad0 | 2005-09-05 18:44:37 -0700 | [diff] [blame] | 201 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | case BPF_LD|BPF_W|BPF_LEN: |
Patrick McHardy | 3154e54 | 2005-07-05 14:10:40 -0700 | [diff] [blame] | 203 | A = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | continue; |
| 205 | case BPF_LDX|BPF_W|BPF_LEN: |
Patrick McHardy | 3154e54 | 2005-07-05 14:10:40 -0700 | [diff] [blame] | 206 | X = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | continue; |
| 208 | case BPF_LD|BPF_W|BPF_IND: |
| 209 | k = X + fentry->k; |
| 210 | goto load_w; |
| 211 | case BPF_LD|BPF_H|BPF_IND: |
| 212 | k = X + fentry->k; |
| 213 | goto load_h; |
| 214 | case BPF_LD|BPF_B|BPF_IND: |
| 215 | k = X + fentry->k; |
| 216 | goto load_b; |
| 217 | case BPF_LDX|BPF_B|BPF_MSH: |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 218 | ptr = load_pointer(skb, fentry->k, 1, &tmp); |
| 219 | if (ptr != NULL) { |
| 220 | X = (*(u8 *)ptr & 0xf) << 2; |
| 221 | continue; |
| 222 | } |
| 223 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | case BPF_LD|BPF_IMM: |
| 225 | A = fentry->k; |
| 226 | continue; |
| 227 | case BPF_LDX|BPF_IMM: |
| 228 | X = fentry->k; |
| 229 | continue; |
| 230 | case BPF_LD|BPF_MEM: |
| 231 | A = mem[fentry->k]; |
| 232 | continue; |
| 233 | case BPF_LDX|BPF_MEM: |
| 234 | X = mem[fentry->k]; |
| 235 | continue; |
| 236 | case BPF_MISC|BPF_TAX: |
| 237 | X = A; |
| 238 | continue; |
| 239 | case BPF_MISC|BPF_TXA: |
| 240 | A = X; |
| 241 | continue; |
| 242 | case BPF_RET|BPF_K: |
Kris Katterjohn | 4bad4dc | 2006-01-06 13:08:20 -0800 | [diff] [blame] | 243 | return fentry->k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | case BPF_RET|BPF_A: |
Kris Katterjohn | 4bad4dc | 2006-01-06 13:08:20 -0800 | [diff] [blame] | 245 | return A; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | case BPF_ST: |
| 247 | mem[fentry->k] = A; |
| 248 | continue; |
| 249 | case BPF_STX: |
| 250 | mem[fentry->k] = X; |
| 251 | continue; |
| 252 | default: |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 253 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Handle ancillary data, which are impossible |
| 259 | * (or very difficult) to get parsing packet contents. |
| 260 | */ |
| 261 | switch (k-SKF_AD_OFF) { |
| 262 | case SKF_AD_PROTOCOL: |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 263 | A = ntohs(skb->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | continue; |
| 265 | case SKF_AD_PKTTYPE: |
| 266 | A = skb->pkt_type; |
| 267 | continue; |
| 268 | case SKF_AD_IFINDEX: |
| 269 | A = skb->dev->ifindex; |
| 270 | continue; |
| 271 | default: |
| 272 | return 0; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * sk_chk_filter - verify socket filter code |
| 281 | * @filter: filter to verify |
| 282 | * @flen: length of filter |
| 283 | * |
| 284 | * Check the user's filter code. If we let some ugly |
| 285 | * filter code slip through kaboom! The filter must contain |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 286 | * no references or jumps that are out of range, no illegal |
| 287 | * instructions, and must end with a RET instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | * |
Kris Katterjohn | 7b11f69 | 2006-01-13 14:33:06 -0800 | [diff] [blame] | 289 | * All jumps are forward as they are not signed. |
| 290 | * |
| 291 | * Returns 0 if the rule set is legal or -EINVAL if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | */ |
| 293 | int sk_chk_filter(struct sock_filter *filter, int flen) |
| 294 | { |
| 295 | struct sock_filter *ftest; |
| 296 | int pc; |
| 297 | |
David S. Miller | 1b93ae64 | 2005-12-27 13:57:59 -0800 | [diff] [blame] | 298 | if (flen == 0 || flen > BPF_MAXINSNS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return -EINVAL; |
| 300 | |
| 301 | /* check the filter code now */ |
| 302 | for (pc = 0; pc < flen; pc++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | ftest = &filter[pc]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 305 | /* Only allow valid instructions */ |
| 306 | switch (ftest->code) { |
| 307 | case BPF_ALU|BPF_ADD|BPF_K: |
| 308 | case BPF_ALU|BPF_ADD|BPF_X: |
| 309 | case BPF_ALU|BPF_SUB|BPF_K: |
| 310 | case BPF_ALU|BPF_SUB|BPF_X: |
| 311 | case BPF_ALU|BPF_MUL|BPF_K: |
| 312 | case BPF_ALU|BPF_MUL|BPF_X: |
| 313 | case BPF_ALU|BPF_DIV|BPF_X: |
| 314 | case BPF_ALU|BPF_AND|BPF_K: |
| 315 | case BPF_ALU|BPF_AND|BPF_X: |
| 316 | case BPF_ALU|BPF_OR|BPF_K: |
| 317 | case BPF_ALU|BPF_OR|BPF_X: |
| 318 | case BPF_ALU|BPF_LSH|BPF_K: |
| 319 | case BPF_ALU|BPF_LSH|BPF_X: |
| 320 | case BPF_ALU|BPF_RSH|BPF_K: |
| 321 | case BPF_ALU|BPF_RSH|BPF_X: |
| 322 | case BPF_ALU|BPF_NEG: |
| 323 | case BPF_LD|BPF_W|BPF_ABS: |
| 324 | case BPF_LD|BPF_H|BPF_ABS: |
| 325 | case BPF_LD|BPF_B|BPF_ABS: |
| 326 | case BPF_LD|BPF_W|BPF_LEN: |
| 327 | case BPF_LD|BPF_W|BPF_IND: |
| 328 | case BPF_LD|BPF_H|BPF_IND: |
| 329 | case BPF_LD|BPF_B|BPF_IND: |
| 330 | case BPF_LD|BPF_IMM: |
| 331 | case BPF_LDX|BPF_W|BPF_LEN: |
| 332 | case BPF_LDX|BPF_B|BPF_MSH: |
| 333 | case BPF_LDX|BPF_IMM: |
| 334 | case BPF_MISC|BPF_TAX: |
| 335 | case BPF_MISC|BPF_TXA: |
| 336 | case BPF_RET|BPF_K: |
| 337 | case BPF_RET|BPF_A: |
| 338 | break; |
Kris Katterjohn | fb0d366 | 2005-11-20 13:41:34 -0800 | [diff] [blame] | 339 | |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 340 | /* Some instructions need special checks */ |
| 341 | |
| 342 | case BPF_ALU|BPF_DIV|BPF_K: |
| 343 | /* check for division by zero */ |
| 344 | if (ftest->k == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return -EINVAL; |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 346 | break; |
| 347 | |
| 348 | case BPF_LD|BPF_MEM: |
| 349 | case BPF_LDX|BPF_MEM: |
| 350 | case BPF_ST: |
| 351 | case BPF_STX: |
| 352 | /* check for invalid memory addresses */ |
| 353 | if (ftest->k >= BPF_MEMWORDS) |
| 354 | return -EINVAL; |
| 355 | break; |
| 356 | |
| 357 | case BPF_JMP|BPF_JA: |
| 358 | /* |
| 359 | * Note, the large ftest->k might cause loops. |
| 360 | * Compare this with conditional jumps below, |
| 361 | * where offsets are limited. --ANK (981016) |
| 362 | */ |
| 363 | if (ftest->k >= (unsigned)(flen-pc-1)) |
| 364 | return -EINVAL; |
| 365 | break; |
| 366 | |
| 367 | case BPF_JMP|BPF_JEQ|BPF_K: |
| 368 | case BPF_JMP|BPF_JEQ|BPF_X: |
| 369 | case BPF_JMP|BPF_JGE|BPF_K: |
| 370 | case BPF_JMP|BPF_JGE|BPF_X: |
| 371 | case BPF_JMP|BPF_JGT|BPF_K: |
| 372 | case BPF_JMP|BPF_JGT|BPF_X: |
| 373 | case BPF_JMP|BPF_JSET|BPF_K: |
| 374 | case BPF_JMP|BPF_JSET|BPF_X: |
| 375 | /* for conditionals both must be safe */ |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 376 | if (pc + ftest->jt + 1 >= flen || |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 377 | pc + ftest->jf + 1 >= flen) |
| 378 | return -EINVAL; |
| 379 | break; |
| 380 | |
| 381 | default: |
| 382 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 386 | return (BPF_CLASS(filter[flen - 1].code) == BPF_RET) ? 0 : -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
| 390 | * sk_attach_filter - attach a socket filter |
| 391 | * @fprog: the filter program |
| 392 | * @sk: the socket to use |
| 393 | * |
| 394 | * Attach the user's filter code. We first run some sanity checks on |
| 395 | * it to make sure it does not explode on us later. If an error |
| 396 | * occurs or there is insufficient memory for the filter a negative |
| 397 | * errno code is returned. On success the return is zero. |
| 398 | */ |
| 399 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
| 400 | { |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 401 | struct sk_filter *fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | unsigned int fsize = sizeof(struct sock_filter) * fprog->len; |
| 403 | int err; |
| 404 | |
| 405 | /* Make sure new filter is there and in the right amounts. */ |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 406 | if (fprog->filter == NULL) |
| 407 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL); |
| 410 | if (!fp) |
| 411 | return -ENOMEM; |
| 412 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 413 | sock_kfree_s(sk, fp, fsize+sizeof(*fp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | return -EFAULT; |
| 415 | } |
| 416 | |
| 417 | atomic_set(&fp->refcnt, 1); |
| 418 | fp->len = fprog->len; |
| 419 | |
| 420 | err = sk_chk_filter(fp->insns, fp->len); |
| 421 | if (!err) { |
| 422 | struct sk_filter *old_fp; |
| 423 | |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 424 | rcu_read_lock_bh(); |
| 425 | old_fp = rcu_dereference(sk->sk_filter); |
| 426 | rcu_assign_pointer(sk->sk_filter, fp); |
| 427 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | fp = old_fp; |
| 429 | } |
| 430 | |
| 431 | if (fp) |
| 432 | sk_filter_release(sk, fp); |
| 433 | return err; |
| 434 | } |
| 435 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame^] | 436 | int sk_detach_filter(struct sock *sk) |
| 437 | { |
| 438 | int ret = -ENOENT; |
| 439 | struct sk_filter *filter; |
| 440 | |
| 441 | rcu_read_lock_bh(); |
| 442 | filter = rcu_dereference(sk->sk_filter); |
| 443 | if (filter) { |
| 444 | rcu_assign_pointer(sk->sk_filter, NULL); |
| 445 | sk_filter_release(sk, filter); |
| 446 | ret = 0; |
| 447 | } |
| 448 | rcu_read_unlock_bh(); |
| 449 | return ret; |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | EXPORT_SYMBOL(sk_chk_filter); |
| 453 | EXPORT_SYMBOL(sk_run_filter); |