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