From: Eduard Zingerman <eddyz87@gmail.com>
To: Hao Sun <sunhao.th@gmail.com>, bpf@vger.kernel.org
Cc: ppenkov@google.com, willemb@google.com, ast@kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] bpf: Reject variable offset alu on PTR_TO_FLOW_KEYS
Date: Tue, 09 Jan 2024 18:21:01 +0200 [thread overview]
Message-ID: <f84ffb6623d2901624337e88daf73ac639b37a2c.camel@gmail.com> (raw)
In-Reply-To: <20240109153609.10185-1-sunhao.th@gmail.com>
On Tue, 2024-01-09 at 16:36 +0100, Hao Sun wrote:
> For PTR_TO_FLOW_KEYS, check_flow_keys_access() only uses fixed off
> for validation. However, variable offset ptr alu is not prohibited
> for this ptr kind. So the variable offset is not checked.
>
[...]
>
> Fixes: d58e468b1112 ("flow_dissector: implements flow dissector BPF hook")
> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> ---
> kernel/bpf/verifier.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index adbf330d364b..65f598694d55 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -12826,6 +12826,10 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
> }
>
> switch (base_type(ptr_reg->type)) {
> + case PTR_TO_FLOW_KEYS:
> + if (known)
> + break;
> + fallthrough;
> case CONST_PTR_TO_MAP:
> /* smin_val represents the known value */
> if (known && smin_val == 0 && opcode == BPF_ADD)
This change makes sense, could you please add a testcase?
Also, this switch is written to explicitly disallow and implicitly allow
pointer arithmetics, which might be a bit unsafe when new ptr types are added.
Would it make more sense to instead rewrite it to explicitly allow?
E.g. here is what it currently allows / disallows:
| Pointer type | Arithmetics allowed |
|---------------------+---------------------|
| PTR_TO_CTX | yes |
| CONST_PTR_TO_MAP | conditionally |
| PTR_TO_MAP_VALUE | yes |
| PTR_TO_MAP_KEY | yes |
| PTR_TO_STACK | yes |
| PTR_TO_PACKET_META | yes |
| PTR_TO_PACKET | yes |
| PTR_TO_PACKET_END | no |
| PTR_TO_FLOW_KEYS | yes |
| PTR_TO_SOCKET | no |
| PTR_TO_SOCK_COMMON | no |
| PTR_TO_TCP_SOCK | no |
| PTR_TO_TP_BUFFER | yes |
| PTR_TO_XDP_SOCK | no |
| PTR_TO_BTF_ID | yes |
| PTR_TO_MEM | yes |
| PTR_TO_BUF | yes |
| PTR_TO_FUNC | yes |
| CONST_PTR_TO_DYNPTR | yes |
Of these PTR_TO_FUNC and CONST_PTR_TO_DYNPTR (?) should not be allowed
as well, probably (not sure if that could be exploited).
next prev parent reply other threads:[~2024-01-09 16:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-09 15:36 Hao Sun
2024-01-09 16:21 ` Eduard Zingerman [this message]
2024-01-09 16:59 ` Hao Sun
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=f84ffb6623d2901624337e88daf73ac639b37a2c.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ppenkov@google.com \
--cc=sunhao.th@gmail.com \
--cc=willemb@google.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
all inboxes | Powered by JetHome®