[PATCH AUTOSEL 5.10 19/44] bpf: Set uattr->batch.count as zero before batched update or deletion

From: Sasha Levin
Date: Tue Jan 16 2024 - 16:43:37 EST


From: Hou Tao <houtao1@xxxxxxxxxx>

[ Upstream commit 06e5c999f10269a532304e89a6adb2fbfeb0593c ]

generic_map_{delete,update}_batch() doesn't set uattr->batch.count as
zero before it tries to allocate memory for key. If the memory
allocation fails, the value of uattr->batch.count will be incorrect.

Fix it by setting uattr->batch.count as zero beore batched update or
deletion.

Signed-off-by: Hou Tao <houtao1@xxxxxxxxxx>
Link: https://lore.kernel.org/r/20231208102355.2628918-6-houtao@xxxxxxxxxxxxxxx
Signed-off-by: Alexei Starovoitov <ast@xxxxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
kernel/bpf/syscall.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index aaad2dce2be6..16affa09db5c 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -1285,6 +1285,9 @@ int generic_map_delete_batch(struct bpf_map *map,
if (!max_count)
return 0;

+ if (put_user(0, &uattr->batch.count))
+ return -EFAULT;
+
key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
if (!key)
return -ENOMEM;
@@ -1343,6 +1346,9 @@ int generic_map_update_batch(struct bpf_map *map,
if (!max_count)
return 0;

+ if (put_user(0, &uattr->batch.count))
+ return -EFAULT;
+
key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
if (!key)
return -ENOMEM;
--
2.43.0