All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>
Subject: [PATCH bpf-next v1 2/6] bpf: Fix PTR_TO_BTF_ID var_off check
Date: Tue,  1 Mar 2022 12:27:41 +0530	[thread overview]
Message-ID: <20220301065745.1634848-3-memxor@gmail.com> (raw)
In-Reply-To: <20220301065745.1634848-1-memxor@gmail.com>

When kfunc support was added, check_ctx_reg was called for PTR_TO_CTX
register, but no offset checks were made for PTR_TO_BTF_ID. Only
reg->off was taken into account by btf_struct_ids_match, which protected
against type mismatch due to non-zero reg->off, but when reg->off was
zero, a user could set the variable offset of the register and allow it
to be passed to kfunc, leading to bad pointer being passed into the
kernel.

Fix this by reusing the extracted helper check_func_arg_reg_off from
previous commit, and make one call before checking all supported
register types. Since the list is maintained, any future changes will be
taken into account by updating check_func_arg_reg_off. This function
prevents non-zero var_off to be set for PTR_TO_BTF_ID, but still allows
a fixed non-zero reg->off, which is needed for type matching to work
correctly when using pointer arithmetic.

ARG_DONTCARE is passed as arg_type, since kfunc doesn't support
accepting a ARG_PTR_TO_ALLOC_MEM without relying on size of parameter
type from BTF (in case of pointer), or using a mem, len pair. The
forcing of offset check for ARG_PTR_TO_ALLOC_MEM is done because ringbuf
helpers obtain the size from the header located at the beginning of the
memory region, hence any changes to the original pointer shouldn't be
allowed. In case of kfunc, size is always known, either at verification
time, or using the length parameter, hence this forcing is not required.

Since this check will happen once already for PTR_TO_CTX, remove the
check_ptr_off_reg call inside its block.

Cc: Martin KaFai Lau <kafai@fb.com>
Fixes: e6ac2450d6de ("bpf: Support bpf program calling kernel function")
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 kernel/bpf/btf.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index b472cf0c8fdb..7f6a0ae5028b 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -5726,7 +5726,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
 	const char *func_name, *ref_tname;
 	const struct btf_type *t, *ref_t;
 	const struct btf_param *args;
-	int ref_regno = 0;
+	int ref_regno = 0, ret;
 	bool rel = false;
 
 	t = btf_type_by_id(btf, func_id);
@@ -5776,6 +5776,11 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
 
 		ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
 		ref_tname = btf_name_by_offset(btf, ref_t->name_off);
+
+		ret = check_func_arg_reg_off(env, reg, regno, ARG_DONTCARE);
+		if (ret < 0)
+			return ret;
+
 		if (btf_get_prog_ctx_type(log, btf, t,
 					  env->prog->type, i)) {
 			/* If function expects ctx type in BTF check that caller
@@ -5787,8 +5792,6 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
 					i, btf_type_str(t));
 				return -EINVAL;
 			}
-			if (check_ptr_off_reg(env, reg, regno))
-				return -EINVAL;
 		} else if (is_kfunc && (reg->type == PTR_TO_BTF_ID ||
 			   (reg2btf_ids[base_type(reg->type)] && !type_flag(reg->type)))) {
 			const struct btf_type *reg_ref_t;
-- 
2.35.1


  parent reply	other threads:[~2022-03-01  6:57 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-01  6:57 [PATCH bpf-next v1 0/6] Fixes for bad PTR_TO_BTF_ID offset Kumar Kartikeya Dwivedi
2022-03-01  6:57 ` [PATCH bpf-next v1 1/6] bpf: Add check_func_arg_reg_off function Kumar Kartikeya Dwivedi
2022-03-01  6:57 ` Kumar Kartikeya Dwivedi [this message]
2022-03-01  6:57 ` [PATCH bpf-next v1 3/6] bpf: Disallow negative offset in check_ptr_off_reg Kumar Kartikeya Dwivedi
2022-03-01  6:57 ` [PATCH bpf-next v1 4/6] bpf: Harden register offset checks for release kfunc Kumar Kartikeya Dwivedi
2022-03-02  3:20   ` Martin KaFai Lau
2022-03-02  9:42     ` Kumar Kartikeya Dwivedi
2022-03-02 21:56       ` Martin KaFai Lau
2022-03-02 22:30         ` Kumar Kartikeya Dwivedi
2022-03-02 22:44           ` Alexei Starovoitov
2022-03-02 23:00             ` Kumar Kartikeya Dwivedi
2022-03-02 23:17               ` Alexei Starovoitov
2022-03-02 23:29                 ` Kumar Kartikeya Dwivedi
2022-03-02 23:39                   ` Alexei Starovoitov
2022-03-02 23:31             ` Martin KaFai Lau
2022-03-01  6:57 ` [PATCH bpf-next v1 5/6] selftests/bpf: Update tests for new errstr Kumar Kartikeya Dwivedi
2022-03-02 22:45   ` Alexei Starovoitov
2022-03-02 23:02     ` Kumar Kartikeya Dwivedi
2022-03-01  6:57 ` [PATCH bpf-next v1 6/6] selftests/bpf: Add tests for kfunc register offset checks Kumar Kartikeya Dwivedi
2022-03-01 11:40   ` kernel test robot
2022-03-01 11:57     ` Kumar Kartikeya Dwivedi
2022-03-01 11:57       ` Kumar Kartikeya Dwivedi
2022-03-02 22:47       ` Alexei Starovoitov
2022-03-02 22:47         ` Alexei Starovoitov
2022-03-02 23:14         ` Kumar Kartikeya Dwivedi
2022-03-02 23:14           ` Kumar Kartikeya Dwivedi
2022-03-02 23:20           ` Alexei Starovoitov
2022-03-02 23:20             ` Alexei Starovoitov
2022-03-02 23:26           ` Nathan Chancellor
2022-03-02 23:26             ` Nathan Chancellor
2022-03-02 23:37             ` Kumar Kartikeya Dwivedi
2022-03-02 23:37               ` Kumar Kartikeya Dwivedi

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=20220301065745.1634848-3-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=kafai@fb.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.