BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org
Cc: daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com,
	yonghong.song@linux.dev, eddyz87@gmail.com,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>
Subject: [PATCH bpf-next v1 2/8] bpf: rdonly_untrusted_mem for btf id walk pointer leafs
Date: Wed,  2 Jul 2025 15:42:03 -0700	[thread overview]
Message-ID: <20250702224209.3300396-3-eddyz87@gmail.com> (raw)
In-Reply-To: <20250702224209.3300396-1-eddyz87@gmail.com>

When processing a load from a PTR_TO_BTF_ID, the verifier calculates
the type of the loaded structure field based on the load offset.
For example, given the following types:

  struct foo {
    struct foo *a;
    int *b;
  } *p;

The verifier would calculate the type of `p->a` as a pointer to
`struct foo`. However, the type of `p->b` is currently calculated as a
SCALAR_VALUE.

This commit updates the logic for processing PTR_TO_BTF_ID to instead
calculate the type of p->b as PTR_TO_MEM|MEM_RDONLY|PTR_UNTRUSTED.
This change allows further dereferencing of such pointers (using probe
memory instructions).

Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 kernel/bpf/btf.c                                     | 6 ++++++
 kernel/bpf/verifier.c                                | 5 +++++
 tools/testing/selftests/bpf/prog_tests/linked_list.c | 2 +-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 05fd64a371af..b3c8a95d38fb 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -6915,6 +6915,7 @@ enum bpf_struct_walk_result {
 	/* < 0 error */
 	WALK_SCALAR = 0,
 	WALK_PTR,
+	WALK_PTR_UNTRUSTED,
 	WALK_STRUCT,
 };
 
@@ -7156,6 +7157,8 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,
 					*field_name = mname;
 				return WALK_PTR;
 			}
+
+			return WALK_PTR_UNTRUSTED;
 		}
 
 		/* Allow more flexible access within an int as long as
@@ -7228,6 +7231,9 @@ int btf_struct_access(struct bpf_verifier_log *log,
 			*next_btf_id = id;
 			*flag = tmp_flag;
 			return PTR_TO_BTF_ID;
+		case WALK_PTR_UNTRUSTED:
+			*flag = MEM_RDONLY | PTR_UNTRUSTED;
+			return PTR_TO_MEM;
 		case WALK_SCALAR:
 			return SCALAR_VALUE;
 		case WALK_STRUCT:
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index b6d26e8bd767..cd2344e50db8 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -2813,6 +2813,11 @@ static int mark_btf_ld_reg(struct bpf_verifier_env *env,
 		if (type_may_be_null(flag))
 			regs[regno].id = ++env->id_gen;
 		return 0;
+	case PTR_TO_MEM:
+		mark_reg_known_zero(env, regs, regno);
+		regs[regno].type = PTR_TO_MEM | flag;
+		regs[regno].mem_size = 0;
+		return 0;
 	default:
 		verifier_bug(env, "unexpected reg_type %d in %s\n", reg_type, __func__);
 		return -EFAULT;
diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c b/tools/testing/selftests/bpf/prog_tests/linked_list.c
index 5266c7022863..14c5a7ef0e87 100644
--- a/tools/testing/selftests/bpf/prog_tests/linked_list.c
+++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c
@@ -72,7 +72,7 @@ static struct {
 	{ "new_null_ret", "R0 invalid mem access 'ptr_or_null_'" },
 	{ "obj_new_acq", "Unreleased reference id=" },
 	{ "use_after_drop", "invalid mem access 'scalar'" },
-	{ "ptr_walk_scalar", "type=scalar expected=percpu_ptr_" },
+	{ "ptr_walk_scalar", "type=rdonly_untrusted_mem expected=percpu_ptr_" },
 	{ "direct_read_lock", "direct access to bpf_spin_lock is disallowed" },
 	{ "direct_write_lock", "direct access to bpf_spin_lock is disallowed" },
 	{ "direct_read_head", "direct access to bpf_list_head is disallowed" },
-- 
2.47.1


  parent reply	other threads:[~2025-07-02 22:42 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-02 22:42 [PATCH bpf-next v1 0/8] bpf: additional use-cases for untrusted PTR_TO_MEM Eduard Zingerman
2025-07-02 22:42 ` [PATCH bpf-next v1 1/8] bpf: make makr_btf_ld_reg return error for unexpected reg types Eduard Zingerman
2025-07-04 17:20   ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` Eduard Zingerman [this message]
2025-07-04 17:27   ` [PATCH bpf-next v1 2/8] bpf: rdonly_untrusted_mem for btf id walk pointer leafs Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 3/8] selftests/bpf: ptr_to_btf_id struct walk ending with primitive pointer Eduard Zingerman
2025-07-04 17:34   ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 4/8] bpf: attribute __arg_untrusted for global function parameters Eduard Zingerman
2025-07-03  3:18   ` Alexei Starovoitov
2025-07-03 21:25     ` Eduard Zingerman
2025-07-04 18:17       ` Alexei Starovoitov
2025-07-04 18:03   ` Kumar Kartikeya Dwivedi
2025-07-04 18:28     ` Eduard Zingerman
2025-07-04 18:33       ` Eduard Zingerman
2025-07-04 18:50         ` Kumar Kartikeya Dwivedi
2025-07-04 19:07           ` Eduard Zingerman
2025-07-04 19:15             ` Kumar Kartikeya Dwivedi
2025-07-04 19:23               ` Eduard Zingerman
2025-07-04 20:05                 ` Alexei Starovoitov
2025-07-04 20:20                   ` Kumar Kartikeya Dwivedi
2025-07-04 20:34                     ` Eduard Zingerman
2025-07-04 20:47                       ` Alexei Starovoitov
2025-07-02 22:42 ` [PATCH bpf-next v1 5/8] libbpf: __arg_untrusted in bpf_helpers.h Eduard Zingerman
2025-07-04 18:04   ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 6/8] selftests/bpf: test cases for __arg_untrusted Eduard Zingerman
2025-07-04 18:05   ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 7/8] bpf: support for void/primitive __arg_untrusted global func params Eduard Zingerman
2025-07-03  3:20   ` Alexei Starovoitov
2025-07-03 21:49     ` Eduard Zingerman
2025-07-04 18:11       ` Alexei Starovoitov
2025-07-04 18:09   ` Kumar Kartikeya Dwivedi
2025-07-02 22:42 ` [PATCH bpf-next v1 8/8] selftests/bpf: tests for __arg_untrusted void * " Eduard Zingerman
2025-07-04 18:12   ` Kumar Kartikeya Dwivedi
2025-07-04 18:35     ` Eduard Zingerman

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=20250702224209.3300396-3-eddyz87@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@linux.dev \
    --cc=yonghong.song@linux.dev \
    /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