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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/ip.h> |
| 30 | #include <net/protocol.h> |
Patrick McHardy | 4738c1d | 2008-04-10 02:02:28 -0700 | [diff] [blame] | 31 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/skbuff.h> |
| 33 | #include <net/sock.h> |
| 34 | #include <linux/errno.h> |
| 35 | #include <linux/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/uaccess.h> |
Dmitry Mishin | 40daafc | 2006-04-18 14:50:10 -0700 | [diff] [blame] | 37 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/filter.h> |
Eric Dumazet | c26aed4 | 2010-11-18 22:04:46 +0000 | [diff] [blame] | 39 | #include <linux/reciprocal_div.h> |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 40 | #include <linux/ratelimit.h> |
Will Drewry | 46b325c | 2012-04-12 16:47:52 -0500 | [diff] [blame] | 41 | #include <linux/seccomp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Jan Seiffert | f03fb3f | 2012-03-30 05:08:19 +0000 | [diff] [blame] | 43 | /* No hurry in this branch |
| 44 | * |
| 45 | * Exported for the bpf jit load helper. |
| 46 | */ |
| 47 | void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | u8 *ptr = NULL; |
| 50 | |
| 51 | if (k >= SKF_NET_OFF) |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 52 | ptr = skb_network_header(skb) + k - SKF_NET_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | else if (k >= SKF_LL_OFF) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 54 | ptr = skb_mac_header(skb) + k - SKF_LL_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Eric Dumazet | 4bc65dd | 2010-12-07 22:26:15 +0000 | [diff] [blame] | 56 | if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return ptr; |
| 58 | return NULL; |
| 59 | } |
| 60 | |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 61 | static inline void *load_pointer(const struct sk_buff *skb, int k, |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 62 | unsigned int size, void *buffer) |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 63 | { |
| 64 | if (k >= 0) |
| 65 | return skb_header_pointer(skb, k, size, buffer); |
Jan Seiffert | f03fb3f | 2012-03-30 05:08:19 +0000 | [diff] [blame] | 66 | return bpf_internal_load_pointer_neg_helper(skb, k, size); |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | /** |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 70 | * sk_filter - run a packet through a socket filter |
| 71 | * @sk: sock associated with &sk_buff |
| 72 | * @skb: buffer to filter |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 73 | * |
| 74 | * Run the filter code and then cut skb->data to correct size returned by |
| 75 | * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller |
| 76 | * than pkt_len we keep whole skb->data. This is the socket level |
| 77 | * wrapper to sk_run_filter. It returns 0 if the packet should |
| 78 | * be accepted or -EPERM if the packet should be tossed. |
| 79 | * |
| 80 | */ |
| 81 | int sk_filter(struct sock *sk, struct sk_buff *skb) |
| 82 | { |
| 83 | int err; |
| 84 | struct sk_filter *filter; |
| 85 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 86 | /* |
| 87 | * If the skb was allocated from pfmemalloc reserves, only |
| 88 | * allow SOCK_MEMALLOC sockets to use it as this socket is |
| 89 | * helping free memory |
| 90 | */ |
| 91 | if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) |
| 92 | return -ENOMEM; |
| 93 | |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 94 | err = security_sock_rcv_skb(sk, skb); |
| 95 | if (err) |
| 96 | return err; |
| 97 | |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 98 | rcu_read_lock(); |
| 99 | filter = rcu_dereference(sk->sk_filter); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 100 | if (filter) { |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 101 | unsigned int pkt_len = SK_RUN_FILTER(filter, skb); |
Eric Dumazet | 0d7da9d | 2010-10-25 03:47:05 +0000 | [diff] [blame] | 102 | |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 103 | err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM; |
| 104 | } |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 105 | rcu_read_unlock(); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 106 | |
| 107 | return err; |
| 108 | } |
| 109 | EXPORT_SYMBOL(sk_filter); |
| 110 | |
| 111 | /** |
Kris Katterjohn | 2966b66 | 2006-01-23 16:26:16 -0800 | [diff] [blame] | 112 | * sk_run_filter - run a filter on a socket |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * @skb: buffer to run the filter on |
Randy Dunlap | 697d0e3 | 2011-01-08 17:41:42 +0000 | [diff] [blame] | 114 | * @fentry: filter to apply |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * |
| 116 | * Decode and apply filter instructions to the skb->data. |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 117 | * Return length to keep, 0 for none. @skb is the data we are |
| 118 | * filtering, @filter is the array of filter instructions. |
| 119 | * Because all jumps are guaranteed to be before last instruction, |
| 120 | * and last instruction guaranteed to be a RET, we dont need to check |
| 121 | * flen. (We used to pass to this function the length of filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | */ |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 123 | unsigned int sk_run_filter(const struct sk_buff *skb, |
| 124 | const struct sock_filter *fentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 126 | void *ptr; |
Kris Katterjohn | 2966b66 | 2006-01-23 16:26:16 -0800 | [diff] [blame] | 127 | u32 A = 0; /* Accumulator */ |
| 128 | u32 X = 0; /* Index Register */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */ |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 130 | u32 tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | int k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Process array of filter instructions. |
| 135 | */ |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 136 | for (;; fentry++) { |
| 137 | #if defined(CONFIG_X86_32) |
| 138 | #define K (fentry->k) |
| 139 | #else |
| 140 | const u32 K = fentry->k; |
| 141 | #endif |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | switch (fentry->code) { |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 144 | case BPF_S_ALU_ADD_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | A += X; |
| 146 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 147 | case BPF_S_ALU_ADD_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 148 | A += K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 150 | case BPF_S_ALU_SUB_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | A -= X; |
| 152 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 153 | case BPF_S_ALU_SUB_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 154 | A -= K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 156 | case BPF_S_ALU_MUL_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | A *= X; |
| 158 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 159 | case BPF_S_ALU_MUL_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 160 | A *= K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 162 | case BPF_S_ALU_DIV_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | if (X == 0) |
| 164 | return 0; |
| 165 | A /= X; |
| 166 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 167 | case BPF_S_ALU_DIV_K: |
Eric Dumazet | c26aed4 | 2010-11-18 22:04:46 +0000 | [diff] [blame] | 168 | A = reciprocal_divide(A, K); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | continue; |
Eric Dumazet | b6069a9 | 2012-09-07 22:03:35 +0000 | [diff] [blame^] | 170 | case BPF_S_ALU_MOD_X: |
| 171 | if (X == 0) |
| 172 | return 0; |
| 173 | A %= X; |
| 174 | continue; |
| 175 | case BPF_S_ALU_MOD_K: |
| 176 | A %= K; |
| 177 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 178 | case BPF_S_ALU_AND_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | A &= X; |
| 180 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 181 | case BPF_S_ALU_AND_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 182 | A &= K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 184 | case BPF_S_ALU_OR_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | A |= X; |
| 186 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 187 | case BPF_S_ALU_OR_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 188 | A |= K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 190 | case BPF_S_ALU_LSH_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | A <<= X; |
| 192 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 193 | case BPF_S_ALU_LSH_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 194 | A <<= K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 196 | case BPF_S_ALU_RSH_X: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | A >>= X; |
| 198 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 199 | case BPF_S_ALU_RSH_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 200 | A >>= K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 202 | case BPF_S_ALU_NEG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | A = -A; |
| 204 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 205 | case BPF_S_JMP_JA: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 206 | fentry += K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 208 | case BPF_S_JMP_JGT_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 209 | fentry += (A > K) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 211 | case BPF_S_JMP_JGE_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 212 | fentry += (A >= K) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 214 | case BPF_S_JMP_JEQ_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 215 | fentry += (A == K) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 217 | case BPF_S_JMP_JSET_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 218 | fentry += (A & K) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 220 | case BPF_S_JMP_JGT_X: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 221 | fentry += (A > X) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 223 | case BPF_S_JMP_JGE_X: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 224 | fentry += (A >= X) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 226 | case BPF_S_JMP_JEQ_X: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 227 | fentry += (A == X) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 229 | case BPF_S_JMP_JSET_X: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 230 | fentry += (A & X) ? fentry->jt : fentry->jf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 232 | case BPF_S_LD_W_ABS: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 233 | k = K; |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 234 | load_w: |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 235 | ptr = load_pointer(skb, k, 4, &tmp); |
| 236 | if (ptr != NULL) { |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 237 | A = get_unaligned_be32(ptr); |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 238 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 240 | return 0; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 241 | case BPF_S_LD_H_ABS: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 242 | k = K; |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 243 | load_h: |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 244 | ptr = load_pointer(skb, k, 2, &tmp); |
| 245 | if (ptr != NULL) { |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 246 | A = get_unaligned_be16(ptr); |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 247 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 249 | return 0; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 250 | case BPF_S_LD_B_ABS: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 251 | k = K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | load_b: |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 253 | ptr = load_pointer(skb, k, 1, &tmp); |
| 254 | if (ptr != NULL) { |
| 255 | A = *(u8 *)ptr; |
| 256 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 258 | return 0; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 259 | case BPF_S_LD_W_LEN: |
Patrick McHardy | 3154e54 | 2005-07-05 14:10:40 -0700 | [diff] [blame] | 260 | A = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 262 | case BPF_S_LDX_W_LEN: |
Patrick McHardy | 3154e54 | 2005-07-05 14:10:40 -0700 | [diff] [blame] | 263 | X = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 265 | case BPF_S_LD_W_IND: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 266 | k = X + K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | goto load_w; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 268 | case BPF_S_LD_H_IND: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 269 | k = X + K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | goto load_h; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 271 | case BPF_S_LD_B_IND: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 272 | k = X + K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | goto load_b; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 274 | case BPF_S_LDX_B_MSH: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 275 | ptr = load_pointer(skb, K, 1, &tmp); |
Patrick McHardy | 0b05b2a | 2005-07-05 14:10:21 -0700 | [diff] [blame] | 276 | if (ptr != NULL) { |
| 277 | X = (*(u8 *)ptr & 0xf) << 2; |
| 278 | continue; |
| 279 | } |
| 280 | return 0; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 281 | case BPF_S_LD_IMM: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 282 | A = K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 284 | case BPF_S_LDX_IMM: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 285 | X = K; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 287 | case BPF_S_LD_MEM: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 288 | A = mem[K]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 290 | case BPF_S_LDX_MEM: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 291 | X = mem[K]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 293 | case BPF_S_MISC_TAX: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | X = A; |
| 295 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 296 | case BPF_S_MISC_TXA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | A = X; |
| 298 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 299 | case BPF_S_RET_K: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 300 | return K; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 301 | case BPF_S_RET_A: |
Kris Katterjohn | 4bad4dc | 2006-01-06 13:08:20 -0800 | [diff] [blame] | 302 | return A; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 303 | case BPF_S_ST: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 304 | mem[K] = A; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | continue; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 306 | case BPF_S_STX: |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 307 | mem[K] = X; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 309 | case BPF_S_ANC_PROTOCOL: |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 310 | A = ntohs(skb->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 312 | case BPF_S_ANC_PKTTYPE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | A = skb->pkt_type; |
| 314 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 315 | case BPF_S_ANC_IFINDEX: |
Paul LeoNerd Evans | 40eaf96 | 2010-04-22 03:32:22 +0000 | [diff] [blame] | 316 | if (!skb->dev) |
| 317 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | A = skb->dev->ifindex; |
| 319 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 320 | case BPF_S_ANC_MARK: |
jamal | 7e75f93 | 2009-10-19 02:17:56 +0000 | [diff] [blame] | 321 | A = skb->mark; |
| 322 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 323 | case BPF_S_ANC_QUEUE: |
Eric Dumazet | d19742f | 2009-10-20 01:06:22 -0700 | [diff] [blame] | 324 | A = skb->queue_mapping; |
| 325 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 326 | case BPF_S_ANC_HATYPE: |
Paul LeoNerd Evans | 40eaf96 | 2010-04-22 03:32:22 +0000 | [diff] [blame] | 327 | if (!skb->dev) |
| 328 | return 0; |
| 329 | A = skb->dev->type; |
| 330 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 331 | case BPF_S_ANC_RXHASH: |
Eric Dumazet | da2033c | 2010-11-30 21:45:56 +0000 | [diff] [blame] | 332 | A = skb->rxhash; |
| 333 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 334 | case BPF_S_ANC_CPU: |
Eric Dumazet | da2033c | 2010-11-30 21:45:56 +0000 | [diff] [blame] | 335 | A = raw_smp_processor_id(); |
| 336 | continue; |
Jiri Pirko | ffe06c1 | 2012-03-31 11:01:20 +0000 | [diff] [blame] | 337 | case BPF_S_ANC_ALU_XOR_X: |
| 338 | A ^= X; |
| 339 | continue; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 340 | case BPF_S_ANC_NLATTR: { |
Patrick McHardy | 4738c1d | 2008-04-10 02:02:28 -0700 | [diff] [blame] | 341 | struct nlattr *nla; |
| 342 | |
| 343 | if (skb_is_nonlinear(skb)) |
| 344 | return 0; |
| 345 | if (A > skb->len - sizeof(struct nlattr)) |
| 346 | return 0; |
| 347 | |
| 348 | nla = nla_find((struct nlattr *)&skb->data[A], |
| 349 | skb->len - A, X); |
| 350 | if (nla) |
| 351 | A = (void *)nla - (void *)skb->data; |
| 352 | else |
| 353 | A = 0; |
| 354 | continue; |
| 355 | } |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 356 | case BPF_S_ANC_NLATTR_NEST: { |
Pablo Neira Ayuso | d214c75 | 2008-11-20 00:49:27 -0800 | [diff] [blame] | 357 | struct nlattr *nla; |
| 358 | |
| 359 | if (skb_is_nonlinear(skb)) |
| 360 | return 0; |
| 361 | if (A > skb->len - sizeof(struct nlattr)) |
| 362 | return 0; |
| 363 | |
| 364 | nla = (struct nlattr *)&skb->data[A]; |
| 365 | if (nla->nla_len > A - skb->len) |
| 366 | return 0; |
| 367 | |
| 368 | nla = nla_find_nested(nla, X); |
| 369 | if (nla) |
| 370 | A = (void *)nla - (void *)skb->data; |
| 371 | else |
| 372 | A = 0; |
| 373 | continue; |
| 374 | } |
Will Drewry | 46b325c | 2012-04-12 16:47:52 -0500 | [diff] [blame] | 375 | #ifdef CONFIG_SECCOMP_FILTER |
| 376 | case BPF_S_ANC_SECCOMP_LD_W: |
| 377 | A = seccomp_bpf_load(fentry->k); |
| 378 | continue; |
| 379 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | default: |
Joe Perches | 6c4a5cb | 2011-05-21 07:48:40 +0000 | [diff] [blame] | 381 | WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n", |
| 382 | fentry->code, fentry->jt, |
| 383 | fentry->jf, fentry->k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return 0; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
Stephen Hemminger | b715631 | 2008-04-10 01:33:47 -0700 | [diff] [blame] | 390 | EXPORT_SYMBOL(sk_run_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 392 | /* |
| 393 | * Security : |
| 394 | * A BPF program is able to use 16 cells of memory to store intermediate |
| 395 | * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter()) |
| 396 | * As we dont want to clear mem[] array for each packet going through |
| 397 | * sk_run_filter(), we check that filter loaded by user never try to read |
| 398 | * a cell if not previously written, and we check all branches to be sure |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 399 | * a malicious user doesn't try to abuse us. |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 400 | */ |
| 401 | static int check_load_and_stores(struct sock_filter *filter, int flen) |
| 402 | { |
| 403 | u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */ |
| 404 | int pc, ret = 0; |
| 405 | |
| 406 | BUILD_BUG_ON(BPF_MEMWORDS > 16); |
| 407 | masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL); |
| 408 | if (!masks) |
| 409 | return -ENOMEM; |
| 410 | memset(masks, 0xff, flen * sizeof(*masks)); |
| 411 | |
| 412 | for (pc = 0; pc < flen; pc++) { |
| 413 | memvalid &= masks[pc]; |
| 414 | |
| 415 | switch (filter[pc].code) { |
| 416 | case BPF_S_ST: |
| 417 | case BPF_S_STX: |
| 418 | memvalid |= (1 << filter[pc].k); |
| 419 | break; |
| 420 | case BPF_S_LD_MEM: |
| 421 | case BPF_S_LDX_MEM: |
| 422 | if (!(memvalid & (1 << filter[pc].k))) { |
| 423 | ret = -EINVAL; |
| 424 | goto error; |
| 425 | } |
| 426 | break; |
| 427 | case BPF_S_JMP_JA: |
| 428 | /* a jump must set masks on target */ |
| 429 | masks[pc + 1 + filter[pc].k] &= memvalid; |
| 430 | memvalid = ~0; |
| 431 | break; |
| 432 | case BPF_S_JMP_JEQ_K: |
| 433 | case BPF_S_JMP_JEQ_X: |
| 434 | case BPF_S_JMP_JGE_K: |
| 435 | case BPF_S_JMP_JGE_X: |
| 436 | case BPF_S_JMP_JGT_K: |
| 437 | case BPF_S_JMP_JGT_X: |
| 438 | case BPF_S_JMP_JSET_X: |
| 439 | case BPF_S_JMP_JSET_K: |
| 440 | /* a jump must set masks on targets */ |
| 441 | masks[pc + 1 + filter[pc].jt] &= memvalid; |
| 442 | masks[pc + 1 + filter[pc].jf] &= memvalid; |
| 443 | memvalid = ~0; |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | error: |
| 448 | kfree(masks); |
| 449 | return ret; |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /** |
| 453 | * sk_chk_filter - verify socket filter code |
| 454 | * @filter: filter to verify |
| 455 | * @flen: length of filter |
| 456 | * |
| 457 | * Check the user's filter code. If we let some ugly |
| 458 | * filter code slip through kaboom! The filter must contain |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 459 | * no references or jumps that are out of range, no illegal |
| 460 | * instructions, and must end with a RET instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | * |
Kris Katterjohn | 7b11f69 | 2006-01-13 14:33:06 -0800 | [diff] [blame] | 462 | * All jumps are forward as they are not signed. |
| 463 | * |
| 464 | * Returns 0 if the rule set is legal or -EINVAL if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | */ |
Dan Carpenter | 4f25af2 | 2011-10-17 21:04:20 +0000 | [diff] [blame] | 466 | int sk_chk_filter(struct sock_filter *filter, unsigned int flen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 468 | /* |
| 469 | * Valid instructions are initialized to non-0. |
| 470 | * Invalid instructions are initialized to 0. |
| 471 | */ |
| 472 | static const u8 codes[] = { |
Eric Dumazet | 8c1592d | 2010-11-18 21:56:38 +0000 | [diff] [blame] | 473 | [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K, |
| 474 | [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X, |
| 475 | [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K, |
| 476 | [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X, |
| 477 | [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K, |
| 478 | [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X, |
| 479 | [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X, |
Eric Dumazet | b6069a9 | 2012-09-07 22:03:35 +0000 | [diff] [blame^] | 480 | [BPF_ALU|BPF_MOD|BPF_K] = BPF_S_ALU_MOD_K, |
| 481 | [BPF_ALU|BPF_MOD|BPF_X] = BPF_S_ALU_MOD_X, |
Eric Dumazet | 8c1592d | 2010-11-18 21:56:38 +0000 | [diff] [blame] | 482 | [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K, |
| 483 | [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X, |
| 484 | [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K, |
| 485 | [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X, |
| 486 | [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K, |
| 487 | [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X, |
| 488 | [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K, |
| 489 | [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X, |
| 490 | [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG, |
| 491 | [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS, |
| 492 | [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS, |
| 493 | [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS, |
| 494 | [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN, |
| 495 | [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND, |
| 496 | [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND, |
| 497 | [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND, |
| 498 | [BPF_LD|BPF_IMM] = BPF_S_LD_IMM, |
| 499 | [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN, |
| 500 | [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH, |
| 501 | [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM, |
| 502 | [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX, |
| 503 | [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA, |
| 504 | [BPF_RET|BPF_K] = BPF_S_RET_K, |
| 505 | [BPF_RET|BPF_A] = BPF_S_RET_A, |
| 506 | [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K, |
| 507 | [BPF_LD|BPF_MEM] = BPF_S_LD_MEM, |
| 508 | [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM, |
| 509 | [BPF_ST] = BPF_S_ST, |
| 510 | [BPF_STX] = BPF_S_STX, |
| 511 | [BPF_JMP|BPF_JA] = BPF_S_JMP_JA, |
| 512 | [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K, |
| 513 | [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X, |
| 514 | [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K, |
| 515 | [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X, |
| 516 | [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K, |
| 517 | [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X, |
| 518 | [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K, |
| 519 | [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X, |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 520 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | int pc; |
| 522 | |
David S. Miller | 1b93ae64 | 2005-12-27 13:57:59 -0800 | [diff] [blame] | 523 | if (flen == 0 || flen > BPF_MAXINSNS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return -EINVAL; |
| 525 | |
| 526 | /* check the filter code now */ |
| 527 | for (pc = 0; pc < flen; pc++) { |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 528 | struct sock_filter *ftest = &filter[pc]; |
| 529 | u16 code = ftest->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 531 | if (code >= ARRAY_SIZE(codes)) |
| 532 | return -EINVAL; |
| 533 | code = codes[code]; |
Eric Dumazet | 8c1592d | 2010-11-18 21:56:38 +0000 | [diff] [blame] | 534 | if (!code) |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 535 | return -EINVAL; |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 536 | /* Some instructions need special checks */ |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 537 | switch (code) { |
| 538 | case BPF_S_ALU_DIV_K: |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 539 | /* check for division by zero */ |
| 540 | if (ftest->k == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | return -EINVAL; |
Eric Dumazet | c26aed4 | 2010-11-18 22:04:46 +0000 | [diff] [blame] | 542 | ftest->k = reciprocal_value(ftest->k); |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 543 | break; |
Eric Dumazet | b6069a9 | 2012-09-07 22:03:35 +0000 | [diff] [blame^] | 544 | case BPF_S_ALU_MOD_K: |
| 545 | /* check for division by zero */ |
| 546 | if (ftest->k == 0) |
| 547 | return -EINVAL; |
| 548 | break; |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 549 | case BPF_S_LD_MEM: |
| 550 | case BPF_S_LDX_MEM: |
| 551 | case BPF_S_ST: |
| 552 | case BPF_S_STX: |
| 553 | /* check for invalid memory addresses */ |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 554 | if (ftest->k >= BPF_MEMWORDS) |
| 555 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 556 | break; |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 557 | case BPF_S_JMP_JA: |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 558 | /* |
| 559 | * Note, the large ftest->k might cause loops. |
| 560 | * Compare this with conditional jumps below, |
| 561 | * where offsets are limited. --ANK (981016) |
| 562 | */ |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 563 | if (ftest->k >= (unsigned int)(flen-pc-1)) |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 564 | return -EINVAL; |
| 565 | break; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 566 | case BPF_S_JMP_JEQ_K: |
| 567 | case BPF_S_JMP_JEQ_X: |
| 568 | case BPF_S_JMP_JGE_K: |
| 569 | case BPF_S_JMP_JGE_X: |
| 570 | case BPF_S_JMP_JGT_K: |
| 571 | case BPF_S_JMP_JGT_X: |
| 572 | case BPF_S_JMP_JSET_X: |
| 573 | case BPF_S_JMP_JSET_K: |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 574 | /* for conditionals both must be safe */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 575 | if (pc + ftest->jt + 1 >= flen || |
| 576 | pc + ftest->jf + 1 >= flen) |
| 577 | return -EINVAL; |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 578 | break; |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 579 | case BPF_S_LD_W_ABS: |
| 580 | case BPF_S_LD_H_ABS: |
| 581 | case BPF_S_LD_B_ABS: |
| 582 | #define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \ |
| 583 | code = BPF_S_ANC_##CODE; \ |
| 584 | break |
| 585 | switch (ftest->k) { |
| 586 | ANCILLARY(PROTOCOL); |
| 587 | ANCILLARY(PKTTYPE); |
| 588 | ANCILLARY(IFINDEX); |
| 589 | ANCILLARY(NLATTR); |
| 590 | ANCILLARY(NLATTR_NEST); |
| 591 | ANCILLARY(MARK); |
| 592 | ANCILLARY(QUEUE); |
| 593 | ANCILLARY(HATYPE); |
| 594 | ANCILLARY(RXHASH); |
| 595 | ANCILLARY(CPU); |
Jiri Pirko | ffe06c1 | 2012-03-31 11:01:20 +0000 | [diff] [blame] | 596 | ANCILLARY(ALU_XOR_X); |
Eric Dumazet | 12b16da | 2010-12-15 19:45:28 +0000 | [diff] [blame] | 597 | } |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 598 | } |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 599 | ftest->code = code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 602 | /* last instruction must be a RET code */ |
| 603 | switch (filter[flen - 1].code) { |
| 604 | case BPF_S_RET_K: |
| 605 | case BPF_S_RET_A: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 606 | return check_load_and_stores(filter, flen); |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 607 | } |
| 608 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
Stephen Hemminger | b715631 | 2008-04-10 01:33:47 -0700 | [diff] [blame] | 610 | EXPORT_SYMBOL(sk_chk_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | /** |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 613 | * sk_filter_release_rcu - Release a socket filter by rcu_head |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 614 | * @rcu: rcu_head that contains the sk_filter to free |
| 615 | */ |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 616 | void sk_filter_release_rcu(struct rcu_head *rcu) |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 617 | { |
| 618 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); |
| 619 | |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 620 | bpf_jit_free(fp); |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 621 | kfree(fp); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 622 | } |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 623 | EXPORT_SYMBOL(sk_filter_release_rcu); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 624 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 625 | static int __sk_prepare_filter(struct sk_filter *fp) |
| 626 | { |
| 627 | int err; |
| 628 | |
| 629 | fp->bpf_func = sk_run_filter; |
| 630 | |
| 631 | err = sk_chk_filter(fp->insns, fp->len); |
| 632 | if (err) |
| 633 | return err; |
| 634 | |
| 635 | bpf_jit_compile(fp); |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * sk_unattached_filter_create - create an unattached filter |
| 641 | * @fprog: the filter program |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 642 | * @pfp: the unattached filter that is created |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 643 | * |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 644 | * Create a filter independent of any socket. We first run some |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 645 | * sanity checks on it to make sure it does not explode on us later. |
| 646 | * If an error occurs or there is insufficient memory for the filter |
| 647 | * a negative errno code is returned. On success the return is zero. |
| 648 | */ |
| 649 | int sk_unattached_filter_create(struct sk_filter **pfp, |
| 650 | struct sock_fprog *fprog) |
| 651 | { |
| 652 | struct sk_filter *fp; |
| 653 | unsigned int fsize = sizeof(struct sock_filter) * fprog->len; |
| 654 | int err; |
| 655 | |
| 656 | /* Make sure new filter is there and in the right amounts. */ |
| 657 | if (fprog->filter == NULL) |
| 658 | return -EINVAL; |
| 659 | |
| 660 | fp = kmalloc(fsize + sizeof(*fp), GFP_KERNEL); |
| 661 | if (!fp) |
| 662 | return -ENOMEM; |
| 663 | memcpy(fp->insns, fprog->filter, fsize); |
| 664 | |
| 665 | atomic_set(&fp->refcnt, 1); |
| 666 | fp->len = fprog->len; |
| 667 | |
| 668 | err = __sk_prepare_filter(fp); |
| 669 | if (err) |
| 670 | goto free_mem; |
| 671 | |
| 672 | *pfp = fp; |
| 673 | return 0; |
| 674 | free_mem: |
| 675 | kfree(fp); |
| 676 | return err; |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(sk_unattached_filter_create); |
| 679 | |
| 680 | void sk_unattached_filter_destroy(struct sk_filter *fp) |
| 681 | { |
| 682 | sk_filter_release(fp); |
| 683 | } |
| 684 | EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy); |
| 685 | |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 686 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | * sk_attach_filter - attach a socket filter |
| 688 | * @fprog: the filter program |
| 689 | * @sk: the socket to use |
| 690 | * |
| 691 | * Attach the user's filter code. We first run some sanity checks on |
| 692 | * it to make sure it does not explode on us later. If an error |
| 693 | * occurs or there is insufficient memory for the filter a negative |
| 694 | * errno code is returned. On success the return is zero. |
| 695 | */ |
| 696 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
| 697 | { |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 698 | struct sk_filter *fp, *old_fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | unsigned int fsize = sizeof(struct sock_filter) * fprog->len; |
| 700 | int err; |
| 701 | |
| 702 | /* Make sure new filter is there and in the right amounts. */ |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 703 | if (fprog->filter == NULL) |
| 704 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL); |
| 707 | if (!fp) |
| 708 | return -ENOMEM; |
| 709 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 710 | sock_kfree_s(sk, fp, fsize+sizeof(*fp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | return -EFAULT; |
| 712 | } |
| 713 | |
| 714 | atomic_set(&fp->refcnt, 1); |
| 715 | fp->len = fprog->len; |
| 716 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 717 | err = __sk_prepare_filter(fp); |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 718 | if (err) { |
| 719 | sk_filter_uncharge(sk, fp); |
| 720 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Eric Dumazet | f91ff5b | 2010-09-27 06:07:30 +0000 | [diff] [blame] | 723 | old_fp = rcu_dereference_protected(sk->sk_filter, |
| 724 | sock_owned_by_user(sk)); |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 725 | rcu_assign_pointer(sk->sk_filter, fp); |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 726 | |
Olof Johansson | 9b013e0 | 2007-10-18 21:48:39 -0700 | [diff] [blame] | 727 | if (old_fp) |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 728 | sk_filter_uncharge(sk, old_fp); |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 729 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 731 | EXPORT_SYMBOL_GPL(sk_attach_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 733 | int sk_detach_filter(struct sock *sk) |
| 734 | { |
| 735 | int ret = -ENOENT; |
| 736 | struct sk_filter *filter; |
| 737 | |
Eric Dumazet | f91ff5b | 2010-09-27 06:07:30 +0000 | [diff] [blame] | 738 | filter = rcu_dereference_protected(sk->sk_filter, |
| 739 | sock_owned_by_user(sk)); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 740 | if (filter) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 741 | RCU_INIT_POINTER(sk->sk_filter, NULL); |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 742 | sk_filter_uncharge(sk, filter); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 743 | ret = 0; |
| 744 | } |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 745 | return ret; |
| 746 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 747 | EXPORT_SYMBOL_GPL(sk_detach_filter); |