blob: cdf3c7e611d91dd7f02d93fc58b5725a1381e53f [file] [log] [blame]
Thomas Gleixnerf85d2082019-06-04 10:10:45 +02001// SPDX-License-Identifier: GPL-2.0-only
Daniel Mack30070982016-11-23 16:52:26 +01002/*
3 * Functions to manage eBPF programs attached to cgroups
4 *
5 * Copyright (c) 2016 Daniel Mack
Daniel Mack30070982016-11-23 16:52:26 +01006 */
7
8#include <linux/kernel.h>
9#include <linux/atomic.h>
10#include <linux/cgroup.h>
Andrey Ignatov7b146ce2019-02-27 12:59:24 -080011#include <linux/filter.h>
Daniel Mack30070982016-11-23 16:52:26 +010012#include <linux/slab.h>
Andrey Ignatov7b146ce2019-02-27 12:59:24 -080013#include <linux/sysctl.h>
Andrey Ignatov808649f2019-02-27 13:28:48 -080014#include <linux/string.h>
Daniel Mack30070982016-11-23 16:52:26 +010015#include <linux/bpf.h>
16#include <linux/bpf-cgroup.h>
17#include <net/sock.h>
Stanislav Fomichev0d01da62019-06-27 13:38:47 -070018#include <net/bpf_sk_storage.h>
Daniel Mack30070982016-11-23 16:52:26 +010019
Roman Gushchine5c891a2019-06-25 14:38:58 -070020#include "../cgroup/cgroup-internal.h"
21
Stanislav Fomicheva9ed15da2021-01-15 08:35:01 -080022DEFINE_STATIC_KEY_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_BPF_ATTACH_TYPE);
Daniel Mack30070982016-11-23 16:52:26 +010023EXPORT_SYMBOL(cgroup_bpf_enabled_key);
24
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -070025void cgroup_bpf_offline(struct cgroup *cgrp)
Daniel Mack30070982016-11-23 16:52:26 +010026{
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -070027 cgroup_get(cgrp);
28 percpu_ref_kill(&cgrp->bpf.refcnt);
29}
30
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -070031static void bpf_cgroup_storages_free(struct bpf_cgroup_storage *storages[])
32{
33 enum bpf_cgroup_storage_type stype;
34
35 for_each_cgroup_storage_type(stype)
36 bpf_cgroup_storage_free(storages[stype]);
37}
38
39static int bpf_cgroup_storages_alloc(struct bpf_cgroup_storage *storages[],
YiFei Zhu7d9c3422020-07-23 23:47:43 -050040 struct bpf_cgroup_storage *new_storages[],
41 enum bpf_attach_type type,
42 struct bpf_prog *prog,
43 struct cgroup *cgrp)
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -070044{
45 enum bpf_cgroup_storage_type stype;
YiFei Zhu7d9c3422020-07-23 23:47:43 -050046 struct bpf_cgroup_storage_key key;
47 struct bpf_map *map;
48
49 key.cgroup_inode_id = cgroup_id(cgrp);
50 key.attach_type = type;
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -070051
52 for_each_cgroup_storage_type(stype) {
YiFei Zhu7d9c3422020-07-23 23:47:43 -050053 map = prog->aux->cgroup_storage[stype];
54 if (!map)
55 continue;
56
57 storages[stype] = cgroup_storage_lookup((void *)map, &key, false);
58 if (storages[stype])
59 continue;
60
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -070061 storages[stype] = bpf_cgroup_storage_alloc(prog, stype);
62 if (IS_ERR(storages[stype])) {
YiFei Zhu7d9c3422020-07-23 23:47:43 -050063 bpf_cgroup_storages_free(new_storages);
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -070064 return -ENOMEM;
65 }
YiFei Zhu7d9c3422020-07-23 23:47:43 -050066
67 new_storages[stype] = storages[stype];
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -070068 }
69
70 return 0;
71}
72
73static void bpf_cgroup_storages_assign(struct bpf_cgroup_storage *dst[],
74 struct bpf_cgroup_storage *src[])
75{
76 enum bpf_cgroup_storage_type stype;
77
78 for_each_cgroup_storage_type(stype)
79 dst[stype] = src[stype];
80}
81
82static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],
YiFei Zhu7d9c3422020-07-23 23:47:43 -050083 struct cgroup *cgrp,
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -070084 enum bpf_attach_type attach_type)
85{
86 enum bpf_cgroup_storage_type stype;
87
88 for_each_cgroup_storage_type(stype)
89 bpf_cgroup_storage_link(storages[stype], cgrp, attach_type);
90}
91
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -070092/* Called when bpf_cgroup_link is auto-detached from dying cgroup.
93 * It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It
94 * doesn't free link memory, which will eventually be done by bpf_link's
95 * release() callback, when its last FD is closed.
96 */
97static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link)
98{
99 cgroup_put(link->cgroup);
100 link->cgroup = NULL;
101}
102
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -0700103/**
104 * cgroup_bpf_release() - put references of all bpf programs and
105 * release all cgroup bpf data
106 * @work: work structure embedded into the cgroup to modify
107 */
108static void cgroup_bpf_release(struct work_struct *work)
109{
Roman Gushchine10360f2019-12-27 13:50:34 -0800110 struct cgroup *p, *cgrp = container_of(work, struct cgroup,
111 bpf.release_work);
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700112 struct bpf_prog_array *old_array;
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500113 struct list_head *storages = &cgrp->bpf.storages;
114 struct bpf_cgroup_storage *storage, *stmp;
115
Daniel Mack30070982016-11-23 16:52:26 +0100116 unsigned int type;
117
Roman Gushchine5c891a2019-06-25 14:38:58 -0700118 mutex_lock(&cgroup_mutex);
119
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700120 for (type = 0; type < ARRAY_SIZE(cgrp->bpf.progs); type++) {
121 struct list_head *progs = &cgrp->bpf.progs[type];
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500122 struct bpf_prog_list *pl, *pltmp;
Daniel Mack30070982016-11-23 16:52:26 +0100123
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500124 list_for_each_entry_safe(pl, pltmp, progs, node) {
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700125 list_del(&pl->node);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700126 if (pl->prog)
127 bpf_prog_put(pl->prog);
128 if (pl->link)
129 bpf_cgroup_link_auto_detach(pl->link);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700130 kfree(pl);
Stanislav Fomicheva9ed15da2021-01-15 08:35:01 -0800131 static_branch_dec(&cgroup_bpf_enabled_key[type]);
Daniel Mack30070982016-11-23 16:52:26 +0100132 }
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700133 old_array = rcu_dereference_protected(
134 cgrp->bpf.effective[type],
Roman Gushchine5c891a2019-06-25 14:38:58 -0700135 lockdep_is_held(&cgroup_mutex));
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700136 bpf_prog_array_free(old_array);
Daniel Mack30070982016-11-23 16:52:26 +0100137 }
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -0700138
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500139 list_for_each_entry_safe(storage, stmp, storages, list_cg) {
140 bpf_cgroup_storage_unlink(storage);
141 bpf_cgroup_storage_free(storage);
142 }
143
Roman Gushchine5c891a2019-06-25 14:38:58 -0700144 mutex_unlock(&cgroup_mutex);
145
Roman Gushchine10360f2019-12-27 13:50:34 -0800146 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
147 cgroup_bpf_put(p);
148
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -0700149 percpu_ref_exit(&cgrp->bpf.refcnt);
150 cgroup_put(cgrp);
151}
152
153/**
154 * cgroup_bpf_release_fn() - callback used to schedule releasing
155 * of bpf cgroup data
156 * @ref: percpu ref counter structure
157 */
158static void cgroup_bpf_release_fn(struct percpu_ref *ref)
159{
160 struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt);
161
162 INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release);
163 queue_work(system_wq, &cgrp->bpf.release_work);
Daniel Mack30070982016-11-23 16:52:26 +0100164}
165
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700166/* Get underlying bpf_prog of bpf_prog_list entry, regardless if it's through
167 * link or direct prog.
168 */
169static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl)
170{
171 if (pl->prog)
172 return pl->prog;
173 if (pl->link)
174 return pl->link->link.prog;
175 return NULL;
176}
177
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700178/* count number of elements in the list.
179 * it's slow but the list cannot be long
180 */
181static u32 prog_list_length(struct list_head *head)
182{
183 struct bpf_prog_list *pl;
184 u32 cnt = 0;
185
186 list_for_each_entry(pl, head, node) {
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700187 if (!prog_list_prog(pl))
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700188 continue;
189 cnt++;
190 }
191 return cnt;
192}
193
194/* if parent has non-overridable prog attached,
195 * disallow attaching new programs to the descendent cgroup.
196 * if parent has overridable or multi-prog, allow attaching
197 */
198static bool hierarchy_allows_attach(struct cgroup *cgrp,
Andrey Ignatov9fab3292019-12-18 23:44:34 -0800199 enum bpf_attach_type type)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700200{
201 struct cgroup *p;
202
203 p = cgroup_parent(cgrp);
204 if (!p)
205 return true;
206 do {
207 u32 flags = p->bpf.flags[type];
208 u32 cnt;
209
210 if (flags & BPF_F_ALLOW_MULTI)
211 return true;
212 cnt = prog_list_length(&p->bpf.progs[type]);
213 WARN_ON_ONCE(cnt > 1);
214 if (cnt == 1)
215 return !!(flags & BPF_F_ALLOW_OVERRIDE);
216 p = cgroup_parent(p);
217 } while (p);
218 return true;
219}
220
221/* compute a chain of effective programs for a given cgroup:
222 * start from the list of programs in this cgroup and add
223 * all parent programs.
224 * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
225 * to programs in this cgroup
226 */
227static int compute_effective_progs(struct cgroup *cgrp,
228 enum bpf_attach_type type,
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700229 struct bpf_prog_array **array)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700230{
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -0700231 struct bpf_prog_array_item *item;
Roman Gushchin3960f4f2018-07-13 12:41:11 -0700232 struct bpf_prog_array *progs;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700233 struct bpf_prog_list *pl;
234 struct cgroup *p = cgrp;
235 int cnt = 0;
236
237 /* count number of effective programs by walking parents */
238 do {
239 if (cnt == 0 || (p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
240 cnt += prog_list_length(&p->bpf.progs[type]);
241 p = cgroup_parent(p);
242 } while (p);
243
244 progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
245 if (!progs)
246 return -ENOMEM;
247
248 /* populate the array with effective progs */
249 cnt = 0;
250 p = cgrp;
251 do {
Roman Gushchin394e40a2018-08-02 14:27:21 -0700252 if (cnt > 0 && !(p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
253 continue;
254
255 list_for_each_entry(pl, &p->bpf.progs[type], node) {
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700256 if (!prog_list_prog(pl))
Roman Gushchin394e40a2018-08-02 14:27:21 -0700257 continue;
258
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -0700259 item = &progs->items[cnt];
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700260 item->prog = prog_list_prog(pl);
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -0700261 bpf_cgroup_storages_assign(item->cgroup_storage,
262 pl->storage);
Roman Gushchin394e40a2018-08-02 14:27:21 -0700263 cnt++;
264 }
265 } while ((p = cgroup_parent(p)));
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700266
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700267 *array = progs;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700268 return 0;
269}
270
271static void activate_effective_progs(struct cgroup *cgrp,
272 enum bpf_attach_type type,
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700273 struct bpf_prog_array *old_array)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700274{
Paul E. McKenney6092f7262019-09-23 15:37:04 -0700275 old_array = rcu_replace_pointer(cgrp->bpf.effective[type], old_array,
276 lockdep_is_held(&cgroup_mutex));
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700277 /* free prog array after grace period, since __cgroup_bpf_run_*()
278 * might be still walking the array
279 */
280 bpf_prog_array_free(old_array);
281}
282
Daniel Mack30070982016-11-23 16:52:26 +0100283/**
284 * cgroup_bpf_inherit() - inherit effective programs from parent
285 * @cgrp: the cgroup to modify
Daniel Mack30070982016-11-23 16:52:26 +0100286 */
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700287int cgroup_bpf_inherit(struct cgroup *cgrp)
Daniel Mack30070982016-11-23 16:52:26 +0100288{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700289/* has to use marco instead of const int, since compiler thinks
290 * that array below is variable length
291 */
292#define NR ARRAY_SIZE(cgrp->bpf.effective)
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700293 struct bpf_prog_array *arrays[NR] = {};
Roman Gushchine10360f2019-12-27 13:50:34 -0800294 struct cgroup *p;
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -0700295 int ret, i;
296
297 ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0,
298 GFP_KERNEL);
299 if (ret)
300 return ret;
Daniel Mack30070982016-11-23 16:52:26 +0100301
Roman Gushchine10360f2019-12-27 13:50:34 -0800302 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
303 cgroup_bpf_get(p);
304
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700305 for (i = 0; i < NR; i++)
306 INIT_LIST_HEAD(&cgrp->bpf.progs[i]);
Daniel Mack30070982016-11-23 16:52:26 +0100307
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500308 INIT_LIST_HEAD(&cgrp->bpf.storages);
309
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700310 for (i = 0; i < NR; i++)
311 if (compute_effective_progs(cgrp, i, &arrays[i]))
312 goto cleanup;
313
314 for (i = 0; i < NR; i++)
315 activate_effective_progs(cgrp, i, arrays[i]);
316
317 return 0;
318cleanup:
319 for (i = 0; i < NR; i++)
320 bpf_prog_array_free(arrays[i]);
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -0700321
Andrii Nakryiko1d8006a2020-03-09 15:40:17 -0700322 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
323 cgroup_bpf_put(p);
324
Roman Gushchin4bfc0bb2019-05-25 09:37:39 -0700325 percpu_ref_exit(&cgrp->bpf.refcnt);
326
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700327 return -ENOMEM;
Daniel Mack30070982016-11-23 16:52:26 +0100328}
329
Roman Gushchin85fc4b12018-08-06 14:27:28 -0700330static int update_effective_progs(struct cgroup *cgrp,
331 enum bpf_attach_type type)
332{
333 struct cgroup_subsys_state *css;
334 int err;
335
336 /* allocate and recompute effective prog arrays */
337 css_for_each_descendant_pre(css, &cgrp->self) {
338 struct cgroup *desc = container_of(css, struct cgroup, self);
339
Roman Gushchine5c891a2019-06-25 14:38:58 -0700340 if (percpu_ref_is_zero(&desc->bpf.refcnt))
341 continue;
342
Roman Gushchin85fc4b12018-08-06 14:27:28 -0700343 err = compute_effective_progs(desc, type, &desc->bpf.inactive);
344 if (err)
345 goto cleanup;
346 }
347
348 /* all allocations were successful. Activate all prog arrays */
349 css_for_each_descendant_pre(css, &cgrp->self) {
350 struct cgroup *desc = container_of(css, struct cgroup, self);
351
Roman Gushchine5c891a2019-06-25 14:38:58 -0700352 if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
353 if (unlikely(desc->bpf.inactive)) {
354 bpf_prog_array_free(desc->bpf.inactive);
355 desc->bpf.inactive = NULL;
356 }
357 continue;
358 }
359
Roman Gushchin85fc4b12018-08-06 14:27:28 -0700360 activate_effective_progs(desc, type, desc->bpf.inactive);
361 desc->bpf.inactive = NULL;
362 }
363
364 return 0;
365
366cleanup:
367 /* oom while computing effective. Free all computed effective arrays
368 * since they were not activated
369 */
370 css_for_each_descendant_pre(css, &cgrp->self) {
371 struct cgroup *desc = container_of(css, struct cgroup, self);
372
373 bpf_prog_array_free(desc->bpf.inactive);
374 desc->bpf.inactive = NULL;
375 }
376
377 return err;
378}
379
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700380#define BPF_CGROUP_MAX_PROGS 64
381
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700382static struct bpf_prog_list *find_attach_entry(struct list_head *progs,
383 struct bpf_prog *prog,
384 struct bpf_cgroup_link *link,
385 struct bpf_prog *replace_prog,
386 bool allow_multi)
387{
388 struct bpf_prog_list *pl;
389
390 /* single-attach case */
391 if (!allow_multi) {
392 if (list_empty(progs))
393 return NULL;
394 return list_first_entry(progs, typeof(*pl), node);
395 }
396
397 list_for_each_entry(pl, progs, node) {
Lorenz Bauer248e00a2020-06-08 17:22:01 +0100398 if (prog && pl->prog == prog && prog != replace_prog)
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700399 /* disallow attaching the same prog twice */
400 return ERR_PTR(-EINVAL);
401 if (link && pl->link == link)
402 /* disallow attaching the same link twice */
403 return ERR_PTR(-EINVAL);
404 }
405
406 /* direct prog multi-attach w/ replacement case */
407 if (replace_prog) {
408 list_for_each_entry(pl, progs, node) {
409 if (pl->prog == replace_prog)
410 /* a match found */
411 return pl;
412 }
413 /* prog to replace not found for cgroup */
414 return ERR_PTR(-ENOENT);
415 }
416
417 return NULL;
418}
419
Daniel Mack30070982016-11-23 16:52:26 +0100420/**
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700421 * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and
Daniel Mack30070982016-11-23 16:52:26 +0100422 * propagate the change to descendants
423 * @cgrp: The cgroup which descendants to traverse
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700424 * @prog: A program to attach
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700425 * @link: A link to attach
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800426 * @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700427 * @type: Type of attach operation
Valdis Kletnieks1832f4e2019-01-29 01:47:06 -0500428 * @flags: Option flags
Daniel Mack30070982016-11-23 16:52:26 +0100429 *
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700430 * Exactly one of @prog or @link can be non-null.
Daniel Mack30070982016-11-23 16:52:26 +0100431 * Must be called with cgroup_mutex held.
432 */
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700433int __cgroup_bpf_attach(struct cgroup *cgrp,
434 struct bpf_prog *prog, struct bpf_prog *replace_prog,
435 struct bpf_cgroup_link *link,
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700436 enum bpf_attach_type type, u32 flags)
Daniel Mack30070982016-11-23 16:52:26 +0100437{
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800438 u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI));
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700439 struct list_head *progs = &cgrp->bpf.progs[type];
440 struct bpf_prog *old_prog = NULL;
Andrii Nakryiko62039c32020-03-09 15:27:55 -0700441 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500442 struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700443 struct bpf_prog_list *pl;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700444 int err;
Daniel Mack30070982016-11-23 16:52:26 +0100445
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800446 if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) ||
447 ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700448 /* invalid combination */
449 return -EINVAL;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700450 if (link && (prog || replace_prog))
451 /* only either link or prog/replace_prog can be specified */
452 return -EINVAL;
453 if (!!replace_prog != !!(flags & BPF_F_REPLACE))
454 /* replace_prog implies BPF_F_REPLACE, and vice versa */
455 return -EINVAL;
Daniel Mack30070982016-11-23 16:52:26 +0100456
Andrey Ignatov9fab3292019-12-18 23:44:34 -0800457 if (!hierarchy_allows_attach(cgrp, type))
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700458 return -EPERM;
459
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800460 if (!list_empty(progs) && cgrp->bpf.flags[type] != saved_flags)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700461 /* Disallow attaching non-overridable on top
462 * of existing overridable in this cgroup.
463 * Disallow attaching multi-prog if overridable or none
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800464 */
465 return -EPERM;
466
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700467 if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
468 return -E2BIG;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800469
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700470 pl = find_attach_entry(progs, prog, link, replace_prog,
471 flags & BPF_F_ALLOW_MULTI);
472 if (IS_ERR(pl))
473 return PTR_ERR(pl);
Andrey Ignatov1020c1f2019-12-18 23:44:33 -0800474
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500475 if (bpf_cgroup_storages_alloc(storage, new_storage, type,
476 prog ? : link->link.prog, cgrp))
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -0700477 return -ENOMEM;
Roman Gushchind7bf2c12018-08-02 14:27:20 -0700478
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700479 if (pl) {
Andrey Ignatov1020c1f2019-12-18 23:44:33 -0800480 old_prog = pl->prog;
Andrey Ignatov1020c1f2019-12-18 23:44:33 -0800481 } else {
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700482 pl = kmalloc(sizeof(*pl), GFP_KERNEL);
Roman Gushchind7bf2c12018-08-02 14:27:20 -0700483 if (!pl) {
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500484 bpf_cgroup_storages_free(new_storage);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700485 return -ENOMEM;
Roman Gushchind7bf2c12018-08-02 14:27:20 -0700486 }
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700487 list_add_tail(&pl->node, progs);
Daniel Mack30070982016-11-23 16:52:26 +0100488 }
489
Andrey Ignatov1020c1f2019-12-18 23:44:33 -0800490 pl->prog = prog;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700491 pl->link = link;
Andrii Nakryiko00c4edd2020-03-24 23:57:41 -0700492 bpf_cgroup_storages_assign(pl->storage, storage);
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800493 cgrp->bpf.flags[type] = saved_flags;
Daniel Mack30070982016-11-23 16:52:26 +0100494
Roman Gushchin85fc4b12018-08-06 14:27:28 -0700495 err = update_effective_progs(cgrp, type);
496 if (err)
497 goto cleanup;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700498
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700499 if (old_prog)
Daniel Mack30070982016-11-23 16:52:26 +0100500 bpf_prog_put(old_prog);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700501 else
Stanislav Fomicheva9ed15da2021-01-15 08:35:01 -0800502 static_branch_inc(&cgroup_bpf_enabled_key[type]);
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500503 bpf_cgroup_storages_link(new_storage, cgrp, type);
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800504 return 0;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700505
506cleanup:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700507 if (old_prog) {
508 pl->prog = old_prog;
509 pl->link = NULL;
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000510 }
YiFei Zhu7d9c3422020-07-23 23:47:43 -0500511 bpf_cgroup_storages_free(new_storage);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700512 if (!old_prog) {
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700513 list_del(&pl->node);
514 kfree(pl);
515 }
516 return err;
517}
518
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700519/* Swap updated BPF program for given link in effective program arrays across
520 * all descendant cgroups. This function is guaranteed to succeed.
521 */
522static void replace_effective_prog(struct cgroup *cgrp,
523 enum bpf_attach_type type,
524 struct bpf_cgroup_link *link)
525{
526 struct bpf_prog_array_item *item;
527 struct cgroup_subsys_state *css;
528 struct bpf_prog_array *progs;
529 struct bpf_prog_list *pl;
530 struct list_head *head;
531 struct cgroup *cg;
532 int pos;
533
534 css_for_each_descendant_pre(css, &cgrp->self) {
535 struct cgroup *desc = container_of(css, struct cgroup, self);
536
537 if (percpu_ref_is_zero(&desc->bpf.refcnt))
538 continue;
539
540 /* find position of link in effective progs array */
541 for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
542 if (pos && !(cg->bpf.flags[type] & BPF_F_ALLOW_MULTI))
543 continue;
544
545 head = &cg->bpf.progs[type];
546 list_for_each_entry(pl, head, node) {
547 if (!prog_list_prog(pl))
548 continue;
549 if (pl->link == link)
550 goto found;
551 pos++;
552 }
553 }
554found:
555 BUG_ON(!cg);
556 progs = rcu_dereference_protected(
557 desc->bpf.effective[type],
558 lockdep_is_held(&cgroup_mutex));
559 item = &progs->items[pos];
560 WRITE_ONCE(item->prog, link->link.prog);
561 }
562}
563
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700564/**
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700565 * __cgroup_bpf_replace() - Replace link's program and propagate the change
566 * to descendants
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700567 * @cgrp: The cgroup which descendants to traverse
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700568 * @link: A link for which to replace BPF program
569 * @type: Type of attach operation
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700570 *
571 * Must be called with cgroup_mutex held.
572 */
Andrii Nakryikof9d04122020-04-28 17:16:05 -0700573static int __cgroup_bpf_replace(struct cgroup *cgrp,
574 struct bpf_cgroup_link *link,
575 struct bpf_prog *new_prog)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700576{
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700577 struct list_head *progs = &cgrp->bpf.progs[link->type];
578 struct bpf_prog *old_prog;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700579 struct bpf_prog_list *pl;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700580 bool found = false;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700581
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700582 if (link->link.prog->type != new_prog->type)
583 return -EINVAL;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700584
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700585 list_for_each_entry(pl, progs, node) {
586 if (pl->link == link) {
587 found = true;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700588 break;
589 }
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700590 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -0700591 if (!found)
592 return -ENOENT;
593
594 old_prog = xchg(&link->link.prog, new_prog);
595 replace_effective_prog(cgrp, link->type, link);
596 bpf_prog_put(old_prog);
597 return 0;
598}
599
Andrii Nakryikof9d04122020-04-28 17:16:05 -0700600static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
601 struct bpf_prog *old_prog)
602{
603 struct bpf_cgroup_link *cg_link;
604 int ret;
605
606 cg_link = container_of(link, struct bpf_cgroup_link, link);
607
608 mutex_lock(&cgroup_mutex);
609 /* link might have been auto-released by dying cgroup, so fail */
610 if (!cg_link->cgroup) {
Jakub Sitnicki0c047ec2020-05-31 10:28:39 +0200611 ret = -ENOLINK;
Andrii Nakryikof9d04122020-04-28 17:16:05 -0700612 goto out_unlock;
613 }
614 if (old_prog && link->prog != old_prog) {
615 ret = -EPERM;
616 goto out_unlock;
617 }
618 ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
619out_unlock:
620 mutex_unlock(&cgroup_mutex);
621 return ret;
622}
623
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700624static struct bpf_prog_list *find_detach_entry(struct list_head *progs,
625 struct bpf_prog *prog,
626 struct bpf_cgroup_link *link,
627 bool allow_multi)
628{
629 struct bpf_prog_list *pl;
630
631 if (!allow_multi) {
632 if (list_empty(progs))
633 /* report error when trying to detach and nothing is attached */
634 return ERR_PTR(-ENOENT);
635
636 /* to maintain backward compatibility NONE and OVERRIDE cgroups
637 * allow detaching with invalid FD (prog==NULL) in legacy mode
638 */
639 return list_first_entry(progs, typeof(*pl), node);
640 }
641
642 if (!prog && !link)
643 /* to detach MULTI prog the user has to specify valid FD
644 * of the program or link to be detached
645 */
646 return ERR_PTR(-EINVAL);
647
648 /* find the prog or link and detach it */
649 list_for_each_entry(pl, progs, node) {
650 if (pl->prog == prog && pl->link == link)
651 return pl;
652 }
653 return ERR_PTR(-ENOENT);
654}
655
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700656/**
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700657 * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700658 * propagate the change to descendants
659 * @cgrp: The cgroup which descendants to traverse
660 * @prog: A program to detach or NULL
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700661 * @prog: A link to detach or NULL
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700662 * @type: Type of detach operation
663 *
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700664 * At most one of @prog or @link can be non-NULL.
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700665 * Must be called with cgroup_mutex held.
666 */
667int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700668 struct bpf_cgroup_link *link, enum bpf_attach_type type)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700669{
670 struct list_head *progs = &cgrp->bpf.progs[type];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700671 u32 flags = cgrp->bpf.flags[type];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700672 struct bpf_prog_list *pl;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700673 struct bpf_prog *old_prog;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700674 int err;
675
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700676 if (prog && link)
677 /* only one of prog or link can be specified */
678 return -EINVAL;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700679
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700680 pl = find_detach_entry(progs, prog, link, flags & BPF_F_ALLOW_MULTI);
681 if (IS_ERR(pl))
682 return PTR_ERR(pl);
683
684 /* mark it deleted, so it's ignored while recomputing effective */
685 old_prog = pl->prog;
686 pl->prog = NULL;
687 pl->link = NULL;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700688
Roman Gushchin85fc4b12018-08-06 14:27:28 -0700689 err = update_effective_progs(cgrp, type);
690 if (err)
691 goto cleanup;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700692
693 /* now can actually delete it from this cgroup list */
694 list_del(&pl->node);
695 kfree(pl);
696 if (list_empty(progs))
697 /* last program was detached, reset flags to zero */
698 cgrp->bpf.flags[type] = 0;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700699 if (old_prog)
700 bpf_prog_put(old_prog);
Stanislav Fomicheva9ed15da2021-01-15 08:35:01 -0800701 static_branch_dec(&cgroup_bpf_enabled_key[type]);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700702 return 0;
703
704cleanup:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700705 /* restore back prog or link */
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700706 pl->prog = old_prog;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700707 pl->link = link;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700708 return err;
Daniel Mack30070982016-11-23 16:52:26 +0100709}
710
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700711/* Must be called with cgroup_mutex held to avoid races. */
712int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
713 union bpf_attr __user *uattr)
714{
715 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
716 enum bpf_attach_type type = attr->query.attach_type;
717 struct list_head *progs = &cgrp->bpf.progs[type];
718 u32 flags = cgrp->bpf.flags[type];
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700719 struct bpf_prog_array *effective;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700720 struct bpf_prog *prog;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700721 int cnt, ret = 0, i;
722
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700723 effective = rcu_dereference_protected(cgrp->bpf.effective[type],
724 lockdep_is_held(&cgroup_mutex));
725
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700726 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700727 cnt = bpf_prog_array_length(effective);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700728 else
729 cnt = prog_list_length(progs);
730
731 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
732 return -EFAULT;
733 if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
734 return -EFAULT;
735 if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
736 /* return early if user requested only program count + flags */
737 return 0;
738 if (attr->query.prog_cnt < cnt) {
739 cnt = attr->query.prog_cnt;
740 ret = -ENOSPC;
741 }
742
743 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
Stanislav Fomichevdbcc1ba2019-05-28 14:14:43 -0700744 return bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700745 } else {
746 struct bpf_prog_list *pl;
747 u32 id;
748
749 i = 0;
750 list_for_each_entry(pl, progs, node) {
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700751 prog = prog_list_prog(pl);
752 id = prog->aux->id;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700753 if (copy_to_user(prog_ids + i, &id, sizeof(id)))
754 return -EFAULT;
755 if (++i == cnt)
756 break;
757 }
758 }
759 return ret;
760}
761
Sean Youngfdb5c452018-06-19 00:04:24 +0100762int cgroup_bpf_prog_attach(const union bpf_attr *attr,
763 enum bpf_prog_type ptype, struct bpf_prog *prog)
764{
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800765 struct bpf_prog *replace_prog = NULL;
Sean Youngfdb5c452018-06-19 00:04:24 +0100766 struct cgroup *cgrp;
767 int ret;
768
769 cgrp = cgroup_get_from_fd(attr->target_fd);
770 if (IS_ERR(cgrp))
771 return PTR_ERR(cgrp);
772
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800773 if ((attr->attach_flags & BPF_F_ALLOW_MULTI) &&
774 (attr->attach_flags & BPF_F_REPLACE)) {
775 replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype);
776 if (IS_ERR(replace_prog)) {
777 cgroup_put(cgrp);
778 return PTR_ERR(replace_prog);
779 }
780 }
781
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700782 ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL,
783 attr->attach_type, attr->attach_flags);
Andrey Ignatov7dd68b32019-12-18 23:44:35 -0800784
785 if (replace_prog)
786 bpf_prog_put(replace_prog);
Sean Youngfdb5c452018-06-19 00:04:24 +0100787 cgroup_put(cgrp);
788 return ret;
789}
790
791int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
792{
793 struct bpf_prog *prog;
794 struct cgroup *cgrp;
795 int ret;
796
797 cgrp = cgroup_get_from_fd(attr->target_fd);
798 if (IS_ERR(cgrp))
799 return PTR_ERR(cgrp);
800
801 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
802 if (IS_ERR(prog))
803 prog = NULL;
804
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700805 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type);
Sean Youngfdb5c452018-06-19 00:04:24 +0100806 if (prog)
807 bpf_prog_put(prog);
808
809 cgroup_put(cgrp);
810 return ret;
811}
812
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700813static void bpf_cgroup_link_release(struct bpf_link *link)
814{
815 struct bpf_cgroup_link *cg_link =
816 container_of(link, struct bpf_cgroup_link, link);
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -0700817 struct cgroup *cg;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700818
819 /* link might have been auto-detached by dying cgroup already,
820 * in that case our work is done here
821 */
822 if (!cg_link->cgroup)
823 return;
824
825 mutex_lock(&cgroup_mutex);
826
827 /* re-check cgroup under lock again */
828 if (!cg_link->cgroup) {
829 mutex_unlock(&cgroup_mutex);
830 return;
831 }
832
833 WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
834 cg_link->type));
835
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -0700836 cg = cg_link->cgroup;
837 cg_link->cgroup = NULL;
838
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700839 mutex_unlock(&cgroup_mutex);
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -0700840
841 cgroup_put(cg);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700842}
843
844static void bpf_cgroup_link_dealloc(struct bpf_link *link)
845{
846 struct bpf_cgroup_link *cg_link =
847 container_of(link, struct bpf_cgroup_link, link);
848
849 kfree(cg_link);
850}
851
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -0700852static int bpf_cgroup_link_detach(struct bpf_link *link)
853{
854 bpf_cgroup_link_release(link);
855
856 return 0;
857}
858
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -0700859static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
860 struct seq_file *seq)
861{
862 struct bpf_cgroup_link *cg_link =
863 container_of(link, struct bpf_cgroup_link, link);
864 u64 cg_id = 0;
865
866 mutex_lock(&cgroup_mutex);
867 if (cg_link->cgroup)
868 cg_id = cgroup_id(cg_link->cgroup);
869 mutex_unlock(&cgroup_mutex);
870
871 seq_printf(seq,
872 "cgroup_id:\t%llu\n"
873 "attach_type:\t%d\n",
874 cg_id,
875 cg_link->type);
876}
877
878static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
879 struct bpf_link_info *info)
880{
881 struct bpf_cgroup_link *cg_link =
882 container_of(link, struct bpf_cgroup_link, link);
883 u64 cg_id = 0;
884
885 mutex_lock(&cgroup_mutex);
886 if (cg_link->cgroup)
887 cg_id = cgroup_id(cg_link->cgroup);
888 mutex_unlock(&cgroup_mutex);
889
890 info->cgroup.cgroup_id = cg_id;
891 info->cgroup.attach_type = cg_link->type;
892 return 0;
893}
894
895static const struct bpf_link_ops bpf_cgroup_link_lops = {
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700896 .release = bpf_cgroup_link_release,
897 .dealloc = bpf_cgroup_link_dealloc,
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -0700898 .detach = bpf_cgroup_link_detach,
Andrii Nakryikof9d04122020-04-28 17:16:05 -0700899 .update_prog = cgroup_bpf_replace,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -0700900 .show_fdinfo = bpf_cgroup_link_show_fdinfo,
901 .fill_link_info = bpf_cgroup_link_fill_link_info,
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700902};
903
904int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
905{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -0700906 struct bpf_link_primer link_primer;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700907 struct bpf_cgroup_link *link;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700908 struct cgroup *cgrp;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -0700909 int err;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700910
911 if (attr->link_create.flags)
912 return -EINVAL;
913
914 cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
915 if (IS_ERR(cgrp))
916 return PTR_ERR(cgrp);
917
918 link = kzalloc(sizeof(*link), GFP_USER);
919 if (!link) {
920 err = -ENOMEM;
921 goto out_put_cgroup;
922 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -0700923 bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops,
924 prog);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700925 link->cgroup = cgrp;
926 link->type = attr->link_create.attach_type;
927
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -0700928 err = bpf_link_prime(&link->link, &link_primer);
929 if (err) {
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700930 kfree(link);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700931 goto out_put_cgroup;
932 }
933
934 err = cgroup_bpf_attach(cgrp, NULL, NULL, link, link->type,
935 BPF_F_ALLOW_MULTI);
936 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -0700937 bpf_link_cleanup(&link_primer);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700938 goto out_put_cgroup;
939 }
940
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -0700941 return bpf_link_settle(&link_primer);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -0700942
943out_put_cgroup:
944 cgroup_put(cgrp);
945 return err;
946}
947
Sean Youngfdb5c452018-06-19 00:04:24 +0100948int cgroup_bpf_prog_query(const union bpf_attr *attr,
949 union bpf_attr __user *uattr)
950{
951 struct cgroup *cgrp;
952 int ret;
953
954 cgrp = cgroup_get_from_fd(attr->query.target_fd);
955 if (IS_ERR(cgrp))
956 return PTR_ERR(cgrp);
957
958 ret = cgroup_bpf_query(cgrp, attr, uattr);
959
960 cgroup_put(cgrp);
961 return ret;
962}
963
Daniel Mack30070982016-11-23 16:52:26 +0100964/**
David Ahernb2cd1252016-12-01 08:48:03 -0800965 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
Willem de Bruijn8f917bb2017-04-11 14:08:08 -0400966 * @sk: The socket sending or receiving traffic
Daniel Mack30070982016-11-23 16:52:26 +0100967 * @skb: The skb that is being sent or received
968 * @type: The type of program to be exectuted
969 *
970 * If no socket is passed, or the socket is not of type INET or INET6,
971 * this function does nothing and returns 0.
972 *
973 * The program type passed in via @type must be suitable for network
974 * filtering. No further check is performed to assert that.
975 *
brakmoe7a31602019-05-28 16:59:37 -0700976 * For egress packets, this function can return:
977 * NET_XMIT_SUCCESS (0) - continue with packet output
978 * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
979 * NET_XMIT_CN (2) - continue with packet output and notify TCP
980 * to call cwr
981 * -EPERM - drop packet
982 *
983 * For ingress packets, this function will return -EPERM if any
984 * attached program was found and if it returned != 1 during execution.
985 * Otherwise 0 is returned.
Daniel Mack30070982016-11-23 16:52:26 +0100986 */
David Ahernb2cd1252016-12-01 08:48:03 -0800987int __cgroup_bpf_run_filter_skb(struct sock *sk,
988 struct sk_buff *skb,
989 enum bpf_attach_type type)
Daniel Mack30070982016-11-23 16:52:26 +0100990{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700991 unsigned int offset = skb->data - skb_network_header(skb);
992 struct sock *save_sk;
Song Liub39b5f42018-10-19 09:57:57 -0700993 void *saved_data_end;
Daniel Mack30070982016-11-23 16:52:26 +0100994 struct cgroup *cgrp;
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700995 int ret;
Daniel Mack30070982016-11-23 16:52:26 +0100996
997 if (!sk || !sk_fullsock(sk))
998 return 0;
999
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001000 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
Daniel Mack30070982016-11-23 16:52:26 +01001001 return 0;
1002
1003 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001004 save_sk = skb->sk;
1005 skb->sk = sk;
1006 __skb_push(skb, offset);
Song Liub39b5f42018-10-19 09:57:57 -07001007
1008 /* compute pointers for the bpf prog */
1009 bpf_compute_and_save_data_end(skb, &saved_data_end);
1010
brakmoe7a31602019-05-28 16:59:37 -07001011 if (type == BPF_CGROUP_INET_EGRESS) {
1012 ret = BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(
1013 cgrp->bpf.effective[type], skb, __bpf_prog_run_save_cb);
1014 } else {
1015 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], skb,
1016 __bpf_prog_run_save_cb);
1017 ret = (ret == 1 ? 0 : -EPERM);
1018 }
Song Liub39b5f42018-10-19 09:57:57 -07001019 bpf_restore_data_end(skb, saved_data_end);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001020 __skb_pull(skb, offset);
1021 skb->sk = save_sk;
brakmoe7a31602019-05-28 16:59:37 -07001022
1023 return ret;
Daniel Mack30070982016-11-23 16:52:26 +01001024}
David Ahernb2cd1252016-12-01 08:48:03 -08001025EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
David Ahern610236582016-12-01 08:48:04 -08001026
1027/**
1028 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
1029 * @sk: sock structure to manipulate
1030 * @type: The type of program to be exectuted
1031 *
1032 * socket is passed is expected to be of type INET or INET6.
1033 *
1034 * The program type passed in via @type must be suitable for sock
1035 * filtering. No further check is performed to assert that.
1036 *
1037 * This function will return %-EPERM if any if an attached program was found
1038 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1039 */
1040int __cgroup_bpf_run_filter_sk(struct sock *sk,
1041 enum bpf_attach_type type)
1042{
1043 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001044 int ret;
David Ahern610236582016-12-01 08:48:04 -08001045
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001046 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sk, BPF_PROG_RUN);
1047 return ret == 1 ? 0 : -EPERM;
David Ahern610236582016-12-01 08:48:04 -08001048}
1049EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001050
1051/**
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001052 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
1053 * provided by user sockaddr
1054 * @sk: sock struct that will use sockaddr
1055 * @uaddr: sockaddr struct provided by user
1056 * @type: The type of program to be exectuted
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001057 * @t_ctx: Pointer to attach type specific context
Stanislav Fomichev77241212021-01-27 11:31:39 -08001058 * @flags: Pointer to u32 which contains higher bits of BPF program
1059 * return value (OR'ed together).
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001060 *
1061 * socket is expected to be of type INET or INET6.
1062 *
1063 * This function will return %-EPERM if an attached program is found and
1064 * returned value != 1 during execution. In all other cases, 0 is returned.
1065 */
1066int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
1067 struct sockaddr *uaddr,
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001068 enum bpf_attach_type type,
Stanislav Fomichev77241212021-01-27 11:31:39 -08001069 void *t_ctx,
1070 u32 *flags)
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001071{
1072 struct bpf_sock_addr_kern ctx = {
1073 .sk = sk,
1074 .uaddr = uaddr,
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001075 .t_ctx = t_ctx,
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001076 };
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001077 struct sockaddr_storage unspec;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001078 struct cgroup *cgrp;
1079 int ret;
1080
1081 /* Check socket family since not all sockets represent network
1082 * endpoint (e.g. AF_UNIX).
1083 */
1084 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1085 return 0;
1086
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001087 if (!ctx.uaddr) {
1088 memset(&unspec, 0, sizeof(unspec));
1089 ctx.uaddr = (struct sockaddr *)&unspec;
1090 }
1091
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001092 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
Stanislav Fomichev77241212021-01-27 11:31:39 -08001093 ret = BPF_PROG_RUN_ARRAY_FLAGS(cgrp->bpf.effective[type], &ctx,
1094 BPF_PROG_RUN, flags);
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001095
1096 return ret == 1 ? 0 : -EPERM;
1097}
1098EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
1099
1100/**
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001101 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
1102 * @sk: socket to get cgroup from
1103 * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
1104 * sk with connection information (IP addresses, etc.) May not contain
1105 * cgroup info if it is a req sock.
1106 * @type: The type of program to be exectuted
1107 *
1108 * socket passed is expected to be of type INET or INET6.
1109 *
1110 * The program type passed in via @type must be suitable for sock_ops
1111 * filtering. No further check is performed to assert that.
1112 *
1113 * This function will return %-EPERM if any if an attached program was found
1114 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1115 */
1116int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
1117 struct bpf_sock_ops_kern *sock_ops,
1118 enum bpf_attach_type type)
1119{
1120 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001121 int ret;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001122
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001123 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sock_ops,
1124 BPF_PROG_RUN);
1125 return ret == 1 ? 0 : -EPERM;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001126}
1127EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
Roman Gushchinebc614f2017-11-05 08:15:32 -05001128
1129int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
1130 short access, enum bpf_attach_type type)
1131{
1132 struct cgroup *cgrp;
1133 struct bpf_cgroup_dev_ctx ctx = {
1134 .access_type = (access << 16) | dev_type,
1135 .major = major,
1136 .minor = minor,
1137 };
1138 int allow = 1;
1139
1140 rcu_read_lock();
1141 cgrp = task_dfl_cgroup(current);
1142 allow = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx,
1143 BPF_PROG_RUN);
1144 rcu_read_unlock();
1145
1146 return !allow;
1147}
Roman Gushchinebc614f2017-11-05 08:15:32 -05001148
1149static const struct bpf_func_proto *
Andrey Ignatovb1cd6092019-03-12 09:27:09 -07001150cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Roman Gushchinebc614f2017-11-05 08:15:32 -05001151{
1152 switch (func_id) {
Roman Gushchinebc614f2017-11-05 08:15:32 -05001153 case BPF_FUNC_get_current_uid_gid:
1154 return &bpf_get_current_uid_gid_proto;
Roman Gushchincd339432018-08-02 14:27:24 -07001155 case BPF_FUNC_get_local_storage:
1156 return &bpf_get_local_storage_proto;
Yonghong Song5bf7a602018-09-27 14:37:30 -07001157 case BPF_FUNC_get_current_cgroup_id:
1158 return &bpf_get_current_cgroup_id_proto;
Stanislav Fomichev0456ea12020-04-20 10:46:10 -07001159 case BPF_FUNC_perf_event_output:
1160 return &bpf_event_output_data_proto;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001161 default:
Stanislav Fomichev0456ea12020-04-20 10:46:10 -07001162 return bpf_base_func_proto(func_id);
Roman Gushchinebc614f2017-11-05 08:15:32 -05001163 }
1164}
1165
Andrey Ignatovb1cd6092019-03-12 09:27:09 -07001166static const struct bpf_func_proto *
1167cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1168{
1169 return cgroup_base_func_proto(func_id, prog);
1170}
1171
Roman Gushchinebc614f2017-11-05 08:15:32 -05001172static bool cgroup_dev_is_valid_access(int off, int size,
1173 enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001174 const struct bpf_prog *prog,
Roman Gushchinebc614f2017-11-05 08:15:32 -05001175 struct bpf_insn_access_aux *info)
1176{
Yonghong Song06ef0cc2017-12-18 10:13:44 -08001177 const int size_default = sizeof(__u32);
1178
Roman Gushchinebc614f2017-11-05 08:15:32 -05001179 if (type == BPF_WRITE)
1180 return false;
1181
1182 if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
1183 return false;
1184 /* The verifier guarantees that size > 0. */
1185 if (off % size != 0)
1186 return false;
Yonghong Song06ef0cc2017-12-18 10:13:44 -08001187
1188 switch (off) {
1189 case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
1190 bpf_ctx_record_field_size(info, size_default);
1191 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
1192 return false;
1193 break;
1194 default:
1195 if (size != size_default)
1196 return false;
1197 }
Roman Gushchinebc614f2017-11-05 08:15:32 -05001198
1199 return true;
1200}
1201
1202const struct bpf_prog_ops cg_dev_prog_ops = {
1203};
1204
1205const struct bpf_verifier_ops cg_dev_verifier_ops = {
1206 .get_func_proto = cgroup_dev_func_proto,
1207 .is_valid_access = cgroup_dev_is_valid_access,
1208};
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001209
1210/**
1211 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
1212 *
1213 * @head: sysctl table header
1214 * @table: sysctl table
1215 * @write: sysctl is being read (= 0) or written (= 1)
Christoph Hellwig32927392020-04-24 08:43:38 +02001216 * @buf: pointer to buffer (in and out)
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001217 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
1218 * result is size of @new_buf if program set new value, initial value
1219 * otherwise
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001220 * @ppos: value-result argument: value is position at which read from or write
1221 * to sysctl is happening, result is new position if program overrode it,
1222 * initial value otherwise
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001223 * @type: type of program to be executed
1224 *
1225 * Program is run when sysctl is being accessed, either read or written, and
1226 * can allow or deny such access.
1227 *
1228 * This function will return %-EPERM if an attached program is found and
1229 * returned value != 1 during execution. In all other cases 0 is returned.
1230 */
1231int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
1232 struct ctl_table *table, int write,
Matthew Wilcox (Oracle)4bd6a732020-09-03 16:22:32 +02001233 char **buf, size_t *pcount, loff_t *ppos,
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001234 enum bpf_attach_type type)
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001235{
1236 struct bpf_sysctl_kern ctx = {
1237 .head = head,
1238 .table = table,
1239 .write = write,
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001240 .ppos = ppos,
Andrey Ignatov1d11b302019-02-28 19:22:15 -08001241 .cur_val = NULL,
1242 .cur_len = PAGE_SIZE,
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001243 .new_val = NULL,
1244 .new_len = 0,
1245 .new_updated = 0,
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001246 };
1247 struct cgroup *cgrp;
Christoph Hellwig32927392020-04-24 08:43:38 +02001248 loff_t pos = 0;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001249 int ret;
1250
Andrey Ignatov1d11b302019-02-28 19:22:15 -08001251 ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
Christoph Hellwig32927392020-04-24 08:43:38 +02001252 if (!ctx.cur_val ||
1253 table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) {
Andrey Ignatov1d11b302019-02-28 19:22:15 -08001254 /* Let BPF program decide how to proceed. */
1255 ctx.cur_len = 0;
1256 }
1257
Christoph Hellwig32927392020-04-24 08:43:38 +02001258 if (write && *buf && *pcount) {
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001259 /* BPF program should be able to override new value with a
1260 * buffer bigger than provided by user.
1261 */
1262 ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
Andrey Ignatov51356ac2019-04-12 16:01:01 -07001263 ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
Christoph Hellwig32927392020-04-24 08:43:38 +02001264 if (ctx.new_val) {
1265 memcpy(ctx.new_val, *buf, ctx.new_len);
1266 } else {
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001267 /* Let BPF program decide how to proceed. */
1268 ctx.new_len = 0;
Christoph Hellwig32927392020-04-24 08:43:38 +02001269 }
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001270 }
1271
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001272 rcu_read_lock();
1273 cgrp = task_dfl_cgroup(current);
1274 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx, BPF_PROG_RUN);
1275 rcu_read_unlock();
1276
Andrey Ignatov1d11b302019-02-28 19:22:15 -08001277 kfree(ctx.cur_val);
1278
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001279 if (ret == 1 && ctx.new_updated) {
Christoph Hellwig32927392020-04-24 08:43:38 +02001280 kfree(*buf);
1281 *buf = ctx.new_val;
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001282 *pcount = ctx.new_len;
1283 } else {
1284 kfree(ctx.new_val);
1285 }
1286
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001287 return ret == 1 ? 0 : -EPERM;
1288}
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001289
YueHaibing6705fea2019-07-03 16:26:30 +08001290#ifdef CONFIG_NET
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001291static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,
1292 enum bpf_attach_type attach_type)
1293{
1294 struct bpf_prog_array *prog_array;
1295 bool empty;
1296
1297 rcu_read_lock();
1298 prog_array = rcu_dereference(cgrp->bpf.effective[attach_type]);
1299 empty = bpf_prog_array_is_empty(prog_array);
1300 rcu_read_unlock();
1301
1302 return empty;
1303}
1304
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001305static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
1306 struct bpf_sockopt_buf *buf)
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001307{
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001308 if (unlikely(max_optlen < 0))
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001309 return -EINVAL;
1310
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001311 if (unlikely(max_optlen > PAGE_SIZE)) {
1312 /* We don't expose optvals that are greater than PAGE_SIZE
1313 * to the BPF program.
1314 */
1315 max_optlen = PAGE_SIZE;
1316 }
1317
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001318 if (max_optlen <= sizeof(buf->data)) {
1319 /* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
1320 * bytes avoid the cost of kzalloc.
1321 */
1322 ctx->optval = buf->data;
1323 ctx->optval_end = ctx->optval + max_optlen;
1324 return max_optlen;
1325 }
1326
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001327 ctx->optval = kzalloc(max_optlen, GFP_USER);
1328 if (!ctx->optval)
1329 return -ENOMEM;
1330
1331 ctx->optval_end = ctx->optval + max_optlen;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001332
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001333 return max_optlen;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001334}
1335
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001336static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
1337 struct bpf_sockopt_buf *buf)
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001338{
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001339 if (ctx->optval == buf->data)
1340 return;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001341 kfree(ctx->optval);
1342}
1343
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001344static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
1345 struct bpf_sockopt_buf *buf)
1346{
1347 return ctx->optval != buf->data;
1348}
1349
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001350int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
1351 int *optname, char __user *optval,
1352 int *optlen, char **kernel_optval)
1353{
1354 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001355 struct bpf_sockopt_buf buf = {};
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001356 struct bpf_sockopt_kern ctx = {
1357 .sk = sk,
1358 .level = *level,
1359 .optname = *optname,
1360 };
Stanislav Fomichev9babe822019-07-29 14:51:10 -07001361 int ret, max_optlen;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001362
1363 /* Opportunistic check to see whether we have any BPF program
1364 * attached to the hook so we don't waste time allocating
1365 * memory and locking the socket.
1366 */
Stanislav Fomicheva9ed15da2021-01-15 08:35:01 -08001367 if (__cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_SETSOCKOPT))
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001368 return 0;
1369
Stanislav Fomichev9babe822019-07-29 14:51:10 -07001370 /* Allocate a bit more than the initial user buffer for
1371 * BPF program. The canonical use case is overriding
1372 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
1373 */
1374 max_optlen = max_t(int, 16, *optlen);
1375
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001376 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001377 if (max_optlen < 0)
1378 return max_optlen;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001379
Stanislav Fomichev9babe822019-07-29 14:51:10 -07001380 ctx.optlen = *optlen;
1381
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001382 if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001383 ret = -EFAULT;
1384 goto out;
1385 }
1386
1387 lock_sock(sk);
1388 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_SETSOCKOPT],
1389 &ctx, BPF_PROG_RUN);
1390 release_sock(sk);
1391
1392 if (!ret) {
1393 ret = -EPERM;
1394 goto out;
1395 }
1396
1397 if (ctx.optlen == -1) {
1398 /* optlen set to -1, bypass kernel */
1399 ret = 1;
Stanislav Fomichev9babe822019-07-29 14:51:10 -07001400 } else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001401 /* optlen is out of bounds */
1402 ret = -EFAULT;
1403 } else {
1404 /* optlen within bounds, run kernel handler */
1405 ret = 0;
1406
1407 /* export any potential modifications */
1408 *level = ctx.level;
1409 *optname = ctx.optname;
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001410
1411 /* optlen == 0 from BPF indicates that we should
1412 * use original userspace data.
1413 */
1414 if (ctx.optlen != 0) {
1415 *optlen = ctx.optlen;
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001416 /* We've used bpf_sockopt_kern->buf as an intermediary
1417 * storage, but the BPF program indicates that we need
1418 * to pass this data to the kernel setsockopt handler.
1419 * No way to export on-stack buf, have to allocate a
1420 * new buffer.
1421 */
1422 if (!sockopt_buf_allocated(&ctx, &buf)) {
1423 void *p = kmalloc(ctx.optlen, GFP_USER);
1424
1425 if (!p) {
1426 ret = -ENOMEM;
1427 goto out;
1428 }
1429 memcpy(p, ctx.optval, ctx.optlen);
1430 *kernel_optval = p;
1431 } else {
1432 *kernel_optval = ctx.optval;
1433 }
Stanislav Fomichev4be34f32021-01-12 08:28:29 -08001434 /* export and don't free sockopt buf */
1435 return 0;
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001436 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001437 }
1438
1439out:
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001440 sockopt_free_buf(&ctx, &buf);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001441 return ret;
1442}
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001443
1444int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
1445 int optname, char __user *optval,
1446 int __user *optlen, int max_optlen,
1447 int retval)
1448{
1449 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001450 struct bpf_sockopt_buf buf = {};
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001451 struct bpf_sockopt_kern ctx = {
1452 .sk = sk,
1453 .level = level,
1454 .optname = optname,
1455 .retval = retval,
1456 };
1457 int ret;
1458
1459 /* Opportunistic check to see whether we have any BPF program
1460 * attached to the hook so we don't waste time allocating
1461 * memory and locking the socket.
1462 */
Stanislav Fomicheva9ed15da2021-01-15 08:35:01 -08001463 if (__cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_GETSOCKOPT))
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001464 return retval;
1465
Stanislav Fomichev9babe822019-07-29 14:51:10 -07001466 ctx.optlen = max_optlen;
1467
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001468 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001469 if (max_optlen < 0)
1470 return max_optlen;
1471
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001472 if (!retval) {
1473 /* If kernel getsockopt finished successfully,
1474 * copy whatever was returned to the user back
1475 * into our temporary buffer. Set optlen to the
1476 * one that kernel returned as well to let
1477 * BPF programs inspect the value.
1478 */
1479
1480 if (get_user(ctx.optlen, optlen)) {
1481 ret = -EFAULT;
1482 goto out;
1483 }
1484
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001485 if (copy_from_user(ctx.optval, optval,
1486 min(ctx.optlen, max_optlen)) != 0) {
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001487 ret = -EFAULT;
1488 goto out;
1489 }
1490 }
1491
1492 lock_sock(sk);
1493 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_GETSOCKOPT],
1494 &ctx, BPF_PROG_RUN);
1495 release_sock(sk);
1496
1497 if (!ret) {
1498 ret = -EPERM;
1499 goto out;
1500 }
1501
1502 if (ctx.optlen > max_optlen) {
1503 ret = -EFAULT;
1504 goto out;
1505 }
1506
1507 /* BPF programs only allowed to set retval to 0, not some
1508 * arbitrary value.
1509 */
1510 if (ctx.retval != 0 && ctx.retval != retval) {
1511 ret = -EFAULT;
1512 goto out;
1513 }
1514
Stanislav Fomichevd8fe4492020-06-16 18:04:14 -07001515 if (ctx.optlen != 0) {
1516 if (copy_to_user(optval, ctx.optval, ctx.optlen) ||
1517 put_user(ctx.optlen, optlen)) {
1518 ret = -EFAULT;
1519 goto out;
1520 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001521 }
1522
1523 ret = ctx.retval;
1524
1525out:
Stanislav Fomichev20f25052021-01-15 08:35:00 -08001526 sockopt_free_buf(&ctx, &buf);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001527 return ret;
1528}
Stanislav Fomichev9cacf812021-01-15 08:34:59 -08001529
1530int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
1531 int optname, void *optval,
1532 int *optlen, int retval)
1533{
1534 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1535 struct bpf_sockopt_kern ctx = {
1536 .sk = sk,
1537 .level = level,
1538 .optname = optname,
1539 .retval = retval,
1540 .optlen = *optlen,
1541 .optval = optval,
1542 .optval_end = optval + *optlen,
1543 };
1544 int ret;
1545
1546 /* Note that __cgroup_bpf_run_filter_getsockopt doesn't copy
1547 * user data back into BPF buffer when reval != 0. This is
1548 * done as an optimization to avoid extra copy, assuming
1549 * kernel won't populate the data in case of an error.
1550 * Here we always pass the data and memset() should
1551 * be called if that data shouldn't be "exported".
1552 */
1553
1554 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_GETSOCKOPT],
1555 &ctx, BPF_PROG_RUN);
1556 if (!ret)
1557 return -EPERM;
1558
1559 if (ctx.optlen > *optlen)
1560 return -EFAULT;
1561
1562 /* BPF programs only allowed to set retval to 0, not some
1563 * arbitrary value.
1564 */
1565 if (ctx.retval != 0 && ctx.retval != retval)
1566 return -EFAULT;
1567
1568 /* BPF programs can shrink the buffer, export the modifications.
1569 */
1570 if (ctx.optlen != 0)
1571 *optlen = ctx.optlen;
1572
1573 return ctx.retval;
1574}
YueHaibing6705fea2019-07-03 16:26:30 +08001575#endif
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001576
Andrey Ignatov808649f2019-02-27 13:28:48 -08001577static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
1578 size_t *lenp)
1579{
1580 ssize_t tmp_ret = 0, ret;
1581
1582 if (dir->header.parent) {
1583 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
1584 if (tmp_ret < 0)
1585 return tmp_ret;
1586 }
1587
1588 ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
1589 if (ret < 0)
1590 return ret;
1591 *bufp += ret;
1592 *lenp -= ret;
1593 ret += tmp_ret;
1594
1595 /* Avoid leading slash. */
1596 if (!ret)
1597 return ret;
1598
1599 tmp_ret = strscpy(*bufp, "/", *lenp);
1600 if (tmp_ret < 0)
1601 return tmp_ret;
1602 *bufp += tmp_ret;
1603 *lenp -= tmp_ret;
1604
1605 return ret + tmp_ret;
1606}
1607
1608BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
1609 size_t, buf_len, u64, flags)
1610{
1611 ssize_t tmp_ret = 0, ret;
1612
1613 if (!buf)
1614 return -EINVAL;
1615
1616 if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
1617 if (!ctx->head)
1618 return -EINVAL;
1619 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
1620 if (tmp_ret < 0)
1621 return tmp_ret;
1622 }
1623
1624 ret = strscpy(buf, ctx->table->procname, buf_len);
1625
1626 return ret < 0 ? ret : tmp_ret + ret;
1627}
1628
1629static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
1630 .func = bpf_sysctl_get_name,
1631 .gpl_only = false,
1632 .ret_type = RET_INTEGER,
1633 .arg1_type = ARG_PTR_TO_CTX,
1634 .arg2_type = ARG_PTR_TO_MEM,
1635 .arg3_type = ARG_CONST_SIZE,
1636 .arg4_type = ARG_ANYTHING,
1637};
1638
Andrey Ignatov1d11b302019-02-28 19:22:15 -08001639static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
1640 size_t src_len)
1641{
1642 if (!dst)
1643 return -EINVAL;
1644
1645 if (!dst_len)
1646 return -E2BIG;
1647
1648 if (!src || !src_len) {
1649 memset(dst, 0, dst_len);
1650 return -EINVAL;
1651 }
1652
1653 memcpy(dst, src, min(dst_len, src_len));
1654
1655 if (dst_len > src_len) {
1656 memset(dst + src_len, '\0', dst_len - src_len);
1657 return src_len;
1658 }
1659
1660 dst[dst_len - 1] = '\0';
1661
1662 return -E2BIG;
1663}
1664
1665BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
1666 char *, buf, size_t, buf_len)
1667{
1668 return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
1669}
1670
1671static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
1672 .func = bpf_sysctl_get_current_value,
1673 .gpl_only = false,
1674 .ret_type = RET_INTEGER,
1675 .arg1_type = ARG_PTR_TO_CTX,
1676 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1677 .arg3_type = ARG_CONST_SIZE,
1678};
1679
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001680BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
1681 size_t, buf_len)
1682{
1683 if (!ctx->write) {
1684 if (buf && buf_len)
1685 memset(buf, '\0', buf_len);
1686 return -EINVAL;
1687 }
1688 return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
1689}
1690
1691static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
1692 .func = bpf_sysctl_get_new_value,
1693 .gpl_only = false,
1694 .ret_type = RET_INTEGER,
1695 .arg1_type = ARG_PTR_TO_CTX,
1696 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1697 .arg3_type = ARG_CONST_SIZE,
1698};
1699
1700BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
1701 const char *, buf, size_t, buf_len)
1702{
1703 if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
1704 return -EINVAL;
1705
1706 if (buf_len > PAGE_SIZE - 1)
1707 return -E2BIG;
1708
1709 memcpy(ctx->new_val, buf, buf_len);
1710 ctx->new_len = buf_len;
1711 ctx->new_updated = 1;
1712
1713 return 0;
1714}
1715
1716static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
1717 .func = bpf_sysctl_set_new_value,
1718 .gpl_only = false,
1719 .ret_type = RET_INTEGER,
1720 .arg1_type = ARG_PTR_TO_CTX,
1721 .arg2_type = ARG_PTR_TO_MEM,
1722 .arg3_type = ARG_CONST_SIZE,
1723};
1724
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001725static const struct bpf_func_proto *
1726sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1727{
Andrey Ignatov808649f2019-02-27 13:28:48 -08001728 switch (func_id) {
Andrey Ignatovd7a4cb92019-03-18 17:55:26 -07001729 case BPF_FUNC_strtol:
1730 return &bpf_strtol_proto;
1731 case BPF_FUNC_strtoul:
1732 return &bpf_strtoul_proto;
Andrey Ignatov808649f2019-02-27 13:28:48 -08001733 case BPF_FUNC_sysctl_get_name:
1734 return &bpf_sysctl_get_name_proto;
Andrey Ignatov1d11b302019-02-28 19:22:15 -08001735 case BPF_FUNC_sysctl_get_current_value:
1736 return &bpf_sysctl_get_current_value_proto;
Andrey Ignatov4e63acd2019-03-07 18:38:43 -08001737 case BPF_FUNC_sysctl_get_new_value:
1738 return &bpf_sysctl_get_new_value_proto;
1739 case BPF_FUNC_sysctl_set_new_value:
1740 return &bpf_sysctl_set_new_value_proto;
Andrey Ignatov808649f2019-02-27 13:28:48 -08001741 default:
1742 return cgroup_base_func_proto(func_id, prog);
1743 }
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001744}
1745
1746static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
1747 const struct bpf_prog *prog,
1748 struct bpf_insn_access_aux *info)
1749{
1750 const int size_default = sizeof(__u32);
1751
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001752 if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001753 return false;
1754
1755 switch (off) {
Ilya Leoshkevich7541c872019-10-28 13:29:02 +01001756 case bpf_ctx_range(struct bpf_sysctl, write):
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001757 if (type != BPF_READ)
1758 return false;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001759 bpf_ctx_record_field_size(info, size_default);
1760 return bpf_ctx_narrow_access_ok(off, size, size_default);
Ilya Leoshkevich7541c872019-10-28 13:29:02 +01001761 case bpf_ctx_range(struct bpf_sysctl, file_pos):
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001762 if (type == BPF_READ) {
1763 bpf_ctx_record_field_size(info, size_default);
1764 return bpf_ctx_narrow_access_ok(off, size, size_default);
1765 } else {
1766 return size == size_default;
1767 }
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001768 default:
1769 return false;
1770 }
1771}
1772
1773static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
1774 const struct bpf_insn *si,
1775 struct bpf_insn *insn_buf,
1776 struct bpf_prog *prog, u32 *target_size)
1777{
1778 struct bpf_insn *insn = insn_buf;
Ilya Leoshkevichd895a0f2019-08-16 12:53:00 +02001779 u32 read_size;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001780
1781 switch (si->off) {
1782 case offsetof(struct bpf_sysctl, write):
1783 *insn++ = BPF_LDX_MEM(
1784 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
1785 bpf_target_off(struct bpf_sysctl_kern, write,
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001786 sizeof_field(struct bpf_sysctl_kern,
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001787 write),
1788 target_size));
1789 break;
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001790 case offsetof(struct bpf_sysctl, file_pos):
1791 /* ppos is a pointer so it should be accessed via indirect
1792 * loads and stores. Also for stores additional temporary
1793 * register is used since neither src_reg nor dst_reg can be
1794 * overridden.
1795 */
1796 if (type == BPF_WRITE) {
1797 int treg = BPF_REG_9;
1798
1799 if (si->src_reg == treg || si->dst_reg == treg)
1800 --treg;
1801 if (si->src_reg == treg || si->dst_reg == treg)
1802 --treg;
1803 *insn++ = BPF_STX_MEM(
1804 BPF_DW, si->dst_reg, treg,
1805 offsetof(struct bpf_sysctl_kern, tmp_reg));
1806 *insn++ = BPF_LDX_MEM(
1807 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1808 treg, si->dst_reg,
1809 offsetof(struct bpf_sysctl_kern, ppos));
1810 *insn++ = BPF_STX_MEM(
Ilya Leoshkevichd895a0f2019-08-16 12:53:00 +02001811 BPF_SIZEOF(u32), treg, si->src_reg,
1812 bpf_ctx_narrow_access_offset(
1813 0, sizeof(u32), sizeof(loff_t)));
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001814 *insn++ = BPF_LDX_MEM(
1815 BPF_DW, treg, si->dst_reg,
1816 offsetof(struct bpf_sysctl_kern, tmp_reg));
1817 } else {
1818 *insn++ = BPF_LDX_MEM(
1819 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1820 si->dst_reg, si->src_reg,
1821 offsetof(struct bpf_sysctl_kern, ppos));
Ilya Leoshkevichd895a0f2019-08-16 12:53:00 +02001822 read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001823 *insn++ = BPF_LDX_MEM(
Ilya Leoshkevichd895a0f2019-08-16 12:53:00 +02001824 BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
1825 bpf_ctx_narrow_access_offset(
1826 0, read_size, sizeof(loff_t)));
Andrey Ignatove1550bf2019-03-07 18:50:52 -08001827 }
1828 *target_size = sizeof(u32);
1829 break;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001830 }
1831
1832 return insn - insn_buf;
1833}
1834
1835const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
1836 .get_func_proto = sysctl_func_proto,
1837 .is_valid_access = sysctl_is_valid_access,
1838 .convert_ctx_access = sysctl_convert_ctx_access,
1839};
1840
1841const struct bpf_prog_ops cg_sysctl_prog_ops = {
1842};
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001843
1844static const struct bpf_func_proto *
1845cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1846{
1847 switch (func_id) {
YueHaibing6705fea2019-07-03 16:26:30 +08001848#ifdef CONFIG_NET
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001849 case BPF_FUNC_sk_storage_get:
1850 return &bpf_sk_storage_get_proto;
1851 case BPF_FUNC_sk_storage_delete:
1852 return &bpf_sk_storage_delete_proto;
YueHaibing6705fea2019-07-03 16:26:30 +08001853#endif
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001854#ifdef CONFIG_INET
1855 case BPF_FUNC_tcp_sock:
1856 return &bpf_tcp_sock_proto;
1857#endif
1858 default:
1859 return cgroup_base_func_proto(func_id, prog);
1860 }
1861}
1862
1863static bool cg_sockopt_is_valid_access(int off, int size,
1864 enum bpf_access_type type,
1865 const struct bpf_prog *prog,
1866 struct bpf_insn_access_aux *info)
1867{
1868 const int size_default = sizeof(__u32);
1869
1870 if (off < 0 || off >= sizeof(struct bpf_sockopt))
1871 return false;
1872
1873 if (off % size != 0)
1874 return false;
1875
1876 if (type == BPF_WRITE) {
1877 switch (off) {
1878 case offsetof(struct bpf_sockopt, retval):
1879 if (size != size_default)
1880 return false;
1881 return prog->expected_attach_type ==
1882 BPF_CGROUP_GETSOCKOPT;
1883 case offsetof(struct bpf_sockopt, optname):
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001884 fallthrough;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001885 case offsetof(struct bpf_sockopt, level):
1886 if (size != size_default)
1887 return false;
1888 return prog->expected_attach_type ==
1889 BPF_CGROUP_SETSOCKOPT;
1890 case offsetof(struct bpf_sockopt, optlen):
1891 return size == size_default;
1892 default:
1893 return false;
1894 }
1895 }
1896
1897 switch (off) {
1898 case offsetof(struct bpf_sockopt, sk):
1899 if (size != sizeof(__u64))
1900 return false;
1901 info->reg_type = PTR_TO_SOCKET;
1902 break;
1903 case offsetof(struct bpf_sockopt, optval):
1904 if (size != sizeof(__u64))
1905 return false;
1906 info->reg_type = PTR_TO_PACKET;
1907 break;
1908 case offsetof(struct bpf_sockopt, optval_end):
1909 if (size != sizeof(__u64))
1910 return false;
1911 info->reg_type = PTR_TO_PACKET_END;
1912 break;
1913 case offsetof(struct bpf_sockopt, retval):
1914 if (size != size_default)
1915 return false;
1916 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
1917 default:
1918 if (size != size_default)
1919 return false;
1920 break;
1921 }
1922 return true;
1923}
1924
1925#define CG_SOCKOPT_ACCESS_FIELD(T, F) \
1926 T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \
1927 si->dst_reg, si->src_reg, \
1928 offsetof(struct bpf_sockopt_kern, F))
1929
1930static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
1931 const struct bpf_insn *si,
1932 struct bpf_insn *insn_buf,
1933 struct bpf_prog *prog,
1934 u32 *target_size)
1935{
1936 struct bpf_insn *insn = insn_buf;
1937
1938 switch (si->off) {
1939 case offsetof(struct bpf_sockopt, sk):
1940 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk);
1941 break;
1942 case offsetof(struct bpf_sockopt, level):
1943 if (type == BPF_WRITE)
1944 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level);
1945 else
1946 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level);
1947 break;
1948 case offsetof(struct bpf_sockopt, optname):
1949 if (type == BPF_WRITE)
1950 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname);
1951 else
1952 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname);
1953 break;
1954 case offsetof(struct bpf_sockopt, optlen):
1955 if (type == BPF_WRITE)
1956 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen);
1957 else
1958 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen);
1959 break;
1960 case offsetof(struct bpf_sockopt, retval):
1961 if (type == BPF_WRITE)
1962 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, retval);
1963 else
1964 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, retval);
1965 break;
1966 case offsetof(struct bpf_sockopt, optval):
1967 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval);
1968 break;
1969 case offsetof(struct bpf_sockopt, optval_end):
1970 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end);
1971 break;
1972 }
1973
1974 return insn - insn_buf;
1975}
1976
1977static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
1978 bool direct_write,
1979 const struct bpf_prog *prog)
1980{
1981 /* Nothing to do for sockopt argument. The data is kzalloc'ated.
1982 */
1983 return 0;
1984}
1985
1986const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
1987 .get_func_proto = cg_sockopt_func_proto,
1988 .is_valid_access = cg_sockopt_is_valid_access,
1989 .convert_ctx_access = cg_sockopt_convert_ctx_access,
1990 .gen_prologue = cg_sockopt_get_prologue,
1991};
1992
1993const struct bpf_prog_ops cg_sockopt_prog_ops = {
1994};