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