Skip to content

Commit fd8db07

Browse files
florianlborkmann
authored andcommitted
bpf, devmap: Add .map_alloc_check
Use the .map_allock_check callback to perform allocation checks before allocating memory for the devmap. Signed-off-by: Florian Lehner <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 69716e4 commit fd8db07

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

kernel/bpf/devmap.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static inline struct hlist_head *dev_map_index_hash(struct bpf_dtab *dtab,
107107
return &dtab->dev_index_head[idx & (dtab->n_buckets - 1)];
108108
}
109109

110-
static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
110+
static int dev_map_alloc_check(union bpf_attr *attr)
111111
{
112112
u32 valsize = attr->value_size;
113113

@@ -121,23 +121,28 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
121121
attr->map_flags & ~DEV_CREATE_FLAG_MASK)
122122
return -EINVAL;
123123

124+
if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
125+
/* Hash table size must be power of 2; roundup_pow_of_two()
126+
* can overflow into UB on 32-bit arches
127+
*/
128+
if (attr->max_entries > 1UL << 31)
129+
return -EINVAL;
130+
}
131+
132+
return 0;
133+
}
134+
135+
static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
136+
{
124137
/* Lookup returns a pointer straight to dev->ifindex, so make sure the
125138
* verifier prevents writes from the BPF side
126139
*/
127140
attr->map_flags |= BPF_F_RDONLY_PROG;
128-
129-
130141
bpf_map_init_from_attr(&dtab->map, attr);
131142

132143
if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
133-
/* hash table size must be power of 2; roundup_pow_of_two() can
134-
* overflow into UB on 32-bit arches, so check that first
135-
*/
136-
if (dtab->map.max_entries > 1UL << 31)
137-
return -EINVAL;
138-
144+
/* Hash table size must be power of 2 */
139145
dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries);
140-
141146
dtab->dev_index_head = dev_map_create_hash(dtab->n_buckets,
142147
dtab->map.numa_node);
143148
if (!dtab->dev_index_head)
@@ -1040,6 +1045,7 @@ static u64 dev_map_mem_usage(const struct bpf_map *map)
10401045
BTF_ID_LIST_SINGLE(dev_map_btf_ids, struct, bpf_dtab)
10411046
const struct bpf_map_ops dev_map_ops = {
10421047
.map_meta_equal = bpf_map_meta_equal,
1048+
.map_alloc_check = dev_map_alloc_check,
10431049
.map_alloc = dev_map_alloc,
10441050
.map_free = dev_map_free,
10451051
.map_get_next_key = dev_map_get_next_key,
@@ -1054,6 +1060,7 @@ const struct bpf_map_ops dev_map_ops = {
10541060

10551061
const struct bpf_map_ops dev_map_hash_ops = {
10561062
.map_meta_equal = bpf_map_meta_equal,
1063+
.map_alloc_check = dev_map_alloc_check,
10571064
.map_alloc = dev_map_alloc,
10581065
.map_free = dev_map_free,
10591066
.map_get_next_key = dev_map_hash_get_next_key,

0 commit comments

Comments
 (0)