|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <linux/rculist.h> |
| 4 | +#include <linux/list.h> |
| 5 | +#include <linux/hash.h> |
| 6 | +#include <linux/types.h> |
| 7 | +#include <linux/spinlock.h> |
| 8 | +#include <linux/bpf.h> |
| 9 | +#include <linux/bpf_local_storage.h> |
| 10 | +#include <linux/bpf_lsm.h> |
| 11 | +#include <linux/cred.h> |
| 12 | +#include <linux/btf_ids.h> |
| 13 | +#include <linux/rcupdate_trace.h> |
| 14 | + |
| 15 | +DEFINE_BPF_STORAGE_CACHE(cred_cache); |
| 16 | + |
| 17 | +static struct bpf_local_storage __rcu **cred_storage_ptr(void *owner) |
| 18 | +{ |
| 19 | + struct cred *cred = owner; |
| 20 | + struct bpf_storage_blob *bsb; |
| 21 | + |
| 22 | + bsb = bpf_cred(cred); |
| 23 | + if (!bsb) |
| 24 | + return NULL; |
| 25 | + return &bsb->storage; |
| 26 | +} |
| 27 | + |
| 28 | +static struct bpf_local_storage_data *cred_storage_lookup(struct cred *cred, |
| 29 | + struct bpf_map *map, |
| 30 | + bool cacheit_lockit) |
| 31 | +{ |
| 32 | + struct bpf_local_storage *cred_storage; |
| 33 | + struct bpf_local_storage_map *smap; |
| 34 | + struct bpf_storage_blob *bsb; |
| 35 | + |
| 36 | + bsb = bpf_cred(cred); |
| 37 | + if (!bsb) |
| 38 | + return NULL; |
| 39 | + |
| 40 | + cred_storage = rcu_dereference_check(bsb->storage, bpf_rcu_lock_held()); |
| 41 | + if (!cred_storage) |
| 42 | + return NULL; |
| 43 | + |
| 44 | + smap = (struct bpf_local_storage_map *)map; |
| 45 | + return bpf_local_storage_lookup(cred_storage, smap, cacheit_lockit); |
| 46 | +} |
| 47 | + |
| 48 | +void bpf_cred_storage_free(struct cred *cred) |
| 49 | +{ |
| 50 | + struct bpf_local_storage *local_storage; |
| 51 | + struct bpf_storage_blob *bsb; |
| 52 | + |
| 53 | + bsb = bpf_cred(cred); |
| 54 | + if (!bsb) |
| 55 | + return; |
| 56 | + |
| 57 | + migrate_disable(); |
| 58 | + rcu_read_lock(); |
| 59 | + |
| 60 | + local_storage = rcu_dereference(bsb->storage); |
| 61 | + if (!local_storage) |
| 62 | + goto out; |
| 63 | + |
| 64 | + bpf_local_storage_destroy(local_storage); |
| 65 | +out: |
| 66 | + rcu_read_unlock(); |
| 67 | + migrate_enable(); |
| 68 | +} |
| 69 | + |
| 70 | +static int cred_storage_delete(struct cred *cred, struct bpf_map *map) |
| 71 | +{ |
| 72 | + struct bpf_local_storage_data *sdata; |
| 73 | + |
| 74 | + sdata = cred_storage_lookup(cred, map, false); |
| 75 | + if (!sdata) |
| 76 | + return -ENOENT; |
| 77 | + |
| 78 | + bpf_selem_unlink(SELEM(sdata), false); |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +static struct bpf_map *cred_storage_map_alloc(union bpf_attr *attr) |
| 84 | +{ |
| 85 | + return bpf_local_storage_map_alloc(attr, &cred_cache, false); |
| 86 | +} |
| 87 | + |
| 88 | +static void cred_storage_map_free(struct bpf_map *map) |
| 89 | +{ |
| 90 | + bpf_local_storage_map_free(map, &cred_cache, NULL); |
| 91 | +} |
| 92 | + |
| 93 | +static int notsupp_get_next_key(struct bpf_map *map, void *key, |
| 94 | + void *next_key) |
| 95 | +{ |
| 96 | + return -ENOTSUPP; |
| 97 | +} |
| 98 | + |
| 99 | +const struct bpf_map_ops cred_storage_map_ops = { |
| 100 | + .map_meta_equal = bpf_map_meta_equal, |
| 101 | + .map_alloc_check = bpf_local_storage_map_alloc_check, |
| 102 | + .map_alloc = cred_storage_map_alloc, |
| 103 | + .map_free = cred_storage_map_free, |
| 104 | + .map_get_next_key = notsupp_get_next_key, |
| 105 | + .map_check_btf = bpf_local_storage_map_check_btf, |
| 106 | + .map_mem_usage = bpf_local_storage_map_mem_usage, |
| 107 | + .map_btf_id = &bpf_local_storage_map_btf_id[0], |
| 108 | + .map_owner_storage_ptr = cred_storage_ptr, |
| 109 | +}; |
| 110 | + |
| 111 | +BTF_ID_LIST_SINGLE(bpf_cred_storage_btf_ids, struct, cred) |
| 112 | + |
| 113 | +__bpf_kfunc struct bpf_local_storage_data *bpf_cred_storage_get(struct bpf_map *map, |
| 114 | + struct cred *cred, |
| 115 | + void *init, |
| 116 | + int init__sz, |
| 117 | + u64 flags) |
| 118 | +{ |
| 119 | + struct bpf_local_storage_data *sdata; |
| 120 | + |
| 121 | + WARN_ON_ONCE(!bpf_rcu_lock_held()); |
| 122 | + if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) |
| 123 | + return NULL; |
| 124 | + |
| 125 | + if (!cred || !cred_storage_ptr(cred)) |
| 126 | + return NULL; |
| 127 | + |
| 128 | + sdata = cred_storage_lookup(cred, map, true); |
| 129 | + if (sdata) |
| 130 | + return sdata; |
| 131 | + |
| 132 | + /* This helper must only called from where the cred is guaranteed |
| 133 | + * to have a refcount and cannot be freed. |
| 134 | + */ |
| 135 | + if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) { |
| 136 | + sdata = bpf_local_storage_update( |
| 137 | + cred, (struct bpf_local_storage_map *)map, init, |
| 138 | + BPF_NOEXIST, false, GFP_ATOMIC); |
| 139 | + return IS_ERR(sdata) ? NULL : sdata; |
| 140 | + } |
| 141 | + |
| 142 | + return NULL; |
| 143 | +} |
| 144 | + |
| 145 | +__bpf_kfunc int bpf_cred_storage_delete(struct bpf_map *map, struct cred *cred) |
| 146 | +{ |
| 147 | + if (!cred) |
| 148 | + return -EINVAL; |
| 149 | + |
| 150 | + return cred_storage_delete(cred, map); |
| 151 | +} |
| 152 | + |
| 153 | +BTF_KFUNCS_START(bpf_cred_storage_kfunc_ids) |
| 154 | +BTF_ID_FLAGS(func, bpf_cred_storage_delete, 0) |
| 155 | +BTF_ID_FLAGS(func, bpf_cred_storage_get, KF_RET_NULL) |
| 156 | +BTF_KFUNCS_END(bpf_cred_storage_kfunc_ids) |
| 157 | + |
| 158 | +static const struct btf_kfunc_id_set bpf_cred_storage_kfunc_set = { |
| 159 | + .owner = THIS_MODULE, |
| 160 | + .set = &bpf_cred_storage_kfunc_ids, |
| 161 | +}; |
| 162 | + |
| 163 | +static int __init bpf_cred_storage_init(void) |
| 164 | +{ |
| 165 | + int err; |
| 166 | + err = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_cred_storage_kfunc_set); |
| 167 | + if (err) { |
| 168 | + pr_err("bpf_cred_storage: failed to register kfuncs: %d\n", err); |
| 169 | + return err; |
| 170 | + } |
| 171 | + |
| 172 | + pr_info("bpf_cred_storage: kfuncs registered successfully\n"); |
| 173 | + return 0; |
| 174 | +} |
| 175 | +late_initcall(bpf_cred_storage_init); |
0 commit comments