Commit 2e3a94aa authored by Brian Vazquez's avatar Brian Vazquez Committed by Daniel Borkmann

bpf: Fix memory leaks in generic update/delete batch ops

Generic update/delete batch ops functions were using __bpf_copy_key
without properly freeing the memory. Handle the memory allocation and
copy_from_user separately.

Fixes: aa2e93b8 ("bpf: Add generic support for update and delete batch ops")
Reported-by: default avatarDan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: default avatarBrian Vazquez <brianvv@google.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200119194040.128369-1-brianvv@google.com
parent a6ed02ca
...@@ -1239,12 +1239,15 @@ int generic_map_delete_batch(struct bpf_map *map, ...@@ -1239,12 +1239,15 @@ int generic_map_delete_batch(struct bpf_map *map,
if (!max_count) if (!max_count)
return 0; return 0;
key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
if (!key)
return -ENOMEM;
for (cp = 0; cp < max_count; cp++) { for (cp = 0; cp < max_count; cp++) {
key = __bpf_copy_key(keys + cp * map->key_size, map->key_size); err = -EFAULT;
if (IS_ERR(key)) { if (copy_from_user(key, keys + cp * map->key_size,
err = PTR_ERR(key); map->key_size))
break; break;
}
if (bpf_map_is_dev_bound(map)) { if (bpf_map_is_dev_bound(map)) {
err = bpf_map_offload_delete_elem(map, key); err = bpf_map_offload_delete_elem(map, key);
...@@ -1264,6 +1267,8 @@ int generic_map_delete_batch(struct bpf_map *map, ...@@ -1264,6 +1267,8 @@ int generic_map_delete_batch(struct bpf_map *map,
} }
if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
err = -EFAULT; err = -EFAULT;
kfree(key);
return err; return err;
} }
...@@ -1294,18 +1299,21 @@ int generic_map_update_batch(struct bpf_map *map, ...@@ -1294,18 +1299,21 @@ int generic_map_update_batch(struct bpf_map *map,
if (!max_count) if (!max_count)
return 0; return 0;
key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
if (!key)
return -ENOMEM;
value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
if (!value) if (!value) {
kfree(key);
return -ENOMEM; return -ENOMEM;
}
for (cp = 0; cp < max_count; cp++) { for (cp = 0; cp < max_count; cp++) {
key = __bpf_copy_key(keys + cp * map->key_size, map->key_size);
if (IS_ERR(key)) {
err = PTR_ERR(key);
break;
}
err = -EFAULT; err = -EFAULT;
if (copy_from_user(value, values + cp * value_size, value_size)) if (copy_from_user(key, keys + cp * map->key_size,
map->key_size) ||
copy_from_user(value, values + cp * value_size, value_size))
break; break;
err = bpf_map_update_value(map, f, key, value, err = bpf_map_update_value(map, f, key, value,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment