Skip to content

Commit

Permalink
bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails
Browse files Browse the repository at this point in the history
In the unlikely event that bpf_global_ma is not correctly initialized,
instead of checking the boolean everytime bpf_obj_new_impl is called,
simply check it while loading the program and return an error if
bpf_global_ma_set is false.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20221120212610.2361700-1-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Kumar Kartikeya Dwivedi authored and Alexei Starovoitov committed Nov 20, 2022
1 parent efc1970 commit e181d3f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 0 additions & 2 deletions kernel/bpf/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1760,8 +1760,6 @@ void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign)
u64 size = local_type_id__k;
void *p;

if (unlikely(!bpf_global_ma_set))
return NULL;
p = bpf_mem_alloc(&bpf_global_ma, size);
if (!p)
return NULL;
Expand Down
3 changes: 3 additions & 0 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -8878,6 +8878,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
struct btf *ret_btf;
u32 ret_btf_id;

if (unlikely(!bpf_global_ma_set))
return -ENOMEM;

if (((u64)(u32)meta.arg_constant.value) != meta.arg_constant.value) {
verbose(env, "local type ID argument must be in range [0, U32_MAX]\n");
return -EINVAL;
Expand Down

0 comments on commit e181d3f

Please sign in to comment.