stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ovidiu Panait <ovidiu.panait@windriver.com>
To: stable@vger.kernel.org
Cc: fllinden@amazon.com, bpf@vger.kernel.org, ast@kernel.org,
	daniel@iogearbox.net, yhs@fb.com, john.fastabend@gmail.com,
	samjonas@amazon.com
Subject: [PATCH 4.19 06/12] bpf: Ensure off_reg has no mixed signed bounds for all types
Date: Thu, 27 May 2021 20:37:26 +0300	[thread overview]
Message-ID: <20210527173732.20860-7-ovidiu.panait@windriver.com> (raw)
In-Reply-To: <20210527173732.20860-1-ovidiu.panait@windriver.com>

From: Daniel Borkmann <daniel@iogearbox.net>

commit 24c109bb1537c12c02aeed2d51a347b4d6a9b76e upstream.

The mixed signed bounds check really belongs into retrieve_ptr_limit()
instead of outside of it in adjust_ptr_min_max_vals(). The reason is
that this check is not tied to PTR_TO_MAP_VALUE only, but to all pointer
types that we handle in retrieve_ptr_limit() and given errors from the latter
propagate back to adjust_ptr_min_max_vals() and lead to rejection of the
program, it's a better place to reside to avoid anything slipping through
for future types. The reason why we must reject such off_reg is that we
otherwise would not be able to derive a mask, see details in 9d7eceede769
("bpf: restrict unknown scalars of mixed signed bounds for unprivileged").

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
[fllinden@amazon.com: backport to 5.4]
Signed-off-by: Frank van der Linden <fllinden@amazon.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[OP: backport to 4.19]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 kernel/bpf/verifier.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 5e0646efac6d..b260bcc7215f 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -2730,12 +2730,18 @@ static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
 }
 
 static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
-			      u32 *ptr_limit, u8 opcode, bool off_is_neg)
+			      const struct bpf_reg_state *off_reg,
+			      u32 *ptr_limit, u8 opcode)
 {
+	bool off_is_neg = off_reg->smin_value < 0;
 	bool mask_to_left = (opcode == BPF_ADD &&  off_is_neg) ||
 			    (opcode == BPF_SUB && !off_is_neg);
 	u32 off, max;
 
+	if (!tnum_is_const(off_reg->var_off) &&
+	    (off_reg->smin_value < 0) != (off_reg->smax_value < 0))
+		return -EACCES;
+
 	switch (ptr_reg->type) {
 	case PTR_TO_STACK:
 		/* Offset 0 is out-of-bounds, but acceptable start for the
@@ -2826,7 +2832,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
 	alu_state |= ptr_is_dst_reg ?
 		     BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
 
-	err = retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg);
+	err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode);
 	if (err < 0)
 		return err;
 
@@ -2871,8 +2877,8 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 	    smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
 	u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
 	    umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
-	u32 dst = insn->dst_reg, src = insn->src_reg;
 	u8 opcode = BPF_OP(insn->code);
+	u32 dst = insn->dst_reg;
 	int ret;
 
 	dst_reg = &regs[dst];
@@ -2909,12 +2915,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 			dst);
 		return -EACCES;
 	}
-	if (ptr_reg->type == PTR_TO_MAP_VALUE &&
-	    !env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
-		verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
-			off_reg == dst_reg ? dst : src);
-		return -EACCES;
-	}
 
 	/* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
 	 * The id may be overwritten later if we create a new variable offset.
-- 
2.17.1


  parent reply	other threads:[~2021-05-27 17:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-27 17:37 [PATCH 4.19 00/12] bpf: fix verifier selftests, add CVE-2021-29155 fixes Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 01/12] bpf: fix up selftests after backports were fixed Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 02/12] bpf, selftests: Fix up some test_verifier cases for unprivileged Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 03/12] selftests/bpf: Test narrow loads with off > 0 in test_verifier Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 04/12] selftests/bpf: add selftest part of "bpf: improve verifier branch analysis" Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 05/12] bpf: Move off_reg into sanitize_ptr_alu Ovidiu Panait
2021-05-27 17:37 ` Ovidiu Panait [this message]
2021-05-27 17:37 ` [PATCH 4.19 07/12] bpf: Rework ptr_limit into alu_limit and add common error path Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 08/12] bpf: Improve verifier error messages for users Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 09/12] bpf: Refactor and streamline bounds check into helper Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 10/12] bpf: Move sanitize_val_alu out of op switch Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 11/12] bpf: Tighten speculative pointer arithmetic mask Ovidiu Panait
2021-05-27 17:37 ` [PATCH 4.19 12/12] bpf: Update selftests to reflect new error states Ovidiu Panait
2021-05-27 19:44 ` [PATCH 4.19 00/12] bpf: fix verifier selftests, add CVE-2021-29155 fixes Daniel Borkmann
2021-05-27 20:31 ` Frank van der Linden
2021-05-28 10:12 ` Tiezhu Yang

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=20210527173732.20860-7-ovidiu.panait@windriver.com \
    --to=ovidiu.panait@windriver.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=fllinden@amazon.com \
    --cc=john.fastabend@gmail.com \
    --cc=samjonas@amazon.com \
    --cc=stable@vger.kernel.org \
    --cc=yhs@fb.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;
as well as URLs for NNTP newsgroup(s).