xref: /openbmc/linux/kernel/bpf/cgroup.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Functions to manage eBPF programs attached to cgroups
4  *
5  * Copyright (c) 2016 Daniel Mack
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/atomic.h>
10 #include <linux/cgroup.h>
11 #include <linux/filter.h>
12 #include <linux/slab.h>
13 #include <linux/sysctl.h>
14 #include <linux/string.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf-cgroup.h>
17 #include <linux/bpf_lsm.h>
18 #include <linux/bpf_verifier.h>
19 #include <net/sock.h>
20 #include <net/bpf_sk_storage.h>
21 
22 #include "../cgroup/cgroup-internal.h"
23 
24 DEFINE_STATIC_KEY_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_CGROUP_BPF_ATTACH_TYPE);
25 EXPORT_SYMBOL(cgroup_bpf_enabled_key);
26 
27 /*
28  * cgroup bpf destruction makes heavy use of work items and there can be a lot
29  * of concurrent destructions.  Use a separate workqueue so that cgroup bpf
30  * destruction work items don't end up filling up max_active of system_wq
31  * which may lead to deadlock.
32  */
33 static struct workqueue_struct *cgroup_bpf_destroy_wq;
34 
cgroup_bpf_wq_init(void)35 static int __init cgroup_bpf_wq_init(void)
36 {
37 	cgroup_bpf_destroy_wq = alloc_workqueue("cgroup_bpf_destroy", 0, 1);
38 	if (!cgroup_bpf_destroy_wq)
39 		panic("Failed to alloc workqueue for cgroup bpf destroy.\n");
40 	return 0;
41 }
42 core_initcall(cgroup_bpf_wq_init);
43 
44 /* __always_inline is necessary to prevent indirect call through run_prog
45  * function pointer.
46  */
47 static __always_inline int
bpf_prog_run_array_cg(const struct cgroup_bpf * cgrp,enum cgroup_bpf_attach_type atype,const void * ctx,bpf_prog_run_fn run_prog,int retval,u32 * ret_flags)48 bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
49 		      enum cgroup_bpf_attach_type atype,
50 		      const void *ctx, bpf_prog_run_fn run_prog,
51 		      int retval, u32 *ret_flags)
52 {
53 	const struct bpf_prog_array_item *item;
54 	const struct bpf_prog *prog;
55 	const struct bpf_prog_array *array;
56 	struct bpf_run_ctx *old_run_ctx;
57 	struct bpf_cg_run_ctx run_ctx;
58 	u32 func_ret;
59 
60 	run_ctx.retval = retval;
61 	migrate_disable();
62 	rcu_read_lock();
63 	array = rcu_dereference(cgrp->effective[atype]);
64 	item = &array->items[0];
65 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
66 	while ((prog = READ_ONCE(item->prog))) {
67 		run_ctx.prog_item = item;
68 		func_ret = run_prog(prog, ctx);
69 		if (ret_flags) {
70 			*(ret_flags) |= (func_ret >> 1);
71 			func_ret &= 1;
72 		}
73 		if (!func_ret && !IS_ERR_VALUE((long)run_ctx.retval))
74 			run_ctx.retval = -EPERM;
75 		item++;
76 	}
77 	bpf_reset_run_ctx(old_run_ctx);
78 	rcu_read_unlock();
79 	migrate_enable();
80 	return run_ctx.retval;
81 }
82 
__cgroup_bpf_run_lsm_sock(const void * ctx,const struct bpf_insn * insn)83 unsigned int __cgroup_bpf_run_lsm_sock(const void *ctx,
84 				       const struct bpf_insn *insn)
85 {
86 	const struct bpf_prog *shim_prog;
87 	struct sock *sk;
88 	struct cgroup *cgrp;
89 	int ret = 0;
90 	u64 *args;
91 
92 	args = (u64 *)ctx;
93 	sk = (void *)(unsigned long)args[0];
94 	/*shim_prog = container_of(insn, struct bpf_prog, insnsi);*/
95 	shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi));
96 
97 	cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
98 	if (likely(cgrp))
99 		ret = bpf_prog_run_array_cg(&cgrp->bpf,
100 					    shim_prog->aux->cgroup_atype,
101 					    ctx, bpf_prog_run, 0, NULL);
102 	return ret;
103 }
104 
__cgroup_bpf_run_lsm_socket(const void * ctx,const struct bpf_insn * insn)105 unsigned int __cgroup_bpf_run_lsm_socket(const void *ctx,
106 					 const struct bpf_insn *insn)
107 {
108 	const struct bpf_prog *shim_prog;
109 	struct socket *sock;
110 	struct cgroup *cgrp;
111 	int ret = 0;
112 	u64 *args;
113 
114 	args = (u64 *)ctx;
115 	sock = (void *)(unsigned long)args[0];
116 	/*shim_prog = container_of(insn, struct bpf_prog, insnsi);*/
117 	shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi));
118 
119 	cgrp = sock_cgroup_ptr(&sock->sk->sk_cgrp_data);
120 	if (likely(cgrp))
121 		ret = bpf_prog_run_array_cg(&cgrp->bpf,
122 					    shim_prog->aux->cgroup_atype,
123 					    ctx, bpf_prog_run, 0, NULL);
124 	return ret;
125 }
126 
__cgroup_bpf_run_lsm_current(const void * ctx,const struct bpf_insn * insn)127 unsigned int __cgroup_bpf_run_lsm_current(const void *ctx,
128 					  const struct bpf_insn *insn)
129 {
130 	const struct bpf_prog *shim_prog;
131 	struct cgroup *cgrp;
132 	int ret = 0;
133 
134 	/*shim_prog = container_of(insn, struct bpf_prog, insnsi);*/
135 	shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi));
136 
137 	/* We rely on trampoline's __bpf_prog_enter_lsm_cgroup to grab RCU read lock. */
138 	cgrp = task_dfl_cgroup(current);
139 	if (likely(cgrp))
140 		ret = bpf_prog_run_array_cg(&cgrp->bpf,
141 					    shim_prog->aux->cgroup_atype,
142 					    ctx, bpf_prog_run, 0, NULL);
143 	return ret;
144 }
145 
146 #ifdef CONFIG_BPF_LSM
147 struct cgroup_lsm_atype {
148 	u32 attach_btf_id;
149 	int refcnt;
150 };
151 
152 static struct cgroup_lsm_atype cgroup_lsm_atype[CGROUP_LSM_NUM];
153 
154 static enum cgroup_bpf_attach_type
bpf_cgroup_atype_find(enum bpf_attach_type attach_type,u32 attach_btf_id)155 bpf_cgroup_atype_find(enum bpf_attach_type attach_type, u32 attach_btf_id)
156 {
157 	int i;
158 
159 	lockdep_assert_held(&cgroup_mutex);
160 
161 	if (attach_type != BPF_LSM_CGROUP)
162 		return to_cgroup_bpf_attach_type(attach_type);
163 
164 	for (i = 0; i < ARRAY_SIZE(cgroup_lsm_atype); i++)
165 		if (cgroup_lsm_atype[i].attach_btf_id == attach_btf_id)
166 			return CGROUP_LSM_START + i;
167 
168 	for (i = 0; i < ARRAY_SIZE(cgroup_lsm_atype); i++)
169 		if (cgroup_lsm_atype[i].attach_btf_id == 0)
170 			return CGROUP_LSM_START + i;
171 
172 	return -E2BIG;
173 
174 }
175 
bpf_cgroup_atype_get(u32 attach_btf_id,int cgroup_atype)176 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype)
177 {
178 	int i = cgroup_atype - CGROUP_LSM_START;
179 
180 	lockdep_assert_held(&cgroup_mutex);
181 
182 	WARN_ON_ONCE(cgroup_lsm_atype[i].attach_btf_id &&
183 		     cgroup_lsm_atype[i].attach_btf_id != attach_btf_id);
184 
185 	cgroup_lsm_atype[i].attach_btf_id = attach_btf_id;
186 	cgroup_lsm_atype[i].refcnt++;
187 }
188 
bpf_cgroup_atype_put(int cgroup_atype)189 void bpf_cgroup_atype_put(int cgroup_atype)
190 {
191 	int i = cgroup_atype - CGROUP_LSM_START;
192 
193 	cgroup_lock();
194 	if (--cgroup_lsm_atype[i].refcnt <= 0)
195 		cgroup_lsm_atype[i].attach_btf_id = 0;
196 	WARN_ON_ONCE(cgroup_lsm_atype[i].refcnt < 0);
197 	cgroup_unlock();
198 }
199 #else
200 static enum cgroup_bpf_attach_type
bpf_cgroup_atype_find(enum bpf_attach_type attach_type,u32 attach_btf_id)201 bpf_cgroup_atype_find(enum bpf_attach_type attach_type, u32 attach_btf_id)
202 {
203 	if (attach_type != BPF_LSM_CGROUP)
204 		return to_cgroup_bpf_attach_type(attach_type);
205 	return -EOPNOTSUPP;
206 }
207 #endif /* CONFIG_BPF_LSM */
208 
cgroup_bpf_offline(struct cgroup * cgrp)209 void cgroup_bpf_offline(struct cgroup *cgrp)
210 {
211 	cgroup_get(cgrp);
212 	percpu_ref_kill(&cgrp->bpf.refcnt);
213 }
214 
bpf_cgroup_storages_free(struct bpf_cgroup_storage * storages[])215 static void bpf_cgroup_storages_free(struct bpf_cgroup_storage *storages[])
216 {
217 	enum bpf_cgroup_storage_type stype;
218 
219 	for_each_cgroup_storage_type(stype)
220 		bpf_cgroup_storage_free(storages[stype]);
221 }
222 
bpf_cgroup_storages_alloc(struct bpf_cgroup_storage * storages[],struct bpf_cgroup_storage * new_storages[],enum bpf_attach_type type,struct bpf_prog * prog,struct cgroup * cgrp)223 static int bpf_cgroup_storages_alloc(struct bpf_cgroup_storage *storages[],
224 				     struct bpf_cgroup_storage *new_storages[],
225 				     enum bpf_attach_type type,
226 				     struct bpf_prog *prog,
227 				     struct cgroup *cgrp)
228 {
229 	enum bpf_cgroup_storage_type stype;
230 	struct bpf_cgroup_storage_key key;
231 	struct bpf_map *map;
232 
233 	key.cgroup_inode_id = cgroup_id(cgrp);
234 	key.attach_type = type;
235 
236 	for_each_cgroup_storage_type(stype) {
237 		map = prog->aux->cgroup_storage[stype];
238 		if (!map)
239 			continue;
240 
241 		storages[stype] = cgroup_storage_lookup((void *)map, &key, false);
242 		if (storages[stype])
243 			continue;
244 
245 		storages[stype] = bpf_cgroup_storage_alloc(prog, stype);
246 		if (IS_ERR(storages[stype])) {
247 			bpf_cgroup_storages_free(new_storages);
248 			return -ENOMEM;
249 		}
250 
251 		new_storages[stype] = storages[stype];
252 	}
253 
254 	return 0;
255 }
256 
bpf_cgroup_storages_assign(struct bpf_cgroup_storage * dst[],struct bpf_cgroup_storage * src[])257 static void bpf_cgroup_storages_assign(struct bpf_cgroup_storage *dst[],
258 				       struct bpf_cgroup_storage *src[])
259 {
260 	enum bpf_cgroup_storage_type stype;
261 
262 	for_each_cgroup_storage_type(stype)
263 		dst[stype] = src[stype];
264 }
265 
bpf_cgroup_storages_link(struct bpf_cgroup_storage * storages[],struct cgroup * cgrp,enum bpf_attach_type attach_type)266 static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],
267 				     struct cgroup *cgrp,
268 				     enum bpf_attach_type attach_type)
269 {
270 	enum bpf_cgroup_storage_type stype;
271 
272 	for_each_cgroup_storage_type(stype)
273 		bpf_cgroup_storage_link(storages[stype], cgrp, attach_type);
274 }
275 
276 /* Called when bpf_cgroup_link is auto-detached from dying cgroup.
277  * It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It
278  * doesn't free link memory, which will eventually be done by bpf_link's
279  * release() callback, when its last FD is closed.
280  */
bpf_cgroup_link_auto_detach(struct bpf_cgroup_link * link)281 static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link)
282 {
283 	cgroup_put(link->cgroup);
284 	link->cgroup = NULL;
285 }
286 
287 /**
288  * cgroup_bpf_release() - put references of all bpf programs and
289  *                        release all cgroup bpf data
290  * @work: work structure embedded into the cgroup to modify
291  */
cgroup_bpf_release(struct work_struct * work)292 static void cgroup_bpf_release(struct work_struct *work)
293 {
294 	struct cgroup *p, *cgrp = container_of(work, struct cgroup,
295 					       bpf.release_work);
296 	struct bpf_prog_array *old_array;
297 	struct list_head *storages = &cgrp->bpf.storages;
298 	struct bpf_cgroup_storage *storage, *stmp;
299 
300 	unsigned int atype;
301 
302 	cgroup_lock();
303 
304 	for (atype = 0; atype < ARRAY_SIZE(cgrp->bpf.progs); atype++) {
305 		struct hlist_head *progs = &cgrp->bpf.progs[atype];
306 		struct bpf_prog_list *pl;
307 		struct hlist_node *pltmp;
308 
309 		hlist_for_each_entry_safe(pl, pltmp, progs, node) {
310 			hlist_del(&pl->node);
311 			if (pl->prog) {
312 				if (pl->prog->expected_attach_type == BPF_LSM_CGROUP)
313 					bpf_trampoline_unlink_cgroup_shim(pl->prog);
314 				bpf_prog_put(pl->prog);
315 			}
316 			if (pl->link) {
317 				if (pl->link->link.prog->expected_attach_type == BPF_LSM_CGROUP)
318 					bpf_trampoline_unlink_cgroup_shim(pl->link->link.prog);
319 				bpf_cgroup_link_auto_detach(pl->link);
320 			}
321 			kfree(pl);
322 			static_branch_dec(&cgroup_bpf_enabled_key[atype]);
323 		}
324 		old_array = rcu_dereference_protected(
325 				cgrp->bpf.effective[atype],
326 				lockdep_is_held(&cgroup_mutex));
327 		bpf_prog_array_free(old_array);
328 	}
329 
330 	list_for_each_entry_safe(storage, stmp, storages, list_cg) {
331 		bpf_cgroup_storage_unlink(storage);
332 		bpf_cgroup_storage_free(storage);
333 	}
334 
335 	cgroup_unlock();
336 
337 	for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
338 		cgroup_bpf_put(p);
339 
340 	percpu_ref_exit(&cgrp->bpf.refcnt);
341 	cgroup_put(cgrp);
342 }
343 
344 /**
345  * cgroup_bpf_release_fn() - callback used to schedule releasing
346  *                           of bpf cgroup data
347  * @ref: percpu ref counter structure
348  */
cgroup_bpf_release_fn(struct percpu_ref * ref)349 static void cgroup_bpf_release_fn(struct percpu_ref *ref)
350 {
351 	struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt);
352 
353 	INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release);
354 	queue_work(cgroup_bpf_destroy_wq, &cgrp->bpf.release_work);
355 }
356 
357 /* Get underlying bpf_prog of bpf_prog_list entry, regardless if it's through
358  * link or direct prog.
359  */
prog_list_prog(struct bpf_prog_list * pl)360 static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl)
361 {
362 	if (pl->prog)
363 		return pl->prog;
364 	if (pl->link)
365 		return pl->link->link.prog;
366 	return NULL;
367 }
368 
369 /* count number of elements in the list.
370  * it's slow but the list cannot be long
371  */
prog_list_length(struct hlist_head * head,int * preorder_cnt)372 static u32 prog_list_length(struct hlist_head *head, int *preorder_cnt)
373 {
374 	struct bpf_prog_list *pl;
375 	u32 cnt = 0;
376 
377 	hlist_for_each_entry(pl, head, node) {
378 		if (!prog_list_prog(pl))
379 			continue;
380 		if (preorder_cnt && (pl->flags & BPF_F_PREORDER))
381 			(*preorder_cnt)++;
382 		cnt++;
383 	}
384 	return cnt;
385 }
386 
387 /* if parent has non-overridable prog attached,
388  * disallow attaching new programs to the descendent cgroup.
389  * if parent has overridable or multi-prog, allow attaching
390  */
hierarchy_allows_attach(struct cgroup * cgrp,enum cgroup_bpf_attach_type atype)391 static bool hierarchy_allows_attach(struct cgroup *cgrp,
392 				    enum cgroup_bpf_attach_type atype)
393 {
394 	struct cgroup *p;
395 
396 	p = cgroup_parent(cgrp);
397 	if (!p)
398 		return true;
399 	do {
400 		u32 flags = p->bpf.flags[atype];
401 		u32 cnt;
402 
403 		if (flags & BPF_F_ALLOW_MULTI)
404 			return true;
405 		cnt = prog_list_length(&p->bpf.progs[atype], NULL);
406 		WARN_ON_ONCE(cnt > 1);
407 		if (cnt == 1)
408 			return !!(flags & BPF_F_ALLOW_OVERRIDE);
409 		p = cgroup_parent(p);
410 	} while (p);
411 	return true;
412 }
413 
414 /* compute a chain of effective programs for a given cgroup:
415  * start from the list of programs in this cgroup and add
416  * all parent programs.
417  * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
418  * to programs in this cgroup
419  */
compute_effective_progs(struct cgroup * cgrp,enum cgroup_bpf_attach_type atype,struct bpf_prog_array ** array)420 static int compute_effective_progs(struct cgroup *cgrp,
421 				   enum cgroup_bpf_attach_type atype,
422 				   struct bpf_prog_array **array)
423 {
424 	struct bpf_prog_array_item *item;
425 	struct bpf_prog_array *progs;
426 	struct bpf_prog_list *pl;
427 	struct cgroup *p = cgrp;
428 	int i, j, cnt = 0, preorder_cnt = 0, fstart, bstart, init_bstart;
429 
430 	/* count number of effective programs by walking parents */
431 	do {
432 		if (cnt == 0 || (p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
433 			cnt += prog_list_length(&p->bpf.progs[atype], &preorder_cnt);
434 		p = cgroup_parent(p);
435 	} while (p);
436 
437 	progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
438 	if (!progs)
439 		return -ENOMEM;
440 
441 	/* populate the array with effective progs */
442 	cnt = 0;
443 	p = cgrp;
444 	fstart = preorder_cnt;
445 	bstart = preorder_cnt - 1;
446 	do {
447 		if (cnt > 0 && !(p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
448 			continue;
449 
450 		init_bstart = bstart;
451 		hlist_for_each_entry(pl, &p->bpf.progs[atype], node) {
452 			if (!prog_list_prog(pl))
453 				continue;
454 
455 			if (pl->flags & BPF_F_PREORDER) {
456 				item = &progs->items[bstart];
457 				bstart--;
458 			} else {
459 				item = &progs->items[fstart];
460 				fstart++;
461 			}
462 			item->prog = prog_list_prog(pl);
463 			bpf_cgroup_storages_assign(item->cgroup_storage,
464 						   pl->storage);
465 			cnt++;
466 		}
467 
468 		/* reverse pre-ordering progs at this cgroup level */
469 		for (i = bstart + 1, j = init_bstart; i < j; i++, j--)
470 			swap(progs->items[i], progs->items[j]);
471 
472 	} while ((p = cgroup_parent(p)));
473 
474 	*array = progs;
475 	return 0;
476 }
477 
activate_effective_progs(struct cgroup * cgrp,enum cgroup_bpf_attach_type atype,struct bpf_prog_array * old_array)478 static void activate_effective_progs(struct cgroup *cgrp,
479 				     enum cgroup_bpf_attach_type atype,
480 				     struct bpf_prog_array *old_array)
481 {
482 	old_array = rcu_replace_pointer(cgrp->bpf.effective[atype], old_array,
483 					lockdep_is_held(&cgroup_mutex));
484 	/* free prog array after grace period, since __cgroup_bpf_run_*()
485 	 * might be still walking the array
486 	 */
487 	bpf_prog_array_free(old_array);
488 }
489 
490 /**
491  * cgroup_bpf_inherit() - inherit effective programs from parent
492  * @cgrp: the cgroup to modify
493  */
cgroup_bpf_inherit(struct cgroup * cgrp)494 int cgroup_bpf_inherit(struct cgroup *cgrp)
495 {
496 /* has to use marco instead of const int, since compiler thinks
497  * that array below is variable length
498  */
499 #define	NR ARRAY_SIZE(cgrp->bpf.effective)
500 	struct bpf_prog_array *arrays[NR] = {};
501 	struct cgroup *p;
502 	int ret, i;
503 
504 	ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0,
505 			      GFP_KERNEL);
506 	if (ret)
507 		return ret;
508 
509 	for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
510 		cgroup_bpf_get(p);
511 
512 	for (i = 0; i < NR; i++)
513 		INIT_HLIST_HEAD(&cgrp->bpf.progs[i]);
514 
515 	INIT_LIST_HEAD(&cgrp->bpf.storages);
516 
517 	for (i = 0; i < NR; i++)
518 		if (compute_effective_progs(cgrp, i, &arrays[i]))
519 			goto cleanup;
520 
521 	for (i = 0; i < NR; i++)
522 		activate_effective_progs(cgrp, i, arrays[i]);
523 
524 	return 0;
525 cleanup:
526 	for (i = 0; i < NR; i++)
527 		bpf_prog_array_free(arrays[i]);
528 
529 	for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
530 		cgroup_bpf_put(p);
531 
532 	percpu_ref_exit(&cgrp->bpf.refcnt);
533 
534 	return -ENOMEM;
535 }
536 
update_effective_progs(struct cgroup * cgrp,enum cgroup_bpf_attach_type atype)537 static int update_effective_progs(struct cgroup *cgrp,
538 				  enum cgroup_bpf_attach_type atype)
539 {
540 	struct cgroup_subsys_state *css;
541 	int err;
542 
543 	/* allocate and recompute effective prog arrays */
544 	css_for_each_descendant_pre(css, &cgrp->self) {
545 		struct cgroup *desc = container_of(css, struct cgroup, self);
546 
547 		if (percpu_ref_is_zero(&desc->bpf.refcnt))
548 			continue;
549 
550 		err = compute_effective_progs(desc, atype, &desc->bpf.inactive);
551 		if (err)
552 			goto cleanup;
553 	}
554 
555 	/* all allocations were successful. Activate all prog arrays */
556 	css_for_each_descendant_pre(css, &cgrp->self) {
557 		struct cgroup *desc = container_of(css, struct cgroup, self);
558 
559 		if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
560 			if (unlikely(desc->bpf.inactive)) {
561 				bpf_prog_array_free(desc->bpf.inactive);
562 				desc->bpf.inactive = NULL;
563 			}
564 			continue;
565 		}
566 
567 		activate_effective_progs(desc, atype, desc->bpf.inactive);
568 		desc->bpf.inactive = NULL;
569 	}
570 
571 	return 0;
572 
573 cleanup:
574 	/* oom while computing effective. Free all computed effective arrays
575 	 * since they were not activated
576 	 */
577 	css_for_each_descendant_pre(css, &cgrp->self) {
578 		struct cgroup *desc = container_of(css, struct cgroup, self);
579 
580 		bpf_prog_array_free(desc->bpf.inactive);
581 		desc->bpf.inactive = NULL;
582 	}
583 
584 	return err;
585 }
586 
587 #define BPF_CGROUP_MAX_PROGS 64
588 
find_attach_entry(struct hlist_head * progs,struct bpf_prog * prog,struct bpf_cgroup_link * link,struct bpf_prog * replace_prog,bool allow_multi)589 static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,
590 					       struct bpf_prog *prog,
591 					       struct bpf_cgroup_link *link,
592 					       struct bpf_prog *replace_prog,
593 					       bool allow_multi)
594 {
595 	struct bpf_prog_list *pl;
596 
597 	/* single-attach case */
598 	if (!allow_multi) {
599 		if (hlist_empty(progs))
600 			return NULL;
601 		return hlist_entry(progs->first, typeof(*pl), node);
602 	}
603 
604 	hlist_for_each_entry(pl, progs, node) {
605 		if (prog && pl->prog == prog && prog != replace_prog)
606 			/* disallow attaching the same prog twice */
607 			return ERR_PTR(-EINVAL);
608 		if (link && pl->link == link)
609 			/* disallow attaching the same link twice */
610 			return ERR_PTR(-EINVAL);
611 	}
612 
613 	/* direct prog multi-attach w/ replacement case */
614 	if (replace_prog) {
615 		hlist_for_each_entry(pl, progs, node) {
616 			if (pl->prog == replace_prog)
617 				/* a match found */
618 				return pl;
619 		}
620 		/* prog to replace not found for cgroup */
621 		return ERR_PTR(-ENOENT);
622 	}
623 
624 	return NULL;
625 }
626 
627 /**
628  * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and
629  *                         propagate the change to descendants
630  * @cgrp: The cgroup which descendants to traverse
631  * @prog: A program to attach
632  * @link: A link to attach
633  * @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set
634  * @type: Type of attach operation
635  * @flags: Option flags
636  *
637  * Exactly one of @prog or @link can be non-null.
638  * Must be called with cgroup_mutex held.
639  */
__cgroup_bpf_attach(struct cgroup * cgrp,struct bpf_prog * prog,struct bpf_prog * replace_prog,struct bpf_cgroup_link * link,enum bpf_attach_type type,u32 flags)640 static int __cgroup_bpf_attach(struct cgroup *cgrp,
641 			       struct bpf_prog *prog, struct bpf_prog *replace_prog,
642 			       struct bpf_cgroup_link *link,
643 			       enum bpf_attach_type type, u32 flags)
644 {
645 	u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI));
646 	struct bpf_prog *old_prog = NULL;
647 	struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
648 	struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
649 	struct bpf_prog *new_prog = prog ? : link->link.prog;
650 	enum cgroup_bpf_attach_type atype;
651 	struct bpf_prog_list *pl;
652 	struct hlist_head *progs;
653 	int err;
654 
655 	if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) ||
656 	    ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))
657 		/* invalid combination */
658 		return -EINVAL;
659 	if (link && (prog || replace_prog))
660 		/* only either link or prog/replace_prog can be specified */
661 		return -EINVAL;
662 	if (!!replace_prog != !!(flags & BPF_F_REPLACE))
663 		/* replace_prog implies BPF_F_REPLACE, and vice versa */
664 		return -EINVAL;
665 
666 	atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id);
667 	if (atype < 0)
668 		return -EINVAL;
669 
670 	progs = &cgrp->bpf.progs[atype];
671 
672 	if (!hierarchy_allows_attach(cgrp, atype))
673 		return -EPERM;
674 
675 	if (!hlist_empty(progs) && cgrp->bpf.flags[atype] != saved_flags)
676 		/* Disallow attaching non-overridable on top
677 		 * of existing overridable in this cgroup.
678 		 * Disallow attaching multi-prog if overridable or none
679 		 */
680 		return -EPERM;
681 
682 	if (prog_list_length(progs, NULL) >= BPF_CGROUP_MAX_PROGS)
683 		return -E2BIG;
684 
685 	pl = find_attach_entry(progs, prog, link, replace_prog,
686 			       flags & BPF_F_ALLOW_MULTI);
687 	if (IS_ERR(pl))
688 		return PTR_ERR(pl);
689 
690 	if (bpf_cgroup_storages_alloc(storage, new_storage, type,
691 				      prog ? : link->link.prog, cgrp))
692 		return -ENOMEM;
693 
694 	if (pl) {
695 		old_prog = pl->prog;
696 	} else {
697 		struct hlist_node *last = NULL;
698 
699 		pl = kmalloc(sizeof(*pl), GFP_KERNEL);
700 		if (!pl) {
701 			bpf_cgroup_storages_free(new_storage);
702 			return -ENOMEM;
703 		}
704 		if (hlist_empty(progs))
705 			hlist_add_head(&pl->node, progs);
706 		else
707 			hlist_for_each(last, progs) {
708 				if (last->next)
709 					continue;
710 				hlist_add_behind(&pl->node, last);
711 				break;
712 			}
713 	}
714 
715 	pl->prog = prog;
716 	pl->link = link;
717 	pl->flags = flags;
718 	bpf_cgroup_storages_assign(pl->storage, storage);
719 	cgrp->bpf.flags[atype] = saved_flags;
720 
721 	if (type == BPF_LSM_CGROUP) {
722 		err = bpf_trampoline_link_cgroup_shim(new_prog, atype);
723 		if (err)
724 			goto cleanup;
725 	}
726 
727 	err = update_effective_progs(cgrp, atype);
728 	if (err)
729 		goto cleanup_trampoline;
730 
731 	if (old_prog) {
732 		if (type == BPF_LSM_CGROUP)
733 			bpf_trampoline_unlink_cgroup_shim(old_prog);
734 		bpf_prog_put(old_prog);
735 	} else {
736 		static_branch_inc(&cgroup_bpf_enabled_key[atype]);
737 	}
738 	bpf_cgroup_storages_link(new_storage, cgrp, type);
739 	return 0;
740 
741 cleanup_trampoline:
742 	if (type == BPF_LSM_CGROUP)
743 		bpf_trampoline_unlink_cgroup_shim(new_prog);
744 
745 cleanup:
746 	if (old_prog) {
747 		pl->prog = old_prog;
748 		pl->link = NULL;
749 	}
750 	bpf_cgroup_storages_free(new_storage);
751 	if (!old_prog) {
752 		hlist_del(&pl->node);
753 		kfree(pl);
754 	}
755 	return err;
756 }
757 
cgroup_bpf_attach(struct cgroup * cgrp,struct bpf_prog * prog,struct bpf_prog * replace_prog,struct bpf_cgroup_link * link,enum bpf_attach_type type,u32 flags)758 static int cgroup_bpf_attach(struct cgroup *cgrp,
759 			     struct bpf_prog *prog, struct bpf_prog *replace_prog,
760 			     struct bpf_cgroup_link *link,
761 			     enum bpf_attach_type type,
762 			     u32 flags)
763 {
764 	int ret;
765 
766 	cgroup_lock();
767 	ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags);
768 	cgroup_unlock();
769 	return ret;
770 }
771 
772 /* Swap updated BPF program for given link in effective program arrays across
773  * all descendant cgroups. This function is guaranteed to succeed.
774  */
replace_effective_prog(struct cgroup * cgrp,enum cgroup_bpf_attach_type atype,struct bpf_cgroup_link * link)775 static void replace_effective_prog(struct cgroup *cgrp,
776 				   enum cgroup_bpf_attach_type atype,
777 				   struct bpf_cgroup_link *link)
778 {
779 	struct bpf_prog_array_item *item;
780 	struct cgroup_subsys_state *css;
781 	struct bpf_prog_array *progs;
782 	struct bpf_prog_list *pl;
783 	struct hlist_head *head;
784 	struct cgroup *cg;
785 	int pos;
786 
787 	css_for_each_descendant_pre(css, &cgrp->self) {
788 		struct cgroup *desc = container_of(css, struct cgroup, self);
789 
790 		if (percpu_ref_is_zero(&desc->bpf.refcnt))
791 			continue;
792 
793 		/* find position of link in effective progs array */
794 		for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
795 			if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
796 				continue;
797 
798 			head = &cg->bpf.progs[atype];
799 			hlist_for_each_entry(pl, head, node) {
800 				if (!prog_list_prog(pl))
801 					continue;
802 				if (pl->link == link)
803 					goto found;
804 				pos++;
805 			}
806 		}
807 found:
808 		BUG_ON(!cg);
809 		progs = rcu_dereference_protected(
810 				desc->bpf.effective[atype],
811 				lockdep_is_held(&cgroup_mutex));
812 		item = &progs->items[pos];
813 		WRITE_ONCE(item->prog, link->link.prog);
814 	}
815 }
816 
817 /**
818  * __cgroup_bpf_replace() - Replace link's program and propagate the change
819  *                          to descendants
820  * @cgrp: The cgroup which descendants to traverse
821  * @link: A link for which to replace BPF program
822  * @new_prog: &struct bpf_prog for the target BPF program with its refcnt
823  *            incremented
824  *
825  * Must be called with cgroup_mutex held.
826  */
__cgroup_bpf_replace(struct cgroup * cgrp,struct bpf_cgroup_link * link,struct bpf_prog * new_prog)827 static int __cgroup_bpf_replace(struct cgroup *cgrp,
828 				struct bpf_cgroup_link *link,
829 				struct bpf_prog *new_prog)
830 {
831 	enum cgroup_bpf_attach_type atype;
832 	struct bpf_prog *old_prog;
833 	struct bpf_prog_list *pl;
834 	struct hlist_head *progs;
835 	bool found = false;
836 
837 	atype = bpf_cgroup_atype_find(link->type, new_prog->aux->attach_btf_id);
838 	if (atype < 0)
839 		return -EINVAL;
840 
841 	progs = &cgrp->bpf.progs[atype];
842 
843 	if (link->link.prog->type != new_prog->type)
844 		return -EINVAL;
845 
846 	hlist_for_each_entry(pl, progs, node) {
847 		if (pl->link == link) {
848 			found = true;
849 			break;
850 		}
851 	}
852 	if (!found)
853 		return -ENOENT;
854 
855 	old_prog = xchg(&link->link.prog, new_prog);
856 	replace_effective_prog(cgrp, atype, link);
857 	bpf_prog_put(old_prog);
858 	return 0;
859 }
860 
cgroup_bpf_replace(struct bpf_link * link,struct bpf_prog * new_prog,struct bpf_prog * old_prog)861 static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
862 			      struct bpf_prog *old_prog)
863 {
864 	struct bpf_cgroup_link *cg_link;
865 	int ret;
866 
867 	cg_link = container_of(link, struct bpf_cgroup_link, link);
868 
869 	cgroup_lock();
870 	/* link might have been auto-released by dying cgroup, so fail */
871 	if (!cg_link->cgroup) {
872 		ret = -ENOLINK;
873 		goto out_unlock;
874 	}
875 	if (old_prog && link->prog != old_prog) {
876 		ret = -EPERM;
877 		goto out_unlock;
878 	}
879 	ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
880 out_unlock:
881 	cgroup_unlock();
882 	return ret;
883 }
884 
find_detach_entry(struct hlist_head * progs,struct bpf_prog * prog,struct bpf_cgroup_link * link,bool allow_multi)885 static struct bpf_prog_list *find_detach_entry(struct hlist_head *progs,
886 					       struct bpf_prog *prog,
887 					       struct bpf_cgroup_link *link,
888 					       bool allow_multi)
889 {
890 	struct bpf_prog_list *pl;
891 
892 	if (!allow_multi) {
893 		if (hlist_empty(progs))
894 			/* report error when trying to detach and nothing is attached */
895 			return ERR_PTR(-ENOENT);
896 
897 		/* to maintain backward compatibility NONE and OVERRIDE cgroups
898 		 * allow detaching with invalid FD (prog==NULL) in legacy mode
899 		 */
900 		return hlist_entry(progs->first, typeof(*pl), node);
901 	}
902 
903 	if (!prog && !link)
904 		/* to detach MULTI prog the user has to specify valid FD
905 		 * of the program or link to be detached
906 		 */
907 		return ERR_PTR(-EINVAL);
908 
909 	/* find the prog or link and detach it */
910 	hlist_for_each_entry(pl, progs, node) {
911 		if (pl->prog == prog && pl->link == link)
912 			return pl;
913 	}
914 	return ERR_PTR(-ENOENT);
915 }
916 
917 /**
918  * purge_effective_progs() - After compute_effective_progs fails to alloc new
919  *                           cgrp->bpf.inactive table we can recover by
920  *                           recomputing the array in place.
921  *
922  * @cgrp: The cgroup which descendants to travers
923  * @prog: A program to detach or NULL
924  * @link: A link to detach or NULL
925  * @atype: Type of detach operation
926  */
purge_effective_progs(struct cgroup * cgrp,struct bpf_prog * prog,struct bpf_cgroup_link * link,enum cgroup_bpf_attach_type atype)927 static void purge_effective_progs(struct cgroup *cgrp, struct bpf_prog *prog,
928 				  struct bpf_cgroup_link *link,
929 				  enum cgroup_bpf_attach_type atype)
930 {
931 	struct cgroup_subsys_state *css;
932 	struct bpf_prog_array *progs;
933 	struct bpf_prog_list *pl;
934 	struct hlist_head *head;
935 	struct cgroup *cg;
936 	int pos;
937 
938 	/* recompute effective prog array in place */
939 	css_for_each_descendant_pre(css, &cgrp->self) {
940 		struct cgroup *desc = container_of(css, struct cgroup, self);
941 
942 		if (percpu_ref_is_zero(&desc->bpf.refcnt))
943 			continue;
944 
945 		/* find position of link or prog in effective progs array */
946 		for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
947 			if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
948 				continue;
949 
950 			head = &cg->bpf.progs[atype];
951 			hlist_for_each_entry(pl, head, node) {
952 				if (!prog_list_prog(pl))
953 					continue;
954 				if (pl->prog == prog && pl->link == link)
955 					goto found;
956 				pos++;
957 			}
958 		}
959 
960 		/* no link or prog match, skip the cgroup of this layer */
961 		continue;
962 found:
963 		progs = rcu_dereference_protected(
964 				desc->bpf.effective[atype],
965 				lockdep_is_held(&cgroup_mutex));
966 
967 		/* Remove the program from the array */
968 		WARN_ONCE(bpf_prog_array_delete_safe_at(progs, pos),
969 			  "Failed to purge a prog from array at index %d", pos);
970 	}
971 }
972 
973 /**
974  * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
975  *                         propagate the change to descendants
976  * @cgrp: The cgroup which descendants to traverse
977  * @prog: A program to detach or NULL
978  * @link: A link to detach or NULL
979  * @type: Type of detach operation
980  *
981  * At most one of @prog or @link can be non-NULL.
982  * Must be called with cgroup_mutex held.
983  */
__cgroup_bpf_detach(struct cgroup * cgrp,struct bpf_prog * prog,struct bpf_cgroup_link * link,enum bpf_attach_type type)984 static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
985 			       struct bpf_cgroup_link *link, enum bpf_attach_type type)
986 {
987 	enum cgroup_bpf_attach_type atype;
988 	struct bpf_prog *old_prog;
989 	struct bpf_prog_list *pl;
990 	struct hlist_head *progs;
991 	u32 attach_btf_id = 0;
992 	u32 flags;
993 
994 	if (prog)
995 		attach_btf_id = prog->aux->attach_btf_id;
996 	if (link)
997 		attach_btf_id = link->link.prog->aux->attach_btf_id;
998 
999 	atype = bpf_cgroup_atype_find(type, attach_btf_id);
1000 	if (atype < 0)
1001 		return -EINVAL;
1002 
1003 	progs = &cgrp->bpf.progs[atype];
1004 	flags = cgrp->bpf.flags[atype];
1005 
1006 	if (prog && link)
1007 		/* only one of prog or link can be specified */
1008 		return -EINVAL;
1009 
1010 	pl = find_detach_entry(progs, prog, link, flags & BPF_F_ALLOW_MULTI);
1011 	if (IS_ERR(pl))
1012 		return PTR_ERR(pl);
1013 
1014 	/* mark it deleted, so it's ignored while recomputing effective */
1015 	old_prog = pl->prog;
1016 	pl->prog = NULL;
1017 	pl->link = NULL;
1018 
1019 	if (update_effective_progs(cgrp, atype)) {
1020 		/* if update effective array failed replace the prog with a dummy prog*/
1021 		pl->prog = old_prog;
1022 		pl->link = link;
1023 		purge_effective_progs(cgrp, old_prog, link, atype);
1024 	}
1025 
1026 	/* now can actually delete it from this cgroup list */
1027 	hlist_del(&pl->node);
1028 
1029 	kfree(pl);
1030 	if (hlist_empty(progs))
1031 		/* last program was detached, reset flags to zero */
1032 		cgrp->bpf.flags[atype] = 0;
1033 	if (old_prog) {
1034 		if (type == BPF_LSM_CGROUP)
1035 			bpf_trampoline_unlink_cgroup_shim(old_prog);
1036 		bpf_prog_put(old_prog);
1037 	}
1038 	static_branch_dec(&cgroup_bpf_enabled_key[atype]);
1039 	return 0;
1040 }
1041 
cgroup_bpf_detach(struct cgroup * cgrp,struct bpf_prog * prog,enum bpf_attach_type type)1042 static int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
1043 			     enum bpf_attach_type type)
1044 {
1045 	int ret;
1046 
1047 	cgroup_lock();
1048 	ret = __cgroup_bpf_detach(cgrp, prog, NULL, type);
1049 	cgroup_unlock();
1050 	return ret;
1051 }
1052 
1053 /* Must be called with cgroup_mutex held to avoid races. */
__cgroup_bpf_query(struct cgroup * cgrp,const union bpf_attr * attr,union bpf_attr __user * uattr)1054 static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
1055 			      union bpf_attr __user *uattr)
1056 {
1057 	__u32 __user *prog_attach_flags = u64_to_user_ptr(attr->query.prog_attach_flags);
1058 	bool effective_query = attr->query.query_flags & BPF_F_QUERY_EFFECTIVE;
1059 	__u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
1060 	enum bpf_attach_type type = attr->query.attach_type;
1061 	enum cgroup_bpf_attach_type from_atype, to_atype;
1062 	enum cgroup_bpf_attach_type atype;
1063 	struct bpf_prog_array *effective;
1064 	int cnt, ret = 0, i;
1065 	int total_cnt = 0;
1066 	u32 flags;
1067 
1068 	if (effective_query && prog_attach_flags)
1069 		return -EINVAL;
1070 
1071 	if (type == BPF_LSM_CGROUP) {
1072 		if (!effective_query && attr->query.prog_cnt &&
1073 		    prog_ids && !prog_attach_flags)
1074 			return -EINVAL;
1075 
1076 		from_atype = CGROUP_LSM_START;
1077 		to_atype = CGROUP_LSM_END;
1078 		flags = 0;
1079 	} else {
1080 		from_atype = to_cgroup_bpf_attach_type(type);
1081 		if (from_atype < 0)
1082 			return -EINVAL;
1083 		to_atype = from_atype;
1084 		flags = cgrp->bpf.flags[from_atype];
1085 	}
1086 
1087 	for (atype = from_atype; atype <= to_atype; atype++) {
1088 		if (effective_query) {
1089 			effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
1090 							      lockdep_is_held(&cgroup_mutex));
1091 			total_cnt += bpf_prog_array_length(effective);
1092 		} else {
1093 			total_cnt += prog_list_length(&cgrp->bpf.progs[atype], NULL);
1094 		}
1095 	}
1096 
1097 	/* always output uattr->query.attach_flags as 0 during effective query */
1098 	flags = effective_query ? 0 : flags;
1099 	if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
1100 		return -EFAULT;
1101 	if (copy_to_user(&uattr->query.prog_cnt, &total_cnt, sizeof(total_cnt)))
1102 		return -EFAULT;
1103 	if (attr->query.prog_cnt == 0 || !prog_ids || !total_cnt)
1104 		/* return early if user requested only program count + flags */
1105 		return 0;
1106 
1107 	if (attr->query.prog_cnt < total_cnt) {
1108 		total_cnt = attr->query.prog_cnt;
1109 		ret = -ENOSPC;
1110 	}
1111 
1112 	for (atype = from_atype; atype <= to_atype && total_cnt; atype++) {
1113 		if (effective_query) {
1114 			effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
1115 							      lockdep_is_held(&cgroup_mutex));
1116 			cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);
1117 			ret = bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
1118 		} else {
1119 			struct hlist_head *progs;
1120 			struct bpf_prog_list *pl;
1121 			struct bpf_prog *prog;
1122 			u32 id;
1123 
1124 			progs = &cgrp->bpf.progs[atype];
1125 			cnt = min_t(int, prog_list_length(progs, NULL), total_cnt);
1126 			i = 0;
1127 			hlist_for_each_entry(pl, progs, node) {
1128 				prog = prog_list_prog(pl);
1129 				id = prog->aux->id;
1130 				if (copy_to_user(prog_ids + i, &id, sizeof(id)))
1131 					return -EFAULT;
1132 				if (++i == cnt)
1133 					break;
1134 			}
1135 
1136 			if (prog_attach_flags) {
1137 				flags = cgrp->bpf.flags[atype];
1138 
1139 				for (i = 0; i < cnt; i++)
1140 					if (copy_to_user(prog_attach_flags + i,
1141 							 &flags, sizeof(flags)))
1142 						return -EFAULT;
1143 				prog_attach_flags += cnt;
1144 			}
1145 		}
1146 
1147 		prog_ids += cnt;
1148 		total_cnt -= cnt;
1149 	}
1150 	return ret;
1151 }
1152 
cgroup_bpf_query(struct cgroup * cgrp,const union bpf_attr * attr,union bpf_attr __user * uattr)1153 static int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
1154 			    union bpf_attr __user *uattr)
1155 {
1156 	int ret;
1157 
1158 	cgroup_lock();
1159 	ret = __cgroup_bpf_query(cgrp, attr, uattr);
1160 	cgroup_unlock();
1161 	return ret;
1162 }
1163 
cgroup_bpf_prog_attach(const union bpf_attr * attr,enum bpf_prog_type ptype,struct bpf_prog * prog)1164 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
1165 			   enum bpf_prog_type ptype, struct bpf_prog *prog)
1166 {
1167 	struct bpf_prog *replace_prog = NULL;
1168 	struct cgroup *cgrp;
1169 	int ret;
1170 
1171 	cgrp = cgroup_get_from_fd(attr->target_fd);
1172 	if (IS_ERR(cgrp))
1173 		return PTR_ERR(cgrp);
1174 
1175 	if ((attr->attach_flags & BPF_F_ALLOW_MULTI) &&
1176 	    (attr->attach_flags & BPF_F_REPLACE)) {
1177 		replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype);
1178 		if (IS_ERR(replace_prog)) {
1179 			cgroup_put(cgrp);
1180 			return PTR_ERR(replace_prog);
1181 		}
1182 	}
1183 
1184 	ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL,
1185 				attr->attach_type, attr->attach_flags);
1186 
1187 	if (replace_prog)
1188 		bpf_prog_put(replace_prog);
1189 	cgroup_put(cgrp);
1190 	return ret;
1191 }
1192 
cgroup_bpf_prog_detach(const union bpf_attr * attr,enum bpf_prog_type ptype)1193 int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
1194 {
1195 	struct bpf_prog *prog;
1196 	struct cgroup *cgrp;
1197 	int ret;
1198 
1199 	cgrp = cgroup_get_from_fd(attr->target_fd);
1200 	if (IS_ERR(cgrp))
1201 		return PTR_ERR(cgrp);
1202 
1203 	prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1204 	if (IS_ERR(prog))
1205 		prog = NULL;
1206 
1207 	ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type);
1208 	if (prog)
1209 		bpf_prog_put(prog);
1210 
1211 	cgroup_put(cgrp);
1212 	return ret;
1213 }
1214 
bpf_cgroup_link_release(struct bpf_link * link)1215 static void bpf_cgroup_link_release(struct bpf_link *link)
1216 {
1217 	struct bpf_cgroup_link *cg_link =
1218 		container_of(link, struct bpf_cgroup_link, link);
1219 	struct cgroup *cg;
1220 
1221 	/* link might have been auto-detached by dying cgroup already,
1222 	 * in that case our work is done here
1223 	 */
1224 	if (!cg_link->cgroup)
1225 		return;
1226 
1227 	cgroup_lock();
1228 
1229 	/* re-check cgroup under lock again */
1230 	if (!cg_link->cgroup) {
1231 		cgroup_unlock();
1232 		return;
1233 	}
1234 
1235 	WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
1236 				    cg_link->type));
1237 	if (cg_link->type == BPF_LSM_CGROUP)
1238 		bpf_trampoline_unlink_cgroup_shim(cg_link->link.prog);
1239 
1240 	cg = cg_link->cgroup;
1241 	cg_link->cgroup = NULL;
1242 
1243 	cgroup_unlock();
1244 
1245 	cgroup_put(cg);
1246 }
1247 
bpf_cgroup_link_dealloc(struct bpf_link * link)1248 static void bpf_cgroup_link_dealloc(struct bpf_link *link)
1249 {
1250 	struct bpf_cgroup_link *cg_link =
1251 		container_of(link, struct bpf_cgroup_link, link);
1252 
1253 	kfree(cg_link);
1254 }
1255 
bpf_cgroup_link_detach(struct bpf_link * link)1256 static int bpf_cgroup_link_detach(struct bpf_link *link)
1257 {
1258 	bpf_cgroup_link_release(link);
1259 
1260 	return 0;
1261 }
1262 
bpf_cgroup_link_show_fdinfo(const struct bpf_link * link,struct seq_file * seq)1263 static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
1264 					struct seq_file *seq)
1265 {
1266 	struct bpf_cgroup_link *cg_link =
1267 		container_of(link, struct bpf_cgroup_link, link);
1268 	u64 cg_id = 0;
1269 
1270 	cgroup_lock();
1271 	if (cg_link->cgroup)
1272 		cg_id = cgroup_id(cg_link->cgroup);
1273 	cgroup_unlock();
1274 
1275 	seq_printf(seq,
1276 		   "cgroup_id:\t%llu\n"
1277 		   "attach_type:\t%d\n",
1278 		   cg_id,
1279 		   cg_link->type);
1280 }
1281 
bpf_cgroup_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)1282 static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
1283 					  struct bpf_link_info *info)
1284 {
1285 	struct bpf_cgroup_link *cg_link =
1286 		container_of(link, struct bpf_cgroup_link, link);
1287 	u64 cg_id = 0;
1288 
1289 	cgroup_lock();
1290 	if (cg_link->cgroup)
1291 		cg_id = cgroup_id(cg_link->cgroup);
1292 	cgroup_unlock();
1293 
1294 	info->cgroup.cgroup_id = cg_id;
1295 	info->cgroup.attach_type = cg_link->type;
1296 	return 0;
1297 }
1298 
1299 static const struct bpf_link_ops bpf_cgroup_link_lops = {
1300 	.release = bpf_cgroup_link_release,
1301 	.dealloc = bpf_cgroup_link_dealloc,
1302 	.detach = bpf_cgroup_link_detach,
1303 	.update_prog = cgroup_bpf_replace,
1304 	.show_fdinfo = bpf_cgroup_link_show_fdinfo,
1305 	.fill_link_info = bpf_cgroup_link_fill_link_info,
1306 };
1307 
cgroup_bpf_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)1308 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
1309 {
1310 	struct bpf_link_primer link_primer;
1311 	struct bpf_cgroup_link *link;
1312 	struct cgroup *cgrp;
1313 	int err;
1314 
1315 	if (attr->link_create.flags)
1316 		return -EINVAL;
1317 
1318 	cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
1319 	if (IS_ERR(cgrp))
1320 		return PTR_ERR(cgrp);
1321 
1322 	link = kzalloc(sizeof(*link), GFP_USER);
1323 	if (!link) {
1324 		err = -ENOMEM;
1325 		goto out_put_cgroup;
1326 	}
1327 	bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops,
1328 		      prog);
1329 	link->cgroup = cgrp;
1330 	link->type = attr->link_create.attach_type;
1331 
1332 	err = bpf_link_prime(&link->link, &link_primer);
1333 	if (err) {
1334 		kfree(link);
1335 		goto out_put_cgroup;
1336 	}
1337 
1338 	err = cgroup_bpf_attach(cgrp, NULL, NULL, link,
1339 				link->type, BPF_F_ALLOW_MULTI);
1340 	if (err) {
1341 		bpf_link_cleanup(&link_primer);
1342 		goto out_put_cgroup;
1343 	}
1344 
1345 	return bpf_link_settle(&link_primer);
1346 
1347 out_put_cgroup:
1348 	cgroup_put(cgrp);
1349 	return err;
1350 }
1351 
cgroup_bpf_prog_query(const union bpf_attr * attr,union bpf_attr __user * uattr)1352 int cgroup_bpf_prog_query(const union bpf_attr *attr,
1353 			  union bpf_attr __user *uattr)
1354 {
1355 	struct cgroup *cgrp;
1356 	int ret;
1357 
1358 	cgrp = cgroup_get_from_fd(attr->query.target_fd);
1359 	if (IS_ERR(cgrp))
1360 		return PTR_ERR(cgrp);
1361 
1362 	ret = cgroup_bpf_query(cgrp, attr, uattr);
1363 
1364 	cgroup_put(cgrp);
1365 	return ret;
1366 }
1367 
1368 /**
1369  * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
1370  * @sk: The socket sending or receiving traffic
1371  * @skb: The skb that is being sent or received
1372  * @atype: The type of program to be executed
1373  *
1374  * If no socket is passed, or the socket is not of type INET or INET6,
1375  * this function does nothing and returns 0.
1376  *
1377  * The program type passed in via @type must be suitable for network
1378  * filtering. No further check is performed to assert that.
1379  *
1380  * For egress packets, this function can return:
1381  *   NET_XMIT_SUCCESS    (0)	- continue with packet output
1382  *   NET_XMIT_DROP       (1)	- drop packet and notify TCP to call cwr
1383  *   NET_XMIT_CN         (2)	- continue with packet output and notify TCP
1384  *				  to call cwr
1385  *   -err			- drop packet
1386  *
1387  * For ingress packets, this function will return -EPERM if any
1388  * attached program was found and if it returned != 1 during execution.
1389  * Otherwise 0 is returned.
1390  */
__cgroup_bpf_run_filter_skb(struct sock * sk,struct sk_buff * skb,enum cgroup_bpf_attach_type atype)1391 int __cgroup_bpf_run_filter_skb(struct sock *sk,
1392 				struct sk_buff *skb,
1393 				enum cgroup_bpf_attach_type atype)
1394 {
1395 	unsigned int offset = skb->data - skb_network_header(skb);
1396 	struct sock *save_sk;
1397 	void *saved_data_end;
1398 	struct cgroup *cgrp;
1399 	int ret;
1400 
1401 	if (!sk || !sk_fullsock(sk))
1402 		return 0;
1403 
1404 	if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1405 		return 0;
1406 
1407 	cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1408 	save_sk = skb->sk;
1409 	skb->sk = sk;
1410 	__skb_push(skb, offset);
1411 
1412 	/* compute pointers for the bpf prog */
1413 	bpf_compute_and_save_data_end(skb, &saved_data_end);
1414 
1415 	if (atype == CGROUP_INET_EGRESS) {
1416 		u32 flags = 0;
1417 		bool cn;
1418 
1419 		ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, skb,
1420 					    __bpf_prog_run_save_cb, 0, &flags);
1421 
1422 		/* Return values of CGROUP EGRESS BPF programs are:
1423 		 *   0: drop packet
1424 		 *   1: keep packet
1425 		 *   2: drop packet and cn
1426 		 *   3: keep packet and cn
1427 		 *
1428 		 * The returned value is then converted to one of the NET_XMIT
1429 		 * or an error code that is then interpreted as drop packet
1430 		 * (and no cn):
1431 		 *   0: NET_XMIT_SUCCESS  skb should be transmitted
1432 		 *   1: NET_XMIT_DROP     skb should be dropped and cn
1433 		 *   2: NET_XMIT_CN       skb should be transmitted and cn
1434 		 *   3: -err              skb should be dropped
1435 		 */
1436 
1437 		cn = flags & BPF_RET_SET_CN;
1438 		if (ret && !IS_ERR_VALUE((long)ret))
1439 			ret = -EFAULT;
1440 		if (!ret)
1441 			ret = (cn ? NET_XMIT_CN : NET_XMIT_SUCCESS);
1442 		else
1443 			ret = (cn ? NET_XMIT_DROP : ret);
1444 	} else {
1445 		ret = bpf_prog_run_array_cg(&cgrp->bpf, atype,
1446 					    skb, __bpf_prog_run_save_cb, 0,
1447 					    NULL);
1448 		if (ret && !IS_ERR_VALUE((long)ret))
1449 			ret = -EFAULT;
1450 	}
1451 	bpf_restore_data_end(skb, saved_data_end);
1452 	__skb_pull(skb, offset);
1453 	skb->sk = save_sk;
1454 
1455 	return ret;
1456 }
1457 EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
1458 
1459 /**
1460  * __cgroup_bpf_run_filter_sk() - Run a program on a sock
1461  * @sk: sock structure to manipulate
1462  * @atype: The type of program to be executed
1463  *
1464  * socket is passed is expected to be of type INET or INET6.
1465  *
1466  * The program type passed in via @type must be suitable for sock
1467  * filtering. No further check is performed to assert that.
1468  *
1469  * This function will return %-EPERM if any if an attached program was found
1470  * and if it returned != 1 during execution. In all other cases, 0 is returned.
1471  */
__cgroup_bpf_run_filter_sk(struct sock * sk,enum cgroup_bpf_attach_type atype)1472 int __cgroup_bpf_run_filter_sk(struct sock *sk,
1473 			       enum cgroup_bpf_attach_type atype)
1474 {
1475 	struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1476 
1477 	return bpf_prog_run_array_cg(&cgrp->bpf, atype, sk, bpf_prog_run, 0,
1478 				     NULL);
1479 }
1480 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
1481 
1482 /**
1483  * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
1484  *                                       provided by user sockaddr
1485  * @sk: sock struct that will use sockaddr
1486  * @uaddr: sockaddr struct provided by user
1487  * @uaddrlen: Pointer to the size of the sockaddr struct provided by user. It is
1488  *            read-only for AF_INET[6] uaddr but can be modified for AF_UNIX
1489  *            uaddr.
1490  * @atype: The type of program to be executed
1491  * @t_ctx: Pointer to attach type specific context
1492  * @flags: Pointer to u32 which contains higher bits of BPF program
1493  *         return value (OR'ed together).
1494  *
1495  * socket is expected to be of type INET or INET6.
1496  *
1497  * This function will return %-EPERM if an attached program is found and
1498  * returned value != 1 during execution. In all other cases, 0 is returned.
1499  */
__cgroup_bpf_run_filter_sock_addr(struct sock * sk,struct sockaddr * uaddr,int * uaddrlen,enum cgroup_bpf_attach_type atype,void * t_ctx,u32 * flags)1500 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
1501 				      struct sockaddr *uaddr,
1502 				      int *uaddrlen,
1503 				      enum cgroup_bpf_attach_type atype,
1504 				      void *t_ctx,
1505 				      u32 *flags)
1506 {
1507 	struct bpf_sock_addr_kern ctx = {
1508 		.sk = sk,
1509 		.uaddr = uaddr,
1510 		.t_ctx = t_ctx,
1511 	};
1512 	struct sockaddr_storage unspec;
1513 	struct cgroup *cgrp;
1514 	int ret;
1515 
1516 	/* Check socket family since not all sockets represent network
1517 	 * endpoint (e.g. AF_UNIX).
1518 	 */
1519 	if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1520 		return 0;
1521 
1522 	if (!ctx.uaddr) {
1523 		memset(&unspec, 0, sizeof(unspec));
1524 		ctx.uaddr = (struct sockaddr *)&unspec;
1525 		ctx.uaddrlen = 0;
1526 	} else {
1527 		ctx.uaddrlen = *uaddrlen;
1528 	}
1529 
1530 	cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1531 	ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run,
1532 				    0, flags);
1533 
1534 	if (!ret && uaddr)
1535 		*uaddrlen = ctx.uaddrlen;
1536 
1537 	return ret;
1538 }
1539 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
1540 
1541 /**
1542  * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
1543  * @sk: socket to get cgroup from
1544  * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
1545  * sk with connection information (IP addresses, etc.) May not contain
1546  * cgroup info if it is a req sock.
1547  * @atype: The type of program to be executed
1548  *
1549  * socket passed is expected to be of type INET or INET6.
1550  *
1551  * The program type passed in via @type must be suitable for sock_ops
1552  * filtering. No further check is performed to assert that.
1553  *
1554  * This function will return %-EPERM if any if an attached program was found
1555  * and if it returned != 1 during execution. In all other cases, 0 is returned.
1556  */
__cgroup_bpf_run_filter_sock_ops(struct sock * sk,struct bpf_sock_ops_kern * sock_ops,enum cgroup_bpf_attach_type atype)1557 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
1558 				     struct bpf_sock_ops_kern *sock_ops,
1559 				     enum cgroup_bpf_attach_type atype)
1560 {
1561 	struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1562 
1563 	return bpf_prog_run_array_cg(&cgrp->bpf, atype, sock_ops, bpf_prog_run,
1564 				     0, NULL);
1565 }
1566 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
1567 
__cgroup_bpf_check_dev_permission(short dev_type,u32 major,u32 minor,short access,enum cgroup_bpf_attach_type atype)1568 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
1569 				      short access, enum cgroup_bpf_attach_type atype)
1570 {
1571 	struct cgroup *cgrp;
1572 	struct bpf_cgroup_dev_ctx ctx = {
1573 		.access_type = (access << 16) | dev_type,
1574 		.major = major,
1575 		.minor = minor,
1576 	};
1577 	int ret;
1578 
1579 	rcu_read_lock();
1580 	cgrp = task_dfl_cgroup(current);
1581 	ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0,
1582 				    NULL);
1583 	rcu_read_unlock();
1584 
1585 	return ret;
1586 }
1587 
BPF_CALL_2(bpf_get_local_storage,struct bpf_map *,map,u64,flags)1588 BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
1589 {
1590 	/* flags argument is not used now,
1591 	 * but provides an ability to extend the API.
1592 	 * verifier checks that its value is correct.
1593 	 */
1594 	enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
1595 	struct bpf_cgroup_storage *storage;
1596 	struct bpf_cg_run_ctx *ctx;
1597 	void *ptr;
1598 
1599 	/* get current cgroup storage from BPF run context */
1600 	ctx = container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
1601 	storage = ctx->prog_item->cgroup_storage[stype];
1602 
1603 	if (stype == BPF_CGROUP_STORAGE_SHARED)
1604 		ptr = &READ_ONCE(storage->buf)->data[0];
1605 	else
1606 		ptr = this_cpu_ptr(storage->percpu_buf);
1607 
1608 	return (unsigned long)ptr;
1609 }
1610 
1611 const struct bpf_func_proto bpf_get_local_storage_proto = {
1612 	.func		= bpf_get_local_storage,
1613 	.gpl_only	= false,
1614 	.ret_type	= RET_PTR_TO_MAP_VALUE,
1615 	.arg1_type	= ARG_CONST_MAP_PTR,
1616 	.arg2_type	= ARG_ANYTHING,
1617 };
1618 
BPF_CALL_0(bpf_get_retval)1619 BPF_CALL_0(bpf_get_retval)
1620 {
1621 	struct bpf_cg_run_ctx *ctx =
1622 		container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
1623 
1624 	return ctx->retval;
1625 }
1626 
1627 const struct bpf_func_proto bpf_get_retval_proto = {
1628 	.func		= bpf_get_retval,
1629 	.gpl_only	= false,
1630 	.ret_type	= RET_INTEGER,
1631 };
1632 
BPF_CALL_1(bpf_set_retval,int,retval)1633 BPF_CALL_1(bpf_set_retval, int, retval)
1634 {
1635 	struct bpf_cg_run_ctx *ctx =
1636 		container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
1637 
1638 	ctx->retval = retval;
1639 	return 0;
1640 }
1641 
1642 const struct bpf_func_proto bpf_set_retval_proto = {
1643 	.func		= bpf_set_retval,
1644 	.gpl_only	= false,
1645 	.ret_type	= RET_INTEGER,
1646 	.arg1_type	= ARG_ANYTHING,
1647 };
1648 
1649 static const struct bpf_func_proto *
cgroup_dev_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1650 cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1651 {
1652 	const struct bpf_func_proto *func_proto;
1653 
1654 	func_proto = cgroup_common_func_proto(func_id, prog);
1655 	if (func_proto)
1656 		return func_proto;
1657 
1658 	func_proto = cgroup_current_func_proto(func_id, prog);
1659 	if (func_proto)
1660 		return func_proto;
1661 
1662 	switch (func_id) {
1663 	case BPF_FUNC_perf_event_output:
1664 		return &bpf_event_output_data_proto;
1665 	default:
1666 		return bpf_base_func_proto(func_id);
1667 	}
1668 }
1669 
cgroup_dev_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)1670 static bool cgroup_dev_is_valid_access(int off, int size,
1671 				       enum bpf_access_type type,
1672 				       const struct bpf_prog *prog,
1673 				       struct bpf_insn_access_aux *info)
1674 {
1675 	const int size_default = sizeof(__u32);
1676 
1677 	if (type == BPF_WRITE)
1678 		return false;
1679 
1680 	if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
1681 		return false;
1682 	/* The verifier guarantees that size > 0. */
1683 	if (off % size != 0)
1684 		return false;
1685 
1686 	switch (off) {
1687 	case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
1688 		bpf_ctx_record_field_size(info, size_default);
1689 		if (!bpf_ctx_narrow_access_ok(off, size, size_default))
1690 			return false;
1691 		break;
1692 	default:
1693 		if (size != size_default)
1694 			return false;
1695 	}
1696 
1697 	return true;
1698 }
1699 
1700 const struct bpf_prog_ops cg_dev_prog_ops = {
1701 };
1702 
1703 const struct bpf_verifier_ops cg_dev_verifier_ops = {
1704 	.get_func_proto		= cgroup_dev_func_proto,
1705 	.is_valid_access	= cgroup_dev_is_valid_access,
1706 };
1707 
1708 /**
1709  * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
1710  *
1711  * @head: sysctl table header
1712  * @table: sysctl table
1713  * @write: sysctl is being read (= 0) or written (= 1)
1714  * @buf: pointer to buffer (in and out)
1715  * @pcount: value-result argument: value is size of buffer pointed to by @buf,
1716  *	result is size of @new_buf if program set new value, initial value
1717  *	otherwise
1718  * @ppos: value-result argument: value is position at which read from or write
1719  *	to sysctl is happening, result is new position if program overrode it,
1720  *	initial value otherwise
1721  * @atype: type of program to be executed
1722  *
1723  * Program is run when sysctl is being accessed, either read or written, and
1724  * can allow or deny such access.
1725  *
1726  * This function will return %-EPERM if an attached program is found and
1727  * returned value != 1 during execution. In all other cases 0 is returned.
1728  */
__cgroup_bpf_run_filter_sysctl(struct ctl_table_header * head,struct ctl_table * table,int write,char ** buf,size_t * pcount,loff_t * ppos,enum cgroup_bpf_attach_type atype)1729 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
1730 				   struct ctl_table *table, int write,
1731 				   char **buf, size_t *pcount, loff_t *ppos,
1732 				   enum cgroup_bpf_attach_type atype)
1733 {
1734 	struct bpf_sysctl_kern ctx = {
1735 		.head = head,
1736 		.table = table,
1737 		.write = write,
1738 		.ppos = ppos,
1739 		.cur_val = NULL,
1740 		.cur_len = PAGE_SIZE,
1741 		.new_val = NULL,
1742 		.new_len = 0,
1743 		.new_updated = 0,
1744 	};
1745 	struct cgroup *cgrp;
1746 	loff_t pos = 0;
1747 	int ret;
1748 
1749 	ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
1750 	if (!ctx.cur_val ||
1751 	    table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) {
1752 		/* Let BPF program decide how to proceed. */
1753 		ctx.cur_len = 0;
1754 	}
1755 
1756 	if (write && *buf && *pcount) {
1757 		/* BPF program should be able to override new value with a
1758 		 * buffer bigger than provided by user.
1759 		 */
1760 		ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
1761 		ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
1762 		if (ctx.new_val) {
1763 			memcpy(ctx.new_val, *buf, ctx.new_len);
1764 		} else {
1765 			/* Let BPF program decide how to proceed. */
1766 			ctx.new_len = 0;
1767 		}
1768 	}
1769 
1770 	rcu_read_lock();
1771 	cgrp = task_dfl_cgroup(current);
1772 	ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0,
1773 				    NULL);
1774 	rcu_read_unlock();
1775 
1776 	kfree(ctx.cur_val);
1777 
1778 	if (ret == 1 && ctx.new_updated) {
1779 		kfree(*buf);
1780 		*buf = ctx.new_val;
1781 		*pcount = ctx.new_len;
1782 	} else {
1783 		kfree(ctx.new_val);
1784 	}
1785 
1786 	return ret;
1787 }
1788 
1789 #ifdef CONFIG_NET
sockopt_alloc_buf(struct bpf_sockopt_kern * ctx,int max_optlen,struct bpf_sockopt_buf * buf)1790 static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
1791 			     struct bpf_sockopt_buf *buf)
1792 {
1793 	if (unlikely(max_optlen < 0))
1794 		return -EINVAL;
1795 
1796 	if (unlikely(max_optlen > PAGE_SIZE)) {
1797 		/* We don't expose optvals that are greater than PAGE_SIZE
1798 		 * to the BPF program.
1799 		 */
1800 		max_optlen = PAGE_SIZE;
1801 	}
1802 
1803 	if (max_optlen <= sizeof(buf->data)) {
1804 		/* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
1805 		 * bytes avoid the cost of kzalloc.
1806 		 */
1807 		ctx->optval = buf->data;
1808 		ctx->optval_end = ctx->optval + max_optlen;
1809 		return max_optlen;
1810 	}
1811 
1812 	ctx->optval = kzalloc(max_optlen, GFP_USER);
1813 	if (!ctx->optval)
1814 		return -ENOMEM;
1815 
1816 	ctx->optval_end = ctx->optval + max_optlen;
1817 
1818 	return max_optlen;
1819 }
1820 
sockopt_free_buf(struct bpf_sockopt_kern * ctx,struct bpf_sockopt_buf * buf)1821 static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
1822 			     struct bpf_sockopt_buf *buf)
1823 {
1824 	if (ctx->optval == buf->data)
1825 		return;
1826 	kfree(ctx->optval);
1827 }
1828 
sockopt_buf_allocated(struct bpf_sockopt_kern * ctx,struct bpf_sockopt_buf * buf)1829 static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
1830 				  struct bpf_sockopt_buf *buf)
1831 {
1832 	return ctx->optval != buf->data;
1833 }
1834 
__cgroup_bpf_run_filter_setsockopt(struct sock * sk,int * level,int * optname,sockptr_t optval,int * optlen,char ** kernel_optval)1835 int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
1836 				       int *optname, sockptr_t optval,
1837 				       int *optlen, char **kernel_optval)
1838 {
1839 	struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1840 	struct bpf_sockopt_buf buf = {};
1841 	struct bpf_sockopt_kern ctx = {
1842 		.sk = sk,
1843 		.level = *level,
1844 		.optname = *optname,
1845 	};
1846 	int ret, max_optlen;
1847 
1848 	/* Allocate a bit more than the initial user buffer for
1849 	 * BPF program. The canonical use case is overriding
1850 	 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
1851 	 */
1852 	max_optlen = max_t(int, 16, *optlen);
1853 	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1854 	if (max_optlen < 0)
1855 		return max_optlen;
1856 
1857 	ctx.optlen = *optlen;
1858 
1859 	if (copy_from_sockptr(ctx.optval, optval,
1860 			      min(*optlen, max_optlen))) {
1861 		ret = -EFAULT;
1862 		goto out;
1863 	}
1864 
1865 	lock_sock(sk);
1866 	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT,
1867 				    &ctx, bpf_prog_run, 0, NULL);
1868 	release_sock(sk);
1869 
1870 	if (ret)
1871 		goto out;
1872 
1873 	if (ctx.optlen == -1) {
1874 		/* optlen set to -1, bypass kernel */
1875 		ret = 1;
1876 	} else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
1877 		/* optlen is out of bounds */
1878 		if (*optlen > PAGE_SIZE && ctx.optlen >= 0) {
1879 			pr_info_once("bpf setsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n",
1880 				     ctx.optlen, max_optlen);
1881 			ret = 0;
1882 			goto out;
1883 		}
1884 		ret = -EFAULT;
1885 	} else {
1886 		/* optlen within bounds, run kernel handler */
1887 		ret = 0;
1888 
1889 		/* export any potential modifications */
1890 		*level = ctx.level;
1891 		*optname = ctx.optname;
1892 
1893 		/* optlen == 0 from BPF indicates that we should
1894 		 * use original userspace data.
1895 		 */
1896 		if (ctx.optlen != 0) {
1897 			*optlen = ctx.optlen;
1898 			/* We've used bpf_sockopt_kern->buf as an intermediary
1899 			 * storage, but the BPF program indicates that we need
1900 			 * to pass this data to the kernel setsockopt handler.
1901 			 * No way to export on-stack buf, have to allocate a
1902 			 * new buffer.
1903 			 */
1904 			if (!sockopt_buf_allocated(&ctx, &buf)) {
1905 				void *p = kmalloc(ctx.optlen, GFP_USER);
1906 
1907 				if (!p) {
1908 					ret = -ENOMEM;
1909 					goto out;
1910 				}
1911 				memcpy(p, ctx.optval, ctx.optlen);
1912 				*kernel_optval = p;
1913 			} else {
1914 				*kernel_optval = ctx.optval;
1915 			}
1916 			/* export and don't free sockopt buf */
1917 			return 0;
1918 		}
1919 	}
1920 
1921 out:
1922 	sockopt_free_buf(&ctx, &buf);
1923 	return ret;
1924 }
1925 
__cgroup_bpf_run_filter_getsockopt(struct sock * sk,int level,int optname,sockptr_t optval,sockptr_t optlen,int max_optlen,int retval)1926 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
1927 				       int optname, sockptr_t optval,
1928 				       sockptr_t optlen, int max_optlen,
1929 				       int retval)
1930 {
1931 	struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1932 	struct bpf_sockopt_buf buf = {};
1933 	struct bpf_sockopt_kern ctx = {
1934 		.sk = sk,
1935 		.level = level,
1936 		.optname = optname,
1937 		.current_task = current,
1938 	};
1939 	int orig_optlen;
1940 	int ret;
1941 
1942 	orig_optlen = max_optlen;
1943 	ctx.optlen = max_optlen;
1944 	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1945 	if (max_optlen < 0)
1946 		return max_optlen;
1947 
1948 	if (!retval) {
1949 		/* If kernel getsockopt finished successfully,
1950 		 * copy whatever was returned to the user back
1951 		 * into our temporary buffer. Set optlen to the
1952 		 * one that kernel returned as well to let
1953 		 * BPF programs inspect the value.
1954 		 */
1955 		if (copy_from_sockptr(&ctx.optlen, optlen,
1956 				      sizeof(ctx.optlen))) {
1957 			ret = -EFAULT;
1958 			goto out;
1959 		}
1960 
1961 		if (ctx.optlen < 0) {
1962 			ret = -EFAULT;
1963 			goto out;
1964 		}
1965 		orig_optlen = ctx.optlen;
1966 
1967 		if (copy_from_sockptr(ctx.optval, optval,
1968 				      min(ctx.optlen, max_optlen))) {
1969 			ret = -EFAULT;
1970 			goto out;
1971 		}
1972 	}
1973 
1974 	lock_sock(sk);
1975 	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
1976 				    &ctx, bpf_prog_run, retval, NULL);
1977 	release_sock(sk);
1978 
1979 	if (ret < 0)
1980 		goto out;
1981 
1982 	if (!sockptr_is_null(optval) &&
1983 	    (ctx.optlen > max_optlen || ctx.optlen < 0)) {
1984 		if (orig_optlen > PAGE_SIZE && ctx.optlen >= 0) {
1985 			pr_info_once("bpf getsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n",
1986 				     ctx.optlen, max_optlen);
1987 			ret = retval;
1988 			goto out;
1989 		}
1990 		ret = -EFAULT;
1991 		goto out;
1992 	}
1993 
1994 	if (ctx.optlen != 0) {
1995 		if (!sockptr_is_null(optval) &&
1996 		    copy_to_sockptr(optval, ctx.optval, ctx.optlen)) {
1997 			ret = -EFAULT;
1998 			goto out;
1999 		}
2000 		if (copy_to_sockptr(optlen, &ctx.optlen, sizeof(ctx.optlen))) {
2001 			ret = -EFAULT;
2002 			goto out;
2003 		}
2004 	}
2005 
2006 out:
2007 	sockopt_free_buf(&ctx, &buf);
2008 	return ret;
2009 }
2010 
__cgroup_bpf_run_filter_getsockopt_kern(struct sock * sk,int level,int optname,void * optval,int * optlen,int retval)2011 int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
2012 					    int optname, void *optval,
2013 					    int *optlen, int retval)
2014 {
2015 	struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
2016 	struct bpf_sockopt_kern ctx = {
2017 		.sk = sk,
2018 		.level = level,
2019 		.optname = optname,
2020 		.optlen = *optlen,
2021 		.optval = optval,
2022 		.optval_end = optval + *optlen,
2023 		.current_task = current,
2024 	};
2025 	int ret;
2026 
2027 	/* Note that __cgroup_bpf_run_filter_getsockopt doesn't copy
2028 	 * user data back into BPF buffer when reval != 0. This is
2029 	 * done as an optimization to avoid extra copy, assuming
2030 	 * kernel won't populate the data in case of an error.
2031 	 * Here we always pass the data and memset() should
2032 	 * be called if that data shouldn't be "exported".
2033 	 */
2034 
2035 	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
2036 				    &ctx, bpf_prog_run, retval, NULL);
2037 	if (ret < 0)
2038 		return ret;
2039 
2040 	if (ctx.optlen > *optlen)
2041 		return -EFAULT;
2042 
2043 	/* BPF programs can shrink the buffer, export the modifications.
2044 	 */
2045 	if (ctx.optlen != 0)
2046 		*optlen = ctx.optlen;
2047 
2048 	return ret;
2049 }
2050 #endif
2051 
sysctl_cpy_dir(const struct ctl_dir * dir,char ** bufp,size_t * lenp)2052 static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
2053 			      size_t *lenp)
2054 {
2055 	ssize_t tmp_ret = 0, ret;
2056 
2057 	if (dir->header.parent) {
2058 		tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
2059 		if (tmp_ret < 0)
2060 			return tmp_ret;
2061 	}
2062 
2063 	ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
2064 	if (ret < 0)
2065 		return ret;
2066 	*bufp += ret;
2067 	*lenp -= ret;
2068 	ret += tmp_ret;
2069 
2070 	/* Avoid leading slash. */
2071 	if (!ret)
2072 		return ret;
2073 
2074 	tmp_ret = strscpy(*bufp, "/", *lenp);
2075 	if (tmp_ret < 0)
2076 		return tmp_ret;
2077 	*bufp += tmp_ret;
2078 	*lenp -= tmp_ret;
2079 
2080 	return ret + tmp_ret;
2081 }
2082 
BPF_CALL_4(bpf_sysctl_get_name,struct bpf_sysctl_kern *,ctx,char *,buf,size_t,buf_len,u64,flags)2083 BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
2084 	   size_t, buf_len, u64, flags)
2085 {
2086 	ssize_t tmp_ret = 0, ret;
2087 
2088 	if (!buf)
2089 		return -EINVAL;
2090 
2091 	if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
2092 		if (!ctx->head)
2093 			return -EINVAL;
2094 		tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
2095 		if (tmp_ret < 0)
2096 			return tmp_ret;
2097 	}
2098 
2099 	ret = strscpy(buf, ctx->table->procname, buf_len);
2100 
2101 	return ret < 0 ? ret : tmp_ret + ret;
2102 }
2103 
2104 static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
2105 	.func		= bpf_sysctl_get_name,
2106 	.gpl_only	= false,
2107 	.ret_type	= RET_INTEGER,
2108 	.arg1_type	= ARG_PTR_TO_CTX,
2109 	.arg2_type	= ARG_PTR_TO_MEM,
2110 	.arg3_type	= ARG_CONST_SIZE,
2111 	.arg4_type	= ARG_ANYTHING,
2112 };
2113 
copy_sysctl_value(char * dst,size_t dst_len,char * src,size_t src_len)2114 static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
2115 			     size_t src_len)
2116 {
2117 	if (!dst)
2118 		return -EINVAL;
2119 
2120 	if (!dst_len)
2121 		return -E2BIG;
2122 
2123 	if (!src || !src_len) {
2124 		memset(dst, 0, dst_len);
2125 		return -EINVAL;
2126 	}
2127 
2128 	memcpy(dst, src, min(dst_len, src_len));
2129 
2130 	if (dst_len > src_len) {
2131 		memset(dst + src_len, '\0', dst_len - src_len);
2132 		return src_len;
2133 	}
2134 
2135 	dst[dst_len - 1] = '\0';
2136 
2137 	return -E2BIG;
2138 }
2139 
BPF_CALL_3(bpf_sysctl_get_current_value,struct bpf_sysctl_kern *,ctx,char *,buf,size_t,buf_len)2140 BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
2141 	   char *, buf, size_t, buf_len)
2142 {
2143 	return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
2144 }
2145 
2146 static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
2147 	.func		= bpf_sysctl_get_current_value,
2148 	.gpl_only	= false,
2149 	.ret_type	= RET_INTEGER,
2150 	.arg1_type	= ARG_PTR_TO_CTX,
2151 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
2152 	.arg3_type	= ARG_CONST_SIZE,
2153 };
2154 
BPF_CALL_3(bpf_sysctl_get_new_value,struct bpf_sysctl_kern *,ctx,char *,buf,size_t,buf_len)2155 BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
2156 	   size_t, buf_len)
2157 {
2158 	if (!ctx->write) {
2159 		if (buf && buf_len)
2160 			memset(buf, '\0', buf_len);
2161 		return -EINVAL;
2162 	}
2163 	return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
2164 }
2165 
2166 static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
2167 	.func		= bpf_sysctl_get_new_value,
2168 	.gpl_only	= false,
2169 	.ret_type	= RET_INTEGER,
2170 	.arg1_type	= ARG_PTR_TO_CTX,
2171 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
2172 	.arg3_type	= ARG_CONST_SIZE,
2173 };
2174 
BPF_CALL_3(bpf_sysctl_set_new_value,struct bpf_sysctl_kern *,ctx,const char *,buf,size_t,buf_len)2175 BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
2176 	   const char *, buf, size_t, buf_len)
2177 {
2178 	if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
2179 		return -EINVAL;
2180 
2181 	if (buf_len > PAGE_SIZE - 1)
2182 		return -E2BIG;
2183 
2184 	memcpy(ctx->new_val, buf, buf_len);
2185 	ctx->new_len = buf_len;
2186 	ctx->new_updated = 1;
2187 
2188 	return 0;
2189 }
2190 
2191 static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
2192 	.func		= bpf_sysctl_set_new_value,
2193 	.gpl_only	= false,
2194 	.ret_type	= RET_INTEGER,
2195 	.arg1_type	= ARG_PTR_TO_CTX,
2196 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
2197 	.arg3_type	= ARG_CONST_SIZE,
2198 };
2199 
2200 static const struct bpf_func_proto *
sysctl_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)2201 sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2202 {
2203 	const struct bpf_func_proto *func_proto;
2204 
2205 	func_proto = cgroup_common_func_proto(func_id, prog);
2206 	if (func_proto)
2207 		return func_proto;
2208 
2209 	func_proto = cgroup_current_func_proto(func_id, prog);
2210 	if (func_proto)
2211 		return func_proto;
2212 
2213 	switch (func_id) {
2214 	case BPF_FUNC_sysctl_get_name:
2215 		return &bpf_sysctl_get_name_proto;
2216 	case BPF_FUNC_sysctl_get_current_value:
2217 		return &bpf_sysctl_get_current_value_proto;
2218 	case BPF_FUNC_sysctl_get_new_value:
2219 		return &bpf_sysctl_get_new_value_proto;
2220 	case BPF_FUNC_sysctl_set_new_value:
2221 		return &bpf_sysctl_set_new_value_proto;
2222 	case BPF_FUNC_ktime_get_coarse_ns:
2223 		return &bpf_ktime_get_coarse_ns_proto;
2224 	case BPF_FUNC_perf_event_output:
2225 		return &bpf_event_output_data_proto;
2226 	default:
2227 		return bpf_base_func_proto(func_id);
2228 	}
2229 }
2230 
sysctl_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2231 static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
2232 				   const struct bpf_prog *prog,
2233 				   struct bpf_insn_access_aux *info)
2234 {
2235 	const int size_default = sizeof(__u32);
2236 
2237 	if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
2238 		return false;
2239 
2240 	switch (off) {
2241 	case bpf_ctx_range(struct bpf_sysctl, write):
2242 		if (type != BPF_READ)
2243 			return false;
2244 		bpf_ctx_record_field_size(info, size_default);
2245 		return bpf_ctx_narrow_access_ok(off, size, size_default);
2246 	case bpf_ctx_range(struct bpf_sysctl, file_pos):
2247 		if (type == BPF_READ) {
2248 			bpf_ctx_record_field_size(info, size_default);
2249 			return bpf_ctx_narrow_access_ok(off, size, size_default);
2250 		} else {
2251 			return size == size_default;
2252 		}
2253 	default:
2254 		return false;
2255 	}
2256 }
2257 
sysctl_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)2258 static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
2259 				     const struct bpf_insn *si,
2260 				     struct bpf_insn *insn_buf,
2261 				     struct bpf_prog *prog, u32 *target_size)
2262 {
2263 	struct bpf_insn *insn = insn_buf;
2264 	u32 read_size;
2265 
2266 	switch (si->off) {
2267 	case offsetof(struct bpf_sysctl, write):
2268 		*insn++ = BPF_LDX_MEM(
2269 			BPF_SIZE(si->code), si->dst_reg, si->src_reg,
2270 			bpf_target_off(struct bpf_sysctl_kern, write,
2271 				       sizeof_field(struct bpf_sysctl_kern,
2272 						    write),
2273 				       target_size));
2274 		break;
2275 	case offsetof(struct bpf_sysctl, file_pos):
2276 		/* ppos is a pointer so it should be accessed via indirect
2277 		 * loads and stores. Also for stores additional temporary
2278 		 * register is used since neither src_reg nor dst_reg can be
2279 		 * overridden.
2280 		 */
2281 		if (type == BPF_WRITE) {
2282 			int treg = BPF_REG_9;
2283 
2284 			if (si->src_reg == treg || si->dst_reg == treg)
2285 				--treg;
2286 			if (si->src_reg == treg || si->dst_reg == treg)
2287 				--treg;
2288 			*insn++ = BPF_STX_MEM(
2289 				BPF_DW, si->dst_reg, treg,
2290 				offsetof(struct bpf_sysctl_kern, tmp_reg));
2291 			*insn++ = BPF_LDX_MEM(
2292 				BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
2293 				treg, si->dst_reg,
2294 				offsetof(struct bpf_sysctl_kern, ppos));
2295 			*insn++ = BPF_RAW_INSN(
2296 				BPF_CLASS(si->code) | BPF_MEM | BPF_SIZEOF(u32),
2297 				treg, si->src_reg,
2298 				bpf_ctx_narrow_access_offset(
2299 					0, sizeof(u32), sizeof(loff_t)),
2300 				si->imm);
2301 			*insn++ = BPF_LDX_MEM(
2302 				BPF_DW, treg, si->dst_reg,
2303 				offsetof(struct bpf_sysctl_kern, tmp_reg));
2304 		} else {
2305 			*insn++ = BPF_LDX_MEM(
2306 				BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
2307 				si->dst_reg, si->src_reg,
2308 				offsetof(struct bpf_sysctl_kern, ppos));
2309 			read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
2310 			*insn++ = BPF_LDX_MEM(
2311 				BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
2312 				bpf_ctx_narrow_access_offset(
2313 					0, read_size, sizeof(loff_t)));
2314 		}
2315 		*target_size = sizeof(u32);
2316 		break;
2317 	}
2318 
2319 	return insn - insn_buf;
2320 }
2321 
2322 const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
2323 	.get_func_proto		= sysctl_func_proto,
2324 	.is_valid_access	= sysctl_is_valid_access,
2325 	.convert_ctx_access	= sysctl_convert_ctx_access,
2326 };
2327 
2328 const struct bpf_prog_ops cg_sysctl_prog_ops = {
2329 };
2330 
2331 #ifdef CONFIG_NET
BPF_CALL_1(bpf_get_netns_cookie_sockopt,struct bpf_sockopt_kern *,ctx)2332 BPF_CALL_1(bpf_get_netns_cookie_sockopt, struct bpf_sockopt_kern *, ctx)
2333 {
2334 	const struct net *net = ctx ? sock_net(ctx->sk) : &init_net;
2335 
2336 	return net->net_cookie;
2337 }
2338 
2339 static const struct bpf_func_proto bpf_get_netns_cookie_sockopt_proto = {
2340 	.func		= bpf_get_netns_cookie_sockopt,
2341 	.gpl_only	= false,
2342 	.ret_type	= RET_INTEGER,
2343 	.arg1_type	= ARG_PTR_TO_CTX_OR_NULL,
2344 };
2345 #endif
2346 
2347 static const struct bpf_func_proto *
cg_sockopt_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)2348 cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2349 {
2350 	const struct bpf_func_proto *func_proto;
2351 
2352 	func_proto = cgroup_common_func_proto(func_id, prog);
2353 	if (func_proto)
2354 		return func_proto;
2355 
2356 	func_proto = cgroup_current_func_proto(func_id, prog);
2357 	if (func_proto)
2358 		return func_proto;
2359 
2360 	switch (func_id) {
2361 #ifdef CONFIG_NET
2362 	case BPF_FUNC_get_netns_cookie:
2363 		return &bpf_get_netns_cookie_sockopt_proto;
2364 	case BPF_FUNC_sk_storage_get:
2365 		return &bpf_sk_storage_get_proto;
2366 	case BPF_FUNC_sk_storage_delete:
2367 		return &bpf_sk_storage_delete_proto;
2368 	case BPF_FUNC_setsockopt:
2369 		if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
2370 			return &bpf_sk_setsockopt_proto;
2371 		return NULL;
2372 	case BPF_FUNC_getsockopt:
2373 		if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
2374 			return &bpf_sk_getsockopt_proto;
2375 		return NULL;
2376 #endif
2377 #ifdef CONFIG_INET
2378 	case BPF_FUNC_tcp_sock:
2379 		return &bpf_tcp_sock_proto;
2380 #endif
2381 	case BPF_FUNC_perf_event_output:
2382 		return &bpf_event_output_data_proto;
2383 	default:
2384 		return bpf_base_func_proto(func_id);
2385 	}
2386 }
2387 
cg_sockopt_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2388 static bool cg_sockopt_is_valid_access(int off, int size,
2389 				       enum bpf_access_type type,
2390 				       const struct bpf_prog *prog,
2391 				       struct bpf_insn_access_aux *info)
2392 {
2393 	const int size_default = sizeof(__u32);
2394 
2395 	if (off < 0 || off >= sizeof(struct bpf_sockopt))
2396 		return false;
2397 
2398 	if (off % size != 0)
2399 		return false;
2400 
2401 	if (type == BPF_WRITE) {
2402 		switch (off) {
2403 		case offsetof(struct bpf_sockopt, retval):
2404 			if (size != size_default)
2405 				return false;
2406 			return prog->expected_attach_type ==
2407 				BPF_CGROUP_GETSOCKOPT;
2408 		case offsetof(struct bpf_sockopt, optname):
2409 			fallthrough;
2410 		case offsetof(struct bpf_sockopt, level):
2411 			if (size != size_default)
2412 				return false;
2413 			return prog->expected_attach_type ==
2414 				BPF_CGROUP_SETSOCKOPT;
2415 		case offsetof(struct bpf_sockopt, optlen):
2416 			return size == size_default;
2417 		default:
2418 			return false;
2419 		}
2420 	}
2421 
2422 	switch (off) {
2423 	case offsetof(struct bpf_sockopt, sk):
2424 		if (size != sizeof(__u64))
2425 			return false;
2426 		info->reg_type = PTR_TO_SOCKET;
2427 		break;
2428 	case offsetof(struct bpf_sockopt, optval):
2429 		if (size != sizeof(__u64))
2430 			return false;
2431 		info->reg_type = PTR_TO_PACKET;
2432 		break;
2433 	case offsetof(struct bpf_sockopt, optval_end):
2434 		if (size != sizeof(__u64))
2435 			return false;
2436 		info->reg_type = PTR_TO_PACKET_END;
2437 		break;
2438 	case offsetof(struct bpf_sockopt, retval):
2439 		if (size != size_default)
2440 			return false;
2441 		return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
2442 	default:
2443 		if (size != size_default)
2444 			return false;
2445 		break;
2446 	}
2447 	return true;
2448 }
2449 
2450 #define CG_SOCKOPT_READ_FIELD(F)					\
2451 	BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F),	\
2452 		    si->dst_reg, si->src_reg,				\
2453 		    offsetof(struct bpf_sockopt_kern, F))
2454 
2455 #define CG_SOCKOPT_WRITE_FIELD(F)					\
2456 	BPF_RAW_INSN((BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F) |	\
2457 		      BPF_MEM | BPF_CLASS(si->code)),			\
2458 		     si->dst_reg, si->src_reg,				\
2459 		     offsetof(struct bpf_sockopt_kern, F),		\
2460 		     si->imm)
2461 
cg_sockopt_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)2462 static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
2463 					 const struct bpf_insn *si,
2464 					 struct bpf_insn *insn_buf,
2465 					 struct bpf_prog *prog,
2466 					 u32 *target_size)
2467 {
2468 	struct bpf_insn *insn = insn_buf;
2469 
2470 	switch (si->off) {
2471 	case offsetof(struct bpf_sockopt, sk):
2472 		*insn++ = CG_SOCKOPT_READ_FIELD(sk);
2473 		break;
2474 	case offsetof(struct bpf_sockopt, level):
2475 		if (type == BPF_WRITE)
2476 			*insn++ = CG_SOCKOPT_WRITE_FIELD(level);
2477 		else
2478 			*insn++ = CG_SOCKOPT_READ_FIELD(level);
2479 		break;
2480 	case offsetof(struct bpf_sockopt, optname):
2481 		if (type == BPF_WRITE)
2482 			*insn++ = CG_SOCKOPT_WRITE_FIELD(optname);
2483 		else
2484 			*insn++ = CG_SOCKOPT_READ_FIELD(optname);
2485 		break;
2486 	case offsetof(struct bpf_sockopt, optlen):
2487 		if (type == BPF_WRITE)
2488 			*insn++ = CG_SOCKOPT_WRITE_FIELD(optlen);
2489 		else
2490 			*insn++ = CG_SOCKOPT_READ_FIELD(optlen);
2491 		break;
2492 	case offsetof(struct bpf_sockopt, retval):
2493 		BUILD_BUG_ON(offsetof(struct bpf_cg_run_ctx, run_ctx) != 0);
2494 
2495 		if (type == BPF_WRITE) {
2496 			int treg = BPF_REG_9;
2497 
2498 			if (si->src_reg == treg || si->dst_reg == treg)
2499 				--treg;
2500 			if (si->src_reg == treg || si->dst_reg == treg)
2501 				--treg;
2502 			*insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, treg,
2503 					      offsetof(struct bpf_sockopt_kern, tmp_reg));
2504 			*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, current_task),
2505 					      treg, si->dst_reg,
2506 					      offsetof(struct bpf_sockopt_kern, current_task));
2507 			*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct task_struct, bpf_ctx),
2508 					      treg, treg,
2509 					      offsetof(struct task_struct, bpf_ctx));
2510 			*insn++ = BPF_RAW_INSN(BPF_CLASS(si->code) | BPF_MEM |
2511 					       BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval),
2512 					       treg, si->src_reg,
2513 					       offsetof(struct bpf_cg_run_ctx, retval),
2514 					       si->imm);
2515 			*insn++ = BPF_LDX_MEM(BPF_DW, treg, si->dst_reg,
2516 					      offsetof(struct bpf_sockopt_kern, tmp_reg));
2517 		} else {
2518 			*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, current_task),
2519 					      si->dst_reg, si->src_reg,
2520 					      offsetof(struct bpf_sockopt_kern, current_task));
2521 			*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct task_struct, bpf_ctx),
2522 					      si->dst_reg, si->dst_reg,
2523 					      offsetof(struct task_struct, bpf_ctx));
2524 			*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval),
2525 					      si->dst_reg, si->dst_reg,
2526 					      offsetof(struct bpf_cg_run_ctx, retval));
2527 		}
2528 		break;
2529 	case offsetof(struct bpf_sockopt, optval):
2530 		*insn++ = CG_SOCKOPT_READ_FIELD(optval);
2531 		break;
2532 	case offsetof(struct bpf_sockopt, optval_end):
2533 		*insn++ = CG_SOCKOPT_READ_FIELD(optval_end);
2534 		break;
2535 	}
2536 
2537 	return insn - insn_buf;
2538 }
2539 
cg_sockopt_get_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)2540 static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
2541 				   bool direct_write,
2542 				   const struct bpf_prog *prog)
2543 {
2544 	/* Nothing to do for sockopt argument. The data is kzalloc'ated.
2545 	 */
2546 	return 0;
2547 }
2548 
2549 const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
2550 	.get_func_proto		= cg_sockopt_func_proto,
2551 	.is_valid_access	= cg_sockopt_is_valid_access,
2552 	.convert_ctx_access	= cg_sockopt_convert_ctx_access,
2553 	.gen_prologue		= cg_sockopt_get_prologue,
2554 };
2555 
2556 const struct bpf_prog_ops cg_sockopt_prog_ops = {
2557 };
2558 
2559 /* Common helpers for cgroup hooks. */
2560 const struct bpf_func_proto *
cgroup_common_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)2561 cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2562 {
2563 	switch (func_id) {
2564 	case BPF_FUNC_get_local_storage:
2565 		return &bpf_get_local_storage_proto;
2566 	case BPF_FUNC_get_retval:
2567 		switch (prog->expected_attach_type) {
2568 		case BPF_CGROUP_INET_INGRESS:
2569 		case BPF_CGROUP_INET_EGRESS:
2570 		case BPF_CGROUP_SOCK_OPS:
2571 		case BPF_CGROUP_UDP4_RECVMSG:
2572 		case BPF_CGROUP_UDP6_RECVMSG:
2573 		case BPF_CGROUP_INET4_GETPEERNAME:
2574 		case BPF_CGROUP_INET6_GETPEERNAME:
2575 		case BPF_CGROUP_INET4_GETSOCKNAME:
2576 		case BPF_CGROUP_INET6_GETSOCKNAME:
2577 			return NULL;
2578 		default:
2579 			return &bpf_get_retval_proto;
2580 		}
2581 	case BPF_FUNC_set_retval:
2582 		switch (prog->expected_attach_type) {
2583 		case BPF_CGROUP_INET_INGRESS:
2584 		case BPF_CGROUP_INET_EGRESS:
2585 		case BPF_CGROUP_SOCK_OPS:
2586 		case BPF_CGROUP_UDP4_RECVMSG:
2587 		case BPF_CGROUP_UDP6_RECVMSG:
2588 		case BPF_CGROUP_INET4_GETPEERNAME:
2589 		case BPF_CGROUP_INET6_GETPEERNAME:
2590 		case BPF_CGROUP_INET4_GETSOCKNAME:
2591 		case BPF_CGROUP_INET6_GETSOCKNAME:
2592 			return NULL;
2593 		default:
2594 			return &bpf_set_retval_proto;
2595 		}
2596 	default:
2597 		return NULL;
2598 	}
2599 }
2600 
2601 /* Common helpers for cgroup hooks with valid process context. */
2602 const struct bpf_func_proto *
cgroup_current_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)2603 cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2604 {
2605 	switch (func_id) {
2606 	case BPF_FUNC_get_current_uid_gid:
2607 		return &bpf_get_current_uid_gid_proto;
2608 	case BPF_FUNC_get_current_pid_tgid:
2609 		return &bpf_get_current_pid_tgid_proto;
2610 	case BPF_FUNC_get_current_comm:
2611 		return &bpf_get_current_comm_proto;
2612 #ifdef CONFIG_CGROUP_NET_CLASSID
2613 	case BPF_FUNC_get_cgroup_classid:
2614 		return &bpf_get_cgroup_classid_curr_proto;
2615 #endif
2616 	default:
2617 		return NULL;
2618 	}
2619 }
2620