1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
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>
9 #include <linux/btf_ids.h>
10 #include <linux/bpf_local_storage.h>
11 #include <net/bpf_sk_storage.h>
13 #include <uapi/linux/sock_diag.h>
14 #include <uapi/linux/btf.h>
16 DEFINE_BPF_STORAGE_CACHE(sk_cache);
18 static struct bpf_local_storage_data *
19 sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit)
21 struct bpf_local_storage *sk_storage;
22 struct bpf_local_storage_map *smap;
24 sk_storage = rcu_dereference(sk->sk_bpf_storage);
28 smap = (struct bpf_local_storage_map *)map;
29 return bpf_local_storage_lookup(sk_storage, smap, cacheit_lockit);
32 static int sk_storage_delete(struct sock *sk, struct bpf_map *map)
34 struct bpf_local_storage_data *sdata;
36 sdata = sk_storage_lookup(sk, map, false);
40 bpf_selem_unlink(SELEM(sdata));
45 /* Called by __sk_destruct() & bpf_sk_storage_clone() */
46 void bpf_sk_storage_free(struct sock *sk)
48 struct bpf_local_storage_elem *selem;
49 struct bpf_local_storage *sk_storage;
50 bool free_sk_storage = false;
54 sk_storage = rcu_dereference(sk->sk_bpf_storage);
60 /* Netiher the bpf_prog nor the bpf-map's syscall
61 * could be modifying the sk_storage->list now.
62 * Thus, no elem can be added-to or deleted-from the
63 * sk_storage->list by the bpf_prog or by the bpf-map's syscall.
65 * It is racing with bpf_local_storage_map_free() alone
66 * when unlinking elem from the sk_storage->list and
67 * the map's bucket->list.
69 raw_spin_lock_bh(&sk_storage->lock);
70 hlist_for_each_entry_safe(selem, n, &sk_storage->list, snode) {
71 /* Always unlink from map before unlinking from
74 bpf_selem_unlink_map(selem);
75 free_sk_storage = bpf_selem_unlink_storage_nolock(sk_storage,
78 raw_spin_unlock_bh(&sk_storage->lock);
82 kfree_rcu(sk_storage, rcu);
85 static void sk_storage_map_free(struct bpf_map *map)
87 struct bpf_local_storage_map *smap;
89 smap = (struct bpf_local_storage_map *)map;
90 bpf_local_storage_cache_idx_free(&sk_cache, smap->cache_idx);
91 bpf_local_storage_map_free(smap);
94 static struct bpf_map *sk_storage_map_alloc(union bpf_attr *attr)
96 struct bpf_local_storage_map *smap;
98 smap = bpf_local_storage_map_alloc(attr);
100 return ERR_CAST(smap);
102 smap->cache_idx = bpf_local_storage_cache_idx_get(&sk_cache);
106 static int notsupp_get_next_key(struct bpf_map *map, void *key,
112 static void *bpf_fd_sk_storage_lookup_elem(struct bpf_map *map, void *key)
114 struct bpf_local_storage_data *sdata;
119 sock = sockfd_lookup(fd, &err);
121 sdata = sk_storage_lookup(sock->sk, map, true);
123 return sdata ? sdata->data : NULL;
129 static int bpf_fd_sk_storage_update_elem(struct bpf_map *map, void *key,
130 void *value, u64 map_flags)
132 struct bpf_local_storage_data *sdata;
137 sock = sockfd_lookup(fd, &err);
139 sdata = bpf_local_storage_update(
140 sock->sk, (struct bpf_local_storage_map *)map, value,
143 return PTR_ERR_OR_ZERO(sdata);
149 static int bpf_fd_sk_storage_delete_elem(struct bpf_map *map, void *key)
155 sock = sockfd_lookup(fd, &err);
157 err = sk_storage_delete(sock->sk, map);
165 static struct bpf_local_storage_elem *
166 bpf_sk_storage_clone_elem(struct sock *newsk,
167 struct bpf_local_storage_map *smap,
168 struct bpf_local_storage_elem *selem)
170 struct bpf_local_storage_elem *copy_selem;
172 copy_selem = bpf_selem_alloc(smap, newsk, NULL, true);
176 if (map_value_has_spin_lock(&smap->map))
177 copy_map_value_locked(&smap->map, SDATA(copy_selem)->data,
178 SDATA(selem)->data, true);
180 copy_map_value(&smap->map, SDATA(copy_selem)->data,
186 int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
188 struct bpf_local_storage *new_sk_storage = NULL;
189 struct bpf_local_storage *sk_storage;
190 struct bpf_local_storage_elem *selem;
193 RCU_INIT_POINTER(newsk->sk_bpf_storage, NULL);
196 sk_storage = rcu_dereference(sk->sk_bpf_storage);
198 if (!sk_storage || hlist_empty(&sk_storage->list))
201 hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
202 struct bpf_local_storage_elem *copy_selem;
203 struct bpf_local_storage_map *smap;
206 smap = rcu_dereference(SDATA(selem)->smap);
207 if (!(smap->map.map_flags & BPF_F_CLONE))
210 /* Note that for lockless listeners adding new element
211 * here can race with cleanup in bpf_local_storage_map_free.
212 * Try to grab map refcnt to make sure that it's still
213 * alive and prevent concurrent removal.
215 map = bpf_map_inc_not_zero(&smap->map);
219 copy_selem = bpf_sk_storage_clone_elem(newsk, smap, selem);
226 if (new_sk_storage) {
227 bpf_selem_link_map(smap, copy_selem);
228 bpf_selem_link_storage_nolock(new_sk_storage, copy_selem);
230 ret = bpf_local_storage_alloc(newsk, smap, copy_selem);
233 atomic_sub(smap->elem_size,
234 &newsk->sk_omem_alloc);
240 rcu_dereference(copy_selem->local_storage);
248 /* In case of an error, don't free anything explicitly here, the
249 * caller is responsible to call bpf_sk_storage_free.
255 BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
256 void *, value, u64, flags)
258 struct bpf_local_storage_data *sdata;
260 if (!sk || !sk_fullsock(sk) || flags > BPF_SK_STORAGE_GET_F_CREATE)
261 return (unsigned long)NULL;
263 sdata = sk_storage_lookup(sk, map, true);
265 return (unsigned long)sdata->data;
267 if (flags == BPF_SK_STORAGE_GET_F_CREATE &&
268 /* Cannot add new elem to a going away sk.
269 * Otherwise, the new elem may become a leak
270 * (and also other memory issues during map
273 refcount_inc_not_zero(&sk->sk_refcnt)) {
274 sdata = bpf_local_storage_update(
275 sk, (struct bpf_local_storage_map *)map, value,
277 /* sk must be a fullsock (guaranteed by verifier),
278 * so sock_gen_put() is unnecessary.
281 return IS_ERR(sdata) ?
282 (unsigned long)NULL : (unsigned long)sdata->data;
285 return (unsigned long)NULL;
288 BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk)
290 if (!sk || !sk_fullsock(sk))
293 if (refcount_inc_not_zero(&sk->sk_refcnt)) {
296 err = sk_storage_delete(sk, map);
304 static int sk_storage_charge(struct bpf_local_storage_map *smap,
305 void *owner, u32 size)
307 int optmem_max = READ_ONCE(sysctl_optmem_max);
308 struct sock *sk = (struct sock *)owner;
310 /* same check as in sock_kmalloc() */
311 if (size <= optmem_max &&
312 atomic_read(&sk->sk_omem_alloc) + size < optmem_max) {
313 atomic_add(size, &sk->sk_omem_alloc);
320 static void sk_storage_uncharge(struct bpf_local_storage_map *smap,
321 void *owner, u32 size)
323 struct sock *sk = owner;
325 atomic_sub(size, &sk->sk_omem_alloc);
328 static struct bpf_local_storage __rcu **
329 sk_storage_ptr(void *owner)
331 struct sock *sk = owner;
333 return &sk->sk_bpf_storage;
336 static int sk_storage_map_btf_id;
337 const struct bpf_map_ops sk_storage_map_ops = {
338 .map_meta_equal = bpf_map_meta_equal,
339 .map_alloc_check = bpf_local_storage_map_alloc_check,
340 .map_alloc = sk_storage_map_alloc,
341 .map_free = sk_storage_map_free,
342 .map_get_next_key = notsupp_get_next_key,
343 .map_lookup_elem = bpf_fd_sk_storage_lookup_elem,
344 .map_update_elem = bpf_fd_sk_storage_update_elem,
345 .map_delete_elem = bpf_fd_sk_storage_delete_elem,
346 .map_check_btf = bpf_local_storage_map_check_btf,
347 .map_btf_name = "bpf_local_storage_map",
348 .map_btf_id = &sk_storage_map_btf_id,
349 .map_local_storage_charge = sk_storage_charge,
350 .map_local_storage_uncharge = sk_storage_uncharge,
351 .map_owner_storage_ptr = sk_storage_ptr,
354 const struct bpf_func_proto bpf_sk_storage_get_proto = {
355 .func = bpf_sk_storage_get,
357 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
358 .arg1_type = ARG_CONST_MAP_PTR,
359 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
360 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
361 .arg4_type = ARG_ANYTHING,
364 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto = {
365 .func = bpf_sk_storage_get,
367 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
368 .arg1_type = ARG_CONST_MAP_PTR,
369 .arg2_type = ARG_PTR_TO_CTX, /* context is 'struct sock' */
370 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
371 .arg4_type = ARG_ANYTHING,
374 const struct bpf_func_proto bpf_sk_storage_delete_proto = {
375 .func = bpf_sk_storage_delete,
377 .ret_type = RET_INTEGER,
378 .arg1_type = ARG_CONST_MAP_PTR,
379 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
382 struct bpf_sk_storage_diag {
384 struct bpf_map *maps[];
387 /* The reply will be like:
388 * INET_DIAG_BPF_SK_STORAGES (nla_nest)
389 * SK_DIAG_BPF_STORAGE (nla_nest)
390 * SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32)
391 * SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit)
392 * SK_DIAG_BPF_STORAGE (nla_nest)
393 * SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32)
394 * SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit)
397 static int nla_value_size(u32 value_size)
399 /* SK_DIAG_BPF_STORAGE (nla_nest)
400 * SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32)
401 * SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit)
403 return nla_total_size(0) + nla_total_size(sizeof(u32)) +
404 nla_total_size_64bit(value_size);
407 void bpf_sk_storage_diag_free(struct bpf_sk_storage_diag *diag)
414 for (i = 0; i < diag->nr_maps; i++)
415 bpf_map_put(diag->maps[i]);
419 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_free);
421 static bool diag_check_dup(const struct bpf_sk_storage_diag *diag,
422 const struct bpf_map *map)
426 for (i = 0; i < diag->nr_maps; i++) {
427 if (diag->maps[i] == map)
434 struct bpf_sk_storage_diag *
435 bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
437 struct bpf_sk_storage_diag *diag;
442 /* bpf_local_storage_map is currently limited to CAP_SYS_ADMIN as
443 * the map_alloc_check() side also does.
446 return ERR_PTR(-EPERM);
448 nla_for_each_nested(nla, nla_stgs, rem) {
449 if (nla_type(nla) == SK_DIAG_BPF_STORAGE_REQ_MAP_FD) {
450 if (nla_len(nla) != sizeof(u32))
451 return ERR_PTR(-EINVAL);
456 diag = kzalloc(sizeof(*diag) + sizeof(diag->maps[0]) * nr_maps,
459 return ERR_PTR(-ENOMEM);
461 nla_for_each_nested(nla, nla_stgs, rem) {
465 if (nla_type(nla) != SK_DIAG_BPF_STORAGE_REQ_MAP_FD)
468 map_fd = nla_get_u32(nla);
469 map = bpf_map_get(map_fd);
474 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) {
479 if (diag_check_dup(diag, map)) {
484 diag->maps[diag->nr_maps++] = map;
490 bpf_sk_storage_diag_free(diag);
493 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_alloc);
495 static int diag_get(struct bpf_local_storage_data *sdata, struct sk_buff *skb)
497 struct nlattr *nla_stg, *nla_value;
498 struct bpf_local_storage_map *smap;
500 /* It cannot exceed max nlattr's payload */
501 BUILD_BUG_ON(U16_MAX - NLA_HDRLEN < BPF_LOCAL_STORAGE_MAX_VALUE_SIZE);
503 nla_stg = nla_nest_start(skb, SK_DIAG_BPF_STORAGE);
507 smap = rcu_dereference(sdata->smap);
508 if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, smap->map.id))
511 nla_value = nla_reserve_64bit(skb, SK_DIAG_BPF_STORAGE_MAP_VALUE,
512 smap->map.value_size,
513 SK_DIAG_BPF_STORAGE_PAD);
517 if (map_value_has_spin_lock(&smap->map))
518 copy_map_value_locked(&smap->map, nla_data(nla_value),
521 copy_map_value(&smap->map, nla_data(nla_value), sdata->data);
523 nla_nest_end(skb, nla_stg);
527 nla_nest_cancel(skb, nla_stg);
531 static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb,
533 unsigned int *res_diag_size)
535 /* stg_array_type (e.g. INET_DIAG_BPF_SK_STORAGES) */
536 unsigned int diag_size = nla_total_size(0);
537 struct bpf_local_storage *sk_storage;
538 struct bpf_local_storage_elem *selem;
539 struct bpf_local_storage_map *smap;
540 struct nlattr *nla_stgs;
541 unsigned int saved_len;
546 sk_storage = rcu_dereference(sk->sk_bpf_storage);
547 if (!sk_storage || hlist_empty(&sk_storage->list)) {
552 nla_stgs = nla_nest_start(skb, stg_array_type);
554 /* Continue to learn diag_size */
557 saved_len = skb->len;
558 hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
559 smap = rcu_dereference(SDATA(selem)->smap);
560 diag_size += nla_value_size(smap->map.value_size);
562 if (nla_stgs && diag_get(SDATA(selem), skb))
563 /* Continue to learn diag_size */
570 if (saved_len == skb->len)
571 nla_nest_cancel(skb, nla_stgs);
573 nla_nest_end(skb, nla_stgs);
576 if (diag_size == nla_total_size(0)) {
581 *res_diag_size = diag_size;
585 int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,
586 struct sock *sk, struct sk_buff *skb,
588 unsigned int *res_diag_size)
590 /* stg_array_type (e.g. INET_DIAG_BPF_SK_STORAGES) */
591 unsigned int diag_size = nla_total_size(0);
592 struct bpf_local_storage *sk_storage;
593 struct bpf_local_storage_data *sdata;
594 struct nlattr *nla_stgs;
595 unsigned int saved_len;
601 /* No map has been specified. Dump all. */
603 return bpf_sk_storage_diag_put_all(sk, skb, stg_array_type,
607 sk_storage = rcu_dereference(sk->sk_bpf_storage);
608 if (!sk_storage || hlist_empty(&sk_storage->list)) {
613 nla_stgs = nla_nest_start(skb, stg_array_type);
615 /* Continue to learn diag_size */
618 saved_len = skb->len;
619 for (i = 0; i < diag->nr_maps; i++) {
620 sdata = bpf_local_storage_lookup(sk_storage,
621 (struct bpf_local_storage_map *)diag->maps[i],
627 diag_size += nla_value_size(diag->maps[i]->value_size);
629 if (nla_stgs && diag_get(sdata, skb))
630 /* Continue to learn diag_size */
636 if (saved_len == skb->len)
637 nla_nest_cancel(skb, nla_stgs);
639 nla_nest_end(skb, nla_stgs);
642 if (diag_size == nla_total_size(0)) {
647 *res_diag_size = diag_size;
650 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_put);
652 struct bpf_iter_seq_sk_storage_map_info {
654 unsigned int bucket_id;
658 static struct bpf_local_storage_elem *
659 bpf_sk_storage_map_seq_find_next(struct bpf_iter_seq_sk_storage_map_info *info,
660 struct bpf_local_storage_elem *prev_selem)
661 __acquires(RCU) __releases(RCU)
663 struct bpf_local_storage *sk_storage;
664 struct bpf_local_storage_elem *selem;
665 u32 skip_elems = info->skip_elems;
666 struct bpf_local_storage_map *smap;
667 u32 bucket_id = info->bucket_id;
668 u32 i, count, n_buckets;
669 struct bpf_local_storage_map_bucket *b;
671 smap = (struct bpf_local_storage_map *)info->map;
672 n_buckets = 1U << smap->bucket_log;
673 if (bucket_id >= n_buckets)
676 /* try to find next selem in the same bucket */
680 selem = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&selem->map_node)),
681 struct bpf_local_storage_elem, map_node);
683 /* not found, unlock and go to the next bucket */
684 b = &smap->buckets[bucket_id++];
689 sk_storage = rcu_dereference(selem->local_storage);
691 info->skip_elems = skip_elems + count;
697 for (i = bucket_id; i < (1U << smap->bucket_log); i++) {
698 b = &smap->buckets[i];
701 hlist_for_each_entry_rcu(selem, &b->list, map_node) {
702 sk_storage = rcu_dereference(selem->local_storage);
703 if (sk_storage && count >= skip_elems) {
705 info->skip_elems = count;
715 info->skip_elems = 0;
719 static void *bpf_sk_storage_map_seq_start(struct seq_file *seq, loff_t *pos)
721 struct bpf_local_storage_elem *selem;
723 selem = bpf_sk_storage_map_seq_find_next(seq->private, NULL);
732 static void *bpf_sk_storage_map_seq_next(struct seq_file *seq, void *v,
735 struct bpf_iter_seq_sk_storage_map_info *info = seq->private;
739 return bpf_sk_storage_map_seq_find_next(seq->private, v);
742 struct bpf_iter__bpf_sk_storage_map {
743 __bpf_md_ptr(struct bpf_iter_meta *, meta);
744 __bpf_md_ptr(struct bpf_map *, map);
745 __bpf_md_ptr(struct sock *, sk);
746 __bpf_md_ptr(void *, value);
749 DEFINE_BPF_ITER_FUNC(bpf_sk_storage_map, struct bpf_iter_meta *meta,
750 struct bpf_map *map, struct sock *sk,
753 static int __bpf_sk_storage_map_seq_show(struct seq_file *seq,
754 struct bpf_local_storage_elem *selem)
756 struct bpf_iter_seq_sk_storage_map_info *info = seq->private;
757 struct bpf_iter__bpf_sk_storage_map ctx = {};
758 struct bpf_local_storage *sk_storage;
759 struct bpf_iter_meta meta;
760 struct bpf_prog *prog;
764 prog = bpf_iter_get_info(&meta, selem == NULL);
769 sk_storage = rcu_dereference(selem->local_storage);
770 ctx.sk = sk_storage->owner;
771 ctx.value = SDATA(selem)->data;
773 ret = bpf_iter_run_prog(prog, &ctx);
779 static int bpf_sk_storage_map_seq_show(struct seq_file *seq, void *v)
781 return __bpf_sk_storage_map_seq_show(seq, v);
784 static void bpf_sk_storage_map_seq_stop(struct seq_file *seq, void *v)
788 (void)__bpf_sk_storage_map_seq_show(seq, v);
793 static int bpf_iter_init_sk_storage_map(void *priv_data,
794 struct bpf_iter_aux_info *aux)
796 struct bpf_iter_seq_sk_storage_map_info *seq_info = priv_data;
798 bpf_map_inc_with_uref(aux->map);
799 seq_info->map = aux->map;
803 static void bpf_iter_fini_sk_storage_map(void *priv_data)
805 struct bpf_iter_seq_sk_storage_map_info *seq_info = priv_data;
807 bpf_map_put_with_uref(seq_info->map);
810 static int bpf_iter_attach_map(struct bpf_prog *prog,
811 union bpf_iter_link_info *linfo,
812 struct bpf_iter_aux_info *aux)
817 if (!linfo->map.map_fd)
820 map = bpf_map_get_with_uref(linfo->map.map_fd);
824 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
827 if (prog->aux->max_rdwr_access > map->value_size) {
836 bpf_map_put_with_uref(map);
840 static void bpf_iter_detach_map(struct bpf_iter_aux_info *aux)
842 bpf_map_put_with_uref(aux->map);
845 static const struct seq_operations bpf_sk_storage_map_seq_ops = {
846 .start = bpf_sk_storage_map_seq_start,
847 .next = bpf_sk_storage_map_seq_next,
848 .stop = bpf_sk_storage_map_seq_stop,
849 .show = bpf_sk_storage_map_seq_show,
852 static const struct bpf_iter_seq_info iter_seq_info = {
853 .seq_ops = &bpf_sk_storage_map_seq_ops,
854 .init_seq_private = bpf_iter_init_sk_storage_map,
855 .fini_seq_private = bpf_iter_fini_sk_storage_map,
856 .seq_priv_size = sizeof(struct bpf_iter_seq_sk_storage_map_info),
859 static struct bpf_iter_reg bpf_sk_storage_map_reg_info = {
860 .target = "bpf_sk_storage_map",
861 .attach_target = bpf_iter_attach_map,
862 .detach_target = bpf_iter_detach_map,
863 .show_fdinfo = bpf_iter_map_show_fdinfo,
864 .fill_link_info = bpf_iter_map_fill_link_info,
865 .ctx_arg_info_size = 2,
867 { offsetof(struct bpf_iter__bpf_sk_storage_map, sk),
868 PTR_TO_BTF_ID_OR_NULL },
869 { offsetof(struct bpf_iter__bpf_sk_storage_map, value),
870 PTR_TO_RDWR_BUF_OR_NULL },
872 .seq_info = &iter_seq_info,
875 static int __init bpf_sk_storage_map_iter_init(void)
877 bpf_sk_storage_map_reg_info.ctx_arg_info[0].btf_id =
878 btf_sock_ids[BTF_SOCK_TYPE_SOCK];
879 return bpf_iter_reg_target(&bpf_sk_storage_map_reg_info);
881 late_initcall(bpf_sk_storage_map_iter_init);