Thomas Gleixner | f85d208 | 2019-06-04 10:10:45 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Functions to manage eBPF programs attached to cgroups |
| 4 | * |
| 5 | * Copyright (c) 2016 Daniel Mack |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/atomic.h> |
| 10 | #include <linux/cgroup.h> |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 11 | #include <linux/filter.h> |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 12 | #include <linux/slab.h> |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 13 | #include <linux/sysctl.h> |
Andrey Ignatov | 808649f | 2019-02-27 13:28:48 -0800 | [diff] [blame] | 14 | #include <linux/string.h> |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 15 | #include <linux/bpf.h> |
| 16 | #include <linux/bpf-cgroup.h> |
| 17 | #include <net/sock.h> |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 18 | #include <net/bpf_sk_storage.h> |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 19 | |
Roman Gushchin | e5c891a | 2019-06-25 14:38:58 -0700 | [diff] [blame] | 20 | #include "../cgroup/cgroup-internal.h" |
| 21 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 22 | DEFINE_STATIC_KEY_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_CGROUP_BPF_ATTACH_TYPE); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 23 | EXPORT_SYMBOL(cgroup_bpf_enabled_key); |
| 24 | |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 25 | void cgroup_bpf_offline(struct cgroup *cgrp) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 26 | { |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 27 | cgroup_get(cgrp); |
| 28 | percpu_ref_kill(&cgrp->bpf.refcnt); |
| 29 | } |
| 30 | |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 31 | static void bpf_cgroup_storages_free(struct bpf_cgroup_storage *storages[]) |
| 32 | { |
| 33 | enum bpf_cgroup_storage_type stype; |
| 34 | |
| 35 | for_each_cgroup_storage_type(stype) |
| 36 | bpf_cgroup_storage_free(storages[stype]); |
| 37 | } |
| 38 | |
| 39 | static int bpf_cgroup_storages_alloc(struct bpf_cgroup_storage *storages[], |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 40 | struct bpf_cgroup_storage *new_storages[], |
| 41 | enum bpf_attach_type type, |
| 42 | struct bpf_prog *prog, |
| 43 | struct cgroup *cgrp) |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 44 | { |
| 45 | enum bpf_cgroup_storage_type stype; |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 46 | struct bpf_cgroup_storage_key key; |
| 47 | struct bpf_map *map; |
| 48 | |
| 49 | key.cgroup_inode_id = cgroup_id(cgrp); |
| 50 | key.attach_type = type; |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 51 | |
| 52 | for_each_cgroup_storage_type(stype) { |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 53 | map = prog->aux->cgroup_storage[stype]; |
| 54 | if (!map) |
| 55 | continue; |
| 56 | |
| 57 | storages[stype] = cgroup_storage_lookup((void *)map, &key, false); |
| 58 | if (storages[stype]) |
| 59 | continue; |
| 60 | |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 61 | storages[stype] = bpf_cgroup_storage_alloc(prog, stype); |
| 62 | if (IS_ERR(storages[stype])) { |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 63 | bpf_cgroup_storages_free(new_storages); |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 64 | return -ENOMEM; |
| 65 | } |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 66 | |
| 67 | new_storages[stype] = storages[stype]; |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static void bpf_cgroup_storages_assign(struct bpf_cgroup_storage *dst[], |
| 74 | struct bpf_cgroup_storage *src[]) |
| 75 | { |
| 76 | enum bpf_cgroup_storage_type stype; |
| 77 | |
| 78 | for_each_cgroup_storage_type(stype) |
| 79 | dst[stype] = src[stype]; |
| 80 | } |
| 81 | |
| 82 | static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[], |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 83 | struct cgroup *cgrp, |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 84 | enum bpf_attach_type attach_type) |
| 85 | { |
| 86 | enum bpf_cgroup_storage_type stype; |
| 87 | |
| 88 | for_each_cgroup_storage_type(stype) |
| 89 | bpf_cgroup_storage_link(storages[stype], cgrp, attach_type); |
| 90 | } |
| 91 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 92 | /* Called when bpf_cgroup_link is auto-detached from dying cgroup. |
| 93 | * It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It |
| 94 | * doesn't free link memory, which will eventually be done by bpf_link's |
| 95 | * release() callback, when its last FD is closed. |
| 96 | */ |
| 97 | static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link) |
| 98 | { |
| 99 | cgroup_put(link->cgroup); |
| 100 | link->cgroup = NULL; |
| 101 | } |
| 102 | |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 103 | /** |
| 104 | * cgroup_bpf_release() - put references of all bpf programs and |
| 105 | * release all cgroup bpf data |
| 106 | * @work: work structure embedded into the cgroup to modify |
| 107 | */ |
| 108 | static void cgroup_bpf_release(struct work_struct *work) |
| 109 | { |
Roman Gushchin | e10360f | 2019-12-27 13:50:34 -0800 | [diff] [blame] | 110 | struct cgroup *p, *cgrp = container_of(work, struct cgroup, |
| 111 | bpf.release_work); |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 112 | struct bpf_prog_array *old_array; |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 113 | struct list_head *storages = &cgrp->bpf.storages; |
| 114 | struct bpf_cgroup_storage *storage, *stmp; |
| 115 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 116 | unsigned int atype; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 117 | |
Roman Gushchin | e5c891a | 2019-06-25 14:38:58 -0700 | [diff] [blame] | 118 | mutex_lock(&cgroup_mutex); |
| 119 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 120 | for (atype = 0; atype < ARRAY_SIZE(cgrp->bpf.progs); atype++) { |
| 121 | struct list_head *progs = &cgrp->bpf.progs[atype]; |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 122 | struct bpf_prog_list *pl, *pltmp; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 123 | |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 124 | list_for_each_entry_safe(pl, pltmp, progs, node) { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 125 | list_del(&pl->node); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 126 | if (pl->prog) |
| 127 | bpf_prog_put(pl->prog); |
| 128 | if (pl->link) |
| 129 | bpf_cgroup_link_auto_detach(pl->link); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 130 | kfree(pl); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 131 | static_branch_dec(&cgroup_bpf_enabled_key[atype]); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 132 | } |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 133 | old_array = rcu_dereference_protected( |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 134 | cgrp->bpf.effective[atype], |
Roman Gushchin | e5c891a | 2019-06-25 14:38:58 -0700 | [diff] [blame] | 135 | lockdep_is_held(&cgroup_mutex)); |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 136 | bpf_prog_array_free(old_array); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 137 | } |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 138 | |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 139 | list_for_each_entry_safe(storage, stmp, storages, list_cg) { |
| 140 | bpf_cgroup_storage_unlink(storage); |
| 141 | bpf_cgroup_storage_free(storage); |
| 142 | } |
| 143 | |
Roman Gushchin | e5c891a | 2019-06-25 14:38:58 -0700 | [diff] [blame] | 144 | mutex_unlock(&cgroup_mutex); |
| 145 | |
Roman Gushchin | e10360f | 2019-12-27 13:50:34 -0800 | [diff] [blame] | 146 | for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p)) |
| 147 | cgroup_bpf_put(p); |
| 148 | |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 149 | percpu_ref_exit(&cgrp->bpf.refcnt); |
| 150 | cgroup_put(cgrp); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * cgroup_bpf_release_fn() - callback used to schedule releasing |
| 155 | * of bpf cgroup data |
| 156 | * @ref: percpu ref counter structure |
| 157 | */ |
| 158 | static void cgroup_bpf_release_fn(struct percpu_ref *ref) |
| 159 | { |
| 160 | struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt); |
| 161 | |
| 162 | INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release); |
| 163 | queue_work(system_wq, &cgrp->bpf.release_work); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 166 | /* Get underlying bpf_prog of bpf_prog_list entry, regardless if it's through |
| 167 | * link or direct prog. |
| 168 | */ |
| 169 | static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl) |
| 170 | { |
| 171 | if (pl->prog) |
| 172 | return pl->prog; |
| 173 | if (pl->link) |
| 174 | return pl->link->link.prog; |
| 175 | return NULL; |
| 176 | } |
| 177 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 178 | /* count number of elements in the list. |
| 179 | * it's slow but the list cannot be long |
| 180 | */ |
| 181 | static u32 prog_list_length(struct list_head *head) |
| 182 | { |
| 183 | struct bpf_prog_list *pl; |
| 184 | u32 cnt = 0; |
| 185 | |
| 186 | list_for_each_entry(pl, head, node) { |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 187 | if (!prog_list_prog(pl)) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 188 | continue; |
| 189 | cnt++; |
| 190 | } |
| 191 | return cnt; |
| 192 | } |
| 193 | |
| 194 | /* if parent has non-overridable prog attached, |
| 195 | * disallow attaching new programs to the descendent cgroup. |
| 196 | * if parent has overridable or multi-prog, allow attaching |
| 197 | */ |
| 198 | static bool hierarchy_allows_attach(struct cgroup *cgrp, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 199 | enum cgroup_bpf_attach_type atype) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 200 | { |
| 201 | struct cgroup *p; |
| 202 | |
| 203 | p = cgroup_parent(cgrp); |
| 204 | if (!p) |
| 205 | return true; |
| 206 | do { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 207 | u32 flags = p->bpf.flags[atype]; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 208 | u32 cnt; |
| 209 | |
| 210 | if (flags & BPF_F_ALLOW_MULTI) |
| 211 | return true; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 212 | cnt = prog_list_length(&p->bpf.progs[atype]); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 213 | WARN_ON_ONCE(cnt > 1); |
| 214 | if (cnt == 1) |
| 215 | return !!(flags & BPF_F_ALLOW_OVERRIDE); |
| 216 | p = cgroup_parent(p); |
| 217 | } while (p); |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | /* compute a chain of effective programs for a given cgroup: |
| 222 | * start from the list of programs in this cgroup and add |
| 223 | * all parent programs. |
| 224 | * Note that parent's F_ALLOW_OVERRIDE-type program is yielding |
| 225 | * to programs in this cgroup |
| 226 | */ |
| 227 | static int compute_effective_progs(struct cgroup *cgrp, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 228 | enum cgroup_bpf_attach_type atype, |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 229 | struct bpf_prog_array **array) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 230 | { |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 231 | struct bpf_prog_array_item *item; |
Roman Gushchin | 3960f4f | 2018-07-13 12:41:11 -0700 | [diff] [blame] | 232 | struct bpf_prog_array *progs; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 233 | struct bpf_prog_list *pl; |
| 234 | struct cgroup *p = cgrp; |
| 235 | int cnt = 0; |
| 236 | |
| 237 | /* count number of effective programs by walking parents */ |
| 238 | do { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 239 | if (cnt == 0 || (p->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) |
| 240 | cnt += prog_list_length(&p->bpf.progs[atype]); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 241 | p = cgroup_parent(p); |
| 242 | } while (p); |
| 243 | |
| 244 | progs = bpf_prog_array_alloc(cnt, GFP_KERNEL); |
| 245 | if (!progs) |
| 246 | return -ENOMEM; |
| 247 | |
| 248 | /* populate the array with effective progs */ |
| 249 | cnt = 0; |
| 250 | p = cgrp; |
| 251 | do { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 252 | if (cnt > 0 && !(p->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 253 | continue; |
| 254 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 255 | list_for_each_entry(pl, &p->bpf.progs[atype], node) { |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 256 | if (!prog_list_prog(pl)) |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 257 | continue; |
| 258 | |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 259 | item = &progs->items[cnt]; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 260 | item->prog = prog_list_prog(pl); |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 261 | bpf_cgroup_storages_assign(item->cgroup_storage, |
| 262 | pl->storage); |
Roman Gushchin | 394e40a | 2018-08-02 14:27:21 -0700 | [diff] [blame] | 263 | cnt++; |
| 264 | } |
| 265 | } while ((p = cgroup_parent(p))); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 266 | |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 267 | *array = progs; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static void activate_effective_progs(struct cgroup *cgrp, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 272 | enum cgroup_bpf_attach_type atype, |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 273 | struct bpf_prog_array *old_array) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 274 | { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 275 | old_array = rcu_replace_pointer(cgrp->bpf.effective[atype], old_array, |
Paul E. McKenney | 6092f726 | 2019-09-23 15:37:04 -0700 | [diff] [blame] | 276 | lockdep_is_held(&cgroup_mutex)); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 277 | /* free prog array after grace period, since __cgroup_bpf_run_*() |
| 278 | * might be still walking the array |
| 279 | */ |
| 280 | bpf_prog_array_free(old_array); |
| 281 | } |
| 282 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 283 | /** |
| 284 | * cgroup_bpf_inherit() - inherit effective programs from parent |
| 285 | * @cgrp: the cgroup to modify |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 286 | */ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 287 | int cgroup_bpf_inherit(struct cgroup *cgrp) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 288 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 289 | /* has to use marco instead of const int, since compiler thinks |
| 290 | * that array below is variable length |
| 291 | */ |
| 292 | #define NR ARRAY_SIZE(cgrp->bpf.effective) |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 293 | struct bpf_prog_array *arrays[NR] = {}; |
Roman Gushchin | e10360f | 2019-12-27 13:50:34 -0800 | [diff] [blame] | 294 | struct cgroup *p; |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 295 | int ret, i; |
| 296 | |
| 297 | ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0, |
| 298 | GFP_KERNEL); |
| 299 | if (ret) |
| 300 | return ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 301 | |
Roman Gushchin | e10360f | 2019-12-27 13:50:34 -0800 | [diff] [blame] | 302 | for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p)) |
| 303 | cgroup_bpf_get(p); |
| 304 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 305 | for (i = 0; i < NR; i++) |
| 306 | INIT_LIST_HEAD(&cgrp->bpf.progs[i]); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 307 | |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 308 | INIT_LIST_HEAD(&cgrp->bpf.storages); |
| 309 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 310 | for (i = 0; i < NR; i++) |
| 311 | if (compute_effective_progs(cgrp, i, &arrays[i])) |
| 312 | goto cleanup; |
| 313 | |
| 314 | for (i = 0; i < NR; i++) |
| 315 | activate_effective_progs(cgrp, i, arrays[i]); |
| 316 | |
| 317 | return 0; |
| 318 | cleanup: |
| 319 | for (i = 0; i < NR; i++) |
| 320 | bpf_prog_array_free(arrays[i]); |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 321 | |
Andrii Nakryiko | 1d8006a | 2020-03-09 15:40:17 -0700 | [diff] [blame] | 322 | for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p)) |
| 323 | cgroup_bpf_put(p); |
| 324 | |
Roman Gushchin | 4bfc0bb | 2019-05-25 09:37:39 -0700 | [diff] [blame] | 325 | percpu_ref_exit(&cgrp->bpf.refcnt); |
| 326 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 327 | return -ENOMEM; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Roman Gushchin | 85fc4b1 | 2018-08-06 14:27:28 -0700 | [diff] [blame] | 330 | static int update_effective_progs(struct cgroup *cgrp, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 331 | enum cgroup_bpf_attach_type atype) |
Roman Gushchin | 85fc4b1 | 2018-08-06 14:27:28 -0700 | [diff] [blame] | 332 | { |
| 333 | struct cgroup_subsys_state *css; |
| 334 | int err; |
| 335 | |
| 336 | /* allocate and recompute effective prog arrays */ |
| 337 | css_for_each_descendant_pre(css, &cgrp->self) { |
| 338 | struct cgroup *desc = container_of(css, struct cgroup, self); |
| 339 | |
Roman Gushchin | e5c891a | 2019-06-25 14:38:58 -0700 | [diff] [blame] | 340 | if (percpu_ref_is_zero(&desc->bpf.refcnt)) |
| 341 | continue; |
| 342 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 343 | err = compute_effective_progs(desc, atype, &desc->bpf.inactive); |
Roman Gushchin | 85fc4b1 | 2018-08-06 14:27:28 -0700 | [diff] [blame] | 344 | if (err) |
| 345 | goto cleanup; |
| 346 | } |
| 347 | |
| 348 | /* all allocations were successful. Activate all prog arrays */ |
| 349 | css_for_each_descendant_pre(css, &cgrp->self) { |
| 350 | struct cgroup *desc = container_of(css, struct cgroup, self); |
| 351 | |
Roman Gushchin | e5c891a | 2019-06-25 14:38:58 -0700 | [diff] [blame] | 352 | if (percpu_ref_is_zero(&desc->bpf.refcnt)) { |
| 353 | if (unlikely(desc->bpf.inactive)) { |
| 354 | bpf_prog_array_free(desc->bpf.inactive); |
| 355 | desc->bpf.inactive = NULL; |
| 356 | } |
| 357 | continue; |
| 358 | } |
| 359 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 360 | activate_effective_progs(desc, atype, desc->bpf.inactive); |
Roman Gushchin | 85fc4b1 | 2018-08-06 14:27:28 -0700 | [diff] [blame] | 361 | desc->bpf.inactive = NULL; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | |
| 366 | cleanup: |
| 367 | /* oom while computing effective. Free all computed effective arrays |
| 368 | * since they were not activated |
| 369 | */ |
| 370 | css_for_each_descendant_pre(css, &cgrp->self) { |
| 371 | struct cgroup *desc = container_of(css, struct cgroup, self); |
| 372 | |
| 373 | bpf_prog_array_free(desc->bpf.inactive); |
| 374 | desc->bpf.inactive = NULL; |
| 375 | } |
| 376 | |
| 377 | return err; |
| 378 | } |
| 379 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 380 | #define BPF_CGROUP_MAX_PROGS 64 |
| 381 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 382 | static struct bpf_prog_list *find_attach_entry(struct list_head *progs, |
| 383 | struct bpf_prog *prog, |
| 384 | struct bpf_cgroup_link *link, |
| 385 | struct bpf_prog *replace_prog, |
| 386 | bool allow_multi) |
| 387 | { |
| 388 | struct bpf_prog_list *pl; |
| 389 | |
| 390 | /* single-attach case */ |
| 391 | if (!allow_multi) { |
| 392 | if (list_empty(progs)) |
| 393 | return NULL; |
| 394 | return list_first_entry(progs, typeof(*pl), node); |
| 395 | } |
| 396 | |
| 397 | list_for_each_entry(pl, progs, node) { |
Lorenz Bauer | 248e00a | 2020-06-08 17:22:01 +0100 | [diff] [blame] | 398 | if (prog && pl->prog == prog && prog != replace_prog) |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 399 | /* disallow attaching the same prog twice */ |
| 400 | return ERR_PTR(-EINVAL); |
| 401 | if (link && pl->link == link) |
| 402 | /* disallow attaching the same link twice */ |
| 403 | return ERR_PTR(-EINVAL); |
| 404 | } |
| 405 | |
| 406 | /* direct prog multi-attach w/ replacement case */ |
| 407 | if (replace_prog) { |
| 408 | list_for_each_entry(pl, progs, node) { |
| 409 | if (pl->prog == replace_prog) |
| 410 | /* a match found */ |
| 411 | return pl; |
| 412 | } |
| 413 | /* prog to replace not found for cgroup */ |
| 414 | return ERR_PTR(-ENOENT); |
| 415 | } |
| 416 | |
| 417 | return NULL; |
| 418 | } |
| 419 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 420 | /** |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 421 | * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 422 | * propagate the change to descendants |
| 423 | * @cgrp: The cgroup which descendants to traverse |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 424 | * @prog: A program to attach |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 425 | * @link: A link to attach |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 426 | * @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 427 | * @type: Type of attach operation |
Valdis Kletnieks | 1832f4e | 2019-01-29 01:47:06 -0500 | [diff] [blame] | 428 | * @flags: Option flags |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 429 | * |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 430 | * Exactly one of @prog or @link can be non-null. |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 431 | * Must be called with cgroup_mutex held. |
| 432 | */ |
He Fengqing | 588e5d8 | 2021-10-29 02:39:06 +0000 | [diff] [blame] | 433 | static int __cgroup_bpf_attach(struct cgroup *cgrp, |
| 434 | struct bpf_prog *prog, struct bpf_prog *replace_prog, |
| 435 | struct bpf_cgroup_link *link, |
| 436 | enum bpf_attach_type type, u32 flags) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 437 | { |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 438 | u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 439 | struct bpf_prog *old_prog = NULL; |
Andrii Nakryiko | 62039c3 | 2020-03-09 15:27:55 -0700 | [diff] [blame] | 440 | struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {}; |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 441 | struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {}; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 442 | enum cgroup_bpf_attach_type atype; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 443 | struct bpf_prog_list *pl; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 444 | struct list_head *progs; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 445 | int err; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 446 | |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 447 | if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) || |
| 448 | ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI))) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 449 | /* invalid combination */ |
| 450 | return -EINVAL; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 451 | if (link && (prog || replace_prog)) |
| 452 | /* only either link or prog/replace_prog can be specified */ |
| 453 | return -EINVAL; |
| 454 | if (!!replace_prog != !!(flags & BPF_F_REPLACE)) |
| 455 | /* replace_prog implies BPF_F_REPLACE, and vice versa */ |
| 456 | return -EINVAL; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 457 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 458 | atype = to_cgroup_bpf_attach_type(type); |
| 459 | if (atype < 0) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | progs = &cgrp->bpf.progs[atype]; |
| 463 | |
| 464 | if (!hierarchy_allows_attach(cgrp, atype)) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 465 | return -EPERM; |
| 466 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 467 | if (!list_empty(progs) && cgrp->bpf.flags[atype] != saved_flags) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 468 | /* Disallow attaching non-overridable on top |
| 469 | * of existing overridable in this cgroup. |
| 470 | * Disallow attaching multi-prog if overridable or none |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 471 | */ |
| 472 | return -EPERM; |
| 473 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 474 | if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS) |
| 475 | return -E2BIG; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 476 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 477 | pl = find_attach_entry(progs, prog, link, replace_prog, |
| 478 | flags & BPF_F_ALLOW_MULTI); |
| 479 | if (IS_ERR(pl)) |
| 480 | return PTR_ERR(pl); |
Andrey Ignatov | 1020c1f | 2019-12-18 23:44:33 -0800 | [diff] [blame] | 481 | |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 482 | if (bpf_cgroup_storages_alloc(storage, new_storage, type, |
| 483 | prog ? : link->link.prog, cgrp)) |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 484 | return -ENOMEM; |
Roman Gushchin | d7bf2c1 | 2018-08-02 14:27:20 -0700 | [diff] [blame] | 485 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 486 | if (pl) { |
Andrey Ignatov | 1020c1f | 2019-12-18 23:44:33 -0800 | [diff] [blame] | 487 | old_prog = pl->prog; |
Andrey Ignatov | 1020c1f | 2019-12-18 23:44:33 -0800 | [diff] [blame] | 488 | } else { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 489 | pl = kmalloc(sizeof(*pl), GFP_KERNEL); |
Roman Gushchin | d7bf2c1 | 2018-08-02 14:27:20 -0700 | [diff] [blame] | 490 | if (!pl) { |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 491 | bpf_cgroup_storages_free(new_storage); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 492 | return -ENOMEM; |
Roman Gushchin | d7bf2c1 | 2018-08-02 14:27:20 -0700 | [diff] [blame] | 493 | } |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 494 | list_add_tail(&pl->node, progs); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Andrey Ignatov | 1020c1f | 2019-12-18 23:44:33 -0800 | [diff] [blame] | 497 | pl->prog = prog; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 498 | pl->link = link; |
Andrii Nakryiko | 00c4edd | 2020-03-24 23:57:41 -0700 | [diff] [blame] | 499 | bpf_cgroup_storages_assign(pl->storage, storage); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 500 | cgrp->bpf.flags[atype] = saved_flags; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 501 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 502 | err = update_effective_progs(cgrp, atype); |
Roman Gushchin | 85fc4b1 | 2018-08-06 14:27:28 -0700 | [diff] [blame] | 503 | if (err) |
| 504 | goto cleanup; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 505 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 506 | if (old_prog) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 507 | bpf_prog_put(old_prog); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 508 | else |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 509 | static_branch_inc(&cgroup_bpf_enabled_key[atype]); |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 510 | bpf_cgroup_storages_link(new_storage, cgrp, type); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 511 | return 0; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 512 | |
| 513 | cleanup: |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 514 | if (old_prog) { |
| 515 | pl->prog = old_prog; |
| 516 | pl->link = NULL; |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 517 | } |
YiFei Zhu | 7d9c342 | 2020-07-23 23:47:43 -0500 | [diff] [blame] | 518 | bpf_cgroup_storages_free(new_storage); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 519 | if (!old_prog) { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 520 | list_del(&pl->node); |
| 521 | kfree(pl); |
| 522 | } |
| 523 | return err; |
| 524 | } |
| 525 | |
He Fengqing | 588e5d8 | 2021-10-29 02:39:06 +0000 | [diff] [blame] | 526 | static int cgroup_bpf_attach(struct cgroup *cgrp, |
| 527 | struct bpf_prog *prog, struct bpf_prog *replace_prog, |
| 528 | struct bpf_cgroup_link *link, |
| 529 | enum bpf_attach_type type, |
| 530 | u32 flags) |
| 531 | { |
| 532 | int ret; |
| 533 | |
| 534 | mutex_lock(&cgroup_mutex); |
| 535 | ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags); |
| 536 | mutex_unlock(&cgroup_mutex); |
| 537 | return ret; |
| 538 | } |
| 539 | |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 540 | /* Swap updated BPF program for given link in effective program arrays across |
| 541 | * all descendant cgroups. This function is guaranteed to succeed. |
| 542 | */ |
| 543 | static void replace_effective_prog(struct cgroup *cgrp, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 544 | enum cgroup_bpf_attach_type atype, |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 545 | struct bpf_cgroup_link *link) |
| 546 | { |
| 547 | struct bpf_prog_array_item *item; |
| 548 | struct cgroup_subsys_state *css; |
| 549 | struct bpf_prog_array *progs; |
| 550 | struct bpf_prog_list *pl; |
| 551 | struct list_head *head; |
| 552 | struct cgroup *cg; |
| 553 | int pos; |
| 554 | |
| 555 | css_for_each_descendant_pre(css, &cgrp->self) { |
| 556 | struct cgroup *desc = container_of(css, struct cgroup, self); |
| 557 | |
| 558 | if (percpu_ref_is_zero(&desc->bpf.refcnt)) |
| 559 | continue; |
| 560 | |
| 561 | /* find position of link in effective progs array */ |
| 562 | for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 563 | if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 564 | continue; |
| 565 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 566 | head = &cg->bpf.progs[atype]; |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 567 | list_for_each_entry(pl, head, node) { |
| 568 | if (!prog_list_prog(pl)) |
| 569 | continue; |
| 570 | if (pl->link == link) |
| 571 | goto found; |
| 572 | pos++; |
| 573 | } |
| 574 | } |
| 575 | found: |
| 576 | BUG_ON(!cg); |
| 577 | progs = rcu_dereference_protected( |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 578 | desc->bpf.effective[atype], |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 579 | lockdep_is_held(&cgroup_mutex)); |
| 580 | item = &progs->items[pos]; |
| 581 | WRITE_ONCE(item->prog, link->link.prog); |
| 582 | } |
| 583 | } |
| 584 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 585 | /** |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 586 | * __cgroup_bpf_replace() - Replace link's program and propagate the change |
| 587 | * to descendants |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 588 | * @cgrp: The cgroup which descendants to traverse |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 589 | * @link: A link for which to replace BPF program |
| 590 | * @type: Type of attach operation |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 591 | * |
| 592 | * Must be called with cgroup_mutex held. |
| 593 | */ |
Andrii Nakryiko | f9d0412 | 2020-04-28 17:16:05 -0700 | [diff] [blame] | 594 | static int __cgroup_bpf_replace(struct cgroup *cgrp, |
| 595 | struct bpf_cgroup_link *link, |
| 596 | struct bpf_prog *new_prog) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 597 | { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 598 | enum cgroup_bpf_attach_type atype; |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 599 | struct bpf_prog *old_prog; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 600 | struct bpf_prog_list *pl; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 601 | struct list_head *progs; |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 602 | bool found = false; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 603 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 604 | atype = to_cgroup_bpf_attach_type(link->type); |
| 605 | if (atype < 0) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | progs = &cgrp->bpf.progs[atype]; |
| 609 | |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 610 | if (link->link.prog->type != new_prog->type) |
| 611 | return -EINVAL; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 612 | |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 613 | list_for_each_entry(pl, progs, node) { |
| 614 | if (pl->link == link) { |
| 615 | found = true; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 616 | break; |
| 617 | } |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 618 | } |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 619 | if (!found) |
| 620 | return -ENOENT; |
| 621 | |
| 622 | old_prog = xchg(&link->link.prog, new_prog); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 623 | replace_effective_prog(cgrp, atype, link); |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 624 | bpf_prog_put(old_prog); |
| 625 | return 0; |
| 626 | } |
| 627 | |
Andrii Nakryiko | f9d0412 | 2020-04-28 17:16:05 -0700 | [diff] [blame] | 628 | static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog, |
| 629 | struct bpf_prog *old_prog) |
| 630 | { |
| 631 | struct bpf_cgroup_link *cg_link; |
| 632 | int ret; |
| 633 | |
| 634 | cg_link = container_of(link, struct bpf_cgroup_link, link); |
| 635 | |
| 636 | mutex_lock(&cgroup_mutex); |
| 637 | /* link might have been auto-released by dying cgroup, so fail */ |
| 638 | if (!cg_link->cgroup) { |
Jakub Sitnicki | 0c047ec | 2020-05-31 10:28:39 +0200 | [diff] [blame] | 639 | ret = -ENOLINK; |
Andrii Nakryiko | f9d0412 | 2020-04-28 17:16:05 -0700 | [diff] [blame] | 640 | goto out_unlock; |
| 641 | } |
| 642 | if (old_prog && link->prog != old_prog) { |
| 643 | ret = -EPERM; |
| 644 | goto out_unlock; |
| 645 | } |
| 646 | ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog); |
| 647 | out_unlock: |
| 648 | mutex_unlock(&cgroup_mutex); |
| 649 | return ret; |
| 650 | } |
| 651 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 652 | static struct bpf_prog_list *find_detach_entry(struct list_head *progs, |
| 653 | struct bpf_prog *prog, |
| 654 | struct bpf_cgroup_link *link, |
| 655 | bool allow_multi) |
| 656 | { |
| 657 | struct bpf_prog_list *pl; |
| 658 | |
| 659 | if (!allow_multi) { |
| 660 | if (list_empty(progs)) |
| 661 | /* report error when trying to detach and nothing is attached */ |
| 662 | return ERR_PTR(-ENOENT); |
| 663 | |
| 664 | /* to maintain backward compatibility NONE and OVERRIDE cgroups |
| 665 | * allow detaching with invalid FD (prog==NULL) in legacy mode |
| 666 | */ |
| 667 | return list_first_entry(progs, typeof(*pl), node); |
| 668 | } |
| 669 | |
| 670 | if (!prog && !link) |
| 671 | /* to detach MULTI prog the user has to specify valid FD |
| 672 | * of the program or link to be detached |
| 673 | */ |
| 674 | return ERR_PTR(-EINVAL); |
| 675 | |
| 676 | /* find the prog or link and detach it */ |
| 677 | list_for_each_entry(pl, progs, node) { |
| 678 | if (pl->prog == prog && pl->link == link) |
| 679 | return pl; |
| 680 | } |
| 681 | return ERR_PTR(-ENOENT); |
| 682 | } |
| 683 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 684 | /** |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 685 | * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 686 | * propagate the change to descendants |
| 687 | * @cgrp: The cgroup which descendants to traverse |
| 688 | * @prog: A program to detach or NULL |
He Fengqing | 588e5d8 | 2021-10-29 02:39:06 +0000 | [diff] [blame] | 689 | * @link: A link to detach or NULL |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 690 | * @type: Type of detach operation |
| 691 | * |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 692 | * At most one of @prog or @link can be non-NULL. |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 693 | * Must be called with cgroup_mutex held. |
| 694 | */ |
He Fengqing | 588e5d8 | 2021-10-29 02:39:06 +0000 | [diff] [blame] | 695 | static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 696 | struct bpf_cgroup_link *link, enum bpf_attach_type type) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 697 | { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 698 | enum cgroup_bpf_attach_type atype; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 699 | struct bpf_prog *old_prog; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 700 | struct bpf_prog_list *pl; |
| 701 | struct list_head *progs; |
| 702 | u32 flags; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 703 | int err; |
| 704 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 705 | atype = to_cgroup_bpf_attach_type(type); |
| 706 | if (atype < 0) |
| 707 | return -EINVAL; |
| 708 | |
| 709 | progs = &cgrp->bpf.progs[atype]; |
| 710 | flags = cgrp->bpf.flags[atype]; |
| 711 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 712 | if (prog && link) |
| 713 | /* only one of prog or link can be specified */ |
| 714 | return -EINVAL; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 715 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 716 | pl = find_detach_entry(progs, prog, link, flags & BPF_F_ALLOW_MULTI); |
| 717 | if (IS_ERR(pl)) |
| 718 | return PTR_ERR(pl); |
| 719 | |
| 720 | /* mark it deleted, so it's ignored while recomputing effective */ |
| 721 | old_prog = pl->prog; |
| 722 | pl->prog = NULL; |
| 723 | pl->link = NULL; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 724 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 725 | err = update_effective_progs(cgrp, atype); |
Roman Gushchin | 85fc4b1 | 2018-08-06 14:27:28 -0700 | [diff] [blame] | 726 | if (err) |
| 727 | goto cleanup; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 728 | |
| 729 | /* now can actually delete it from this cgroup list */ |
| 730 | list_del(&pl->node); |
| 731 | kfree(pl); |
| 732 | if (list_empty(progs)) |
| 733 | /* last program was detached, reset flags to zero */ |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 734 | cgrp->bpf.flags[atype] = 0; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 735 | if (old_prog) |
| 736 | bpf_prog_put(old_prog); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 737 | static_branch_dec(&cgroup_bpf_enabled_key[atype]); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 738 | return 0; |
| 739 | |
| 740 | cleanup: |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 741 | /* restore back prog or link */ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 742 | pl->prog = old_prog; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 743 | pl->link = link; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 744 | return err; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 745 | } |
| 746 | |
He Fengqing | 588e5d8 | 2021-10-29 02:39:06 +0000 | [diff] [blame] | 747 | static int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 748 | enum bpf_attach_type type) |
| 749 | { |
| 750 | int ret; |
| 751 | |
| 752 | mutex_lock(&cgroup_mutex); |
| 753 | ret = __cgroup_bpf_detach(cgrp, prog, NULL, type); |
| 754 | mutex_unlock(&cgroup_mutex); |
| 755 | return ret; |
| 756 | } |
| 757 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 758 | /* Must be called with cgroup_mutex held to avoid races. */ |
He Fengqing | 588e5d8 | 2021-10-29 02:39:06 +0000 | [diff] [blame] | 759 | static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 760 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 761 | { |
| 762 | __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids); |
| 763 | enum bpf_attach_type type = attr->query.attach_type; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 764 | enum cgroup_bpf_attach_type atype; |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 765 | struct bpf_prog_array *effective; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 766 | struct list_head *progs; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 767 | struct bpf_prog *prog; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 768 | int cnt, ret = 0, i; |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 769 | u32 flags; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 770 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 771 | atype = to_cgroup_bpf_attach_type(type); |
| 772 | if (atype < 0) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | progs = &cgrp->bpf.progs[atype]; |
| 776 | flags = cgrp->bpf.flags[atype]; |
| 777 | |
| 778 | effective = rcu_dereference_protected(cgrp->bpf.effective[atype], |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 779 | lockdep_is_held(&cgroup_mutex)); |
| 780 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 781 | if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 782 | cnt = bpf_prog_array_length(effective); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 783 | else |
| 784 | cnt = prog_list_length(progs); |
| 785 | |
| 786 | if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags))) |
| 787 | return -EFAULT; |
| 788 | if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt))) |
| 789 | return -EFAULT; |
| 790 | if (attr->query.prog_cnt == 0 || !prog_ids || !cnt) |
| 791 | /* return early if user requested only program count + flags */ |
| 792 | return 0; |
| 793 | if (attr->query.prog_cnt < cnt) { |
| 794 | cnt = attr->query.prog_cnt; |
| 795 | ret = -ENOSPC; |
| 796 | } |
| 797 | |
| 798 | if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) { |
Stanislav Fomichev | dbcc1ba | 2019-05-28 14:14:43 -0700 | [diff] [blame] | 799 | return bpf_prog_array_copy_to_user(effective, prog_ids, cnt); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 800 | } else { |
| 801 | struct bpf_prog_list *pl; |
| 802 | u32 id; |
| 803 | |
| 804 | i = 0; |
| 805 | list_for_each_entry(pl, progs, node) { |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 806 | prog = prog_list_prog(pl); |
| 807 | id = prog->aux->id; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 808 | if (copy_to_user(prog_ids + i, &id, sizeof(id))) |
| 809 | return -EFAULT; |
| 810 | if (++i == cnt) |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | return ret; |
| 815 | } |
| 816 | |
He Fengqing | 588e5d8 | 2021-10-29 02:39:06 +0000 | [diff] [blame] | 817 | static int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 818 | union bpf_attr __user *uattr) |
| 819 | { |
| 820 | int ret; |
| 821 | |
| 822 | mutex_lock(&cgroup_mutex); |
| 823 | ret = __cgroup_bpf_query(cgrp, attr, uattr); |
| 824 | mutex_unlock(&cgroup_mutex); |
| 825 | return ret; |
| 826 | } |
| 827 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 828 | int cgroup_bpf_prog_attach(const union bpf_attr *attr, |
| 829 | enum bpf_prog_type ptype, struct bpf_prog *prog) |
| 830 | { |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 831 | struct bpf_prog *replace_prog = NULL; |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 832 | struct cgroup *cgrp; |
| 833 | int ret; |
| 834 | |
| 835 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 836 | if (IS_ERR(cgrp)) |
| 837 | return PTR_ERR(cgrp); |
| 838 | |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 839 | if ((attr->attach_flags & BPF_F_ALLOW_MULTI) && |
| 840 | (attr->attach_flags & BPF_F_REPLACE)) { |
| 841 | replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype); |
| 842 | if (IS_ERR(replace_prog)) { |
| 843 | cgroup_put(cgrp); |
| 844 | return PTR_ERR(replace_prog); |
| 845 | } |
| 846 | } |
| 847 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 848 | ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL, |
| 849 | attr->attach_type, attr->attach_flags); |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 850 | |
| 851 | if (replace_prog) |
| 852 | bpf_prog_put(replace_prog); |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 853 | cgroup_put(cgrp); |
| 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype) |
| 858 | { |
| 859 | struct bpf_prog *prog; |
| 860 | struct cgroup *cgrp; |
| 861 | int ret; |
| 862 | |
| 863 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 864 | if (IS_ERR(cgrp)) |
| 865 | return PTR_ERR(cgrp); |
| 866 | |
| 867 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 868 | if (IS_ERR(prog)) |
| 869 | prog = NULL; |
| 870 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 871 | ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type); |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 872 | if (prog) |
| 873 | bpf_prog_put(prog); |
| 874 | |
| 875 | cgroup_put(cgrp); |
| 876 | return ret; |
| 877 | } |
| 878 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 879 | static void bpf_cgroup_link_release(struct bpf_link *link) |
| 880 | { |
| 881 | struct bpf_cgroup_link *cg_link = |
| 882 | container_of(link, struct bpf_cgroup_link, link); |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 883 | struct cgroup *cg; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 884 | |
| 885 | /* link might have been auto-detached by dying cgroup already, |
| 886 | * in that case our work is done here |
| 887 | */ |
| 888 | if (!cg_link->cgroup) |
| 889 | return; |
| 890 | |
| 891 | mutex_lock(&cgroup_mutex); |
| 892 | |
| 893 | /* re-check cgroup under lock again */ |
| 894 | if (!cg_link->cgroup) { |
| 895 | mutex_unlock(&cgroup_mutex); |
| 896 | return; |
| 897 | } |
| 898 | |
| 899 | WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link, |
| 900 | cg_link->type)); |
| 901 | |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 902 | cg = cg_link->cgroup; |
| 903 | cg_link->cgroup = NULL; |
| 904 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 905 | mutex_unlock(&cgroup_mutex); |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 906 | |
| 907 | cgroup_put(cg); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static void bpf_cgroup_link_dealloc(struct bpf_link *link) |
| 911 | { |
| 912 | struct bpf_cgroup_link *cg_link = |
| 913 | container_of(link, struct bpf_cgroup_link, link); |
| 914 | |
| 915 | kfree(cg_link); |
| 916 | } |
| 917 | |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 918 | static int bpf_cgroup_link_detach(struct bpf_link *link) |
| 919 | { |
| 920 | bpf_cgroup_link_release(link); |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 925 | static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link, |
| 926 | struct seq_file *seq) |
| 927 | { |
| 928 | struct bpf_cgroup_link *cg_link = |
| 929 | container_of(link, struct bpf_cgroup_link, link); |
| 930 | u64 cg_id = 0; |
| 931 | |
| 932 | mutex_lock(&cgroup_mutex); |
| 933 | if (cg_link->cgroup) |
| 934 | cg_id = cgroup_id(cg_link->cgroup); |
| 935 | mutex_unlock(&cgroup_mutex); |
| 936 | |
| 937 | seq_printf(seq, |
| 938 | "cgroup_id:\t%llu\n" |
| 939 | "attach_type:\t%d\n", |
| 940 | cg_id, |
| 941 | cg_link->type); |
| 942 | } |
| 943 | |
| 944 | static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link, |
| 945 | struct bpf_link_info *info) |
| 946 | { |
| 947 | struct bpf_cgroup_link *cg_link = |
| 948 | container_of(link, struct bpf_cgroup_link, link); |
| 949 | u64 cg_id = 0; |
| 950 | |
| 951 | mutex_lock(&cgroup_mutex); |
| 952 | if (cg_link->cgroup) |
| 953 | cg_id = cgroup_id(cg_link->cgroup); |
| 954 | mutex_unlock(&cgroup_mutex); |
| 955 | |
| 956 | info->cgroup.cgroup_id = cg_id; |
| 957 | info->cgroup.attach_type = cg_link->type; |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static const struct bpf_link_ops bpf_cgroup_link_lops = { |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 962 | .release = bpf_cgroup_link_release, |
| 963 | .dealloc = bpf_cgroup_link_dealloc, |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 964 | .detach = bpf_cgroup_link_detach, |
Andrii Nakryiko | f9d0412 | 2020-04-28 17:16:05 -0700 | [diff] [blame] | 965 | .update_prog = cgroup_bpf_replace, |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 966 | .show_fdinfo = bpf_cgroup_link_show_fdinfo, |
| 967 | .fill_link_info = bpf_cgroup_link_fill_link_info, |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 968 | }; |
| 969 | |
| 970 | int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) |
| 971 | { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 972 | struct bpf_link_primer link_primer; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 973 | struct bpf_cgroup_link *link; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 974 | struct cgroup *cgrp; |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 975 | int err; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 976 | |
| 977 | if (attr->link_create.flags) |
| 978 | return -EINVAL; |
| 979 | |
| 980 | cgrp = cgroup_get_from_fd(attr->link_create.target_fd); |
| 981 | if (IS_ERR(cgrp)) |
| 982 | return PTR_ERR(cgrp); |
| 983 | |
| 984 | link = kzalloc(sizeof(*link), GFP_USER); |
| 985 | if (!link) { |
| 986 | err = -ENOMEM; |
| 987 | goto out_put_cgroup; |
| 988 | } |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 989 | bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops, |
| 990 | prog); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 991 | link->cgroup = cgrp; |
| 992 | link->type = attr->link_create.attach_type; |
| 993 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 994 | err = bpf_link_prime(&link->link, &link_primer); |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 995 | if (err) { |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 996 | kfree(link); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 997 | goto out_put_cgroup; |
| 998 | } |
| 999 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1000 | err = cgroup_bpf_attach(cgrp, NULL, NULL, link, |
| 1001 | link->type, BPF_F_ALLOW_MULTI); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 1002 | if (err) { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 1003 | bpf_link_cleanup(&link_primer); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 1004 | goto out_put_cgroup; |
| 1005 | } |
| 1006 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 1007 | return bpf_link_settle(&link_primer); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 1008 | |
| 1009 | out_put_cgroup: |
| 1010 | cgroup_put(cgrp); |
| 1011 | return err; |
| 1012 | } |
| 1013 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1014 | int cgroup_bpf_prog_query(const union bpf_attr *attr, |
| 1015 | union bpf_attr __user *uattr) |
| 1016 | { |
| 1017 | struct cgroup *cgrp; |
| 1018 | int ret; |
| 1019 | |
| 1020 | cgrp = cgroup_get_from_fd(attr->query.target_fd); |
| 1021 | if (IS_ERR(cgrp)) |
| 1022 | return PTR_ERR(cgrp); |
| 1023 | |
| 1024 | ret = cgroup_bpf_query(cgrp, attr, uattr); |
| 1025 | |
| 1026 | cgroup_put(cgrp); |
| 1027 | return ret; |
| 1028 | } |
| 1029 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1030 | /** |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1031 | * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering |
Willem de Bruijn | 8f917bb | 2017-04-11 14:08:08 -0400 | [diff] [blame] | 1032 | * @sk: The socket sending or receiving traffic |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1033 | * @skb: The skb that is being sent or received |
| 1034 | * @type: The type of program to be exectuted |
| 1035 | * |
| 1036 | * If no socket is passed, or the socket is not of type INET or INET6, |
| 1037 | * this function does nothing and returns 0. |
| 1038 | * |
| 1039 | * The program type passed in via @type must be suitable for network |
| 1040 | * filtering. No further check is performed to assert that. |
| 1041 | * |
brakmo | e7a3160 | 2019-05-28 16:59:37 -0700 | [diff] [blame] | 1042 | * For egress packets, this function can return: |
| 1043 | * NET_XMIT_SUCCESS (0) - continue with packet output |
| 1044 | * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr |
| 1045 | * NET_XMIT_CN (2) - continue with packet output and notify TCP |
| 1046 | * to call cwr |
| 1047 | * -EPERM - drop packet |
| 1048 | * |
| 1049 | * For ingress packets, this function will return -EPERM if any |
| 1050 | * attached program was found and if it returned != 1 during execution. |
| 1051 | * Otherwise 0 is returned. |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1052 | */ |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1053 | int __cgroup_bpf_run_filter_skb(struct sock *sk, |
| 1054 | struct sk_buff *skb, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1055 | enum cgroup_bpf_attach_type atype) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1056 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1057 | unsigned int offset = skb->data - skb_network_header(skb); |
| 1058 | struct sock *save_sk; |
Song Liu | b39b5f4 | 2018-10-19 09:57:57 -0700 | [diff] [blame] | 1059 | void *saved_data_end; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1060 | struct cgroup *cgrp; |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1061 | int ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1062 | |
| 1063 | if (!sk || !sk_fullsock(sk)) |
| 1064 | return 0; |
| 1065 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1066 | if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1067 | return 0; |
| 1068 | |
| 1069 | cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1070 | save_sk = skb->sk; |
| 1071 | skb->sk = sk; |
| 1072 | __skb_push(skb, offset); |
Song Liu | b39b5f4 | 2018-10-19 09:57:57 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* compute pointers for the bpf prog */ |
| 1075 | bpf_compute_and_save_data_end(skb, &saved_data_end); |
| 1076 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1077 | if (atype == CGROUP_INET_EGRESS) { |
brakmo | e7a3160 | 2019-05-28 16:59:37 -0700 | [diff] [blame] | 1078 | ret = BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY( |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1079 | cgrp->bpf.effective[atype], skb, __bpf_prog_run_save_cb); |
brakmo | e7a3160 | 2019-05-28 16:59:37 -0700 | [diff] [blame] | 1080 | } else { |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1081 | ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], skb, |
Andrii Nakryiko | 7d08c2c | 2021-08-15 00:05:55 -0700 | [diff] [blame] | 1082 | __bpf_prog_run_save_cb); |
brakmo | e7a3160 | 2019-05-28 16:59:37 -0700 | [diff] [blame] | 1083 | ret = (ret == 1 ? 0 : -EPERM); |
| 1084 | } |
Song Liu | b39b5f4 | 2018-10-19 09:57:57 -0700 | [diff] [blame] | 1085 | bpf_restore_data_end(skb, saved_data_end); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1086 | __skb_pull(skb, offset); |
| 1087 | skb->sk = save_sk; |
brakmo | e7a3160 | 2019-05-28 16:59:37 -0700 | [diff] [blame] | 1088 | |
| 1089 | return ret; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 1090 | } |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1091 | EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb); |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1092 | |
| 1093 | /** |
| 1094 | * __cgroup_bpf_run_filter_sk() - Run a program on a sock |
| 1095 | * @sk: sock structure to manipulate |
| 1096 | * @type: The type of program to be exectuted |
| 1097 | * |
| 1098 | * socket is passed is expected to be of type INET or INET6. |
| 1099 | * |
| 1100 | * The program type passed in via @type must be suitable for sock |
| 1101 | * filtering. No further check is performed to assert that. |
| 1102 | * |
| 1103 | * This function will return %-EPERM if any if an attached program was found |
| 1104 | * and if it returned != 1 during execution. In all other cases, 0 is returned. |
| 1105 | */ |
| 1106 | int __cgroup_bpf_run_filter_sk(struct sock *sk, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1107 | enum cgroup_bpf_attach_type atype) |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1108 | { |
| 1109 | struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1110 | int ret; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1111 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1112 | ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], sk, bpf_prog_run); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1113 | return ret == 1 ? 0 : -EPERM; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1114 | } |
| 1115 | EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk); |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1116 | |
| 1117 | /** |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1118 | * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and |
| 1119 | * provided by user sockaddr |
| 1120 | * @sk: sock struct that will use sockaddr |
| 1121 | * @uaddr: sockaddr struct provided by user |
| 1122 | * @type: The type of program to be exectuted |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1123 | * @t_ctx: Pointer to attach type specific context |
Stanislav Fomichev | 7724121 | 2021-01-27 11:31:39 -0800 | [diff] [blame] | 1124 | * @flags: Pointer to u32 which contains higher bits of BPF program |
| 1125 | * return value (OR'ed together). |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1126 | * |
| 1127 | * socket is expected to be of type INET or INET6. |
| 1128 | * |
| 1129 | * This function will return %-EPERM if an attached program is found and |
| 1130 | * returned value != 1 during execution. In all other cases, 0 is returned. |
| 1131 | */ |
| 1132 | int __cgroup_bpf_run_filter_sock_addr(struct sock *sk, |
| 1133 | struct sockaddr *uaddr, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1134 | enum cgroup_bpf_attach_type atype, |
Stanislav Fomichev | 7724121 | 2021-01-27 11:31:39 -0800 | [diff] [blame] | 1135 | void *t_ctx, |
| 1136 | u32 *flags) |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1137 | { |
| 1138 | struct bpf_sock_addr_kern ctx = { |
| 1139 | .sk = sk, |
| 1140 | .uaddr = uaddr, |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1141 | .t_ctx = t_ctx, |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1142 | }; |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1143 | struct sockaddr_storage unspec; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1144 | struct cgroup *cgrp; |
| 1145 | int ret; |
| 1146 | |
| 1147 | /* Check socket family since not all sockets represent network |
| 1148 | * endpoint (e.g. AF_UNIX). |
| 1149 | */ |
| 1150 | if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) |
| 1151 | return 0; |
| 1152 | |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1153 | if (!ctx.uaddr) { |
| 1154 | memset(&unspec, 0, sizeof(unspec)); |
| 1155 | ctx.uaddr = (struct sockaddr *)&unspec; |
| 1156 | } |
| 1157 | |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1158 | cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1159 | ret = BPF_PROG_RUN_ARRAY_CG_FLAGS(cgrp->bpf.effective[atype], &ctx, |
Andrii Nakryiko | 7d08c2c | 2021-08-15 00:05:55 -0700 | [diff] [blame] | 1160 | bpf_prog_run, flags); |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1161 | |
| 1162 | return ret == 1 ? 0 : -EPERM; |
| 1163 | } |
| 1164 | EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr); |
| 1165 | |
| 1166 | /** |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1167 | * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock |
| 1168 | * @sk: socket to get cgroup from |
| 1169 | * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains |
| 1170 | * sk with connection information (IP addresses, etc.) May not contain |
| 1171 | * cgroup info if it is a req sock. |
| 1172 | * @type: The type of program to be exectuted |
| 1173 | * |
| 1174 | * socket passed is expected to be of type INET or INET6. |
| 1175 | * |
| 1176 | * The program type passed in via @type must be suitable for sock_ops |
| 1177 | * filtering. No further check is performed to assert that. |
| 1178 | * |
| 1179 | * This function will return %-EPERM if any if an attached program was found |
| 1180 | * and if it returned != 1 during execution. In all other cases, 0 is returned. |
| 1181 | */ |
| 1182 | int __cgroup_bpf_run_filter_sock_ops(struct sock *sk, |
| 1183 | struct bpf_sock_ops_kern *sock_ops, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1184 | enum cgroup_bpf_attach_type atype) |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1185 | { |
| 1186 | struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1187 | int ret; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1188 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1189 | ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], sock_ops, |
Andrii Nakryiko | 7d08c2c | 2021-08-15 00:05:55 -0700 | [diff] [blame] | 1190 | bpf_prog_run); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1191 | return ret == 1 ? 0 : -EPERM; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1192 | } |
| 1193 | EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops); |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1194 | |
| 1195 | int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1196 | short access, enum cgroup_bpf_attach_type atype) |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1197 | { |
| 1198 | struct cgroup *cgrp; |
| 1199 | struct bpf_cgroup_dev_ctx ctx = { |
| 1200 | .access_type = (access << 16) | dev_type, |
| 1201 | .major = major, |
| 1202 | .minor = minor, |
| 1203 | }; |
Colin Ian King | 8cacfc8 | 2021-08-17 18:08:42 +0100 | [diff] [blame] | 1204 | int allow; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1205 | |
| 1206 | rcu_read_lock(); |
| 1207 | cgrp = task_dfl_cgroup(current); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1208 | allow = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], &ctx, |
Andrii Nakryiko | 7d08c2c | 2021-08-15 00:05:55 -0700 | [diff] [blame] | 1209 | bpf_prog_run); |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1210 | rcu_read_unlock(); |
| 1211 | |
| 1212 | return !allow; |
| 1213 | } |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1214 | |
| 1215 | static const struct bpf_func_proto * |
Andrey Ignatov | b1cd609 | 2019-03-12 09:27:09 -0700 | [diff] [blame] | 1216 | cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1217 | { |
| 1218 | switch (func_id) { |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1219 | case BPF_FUNC_get_current_uid_gid: |
| 1220 | return &bpf_get_current_uid_gid_proto; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 1221 | case BPF_FUNC_get_local_storage: |
| 1222 | return &bpf_get_local_storage_proto; |
Yonghong Song | 5bf7a60 | 2018-09-27 14:37:30 -0700 | [diff] [blame] | 1223 | case BPF_FUNC_get_current_cgroup_id: |
| 1224 | return &bpf_get_current_cgroup_id_proto; |
Stanislav Fomichev | 0456ea1 | 2020-04-20 10:46:10 -0700 | [diff] [blame] | 1225 | case BPF_FUNC_perf_event_output: |
| 1226 | return &bpf_event_output_data_proto; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1227 | default: |
Stanislav Fomichev | 0456ea1 | 2020-04-20 10:46:10 -0700 | [diff] [blame] | 1228 | return bpf_base_func_proto(func_id); |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Andrey Ignatov | b1cd609 | 2019-03-12 09:27:09 -0700 | [diff] [blame] | 1232 | static const struct bpf_func_proto * |
| 1233 | cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1234 | { |
| 1235 | return cgroup_base_func_proto(func_id, prog); |
| 1236 | } |
| 1237 | |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1238 | static bool cgroup_dev_is_valid_access(int off, int size, |
| 1239 | enum bpf_access_type type, |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1240 | const struct bpf_prog *prog, |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1241 | struct bpf_insn_access_aux *info) |
| 1242 | { |
Yonghong Song | 06ef0cc | 2017-12-18 10:13:44 -0800 | [diff] [blame] | 1243 | const int size_default = sizeof(__u32); |
| 1244 | |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1245 | if (type == BPF_WRITE) |
| 1246 | return false; |
| 1247 | |
| 1248 | if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx)) |
| 1249 | return false; |
| 1250 | /* The verifier guarantees that size > 0. */ |
| 1251 | if (off % size != 0) |
| 1252 | return false; |
Yonghong Song | 06ef0cc | 2017-12-18 10:13:44 -0800 | [diff] [blame] | 1253 | |
| 1254 | switch (off) { |
| 1255 | case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type): |
| 1256 | bpf_ctx_record_field_size(info, size_default); |
| 1257 | if (!bpf_ctx_narrow_access_ok(off, size, size_default)) |
| 1258 | return false; |
| 1259 | break; |
| 1260 | default: |
| 1261 | if (size != size_default) |
| 1262 | return false; |
| 1263 | } |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1264 | |
| 1265 | return true; |
| 1266 | } |
| 1267 | |
| 1268 | const struct bpf_prog_ops cg_dev_prog_ops = { |
| 1269 | }; |
| 1270 | |
| 1271 | const struct bpf_verifier_ops cg_dev_verifier_ops = { |
| 1272 | .get_func_proto = cgroup_dev_func_proto, |
| 1273 | .is_valid_access = cgroup_dev_is_valid_access, |
| 1274 | }; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1275 | |
| 1276 | /** |
| 1277 | * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl |
| 1278 | * |
| 1279 | * @head: sysctl table header |
| 1280 | * @table: sysctl table |
| 1281 | * @write: sysctl is being read (= 0) or written (= 1) |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1282 | * @buf: pointer to buffer (in and out) |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1283 | * @pcount: value-result argument: value is size of buffer pointed to by @buf, |
| 1284 | * result is size of @new_buf if program set new value, initial value |
| 1285 | * otherwise |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1286 | * @ppos: value-result argument: value is position at which read from or write |
| 1287 | * to sysctl is happening, result is new position if program overrode it, |
| 1288 | * initial value otherwise |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1289 | * @type: type of program to be executed |
| 1290 | * |
| 1291 | * Program is run when sysctl is being accessed, either read or written, and |
| 1292 | * can allow or deny such access. |
| 1293 | * |
| 1294 | * This function will return %-EPERM if an attached program is found and |
| 1295 | * returned value != 1 during execution. In all other cases 0 is returned. |
| 1296 | */ |
| 1297 | int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head, |
| 1298 | struct ctl_table *table, int write, |
Matthew Wilcox (Oracle) | 4bd6a73 | 2020-09-03 16:22:32 +0200 | [diff] [blame] | 1299 | char **buf, size_t *pcount, loff_t *ppos, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1300 | enum cgroup_bpf_attach_type atype) |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1301 | { |
| 1302 | struct bpf_sysctl_kern ctx = { |
| 1303 | .head = head, |
| 1304 | .table = table, |
| 1305 | .write = write, |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1306 | .ppos = ppos, |
Andrey Ignatov | 1d11b30 | 2019-02-28 19:22:15 -0800 | [diff] [blame] | 1307 | .cur_val = NULL, |
| 1308 | .cur_len = PAGE_SIZE, |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1309 | .new_val = NULL, |
| 1310 | .new_len = 0, |
| 1311 | .new_updated = 0, |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1312 | }; |
| 1313 | struct cgroup *cgrp; |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1314 | loff_t pos = 0; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1315 | int ret; |
| 1316 | |
Andrey Ignatov | 1d11b30 | 2019-02-28 19:22:15 -0800 | [diff] [blame] | 1317 | ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL); |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1318 | if (!ctx.cur_val || |
| 1319 | table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) { |
Andrey Ignatov | 1d11b30 | 2019-02-28 19:22:15 -0800 | [diff] [blame] | 1320 | /* Let BPF program decide how to proceed. */ |
| 1321 | ctx.cur_len = 0; |
| 1322 | } |
| 1323 | |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1324 | if (write && *buf && *pcount) { |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1325 | /* BPF program should be able to override new value with a |
| 1326 | * buffer bigger than provided by user. |
| 1327 | */ |
| 1328 | ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL); |
Andrey Ignatov | 51356ac | 2019-04-12 16:01:01 -0700 | [diff] [blame] | 1329 | ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount); |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1330 | if (ctx.new_val) { |
| 1331 | memcpy(ctx.new_val, *buf, ctx.new_len); |
| 1332 | } else { |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1333 | /* Let BPF program decide how to proceed. */ |
| 1334 | ctx.new_len = 0; |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1335 | } |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1338 | rcu_read_lock(); |
| 1339 | cgrp = task_dfl_cgroup(current); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1340 | ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], &ctx, bpf_prog_run); |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1341 | rcu_read_unlock(); |
| 1342 | |
Andrey Ignatov | 1d11b30 | 2019-02-28 19:22:15 -0800 | [diff] [blame] | 1343 | kfree(ctx.cur_val); |
| 1344 | |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1345 | if (ret == 1 && ctx.new_updated) { |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 1346 | kfree(*buf); |
| 1347 | *buf = ctx.new_val; |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1348 | *pcount = ctx.new_len; |
| 1349 | } else { |
| 1350 | kfree(ctx.new_val); |
| 1351 | } |
| 1352 | |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1353 | return ret == 1 ? 0 : -EPERM; |
| 1354 | } |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1355 | |
YueHaibing | 6705fea | 2019-07-03 16:26:30 +0800 | [diff] [blame] | 1356 | #ifdef CONFIG_NET |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1357 | static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp, |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1358 | enum cgroup_bpf_attach_type attach_type) |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1359 | { |
| 1360 | struct bpf_prog_array *prog_array; |
| 1361 | bool empty; |
| 1362 | |
| 1363 | rcu_read_lock(); |
| 1364 | prog_array = rcu_dereference(cgrp->bpf.effective[attach_type]); |
| 1365 | empty = bpf_prog_array_is_empty(prog_array); |
| 1366 | rcu_read_unlock(); |
| 1367 | |
| 1368 | return empty; |
| 1369 | } |
| 1370 | |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1371 | static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen, |
| 1372 | struct bpf_sockopt_buf *buf) |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1373 | { |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1374 | if (unlikely(max_optlen < 0)) |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1375 | return -EINVAL; |
| 1376 | |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1377 | if (unlikely(max_optlen > PAGE_SIZE)) { |
| 1378 | /* We don't expose optvals that are greater than PAGE_SIZE |
| 1379 | * to the BPF program. |
| 1380 | */ |
| 1381 | max_optlen = PAGE_SIZE; |
| 1382 | } |
| 1383 | |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1384 | if (max_optlen <= sizeof(buf->data)) { |
| 1385 | /* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE |
| 1386 | * bytes avoid the cost of kzalloc. |
| 1387 | */ |
| 1388 | ctx->optval = buf->data; |
| 1389 | ctx->optval_end = ctx->optval + max_optlen; |
| 1390 | return max_optlen; |
| 1391 | } |
| 1392 | |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1393 | ctx->optval = kzalloc(max_optlen, GFP_USER); |
| 1394 | if (!ctx->optval) |
| 1395 | return -ENOMEM; |
| 1396 | |
| 1397 | ctx->optval_end = ctx->optval + max_optlen; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1398 | |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1399 | return max_optlen; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1402 | static void sockopt_free_buf(struct bpf_sockopt_kern *ctx, |
| 1403 | struct bpf_sockopt_buf *buf) |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1404 | { |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1405 | if (ctx->optval == buf->data) |
| 1406 | return; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1407 | kfree(ctx->optval); |
| 1408 | } |
| 1409 | |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1410 | static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx, |
| 1411 | struct bpf_sockopt_buf *buf) |
| 1412 | { |
| 1413 | return ctx->optval != buf->data; |
| 1414 | } |
| 1415 | |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1416 | int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, |
| 1417 | int *optname, char __user *optval, |
| 1418 | int *optlen, char **kernel_optval) |
| 1419 | { |
| 1420 | struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1421 | struct bpf_sockopt_buf buf = {}; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1422 | struct bpf_sockopt_kern ctx = { |
| 1423 | .sk = sk, |
| 1424 | .level = *level, |
| 1425 | .optname = *optname, |
| 1426 | }; |
Stanislav Fomichev | 9babe82 | 2019-07-29 14:51:10 -0700 | [diff] [blame] | 1427 | int ret, max_optlen; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1428 | |
| 1429 | /* Opportunistic check to see whether we have any BPF program |
| 1430 | * attached to the hook so we don't waste time allocating |
| 1431 | * memory and locking the socket. |
| 1432 | */ |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1433 | if (__cgroup_bpf_prog_array_is_empty(cgrp, CGROUP_SETSOCKOPT)) |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1434 | return 0; |
| 1435 | |
Stanislav Fomichev | 9babe82 | 2019-07-29 14:51:10 -0700 | [diff] [blame] | 1436 | /* Allocate a bit more than the initial user buffer for |
| 1437 | * BPF program. The canonical use case is overriding |
| 1438 | * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic). |
| 1439 | */ |
| 1440 | max_optlen = max_t(int, 16, *optlen); |
| 1441 | |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1442 | max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf); |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1443 | if (max_optlen < 0) |
| 1444 | return max_optlen; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1445 | |
Stanislav Fomichev | 9babe82 | 2019-07-29 14:51:10 -0700 | [diff] [blame] | 1446 | ctx.optlen = *optlen; |
| 1447 | |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1448 | if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) { |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1449 | ret = -EFAULT; |
| 1450 | goto out; |
| 1451 | } |
| 1452 | |
| 1453 | lock_sock(sk); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1454 | ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[CGROUP_SETSOCKOPT], |
Andrii Nakryiko | 7d08c2c | 2021-08-15 00:05:55 -0700 | [diff] [blame] | 1455 | &ctx, bpf_prog_run); |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1456 | release_sock(sk); |
| 1457 | |
| 1458 | if (!ret) { |
| 1459 | ret = -EPERM; |
| 1460 | goto out; |
| 1461 | } |
| 1462 | |
| 1463 | if (ctx.optlen == -1) { |
| 1464 | /* optlen set to -1, bypass kernel */ |
| 1465 | ret = 1; |
Stanislav Fomichev | 9babe82 | 2019-07-29 14:51:10 -0700 | [diff] [blame] | 1466 | } else if (ctx.optlen > max_optlen || ctx.optlen < -1) { |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1467 | /* optlen is out of bounds */ |
| 1468 | ret = -EFAULT; |
| 1469 | } else { |
| 1470 | /* optlen within bounds, run kernel handler */ |
| 1471 | ret = 0; |
| 1472 | |
| 1473 | /* export any potential modifications */ |
| 1474 | *level = ctx.level; |
| 1475 | *optname = ctx.optname; |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1476 | |
| 1477 | /* optlen == 0 from BPF indicates that we should |
| 1478 | * use original userspace data. |
| 1479 | */ |
| 1480 | if (ctx.optlen != 0) { |
| 1481 | *optlen = ctx.optlen; |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1482 | /* We've used bpf_sockopt_kern->buf as an intermediary |
| 1483 | * storage, but the BPF program indicates that we need |
| 1484 | * to pass this data to the kernel setsockopt handler. |
| 1485 | * No way to export on-stack buf, have to allocate a |
| 1486 | * new buffer. |
| 1487 | */ |
| 1488 | if (!sockopt_buf_allocated(&ctx, &buf)) { |
| 1489 | void *p = kmalloc(ctx.optlen, GFP_USER); |
| 1490 | |
| 1491 | if (!p) { |
| 1492 | ret = -ENOMEM; |
| 1493 | goto out; |
| 1494 | } |
| 1495 | memcpy(p, ctx.optval, ctx.optlen); |
| 1496 | *kernel_optval = p; |
| 1497 | } else { |
| 1498 | *kernel_optval = ctx.optval; |
| 1499 | } |
Stanislav Fomichev | 4be34f3 | 2021-01-12 08:28:29 -0800 | [diff] [blame] | 1500 | /* export and don't free sockopt buf */ |
| 1501 | return 0; |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1502 | } |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | out: |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1506 | sockopt_free_buf(&ctx, &buf); |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1507 | return ret; |
| 1508 | } |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1509 | |
| 1510 | int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, |
| 1511 | int optname, char __user *optval, |
| 1512 | int __user *optlen, int max_optlen, |
| 1513 | int retval) |
| 1514 | { |
| 1515 | struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1516 | struct bpf_sockopt_buf buf = {}; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1517 | struct bpf_sockopt_kern ctx = { |
| 1518 | .sk = sk, |
| 1519 | .level = level, |
| 1520 | .optname = optname, |
| 1521 | .retval = retval, |
| 1522 | }; |
| 1523 | int ret; |
| 1524 | |
| 1525 | /* Opportunistic check to see whether we have any BPF program |
| 1526 | * attached to the hook so we don't waste time allocating |
| 1527 | * memory and locking the socket. |
| 1528 | */ |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1529 | if (__cgroup_bpf_prog_array_is_empty(cgrp, CGROUP_GETSOCKOPT)) |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1530 | return retval; |
| 1531 | |
Stanislav Fomichev | 9babe82 | 2019-07-29 14:51:10 -0700 | [diff] [blame] | 1532 | ctx.optlen = max_optlen; |
| 1533 | |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1534 | max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf); |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1535 | if (max_optlen < 0) |
| 1536 | return max_optlen; |
| 1537 | |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1538 | if (!retval) { |
| 1539 | /* If kernel getsockopt finished successfully, |
| 1540 | * copy whatever was returned to the user back |
| 1541 | * into our temporary buffer. Set optlen to the |
| 1542 | * one that kernel returned as well to let |
| 1543 | * BPF programs inspect the value. |
| 1544 | */ |
| 1545 | |
| 1546 | if (get_user(ctx.optlen, optlen)) { |
| 1547 | ret = -EFAULT; |
| 1548 | goto out; |
| 1549 | } |
| 1550 | |
Loris Reiff | bb8b81e | 2021-01-22 17:42:31 +0100 | [diff] [blame] | 1551 | if (ctx.optlen < 0) { |
| 1552 | ret = -EFAULT; |
| 1553 | goto out; |
| 1554 | } |
| 1555 | |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1556 | if (copy_from_user(ctx.optval, optval, |
| 1557 | min(ctx.optlen, max_optlen)) != 0) { |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1558 | ret = -EFAULT; |
| 1559 | goto out; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | lock_sock(sk); |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1564 | ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[CGROUP_GETSOCKOPT], |
Andrii Nakryiko | 7d08c2c | 2021-08-15 00:05:55 -0700 | [diff] [blame] | 1565 | &ctx, bpf_prog_run); |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1566 | release_sock(sk); |
| 1567 | |
| 1568 | if (!ret) { |
| 1569 | ret = -EPERM; |
| 1570 | goto out; |
| 1571 | } |
| 1572 | |
Loris Reiff | f4a2da7 | 2021-01-22 17:42:32 +0100 | [diff] [blame] | 1573 | if (ctx.optlen > max_optlen || ctx.optlen < 0) { |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1574 | ret = -EFAULT; |
| 1575 | goto out; |
| 1576 | } |
| 1577 | |
| 1578 | /* BPF programs only allowed to set retval to 0, not some |
| 1579 | * arbitrary value. |
| 1580 | */ |
| 1581 | if (ctx.retval != 0 && ctx.retval != retval) { |
| 1582 | ret = -EFAULT; |
| 1583 | goto out; |
| 1584 | } |
| 1585 | |
Stanislav Fomichev | d8fe449 | 2020-06-16 18:04:14 -0700 | [diff] [blame] | 1586 | if (ctx.optlen != 0) { |
| 1587 | if (copy_to_user(optval, ctx.optval, ctx.optlen) || |
| 1588 | put_user(ctx.optlen, optlen)) { |
| 1589 | ret = -EFAULT; |
| 1590 | goto out; |
| 1591 | } |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | ret = ctx.retval; |
| 1595 | |
| 1596 | out: |
Stanislav Fomichev | 20f2505 | 2021-01-15 08:35:00 -0800 | [diff] [blame] | 1597 | sockopt_free_buf(&ctx, &buf); |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1598 | return ret; |
| 1599 | } |
Stanislav Fomichev | 9cacf81 | 2021-01-15 08:34:59 -0800 | [diff] [blame] | 1600 | |
| 1601 | int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level, |
| 1602 | int optname, void *optval, |
| 1603 | int *optlen, int retval) |
| 1604 | { |
| 1605 | struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
| 1606 | struct bpf_sockopt_kern ctx = { |
| 1607 | .sk = sk, |
| 1608 | .level = level, |
| 1609 | .optname = optname, |
| 1610 | .retval = retval, |
| 1611 | .optlen = *optlen, |
| 1612 | .optval = optval, |
| 1613 | .optval_end = optval + *optlen, |
| 1614 | }; |
| 1615 | int ret; |
| 1616 | |
| 1617 | /* Note that __cgroup_bpf_run_filter_getsockopt doesn't copy |
| 1618 | * user data back into BPF buffer when reval != 0. This is |
| 1619 | * done as an optimization to avoid extra copy, assuming |
| 1620 | * kernel won't populate the data in case of an error. |
| 1621 | * Here we always pass the data and memset() should |
| 1622 | * be called if that data shouldn't be "exported". |
| 1623 | */ |
| 1624 | |
Dave Marchevsky | 6fc88c3 | 2021-08-19 02:24:20 -0700 | [diff] [blame] | 1625 | ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[CGROUP_GETSOCKOPT], |
Andrii Nakryiko | 7d08c2c | 2021-08-15 00:05:55 -0700 | [diff] [blame] | 1626 | &ctx, bpf_prog_run); |
Stanislav Fomichev | 9cacf81 | 2021-01-15 08:34:59 -0800 | [diff] [blame] | 1627 | if (!ret) |
| 1628 | return -EPERM; |
| 1629 | |
| 1630 | if (ctx.optlen > *optlen) |
| 1631 | return -EFAULT; |
| 1632 | |
| 1633 | /* BPF programs only allowed to set retval to 0, not some |
| 1634 | * arbitrary value. |
| 1635 | */ |
| 1636 | if (ctx.retval != 0 && ctx.retval != retval) |
| 1637 | return -EFAULT; |
| 1638 | |
| 1639 | /* BPF programs can shrink the buffer, export the modifications. |
| 1640 | */ |
| 1641 | if (ctx.optlen != 0) |
| 1642 | *optlen = ctx.optlen; |
| 1643 | |
| 1644 | return ctx.retval; |
| 1645 | } |
YueHaibing | 6705fea | 2019-07-03 16:26:30 +0800 | [diff] [blame] | 1646 | #endif |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1647 | |
Andrey Ignatov | 808649f | 2019-02-27 13:28:48 -0800 | [diff] [blame] | 1648 | static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp, |
| 1649 | size_t *lenp) |
| 1650 | { |
| 1651 | ssize_t tmp_ret = 0, ret; |
| 1652 | |
| 1653 | if (dir->header.parent) { |
| 1654 | tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp); |
| 1655 | if (tmp_ret < 0) |
| 1656 | return tmp_ret; |
| 1657 | } |
| 1658 | |
| 1659 | ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp); |
| 1660 | if (ret < 0) |
| 1661 | return ret; |
| 1662 | *bufp += ret; |
| 1663 | *lenp -= ret; |
| 1664 | ret += tmp_ret; |
| 1665 | |
| 1666 | /* Avoid leading slash. */ |
| 1667 | if (!ret) |
| 1668 | return ret; |
| 1669 | |
| 1670 | tmp_ret = strscpy(*bufp, "/", *lenp); |
| 1671 | if (tmp_ret < 0) |
| 1672 | return tmp_ret; |
| 1673 | *bufp += tmp_ret; |
| 1674 | *lenp -= tmp_ret; |
| 1675 | |
| 1676 | return ret + tmp_ret; |
| 1677 | } |
| 1678 | |
| 1679 | BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf, |
| 1680 | size_t, buf_len, u64, flags) |
| 1681 | { |
| 1682 | ssize_t tmp_ret = 0, ret; |
| 1683 | |
| 1684 | if (!buf) |
| 1685 | return -EINVAL; |
| 1686 | |
| 1687 | if (!(flags & BPF_F_SYSCTL_BASE_NAME)) { |
| 1688 | if (!ctx->head) |
| 1689 | return -EINVAL; |
| 1690 | tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len); |
| 1691 | if (tmp_ret < 0) |
| 1692 | return tmp_ret; |
| 1693 | } |
| 1694 | |
| 1695 | ret = strscpy(buf, ctx->table->procname, buf_len); |
| 1696 | |
| 1697 | return ret < 0 ? ret : tmp_ret + ret; |
| 1698 | } |
| 1699 | |
| 1700 | static const struct bpf_func_proto bpf_sysctl_get_name_proto = { |
| 1701 | .func = bpf_sysctl_get_name, |
| 1702 | .gpl_only = false, |
| 1703 | .ret_type = RET_INTEGER, |
| 1704 | .arg1_type = ARG_PTR_TO_CTX, |
| 1705 | .arg2_type = ARG_PTR_TO_MEM, |
| 1706 | .arg3_type = ARG_CONST_SIZE, |
| 1707 | .arg4_type = ARG_ANYTHING, |
| 1708 | }; |
| 1709 | |
Andrey Ignatov | 1d11b30 | 2019-02-28 19:22:15 -0800 | [diff] [blame] | 1710 | static int copy_sysctl_value(char *dst, size_t dst_len, char *src, |
| 1711 | size_t src_len) |
| 1712 | { |
| 1713 | if (!dst) |
| 1714 | return -EINVAL; |
| 1715 | |
| 1716 | if (!dst_len) |
| 1717 | return -E2BIG; |
| 1718 | |
| 1719 | if (!src || !src_len) { |
| 1720 | memset(dst, 0, dst_len); |
| 1721 | return -EINVAL; |
| 1722 | } |
| 1723 | |
| 1724 | memcpy(dst, src, min(dst_len, src_len)); |
| 1725 | |
| 1726 | if (dst_len > src_len) { |
| 1727 | memset(dst + src_len, '\0', dst_len - src_len); |
| 1728 | return src_len; |
| 1729 | } |
| 1730 | |
| 1731 | dst[dst_len - 1] = '\0'; |
| 1732 | |
| 1733 | return -E2BIG; |
| 1734 | } |
| 1735 | |
| 1736 | BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx, |
| 1737 | char *, buf, size_t, buf_len) |
| 1738 | { |
| 1739 | return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len); |
| 1740 | } |
| 1741 | |
| 1742 | static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = { |
| 1743 | .func = bpf_sysctl_get_current_value, |
| 1744 | .gpl_only = false, |
| 1745 | .ret_type = RET_INTEGER, |
| 1746 | .arg1_type = ARG_PTR_TO_CTX, |
| 1747 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 1748 | .arg3_type = ARG_CONST_SIZE, |
| 1749 | }; |
| 1750 | |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1751 | BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf, |
| 1752 | size_t, buf_len) |
| 1753 | { |
| 1754 | if (!ctx->write) { |
| 1755 | if (buf && buf_len) |
| 1756 | memset(buf, '\0', buf_len); |
| 1757 | return -EINVAL; |
| 1758 | } |
| 1759 | return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len); |
| 1760 | } |
| 1761 | |
| 1762 | static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = { |
| 1763 | .func = bpf_sysctl_get_new_value, |
| 1764 | .gpl_only = false, |
| 1765 | .ret_type = RET_INTEGER, |
| 1766 | .arg1_type = ARG_PTR_TO_CTX, |
| 1767 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 1768 | .arg3_type = ARG_CONST_SIZE, |
| 1769 | }; |
| 1770 | |
| 1771 | BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx, |
| 1772 | const char *, buf, size_t, buf_len) |
| 1773 | { |
| 1774 | if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len) |
| 1775 | return -EINVAL; |
| 1776 | |
| 1777 | if (buf_len > PAGE_SIZE - 1) |
| 1778 | return -E2BIG; |
| 1779 | |
| 1780 | memcpy(ctx->new_val, buf, buf_len); |
| 1781 | ctx->new_len = buf_len; |
| 1782 | ctx->new_updated = 1; |
| 1783 | |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
| 1787 | static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = { |
| 1788 | .func = bpf_sysctl_set_new_value, |
| 1789 | .gpl_only = false, |
| 1790 | .ret_type = RET_INTEGER, |
| 1791 | .arg1_type = ARG_PTR_TO_CTX, |
Hao Luo | 216e3cd | 2021-12-16 16:31:51 -0800 | [diff] [blame] | 1792 | .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1793 | .arg3_type = ARG_CONST_SIZE, |
| 1794 | }; |
| 1795 | |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1796 | static const struct bpf_func_proto * |
| 1797 | sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1798 | { |
Andrey Ignatov | 808649f | 2019-02-27 13:28:48 -0800 | [diff] [blame] | 1799 | switch (func_id) { |
Andrey Ignatov | d7a4cb9 | 2019-03-18 17:55:26 -0700 | [diff] [blame] | 1800 | case BPF_FUNC_strtol: |
| 1801 | return &bpf_strtol_proto; |
| 1802 | case BPF_FUNC_strtoul: |
| 1803 | return &bpf_strtoul_proto; |
Andrey Ignatov | 808649f | 2019-02-27 13:28:48 -0800 | [diff] [blame] | 1804 | case BPF_FUNC_sysctl_get_name: |
| 1805 | return &bpf_sysctl_get_name_proto; |
Andrey Ignatov | 1d11b30 | 2019-02-28 19:22:15 -0800 | [diff] [blame] | 1806 | case BPF_FUNC_sysctl_get_current_value: |
| 1807 | return &bpf_sysctl_get_current_value_proto; |
Andrey Ignatov | 4e63acd | 2019-03-07 18:38:43 -0800 | [diff] [blame] | 1808 | case BPF_FUNC_sysctl_get_new_value: |
| 1809 | return &bpf_sysctl_get_new_value_proto; |
| 1810 | case BPF_FUNC_sysctl_set_new_value: |
| 1811 | return &bpf_sysctl_set_new_value_proto; |
Dmitrii Banshchikov | 5e0bc30 | 2021-11-13 18:22:26 +0400 | [diff] [blame] | 1812 | case BPF_FUNC_ktime_get_coarse_ns: |
| 1813 | return &bpf_ktime_get_coarse_ns_proto; |
Andrey Ignatov | 808649f | 2019-02-27 13:28:48 -0800 | [diff] [blame] | 1814 | default: |
| 1815 | return cgroup_base_func_proto(func_id, prog); |
| 1816 | } |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type, |
| 1820 | const struct bpf_prog *prog, |
| 1821 | struct bpf_insn_access_aux *info) |
| 1822 | { |
| 1823 | const int size_default = sizeof(__u32); |
| 1824 | |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1825 | if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size) |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1826 | return false; |
| 1827 | |
| 1828 | switch (off) { |
Ilya Leoshkevich | 7541c87 | 2019-10-28 13:29:02 +0100 | [diff] [blame] | 1829 | case bpf_ctx_range(struct bpf_sysctl, write): |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1830 | if (type != BPF_READ) |
| 1831 | return false; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1832 | bpf_ctx_record_field_size(info, size_default); |
| 1833 | return bpf_ctx_narrow_access_ok(off, size, size_default); |
Ilya Leoshkevich | 7541c87 | 2019-10-28 13:29:02 +0100 | [diff] [blame] | 1834 | case bpf_ctx_range(struct bpf_sysctl, file_pos): |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1835 | if (type == BPF_READ) { |
| 1836 | bpf_ctx_record_field_size(info, size_default); |
| 1837 | return bpf_ctx_narrow_access_ok(off, size, size_default); |
| 1838 | } else { |
| 1839 | return size == size_default; |
| 1840 | } |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1841 | default: |
| 1842 | return false; |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | static u32 sysctl_convert_ctx_access(enum bpf_access_type type, |
| 1847 | const struct bpf_insn *si, |
| 1848 | struct bpf_insn *insn_buf, |
| 1849 | struct bpf_prog *prog, u32 *target_size) |
| 1850 | { |
| 1851 | struct bpf_insn *insn = insn_buf; |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 1852 | u32 read_size; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1853 | |
| 1854 | switch (si->off) { |
| 1855 | case offsetof(struct bpf_sysctl, write): |
| 1856 | *insn++ = BPF_LDX_MEM( |
| 1857 | BPF_SIZE(si->code), si->dst_reg, si->src_reg, |
| 1858 | bpf_target_off(struct bpf_sysctl_kern, write, |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 1859 | sizeof_field(struct bpf_sysctl_kern, |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1860 | write), |
| 1861 | target_size)); |
| 1862 | break; |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1863 | case offsetof(struct bpf_sysctl, file_pos): |
| 1864 | /* ppos is a pointer so it should be accessed via indirect |
| 1865 | * loads and stores. Also for stores additional temporary |
| 1866 | * register is used since neither src_reg nor dst_reg can be |
| 1867 | * overridden. |
| 1868 | */ |
| 1869 | if (type == BPF_WRITE) { |
| 1870 | int treg = BPF_REG_9; |
| 1871 | |
| 1872 | if (si->src_reg == treg || si->dst_reg == treg) |
| 1873 | --treg; |
| 1874 | if (si->src_reg == treg || si->dst_reg == treg) |
| 1875 | --treg; |
| 1876 | *insn++ = BPF_STX_MEM( |
| 1877 | BPF_DW, si->dst_reg, treg, |
| 1878 | offsetof(struct bpf_sysctl_kern, tmp_reg)); |
| 1879 | *insn++ = BPF_LDX_MEM( |
| 1880 | BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos), |
| 1881 | treg, si->dst_reg, |
| 1882 | offsetof(struct bpf_sysctl_kern, ppos)); |
| 1883 | *insn++ = BPF_STX_MEM( |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 1884 | BPF_SIZEOF(u32), treg, si->src_reg, |
| 1885 | bpf_ctx_narrow_access_offset( |
| 1886 | 0, sizeof(u32), sizeof(loff_t))); |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1887 | *insn++ = BPF_LDX_MEM( |
| 1888 | BPF_DW, treg, si->dst_reg, |
| 1889 | offsetof(struct bpf_sysctl_kern, tmp_reg)); |
| 1890 | } else { |
| 1891 | *insn++ = BPF_LDX_MEM( |
| 1892 | BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos), |
| 1893 | si->dst_reg, si->src_reg, |
| 1894 | offsetof(struct bpf_sysctl_kern, ppos)); |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 1895 | read_size = bpf_size_to_bytes(BPF_SIZE(si->code)); |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1896 | *insn++ = BPF_LDX_MEM( |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 1897 | BPF_SIZE(si->code), si->dst_reg, si->dst_reg, |
| 1898 | bpf_ctx_narrow_access_offset( |
| 1899 | 0, read_size, sizeof(loff_t))); |
Andrey Ignatov | e1550bf | 2019-03-07 18:50:52 -0800 | [diff] [blame] | 1900 | } |
| 1901 | *target_size = sizeof(u32); |
| 1902 | break; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | return insn - insn_buf; |
| 1906 | } |
| 1907 | |
| 1908 | const struct bpf_verifier_ops cg_sysctl_verifier_ops = { |
| 1909 | .get_func_proto = sysctl_func_proto, |
| 1910 | .is_valid_access = sysctl_is_valid_access, |
| 1911 | .convert_ctx_access = sysctl_convert_ctx_access, |
| 1912 | }; |
| 1913 | |
| 1914 | const struct bpf_prog_ops cg_sysctl_prog_ops = { |
| 1915 | }; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1916 | |
Stanislav Fomichev | f1248de | 2021-08-13 16:05:29 -0700 | [diff] [blame] | 1917 | #ifdef CONFIG_NET |
| 1918 | BPF_CALL_1(bpf_get_netns_cookie_sockopt, struct bpf_sockopt_kern *, ctx) |
| 1919 | { |
| 1920 | const struct net *net = ctx ? sock_net(ctx->sk) : &init_net; |
| 1921 | |
| 1922 | return net->net_cookie; |
| 1923 | } |
| 1924 | |
| 1925 | static const struct bpf_func_proto bpf_get_netns_cookie_sockopt_proto = { |
| 1926 | .func = bpf_get_netns_cookie_sockopt, |
| 1927 | .gpl_only = false, |
| 1928 | .ret_type = RET_INTEGER, |
| 1929 | .arg1_type = ARG_PTR_TO_CTX_OR_NULL, |
| 1930 | }; |
| 1931 | #endif |
| 1932 | |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1933 | static const struct bpf_func_proto * |
| 1934 | cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1935 | { |
| 1936 | switch (func_id) { |
YueHaibing | 6705fea | 2019-07-03 16:26:30 +0800 | [diff] [blame] | 1937 | #ifdef CONFIG_NET |
Stanislav Fomichev | f1248de | 2021-08-13 16:05:29 -0700 | [diff] [blame] | 1938 | case BPF_FUNC_get_netns_cookie: |
| 1939 | return &bpf_get_netns_cookie_sockopt_proto; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1940 | case BPF_FUNC_sk_storage_get: |
| 1941 | return &bpf_sk_storage_get_proto; |
| 1942 | case BPF_FUNC_sk_storage_delete: |
| 1943 | return &bpf_sk_storage_delete_proto; |
Prankur Gupta | 2c53163 | 2021-08-17 15:42:20 -0700 | [diff] [blame] | 1944 | case BPF_FUNC_setsockopt: |
| 1945 | if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT) |
| 1946 | return &bpf_sk_setsockopt_proto; |
| 1947 | return NULL; |
| 1948 | case BPF_FUNC_getsockopt: |
| 1949 | if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT) |
| 1950 | return &bpf_sk_getsockopt_proto; |
| 1951 | return NULL; |
YueHaibing | 6705fea | 2019-07-03 16:26:30 +0800 | [diff] [blame] | 1952 | #endif |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1953 | #ifdef CONFIG_INET |
| 1954 | case BPF_FUNC_tcp_sock: |
| 1955 | return &bpf_tcp_sock_proto; |
| 1956 | #endif |
| 1957 | default: |
| 1958 | return cgroup_base_func_proto(func_id, prog); |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | static bool cg_sockopt_is_valid_access(int off, int size, |
| 1963 | enum bpf_access_type type, |
| 1964 | const struct bpf_prog *prog, |
| 1965 | struct bpf_insn_access_aux *info) |
| 1966 | { |
| 1967 | const int size_default = sizeof(__u32); |
| 1968 | |
| 1969 | if (off < 0 || off >= sizeof(struct bpf_sockopt)) |
| 1970 | return false; |
| 1971 | |
| 1972 | if (off % size != 0) |
| 1973 | return false; |
| 1974 | |
| 1975 | if (type == BPF_WRITE) { |
| 1976 | switch (off) { |
| 1977 | case offsetof(struct bpf_sockopt, retval): |
| 1978 | if (size != size_default) |
| 1979 | return false; |
| 1980 | return prog->expected_attach_type == |
| 1981 | BPF_CGROUP_GETSOCKOPT; |
| 1982 | case offsetof(struct bpf_sockopt, optname): |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1983 | fallthrough; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1984 | case offsetof(struct bpf_sockopt, level): |
| 1985 | if (size != size_default) |
| 1986 | return false; |
| 1987 | return prog->expected_attach_type == |
| 1988 | BPF_CGROUP_SETSOCKOPT; |
| 1989 | case offsetof(struct bpf_sockopt, optlen): |
| 1990 | return size == size_default; |
| 1991 | default: |
| 1992 | return false; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | switch (off) { |
| 1997 | case offsetof(struct bpf_sockopt, sk): |
| 1998 | if (size != sizeof(__u64)) |
| 1999 | return false; |
| 2000 | info->reg_type = PTR_TO_SOCKET; |
| 2001 | break; |
| 2002 | case offsetof(struct bpf_sockopt, optval): |
| 2003 | if (size != sizeof(__u64)) |
| 2004 | return false; |
| 2005 | info->reg_type = PTR_TO_PACKET; |
| 2006 | break; |
| 2007 | case offsetof(struct bpf_sockopt, optval_end): |
| 2008 | if (size != sizeof(__u64)) |
| 2009 | return false; |
| 2010 | info->reg_type = PTR_TO_PACKET_END; |
| 2011 | break; |
| 2012 | case offsetof(struct bpf_sockopt, retval): |
| 2013 | if (size != size_default) |
| 2014 | return false; |
| 2015 | return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT; |
| 2016 | default: |
| 2017 | if (size != size_default) |
| 2018 | return false; |
| 2019 | break; |
| 2020 | } |
| 2021 | return true; |
| 2022 | } |
| 2023 | |
| 2024 | #define CG_SOCKOPT_ACCESS_FIELD(T, F) \ |
| 2025 | T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \ |
| 2026 | si->dst_reg, si->src_reg, \ |
| 2027 | offsetof(struct bpf_sockopt_kern, F)) |
| 2028 | |
| 2029 | static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type, |
| 2030 | const struct bpf_insn *si, |
| 2031 | struct bpf_insn *insn_buf, |
| 2032 | struct bpf_prog *prog, |
| 2033 | u32 *target_size) |
| 2034 | { |
| 2035 | struct bpf_insn *insn = insn_buf; |
| 2036 | |
| 2037 | switch (si->off) { |
| 2038 | case offsetof(struct bpf_sockopt, sk): |
| 2039 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk); |
| 2040 | break; |
| 2041 | case offsetof(struct bpf_sockopt, level): |
| 2042 | if (type == BPF_WRITE) |
| 2043 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level); |
| 2044 | else |
| 2045 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level); |
| 2046 | break; |
| 2047 | case offsetof(struct bpf_sockopt, optname): |
| 2048 | if (type == BPF_WRITE) |
| 2049 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname); |
| 2050 | else |
| 2051 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname); |
| 2052 | break; |
| 2053 | case offsetof(struct bpf_sockopt, optlen): |
| 2054 | if (type == BPF_WRITE) |
| 2055 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen); |
| 2056 | else |
| 2057 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen); |
| 2058 | break; |
| 2059 | case offsetof(struct bpf_sockopt, retval): |
| 2060 | if (type == BPF_WRITE) |
| 2061 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, retval); |
| 2062 | else |
| 2063 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, retval); |
| 2064 | break; |
| 2065 | case offsetof(struct bpf_sockopt, optval): |
| 2066 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval); |
| 2067 | break; |
| 2068 | case offsetof(struct bpf_sockopt, optval_end): |
| 2069 | *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end); |
| 2070 | break; |
| 2071 | } |
| 2072 | |
| 2073 | return insn - insn_buf; |
| 2074 | } |
| 2075 | |
| 2076 | static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf, |
| 2077 | bool direct_write, |
| 2078 | const struct bpf_prog *prog) |
| 2079 | { |
| 2080 | /* Nothing to do for sockopt argument. The data is kzalloc'ated. |
| 2081 | */ |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | const struct bpf_verifier_ops cg_sockopt_verifier_ops = { |
| 2086 | .get_func_proto = cg_sockopt_func_proto, |
| 2087 | .is_valid_access = cg_sockopt_is_valid_access, |
| 2088 | .convert_ctx_access = cg_sockopt_convert_ctx_access, |
| 2089 | .gen_prologue = cg_sockopt_get_prologue, |
| 2090 | }; |
| 2091 | |
| 2092 | const struct bpf_prog_ops cg_sockopt_prog_ops = { |
| 2093 | }; |