selftests/bpf: fix pointer arithmetic in test_xdp_do_redirect
Subsystems (6)
PATCH 1/1 selftests/bpf: fix pointer arithmetic in test_xdp_do_redirect
Cast operation has a higher precedence than addition. The code here wants to zero the 2nd half of the 64-bit metadata, but due to a pointer arithmetic mistake, it writes the zero at offset 16 instead.
Just adding parentheses around "data + 4" would fix this, but I think this will be slightly better readable with array syntax.
I was unable to test this with tools/testing/selftests/bpf/vmtest.sh, because my glibc is newer than glibc in the provided VM image. So I just checked the difference in the compiled code. objdump -S tools/testing/selftests/bpf/xdp_do_redirect.test.o:
- *((__u32 *)data) = 0x42; /* metadata test value */
+ ((__u32 *)data)[0] = 0x42; /* metadata test value */
be7: 48 8d 85 30 fc ff ff lea -0x3d0(%rbp),%rax
bee: c7 00 42 00 00 00 movl $0x42,(%rax)
- *((__u32 *)data + 4) = 0;
+ ((__u32 *)data)[1] = 0;
bf4: 48 8d 85 30 fc ff ff lea -0x3d0(%rbp),%rax
- bfb: 48 83 c0 10 add $0x10,%rax
+ bfb: 48 83 c0 04 add $0x4,%rax
bff: c7 00 00 00 00 00 movl $0x0,(%rax)--- tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c +2 -2
@@ -107,8 +107,8 @@ void test_xdp_do_redirect(void) .attach_point = BPF_TC_INGRESS);memcpy (&data[sizeof (__u64)], &pkt_udp,sizeof (pkt_udp)); - *((__u32 *)data) =0x42 ;/* metadata test value */ - *((__u32 *)data +4 ) =0 ; + ((__u32 *)data)[0 ] =0x42 ;/* metadata test value */ + ((__u32 *)data)[1 ] =0 ; skel =test_xdp_do_redirect__open ();if (!ASSERT_OK_PTR (skel,"skel" ))
Signature · 1 line
2.44.0
Quoted 22 lines · Michal Schmidt
Cast operation has a higher precedence than addition. The code here wants to zero the 2nd half of the 64-bit metadata, but due to a pointer arithmetic mistake, it writes the zero at offset 16 instead.
Just adding parentheses around "data + 4" would fix this, but I think this will be slightly better readable with array syntax.
I was unable to test this with
tools/testing/selftests/bpf/vmtest.sh, because my glibc is newer than glibc in the provided VM image. So I just checked the difference in the compiled code. objdump -Stools/testing/selftests/bpf/xdp_do_redirect.test.o:
- *((__u32 *)data) =0x42; /* metadata test value */
+ ((__u32*)data)[0] =0x42; /* metadata test value */be7: 48 8d 85 30 fc ff ff lea -0x3d0(%rbp),%rax bee: c7 00 42 00 00 00 movl $0x42,(%rax) - *((__u32 *)data + 4) = 0; + ((__u32 *)data)[1] = 0; bf4: 48 8d 85 30 fc ff ff lea -0x3d0(%rbp),%rax - bfb: 48 83 c0 10 add $0x10,%rax + bfb: 48 83 c0 04 add $0x4,%rax bff: c7 00 00 00 00 00 movl $0x0,(%rax)
Quoted 2 lines
Fixes 5640b6d89434 ("selftests/bpf: fix "metadata marker" getting overwritten by the netstack")Signed-off-by Michal Schmidt <[email protected]>
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Mon, 6 May 2024 16:50:22 +0200 you wrote:
Cast operation has a higher precedence than addition. The code here wants to zero the 2nd half of the 64-bit metadata, but due to a pointer arithmetic mistake, it writes the zero at offset 16 instead.
Just adding parentheses around "data + 4" would fix this, but I think this will be slightly better readable with array syntax.
[...]
Here is the summary with links:
- [net] selftests/bpf: fix pointer arithmetic in test_xdp_do_redirect
https://git.kernel.org/bpf/bpf-next/c/e549b39a0ab8
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
From: Michal Schmidt <[email protected]>
Date: Mon, 6 May 2024 16:50:22 +0200
Quoted 33 lines
Cast operation has a higher precedence than addition. The code here wants to zero the 2nd half of the 64-bit metadata, but due to a pointer arithmetic mistake, it writes the zero at offset 16 instead.
Just adding parentheses around "data + 4" would fix this, but I think this will be slightly better readable with array syntax.
I was unable to test this with
tools/testing/selftests/bpf/vmtest.sh, because my glibc is newer than glibc in the provided VM image. So I just checked the difference in the compiled code. objdump -Stools/testing/selftests/bpf/xdp_do_redirect.test.o:
- *((__u32 *)data) =0x42; /* metadata test value */
+ ((__u32*)data)[0] =0x42; /* metadata test value */be7: 48 8d 85 30 fc ff ff lea -0x3d0(%rbp),%rax bee: c7 00 42 00 00 00 movl $0x42,(%rax) - *((__u32 *)data + 4) = 0; + ((__u32 *)data)[1] = 0; bf4: 48 8d 85 30 fc ff ff lea -0x3d0(%rbp),%rax - bfb: 48 83 c0 10 add $0x10,%rax + bfb: 48 83 c0 04 add $0x4,%rax bff: c7 00 00 00 00 00 movl $0x0,(%rax)Fixes 5640b6d89434 ("selftests/bpf: fix "metadata marker" getting overwritten by the netstack")Signed-off-by Michal Schmidt <[email protected]>--- tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c +0
-0
tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c void test_xdp_do_redirect(void) · line 111
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c +2
-2@@ -107,8 +107,8 @@ void test_xdp_do_redirect(void) .attach_point = BPF_TC_INGRESS);memcpy (&data[sizeof (__u64)], &pkt_udp,sizeof (pkt_udp)); - *((__u32 *)data) =0x42 ;/* metadata test value */ - *((__u32 *)data +4 ) =0 ; + ((__u32 *)data)[0 ] =0x42 ;/* metadata test value */ + ((__u32 *)data)[1 ] =0 ;
Uff sorry. I was a bit in hurry to fix BPF CI and did this braino :z I know pointer arithms obviously :D
I'd give a Rev-by, but it was applied already. Thanks!
tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c void test_xdp_do_redirect(void) · line 114
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c +0
-0skel =test_xdp_do_redirect__open ();if (!ASSERT_OK_PTR (skel,"skel" ))
Thanks,
Olek