mirror of
https://github.com/torvalds/linux.git
synced 2026-11-06 21:58:01 +01:00
bpf: Fix incorrect handling of user flags by percpu hash map updates
For BPF_MAP_TYPE_PERCPU_HASH and BPF_MAP_TYPE_LRU_PERCPU_HASH maps,
htab_map_check_update_flags() and check_flags() are not considering
the possibility of a combination of (BPF_NOEXIST, BPF_EXIST) flags
with (BPF_F_CPU, BPF_F_ALL_CPUS) flags. This causes the
(BPF_NOEXIST, BPF_EXIST) flags to lose their effect in some cases.
For example, when using (BPF_F_CPU | BPF_EXIST) or
(BPF_F_CPU | BPF_NOEXIST) flag combinations with bpf_map_update_elem()
on a percpu hash map, the BPF_EXIST flag does not prevent new
insertions as expected and BPF_NOEXIST flag does not prevent
modification of existing entries as expected.
This patch fixes the bug by adding proper flag validations and checks.
Before this patch, htab_map_check_update_flags() rejected
(BPF_F_ALL_CPUS | BPF_EXIST) and (BPF_F_ALL_CPUS | BPF_NOEXIST) with
-EINVAL. After the patch those combinations are accepted.
This patch also starts returning -EINVAL for (BPF_EXIST | BPF_NOEXIST),
with or without BPF_F_CPU, which was previously accepted.
Fixes: c6936161fd ("bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_hash and lru_percpu_hash maps")
Signed-off-by: Masoud Aghasi <maghasi@disroot.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://patch.msgid.link/20261008120734.1565972-3-maghasi@disroot.org
This commit is contained in:
parent
a898ff0f3e
commit
4a9b25fe8d
|
|
@ -1367,11 +1367,11 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
|
|||
static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
|
||||
u64 map_flags)
|
||||
{
|
||||
if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
|
||||
if (l_old && (map_flags & BPF_NOEXIST))
|
||||
/* elem already exists */
|
||||
return -EEXIST;
|
||||
|
||||
if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
|
||||
if (!l_old && (map_flags & BPF_EXIST))
|
||||
/* elem doesn't exist, cannot update it */
|
||||
return -ENOENT;
|
||||
|
||||
|
|
@ -1560,9 +1560,11 @@ static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value
|
|||
|
||||
static int htab_map_check_update_flags(bool onallcpus, u64 map_flags)
|
||||
{
|
||||
if (unlikely((map_flags & BPF_EXIST_FLAGS) == BPF_EXIST_FLAGS))
|
||||
return -EINVAL;
|
||||
if (unlikely(!onallcpus && map_flags > BPF_EXIST))
|
||||
return -EINVAL;
|
||||
if (unlikely(onallcpus && ((map_flags & BPF_F_LOCK) || (u32)map_flags > BPF_F_ALL_CPUS)))
|
||||
if (unlikely(onallcpus && ((u32)map_flags & ~(BPF_EXIST_FLAGS | BPF_CPU_FLAGS))))
|
||||
return -EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1661,7 +1663,7 @@ static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
|
|||
* to remove older elem from htab and this removal
|
||||
* operation will need a bucket lock.
|
||||
*/
|
||||
if (map_flags != BPF_EXIST) {
|
||||
if (!(map_flags & BPF_EXIST)) {
|
||||
l_new = prealloc_lru_pop(htab, key, hash);
|
||||
if (!l_new)
|
||||
return -ENOMEM;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user