]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Sun, 20 Nov 2022 21:26:10 +0000 (02:56 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 20 Nov 2022 23:38:28 +0000 (15:38 -0800)
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>
kernel/bpf/helpers.c
kernel/bpf/verifier.c

index 89a95f3d854c7192497a9f07bdb8fc7953882538..3d4edd31445052e39157fd8620fcedbd9a5013b6 100644 (file)
@@ -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;
index 5bc9d84d79241d8bad774a7a64c3d7cfc1fe193a..eb090af35477bdc3e897744de69c9b331470ddf6 100644 (file)
@@ -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;