blob: 60e3fe7c59c0e6b15797f7f5af6f4e7765dd80ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01004 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01007 * 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 Torvalds1da177e2005-04-16 15:20:36 -070014 *
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 Starovoitov4df95ff2014-07-30 20:34:14 -070021 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
24#include <linux/module.h>
25#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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 Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/ip.h>
35#include <net/protocol.h>
Patrick McHardy4738c1d2008-04-10 02:02:28 -070036#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/skbuff.h>
38#include <net/sock.h>
Jiri Pirko10b89ee42015-05-12 14:56:09 +020039#include <net/flow_dissector.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
41#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
Dmitry Mishin40daafc2006-04-18 14:50:10 -070043#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/filter.h>
David S. Miller86e4ca62011-05-26 15:00:31 -040045#include <linux/ratelimit.h>
Will Drewry46b325c2012-04-12 16:47:52 -050046#include <linux/seccomp.h>
Eric Dumazetf3335032012-10-27 02:26:17 +000047#include <linux/if_vlan.h>
Alexei Starovoitov89aa0752014-12-01 15:06:35 -080048#include <linux/bpf.h>
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -070049#include <net/sch_generic.h>
Daniel Borkmann8d20aab2015-07-15 14:21:42 +020050#include <net/cls_cgroup.h>
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -070051#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -070054 * sk_filter - run a packet through a socket filter
55 * @sk: sock associated with &sk_buff
56 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070057 *
58 * Run the filter code and then cut skb->data to correct size returned by
Li RongQing8ea6e342014-10-10 13:56:51 +080059 * SK_RUN_FILTER. If pkt_len is 0 we toss packet. If skb->len is smaller
Stephen Hemminger43db6d62008-04-10 01:43:09 -070060 * than pkt_len we keep whole skb->data. This is the socket level
Li RongQing8ea6e342014-10-10 13:56:51 +080061 * wrapper to SK_RUN_FILTER. It returns 0 if the packet should
Stephen Hemminger43db6d62008-04-10 01:43:09 -070062 * be accepted or -EPERM if the packet should be tossed.
63 *
64 */
65int sk_filter(struct sock *sk, struct sk_buff *skb)
66{
67 int err;
68 struct sk_filter *filter;
69
Mel Gormanc93bdd02012-07-31 16:44:19 -070070 /*
71 * If the skb was allocated from pfmemalloc reserves, only
72 * allow SOCK_MEMALLOC sockets to use it as this socket is
73 * helping free memory
74 */
75 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
76 return -ENOMEM;
77
Stephen Hemminger43db6d62008-04-10 01:43:09 -070078 err = security_sock_rcv_skb(sk, skb);
79 if (err)
80 return err;
81
Eric Dumazet80f8f102011-01-18 07:46:52 +000082 rcu_read_lock();
83 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -070084 if (filter) {
Eric Dumazet0a148422011-04-20 09:27:32 +000085 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +000086
Stephen Hemminger43db6d62008-04-10 01:43:09 -070087 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
88 }
Eric Dumazet80f8f102011-01-18 07:46:52 +000089 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -070090
91 return err;
92}
93EXPORT_SYMBOL(sk_filter);
94
Daniel Borkmann30743832014-05-01 18:34:19 +020095static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010096{
Alexander Duyck56193d12014-09-05 19:20:26 -040097 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010098}
99
Daniel Borkmann30743832014-05-01 18:34:19 +0200100static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100101{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200102 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100103 struct nlattr *nla;
104
105 if (skb_is_nonlinear(skb))
106 return 0;
107
Mathias Krause05ab8f22014-04-13 18:23:33 +0200108 if (skb->len < sizeof(struct nlattr))
109 return 0;
110
Daniel Borkmann30743832014-05-01 18:34:19 +0200111 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100112 return 0;
113
Daniel Borkmann30743832014-05-01 18:34:19 +0200114 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100115 if (nla)
116 return (void *) nla - (void *) skb->data;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119}
120
Daniel Borkmann30743832014-05-01 18:34:19 +0200121static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100122{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200123 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100124 struct nlattr *nla;
125
126 if (skb_is_nonlinear(skb))
127 return 0;
128
Mathias Krause05ab8f22014-04-13 18:23:33 +0200129 if (skb->len < sizeof(struct nlattr))
130 return 0;
131
Daniel Borkmann30743832014-05-01 18:34:19 +0200132 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100133 return 0;
134
Daniel Borkmann30743832014-05-01 18:34:19 +0200135 nla = (struct nlattr *) &skb->data[a];
136 if (nla->nla_len > skb->len - a)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100137 return 0;
138
Daniel Borkmann30743832014-05-01 18:34:19 +0200139 nla = nla_find_nested(nla, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100140 if (nla)
141 return (void *) nla - (void *) skb->data;
142
143 return 0;
144}
145
Daniel Borkmann30743832014-05-01 18:34:19 +0200146static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100147{
148 return raw_smp_processor_id();
149}
150
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700151/* note that this only generates 32-bit random numbers */
Daniel Borkmann30743832014-05-01 18:34:19 +0200152static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700153{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200154 return prandom_u32();
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700155}
156
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700157static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
158 struct bpf_insn *insn_buf)
159{
160 struct bpf_insn *insn = insn_buf;
161
162 switch (skb_field) {
163 case SKF_AD_MARK:
164 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
165
166 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
167 offsetof(struct sk_buff, mark));
168 break;
169
170 case SKF_AD_PKTTYPE:
171 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
172 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
173#ifdef __BIG_ENDIAN_BITFIELD
174 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
175#endif
176 break;
177
178 case SKF_AD_QUEUE:
179 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
180
181 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
182 offsetof(struct sk_buff, queue_mapping));
183 break;
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700184
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700185 case SKF_AD_VLAN_TAG:
186 case SKF_AD_VLAN_TAG_PRESENT:
187 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
188 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
189
190 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
191 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
192 offsetof(struct sk_buff, vlan_tci));
193 if (skb_field == SKF_AD_VLAN_TAG) {
194 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
195 ~VLAN_TAG_PRESENT);
196 } else {
197 /* dst_reg >>= 12 */
198 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
199 /* dst_reg &= 1 */
200 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
201 }
202 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700203 }
204
205 return insn - insn_buf;
206}
207
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100208static bool convert_bpf_extensions(struct sock_filter *fp,
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700209 struct bpf_insn **insnp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100210{
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700211 struct bpf_insn *insn = *insnp;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700212 u32 cnt;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100213
214 switch (fp->k) {
215 case SKF_AD_OFF + SKF_AD_PROTOCOL:
Daniel Borkmann0b8c7072015-03-19 19:38:27 +0100216 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
217
218 /* A = *(u16 *) (CTX + offsetof(protocol)) */
219 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
220 offsetof(struct sk_buff, protocol));
221 /* A = ntohs(A) [emitting a nop or swap16] */
222 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100223 break;
224
225 case SKF_AD_OFF + SKF_AD_PKTTYPE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700226 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
227 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100228 break;
229
230 case SKF_AD_OFF + SKF_AD_IFINDEX:
231 case SKF_AD_OFF + SKF_AD_HATYPE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100232 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
233 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200234 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100235
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200236 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
237 BPF_REG_TMP, BPF_REG_CTX,
238 offsetof(struct sk_buff, dev));
239 /* if (tmp != 0) goto pc + 1 */
240 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
241 *insn++ = BPF_EXIT_INSN();
242 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
243 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
244 offsetof(struct net_device, ifindex));
245 else
246 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
247 offsetof(struct net_device, type));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100248 break;
249
250 case SKF_AD_OFF + SKF_AD_MARK:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700251 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
252 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100253 break;
254
255 case SKF_AD_OFF + SKF_AD_RXHASH:
256 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
257
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700258 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
259 offsetof(struct sk_buff, hash));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100260 break;
261
262 case SKF_AD_OFF + SKF_AD_QUEUE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700263 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
264 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100265 break;
266
267 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700268 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
269 BPF_REG_A, BPF_REG_CTX, insn);
270 insn += cnt - 1;
271 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100272
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700273 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
274 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
275 BPF_REG_A, BPF_REG_CTX, insn);
276 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100277 break;
278
Michal Sekletar27cd5452015-03-24 14:48:41 +0100279 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
280 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
281
282 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
283 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
284 offsetof(struct sk_buff, vlan_proto));
285 /* A = ntohs(A) [emitting a nop or swap16] */
286 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
287 break;
288
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100289 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
290 case SKF_AD_OFF + SKF_AD_NLATTR:
291 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
292 case SKF_AD_OFF + SKF_AD_CPU:
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700293 case SKF_AD_OFF + SKF_AD_RANDOM:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700294 /* arg1 = CTX */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200295 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100296 /* arg2 = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200297 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100298 /* arg3 = X */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200299 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700300 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100301 switch (fp->k) {
302 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200303 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100304 break;
305 case SKF_AD_OFF + SKF_AD_NLATTR:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200306 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100307 break;
308 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200309 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100310 break;
311 case SKF_AD_OFF + SKF_AD_CPU:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200312 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100313 break;
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700314 case SKF_AD_OFF + SKF_AD_RANDOM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200315 *insn = BPF_EMIT_CALL(__get_random_u32);
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700316 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100317 }
318 break;
319
320 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700321 /* A ^= X */
322 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100323 break;
324
325 default:
326 /* This is just a dummy call to avoid letting the compiler
327 * evict __bpf_call_base() as an optimization. Placed here
328 * where no-one bothers.
329 */
330 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
331 return false;
332 }
333
334 *insnp = insn;
335 return true;
336}
337
338/**
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700339 * bpf_convert_filter - convert filter program
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100340 * @prog: the user passed filter program
341 * @len: the length of the user passed filter program
342 * @new_prog: buffer where converted program will be stored
343 * @new_len: pointer to store length of converted program
344 *
345 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
346 * Conversion workflow:
347 *
348 * 1) First pass for calculating the new program length:
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700349 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100350 *
351 * 2) 2nd pass to remap in two passes: 1st pass finds new
352 * jump offsets, 2nd pass remapping:
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700353 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700354 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100355 *
356 * User BPF's register A is mapped to our BPF register 6, user BPF
357 * register X is mapped to BPF register 7; frame pointer is always
358 * register 10; Context 'void *ctx' is stored in register 1, that is,
359 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
360 * ctx == 'struct seccomp_data *'.
361 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200362static int bpf_convert_filter(struct sock_filter *prog, int len,
363 struct bpf_insn *new_prog, int *new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100364{
365 int new_flen = 0, pass = 0, target, i;
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700366 struct bpf_insn *new_insn;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100367 struct sock_filter *fp;
368 int *addrs = NULL;
369 u8 bpf_src;
370
371 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
Daniel Borkmann30743832014-05-01 18:34:19 +0200372 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100373
Kees Cook6f9a0932014-06-18 15:34:57 -0700374 if (len <= 0 || len > BPF_MAXINSNS)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100375 return -EINVAL;
376
377 if (new_prog) {
Daniel Borkmann658da932015-05-06 16:12:29 +0200378 addrs = kcalloc(len, sizeof(*addrs),
379 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100380 if (!addrs)
381 return -ENOMEM;
382 }
383
384do_pass:
385 new_insn = new_prog;
386 fp = prog;
387
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200388 if (new_insn)
389 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100390 new_insn++;
391
392 for (i = 0; i < len; fp++, i++) {
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700393 struct bpf_insn tmp_insns[6] = { };
394 struct bpf_insn *insn = tmp_insns;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100395
396 if (addrs)
397 addrs[i] = new_insn - new_prog;
398
399 switch (fp->code) {
400 /* All arithmetic insns and skb loads map as-is. */
401 case BPF_ALU | BPF_ADD | BPF_X:
402 case BPF_ALU | BPF_ADD | BPF_K:
403 case BPF_ALU | BPF_SUB | BPF_X:
404 case BPF_ALU | BPF_SUB | BPF_K:
405 case BPF_ALU | BPF_AND | BPF_X:
406 case BPF_ALU | BPF_AND | BPF_K:
407 case BPF_ALU | BPF_OR | BPF_X:
408 case BPF_ALU | BPF_OR | BPF_K:
409 case BPF_ALU | BPF_LSH | BPF_X:
410 case BPF_ALU | BPF_LSH | BPF_K:
411 case BPF_ALU | BPF_RSH | BPF_X:
412 case BPF_ALU | BPF_RSH | BPF_K:
413 case BPF_ALU | BPF_XOR | BPF_X:
414 case BPF_ALU | BPF_XOR | BPF_K:
415 case BPF_ALU | BPF_MUL | BPF_X:
416 case BPF_ALU | BPF_MUL | BPF_K:
417 case BPF_ALU | BPF_DIV | BPF_X:
418 case BPF_ALU | BPF_DIV | BPF_K:
419 case BPF_ALU | BPF_MOD | BPF_X:
420 case BPF_ALU | BPF_MOD | BPF_K:
421 case BPF_ALU | BPF_NEG:
422 case BPF_LD | BPF_ABS | BPF_W:
423 case BPF_LD | BPF_ABS | BPF_H:
424 case BPF_LD | BPF_ABS | BPF_B:
425 case BPF_LD | BPF_IND | BPF_W:
426 case BPF_LD | BPF_IND | BPF_H:
427 case BPF_LD | BPF_IND | BPF_B:
428 /* Check for overloaded BPF extension and
429 * directly convert it if found, otherwise
430 * just move on with mapping.
431 */
432 if (BPF_CLASS(fp->code) == BPF_LD &&
433 BPF_MODE(fp->code) == BPF_ABS &&
434 convert_bpf_extensions(fp, &insn))
435 break;
436
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200437 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100438 break;
439
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200440 /* Jump transformation cannot use BPF block macros
441 * everywhere as offset calculation and target updates
442 * require a bit more work than the rest, i.e. jump
443 * opcodes map as-is, but offsets need adjustment.
444 */
445
446#define BPF_EMIT_JMP \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100447 do { \
448 if (target >= len || target < 0) \
449 goto err; \
450 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
451 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
452 insn->off -= insn - tmp_insns; \
453 } while (0)
454
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200455 case BPF_JMP | BPF_JA:
456 target = i + fp->k + 1;
457 insn->code = fp->code;
458 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100459 break;
460
461 case BPF_JMP | BPF_JEQ | BPF_K:
462 case BPF_JMP | BPF_JEQ | BPF_X:
463 case BPF_JMP | BPF_JSET | BPF_K:
464 case BPF_JMP | BPF_JSET | BPF_X:
465 case BPF_JMP | BPF_JGT | BPF_K:
466 case BPF_JMP | BPF_JGT | BPF_X:
467 case BPF_JMP | BPF_JGE | BPF_K:
468 case BPF_JMP | BPF_JGE | BPF_X:
469 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
470 /* BPF immediates are signed, zero extend
471 * immediate into tmp register and use it
472 * in compare insn.
473 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200474 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100475
Alexei Starovoitove430f342014-06-06 14:46:06 -0700476 insn->dst_reg = BPF_REG_A;
477 insn->src_reg = BPF_REG_TMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100478 bpf_src = BPF_X;
479 } else {
Alexei Starovoitove430f342014-06-06 14:46:06 -0700480 insn->dst_reg = BPF_REG_A;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100481 insn->imm = fp->k;
482 bpf_src = BPF_SRC(fp->code);
Tycho Andersen19539ce2015-09-10 18:25:07 -0600483 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100484 }
485
486 /* Common case where 'jump_false' is next insn. */
487 if (fp->jf == 0) {
488 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
489 target = i + fp->jt + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200490 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100491 break;
492 }
493
494 /* Convert JEQ into JNE when 'jump_true' is next insn. */
495 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
496 insn->code = BPF_JMP | BPF_JNE | bpf_src;
497 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200498 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100499 break;
500 }
501
502 /* Other jumps are mapped into two insns: Jxx and JA. */
503 target = i + fp->jt + 1;
504 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200505 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100506 insn++;
507
508 insn->code = BPF_JMP | BPF_JA;
509 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200510 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100511 break;
512
513 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
514 case BPF_LDX | BPF_MSH | BPF_B:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700515 /* tmp = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200516 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
David S. Miller1268e252014-05-13 13:13:33 -0400517 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200518 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700519 /* A &= 0xf */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200520 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700521 /* A <<= 2 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200522 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700523 /* X = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200524 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700525 /* A = tmp */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200526 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100527 break;
528
529 /* RET_K, RET_A are remaped into 2 insns. */
530 case BPF_RET | BPF_A:
531 case BPF_RET | BPF_K:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200532 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
533 BPF_K : BPF_X, BPF_REG_0,
534 BPF_REG_A, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700535 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100536 break;
537
538 /* Store to stack. */
539 case BPF_ST:
540 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200541 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
542 BPF_ST ? BPF_REG_A : BPF_REG_X,
543 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100544 break;
545
546 /* Load from stack. */
547 case BPF_LD | BPF_MEM:
548 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200549 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
550 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
551 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100552 break;
553
554 /* A = K or X = K */
555 case BPF_LD | BPF_IMM:
556 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200557 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
558 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100559 break;
560
561 /* X = A */
562 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200563 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100564 break;
565
566 /* A = X */
567 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200568 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100569 break;
570
571 /* A = skb->len or X = skb->len */
572 case BPF_LD | BPF_W | BPF_LEN:
573 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200574 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
575 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
576 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100577 break;
578
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200579 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100580 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700581 /* A = *(u32 *) (ctx + K) */
582 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100583 break;
584
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500585 /* Unknown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100586 default:
587 goto err;
588 }
589
590 insn++;
591 if (new_prog)
592 memcpy(new_insn, tmp_insns,
593 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100594 new_insn += insn - tmp_insns;
595 }
596
597 if (!new_prog) {
598 /* Only calculating new length. */
599 *new_len = new_insn - new_prog;
600 return 0;
601 }
602
603 pass++;
604 if (new_flen != new_insn - new_prog) {
605 new_flen = new_insn - new_prog;
606 if (pass > 2)
607 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100608 goto do_pass;
609 }
610
611 kfree(addrs);
612 BUG_ON(*new_len != new_flen);
613 return 0;
614err:
615 kfree(addrs);
616 return -EINVAL;
617}
618
619/* Security:
620 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000621 * As we dont want to clear mem[] array for each packet going through
Li RongQing8ea6e342014-10-10 13:56:51 +0800622 * __bpf_prog_run(), we check that filter loaded by user never try to read
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000623 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300624 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000625 */
Eric Dumazetec31a052014-07-12 15:49:16 +0200626static int check_load_and_stores(const struct sock_filter *filter, int flen)
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000627{
Daniel Borkmann34805932014-05-29 10:22:50 +0200628 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000629 int pc, ret = 0;
630
631 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +0200632
Tobias Klauser99e72a02014-06-24 15:33:22 +0200633 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000634 if (!masks)
635 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +0200636
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000637 memset(masks, 0xff, flen * sizeof(*masks));
638
639 for (pc = 0; pc < flen; pc++) {
640 memvalid &= masks[pc];
641
642 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200643 case BPF_ST:
644 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000645 memvalid |= (1 << filter[pc].k);
646 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200647 case BPF_LD | BPF_MEM:
648 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000649 if (!(memvalid & (1 << filter[pc].k))) {
650 ret = -EINVAL;
651 goto error;
652 }
653 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200654 case BPF_JMP | BPF_JA:
655 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000656 masks[pc + 1 + filter[pc].k] &= memvalid;
657 memvalid = ~0;
658 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200659 case BPF_JMP | BPF_JEQ | BPF_K:
660 case BPF_JMP | BPF_JEQ | BPF_X:
661 case BPF_JMP | BPF_JGE | BPF_K:
662 case BPF_JMP | BPF_JGE | BPF_X:
663 case BPF_JMP | BPF_JGT | BPF_K:
664 case BPF_JMP | BPF_JGT | BPF_X:
665 case BPF_JMP | BPF_JSET | BPF_K:
666 case BPF_JMP | BPF_JSET | BPF_X:
667 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000668 masks[pc + 1 + filter[pc].jt] &= memvalid;
669 masks[pc + 1 + filter[pc].jf] &= memvalid;
670 memvalid = ~0;
671 break;
672 }
673 }
674error:
675 kfree(masks);
676 return ret;
677}
678
Daniel Borkmann34805932014-05-29 10:22:50 +0200679static bool chk_code_allowed(u16 code_to_probe)
680{
681 static const bool codes[] = {
682 /* 32 bit ALU operations */
683 [BPF_ALU | BPF_ADD | BPF_K] = true,
684 [BPF_ALU | BPF_ADD | BPF_X] = true,
685 [BPF_ALU | BPF_SUB | BPF_K] = true,
686 [BPF_ALU | BPF_SUB | BPF_X] = true,
687 [BPF_ALU | BPF_MUL | BPF_K] = true,
688 [BPF_ALU | BPF_MUL | BPF_X] = true,
689 [BPF_ALU | BPF_DIV | BPF_K] = true,
690 [BPF_ALU | BPF_DIV | BPF_X] = true,
691 [BPF_ALU | BPF_MOD | BPF_K] = true,
692 [BPF_ALU | BPF_MOD | BPF_X] = true,
693 [BPF_ALU | BPF_AND | BPF_K] = true,
694 [BPF_ALU | BPF_AND | BPF_X] = true,
695 [BPF_ALU | BPF_OR | BPF_K] = true,
696 [BPF_ALU | BPF_OR | BPF_X] = true,
697 [BPF_ALU | BPF_XOR | BPF_K] = true,
698 [BPF_ALU | BPF_XOR | BPF_X] = true,
699 [BPF_ALU | BPF_LSH | BPF_K] = true,
700 [BPF_ALU | BPF_LSH | BPF_X] = true,
701 [BPF_ALU | BPF_RSH | BPF_K] = true,
702 [BPF_ALU | BPF_RSH | BPF_X] = true,
703 [BPF_ALU | BPF_NEG] = true,
704 /* Load instructions */
705 [BPF_LD | BPF_W | BPF_ABS] = true,
706 [BPF_LD | BPF_H | BPF_ABS] = true,
707 [BPF_LD | BPF_B | BPF_ABS] = true,
708 [BPF_LD | BPF_W | BPF_LEN] = true,
709 [BPF_LD | BPF_W | BPF_IND] = true,
710 [BPF_LD | BPF_H | BPF_IND] = true,
711 [BPF_LD | BPF_B | BPF_IND] = true,
712 [BPF_LD | BPF_IMM] = true,
713 [BPF_LD | BPF_MEM] = true,
714 [BPF_LDX | BPF_W | BPF_LEN] = true,
715 [BPF_LDX | BPF_B | BPF_MSH] = true,
716 [BPF_LDX | BPF_IMM] = true,
717 [BPF_LDX | BPF_MEM] = true,
718 /* Store instructions */
719 [BPF_ST] = true,
720 [BPF_STX] = true,
721 /* Misc instructions */
722 [BPF_MISC | BPF_TAX] = true,
723 [BPF_MISC | BPF_TXA] = true,
724 /* Return instructions */
725 [BPF_RET | BPF_K] = true,
726 [BPF_RET | BPF_A] = true,
727 /* Jump instructions */
728 [BPF_JMP | BPF_JA] = true,
729 [BPF_JMP | BPF_JEQ | BPF_K] = true,
730 [BPF_JMP | BPF_JEQ | BPF_X] = true,
731 [BPF_JMP | BPF_JGE | BPF_K] = true,
732 [BPF_JMP | BPF_JGE | BPF_X] = true,
733 [BPF_JMP | BPF_JGT | BPF_K] = true,
734 [BPF_JMP | BPF_JGT | BPF_X] = true,
735 [BPF_JMP | BPF_JSET | BPF_K] = true,
736 [BPF_JMP | BPF_JSET | BPF_X] = true,
737 };
738
739 if (code_to_probe >= ARRAY_SIZE(codes))
740 return false;
741
742 return codes[code_to_probe];
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/**
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700746 * bpf_check_classic - verify socket filter code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * @filter: filter to verify
748 * @flen: length of filter
749 *
750 * Check the user's filter code. If we let some ugly
751 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800752 * no references or jumps that are out of range, no illegal
753 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800755 * All jumps are forward as they are not signed.
756 *
757 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200759static int bpf_check_classic(const struct sock_filter *filter,
760 unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000762 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +0200763 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David S. Miller1b93ae642005-12-27 13:57:59 -0800765 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -EINVAL;
767
Daniel Borkmann34805932014-05-29 10:22:50 +0200768 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 for (pc = 0; pc < flen; pc++) {
Eric Dumazetec31a052014-07-12 15:49:16 +0200770 const struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Daniel Borkmann34805932014-05-29 10:22:50 +0200772 /* May we actually operate on this code? */
773 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +0000774 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +0200775
Kris Katterjohn93699862006-01-04 13:58:36 -0800776 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +0200777 switch (ftest->code) {
778 case BPF_ALU | BPF_DIV | BPF_K:
779 case BPF_ALU | BPF_MOD | BPF_K:
780 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +0000781 if (ftest->k == 0)
782 return -EINVAL;
783 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200784 case BPF_LD | BPF_MEM:
785 case BPF_LDX | BPF_MEM:
786 case BPF_ST:
787 case BPF_STX:
788 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800789 if (ftest->k >= BPF_MEMWORDS)
790 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000791 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200792 case BPF_JMP | BPF_JA:
793 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -0800794 * Compare this with conditional jumps below,
795 * where offsets are limited. --ANK (981016)
796 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200797 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800798 return -EINVAL;
799 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200800 case BPF_JMP | BPF_JEQ | BPF_K:
801 case BPF_JMP | BPF_JEQ | BPF_X:
802 case BPF_JMP | BPF_JGE | BPF_K:
803 case BPF_JMP | BPF_JGE | BPF_X:
804 case BPF_JMP | BPF_JGT | BPF_K:
805 case BPF_JMP | BPF_JGT | BPF_X:
806 case BPF_JMP | BPF_JSET | BPF_K:
807 case BPF_JMP | BPF_JSET | BPF_X:
808 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000809 if (pc + ftest->jt + 1 >= flen ||
810 pc + ftest->jf + 1 >= flen)
811 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000812 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200813 case BPF_LD | BPF_W | BPF_ABS:
814 case BPF_LD | BPF_H | BPF_ABS:
815 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000816 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +0200817 if (bpf_anc_helper(ftest) & BPF_ANC)
818 anc_found = true;
819 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000820 if (anc_found == false && ftest->k >= SKF_AD_OFF)
821 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Daniel Borkmann34805932014-05-29 10:22:50 +0200825 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000826 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200827 case BPF_RET | BPF_K:
828 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000829 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000830 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200831
Tetsuo Handacba328f2010-11-16 15:19:51 +0000832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700835static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
836 const struct sock_fprog *fprog)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100837{
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700838 unsigned int fsize = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100839 struct sock_fprog_kern *fkprog;
840
841 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
842 if (!fp->orig_prog)
843 return -ENOMEM;
844
845 fkprog = fp->orig_prog;
846 fkprog->len = fprog->len;
Daniel Borkmann658da932015-05-06 16:12:29 +0200847
848 fkprog->filter = kmemdup(fp->insns, fsize,
849 GFP_KERNEL | __GFP_NOWARN);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100850 if (!fkprog->filter) {
851 kfree(fp->orig_prog);
852 return -ENOMEM;
853 }
854
855 return 0;
856}
857
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700858static void bpf_release_orig_filter(struct bpf_prog *fp)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100859{
860 struct sock_fprog_kern *fprog = fp->orig_prog;
861
862 if (fprog) {
863 kfree(fprog->filter);
864 kfree(fprog);
865 }
866}
867
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700868static void __bpf_prog_release(struct bpf_prog *prog)
869{
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100870 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800871 bpf_prog_put(prog);
872 } else {
873 bpf_release_orig_filter(prog);
874 bpf_prog_free(prog);
875 }
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700876}
877
Pablo Neira34c5bd62014-07-29 17:36:28 +0200878static void __sk_filter_release(struct sk_filter *fp)
879{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700880 __bpf_prog_release(fp->prog);
881 kfree(fp);
Pablo Neira34c5bd62014-07-29 17:36:28 +0200882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800885 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700886 * @rcu: rcu_head that contains the sk_filter to free
887 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100888static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700889{
890 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
891
Pablo Neira34c5bd62014-07-29 17:36:28 +0200892 __sk_filter_release(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700893}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100894
895/**
896 * sk_filter_release - release a socket filter
897 * @fp: filter to remove
898 *
899 * Remove a filter from a socket and release its resources.
900 */
901static void sk_filter_release(struct sk_filter *fp)
902{
903 if (atomic_dec_and_test(&fp->refcnt))
904 call_rcu(&fp->rcu, sk_filter_release_rcu);
905}
906
907void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
908{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700909 u32 filter_size = bpf_prog_size(fp->prog->len);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700910
911 atomic_sub(filter_size, &sk->sk_omem_alloc);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100912 sk_filter_release(fp);
913}
914
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700915/* try to charge the socket memory if there is space available
916 * return true on success
917 */
918bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100919{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700920 u32 filter_size = bpf_prog_size(fp->prog->len);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700921
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700922 /* same check as in sock_kmalloc() */
923 if (filter_size <= sysctl_optmem_max &&
924 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
925 atomic_inc(&fp->refcnt);
926 atomic_add(filter_size, &sk->sk_omem_alloc);
927 return true;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100928 }
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700929 return false;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100930}
931
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700932static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100933{
934 struct sock_filter *old_prog;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700935 struct bpf_prog *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +0200936 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100937
938 /* We are free to overwrite insns et al right here as it
939 * won't be used at this point in time anymore internally
940 * after the migration to the internal BPF instruction
941 * representation.
942 */
943 BUILD_BUG_ON(sizeof(struct sock_filter) !=
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700944 sizeof(struct bpf_insn));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100945
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100946 /* Conversion cannot happen on overlapping memory areas,
947 * so we need to keep the user BPF around until the 2nd
948 * pass. At this time, the user BPF is stored in fp->insns.
949 */
950 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
Daniel Borkmann658da932015-05-06 16:12:29 +0200951 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100952 if (!old_prog) {
953 err = -ENOMEM;
954 goto out_err;
955 }
956
957 /* 1st pass: calculate the new program length. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700958 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100959 if (err)
960 goto out_err_free;
961
962 /* Expand fp for appending the new filter representation. */
963 old_fp = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200964 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100965 if (!fp) {
966 /* The old_fp is still around in case we couldn't
967 * allocate new memory, so uncharge on that one.
968 */
969 fp = old_fp;
970 err = -ENOMEM;
971 goto out_err_free;
972 }
973
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100974 fp->len = new_len;
975
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700976 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700977 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100978 if (err)
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700979 /* 2nd bpf_convert_filter() can fail only if it fails
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100980 * to allocate memory, remapping must succeed. Note,
981 * that at this time old_fp has already been released
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700982 * by krealloc().
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100983 */
984 goto out_err_free;
985
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700986 bpf_prog_select_runtime(fp);
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -0700987
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100988 kfree(old_prog);
989 return fp;
990
991out_err_free:
992 kfree(old_prog);
993out_err:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700994 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100995 return ERR_PTR(err);
996}
997
Daniel Borkmannac67eb22015-05-06 16:12:30 +0200998static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
999 bpf_aux_classic_check_t trans)
Jiri Pirko302d6632012-03-31 11:01:19 +00001000{
1001 int err;
1002
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001003 fp->bpf_func = NULL;
Daniel Borkmann286aad32014-09-08 08:04:49 +02001004 fp->jited = false;
Jiri Pirko302d6632012-03-31 11:01:19 +00001005
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -07001006 err = bpf_check_classic(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001007 if (err) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001008 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001009 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001010 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001011
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001012 /* There might be additional checks and transformations
1013 * needed on classic filters, f.e. in case of seccomp.
1014 */
1015 if (trans) {
1016 err = trans(fp->insns, fp->len);
1017 if (err) {
1018 __bpf_prog_release(fp);
1019 return ERR_PTR(err);
1020 }
1021 }
1022
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001023 /* Probe if we can JIT compile the filter and if so, do
1024 * the compilation of the filter.
1025 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001026 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001027
1028 /* JIT compiler couldn't process this filter, so do the
1029 * internal BPF translation for the optimized interpreter.
1030 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001031 if (!fp->jited)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001032 fp = bpf_migrate_filter(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001033
1034 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001035}
1036
1037/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001038 * bpf_prog_create - create an unattached filter
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001039 * @pfp: the unattached filter that is created
Tobias Klauser677a9fd2014-06-24 15:33:21 +02001040 * @fprog: the filter program
Jiri Pirko302d6632012-03-31 11:01:19 +00001041 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001042 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001043 * sanity checks on it to make sure it does not explode on us later.
1044 * If an error occurs or there is insufficient memory for the filter
1045 * a negative errno code is returned. On success the return is zero.
1046 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001047int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001048{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001049 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001050 struct bpf_prog *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001051
1052 /* Make sure new filter is there and in the right amounts. */
1053 if (fprog->filter == NULL)
1054 return -EINVAL;
1055
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001056 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Jiri Pirko302d6632012-03-31 11:01:19 +00001057 if (!fp)
1058 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001059
Jiri Pirko302d6632012-03-31 11:01:19 +00001060 memcpy(fp->insns, fprog->filter, fsize);
1061
Jiri Pirko302d6632012-03-31 11:01:19 +00001062 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001063 /* Since unattached filters are not copied back to user
1064 * space through sk_get_filter(), we do not need to hold
1065 * a copy here, and can spare us the work.
1066 */
1067 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001068
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001069 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001070 * memory in case something goes wrong.
1071 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001072 fp = bpf_prepare_filter(fp, NULL);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001073 if (IS_ERR(fp))
1074 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001075
1076 *pfp = fp;
1077 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001078}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001079EXPORT_SYMBOL_GPL(bpf_prog_create);
Jiri Pirko302d6632012-03-31 11:01:19 +00001080
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001081/**
1082 * bpf_prog_create_from_user - create an unattached filter from user buffer
1083 * @pfp: the unattached filter that is created
1084 * @fprog: the filter program
1085 * @trans: post-classic verifier transformation handler
1086 *
1087 * This function effectively does the same as bpf_prog_create(), only
1088 * that it builds up its insns buffer from user space provided buffer.
1089 * It also allows for passing a bpf_aux_classic_check_t handler.
1090 */
1091int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1092 bpf_aux_classic_check_t trans)
1093{
1094 unsigned int fsize = bpf_classic_proglen(fprog);
1095 struct bpf_prog *fp;
1096
1097 /* Make sure new filter is there and in the right amounts. */
1098 if (fprog->filter == NULL)
1099 return -EINVAL;
1100
1101 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1102 if (!fp)
1103 return -ENOMEM;
1104
1105 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1106 __bpf_prog_free(fp);
1107 return -EFAULT;
1108 }
1109
1110 fp->len = fprog->len;
1111 /* Since unattached filters are not copied back to user
1112 * space through sk_get_filter(), we do not need to hold
1113 * a copy here, and can spare us the work.
1114 */
1115 fp->orig_prog = NULL;
1116
1117 /* bpf_prepare_filter() already takes care of freeing
1118 * memory in case something goes wrong.
1119 */
1120 fp = bpf_prepare_filter(fp, trans);
1121 if (IS_ERR(fp))
1122 return PTR_ERR(fp);
1123
1124 *pfp = fp;
1125 return 0;
1126}
David S. Miller2ea273d2015-08-17 14:37:06 -07001127EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001128
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001129void bpf_prog_destroy(struct bpf_prog *fp)
Jiri Pirko302d6632012-03-31 11:01:19 +00001130{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001131 __bpf_prog_release(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001132}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001133EXPORT_SYMBOL_GPL(bpf_prog_destroy);
Jiri Pirko302d6632012-03-31 11:01:19 +00001134
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001135static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1136{
1137 struct sk_filter *fp, *old_fp;
1138
1139 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1140 if (!fp)
1141 return -ENOMEM;
1142
1143 fp->prog = prog;
1144 atomic_set(&fp->refcnt, 0);
1145
1146 if (!sk_filter_charge(sk, fp)) {
1147 kfree(fp);
1148 return -ENOMEM;
1149 }
1150
1151 old_fp = rcu_dereference_protected(sk->sk_filter,
1152 sock_owned_by_user(sk));
1153 rcu_assign_pointer(sk->sk_filter, fp);
1154
1155 if (old_fp)
1156 sk_filter_uncharge(sk, old_fp);
1157
1158 return 0;
1159}
1160
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001161/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 * sk_attach_filter - attach a socket filter
1163 * @fprog: the filter program
1164 * @sk: the socket to use
1165 *
1166 * Attach the user's filter code. We first run some sanity checks on
1167 * it to make sure it does not explode on us later. If an error
1168 * occurs or there is insufficient memory for the filter a negative
1169 * errno code is returned. On success the return is zero.
1170 */
1171int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1172{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001173 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001174 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1175 struct bpf_prog *prog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int err;
1177
Vincent Bernatd59577b2013-01-16 22:55:49 +01001178 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1179 return -EPERM;
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -08001182 if (fprog->filter == NULL)
1183 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001185 prog = bpf_prog_alloc(bpf_fsize, 0);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001186 if (!prog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001188
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001189 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
Sasha Levinc0d13792014-09-13 00:06:30 -04001190 __bpf_prog_free(prog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return -EFAULT;
1192 }
1193
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001194 prog->len = fprog->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001196 err = bpf_prog_store_orig_filter(prog, fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001197 if (err) {
Sasha Levinc0d13792014-09-13 00:06:30 -04001198 __bpf_prog_free(prog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001199 return -ENOMEM;
1200 }
1201
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001202 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001203 * memory in case something goes wrong.
1204 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001205 prog = bpf_prepare_filter(prog, NULL);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001206 if (IS_ERR(prog))
1207 return PTR_ERR(prog);
1208
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001209 err = __sk_attach_prog(prog, sk);
1210 if (err < 0) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001211 __bpf_prog_release(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001212 return err;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001213 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001214
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +00001217EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001219int sk_attach_bpf(u32 ufd, struct sock *sk)
1220{
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001221 struct bpf_prog *prog;
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001222 int err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001223
1224 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1225 return -EPERM;
1226
1227 prog = bpf_prog_get(ufd);
Alexei Starovoitov198bf1b2014-12-10 20:14:55 -08001228 if (IS_ERR(prog))
1229 return PTR_ERR(prog);
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001230
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001231 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001232 bpf_prog_put(prog);
1233 return -EINVAL;
1234 }
1235
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001236 err = __sk_attach_prog(prog, sk);
1237 if (err < 0) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001238 bpf_prog_put(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001239 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001240 }
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001241
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001242 return 0;
1243}
1244
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001245#define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1)
1246
1247static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001248{
1249 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001250 int offset = (int) r2;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001251 void *from = (void *) (long) r3;
1252 unsigned int len = (unsigned int) r4;
1253 char buf[16];
1254 void *ptr;
1255
1256 /* bpf verifier guarantees that:
1257 * 'from' pointer points to bpf program stack
1258 * 'len' bytes of it were initialized
1259 * 'len' > 0
1260 * 'skb' is a valid pointer to 'struct sk_buff'
1261 *
1262 * so check for invalid 'offset' and too large 'len'
1263 */
Alexei Starovoitova1661512015-04-15 12:55:45 -07001264 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001265 return -EFAULT;
1266
Alexei Starovoitova1661512015-04-15 12:55:45 -07001267 if (unlikely(skb_cloned(skb) &&
Alexei Starovoitov34312052015-06-04 10:11:53 -07001268 !skb_clone_writable(skb, offset + len)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001269 return -EFAULT;
1270
1271 ptr = skb_header_pointer(skb, offset, len, buf);
1272 if (unlikely(!ptr))
1273 return -EFAULT;
1274
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001275 if (BPF_RECOMPUTE_CSUM(flags))
1276 skb_postpull_rcsum(skb, ptr, len);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001277
1278 memcpy(ptr, from, len);
1279
1280 if (ptr == buf)
1281 /* skb_store_bits cannot return -EFAULT here */
1282 skb_store_bits(skb, offset, ptr, len);
1283
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001284 if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001285 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1286 return 0;
1287}
1288
1289const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1290 .func = bpf_skb_store_bytes,
1291 .gpl_only = false,
1292 .ret_type = RET_INTEGER,
1293 .arg1_type = ARG_PTR_TO_CTX,
1294 .arg2_type = ARG_ANYTHING,
1295 .arg3_type = ARG_PTR_TO_STACK,
1296 .arg4_type = ARG_CONST_STACK_SIZE,
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001297 .arg5_type = ARG_ANYTHING,
1298};
1299
1300#define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f)
1301#define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10)
1302
Alexei Starovoitova1661512015-04-15 12:55:45 -07001303static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001304{
1305 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001306 int offset = (int) r2;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001307 __sum16 sum, *ptr;
1308
Alexei Starovoitova1661512015-04-15 12:55:45 -07001309 if (unlikely((u32) offset > 0xffff))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001310 return -EFAULT;
1311
Alexei Starovoitova1661512015-04-15 12:55:45 -07001312 if (unlikely(skb_cloned(skb) &&
Alexei Starovoitov34312052015-06-04 10:11:53 -07001313 !skb_clone_writable(skb, offset + sizeof(sum))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001314 return -EFAULT;
1315
1316 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1317 if (unlikely(!ptr))
1318 return -EFAULT;
1319
1320 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1321 case 2:
1322 csum_replace2(ptr, from, to);
1323 break;
1324 case 4:
1325 csum_replace4(ptr, from, to);
1326 break;
1327 default:
1328 return -EINVAL;
1329 }
1330
1331 if (ptr == &sum)
1332 /* skb_store_bits guaranteed to not return -EFAULT here */
1333 skb_store_bits(skb, offset, ptr, sizeof(sum));
1334
1335 return 0;
1336}
1337
1338const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1339 .func = bpf_l3_csum_replace,
1340 .gpl_only = false,
1341 .ret_type = RET_INTEGER,
1342 .arg1_type = ARG_PTR_TO_CTX,
1343 .arg2_type = ARG_ANYTHING,
1344 .arg3_type = ARG_ANYTHING,
1345 .arg4_type = ARG_ANYTHING,
1346 .arg5_type = ARG_ANYTHING,
1347};
1348
Alexei Starovoitova1661512015-04-15 12:55:45 -07001349static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001350{
1351 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Tom Herbert4b048d62015-08-17 13:42:25 -07001352 bool is_pseudo = !!BPF_IS_PSEUDO_HEADER(flags);
Alexei Starovoitova1661512015-04-15 12:55:45 -07001353 int offset = (int) r2;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001354 __sum16 sum, *ptr;
1355
Alexei Starovoitova1661512015-04-15 12:55:45 -07001356 if (unlikely((u32) offset > 0xffff))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001357 return -EFAULT;
1358
Alexei Starovoitova1661512015-04-15 12:55:45 -07001359 if (unlikely(skb_cloned(skb) &&
Alexei Starovoitov34312052015-06-04 10:11:53 -07001360 !skb_clone_writable(skb, offset + sizeof(sum))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001361 return -EFAULT;
1362
1363 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1364 if (unlikely(!ptr))
1365 return -EFAULT;
1366
1367 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1368 case 2:
1369 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1370 break;
1371 case 4:
1372 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1373 break;
1374 default:
1375 return -EINVAL;
1376 }
1377
1378 if (ptr == &sum)
1379 /* skb_store_bits guaranteed to not return -EFAULT here */
1380 skb_store_bits(skb, offset, ptr, sizeof(sum));
1381
1382 return 0;
1383}
1384
1385const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1386 .func = bpf_l4_csum_replace,
1387 .gpl_only = false,
1388 .ret_type = RET_INTEGER,
1389 .arg1_type = ARG_PTR_TO_CTX,
1390 .arg2_type = ARG_ANYTHING,
1391 .arg3_type = ARG_ANYTHING,
1392 .arg4_type = ARG_ANYTHING,
1393 .arg5_type = ARG_ANYTHING,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001394};
1395
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001396#define BPF_IS_REDIRECT_INGRESS(flags) ((flags) & 1)
1397
1398static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1399{
1400 struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1401 struct net_device *dev;
1402
1403 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1404 if (unlikely(!dev))
1405 return -EINVAL;
1406
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001407 skb2 = skb_clone(skb, GFP_ATOMIC);
1408 if (unlikely(!skb2))
1409 return -ENOMEM;
1410
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001411 if (BPF_IS_REDIRECT_INGRESS(flags))
1412 return dev_forward_skb(dev, skb2);
1413
1414 skb2->dev = dev;
1415 return dev_queue_xmit(skb2);
1416}
1417
1418const struct bpf_func_proto bpf_clone_redirect_proto = {
1419 .func = bpf_clone_redirect,
1420 .gpl_only = false,
1421 .ret_type = RET_INTEGER,
1422 .arg1_type = ARG_PTR_TO_CTX,
1423 .arg2_type = ARG_ANYTHING,
1424 .arg3_type = ARG_ANYTHING,
1425};
1426
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001427struct redirect_info {
1428 u32 ifindex;
1429 u32 flags;
1430};
1431
1432static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1433static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1434{
1435 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1436
1437 ri->ifindex = ifindex;
1438 ri->flags = flags;
1439 return TC_ACT_REDIRECT;
1440}
1441
1442int skb_do_redirect(struct sk_buff *skb)
1443{
1444 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1445 struct net_device *dev;
1446
1447 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1448 ri->ifindex = 0;
1449 if (unlikely(!dev)) {
1450 kfree_skb(skb);
1451 return -EINVAL;
1452 }
1453
1454 if (BPF_IS_REDIRECT_INGRESS(ri->flags))
1455 return dev_forward_skb(dev, skb);
1456
1457 skb->dev = dev;
1458 return dev_queue_xmit(skb);
1459}
1460
1461const struct bpf_func_proto bpf_redirect_proto = {
1462 .func = bpf_redirect,
1463 .gpl_only = false,
1464 .ret_type = RET_INTEGER,
1465 .arg1_type = ARG_ANYTHING,
1466 .arg2_type = ARG_ANYTHING,
1467};
1468
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001469static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1470{
1471 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1472}
1473
1474static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1475 .func = bpf_get_cgroup_classid,
1476 .gpl_only = false,
1477 .ret_type = RET_INTEGER,
1478 .arg1_type = ARG_PTR_TO_CTX,
1479};
1480
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001481static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1482{
1483 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1484 __be16 vlan_proto = (__force __be16) r2;
1485
1486 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1487 vlan_proto != htons(ETH_P_8021AD)))
1488 vlan_proto = htons(ETH_P_8021Q);
1489
1490 return skb_vlan_push(skb, vlan_proto, vlan_tci);
1491}
1492
1493const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1494 .func = bpf_skb_vlan_push,
1495 .gpl_only = false,
1496 .ret_type = RET_INTEGER,
1497 .arg1_type = ARG_PTR_TO_CTX,
1498 .arg2_type = ARG_ANYTHING,
1499 .arg3_type = ARG_ANYTHING,
1500};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001501EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001502
1503static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1504{
1505 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1506
1507 return skb_vlan_pop(skb);
1508}
1509
1510const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1511 .func = bpf_skb_vlan_pop,
1512 .gpl_only = false,
1513 .ret_type = RET_INTEGER,
1514 .arg1_type = ARG_PTR_TO_CTX,
1515};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001516EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001517
1518bool bpf_helper_changes_skb_data(void *func)
1519{
1520 if (func == bpf_skb_vlan_push)
1521 return true;
1522 if (func == bpf_skb_vlan_pop)
1523 return true;
1524 return false;
1525}
1526
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001527static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1528{
1529 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1530 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
Jiri Benc61adedf2015-08-20 13:56:25 +02001531 struct ip_tunnel_info *info = skb_tunnel_info(skb);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001532
1533 if (unlikely(size != sizeof(struct bpf_tunnel_key) || flags || !info))
1534 return -EINVAL;
Jiri Benc7f9562a2015-08-28 20:48:20 +02001535 if (ip_tunnel_info_af(info) != AF_INET)
1536 return -EINVAL;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001537
1538 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001539 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001540
1541 return 0;
1542}
1543
1544const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
1545 .func = bpf_skb_get_tunnel_key,
1546 .gpl_only = false,
1547 .ret_type = RET_INTEGER,
1548 .arg1_type = ARG_PTR_TO_CTX,
1549 .arg2_type = ARG_PTR_TO_STACK,
1550 .arg3_type = ARG_CONST_STACK_SIZE,
1551 .arg4_type = ARG_ANYTHING,
1552};
1553
1554static struct metadata_dst __percpu *md_dst;
1555
1556static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1557{
1558 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1559 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
1560 struct metadata_dst *md = this_cpu_ptr(md_dst);
1561 struct ip_tunnel_info *info;
1562
1563 if (unlikely(size != sizeof(struct bpf_tunnel_key) || flags))
1564 return -EINVAL;
1565
1566 skb_dst_drop(skb);
1567 dst_hold((struct dst_entry *) md);
1568 skb_dst_set(skb, (struct dst_entry *) md);
1569
1570 info = &md->u.tun_info;
1571 info->mode = IP_TUNNEL_INFO_TX;
Alexei Starovoitov1dd34b52015-08-26 15:57:38 -07001572 info->key.tun_flags = TUNNEL_KEY;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001573 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001574 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001575
1576 return 0;
1577}
1578
1579const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
1580 .func = bpf_skb_set_tunnel_key,
1581 .gpl_only = false,
1582 .ret_type = RET_INTEGER,
1583 .arg1_type = ARG_PTR_TO_CTX,
1584 .arg2_type = ARG_PTR_TO_STACK,
1585 .arg3_type = ARG_CONST_STACK_SIZE,
1586 .arg4_type = ARG_ANYTHING,
1587};
1588
1589static const struct bpf_func_proto *bpf_get_skb_set_tunnel_key_proto(void)
1590{
1591 if (!md_dst) {
1592 /* race is not possible, since it's called from
1593 * verifier that is holding verifier mutex
1594 */
1595 md_dst = metadata_dst_alloc_percpu(0, GFP_KERNEL);
1596 if (!md_dst)
1597 return NULL;
1598 }
1599 return &bpf_skb_set_tunnel_key_proto;
1600}
1601
Daniel Borkmannd4052c42015-03-01 12:31:45 +01001602static const struct bpf_func_proto *
1603sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001604{
1605 switch (func_id) {
1606 case BPF_FUNC_map_lookup_elem:
1607 return &bpf_map_lookup_elem_proto;
1608 case BPF_FUNC_map_update_elem:
1609 return &bpf_map_update_elem_proto;
1610 case BPF_FUNC_map_delete_elem:
1611 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001612 case BPF_FUNC_get_prandom_u32:
1613 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001614 case BPF_FUNC_get_smp_processor_id:
1615 return &bpf_get_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001616 case BPF_FUNC_tail_call:
1617 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001618 case BPF_FUNC_ktime_get_ns:
1619 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001620 case BPF_FUNC_trace_printk:
1621 return bpf_get_trace_printk_proto();
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001622 default:
1623 return NULL;
1624 }
1625}
1626
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001627static const struct bpf_func_proto *
1628tc_cls_act_func_proto(enum bpf_func_id func_id)
1629{
1630 switch (func_id) {
1631 case BPF_FUNC_skb_store_bytes:
1632 return &bpf_skb_store_bytes_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001633 case BPF_FUNC_l3_csum_replace:
1634 return &bpf_l3_csum_replace_proto;
1635 case BPF_FUNC_l4_csum_replace:
1636 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001637 case BPF_FUNC_clone_redirect:
1638 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001639 case BPF_FUNC_get_cgroup_classid:
1640 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001641 case BPF_FUNC_skb_vlan_push:
1642 return &bpf_skb_vlan_push_proto;
1643 case BPF_FUNC_skb_vlan_pop:
1644 return &bpf_skb_vlan_pop_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001645 case BPF_FUNC_skb_get_tunnel_key:
1646 return &bpf_skb_get_tunnel_key_proto;
1647 case BPF_FUNC_skb_set_tunnel_key:
1648 return bpf_get_skb_set_tunnel_key_proto();
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001649 case BPF_FUNC_redirect:
1650 return &bpf_redirect_proto;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001651 default:
1652 return sk_filter_func_proto(func_id);
1653 }
1654}
1655
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001656static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001657{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001658 /* check bounds */
1659 if (off < 0 || off >= sizeof(struct __sk_buff))
1660 return false;
1661
1662 /* disallow misaligned access */
1663 if (off % size != 0)
1664 return false;
1665
1666 /* all __sk_buff fields are __u32 */
1667 if (size != 4)
1668 return false;
1669
1670 return true;
1671}
1672
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001673static bool sk_filter_is_valid_access(int off, int size,
1674 enum bpf_access_type type)
1675{
Daniel Borkmann045efa82015-09-15 23:05:42 -07001676 if (off == offsetof(struct __sk_buff, tc_classid))
1677 return false;
1678
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001679 if (type == BPF_WRITE) {
1680 switch (off) {
1681 case offsetof(struct __sk_buff, cb[0]) ...
1682 offsetof(struct __sk_buff, cb[4]):
1683 break;
1684 default:
1685 return false;
1686 }
1687 }
1688
1689 return __is_valid_access(off, size, type);
1690}
1691
1692static bool tc_cls_act_is_valid_access(int off, int size,
1693 enum bpf_access_type type)
1694{
Daniel Borkmann045efa82015-09-15 23:05:42 -07001695 if (off == offsetof(struct __sk_buff, tc_classid))
1696 return type == BPF_WRITE ? true : false;
1697
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001698 if (type == BPF_WRITE) {
1699 switch (off) {
1700 case offsetof(struct __sk_buff, mark):
1701 case offsetof(struct __sk_buff, tc_index):
1702 case offsetof(struct __sk_buff, cb[0]) ...
1703 offsetof(struct __sk_buff, cb[4]):
1704 break;
1705 default:
1706 return false;
1707 }
1708 }
1709 return __is_valid_access(off, size, type);
1710}
1711
1712static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
1713 int src_reg, int ctx_off,
1714 struct bpf_insn *insn_buf)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001715{
1716 struct bpf_insn *insn = insn_buf;
1717
1718 switch (ctx_off) {
1719 case offsetof(struct __sk_buff, len):
1720 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1721
1722 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1723 offsetof(struct sk_buff, len));
1724 break;
1725
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01001726 case offsetof(struct __sk_buff, protocol):
1727 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1728
1729 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1730 offsetof(struct sk_buff, protocol));
1731 break;
1732
Michal Sekletar27cd5452015-03-24 14:48:41 +01001733 case offsetof(struct __sk_buff, vlan_proto):
1734 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1735
1736 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1737 offsetof(struct sk_buff, vlan_proto));
1738 break;
1739
Daniel Borkmannbcad5712015-04-03 20:52:24 +02001740 case offsetof(struct __sk_buff, priority):
1741 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1742
1743 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1744 offsetof(struct sk_buff, priority));
1745 break;
1746
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07001747 case offsetof(struct __sk_buff, ingress_ifindex):
1748 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
1749
1750 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1751 offsetof(struct sk_buff, skb_iif));
1752 break;
1753
1754 case offsetof(struct __sk_buff, ifindex):
1755 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1756
1757 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
1758 dst_reg, src_reg,
1759 offsetof(struct sk_buff, dev));
1760 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
1761 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
1762 offsetof(struct net_device, ifindex));
1763 break;
1764
Daniel Borkmannba7591d2015-08-01 00:46:29 +02001765 case offsetof(struct __sk_buff, hash):
1766 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1767
1768 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1769 offsetof(struct sk_buff, hash));
1770 break;
1771
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001772 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001773 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1774
1775 if (type == BPF_WRITE)
1776 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
1777 offsetof(struct sk_buff, mark));
1778 else
1779 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1780 offsetof(struct sk_buff, mark));
1781 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001782
1783 case offsetof(struct __sk_buff, pkt_type):
1784 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1785
1786 case offsetof(struct __sk_buff, queue_mapping):
1787 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07001788
Alexei Starovoitovc2497392015-03-16 18:06:02 -07001789 case offsetof(struct __sk_buff, vlan_present):
1790 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1791 dst_reg, src_reg, insn);
1792
1793 case offsetof(struct __sk_buff, vlan_tci):
1794 return convert_skb_access(SKF_AD_VLAN_TAG,
1795 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001796
1797 case offsetof(struct __sk_buff, cb[0]) ...
1798 offsetof(struct __sk_buff, cb[4]):
1799 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
1800
1801 ctx_off -= offsetof(struct __sk_buff, cb[0]);
1802 ctx_off += offsetof(struct sk_buff, cb);
1803 ctx_off += offsetof(struct qdisc_skb_cb, data);
1804 if (type == BPF_WRITE)
1805 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
1806 else
1807 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
1808 break;
1809
Daniel Borkmann045efa82015-09-15 23:05:42 -07001810 case offsetof(struct __sk_buff, tc_classid):
1811 ctx_off -= offsetof(struct __sk_buff, tc_classid);
1812 ctx_off += offsetof(struct sk_buff, cb);
1813 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
1814 WARN_ON(type != BPF_WRITE);
1815 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
1816 break;
1817
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001818 case offsetof(struct __sk_buff, tc_index):
1819#ifdef CONFIG_NET_SCHED
1820 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
1821
1822 if (type == BPF_WRITE)
1823 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
1824 offsetof(struct sk_buff, tc_index));
1825 else
1826 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1827 offsetof(struct sk_buff, tc_index));
1828 break;
1829#else
1830 if (type == BPF_WRITE)
1831 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
1832 else
1833 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
1834 break;
1835#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001836 }
1837
1838 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001839}
1840
Daniel Borkmannd4052c42015-03-01 12:31:45 +01001841static const struct bpf_verifier_ops sk_filter_ops = {
1842 .get_func_proto = sk_filter_func_proto,
1843 .is_valid_access = sk_filter_is_valid_access,
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001844 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001845};
1846
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001847static const struct bpf_verifier_ops tc_cls_act_ops = {
1848 .get_func_proto = tc_cls_act_func_proto,
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001849 .is_valid_access = tc_cls_act_is_valid_access,
1850 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001851};
1852
Daniel Borkmannd4052c42015-03-01 12:31:45 +01001853static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1854 .ops = &sk_filter_ops,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001855 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1856};
1857
Daniel Borkmann96be4322015-03-01 12:31:46 +01001858static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001859 .ops = &tc_cls_act_ops,
Daniel Borkmann96be4322015-03-01 12:31:46 +01001860 .type = BPF_PROG_TYPE_SCHED_CLS,
1861};
1862
Daniel Borkmann94caee82015-03-20 15:11:11 +01001863static struct bpf_prog_type_list sched_act_type __read_mostly = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001864 .ops = &tc_cls_act_ops,
Daniel Borkmann94caee82015-03-20 15:11:11 +01001865 .type = BPF_PROG_TYPE_SCHED_ACT,
1866};
1867
Daniel Borkmannd4052c42015-03-01 12:31:45 +01001868static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001869{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01001870 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01001871 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01001872 bpf_register_prog_type(&sched_act_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01001873
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001874 return 0;
1875}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01001876late_initcall(register_sk_filter_ops);
1877
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001878int sk_detach_filter(struct sock *sk)
1879{
1880 int ret = -ENOENT;
1881 struct sk_filter *filter;
1882
Vincent Bernatd59577b2013-01-16 22:55:49 +01001883 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1884 return -EPERM;
1885
Eric Dumazetf91ff5b2010-09-27 06:07:30 +00001886 filter = rcu_dereference_protected(sk->sk_filter,
1887 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001888 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001889 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08001890 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001891 ret = 0;
1892 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001893
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001894 return ret;
1895}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +00001896EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001897
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001898int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1899 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001900{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001901 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001902 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001903 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001904
1905 lock_sock(sk);
1906 filter = rcu_dereference_protected(sk->sk_filter,
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001907 sock_owned_by_user(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001908 if (!filter)
1909 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001910
1911 /* We're copying the filter that has been originally attached,
1912 * so no conversion/decode needed anymore.
1913 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001914 fprog = filter->prog->orig_prog;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001915
1916 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001917 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001918 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001919 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001920
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001921 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001922 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001923 goto out;
1924
1925 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001926 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001927 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001928
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001929 /* Instead of bytes, the API requests to return the number
1930 * of filter blocks.
1931 */
1932 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001933out:
1934 release_sock(sk);
1935 return ret;
1936}