From: Eduard Zingerman <eddyz87@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org,
memxor@gmail.com, kernel-team@fb.com
Subject: Re: [PATCH v2 bpf-next 2/4] bpf: Track delta between "linked" registers.
Date: Tue, 11 Jun 2024 13:09:47 -0700 [thread overview]
Message-ID: <d454304daffd5fcd8b442f2e29aa493c426dc991.camel@gmail.com> (raw)
In-Reply-To: <20240610230849.80820-3-alexei.starovoitov@gmail.com>
On Mon, 2024-06-10 at 16:08 -0700, Alexei Starovoitov wrote:
[...]
> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
> index 50aa87f8d77f..2b54e25d2364 100644
> --- a/include/linux/bpf_verifier.h
> +++ b/include/linux/bpf_verifier.h
> @@ -73,7 +73,10 @@ enum bpf_iter_state {
> struct bpf_reg_state {
> /* Ordering of fields matters. See states_equal() */
> enum bpf_reg_type type;
> - /* Fixed part of pointer offset, pointer types only */
> + /*
> + * Fixed part of pointer offset, pointer types only.
> + * Or constant delta between "linked" scalars with the same ID.
> + */
> s32 off;
After thinking about this some more I came to conclusion that ->off
has to be checked for scalar registers in regsafe().
Otherwise the following test is marked as safe:
char buf[10] SEC(".data.buf");
SEC("socket")
__failure
__flag(BPF_F_TEST_STATE_FREQ)
__naked void check_add_const_regsafe_off(void)
{
asm volatile (
"r8 = %[buf];"
"call %[bpf_ktime_get_ns];"
"r6 = r0;"
"call %[bpf_ktime_get_ns];"
"r7 = r0;"
"call %[bpf_ktime_get_ns];"
"r1 = r0;" /* same ids for r1 and r0 */
"if r6 > r7 goto 1f;" /* this jump can't be predicted */
"r1 += 1;" /* r1.off == +1 */
"goto 2f;"
"1: r1 += 100;" /* r1.off == +100 */
"goto +0;" /* force checkpoint, must verify r1.off in regsafe() here */
"2: if r0 > 8 goto 3f;" /* r0 range [0,8], r1 range either [1,9] or [100,108]*/
"r8 += r1;"
"*(u8 *)(r8 +0) = r0;" /* potentially unsafe, buf size is 10 */
"3: exit;"
:
: __imm(bpf_ktime_get_ns),
__imm_ptr(buf)
: __clobber_common);
}
Sorry for missing this yesterday.
Something like below is necessary.
(To trigger ((rold->id & BPF_ADD_CONST) != (rcur->id & BPF_ADD_CONST))
a variation of the test where r1 += 1 is not done is necessary).
---
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index ad11e5441860..70e44fa4f765 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -16797,6 +16797,10 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
}
if (!rold->precise && exact == NOT_EXACT)
return true;
+ if ((rold->id & BPF_ADD_CONST) != (rcur->id & BPF_ADD_CONST))
+ return false;
+ if ((rold->id & BPF_ADD_CONST) && (rold->off != rcur->off))
+ return false;
/* Why check_ids() for scalar registers?
*
* Consider the following BPF code:
next prev parent reply other threads:[~2024-06-11 20:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-10 23:08 [PATCH v2 bpf-next 0/4] bpf: Track delta between "linked" registers Alexei Starovoitov
2024-06-10 23:08 ` [PATCH v2 bpf-next 1/4] bpf: Relax tuple len requirement for sk helpers Alexei Starovoitov
2024-06-11 20:31 ` Eduard Zingerman
2024-06-10 23:08 ` [PATCH v2 bpf-next 2/4] bpf: Track delta between "linked" registers Alexei Starovoitov
2024-06-11 20:09 ` Eduard Zingerman [this message]
2024-06-12 15:52 ` Alexei Starovoitov
2024-06-10 23:08 ` [PATCH v2 bpf-next 3/4] bpf: Support can_loop/cond_break on big endian Alexei Starovoitov
2024-06-10 23:08 ` [PATCH v2 bpf-next 4/4] selftests/bpf: Add tests for add_const Alexei Starovoitov
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=d454304daffd5fcd8b442f2e29aa493c426dc991.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox