bpf_inode_storage.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2019 Facebook
  4. * Copyright 2020 Google LLC.
  5. */
  6. #include <linux/rculist.h>
  7. #include <linux/list.h>
  8. #include <linux/hash.h>
  9. #include <linux/types.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/bpf.h>
  12. #include <linux/bpf_local_storage.h>
  13. #include <net/sock.h>
  14. #include <uapi/linux/sock_diag.h>
  15. #include <uapi/linux/btf.h>
  16. #include <linux/bpf_lsm.h>
  17. #include <linux/btf_ids.h>
  18. #include <linux/fdtable.h>
  19. #include <linux/rcupdate_trace.h>
  20. DEFINE_BPF_STORAGE_CACHE(inode_cache);
  21. static struct bpf_local_storage __rcu **
  22. inode_storage_ptr(void *owner)
  23. {
  24. struct inode *inode = owner;
  25. struct bpf_storage_blob *bsb;
  26. bsb = bpf_inode(inode);
  27. if (!bsb)
  28. return NULL;
  29. return &bsb->storage;
  30. }
  31. static struct bpf_local_storage_data *inode_storage_lookup(struct inode *inode,
  32. struct bpf_map *map,
  33. bool cacheit_lockit)
  34. {
  35. struct bpf_local_storage *inode_storage;
  36. struct bpf_local_storage_map *smap;
  37. struct bpf_storage_blob *bsb;
  38. bsb = bpf_inode(inode);
  39. if (!bsb)
  40. return NULL;
  41. inode_storage =
  42. rcu_dereference_check(bsb->storage, bpf_rcu_lock_held());
  43. if (!inode_storage)
  44. return NULL;
  45. smap = (struct bpf_local_storage_map *)map;
  46. return bpf_local_storage_lookup(inode_storage, smap, cacheit_lockit);
  47. }
  48. void bpf_inode_storage_free(struct inode *inode)
  49. {
  50. struct bpf_local_storage *local_storage;
  51. struct bpf_storage_blob *bsb;
  52. bsb = bpf_inode(inode);
  53. if (!bsb)
  54. return;
  55. rcu_read_lock();
  56. local_storage = rcu_dereference(bsb->storage);
  57. if (!local_storage) {
  58. rcu_read_unlock();
  59. return;
  60. }
  61. bpf_local_storage_destroy(local_storage);
  62. rcu_read_unlock();
  63. }
  64. static void *bpf_fd_inode_storage_lookup_elem(struct bpf_map *map, void *key)
  65. {
  66. struct bpf_local_storage_data *sdata;
  67. CLASS(fd_raw, f)(*(int *)key);
  68. if (fd_empty(f))
  69. return ERR_PTR(-EBADF);
  70. sdata = inode_storage_lookup(file_inode(fd_file(f)), map, true);
  71. return sdata ? sdata->data : NULL;
  72. }
  73. static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
  74. void *value, u64 map_flags)
  75. {
  76. struct bpf_local_storage_data *sdata;
  77. CLASS(fd_raw, f)(*(int *)key);
  78. if (fd_empty(f))
  79. return -EBADF;
  80. if (!inode_storage_ptr(file_inode(fd_file(f))))
  81. return -EBADF;
  82. sdata = bpf_local_storage_update(file_inode(fd_file(f)),
  83. (struct bpf_local_storage_map *)map,
  84. value, map_flags, GFP_ATOMIC);
  85. return PTR_ERR_OR_ZERO(sdata);
  86. }
  87. static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
  88. {
  89. struct bpf_local_storage_data *sdata;
  90. sdata = inode_storage_lookup(inode, map, false);
  91. if (!sdata)
  92. return -ENOENT;
  93. bpf_selem_unlink(SELEM(sdata), false);
  94. return 0;
  95. }
  96. static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
  97. {
  98. CLASS(fd_raw, f)(*(int *)key);
  99. if (fd_empty(f))
  100. return -EBADF;
  101. return inode_storage_delete(file_inode(fd_file(f)), map);
  102. }
  103. /* *gfp_flags* is a hidden argument provided by the verifier */
  104. BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
  105. void *, value, u64, flags, gfp_t, gfp_flags)
  106. {
  107. struct bpf_local_storage_data *sdata;
  108. WARN_ON_ONCE(!bpf_rcu_lock_held());
  109. if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
  110. return (unsigned long)NULL;
  111. /* explicitly check that the inode_storage_ptr is not
  112. * NULL as inode_storage_lookup returns NULL in this case and
  113. * bpf_local_storage_update expects the owner to have a
  114. * valid storage pointer.
  115. */
  116. if (!inode || !inode_storage_ptr(inode))
  117. return (unsigned long)NULL;
  118. sdata = inode_storage_lookup(inode, map, true);
  119. if (sdata)
  120. return (unsigned long)sdata->data;
  121. /* This helper must only called from where the inode is guaranteed
  122. * to have a refcount and cannot be freed.
  123. */
  124. if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) {
  125. sdata = bpf_local_storage_update(
  126. inode, (struct bpf_local_storage_map *)map, value,
  127. BPF_NOEXIST, gfp_flags);
  128. return IS_ERR(sdata) ? (unsigned long)NULL :
  129. (unsigned long)sdata->data;
  130. }
  131. return (unsigned long)NULL;
  132. }
  133. BPF_CALL_2(bpf_inode_storage_delete,
  134. struct bpf_map *, map, struct inode *, inode)
  135. {
  136. WARN_ON_ONCE(!bpf_rcu_lock_held());
  137. if (!inode)
  138. return -EINVAL;
  139. /* This helper must only called from where the inode is guaranteed
  140. * to have a refcount and cannot be freed.
  141. */
  142. return inode_storage_delete(inode, map);
  143. }
  144. static int notsupp_get_next_key(struct bpf_map *map, void *key,
  145. void *next_key)
  146. {
  147. return -ENOTSUPP;
  148. }
  149. static struct bpf_map *inode_storage_map_alloc(union bpf_attr *attr)
  150. {
  151. return bpf_local_storage_map_alloc(attr, &inode_cache, false);
  152. }
  153. static void inode_storage_map_free(struct bpf_map *map)
  154. {
  155. bpf_local_storage_map_free(map, &inode_cache, NULL);
  156. }
  157. const struct bpf_map_ops inode_storage_map_ops = {
  158. .map_meta_equal = bpf_map_meta_equal,
  159. .map_alloc_check = bpf_local_storage_map_alloc_check,
  160. .map_alloc = inode_storage_map_alloc,
  161. .map_free = inode_storage_map_free,
  162. .map_get_next_key = notsupp_get_next_key,
  163. .map_lookup_elem = bpf_fd_inode_storage_lookup_elem,
  164. .map_update_elem = bpf_fd_inode_storage_update_elem,
  165. .map_delete_elem = bpf_fd_inode_storage_delete_elem,
  166. .map_check_btf = bpf_local_storage_map_check_btf,
  167. .map_mem_usage = bpf_local_storage_map_mem_usage,
  168. .map_btf_id = &bpf_local_storage_map_btf_id[0],
  169. .map_owner_storage_ptr = inode_storage_ptr,
  170. };
  171. BTF_ID_LIST_SINGLE(bpf_inode_storage_btf_ids, struct, inode)
  172. const struct bpf_func_proto bpf_inode_storage_get_proto = {
  173. .func = bpf_inode_storage_get,
  174. .gpl_only = false,
  175. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  176. .arg1_type = ARG_CONST_MAP_PTR,
  177. .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
  178. .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
  179. .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
  180. .arg4_type = ARG_ANYTHING,
  181. };
  182. const struct bpf_func_proto bpf_inode_storage_delete_proto = {
  183. .func = bpf_inode_storage_delete,
  184. .gpl_only = false,
  185. .ret_type = RET_INTEGER,
  186. .arg1_type = ARG_CONST_MAP_PTR,
  187. .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
  188. .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
  189. };