Skip to contentSkip to conversation

bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.

PATCH Applied Alexei Starovoitov 3 participants 7 messages Nov 1, 2021 – Nov 2, 2021

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.

From: Alexei Starovoitov <[email protected]>

Before this fix:
166: (b5) if r2 <= 0x1 goto pc+22
from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0xffffffff))

After this fix:
166: (b5) if r2 <= 0x1 goto pc+22
from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0x1))

While processing BPF_JLE the reg_set_min_max() would set true_reg->umax_value = 1 and call __reg_combine_64_into_32(true_reg).

Without the fix it would not pass the condition:
if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value))

since umin_value == 0 at this point.
Before commit 10bf4e83167c the umin was incorrectly ingored.
The commit 10bf4e83167c fixed the correctness issue, but pessimized propagation of 64-bit min max into 32-bit min max and corresponding var_off.

Fixes 10bf4e83167c ("bpf: Fix propagation of 32 bit unsigned bounds from 64 bit bounds")
---
 kernel/bpf/verifier.c                               | 2 +-
 tools/testing/selftests/bpf/verifier/array_access.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +1 -1
index 3c8aa7df1773..29671ed49ee8 100644
@@ -1425,7 +1425,7 @@ static bool __reg64_bound_s32(s64 a)
 
 static bool __reg64_bound_u32(u64 a)
 {
-	return a > U32_MIN && a < U32_MAX;
+	return a >= U32_MIN && a <= U32_MAX;
 }
 
 static void __reg_combine_64_into_32(struct bpf_reg_state *reg)
tools/testing/selftests/bpf/verifier/array_access.c +1 -1
index 1b1c798e9248..1b138cd2b187 100644
@@ -186,7 +186,7 @@
 	},
 	.fixup_map_hash_48b = { 3 },
 	.errstr_unpriv = "R0 leaks addr",
-	.errstr = "R0 unbounded memory access",
+	.errstr = "invalid access to map value, value_size=48 off=44 size=8",
 	.result_unpriv = REJECT,
 	.result = REJECT,
 	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 2/3 bpf: Fix propagation of signed bounds from 64-bit min/max into 32-bit.

From: Alexei Starovoitov <[email protected]>

Similar to unsigned bounds propagation fix signed bounds. The 'Fixes' tag is a hint. There is no security bug here. The verifier was too conservative.

Fixes 3f50f132d840 ("bpf: Verifier, do explicit ALU32 bounds tracking")
---
 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index 29671ed49ee8..a4b48bd4e3ca 100644
@@ -1420,7 +1420,7 @@ static void __reg_combine_32_into_64(struct bpf_reg_state *reg)
 
 static bool __reg64_bound_s32(s64 a)
 {
-	return a > S32_MIN && a < S32_MAX;
+	return a >= S32_MIN && a <= S32_MAX;
 }
 
 static bool __reg64_bound_u32(u64 a)
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v2 bpf-next 2/3] bpf: Fix propagation of signed bounds from 64-bit min/max into 32-bit.

Quoted 8 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Similar to unsigned bounds propagation fix signed bounds. The 'Fixes' tag is a hint. There is no security bug here. The verifier was too conservative.

Fixes 3f50f132d840 ("bpf: Verifier, do explicit ALU32 bounds tracking")

The change looks good. Should a new test_verifier test be added to exercise the new change?

Quoted 18 lines
---
  kernel/bpf/verifier.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index 29671ed49ee8..a4b48bd4e3ca 100644
@@ -1420,7 +1420,7 @@ static void __reg_combine_32_into_64(struct bpf_reg_state *reg)
 
  static bool __reg64_bound_s32(s64 a)
  {
-	return a > S32_MIN && a < S32_MAX;
+	return a >= S32_MIN && a <= S32_MAX;
  }
 
  static bool __reg64_bound_u32(u64 a)
#3 lore raw

Re: [PATCH v2 bpf-next 2/3] bpf: Fix propagation of signed bounds from 64-bit min/max into 32-bit.

Quoted 10 lines · Yonghong Song
Quoted 6 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Similar to unsigned bounds propagation fix signed bounds. The 'Fixes' tag is a hint. There is no security bug here. The verifier was too conservative.

Quoted 5 lines
Quoted 2 lines
Fixes 3f50f132d840 ("bpf: Verifier, do explicit ALU32 bounds tracking")

The change looks good. Should a new test_verifier test be added to exercise the new change?

I think manually string comparing output the way VERBOSE_ACCEPT is doing is an overkill here.
The real test case in .c will take some time to craft.

Acked-by
#4 lore raw

Re: [PATCH v2 bpf-next 2/3] bpf: Fix propagation of signed bounds from 64-bit min/max into 32-bit.

Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song
Quoted 8 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Similar to unsigned bounds propagation fix signed bounds. The 'Fixes' tag is a hint. There is no security bug here. The verifier was too conservative.

Fixes 3f50f132d840 ("bpf: Verifier, do explicit ALU32 bounds tracking")
Quoted 6 lines
Quoted 2 lines

The change looks good. Should a new test_verifier test be added to exercise the new change?

I think manually string comparing output the way VERBOSE_ACCEPT is doing is an overkill here.
The real test case in .c will take some time to craft.

Okay. Sounds good to me.

#5 lore raw

PATCH v2 3/3 selftests/bpf: Add a testcase for 64-bit bounds propagation issue.

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

./test_progs-no_alu32 -vv -t twfw

Before the 64-bit_into_32-bit fix:
19: (25) if r1 > 0x3f goto pc+6
R1_w=inv(id=0,umax_value=63,var_off=(0x0; 0xff),s32_max_value=255,u32_max_value=255)

and eventually:

invalid access to map value, value_size=8 off=7 size=8 R6 max value is outside of the allowed memory range
libbpf: failed to load object 'no_alu32/twfw.o'

After the fix:
19: (25) if r1 > 0x3f goto pc+6
R1_w=inv(id=0,umax_value=63,var_off=(0x0; 0x3f))

verif_twfw:OK

---
 .../bpf/prog_tests/bpf_verif_scale.c          |  5 ++
 tools/testing/selftests/bpf/progs/twfw.c      | 58 +++++++++++++++++++
 2 files changed, 63 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/twfw.c
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +5 -0
index 867349e4ed9e..27f5d8ea7964 100644
@@ -202,3 +202,8 @@ void test_verif_scale_seg6_loop()
 {
 	scale_test("test_seg6_loop.o", BPF_PROG_TYPE_LWT_SEG6LOCAL, false);
 }
+
+void test_verif_twfw()
+{
+	scale_test("twfw.o", BPF_PROG_TYPE_CGROUP_SKB, false);
+}
tools/testing/selftests/bpf/progs/twfw.c new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw
In reply to Alexei Starovoitov
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 11 lines · On Mon, 1 Nov 2021 15:21:51 -0700 you wrote:

From: Alexei Starovoitov <[email protected]>

Before this fix:
166: (b5) if r2 <= 0x1 goto pc+22
from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0xffffffff))

After this fix:
166: (b5) if r2 <= 0x1 goto pc+22
from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0x1))

[...]

Here is the summary with links:
- [v2,bpf-next,1/3] bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.

    https://git.kernel.org/bpf/bpf-next/c/b9979db83401
  - [v2,bpf-next,2/3] bpf: Fix propagation of signed bounds from 64-bit min/max into 32-bit.
    https://git.kernel.org/bpf/bpf-next/c/388e2c0b9783
  - [v2,bpf-next,3/3] selftests/bpf: Add a testcase for 64-bit bounds propagation issue.
    https://git.kernel.org/bpf/bpf-next/c/0869e5078afb

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html