Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux Socket Filter - Kernel level socket filtering |
| 3 | * |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 4 | * Based on the design of the Berkeley Packet Filter. The new |
| 5 | * internal format has been designed by PLUMgrid: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 7 | * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com |
| 8 | * |
| 9 | * Authors: |
| 10 | * |
| 11 | * Jay Schulist <jschlst@samba.org> |
| 12 | * Alexei Starovoitov <ast@plumgrid.com> |
| 13 | * Daniel Borkmann <dborkman@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * Andi Kleen - Fix a few bad bugs and races. |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 21 | * Kris Katterjohn - Added many additional checks in sk_chk_filter() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/fcntl.h> |
| 28 | #include <linux/socket.h> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/inet.h> |
| 31 | #include <linux/netdevice.h> |
| 32 | #include <linux/if_packet.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <net/ip.h> |
| 35 | #include <net/protocol.h> |
Patrick McHardy | 4738c1d | 2008-04-10 02:02:28 -0700 | [diff] [blame] | 36 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/skbuff.h> |
| 38 | #include <net/sock.h> |
| 39 | #include <linux/errno.h> |
| 40 | #include <linux/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/uaccess.h> |
Dmitry Mishin | 40daafc | 2006-04-18 14:50:10 -0700 | [diff] [blame] | 42 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/filter.h> |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 44 | #include <linux/ratelimit.h> |
Will Drewry | 46b325c | 2012-04-12 16:47:52 -0500 | [diff] [blame] | 45 | #include <linux/seccomp.h> |
Eric Dumazet | f333503 | 2012-10-27 02:26:17 +0000 | [diff] [blame] | 46 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /** |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 49 | * sk_filter - run a packet through a socket filter |
| 50 | * @sk: sock associated with &sk_buff |
| 51 | * @skb: buffer to filter |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 52 | * |
| 53 | * Run the filter code and then cut skb->data to correct size returned by |
| 54 | * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller |
| 55 | * than pkt_len we keep whole skb->data. This is the socket level |
| 56 | * wrapper to sk_run_filter. It returns 0 if the packet should |
| 57 | * be accepted or -EPERM if the packet should be tossed. |
| 58 | * |
| 59 | */ |
| 60 | int sk_filter(struct sock *sk, struct sk_buff *skb) |
| 61 | { |
| 62 | int err; |
| 63 | struct sk_filter *filter; |
| 64 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 65 | /* |
| 66 | * If the skb was allocated from pfmemalloc reserves, only |
| 67 | * allow SOCK_MEMALLOC sockets to use it as this socket is |
| 68 | * helping free memory |
| 69 | */ |
| 70 | if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) |
| 71 | return -ENOMEM; |
| 72 | |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 73 | err = security_sock_rcv_skb(sk, skb); |
| 74 | if (err) |
| 75 | return err; |
| 76 | |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 77 | rcu_read_lock(); |
| 78 | filter = rcu_dereference(sk->sk_filter); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 79 | if (filter) { |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 80 | unsigned int pkt_len = SK_RUN_FILTER(filter, skb); |
Eric Dumazet | 0d7da9d | 2010-10-25 03:47:05 +0000 | [diff] [blame] | 81 | |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 82 | err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM; |
| 83 | } |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 84 | rcu_read_unlock(); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 85 | |
| 86 | return err; |
| 87 | } |
| 88 | EXPORT_SYMBOL(sk_filter); |
| 89 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 90 | /* Helper to find the offset of pkt_type in sk_buff structure. We want |
| 91 | * to make sure its still a 3bit field starting at a byte boundary; |
| 92 | * taken from arch/x86/net/bpf_jit_comp.c. |
| 93 | */ |
Alexei Starovoitov | 0dcceab | 2014-06-05 14:39:36 -0700 | [diff] [blame] | 94 | #ifdef __BIG_ENDIAN_BITFIELD |
| 95 | #define PKT_TYPE_MAX (7 << 5) |
| 96 | #else |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 97 | #define PKT_TYPE_MAX 7 |
Alexei Starovoitov | 0dcceab | 2014-06-05 14:39:36 -0700 | [diff] [blame] | 98 | #endif |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 99 | static unsigned int pkt_type_offset(void) |
| 100 | { |
| 101 | struct sk_buff skb_probe = { .pkt_type = ~0, }; |
| 102 | u8 *ct = (u8 *) &skb_probe; |
| 103 | unsigned int off; |
| 104 | |
| 105 | for (off = 0; off < sizeof(struct sk_buff); off++) { |
| 106 | if (ct[off] == PKT_TYPE_MAX) |
| 107 | return off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 110 | pr_err_once("Please fix %s, as pkt_type couldn't be found!\n", __func__); |
| 111 | return -1; |
| 112 | } |
| 113 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 114 | static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 115 | { |
Daniel Borkmann | eb9672f | 2014-05-01 18:34:20 +0200 | [diff] [blame] | 116 | return __skb_get_poff((struct sk_buff *)(unsigned long) ctx); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 119 | static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 120 | { |
Daniel Borkmann | eb9672f | 2014-05-01 18:34:20 +0200 | [diff] [blame] | 121 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 122 | struct nlattr *nla; |
| 123 | |
| 124 | if (skb_is_nonlinear(skb)) |
| 125 | return 0; |
| 126 | |
Mathias Krause | 05ab8f2 | 2014-04-13 18:23:33 +0200 | [diff] [blame] | 127 | if (skb->len < sizeof(struct nlattr)) |
| 128 | return 0; |
| 129 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 130 | if (a > skb->len - sizeof(struct nlattr)) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 131 | return 0; |
| 132 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 133 | nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 134 | if (nla) |
| 135 | return (void *) nla - (void *) skb->data; |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 140 | static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 141 | { |
Daniel Borkmann | eb9672f | 2014-05-01 18:34:20 +0200 | [diff] [blame] | 142 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 143 | struct nlattr *nla; |
| 144 | |
| 145 | if (skb_is_nonlinear(skb)) |
| 146 | return 0; |
| 147 | |
Mathias Krause | 05ab8f2 | 2014-04-13 18:23:33 +0200 | [diff] [blame] | 148 | if (skb->len < sizeof(struct nlattr)) |
| 149 | return 0; |
| 150 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 151 | if (a > skb->len - sizeof(struct nlattr)) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 152 | return 0; |
| 153 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 154 | nla = (struct nlattr *) &skb->data[a]; |
| 155 | if (nla->nla_len > skb->len - a) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 156 | return 0; |
| 157 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 158 | nla = nla_find_nested(nla, x); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 159 | if (nla) |
| 160 | return (void *) nla - (void *) skb->data; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 165 | static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 166 | { |
| 167 | return raw_smp_processor_id(); |
| 168 | } |
| 169 | |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 170 | /* note that this only generates 32-bit random numbers */ |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 171 | static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 172 | { |
Daniel Borkmann | eb9672f | 2014-05-01 18:34:20 +0200 | [diff] [blame] | 173 | return prandom_u32(); |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 176 | static bool convert_bpf_extensions(struct sock_filter *fp, |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 177 | struct bpf_insn **insnp) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 178 | { |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 179 | struct bpf_insn *insn = *insnp; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 180 | |
| 181 | switch (fp->k) { |
| 182 | case SKF_AD_OFF + SKF_AD_PROTOCOL: |
| 183 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); |
| 184 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 185 | /* A = *(u16 *) (CTX + offsetof(protocol)) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 186 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, |
| 187 | offsetof(struct sk_buff, protocol)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 188 | /* A = ntohs(A) [emitting a nop or swap16] */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 189 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 190 | break; |
| 191 | |
| 192 | case SKF_AD_OFF + SKF_AD_PKTTYPE: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 193 | *insn = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX, |
| 194 | pkt_type_offset()); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 195 | if (insn->off < 0) |
| 196 | return false; |
| 197 | insn++; |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 198 | *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, PKT_TYPE_MAX); |
Alexei Starovoitov | 0dcceab | 2014-06-05 14:39:36 -0700 | [diff] [blame] | 199 | #ifdef __BIG_ENDIAN_BITFIELD |
| 200 | insn++; |
David S. Miller | f666f87 | 2014-06-05 16:22:02 -0700 | [diff] [blame] | 201 | *insn = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 5); |
Alexei Starovoitov | 0dcceab | 2014-06-05 14:39:36 -0700 | [diff] [blame] | 202 | #endif |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 203 | break; |
| 204 | |
| 205 | case SKF_AD_OFF + SKF_AD_IFINDEX: |
| 206 | case SKF_AD_OFF + SKF_AD_HATYPE: |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 207 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); |
| 208 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2); |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 209 | BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 210 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 211 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), |
| 212 | BPF_REG_TMP, BPF_REG_CTX, |
| 213 | offsetof(struct sk_buff, dev)); |
| 214 | /* if (tmp != 0) goto pc + 1 */ |
| 215 | *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); |
| 216 | *insn++ = BPF_EXIT_INSN(); |
| 217 | if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) |
| 218 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, |
| 219 | offsetof(struct net_device, ifindex)); |
| 220 | else |
| 221 | *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, |
| 222 | offsetof(struct net_device, type)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 223 | break; |
| 224 | |
| 225 | case SKF_AD_OFF + SKF_AD_MARK: |
| 226 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); |
| 227 | |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 228 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, |
| 229 | offsetof(struct sk_buff, mark)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 230 | break; |
| 231 | |
| 232 | case SKF_AD_OFF + SKF_AD_RXHASH: |
| 233 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); |
| 234 | |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 235 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, |
| 236 | offsetof(struct sk_buff, hash)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | case SKF_AD_OFF + SKF_AD_QUEUE: |
| 240 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); |
| 241 | |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 242 | *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, |
| 243 | offsetof(struct sk_buff, queue_mapping)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case SKF_AD_OFF + SKF_AD_VLAN_TAG: |
| 247 | case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: |
| 248 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 249 | BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); |
| 250 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 251 | /* A = *(u16 *) (CTX + offsetof(vlan_tci)) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 252 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, |
| 253 | offsetof(struct sk_buff, vlan_tci)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 254 | if (fp->k == SKF_AD_OFF + SKF_AD_VLAN_TAG) { |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 255 | *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, |
| 256 | ~VLAN_TAG_PRESENT); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 257 | } else { |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 258 | /* A >>= 12 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 259 | *insn++ = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 12); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 260 | /* A &= 1 */ |
| 261 | *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 1); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 262 | } |
| 263 | break; |
| 264 | |
| 265 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: |
| 266 | case SKF_AD_OFF + SKF_AD_NLATTR: |
| 267 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: |
| 268 | case SKF_AD_OFF + SKF_AD_CPU: |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 269 | case SKF_AD_OFF + SKF_AD_RANDOM: |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 270 | /* arg1 = CTX */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 271 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 272 | /* arg2 = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 273 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 274 | /* arg3 = X */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 275 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 276 | /* Emit call(arg1=CTX, arg2=A, arg3=X) */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 277 | switch (fp->k) { |
| 278 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 279 | *insn = BPF_EMIT_CALL(__skb_get_pay_offset); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 280 | break; |
| 281 | case SKF_AD_OFF + SKF_AD_NLATTR: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 282 | *insn = BPF_EMIT_CALL(__skb_get_nlattr); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 283 | break; |
| 284 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 285 | *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 286 | break; |
| 287 | case SKF_AD_OFF + SKF_AD_CPU: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 288 | *insn = BPF_EMIT_CALL(__get_raw_cpu_id); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 289 | break; |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 290 | case SKF_AD_OFF + SKF_AD_RANDOM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 291 | *insn = BPF_EMIT_CALL(__get_random_u32); |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 292 | break; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 293 | } |
| 294 | break; |
| 295 | |
| 296 | case SKF_AD_OFF + SKF_AD_ALU_XOR_X: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 297 | /* A ^= X */ |
| 298 | *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 299 | break; |
| 300 | |
| 301 | default: |
| 302 | /* This is just a dummy call to avoid letting the compiler |
| 303 | * evict __bpf_call_base() as an optimization. Placed here |
| 304 | * where no-one bothers. |
| 305 | */ |
| 306 | BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | *insnp = insn; |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * sk_convert_filter - convert filter program |
| 316 | * @prog: the user passed filter program |
| 317 | * @len: the length of the user passed filter program |
| 318 | * @new_prog: buffer where converted program will be stored |
| 319 | * @new_len: pointer to store length of converted program |
| 320 | * |
| 321 | * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style. |
| 322 | * Conversion workflow: |
| 323 | * |
| 324 | * 1) First pass for calculating the new program length: |
| 325 | * sk_convert_filter(old_prog, old_len, NULL, &new_len) |
| 326 | * |
| 327 | * 2) 2nd pass to remap in two passes: 1st pass finds new |
| 328 | * jump offsets, 2nd pass remapping: |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 329 | * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 330 | * sk_convert_filter(old_prog, old_len, new_prog, &new_len); |
| 331 | * |
| 332 | * User BPF's register A is mapped to our BPF register 6, user BPF |
| 333 | * register X is mapped to BPF register 7; frame pointer is always |
| 334 | * register 10; Context 'void *ctx' is stored in register 1, that is, |
| 335 | * for socket filters: ctx == 'struct sk_buff *', for seccomp: |
| 336 | * ctx == 'struct seccomp_data *'. |
| 337 | */ |
| 338 | int sk_convert_filter(struct sock_filter *prog, int len, |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 339 | struct bpf_insn *new_prog, int *new_len) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 340 | { |
| 341 | int new_flen = 0, pass = 0, target, i; |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 342 | struct bpf_insn *new_insn; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 343 | struct sock_filter *fp; |
| 344 | int *addrs = NULL; |
| 345 | u8 bpf_src; |
| 346 | |
| 347 | BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 348 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 349 | |
Kees Cook | 6f9a093 | 2014-06-18 15:34:57 -0700 | [diff] [blame] | 350 | if (len <= 0 || len > BPF_MAXINSNS) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 351 | return -EINVAL; |
| 352 | |
| 353 | if (new_prog) { |
Tobias Klauser | 99e72a0 | 2014-06-24 15:33:22 +0200 | [diff] [blame] | 354 | addrs = kcalloc(len, sizeof(*addrs), GFP_KERNEL); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 355 | if (!addrs) |
| 356 | return -ENOMEM; |
| 357 | } |
| 358 | |
| 359 | do_pass: |
| 360 | new_insn = new_prog; |
| 361 | fp = prog; |
| 362 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 363 | if (new_insn) |
| 364 | *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 365 | new_insn++; |
| 366 | |
| 367 | for (i = 0; i < len; fp++, i++) { |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 368 | struct bpf_insn tmp_insns[6] = { }; |
| 369 | struct bpf_insn *insn = tmp_insns; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 370 | |
| 371 | if (addrs) |
| 372 | addrs[i] = new_insn - new_prog; |
| 373 | |
| 374 | switch (fp->code) { |
| 375 | /* All arithmetic insns and skb loads map as-is. */ |
| 376 | case BPF_ALU | BPF_ADD | BPF_X: |
| 377 | case BPF_ALU | BPF_ADD | BPF_K: |
| 378 | case BPF_ALU | BPF_SUB | BPF_X: |
| 379 | case BPF_ALU | BPF_SUB | BPF_K: |
| 380 | case BPF_ALU | BPF_AND | BPF_X: |
| 381 | case BPF_ALU | BPF_AND | BPF_K: |
| 382 | case BPF_ALU | BPF_OR | BPF_X: |
| 383 | case BPF_ALU | BPF_OR | BPF_K: |
| 384 | case BPF_ALU | BPF_LSH | BPF_X: |
| 385 | case BPF_ALU | BPF_LSH | BPF_K: |
| 386 | case BPF_ALU | BPF_RSH | BPF_X: |
| 387 | case BPF_ALU | BPF_RSH | BPF_K: |
| 388 | case BPF_ALU | BPF_XOR | BPF_X: |
| 389 | case BPF_ALU | BPF_XOR | BPF_K: |
| 390 | case BPF_ALU | BPF_MUL | BPF_X: |
| 391 | case BPF_ALU | BPF_MUL | BPF_K: |
| 392 | case BPF_ALU | BPF_DIV | BPF_X: |
| 393 | case BPF_ALU | BPF_DIV | BPF_K: |
| 394 | case BPF_ALU | BPF_MOD | BPF_X: |
| 395 | case BPF_ALU | BPF_MOD | BPF_K: |
| 396 | case BPF_ALU | BPF_NEG: |
| 397 | case BPF_LD | BPF_ABS | BPF_W: |
| 398 | case BPF_LD | BPF_ABS | BPF_H: |
| 399 | case BPF_LD | BPF_ABS | BPF_B: |
| 400 | case BPF_LD | BPF_IND | BPF_W: |
| 401 | case BPF_LD | BPF_IND | BPF_H: |
| 402 | case BPF_LD | BPF_IND | BPF_B: |
| 403 | /* Check for overloaded BPF extension and |
| 404 | * directly convert it if found, otherwise |
| 405 | * just move on with mapping. |
| 406 | */ |
| 407 | if (BPF_CLASS(fp->code) == BPF_LD && |
| 408 | BPF_MODE(fp->code) == BPF_ABS && |
| 409 | convert_bpf_extensions(fp, &insn)) |
| 410 | break; |
| 411 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 412 | *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 413 | break; |
| 414 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 415 | /* Jump transformation cannot use BPF block macros |
| 416 | * everywhere as offset calculation and target updates |
| 417 | * require a bit more work than the rest, i.e. jump |
| 418 | * opcodes map as-is, but offsets need adjustment. |
| 419 | */ |
| 420 | |
| 421 | #define BPF_EMIT_JMP \ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 422 | do { \ |
| 423 | if (target >= len || target < 0) \ |
| 424 | goto err; \ |
| 425 | insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ |
| 426 | /* Adjust pc relative offset for 2nd or 3rd insn. */ \ |
| 427 | insn->off -= insn - tmp_insns; \ |
| 428 | } while (0) |
| 429 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 430 | case BPF_JMP | BPF_JA: |
| 431 | target = i + fp->k + 1; |
| 432 | insn->code = fp->code; |
| 433 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 434 | break; |
| 435 | |
| 436 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 437 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 438 | case BPF_JMP | BPF_JSET | BPF_K: |
| 439 | case BPF_JMP | BPF_JSET | BPF_X: |
| 440 | case BPF_JMP | BPF_JGT | BPF_K: |
| 441 | case BPF_JMP | BPF_JGT | BPF_X: |
| 442 | case BPF_JMP | BPF_JGE | BPF_K: |
| 443 | case BPF_JMP | BPF_JGE | BPF_X: |
| 444 | if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { |
| 445 | /* BPF immediates are signed, zero extend |
| 446 | * immediate into tmp register and use it |
| 447 | * in compare insn. |
| 448 | */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 449 | *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 450 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 451 | insn->dst_reg = BPF_REG_A; |
| 452 | insn->src_reg = BPF_REG_TMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 453 | bpf_src = BPF_X; |
| 454 | } else { |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 455 | insn->dst_reg = BPF_REG_A; |
| 456 | insn->src_reg = BPF_REG_X; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 457 | insn->imm = fp->k; |
| 458 | bpf_src = BPF_SRC(fp->code); |
| 459 | } |
| 460 | |
| 461 | /* Common case where 'jump_false' is next insn. */ |
| 462 | if (fp->jf == 0) { |
| 463 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; |
| 464 | target = i + fp->jt + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 465 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | |
| 469 | /* Convert JEQ into JNE when 'jump_true' is next insn. */ |
| 470 | if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) { |
| 471 | insn->code = BPF_JMP | BPF_JNE | bpf_src; |
| 472 | target = i + fp->jf + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 473 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 474 | break; |
| 475 | } |
| 476 | |
| 477 | /* Other jumps are mapped into two insns: Jxx and JA. */ |
| 478 | target = i + fp->jt + 1; |
| 479 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 480 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 481 | insn++; |
| 482 | |
| 483 | insn->code = BPF_JMP | BPF_JA; |
| 484 | target = i + fp->jf + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 485 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 486 | break; |
| 487 | |
| 488 | /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ |
| 489 | case BPF_LDX | BPF_MSH | BPF_B: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 490 | /* tmp = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 491 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A); |
David S. Miller | 1268e25 | 2014-05-13 13:13:33 -0400 | [diff] [blame] | 492 | /* A = BPF_R0 = *(u8 *) (skb->data + K) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 493 | *insn++ = BPF_LD_ABS(BPF_B, fp->k); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 494 | /* A &= 0xf */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 495 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 496 | /* A <<= 2 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 497 | *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 498 | /* X = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 499 | *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 500 | /* A = tmp */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 501 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 502 | break; |
| 503 | |
| 504 | /* RET_K, RET_A are remaped into 2 insns. */ |
| 505 | case BPF_RET | BPF_A: |
| 506 | case BPF_RET | BPF_K: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 507 | *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ? |
| 508 | BPF_K : BPF_X, BPF_REG_0, |
| 509 | BPF_REG_A, fp->k); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 510 | *insn = BPF_EXIT_INSN(); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 511 | break; |
| 512 | |
| 513 | /* Store to stack. */ |
| 514 | case BPF_ST: |
| 515 | case BPF_STX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 516 | *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == |
| 517 | BPF_ST ? BPF_REG_A : BPF_REG_X, |
| 518 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 519 | break; |
| 520 | |
| 521 | /* Load from stack. */ |
| 522 | case BPF_LD | BPF_MEM: |
| 523 | case BPF_LDX | BPF_MEM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 524 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 525 | BPF_REG_A : BPF_REG_X, BPF_REG_FP, |
| 526 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 527 | break; |
| 528 | |
| 529 | /* A = K or X = K */ |
| 530 | case BPF_LD | BPF_IMM: |
| 531 | case BPF_LDX | BPF_IMM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 532 | *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? |
| 533 | BPF_REG_A : BPF_REG_X, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 534 | break; |
| 535 | |
| 536 | /* X = A */ |
| 537 | case BPF_MISC | BPF_TAX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 538 | *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 539 | break; |
| 540 | |
| 541 | /* A = X */ |
| 542 | case BPF_MISC | BPF_TXA: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 543 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 544 | break; |
| 545 | |
| 546 | /* A = skb->len or X = skb->len */ |
| 547 | case BPF_LD | BPF_W | BPF_LEN: |
| 548 | case BPF_LDX | BPF_W | BPF_LEN: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 549 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 550 | BPF_REG_A : BPF_REG_X, BPF_REG_CTX, |
| 551 | offsetof(struct sk_buff, len)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 552 | break; |
| 553 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 554 | /* Access seccomp_data fields. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 555 | case BPF_LDX | BPF_ABS | BPF_W: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 556 | /* A = *(u32 *) (ctx + K) */ |
| 557 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 558 | break; |
| 559 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 560 | /* Unkown instruction. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 561 | default: |
| 562 | goto err; |
| 563 | } |
| 564 | |
| 565 | insn++; |
| 566 | if (new_prog) |
| 567 | memcpy(new_insn, tmp_insns, |
| 568 | sizeof(*insn) * (insn - tmp_insns)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 569 | new_insn += insn - tmp_insns; |
| 570 | } |
| 571 | |
| 572 | if (!new_prog) { |
| 573 | /* Only calculating new length. */ |
| 574 | *new_len = new_insn - new_prog; |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | pass++; |
| 579 | if (new_flen != new_insn - new_prog) { |
| 580 | new_flen = new_insn - new_prog; |
| 581 | if (pass > 2) |
| 582 | goto err; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 583 | goto do_pass; |
| 584 | } |
| 585 | |
| 586 | kfree(addrs); |
| 587 | BUG_ON(*new_len != new_flen); |
| 588 | return 0; |
| 589 | err: |
| 590 | kfree(addrs); |
| 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | /* Security: |
| 595 | * |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 596 | * A BPF program is able to use 16 cells of memory to store intermediate |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 597 | * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter()). |
| 598 | * |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 599 | * As we dont want to clear mem[] array for each packet going through |
| 600 | * sk_run_filter(), we check that filter loaded by user never try to read |
| 601 | * 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] | 602 | * a malicious user doesn't try to abuse us. |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 603 | */ |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 604 | static int check_load_and_stores(const struct sock_filter *filter, int flen) |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 605 | { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 606 | u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 607 | int pc, ret = 0; |
| 608 | |
| 609 | BUILD_BUG_ON(BPF_MEMWORDS > 16); |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 610 | |
Tobias Klauser | 99e72a0 | 2014-06-24 15:33:22 +0200 | [diff] [blame] | 611 | masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 612 | if (!masks) |
| 613 | return -ENOMEM; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 614 | |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 615 | memset(masks, 0xff, flen * sizeof(*masks)); |
| 616 | |
| 617 | for (pc = 0; pc < flen; pc++) { |
| 618 | memvalid &= masks[pc]; |
| 619 | |
| 620 | switch (filter[pc].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 621 | case BPF_ST: |
| 622 | case BPF_STX: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 623 | memvalid |= (1 << filter[pc].k); |
| 624 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 625 | case BPF_LD | BPF_MEM: |
| 626 | case BPF_LDX | BPF_MEM: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 627 | if (!(memvalid & (1 << filter[pc].k))) { |
| 628 | ret = -EINVAL; |
| 629 | goto error; |
| 630 | } |
| 631 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 632 | case BPF_JMP | BPF_JA: |
| 633 | /* A jump must set masks on target */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 634 | masks[pc + 1 + filter[pc].k] &= memvalid; |
| 635 | memvalid = ~0; |
| 636 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 637 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 638 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 639 | case BPF_JMP | BPF_JGE | BPF_K: |
| 640 | case BPF_JMP | BPF_JGE | BPF_X: |
| 641 | case BPF_JMP | BPF_JGT | BPF_K: |
| 642 | case BPF_JMP | BPF_JGT | BPF_X: |
| 643 | case BPF_JMP | BPF_JSET | BPF_K: |
| 644 | case BPF_JMP | BPF_JSET | BPF_X: |
| 645 | /* A jump must set masks on targets */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 646 | masks[pc + 1 + filter[pc].jt] &= memvalid; |
| 647 | masks[pc + 1 + filter[pc].jf] &= memvalid; |
| 648 | memvalid = ~0; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | error: |
| 653 | kfree(masks); |
| 654 | return ret; |
| 655 | } |
| 656 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 657 | static bool chk_code_allowed(u16 code_to_probe) |
| 658 | { |
| 659 | static const bool codes[] = { |
| 660 | /* 32 bit ALU operations */ |
| 661 | [BPF_ALU | BPF_ADD | BPF_K] = true, |
| 662 | [BPF_ALU | BPF_ADD | BPF_X] = true, |
| 663 | [BPF_ALU | BPF_SUB | BPF_K] = true, |
| 664 | [BPF_ALU | BPF_SUB | BPF_X] = true, |
| 665 | [BPF_ALU | BPF_MUL | BPF_K] = true, |
| 666 | [BPF_ALU | BPF_MUL | BPF_X] = true, |
| 667 | [BPF_ALU | BPF_DIV | BPF_K] = true, |
| 668 | [BPF_ALU | BPF_DIV | BPF_X] = true, |
| 669 | [BPF_ALU | BPF_MOD | BPF_K] = true, |
| 670 | [BPF_ALU | BPF_MOD | BPF_X] = true, |
| 671 | [BPF_ALU | BPF_AND | BPF_K] = true, |
| 672 | [BPF_ALU | BPF_AND | BPF_X] = true, |
| 673 | [BPF_ALU | BPF_OR | BPF_K] = true, |
| 674 | [BPF_ALU | BPF_OR | BPF_X] = true, |
| 675 | [BPF_ALU | BPF_XOR | BPF_K] = true, |
| 676 | [BPF_ALU | BPF_XOR | BPF_X] = true, |
| 677 | [BPF_ALU | BPF_LSH | BPF_K] = true, |
| 678 | [BPF_ALU | BPF_LSH | BPF_X] = true, |
| 679 | [BPF_ALU | BPF_RSH | BPF_K] = true, |
| 680 | [BPF_ALU | BPF_RSH | BPF_X] = true, |
| 681 | [BPF_ALU | BPF_NEG] = true, |
| 682 | /* Load instructions */ |
| 683 | [BPF_LD | BPF_W | BPF_ABS] = true, |
| 684 | [BPF_LD | BPF_H | BPF_ABS] = true, |
| 685 | [BPF_LD | BPF_B | BPF_ABS] = true, |
| 686 | [BPF_LD | BPF_W | BPF_LEN] = true, |
| 687 | [BPF_LD | BPF_W | BPF_IND] = true, |
| 688 | [BPF_LD | BPF_H | BPF_IND] = true, |
| 689 | [BPF_LD | BPF_B | BPF_IND] = true, |
| 690 | [BPF_LD | BPF_IMM] = true, |
| 691 | [BPF_LD | BPF_MEM] = true, |
| 692 | [BPF_LDX | BPF_W | BPF_LEN] = true, |
| 693 | [BPF_LDX | BPF_B | BPF_MSH] = true, |
| 694 | [BPF_LDX | BPF_IMM] = true, |
| 695 | [BPF_LDX | BPF_MEM] = true, |
| 696 | /* Store instructions */ |
| 697 | [BPF_ST] = true, |
| 698 | [BPF_STX] = true, |
| 699 | /* Misc instructions */ |
| 700 | [BPF_MISC | BPF_TAX] = true, |
| 701 | [BPF_MISC | BPF_TXA] = true, |
| 702 | /* Return instructions */ |
| 703 | [BPF_RET | BPF_K] = true, |
| 704 | [BPF_RET | BPF_A] = true, |
| 705 | /* Jump instructions */ |
| 706 | [BPF_JMP | BPF_JA] = true, |
| 707 | [BPF_JMP | BPF_JEQ | BPF_K] = true, |
| 708 | [BPF_JMP | BPF_JEQ | BPF_X] = true, |
| 709 | [BPF_JMP | BPF_JGE | BPF_K] = true, |
| 710 | [BPF_JMP | BPF_JGE | BPF_X] = true, |
| 711 | [BPF_JMP | BPF_JGT | BPF_K] = true, |
| 712 | [BPF_JMP | BPF_JGT | BPF_X] = true, |
| 713 | [BPF_JMP | BPF_JSET | BPF_K] = true, |
| 714 | [BPF_JMP | BPF_JSET | BPF_X] = true, |
| 715 | }; |
| 716 | |
| 717 | if (code_to_probe >= ARRAY_SIZE(codes)) |
| 718 | return false; |
| 719 | |
| 720 | return codes[code_to_probe]; |
| 721 | } |
| 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | /** |
| 724 | * sk_chk_filter - verify socket filter code |
| 725 | * @filter: filter to verify |
| 726 | * @flen: length of filter |
| 727 | * |
| 728 | * Check the user's filter code. If we let some ugly |
| 729 | * filter code slip through kaboom! The filter must contain |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 730 | * no references or jumps that are out of range, no illegal |
| 731 | * instructions, and must end with a RET instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | * |
Kris Katterjohn | 7b11f69 | 2006-01-13 14:33:06 -0800 | [diff] [blame] | 733 | * All jumps are forward as they are not signed. |
| 734 | * |
| 735 | * Returns 0 if the rule set is legal or -EINVAL if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | */ |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 737 | int sk_chk_filter(const struct sock_filter *filter, unsigned int flen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | { |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 739 | bool anc_found; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 740 | int pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
David S. Miller | 1b93ae64 | 2005-12-27 13:57:59 -0800 | [diff] [blame] | 742 | if (flen == 0 || flen > BPF_MAXINSNS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return -EINVAL; |
| 744 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 745 | /* Check the filter code now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | for (pc = 0; pc < flen; pc++) { |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 747 | const struct sock_filter *ftest = &filter[pc]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 749 | /* May we actually operate on this code? */ |
| 750 | if (!chk_code_allowed(ftest->code)) |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 751 | return -EINVAL; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 752 | |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 753 | /* Some instructions need special checks */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 754 | switch (ftest->code) { |
| 755 | case BPF_ALU | BPF_DIV | BPF_K: |
| 756 | case BPF_ALU | BPF_MOD | BPF_K: |
| 757 | /* Check for division by zero */ |
Eric Dumazet | b6069a9 | 2012-09-07 22:03:35 +0000 | [diff] [blame] | 758 | if (ftest->k == 0) |
| 759 | return -EINVAL; |
| 760 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 761 | case BPF_LD | BPF_MEM: |
| 762 | case BPF_LDX | BPF_MEM: |
| 763 | case BPF_ST: |
| 764 | case BPF_STX: |
| 765 | /* Check for invalid memory addresses */ |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 766 | if (ftest->k >= BPF_MEMWORDS) |
| 767 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 768 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 769 | case BPF_JMP | BPF_JA: |
| 770 | /* Note, the large ftest->k might cause loops. |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 771 | * Compare this with conditional jumps below, |
| 772 | * where offsets are limited. --ANK (981016) |
| 773 | */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 774 | if (ftest->k >= (unsigned int)(flen - pc - 1)) |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 775 | return -EINVAL; |
| 776 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 777 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 778 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 779 | case BPF_JMP | BPF_JGE | BPF_K: |
| 780 | case BPF_JMP | BPF_JGE | BPF_X: |
| 781 | case BPF_JMP | BPF_JGT | BPF_K: |
| 782 | case BPF_JMP | BPF_JGT | BPF_X: |
| 783 | case BPF_JMP | BPF_JSET | BPF_K: |
| 784 | case BPF_JMP | BPF_JSET | BPF_X: |
| 785 | /* Both conditionals must be safe */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 786 | if (pc + ftest->jt + 1 >= flen || |
| 787 | pc + ftest->jf + 1 >= flen) |
| 788 | return -EINVAL; |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 789 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 790 | case BPF_LD | BPF_W | BPF_ABS: |
| 791 | case BPF_LD | BPF_H | BPF_ABS: |
| 792 | case BPF_LD | BPF_B | BPF_ABS: |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 793 | anc_found = false; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 794 | if (bpf_anc_helper(ftest) & BPF_ANC) |
| 795 | anc_found = true; |
| 796 | /* Ancillary operation unknown or unsupported */ |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 797 | if (anc_found == false && ftest->k >= SKF_AD_OFF) |
| 798 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 802 | /* Last instruction must be a RET code */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 803 | switch (filter[flen - 1].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 804 | case BPF_RET | BPF_K: |
| 805 | case BPF_RET | BPF_A: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 806 | return check_load_and_stores(filter, flen); |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 807 | } |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 808 | |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 809 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
Stephen Hemminger | b715631 | 2008-04-10 01:33:47 -0700 | [diff] [blame] | 811 | EXPORT_SYMBOL(sk_chk_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 813 | static int sk_store_orig_filter(struct sk_filter *fp, |
| 814 | const struct sock_fprog *fprog) |
| 815 | { |
| 816 | unsigned int fsize = sk_filter_proglen(fprog); |
| 817 | struct sock_fprog_kern *fkprog; |
| 818 | |
| 819 | fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); |
| 820 | if (!fp->orig_prog) |
| 821 | return -ENOMEM; |
| 822 | |
| 823 | fkprog = fp->orig_prog; |
| 824 | fkprog->len = fprog->len; |
| 825 | fkprog->filter = kmemdup(fp->insns, fsize, GFP_KERNEL); |
| 826 | if (!fkprog->filter) { |
| 827 | kfree(fp->orig_prog); |
| 828 | return -ENOMEM; |
| 829 | } |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static void sk_release_orig_filter(struct sk_filter *fp) |
| 835 | { |
| 836 | struct sock_fprog_kern *fprog = fp->orig_prog; |
| 837 | |
| 838 | if (fprog) { |
| 839 | kfree(fprog->filter); |
| 840 | kfree(fprog); |
| 841 | } |
| 842 | } |
| 843 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 844 | static void __sk_filter_release(struct sk_filter *fp) |
| 845 | { |
| 846 | sk_release_orig_filter(fp); |
| 847 | sk_filter_free(fp); |
| 848 | } |
| 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | /** |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 851 | * sk_filter_release_rcu - Release a socket filter by rcu_head |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 852 | * @rcu: rcu_head that contains the sk_filter to free |
| 853 | */ |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 854 | static void sk_filter_release_rcu(struct rcu_head *rcu) |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 855 | { |
| 856 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); |
| 857 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 858 | __sk_filter_release(fp); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 859 | } |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 860 | |
| 861 | /** |
| 862 | * sk_filter_release - release a socket filter |
| 863 | * @fp: filter to remove |
| 864 | * |
| 865 | * Remove a filter from a socket and release its resources. |
| 866 | */ |
| 867 | static void sk_filter_release(struct sk_filter *fp) |
| 868 | { |
| 869 | if (atomic_dec_and_test(&fp->refcnt)) |
| 870 | call_rcu(&fp->rcu, sk_filter_release_rcu); |
| 871 | } |
| 872 | |
| 873 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) |
| 874 | { |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 875 | u32 filter_size = sk_filter_size(fp->len); |
| 876 | |
| 877 | atomic_sub(filter_size, &sk->sk_omem_alloc); |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 878 | sk_filter_release(fp); |
| 879 | } |
| 880 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 881 | /* try to charge the socket memory if there is space available |
| 882 | * return true on success |
| 883 | */ |
| 884 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 885 | { |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 886 | u32 filter_size = sk_filter_size(fp->len); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 887 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 888 | /* same check as in sock_kmalloc() */ |
| 889 | if (filter_size <= sysctl_optmem_max && |
| 890 | atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { |
| 891 | atomic_inc(&fp->refcnt); |
| 892 | atomic_add(filter_size, &sk->sk_omem_alloc); |
| 893 | return true; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 894 | } |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 895 | return false; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 898 | static struct sk_filter *__sk_migrate_filter(struct sk_filter *fp) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 899 | { |
| 900 | struct sock_filter *old_prog; |
| 901 | struct sk_filter *old_fp; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 902 | int err, new_len, old_len = fp->len; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 903 | |
| 904 | /* We are free to overwrite insns et al right here as it |
| 905 | * won't be used at this point in time anymore internally |
| 906 | * after the migration to the internal BPF instruction |
| 907 | * representation. |
| 908 | */ |
| 909 | BUILD_BUG_ON(sizeof(struct sock_filter) != |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 910 | sizeof(struct bpf_insn)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 911 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 912 | /* Conversion cannot happen on overlapping memory areas, |
| 913 | * so we need to keep the user BPF around until the 2nd |
| 914 | * pass. At this time, the user BPF is stored in fp->insns. |
| 915 | */ |
| 916 | old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), |
| 917 | GFP_KERNEL); |
| 918 | if (!old_prog) { |
| 919 | err = -ENOMEM; |
| 920 | goto out_err; |
| 921 | } |
| 922 | |
| 923 | /* 1st pass: calculate the new program length. */ |
| 924 | err = sk_convert_filter(old_prog, old_len, NULL, &new_len); |
| 925 | if (err) |
| 926 | goto out_err_free; |
| 927 | |
| 928 | /* Expand fp for appending the new filter representation. */ |
| 929 | old_fp = fp; |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 930 | fp = krealloc(old_fp, sk_filter_size(new_len), GFP_KERNEL); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 931 | if (!fp) { |
| 932 | /* The old_fp is still around in case we couldn't |
| 933 | * allocate new memory, so uncharge on that one. |
| 934 | */ |
| 935 | fp = old_fp; |
| 936 | err = -ENOMEM; |
| 937 | goto out_err_free; |
| 938 | } |
| 939 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 940 | fp->len = new_len; |
| 941 | |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 942 | /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 943 | err = sk_convert_filter(old_prog, old_len, fp->insnsi, &new_len); |
| 944 | if (err) |
| 945 | /* 2nd sk_convert_filter() can fail only if it fails |
| 946 | * to allocate memory, remapping must succeed. Note, |
| 947 | * that at this time old_fp has already been released |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 948 | * by krealloc(). |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 949 | */ |
| 950 | goto out_err_free; |
| 951 | |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 952 | sk_filter_select_runtime(fp); |
| 953 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 954 | kfree(old_prog); |
| 955 | return fp; |
| 956 | |
| 957 | out_err_free: |
| 958 | kfree(old_prog); |
| 959 | out_err: |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 960 | __sk_filter_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 961 | return ERR_PTR(err); |
| 962 | } |
| 963 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 964 | static struct sk_filter *__sk_prepare_filter(struct sk_filter *fp) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 965 | { |
| 966 | int err; |
| 967 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 968 | fp->bpf_func = NULL; |
Daniel Borkmann | f8bbbfc | 2014-03-28 18:58:18 +0100 | [diff] [blame] | 969 | fp->jited = 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 970 | |
| 971 | err = sk_chk_filter(fp->insns, fp->len); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 972 | if (err) { |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 973 | __sk_filter_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 974 | return ERR_PTR(err); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 975 | } |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 976 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 977 | /* Probe if we can JIT compile the filter and if so, do |
| 978 | * the compilation of the filter. |
| 979 | */ |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 980 | bpf_jit_compile(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 981 | |
| 982 | /* JIT compiler couldn't process this filter, so do the |
| 983 | * internal BPF translation for the optimized interpreter. |
| 984 | */ |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 985 | if (!fp->jited) |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 986 | fp = __sk_migrate_filter(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 987 | |
| 988 | return fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /** |
| 992 | * sk_unattached_filter_create - create an unattached filter |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 993 | * @pfp: the unattached filter that is created |
Tobias Klauser | 677a9fd | 2014-06-24 15:33:21 +0200 | [diff] [blame] | 994 | * @fprog: the filter program |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 995 | * |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 996 | * Create a filter independent of any socket. We first run some |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 997 | * sanity checks on it to make sure it does not explode on us later. |
| 998 | * If an error occurs or there is insufficient memory for the filter |
| 999 | * a negative errno code is returned. On success the return is zero. |
| 1000 | */ |
| 1001 | int sk_unattached_filter_create(struct sk_filter **pfp, |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 1002 | struct sock_fprog_kern *fprog) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1003 | { |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1004 | unsigned int fsize = sk_filter_proglen(fprog); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1005 | struct sk_filter *fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* Make sure new filter is there and in the right amounts. */ |
| 1008 | if (fprog->filter == NULL) |
| 1009 | return -EINVAL; |
| 1010 | |
Alexei Starovoitov | d45ed4a | 2013-10-04 00:14:06 -0700 | [diff] [blame] | 1011 | fp = kmalloc(sk_filter_size(fprog->len), GFP_KERNEL); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1012 | if (!fp) |
| 1013 | return -ENOMEM; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1014 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1015 | memcpy(fp->insns, fprog->filter, fsize); |
| 1016 | |
| 1017 | atomic_set(&fp->refcnt, 1); |
| 1018 | fp->len = fprog->len; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1019 | /* Since unattached filters are not copied back to user |
| 1020 | * space through sk_get_filter(), we do not need to hold |
| 1021 | * a copy here, and can spare us the work. |
| 1022 | */ |
| 1023 | fp->orig_prog = NULL; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1024 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1025 | /* __sk_prepare_filter() already takes care of freeing |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1026 | * memory in case something goes wrong. |
| 1027 | */ |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1028 | fp = __sk_prepare_filter(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1029 | if (IS_ERR(fp)) |
| 1030 | return PTR_ERR(fp); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1031 | |
| 1032 | *pfp = fp; |
| 1033 | return 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(sk_unattached_filter_create); |
| 1036 | |
| 1037 | void sk_unattached_filter_destroy(struct sk_filter *fp) |
| 1038 | { |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 1039 | __sk_filter_release(fp); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy); |
| 1042 | |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 1043 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | * sk_attach_filter - attach a socket filter |
| 1045 | * @fprog: the filter program |
| 1046 | * @sk: the socket to use |
| 1047 | * |
| 1048 | * Attach the user's filter code. We first run some sanity checks on |
| 1049 | * it to make sure it does not explode on us later. If an error |
| 1050 | * occurs or there is insufficient memory for the filter a negative |
| 1051 | * errno code is returned. On success the return is zero. |
| 1052 | */ |
| 1053 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
| 1054 | { |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 1055 | struct sk_filter *fp, *old_fp; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1056 | unsigned int fsize = sk_filter_proglen(fprog); |
Alexei Starovoitov | d45ed4a | 2013-10-04 00:14:06 -0700 | [diff] [blame] | 1057 | unsigned int sk_fsize = sk_filter_size(fprog->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | int err; |
| 1059 | |
Vincent Bernat | d59577b | 2013-01-16 22:55:49 +0100 | [diff] [blame] | 1060 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1061 | return -EPERM; |
| 1062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | /* Make sure new filter is there and in the right amounts. */ |
Kris Katterjohn | e35bedf | 2006-01-17 02:25:52 -0800 | [diff] [blame] | 1064 | if (fprog->filter == NULL) |
| 1065 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1067 | fp = kmalloc(sk_fsize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | if (!fp) |
| 1069 | return -ENOMEM; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1072 | kfree(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | return -EFAULT; |
| 1074 | } |
| 1075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | fp->len = fprog->len; |
| 1077 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1078 | err = sk_store_orig_filter(fp, fprog); |
| 1079 | if (err) { |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1080 | kfree(fp); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1081 | return -ENOMEM; |
| 1082 | } |
| 1083 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1084 | /* __sk_prepare_filter() already takes care of freeing |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1085 | * memory in case something goes wrong. |
| 1086 | */ |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1087 | fp = __sk_prepare_filter(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1088 | if (IS_ERR(fp)) |
| 1089 | return PTR_ERR(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame^] | 1091 | atomic_set(&fp->refcnt, 0); |
| 1092 | |
| 1093 | if (!sk_filter_charge(sk, fp)) { |
| 1094 | __sk_filter_release(fp); |
| 1095 | return -ENOMEM; |
| 1096 | } |
| 1097 | |
Eric Dumazet | f91ff5b | 2010-09-27 06:07:30 +0000 | [diff] [blame] | 1098 | old_fp = rcu_dereference_protected(sk->sk_filter, |
| 1099 | sock_owned_by_user(sk)); |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 1100 | rcu_assign_pointer(sk->sk_filter, fp); |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 1101 | |
Olof Johansson | 9b013e0 | 2007-10-18 21:48:39 -0700 | [diff] [blame] | 1102 | if (old_fp) |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 1103 | sk_filter_uncharge(sk, old_fp); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1104 | |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 1105 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 1107 | EXPORT_SYMBOL_GPL(sk_attach_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 1109 | int sk_detach_filter(struct sock *sk) |
| 1110 | { |
| 1111 | int ret = -ENOENT; |
| 1112 | struct sk_filter *filter; |
| 1113 | |
Vincent Bernat | d59577b | 2013-01-16 22:55:49 +0100 | [diff] [blame] | 1114 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1115 | return -EPERM; |
| 1116 | |
Eric Dumazet | f91ff5b | 2010-09-27 06:07:30 +0000 | [diff] [blame] | 1117 | filter = rcu_dereference_protected(sk->sk_filter, |
| 1118 | sock_owned_by_user(sk)); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 1119 | if (filter) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 1120 | RCU_INIT_POINTER(sk->sk_filter, NULL); |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 1121 | sk_filter_uncharge(sk, filter); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 1122 | ret = 0; |
| 1123 | } |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1124 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 1125 | return ret; |
| 1126 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 1127 | EXPORT_SYMBOL_GPL(sk_detach_filter); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1128 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1129 | int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, |
| 1130 | unsigned int len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1131 | { |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1132 | struct sock_fprog_kern *fprog; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1133 | struct sk_filter *filter; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1134 | int ret = 0; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1135 | |
| 1136 | lock_sock(sk); |
| 1137 | filter = rcu_dereference_protected(sk->sk_filter, |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1138 | sock_owned_by_user(sk)); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1139 | if (!filter) |
| 1140 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1141 | |
| 1142 | /* We're copying the filter that has been originally attached, |
| 1143 | * so no conversion/decode needed anymore. |
| 1144 | */ |
| 1145 | fprog = filter->orig_prog; |
| 1146 | |
| 1147 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1148 | if (!len) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1149 | /* User space only enquires number of filter blocks. */ |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1150 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1151 | |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1152 | ret = -EINVAL; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1153 | if (len < fprog->len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1154 | goto out; |
| 1155 | |
| 1156 | ret = -EFAULT; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1157 | if (copy_to_user(ubuf, fprog->filter, sk_filter_proglen(fprog))) |
| 1158 | goto out; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1159 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1160 | /* Instead of bytes, the API requests to return the number |
| 1161 | * of filter blocks. |
| 1162 | */ |
| 1163 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 1164 | out: |
| 1165 | release_sock(sk); |
| 1166 | return ret; |
| 1167 | } |