blob: 583346a0ab299773ce8ab71d0f920a9cd36a20cf [file] [log] [blame]
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07001/* Copyright (c) 2017 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/slab.h>
8#include <linux/bpf.h>
9
10#include "map_in_map.h"
11
12struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
13{
14 struct bpf_map *inner_map, *inner_map_meta;
Daniel Borkmann9d5564d2019-01-17 16:34:45 +010015 u32 inner_map_meta_size;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070016 struct fd f;
17
18 f = fdget(inner_map_ufd);
19 inner_map = __bpf_map_get(f);
20 if (IS_ERR(inner_map))
21 return inner_map;
22
23 /* prog_array->owner_prog_type and owner_jited
24 * is a runtime binding. Doing static check alone
25 * in the verifier is not enough.
26 */
Roman Gushchin7b5dd2b2018-08-02 14:27:23 -070027 if (inner_map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Roman Gushchinc6fdcd62018-09-28 14:45:46 +000028 inner_map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
29 inner_map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070030 fdput(f);
31 return ERR_PTR(-ENOTSUPP);
32 }
33
34 /* Does not support >1 level map-in-map */
35 if (inner_map->inner_map_meta) {
36 fdput(f);
37 return ERR_PTR(-EINVAL);
38 }
39
Alexei Starovoitovd83525c2019-01-31 15:40:04 -080040 if (map_value_has_spin_lock(inner_map)) {
41 fdput(f);
42 return ERR_PTR(-ENOTSUPP);
43 }
44
Daniel Borkmann9d5564d2019-01-17 16:34:45 +010045 inner_map_meta_size = sizeof(*inner_map_meta);
46 /* In some cases verifier needs to access beyond just base map. */
47 if (inner_map->ops == &array_map_ops)
48 inner_map_meta_size = sizeof(struct bpf_array);
49
50 inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070051 if (!inner_map_meta) {
52 fdput(f);
53 return ERR_PTR(-ENOMEM);
54 }
55
56 inner_map_meta->map_type = inner_map->map_type;
57 inner_map_meta->key_size = inner_map->key_size;
58 inner_map_meta->value_size = inner_map->value_size;
59 inner_map_meta->map_flags = inner_map->map_flags;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070060 inner_map_meta->max_entries = inner_map->max_entries;
61
Daniel Borkmann9d5564d2019-01-17 16:34:45 +010062 /* Misc members not needed in bpf_map_meta_equal() check. */
63 inner_map_meta->ops = inner_map->ops;
64 if (inner_map->ops == &array_map_ops) {
65 inner_map_meta->unpriv_array = inner_map->unpriv_array;
66 container_of(inner_map_meta, struct bpf_array, map)->index_mask =
67 container_of(inner_map, struct bpf_array, map)->index_mask;
68 }
69
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070070 fdput(f);
71 return inner_map_meta;
72}
73
74void bpf_map_meta_free(struct bpf_map *map_meta)
75{
76 kfree(map_meta);
77}
78
79bool bpf_map_meta_equal(const struct bpf_map *meta0,
80 const struct bpf_map *meta1)
81{
82 /* No need to compare ops because it is covered by map_type */
83 return meta0->map_type == meta1->map_type &&
84 meta0->key_size == meta1->key_size &&
85 meta0->value_size == meta1->value_size &&
86 meta0->map_flags == meta1->map_flags &&
87 meta0->max_entries == meta1->max_entries;
88}
89
90void *bpf_map_fd_get_ptr(struct bpf_map *map,
91 struct file *map_file /* not used */,
92 int ufd)
93{
94 struct bpf_map *inner_map;
95 struct fd f;
96
97 f = fdget(ufd);
98 inner_map = __bpf_map_get(f);
99 if (IS_ERR(inner_map))
100 return inner_map;
101
102 if (bpf_map_meta_equal(map->inner_map_meta, inner_map))
103 inner_map = bpf_map_inc(inner_map, false);
104 else
105 inner_map = ERR_PTR(-EINVAL);
106
107 fdput(f);
108 return inner_map;
109}
110
111void bpf_map_fd_put_ptr(void *ptr)
112{
113 /* ptr->ops->map_free() has to go through one
114 * rcu grace period by itself.
115 */
116 bpf_map_put(ptr);
117}
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700118
119u32 bpf_map_fd_sys_lookup_elem(void *ptr)
120{
121 return ((struct bpf_map *)ptr)->id;
122}