Re: [PATCH] kernel/bpf: htab_map_alloc() exit by free_map_locked logic issue

From: Stanislav Fomichev
Date: Sat Sep 10 2022 - 16:30:06 EST


On Fri, Sep 9, 2022 at 7:07 PM <eadavis@xxxxxxxx> wrote:
>
> From: eadivs <eadivs@xxxxxxxx>
>
> syzbot is reporting WARNING: ODEBUG bug in htab_map_alloc(), the
> loop exits without reaching length HASHTAB_MAP_LOCK_COUNT, and
> the loop continues HASHTAB_MAP_LOCK_COUNT times in label
> free_map_locked.

Please use [PATCH bpf] vs [PATCH bpf-next] in subject to indicate
which tree you're targeting.
Also, it seems your email hasn't reached the mailing list for some reason.

Are you sure that the issue is due to HASHTAB_MAP_LOCK_COUNT? The code
seems fine as is; unconditionally calling free on NULL shouldn't be an
issue.

htab_map_alloc+0xc76/0x1620 kernel/bpf/hashtab.c:632

Which, if I'm looking at the function is:
bpf_map_area_free(htab);

?

> Link: https://syzkaller.appspot.com/bug?extid=5d1da78b375c3b5e6c2b
> Reported-by: syzbot+5d1da78b375c3b5e6c2b@xxxxxxxxxxxxxxxxxxxxxxxxx
> Signed-off-by: eadivs <eadivs@xxxxxxxx>
> ---
> kernel/bpf/hashtab.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index 65877967f414..f5381e1c00a6 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -473,7 +473,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
> bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
> bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
> struct bpf_htab *htab;
> - int err, i;
> + int err, i, j = HASHTAB_MAP_LOCK_COUNT;
>
> htab = kzalloc(sizeof(*htab), GFP_USER | __GFP_ACCOUNT);
> if (!htab)
> @@ -523,8 +523,10 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
> sizeof(int),
> sizeof(int),
> GFP_USER);
> - if (!htab->map_locked[i])
> + if (!htab->map_locked[i]) {
> + j = i;
> goto free_map_locked;
> + }
> }
>
> if (htab->map.map_flags & BPF_F_ZERO_SEED)
> @@ -554,7 +556,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
> free_prealloc:
> prealloc_destroy(htab);
> free_map_locked:
> - for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++)
> + for (i = 0; i < j; i++)
> free_percpu(htab->map_locked[i]);
> bpf_map_area_free(htab->buckets);
> free_htab:
> --
> 2.37.2
>