From: Eduard Zingerman <eddyz87@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>,
bpf@vger.kernel.org, Andrii Nakryiko <andrii@kernel.org>
Cc: Tao Lyu <tao.lyu@epfl.ch>, Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@kernel.org>,
Mathias Payer <mathias.payer@nebelwelt.net>,
Meng Xu <meng.xu.cs@uwaterloo.ca>,
Sanidhya Kashyap <sanidhya.kashyap@epfl.ch>
Subject: Re: [PATCH bpf-next v2 1/4] bpf: Don't relax STACK_INVALID to STACK_MISC when not allow_ptr_leaks
Date: Wed, 27 Nov 2024 17:09:14 -0800 [thread overview]
Message-ID: <c5f49bb4acabf88539eb28cd8f93446be5f326d8.camel@gmail.com> (raw)
In-Reply-To: <20241127212026.3580542-2-memxor@gmail.com>
On Wed, 2024-11-27 at 13:20 -0800, Kumar Kartikeya Dwivedi wrote:
> Inside mark_stack_slot_misc, we should not upgrade STACK_INVALID to
> STACK_MISC when allow_ptr_leaks is false, since invalid contents
> shouldn't be read unless the program has the relevant capabilities.
> The relaxation only makes sense when env->allow_ptr_leaks is true.
>
> Currently, the condition is inverted (i.e. checking for true instead of
> false), simply invert it to restore correct behavior.
>
> Update error strings of selftests relying on current behavior's verifier
> output.
>
> Fixes: eaf18febd6eb ("bpf: preserve STACK_ZERO slots on partial reg spills")
> Reported-by: Tao Lyu <tao.lyu@epfl.ch>
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
> kernel/bpf/verifier.c | 2 +-
> .../selftests/bpf/progs/verifier_spill_fill.c | 18 +++++++++---------
> 2 files changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 1c4ebb326785..f9791a001e25 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -1209,7 +1209,7 @@ static void mark_stack_slot_misc(struct bpf_verifier_env *env, u8 *stype)
> {
> if (*stype == STACK_ZERO)
> return;
> - if (env->allow_ptr_leaks && *stype == STACK_INVALID)
> + if (!env->allow_ptr_leaks && *stype == STACK_INVALID)
This change makes sense, but it contradicts a few things:
- comment on top of this function;
- commit message for [0]
(there is my ack on that commit, but I have no memory of this place...).
Andrii, do you remember why STACK_INVALID had to be changed to STACK_MISC
for unprivileged case?
Kumar argues that the following program should be rejected when unprivileged:
0: (b7) r2 = 1 ; R2_w=1
1: (bf) r6 = r10 ; R6_w=fp0 R10=fp0
2: (07) r6 += -8 ; R6_w=fp-8
3: (73) *(u8 *)(r6 +0) = r2 ; R2_w=1 R6_w=fp-8 fp-8=???????1
4: (79) r2 = *(u64 *)(r6 +0)
invalid read from stack off -8+1 size 8
(which makes sense). But on master we have:
0: (b7) r2 = 1 ; R2_w=1
1: (bf) r6 = r10 ; R6_w=fp0 R10=fp0
2: (07) r6 += -8 ; R6_w=fp-8
3: (73) *(u8 *)(r6 +0) = r2 ; R2_w=1 R6_w=fp-8 fp-8=mmmmmmm1
4: (79) r2 = *(u64 *)(r6 +0) ; R2_w=scalar() R6_w=fp-8 fp-8=mmmmmmm1
5: (b7) r0 = 0 ; R0_w=0
6: (95) exit
(which makes much less sense).
Also, technically speaking, there is no longer a need in replacing
STACK_INVALID with STACK_MISC at all, only STACK_SPILL should be replaced.
(Because in privileged mode reads from STACK_INVALID are allowed).
[0] eaf18febd6eb ("bpf: preserve STACK_ZERO slots on partial reg spills")
> return;
> *stype = STACK_MISC;
> }
[...]
next prev parent reply other threads:[~2024-11-28 1:09 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-27 21:20 [PATCH bpf-next v2 0/4] Fixes for stack with allow_ptr_leaks Kumar Kartikeya Dwivedi
2024-11-27 21:20 ` [PATCH bpf-next v2 1/4] bpf: Don't relax STACK_INVALID to STACK_MISC when not allow_ptr_leaks Kumar Kartikeya Dwivedi
2024-11-28 1:09 ` Eduard Zingerman [this message]
2024-11-27 21:20 ` [PATCH bpf-next v2 2/4] bpf: Fix narrow scalar spill onto 64-bit spilled scalar slots Kumar Kartikeya Dwivedi
2024-11-28 1:21 ` Eduard Zingerman
2024-11-27 21:20 ` [PATCH bpf-next v2 3/4] selftests/bpf: Add test for reading from STACK_INVALID slots Kumar Kartikeya Dwivedi
2024-11-28 1:50 ` Eduard Zingerman
2024-11-28 1:57 ` Kumar Kartikeya Dwivedi
2024-11-28 2:01 ` Eduard Zingerman
2024-11-28 2:07 ` Kumar Kartikeya Dwivedi
2024-11-27 21:20 ` [PATCH bpf-next v2 4/4] selftests/bpf: Add test for narrow spill into 64-bit spilled scalar Kumar Kartikeya Dwivedi
2024-11-28 1:56 ` 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=c5f49bb4acabf88539eb28cd8f93446be5f326d8.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=martin.lau@kernel.org \
--cc=mathias.payer@nebelwelt.net \
--cc=memxor@gmail.com \
--cc=meng.xu.cs@uwaterloo.ca \
--cc=sanidhya.kashyap@epfl.ch \
--cc=tao.lyu@epfl.ch \
/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