BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails
@ 2022-11-20 21:26 Kumar Kartikeya Dwivedi
  2022-11-20 23:43 ` Alexei Starovoitov
  2022-11-20 23:50 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2022-11-20 21:26 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Martin KaFai Lau

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>
---
 kernel/bpf/helpers.c  | 2 --
 kernel/bpf/verifier.c | 6 ++++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 89a95f3d854c..3d4edd314450 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -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;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 5bc9d84d7924..ea36107deee0 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -8878,6 +8878,12 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 				struct btf *ret_btf;
 				u32 ret_btf_id;

+				/* Unlikely, but fail the kfunc call if bpf_global_ma
+				 * is not initialized.
+				 */
+				if (!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;
--
2.38.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails
  2022-11-20 21:26 [PATCH bpf-next] bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails Kumar Kartikeya Dwivedi
@ 2022-11-20 23:43 ` Alexei Starovoitov
  2022-11-20 23:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2022-11-20 23:43 UTC (permalink / raw)
  To: Kumar Kartikeya Dwivedi
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Martin KaFai Lau

On Sun, Nov 20, 2022 at 1:26 PM Kumar Kartikeya Dwivedi
<memxor@gmail.com> wrote:
>
> 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>
> ---
>  kernel/bpf/helpers.c  | 2 --
>  kernel/bpf/verifier.c | 6 ++++++
>  2 files changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 89a95f3d854c..3d4edd314450 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -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;
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 5bc9d84d7924..ea36107deee0 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -8878,6 +8878,12 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>                                 struct btf *ret_btf;
>                                 u32 ret_btf_id;
>
> +                               /* Unlikely, but fail the kfunc call if bpf_global_ma
> +                                * is not initialized.
> +                                */
> +                               if (!bpf_global_ma_set)
> +                                       return -ENOMEM;

I removed the comment and added unlikely().
Comments should describe things that are not obvious from C code.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails
  2022-11-20 21:26 [PATCH bpf-next] bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails Kumar Kartikeya Dwivedi
  2022-11-20 23:43 ` Alexei Starovoitov
@ 2022-11-20 23:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-11-20 23:50 UTC (permalink / raw)
  To: Kumar Kartikeya Dwivedi; +Cc: bpf, ast, andrii, daniel, martin.lau

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Mon, 21 Nov 2022 02:56:10 +0530 you wrote:
> 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>
> 
> [...]

Here is the summary with links:
  - [bpf-next] bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails
    https://git.kernel.org/bpf/bpf-next/c/e181d3f143f7

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-20 23:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-20 21:26 [PATCH bpf-next] bpf: Disallow bpf_obj_new_impl call when bpf_mem_alloc_init fails Kumar Kartikeya Dwivedi
2022-11-20 23:43 ` Alexei Starovoitov
2022-11-20 23:50 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox