BPF List
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@kernel.org>
Subject: [PATCH bpf-next v1 1/2] bpf: Disallow calling bpf_obj_new_impl on bpf_mem_alloc_init failure
Date: Sat, 19 Nov 2022 00:29:37 +0530	[thread overview]
Message-ID: <20221118185938.2139616-2-memxor@gmail.com> (raw)
In-Reply-To: <20221118185938.2139616-1-memxor@gmail.com>

Instead of checking bpf_global_ma_set at runtime on each allocation
inside bpf_obj_new_impl, simply disallow calling the kfunc in case
bpf_global_ma initialization failed during program verification.

The error generated when bpf_global_ma initialization fails:
...
21: (18) r1 = 0x7                     ; R1_w=7
23: (b7) r2 = 0                       ; R2_w=0
24: (85) call bpf_obj_new_impl#36585
bpf_global_ma initialization failed, can't call bpf_obj_new_impl
calling kernel function bpf_obj_new_impl is not allowed

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 | 13 ++++++++++++-
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 212e791d7452..bc02f55adc1f 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 195d24316750..f04bee7934a8 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -8746,6 +8746,17 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 	return 0;
 }
 
+static bool is_kfunc_disabled(struct bpf_verifier_env *env, const struct btf *btf, u32 func_id)
+{
+	if (btf != btf_vmlinux)
+		return false;
+	if (!bpf_global_ma_set && func_id == special_kfunc_list[KF_bpf_obj_new_impl]) {
+		verbose(env, "bpf_global_ma initialization failed, can't call bpf_obj_new_impl\n");
+		return true;
+	}
+	return false;
+}
+
 static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 			    int *insn_idx_p)
 {
@@ -8773,7 +8784,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	func_proto = btf_type_by_id(desc_btf, func->type);
 
 	kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id);
-	if (!kfunc_flags) {
+	if (!kfunc_flags || is_kfunc_disabled(env, desc_btf, func_id)) {
 		verbose(env, "calling kernel function %s is not allowed\n",
 			func_name);
 		return -EACCES;
-- 
2.38.1


  reply	other threads:[~2022-11-18 19:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-18 18:59 [PATCH bpf-next v1 0/2] Follow ups for bpf-list set Kumar Kartikeya Dwivedi
2022-11-18 18:59 ` Kumar Kartikeya Dwivedi [this message]
2022-11-18 20:17   ` [PATCH bpf-next v1 1/2] bpf: Disallow calling bpf_obj_new_impl on bpf_mem_alloc_init failure Alexei Starovoitov
2022-11-20 20:46     ` Kumar Kartikeya Dwivedi
2022-11-20 22:29       ` Alexei Starovoitov
2022-11-18 18:59 ` [PATCH bpf-next v1 2/2] selftests/bpf: Skip spin lock failure test on s390x Kumar Kartikeya Dwivedi
2022-11-18 20:20 ` [PATCH bpf-next v1 0/2] Follow ups for bpf-list set patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221118185938.2139616-2-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=martin.lau@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox