1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2017 Facebook 3 */ 4 #include <linux/slab.h> 5 #include <linux/bpf.h> 6 #include <linux/btf.h> 7 8 #include "map_in_map.h" 9 10 struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd) 11 { 12 struct bpf_map *inner_map, *inner_map_meta; 13 u32 inner_map_meta_size; 14 struct fd f; 15 int ret; 16 17 f = fdget(inner_map_ufd); 18 inner_map = __bpf_map_get(f); 19 if (IS_ERR(inner_map)) 20 return inner_map; 21 22 /* Does not support >1 level map-in-map */ 23 if (inner_map->inner_map_meta) { 24 ret = -EINVAL; 25 goto put; 26 } 27 28 if (!inner_map->ops->map_meta_equal) { 29 ret = -ENOTSUPP; 30 goto put; 31 } 32 33 inner_map_meta_size = sizeof(*inner_map_meta); 34 /* In some cases verifier needs to access beyond just base map. */ 35 if (inner_map->ops == &array_map_ops) 36 inner_map_meta_size = sizeof(struct bpf_array); 37 38 inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER); 39 if (!inner_map_meta) { 40 ret = -ENOMEM; 41 goto put; 42 } 43 44 inner_map_meta->map_type = inner_map->map_type; 45 inner_map_meta->key_size = inner_map->key_size; 46 inner_map_meta->value_size = inner_map->value_size; 47 inner_map_meta->map_flags = inner_map->map_flags; 48 inner_map_meta->max_entries = inner_map->max_entries; 49 50 inner_map_meta->record = btf_record_dup(inner_map->record); 51 if (IS_ERR(inner_map_meta->record)) { 52 /* btf_record_dup returns NULL or valid pointer in case of 53 * invalid/empty/valid, but ERR_PTR in case of errors. During 54 * equality NULL or IS_ERR is equivalent. 55 */ 56 ret = PTR_ERR(inner_map_meta->record); 57 goto free; 58 } 59 if (inner_map_meta->record) { 60 struct btf_field_offs *field_offs; 61 /* If btf_record is !IS_ERR_OR_NULL, then field_offs is always 62 * valid. 63 */ 64 field_offs = kmemdup(inner_map->field_offs, sizeof(*inner_map->field_offs), GFP_KERNEL | __GFP_NOWARN); 65 if (!field_offs) { 66 ret = -ENOMEM; 67 goto free_rec; 68 } 69 inner_map_meta->field_offs = field_offs; 70 } 71 if (inner_map->btf) { 72 btf_get(inner_map->btf); 73 inner_map_meta->btf = inner_map->btf; 74 } 75 76 /* Misc members not needed in bpf_map_meta_equal() check. */ 77 inner_map_meta->ops = inner_map->ops; 78 if (inner_map->ops == &array_map_ops) { 79 inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1; 80 container_of(inner_map_meta, struct bpf_array, map)->index_mask = 81 container_of(inner_map, struct bpf_array, map)->index_mask; 82 } 83 84 fdput(f); 85 return inner_map_meta; 86 free_rec: 87 btf_record_free(inner_map_meta->record); 88 free: 89 kfree(inner_map_meta); 90 put: 91 fdput(f); 92 return ERR_PTR(ret); 93 } 94 95 void bpf_map_meta_free(struct bpf_map *map_meta) 96 { 97 kfree(map_meta->field_offs); 98 bpf_map_free_record(map_meta); 99 btf_put(map_meta->btf); 100 kfree(map_meta); 101 } 102 103 bool bpf_map_meta_equal(const struct bpf_map *meta0, 104 const struct bpf_map *meta1) 105 { 106 /* No need to compare ops because it is covered by map_type */ 107 return meta0->map_type == meta1->map_type && 108 meta0->key_size == meta1->key_size && 109 meta0->value_size == meta1->value_size && 110 meta0->map_flags == meta1->map_flags && 111 btf_record_equal(meta0->record, meta1->record); 112 } 113 114 void *bpf_map_fd_get_ptr(struct bpf_map *map, 115 struct file *map_file /* not used */, 116 int ufd) 117 { 118 struct bpf_map *inner_map, *inner_map_meta; 119 struct fd f; 120 121 f = fdget(ufd); 122 inner_map = __bpf_map_get(f); 123 if (IS_ERR(inner_map)) 124 return inner_map; 125 126 inner_map_meta = map->inner_map_meta; 127 if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map)) 128 bpf_map_inc(inner_map); 129 else 130 inner_map = ERR_PTR(-EINVAL); 131 132 fdput(f); 133 return inner_map; 134 } 135 136 void bpf_map_fd_put_ptr(void *ptr) 137 { 138 /* ptr->ops->map_free() has to go through one 139 * rcu grace period by itself. 140 */ 141 bpf_map_put(ptr); 142 } 143 144 u32 bpf_map_fd_sys_lookup_elem(void *ptr) 145 { 146 return ((struct bpf_map *)ptr)->id; 147 } 148