bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.
Subsystems (6)
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.
--- 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
@@ -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
@@ -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
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.
--- kernel/bpf/verifier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
@@ -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
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")Signed-off-by Alexei Starovoitov <[email protected]>
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@@ -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)
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")Signed-off-by Alexei Starovoitov <[email protected]>The change looks good. Should a new
test_verifiertest 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.
Yonghong Song
Acked-byRe: [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")Signed-off-by Alexei Starovoitov <[email protected]>
Quoted 6 lines
Quoted 2 lines
The change looks good. Should a new
test_verifiertest be added to exercise the new change?I think manually string comparing output the way
VERBOSE_ACCEPTis doing is an overkill here.
The real test case in .c will take some time to craft.
Okay. Sounds good to me.
PATCH v2 3/3 selftests/bpf: Add a testcase for 64-bit bounds propagation issue.
In reply to Alexei StarovoitovFrom: 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
@@ -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
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 <=0x1goto pc+22
from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0;0xffffffff))After this fix:
166: (b5) if r2 <=0x1goto 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