From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: kkd@meta.com, Manu Bretelle <chantra@meta.com>,
Eduard Zingerman <eddyz87@gmail.com>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@kernel.org>,
kernel-team@fb.com
Subject: [PATCH bpf v3 1/3] bpf: Suppress warning for non-zero off raw_tp arg NULL check
Date: Fri, 6 Dec 2024 08:10:51 -0800 [thread overview]
Message-ID: <20241206161053.809580-2-memxor@gmail.com> (raw)
In-Reply-To: <20241206161053.809580-1-memxor@gmail.com>
The fixed commit began marking raw_tp arguments as PTR_MAYBE_NULL to
avoid dead code elimination in the verifier, since raw_tp arguments
may actually be NULL at runtime. However, to preserve compatibility,
it simulated the raw_tp accesses as if the NULL marking was not present.
One of the behaviors permitted by this simulation is offset modification
for NULL pointers. Typically, this pattern is rejected by the verifier,
and users make workarounds to prevent the compiler from producing such
patterns. However, now that it is allowed, when the compiler emits such
code, the offset modification is allowed and a PTR_MAYBE_NULL raw_tp arg
with non-zero off can be formed.
The failing example program had the following pseudo-code:
r0 = 1024;
r1 = ...; // r1 = trusted_or_null_(id=1)
r3 = r1; // r3 = trusted_or_null_(id=1) r1 = trusted_or_null_(id=1)
r3 += r0; // r3 = trusted_or_null_(id=1, off=1024)
if r1 == 0 goto pc+X;
At this point, while mark_ptr_or_null_reg will see PTR_MAYBE_NULL and
off == 0 for r1, it will notice non-zero off for r3, and the
WARN_ON_ONCE will fire, as the condition checks excluding register types
do not include raw_tp argument type.
This is a pattern produced by LLVM, therefore it is hard to suppress it
everywhere in BPF programs.
The right "generic" fix for this issue in general, will be permitting
offset modification for PTR_MAYBE_NULL pointers everywhere, and
enforcing that the instruction operand of a conditional jump has the
offset as zero. It's other copies may still have non-zero offset, and
that is fine. But this is more involved and will take longer to
integrate.
If a zero offset pointer is NULL checked, all copies can be marked
non-NULL, while checking non-zero offset PTR_MAYBE_NULL is a no-op.
For now, only make this change for raw_tp arguments, and table the
generic fix for later.
Dereferencing such pointers will still work as the fixed commit allowed
it for raw_tp args.
Fixes: cb4158ce8ec8 ("bpf: Mark raw_tp arguments with PTR_MAYBE_NULL")
Reported-by: Manu Bretelle <chantra@meta.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
kernel/bpf/verifier.c | 38 +++++++++++++++++++++++++++++++-------
1 file changed, 31 insertions(+), 7 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 2fd35465d650..82f40d63ad7b 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -15340,7 +15340,8 @@ static int reg_set_min_max(struct bpf_verifier_env *env,
return err;
}
-static void mark_ptr_or_null_reg(struct bpf_func_state *state,
+static void mark_ptr_or_null_reg(struct bpf_verifier_env *env,
+ struct bpf_func_state *state,
struct bpf_reg_state *reg, u32 id,
bool is_null)
{
@@ -15357,7 +15358,9 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
*/
if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || !tnum_equals_const(reg->var_off, 0)))
return;
- if (!(type_is_ptr_alloc_obj(reg->type) || type_is_non_owning_ref(reg->type)) &&
+ if (!type_is_ptr_alloc_obj(reg->type) &&
+ !type_is_non_owning_ref(reg->type) &&
+ !mask_raw_tp_reg_cond(env, reg) &&
WARN_ON_ONCE(reg->off))
return;
@@ -15390,11 +15393,12 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
/* The logic is similar to find_good_pkt_pointers(), both could eventually
* be folded together at some point.
*/
-static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
+static void mark_ptr_or_null_regs(struct bpf_verifier_env *env,
+ struct bpf_verifier_state *vstate, u32 regno,
bool is_null)
{
struct bpf_func_state *state = vstate->frame[vstate->curframe];
- struct bpf_reg_state *regs = state->regs, *reg;
+ struct bpf_reg_state *regs = state->regs, *reg = ®s[regno];
u32 ref_obj_id = regs[regno].ref_obj_id;
u32 id = regs[regno].id;
@@ -15405,8 +15409,28 @@ static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
*/
WARN_ON_ONCE(release_reference_state(state, id));
+ /* For raw_tp args, compiler can produce code of the following
+ * pattern:
+ * r3 = r1; // r1 = trusted_or_null_(id=1) r3 = trusted_or_null_(id=1)
+ * r3 += 8; // r3 = trusted_or_null_(id=1,off=8)
+ * if r1 == 0 goto pc+N; // r1 = trusted_(id=1)
+ *
+ * But we musn't remove the or_null mark from r3, as it won't be
+ * NULL.
+ *
+ * Only do unmarking of everything sharing id if operand of NULL check
+ * has off = 0.
+ */
+ if (mask_raw_tp_reg_cond(env, reg) && reg->off) {
+ /* We don't reset reg->id back to 0, as it's unexpected
+ * when PTR_MAYBE_NULL is set. Simply avoid performing
+ * a walk for other registers with the same id.
+ */
+ return;
+ }
+
bpf_for_each_reg_in_vstate(vstate, state, reg, ({
- mark_ptr_or_null_reg(state, reg, id, is_null);
+ mark_ptr_or_null_reg(env, state, reg, id, is_null);
}));
}
@@ -15832,9 +15856,9 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
/* Mark all identical registers in each branch as either
* safe or unknown depending R == 0 or R != 0 conditional.
*/
- mark_ptr_or_null_regs(this_branch, insn->dst_reg,
+ mark_ptr_or_null_regs(env, this_branch, insn->dst_reg,
opcode == BPF_JNE);
- mark_ptr_or_null_regs(other_branch, insn->dst_reg,
+ mark_ptr_or_null_regs(env, other_branch, insn->dst_reg,
opcode == BPF_JEQ);
} else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg],
this_branch, other_branch) &&
--
2.43.5
next prev parent reply other threads:[~2024-12-06 16:10 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-06 16:10 [PATCH bpf v3 0/3] Fix for raw_tp PTR_MAYBE_NULL handling Kumar Kartikeya Dwivedi
2024-12-06 16:10 ` Kumar Kartikeya Dwivedi [this message]
2024-12-06 16:10 ` [PATCH bpf v3 2/3] bpf: Do not mark NULL-checked raw_tp arg as scalar Kumar Kartikeya Dwivedi
2024-12-06 17:59 ` Alexei Starovoitov
2024-12-06 18:10 ` Kumar Kartikeya Dwivedi
2024-12-06 18:37 ` Alexei Starovoitov
2024-12-06 19:09 ` Kumar Kartikeya Dwivedi
2024-12-06 19:14 ` Alexei Starovoitov
2024-12-09 23:35 ` Jiri Olsa
2024-12-06 18:15 ` Eduard Zingerman
2024-12-06 18:24 ` Kumar Kartikeya Dwivedi
2024-12-06 18:36 ` Alexei Starovoitov
2024-12-06 19:10 ` Kumar Kartikeya Dwivedi
2024-12-06 19:18 ` Alexei Starovoitov
2024-12-06 16:10 ` [PATCH bpf v3 3/3] selftests/bpf: Add raw_tp tests for PTR_MAYBE_NULL marking 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=20241206161053.809580-2-memxor@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chantra@meta.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
--cc=kkd@meta.com \
--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