BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements
@ 2024-05-06 14:18 Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 1/6] bpf/verifier: replace calls to mark_reg_unknown Cupertino Miranda
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Cupertino Miranda @ 2024-05-06 14:18 UTC (permalink / raw)
  To: bpf
  Cc: Cupertino Miranda, Yonghong Song, Alexei Starovoitov, David Faust,
	Jose Marchesi, Elena Zannoni, Eduard Zingerman, Andrii Nakryiko

Hi everyone,

This is what I hope to be the last version. :)

Regards,
Cupertino

Changes from v1:
 - Reordered patches in the series.
 - Fix refactor to be acurate with original code.
 - Fixed other mentioned small problems.

Changes from v2:
 - Added a patch to replace mark_reg_unknowon for __mark_reg_unknown in
   the context of range computation.
 - Reverted implementation of refactor to v1 which used a simpler
   boolean return value in check function.
 - Further relaxed MUL to allow it to still compute a range when neither
   of its registers is a known value.
 - Simplified tests based on Eduards example.
 - Added messages in selftest commits.

Changes from v3:
 - Improved commit message of patch nr 1.
 - Coding style fixes.
 - Improve XOR and OR tests.
 - Made function calls to pass struct bpf_reg_state pointer instead.
 - Improved final code as a last patch.

Changes from v4:
 - Merged patch nr 7 in 2.

Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: David Faust <david.faust@oracle.com>
Cc: Jose Marchesi <jose.marchesi@oracle.com>
Cc: Elena Zannoni <elena.zannoni@oracle.com>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>

Cupertino Miranda (6):
  bpf/verifier: replace calls to mark_reg_unknown.
  bpf/verifier: refactor checks for range computation
  bpf/verifier: improve XOR and OR range computation
  selftests/bpf: XOR and OR range computation tests.
  bpf/verifier: relax MUL range computation check
  selftests/bpf: MUL range computation tests.

 kernel/bpf/verifier.c                         | 106 +++++++-----------
 .../selftests/bpf/progs/verifier_bounds.c     |  63 +++++++++++
 2 files changed, 104 insertions(+), 65 deletions(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v5 1/6] bpf/verifier: replace calls to mark_reg_unknown.
  2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
@ 2024-05-06 14:18 ` Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 2/6] bpf/verifier: refactor checks for range computation Cupertino Miranda
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Cupertino Miranda @ 2024-05-06 14:18 UTC (permalink / raw)
  To: bpf
  Cc: Cupertino Miranda, Eduard Zingerman, Yonghong Song,
	Alexei Starovoitov, David Faust, Jose Marchesi, Elena Zannoni,
	Andrii Nakryiko

In order to further simplify the code in adjust_scalar_min_max_vals all
the calls to mark_reg_unknown are replaced by __mark_reg_unknown.

static void mark_reg_unknown(struct bpf_verifier_env *env,
  			     struct bpf_reg_state *regs, u32 regno)
{
	if (WARN_ON(regno >= MAX_BPF_REG)) {
		... mark all regs not init ...
		return;
    }
	__mark_reg_unknown(env, regs + regno);
}

The 'regno >= MAX_BPF_REG' does not apply to
adjust_scalar_min_max_vals(), because it is only called from the
following stack:
  - check_alu_op
    - adjust_reg_min_max_vals
      - adjust_scalar_min_max_vals

The check_alu_op() does check_reg_arg() which verifies that both src and
dst register numbers are within bounds.

Signed-off-by: Cupertino Miranda <cupertino.miranda@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: David Faust <david.faust@oracle.com>
Cc: Jose Marchesi <jose.marchesi@oracle.com>
Cc: Elena Zannoni <elena.zannoni@oracle.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
---
 kernel/bpf/verifier.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 7360f04f9ec7..41c66cc6db80 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13887,7 +13887,6 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 				      struct bpf_reg_state *dst_reg,
 				      struct bpf_reg_state src_reg)
 {
-	struct bpf_reg_state *regs = cur_regs(env);
 	u8 opcode = BPF_OP(insn->code);
 	bool src_known;
 	s64 smin_val, smax_val;
@@ -13994,7 +13993,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 			/* Shifts greater than 31 or 63 are undefined.
 			 * This includes shifts by a negative number.
 			 */
-			mark_reg_unknown(env, regs, insn->dst_reg);
+			__mark_reg_unknown(env, dst_reg);
 			break;
 		}
 		if (alu32)
@@ -14007,7 +14006,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 			/* Shifts greater than 31 or 63 are undefined.
 			 * This includes shifts by a negative number.
 			 */
-			mark_reg_unknown(env, regs, insn->dst_reg);
+			__mark_reg_unknown(env, dst_reg);
 			break;
 		}
 		if (alu32)
@@ -14020,7 +14019,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 			/* Shifts greater than 31 or 63 are undefined.
 			 * This includes shifts by a negative number.
 			 */
-			mark_reg_unknown(env, regs, insn->dst_reg);
+			__mark_reg_unknown(env, dst_reg);
 			break;
 		}
 		if (alu32)
@@ -14029,7 +14028,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 			scalar_min_max_arsh(dst_reg, &src_reg);
 		break;
 	default:
-		mark_reg_unknown(env, regs, insn->dst_reg);
+		__mark_reg_unknown(env, dst_reg);
 		break;
 	}
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v5 2/6] bpf/verifier: refactor checks for range computation
  2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 1/6] bpf/verifier: replace calls to mark_reg_unknown Cupertino Miranda
@ 2024-05-06 14:18 ` Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 3/6] bpf/verifier: improve XOR and OR " Cupertino Miranda
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Cupertino Miranda @ 2024-05-06 14:18 UTC (permalink / raw)
  To: bpf
  Cc: Cupertino Miranda, Eduard Zingerman, Yonghong Song,
	Alexei Starovoitov, David Faust, Jose Marchesi, Elena Zannoni,
	Andrii Nakryiko

Split range computation checks in its own function, isolating pessimitic
range set for dst_reg and failing return to a single point.

Signed-off-by: Cupertino Miranda <cupertino.miranda@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: David Faust <david.faust@oracle.com>
Cc: Jose Marchesi <jose.marchesi@oracle.com>
Cc: Elena Zannoni <elena.zannoni@oracle.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>

bpf/verifier: improve code after range computation recent changes.
---
 kernel/bpf/verifier.c | 109 +++++++++++++++++-------------------------
 1 file changed, 45 insertions(+), 64 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 41c66cc6db80..bdaf0413bf06 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13878,6 +13878,50 @@ static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg,
 	__update_reg_bounds(dst_reg);
 }
 
+static bool is_safe_to_compute_dst_reg_range(struct bpf_insn *insn,
+					     const struct bpf_reg_state *src_reg)
+{
+	bool src_is_const = false;
+	u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
+
+	if (insn_bitness == 32) {
+		if (tnum_subreg_is_const(src_reg->var_off)
+		    && src_reg->s32_min_value == src_reg->s32_max_value
+		    && src_reg->u32_min_value == src_reg->u32_max_value)
+			src_is_const = true;
+	} else {
+		if (tnum_is_const(src_reg->var_off)
+		    && src_reg->smin_value == src_reg->smax_value
+		    && src_reg->umin_value == src_reg->umax_value)
+			src_is_const = true;
+	}
+
+	switch (BPF_OP(insn->code)) {
+	case BPF_ADD:
+	case BPF_SUB:
+	case BPF_AND:
+		return true;
+
+	/* Compute range for the following only if the src_reg is const.
+	 */
+	case BPF_XOR:
+	case BPF_OR:
+	case BPF_MUL:
+		return src_is_const;
+
+	/* Shift operators range is only computable if shift dimension operand
+	 * is a constant. Shifts greater than 31 or 63 are undefined. This
+	 * includes shifts by a negative number.
+	 */
+	case BPF_LSH:
+	case BPF_RSH:
+	case BPF_ARSH:
+		return (src_is_const && src_reg->umax_value < insn_bitness);
+	default:
+		return false;
+	}
+}
+
 /* WARNING: This function does calculations on 64-bit values, but the actual
  * execution may occur on 32-bit values. Therefore, things like bitshifts
  * need extra checks in the 32-bit case.
@@ -13888,51 +13932,10 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 				      struct bpf_reg_state src_reg)
 {
 	u8 opcode = BPF_OP(insn->code);
-	bool src_known;
-	s64 smin_val, smax_val;
-	u64 umin_val, umax_val;
-	s32 s32_min_val, s32_max_val;
-	u32 u32_min_val, u32_max_val;
-	u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
 	bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64);
 	int ret;
 
-	smin_val = src_reg.smin_value;
-	smax_val = src_reg.smax_value;
-	umin_val = src_reg.umin_value;
-	umax_val = src_reg.umax_value;
-
-	s32_min_val = src_reg.s32_min_value;
-	s32_max_val = src_reg.s32_max_value;
-	u32_min_val = src_reg.u32_min_value;
-	u32_max_val = src_reg.u32_max_value;
-
-	if (alu32) {
-		src_known = tnum_subreg_is_const(src_reg.var_off);
-		if ((src_known &&
-		     (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) ||
-		    s32_min_val > s32_max_val || u32_min_val > u32_max_val) {
-			/* Taint dst register if offset had invalid bounds
-			 * derived from e.g. dead branches.
-			 */
-			__mark_reg_unknown(env, dst_reg);
-			return 0;
-		}
-	} else {
-		src_known = tnum_is_const(src_reg.var_off);
-		if ((src_known &&
-		     (smin_val != smax_val || umin_val != umax_val)) ||
-		    smin_val > smax_val || umin_val > umax_val) {
-			/* Taint dst register if offset had invalid bounds
-			 * derived from e.g. dead branches.
-			 */
-			__mark_reg_unknown(env, dst_reg);
-			return 0;
-		}
-	}
-
-	if (!src_known &&
-	    opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
+	if (!is_safe_to_compute_dst_reg_range(insn, &src_reg)) {
 		__mark_reg_unknown(env, dst_reg);
 		return 0;
 	}
@@ -13989,46 +13992,24 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 		scalar_min_max_xor(dst_reg, &src_reg);
 		break;
 	case BPF_LSH:
-		if (umax_val >= insn_bitness) {
-			/* Shifts greater than 31 or 63 are undefined.
-			 * This includes shifts by a negative number.
-			 */
-			__mark_reg_unknown(env, dst_reg);
-			break;
-		}
 		if (alu32)
 			scalar32_min_max_lsh(dst_reg, &src_reg);
 		else
 			scalar_min_max_lsh(dst_reg, &src_reg);
 		break;
 	case BPF_RSH:
-		if (umax_val >= insn_bitness) {
-			/* Shifts greater than 31 or 63 are undefined.
-			 * This includes shifts by a negative number.
-			 */
-			__mark_reg_unknown(env, dst_reg);
-			break;
-		}
 		if (alu32)
 			scalar32_min_max_rsh(dst_reg, &src_reg);
 		else
 			scalar_min_max_rsh(dst_reg, &src_reg);
 		break;
 	case BPF_ARSH:
-		if (umax_val >= insn_bitness) {
-			/* Shifts greater than 31 or 63 are undefined.
-			 * This includes shifts by a negative number.
-			 */
-			__mark_reg_unknown(env, dst_reg);
-			break;
-		}
 		if (alu32)
 			scalar32_min_max_arsh(dst_reg, &src_reg);
 		else
 			scalar_min_max_arsh(dst_reg, &src_reg);
 		break;
 	default:
-		__mark_reg_unknown(env, dst_reg);
 		break;
 	}
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v5 3/6] bpf/verifier: improve XOR and OR range computation
  2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 1/6] bpf/verifier: replace calls to mark_reg_unknown Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 2/6] bpf/verifier: refactor checks for range computation Cupertino Miranda
@ 2024-05-06 14:18 ` Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 4/6] selftests/bpf: XOR and OR range computation tests Cupertino Miranda
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Cupertino Miranda @ 2024-05-06 14:18 UTC (permalink / raw)
  To: bpf
  Cc: Cupertino Miranda, Eduard Zingerman, Yonghong Song,
	Alexei Starovoitov, David Faust, Jose Marchesi, Elena Zannoni,
	Andrii Nakryiko

Range for XOR and OR operators would not be attempted unless src_reg
would resolve to a single value, i.e. a known constant value.
This condition is unnecessary, and the following XOR/OR operator
handling could compute a possible better range.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

Signed-off-by: Cupertino Miranda <cupertino.miranda@oracle.com
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: David Faust <david.faust@oracle.com>
Cc: Jose Marchesi <jose.marchesi@oracle.com>
Cc: Elena Zannoni <elena.zannoni@oracle.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
---
 kernel/bpf/verifier.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index bdaf0413bf06..1f6deb3e44c5 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13900,12 +13900,12 @@ static bool is_safe_to_compute_dst_reg_range(struct bpf_insn *insn,
 	case BPF_ADD:
 	case BPF_SUB:
 	case BPF_AND:
+	case BPF_XOR:
+	case BPF_OR:
 		return true;
 
 	/* Compute range for the following only if the src_reg is const.
 	 */
-	case BPF_XOR:
-	case BPF_OR:
 	case BPF_MUL:
 		return src_is_const;
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v5 4/6] selftests/bpf: XOR and OR range computation tests.
  2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
                   ` (2 preceding siblings ...)
  2024-05-06 14:18 ` [PATCH bpf-next v5 3/6] bpf/verifier: improve XOR and OR " Cupertino Miranda
@ 2024-05-06 14:18 ` Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 5/6] bpf/verifier: relax MUL range computation check Cupertino Miranda
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Cupertino Miranda @ 2024-05-06 14:18 UTC (permalink / raw)
  To: bpf
  Cc: Cupertino Miranda, Eduard Zingerman, Yonghong Song,
	Alexei Starovoitov, David Faust, Jose Marchesi, Elena Zannoni,
	Andrii Nakryiko

Added a test for bound computation in XOR and OR when non constant
values are used and both registers have bounded ranges.

Signed-off-by: Cupertino Miranda <cupertino.miranda@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: David Faust <david.faust@oracle.com>
Cc: Jose Marchesi <jose.marchesi@oracle.com>
Cc: Elena Zannoni <elena.zannoni@oracle.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
---
 .../selftests/bpf/progs/verifier_bounds.c     | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/verifier_bounds.c b/tools/testing/selftests/bpf/progs/verifier_bounds.c
index 960998f16306..7d570acf23ee 100644
--- a/tools/testing/selftests/bpf/progs/verifier_bounds.c
+++ b/tools/testing/selftests/bpf/progs/verifier_bounds.c
@@ -885,6 +885,48 @@ l1_%=:	r0 = 0;						\
 	: __clobber_all);
 }
 
+SEC("socket")
+__description("bounds check for non const xor src dst")
+__success __log_level(2)
+__msg("5: (af) r0 ^= r6                      ; R0_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=431,var_off=(0x0; 0x1af))")
+__naked void non_const_xor_src_dst(void)
+{
+	asm volatile ("					\
+	call %[bpf_get_prandom_u32];                    \
+	r6 = r0;					\
+	call %[bpf_get_prandom_u32];                    \
+	r6 &= 0xaf;					\
+	r0 &= 0x1a0;					\
+	r0 ^= r6;					\
+	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	__imm_addr(map_hash_8b),
+	__imm(bpf_get_prandom_u32)
+	: __clobber_all);
+}
+
+SEC("socket")
+__description("bounds check for non const or src dst")
+__success __log_level(2)
+__msg("5: (4f) r0 |= r6                      ; R0_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=431,var_off=(0x0; 0x1af))")
+__naked void non_const_or_src_dst(void)
+{
+	asm volatile ("					\
+	call %[bpf_get_prandom_u32];                    \
+	r6 = r0;					\
+	call %[bpf_get_prandom_u32];                    \
+	r6 &= 0xaf;					\
+	r0 &= 0x1a0;					\
+	r0 |= r6;					\
+	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	__imm_addr(map_hash_8b),
+	__imm(bpf_get_prandom_u32)
+	: __clobber_all);
+}
+
 SEC("socket")
 __description("bounds checks after 32-bit truncation. test 1")
 __success __failure_unpriv __msg_unpriv("R0 leaks addr")
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v5 5/6] bpf/verifier: relax MUL range computation check
  2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
                   ` (3 preceding siblings ...)
  2024-05-06 14:18 ` [PATCH bpf-next v5 4/6] selftests/bpf: XOR and OR range computation tests Cupertino Miranda
@ 2024-05-06 14:18 ` Cupertino Miranda
  2024-05-06 14:18 ` [PATCH bpf-next v5 6/6] selftests/bpf: MUL range computation tests Cupertino Miranda
  2024-05-07  0:20 ` [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements patchwork-bot+netdevbpf
  6 siblings, 0 replies; 8+ messages in thread
From: Cupertino Miranda @ 2024-05-06 14:18 UTC (permalink / raw)
  To: bpf
  Cc: Cupertino Miranda, Eduard Zingerman, Andrii Nakryiko,
	Yonghong Song, Alexei Starovoitov, David Faust, Jose Marchesi,
	Elena Zannoni

MUL instruction required that src_reg would be a known value (i.e.
src_reg would be a const value). The condition in this case can be
relaxed, since the range computation algorithm used in current code
already supports a proper range computation for any valid range value on
its operands.

Signed-off-by: Cupertino Miranda <cupertino.miranda@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: David Faust <david.faust@oracle.com>
Cc: Jose Marchesi <jose.marchesi@oracle.com>
Cc: Elena Zannoni <elena.zannoni@oracle.com>
---
 kernel/bpf/verifier.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 1f6deb3e44c5..9e3aba08984e 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13902,12 +13902,8 @@ static bool is_safe_to_compute_dst_reg_range(struct bpf_insn *insn,
 	case BPF_AND:
 	case BPF_XOR:
 	case BPF_OR:
-		return true;
-
-	/* Compute range for the following only if the src_reg is const.
-	 */
 	case BPF_MUL:
-		return src_is_const;
+		return true;
 
 	/* Shift operators range is only computable if shift dimension operand
 	 * is a constant. Shifts greater than 31 or 63 are undefined. This
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH bpf-next v5 6/6] selftests/bpf: MUL range computation tests.
  2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
                   ` (4 preceding siblings ...)
  2024-05-06 14:18 ` [PATCH bpf-next v5 5/6] bpf/verifier: relax MUL range computation check Cupertino Miranda
@ 2024-05-06 14:18 ` Cupertino Miranda
  2024-05-07  0:20 ` [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements patchwork-bot+netdevbpf
  6 siblings, 0 replies; 8+ messages in thread
From: Cupertino Miranda @ 2024-05-06 14:18 UTC (permalink / raw)
  To: bpf
  Cc: Cupertino Miranda, Eduard Zingerman, Andrii Nakryiko,
	Yonghong Song, Alexei Starovoitov, David Faust, Jose Marchesi,
	Elena Zannoni

Added a test for bound computation in MUL when non constant
values are used and both registers have bounded ranges.

Signed-off-by: Cupertino Miranda <cupertino.miranda@oracle.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: David Faust <david.faust@oracle.com>
Cc: Jose Marchesi <jose.marchesi@oracle.com>
Cc: Elena Zannoni <elena.zannoni@oracle.com>
---
 .../selftests/bpf/progs/verifier_bounds.c     | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/verifier_bounds.c b/tools/testing/selftests/bpf/progs/verifier_bounds.c
index 7d570acf23ee..a0bb7fb40ea5 100644
--- a/tools/testing/selftests/bpf/progs/verifier_bounds.c
+++ b/tools/testing/selftests/bpf/progs/verifier_bounds.c
@@ -927,6 +927,27 @@ __naked void non_const_or_src_dst(void)
 	: __clobber_all);
 }
 
+SEC("socket")
+__description("bounds check for non const mul regs")
+__success __log_level(2)
+__msg("5: (2f) r0 *= r6                      ; R0_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=3825,var_off=(0x0; 0xfff))")
+__naked void non_const_mul_regs(void)
+{
+	asm volatile ("					\
+	call %[bpf_get_prandom_u32];                    \
+	r6 = r0;					\
+	call %[bpf_get_prandom_u32];                    \
+	r6 &= 0xff;					\
+	r0 &= 0x0f;					\
+	r0 *= r6;					\
+	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	__imm_addr(map_hash_8b),
+	__imm(bpf_get_prandom_u32)
+	: __clobber_all);
+}
+
 SEC("socket")
 __description("bounds checks after 32-bit truncation. test 1")
 __success __failure_unpriv __msg_unpriv("R0 leaks addr")
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements
  2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
                   ` (5 preceding siblings ...)
  2024-05-06 14:18 ` [PATCH bpf-next v5 6/6] selftests/bpf: MUL range computation tests Cupertino Miranda
@ 2024-05-07  0:20 ` patchwork-bot+netdevbpf
  6 siblings, 0 replies; 8+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-05-07  0:20 UTC (permalink / raw)
  To: Cupertino Miranda
  Cc: bpf, yonghong.song, alexei.starovoitov, david.faust,
	jose.marchesi, elena.zannoni, eddyz87, andrii.nakryiko

Hello:

This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Mon,  6 May 2024 15:18:43 +0100 you wrote:
> Hi everyone,
> 
> This is what I hope to be the last version. :)
> 
> Regards,
> Cupertino
> 
> [...]

Here is the summary with links:
  - [bpf-next,v5,1/6] bpf/verifier: replace calls to mark_reg_unknown.
    https://git.kernel.org/bpf/bpf-next/c/d786957ebd3f
  - [bpf-next,v5,2/6] bpf/verifier: refactor checks for range computation
    https://git.kernel.org/bpf/bpf-next/c/0922c78f592c
  - [bpf-next,v5,3/6] bpf/verifier: improve XOR and OR range computation
    https://git.kernel.org/bpf/bpf-next/c/138cc42c05d1
  - [bpf-next,v5,4/6] selftests/bpf: XOR and OR range computation tests.
    https://git.kernel.org/bpf/bpf-next/c/5ec9a7d13f49
  - [bpf-next,v5,5/6] bpf/verifier: relax MUL range computation check
    https://git.kernel.org/bpf/bpf-next/c/41d047a87106
  - [bpf-next,v5,6/6] selftests/bpf: MUL range computation tests.
    https://git.kernel.org/bpf/bpf-next/c/92956786b4e2

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-05-07  0:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-06 14:18 [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements Cupertino Miranda
2024-05-06 14:18 ` [PATCH bpf-next v5 1/6] bpf/verifier: replace calls to mark_reg_unknown Cupertino Miranda
2024-05-06 14:18 ` [PATCH bpf-next v5 2/6] bpf/verifier: refactor checks for range computation Cupertino Miranda
2024-05-06 14:18 ` [PATCH bpf-next v5 3/6] bpf/verifier: improve XOR and OR " Cupertino Miranda
2024-05-06 14:18 ` [PATCH bpf-next v5 4/6] selftests/bpf: XOR and OR range computation tests Cupertino Miranda
2024-05-06 14:18 ` [PATCH bpf-next v5 5/6] bpf/verifier: relax MUL range computation check Cupertino Miranda
2024-05-06 14:18 ` [PATCH bpf-next v5 6/6] selftests/bpf: MUL range computation tests Cupertino Miranda
2024-05-07  0:20 ` [PATCH bpf-next v5 0/6] bpf/verifier: range computation improvements patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox