From: Eduard Zingerman <eddyz87@gmail.com>
To: Yafang Shao <laoar.shao@gmail.com>,
ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com,
haoluo@google.com, jolsa@kernel.org
Cc: bpf@vger.kernel.org,
Alexei Starovoitov <alexei.starovoitov@gmail.com>,
stable@vger.kernel.org
Subject: Re: [PATCH bpf-next 1/2] bpf: Fix issue in verifying allow_ptr_leaks
Date: Mon, 21 Aug 2023 15:33:41 +0300 [thread overview]
Message-ID: <6e048371123eae0f89b58581a043b1a3de36f7f3.camel@gmail.com> (raw)
In-Reply-To: <20230818083920.3771-2-laoar.shao@gmail.com>
On Fri, 2023-08-18 at 08:39 +0000, Yafang Shao wrote:
> After we converted the capabilities of our networking-bpf program from
> cap_sys_admin to cap_net_admin+cap_bpf, our networking-bpf program
> failed to start. Because it failed the bpf verifier, and the error log
> is "R3 pointer comparison prohibited".
>
> A simple reproducer as follows,
>
> SEC("cls-ingress")
> int ingress(struct __sk_buff *skb)
> {
> struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr);
>
> if ((long)(iph + 1) > (long)skb->data_end)
> return TC_ACT_STOLEN;
> return TC_ACT_OK;
> }
>
> Per discussion with Yonghong and Alexei [1], comparison of two packet
> pointers is not a pointer leak. This patch fixes it.
>
> Our local kernel is 6.1.y and we expect this fix to be backported to
> 6.1.y, so stable is CCed.
>
> [1]. https://lore.kernel.org/bpf/CAADnVQ+Nmspr7Si+pxWn8zkE7hX-7s93ugwC+94aXSy4uQ9vBg@mail.gmail.com/
>
> Suggested-by: Yonghong Song <yonghong.song@linux.dev>
> Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> Cc: stable@vger.kernel.org
> ---
> kernel/bpf/verifier.c | 17 +++++++++--------
> 1 file changed, 9 insertions(+), 8 deletions(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 4ccca1f..b6b60cd 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -14047,6 +14047,12 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
> return -EINVAL;
> }
>
> + /* check src2 operand */
> + err = check_reg_arg(env, insn->dst_reg, SRC_OP);
> + if (err)
> + return err;
> +
> + dst_reg = ®s[insn->dst_reg];
> if (BPF_SRC(insn->code) == BPF_X) {
> if (insn->imm != 0) {
> verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
> @@ -14058,12 +14064,13 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
> if (err)
> return err;
>
> - if (is_pointer_value(env, insn->src_reg)) {
> + src_reg = ®s[insn->src_reg];
> + if (!(reg_is_pkt_pointer_any(dst_reg) && reg_is_pkt_pointer_any(src_reg)) &&
> + is_pointer_value(env, insn->src_reg)) {
> verbose(env, "R%d pointer comparison prohibited\n",
> insn->src_reg);
> return -EACCES;
> }
> - src_reg = ®s[insn->src_reg];
I tested this change and it seem to work as intended. Was worried a
bit that there are three places in this function where such checks are
applied:
1. upon entry for BPF_X case (this one): checks if dst_reg/src_reg are
pointers to packet or packet end or packet meta;
2. when attempting to predict branch: prediction would be triggered
only when dst/src is packet/packet_end (or vice-versa);
3. when prediction failed and both branches have to be visited
(`try_match_pkt_pointers`): dst/src have to be packet/packet_end or
meta/packet-start (or vice versa).
Check (1) is more permissive than (2) or (3) but either (2) or (3)
would be applied before exit, so there is no contradiction.
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
> } else {
> if (insn->src_reg != BPF_REG_0) {
> verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
> @@ -14071,12 +14078,6 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
> }
> }
>
> - /* check src2 operand */
> - err = check_reg_arg(env, insn->dst_reg, SRC_OP);
> - if (err)
> - return err;
> -
> - dst_reg = ®s[insn->dst_reg];
> is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
>
> if (BPF_SRC(insn->code) == BPF_K) {
next prev parent reply other threads:[~2023-08-21 12:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-18 8:39 [PATCH bpf-next 0/2] bpf: Fix an issue in verifing allow_ptr_leaks Yafang Shao
2023-08-18 8:39 ` [PATCH bpf-next 1/2] bpf: Fix issue in verifying allow_ptr_leaks Yafang Shao
2023-08-21 12:33 ` Eduard Zingerman [this message]
2023-08-18 8:39 ` [PATCH bpf-next 2/2] selftests/bpf: Add selftest for allow_ptr_leaks Yafang Shao
2023-08-21 22:45 ` Alexei Starovoitov
2023-08-22 2:42 ` Yafang Shao
2023-08-22 3:28 ` Alexei Starovoitov
2023-08-22 3:44 ` Yafang Shao
2023-08-23 9:44 ` Eduard Zingerman
2023-08-23 15:51 ` 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=6e048371123eae0f89b58581a043b1a3de36f7f3.camel@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=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=laoar.shao@gmail.com \
--cc=martin.lau@linux.dev \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--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