Message ID | 20221121100521.56601-1-xiangxia.m.yue@gmail.com (mailing list archive) |
---|---|
State | Changes Requested |
Delegated to: | BPF |
Headers | show |
Series | [net-next] bpf: avoid the multi checking | expand |
On 11/21/22 11:05 AM, xiangxia.m.yue@gmail.com wrote: > From: Tonghao Zhang <xiangxia.m.yue@gmail.com> > > .map_alloc_check checked bpf_attr::max_entries, and if bpf_attr::max_entries > == 0, return -EINVAL. bpf_htab::n_buckets will not be 0, while -E2BIG is not > appropriate. > > Cc: Alexei Starovoitov <ast@kernel.org> > Cc: Daniel Borkmann <daniel@iogearbox.net> > Cc: Andrii Nakryiko <andrii@kernel.org> > Cc: Martin KaFai Lau <martin.lau@linux.dev> > Cc: Song Liu <song@kernel.org> > Cc: Yonghong Song <yhs@fb.com> > Cc: John Fastabend <john.fastabend@gmail.com> > Cc: KP Singh <kpsingh@kernel.org> > Cc: Stanislav Fomichev <sdf@google.com> > Cc: Hao Luo <haoluo@google.com> > Cc: Jiri Olsa <jolsa@kernel.org> > Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com> Pls Cc bpf@vger.kernel.org list and $subj line should target bpf-next. > --- > kernel/bpf/hashtab.c | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c > index 50d254cd0709..22855d6ff6d3 100644 > --- a/kernel/bpf/hashtab.c > +++ b/kernel/bpf/hashtab.c > @@ -500,9 +500,10 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) > htab->elem_size += round_up(htab->map.value_size, 8); > > err = -E2BIG; > - /* prevent zero size kmalloc and check for u32 overflow */ > - if (htab->n_buckets == 0 || > - htab->n_buckets > U32_MAX / sizeof(struct bucket)) > + /* avoid zero size and u32 overflow kmalloc. > + * bpf_attr::max_entries checked in .map_alloc_check(). > + */ > + if (htab->n_buckets > U32_MAX / sizeof(struct bucket)) This looks buggy to remove it, this is to guard against the previous roundup_pow_of_two() on htab->n_buckets. > goto free_htab; > > err = -ENOMEM; >
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 50d254cd0709..22855d6ff6d3 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -500,9 +500,10 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) htab->elem_size += round_up(htab->map.value_size, 8); err = -E2BIG; - /* prevent zero size kmalloc and check for u32 overflow */ - if (htab->n_buckets == 0 || - htab->n_buckets > U32_MAX / sizeof(struct bucket)) + /* avoid zero size and u32 overflow kmalloc. + * bpf_attr::max_entries checked in .map_alloc_check(). + */ + if (htab->n_buckets > U32_MAX / sizeof(struct bucket)) goto free_htab; err = -ENOMEM;