Skip to content

Commit 1110f3a

Browse files
Jakub Kicinskiborkmann
authored andcommitted
bpf: add map_alloc_check callback
.map_alloc callbacks contain a number of checks validating user- -provided map attributes against constraints of a particular map type. For offloaded maps we will need to check map attributes without actually allocating any memory on the host. Add a new callback for validating attributes before any memory is allocated. This callback can be selectively implemented by map types for sharing code with offloads, or simply to separate the logical steps of validation and allocation. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent fdde5f3 commit 1110f3a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct bpf_map;
2525
/* map is generic key/value storage optionally accesible by eBPF programs */
2626
struct bpf_map_ops {
2727
/* funcs callable from userspace (via syscall) */
28+
int (*map_alloc_check)(union bpf_attr *attr);
2829
struct bpf_map *(*map_alloc)(union bpf_attr *attr);
2930
void (*map_release)(struct bpf_map *map, struct file *map_file);
3031
void (*map_free)(struct bpf_map *map);

kernel/bpf/syscall.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,25 @@ static int check_uarg_tail_zero(void __user *uaddr,
9696

9797
static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
9898
{
99+
const struct bpf_map_ops *ops;
99100
struct bpf_map *map;
101+
int err;
100102

101-
if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
102-
!bpf_map_types[attr->map_type])
103+
if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
104+
return ERR_PTR(-EINVAL);
105+
ops = bpf_map_types[attr->map_type];
106+
if (!ops)
103107
return ERR_PTR(-EINVAL);
104108

105-
map = bpf_map_types[attr->map_type]->map_alloc(attr);
109+
if (ops->map_alloc_check) {
110+
err = ops->map_alloc_check(attr);
111+
if (err)
112+
return ERR_PTR(err);
113+
}
114+
map = ops->map_alloc(attr);
106115
if (IS_ERR(map))
107116
return map;
108-
map->ops = bpf_map_types[attr->map_type];
117+
map->ops = ops;
109118
map->map_type = attr->map_type;
110119
return map;
111120
}

0 commit comments

Comments
 (0)