Skip to contentSkip to conversation

selftests/bpf: Fix flaky test ptr_untrusted

PATCH Applied Yonghong Song 3 participants 4 messages Feb 4, 2024 – Feb 6, 2024

Subsystems (3)
lore
Share & tools
lore

Yonghong Song

author reviewer
#0 lore raw

PATCH 1/1 selftests/bpf: Fix flaky test ptr_untrusted

Somehow recently I frequently hit the following test failure
with either ./test_progs or ./test_progs-cpuv4:
serial_test_ptr_untrusted:PASS:skel_open 0 nsec serial_test_ptr_untrusted:PASS:lsm_attach 0 nsec serial_test_ptr_untrusted:PASS:raw_tp_attach 0 nsec serial_test_ptr_untrusted:FAIL:cmp_tp_name unexpected cmp_tp_name: actual -115 != expected 0
#182 ptr_untrusted:FAIL

Further investigation found the failure is due to
bpf_probe_read_user_str()
where reading user-level string attr->raw_tracepoint.name is not successfully, most likely due to the
string itself still in disk and not populated into memory yet.

One solution is do a printf() call of the string before doing bpf syscall which will force the raw_tracepoint.name into memory. But I think a more robust solution is to use bpf_copy_from_user() which is used in sleepable program and can tolerate page fault, and the fix here used the latter approach.

---
 tools/testing/selftests/bpf/progs/test_ptr_untrusted.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/progs/test_ptr_untrusted.c +3 -3
index 4bdd65b5aa2d..2fdc44e76624 100644
@@ -6,13 +6,13 @@
 
 char tp_name[128];
 
-SEC("lsm/bpf")
+SEC("lsm.s/bpf")
 int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
 {
 	switch (cmd) {
 	case BPF_RAW_TRACEPOINT_OPEN:
-		bpf_probe_read_user_str(tp_name, sizeof(tp_name) - 1,
-					(void *)attr->raw_tracepoint.name);
+		bpf_copy_from_user(tp_name, sizeof(tp_name) - 1,
+				   (void *)attr->raw_tracepoint.name);
 		break;
 	default:
 		break;
Signature · 1 line
2.34.1
#1 lore raw
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 9 lines · On Sun, 4 Feb 2024 11:44:52 -0800 you wrote:

Somehow recently I frequently hit the following test failure
with either ./test_progs or ./test_progs-cpuv4:
serial_test_ptr_untrusted:PASS:skel_open 0 nsec serial_test_ptr_untrusted:PASS:lsm_attach 0 nsec serial_test_ptr_untrusted:PASS:raw_tp_attach 0 nsec serial_test_ptr_untrusted:FAIL:cmp_tp_name unexpected cmp_tp_name: actual -115 != expected 0
#182 ptr_untrusted:FAIL

[...]

Here is the summary with links:
- [bpf-next] selftests/bpf: Fix flaky test ptr_untrusted

    https://git.kernel.org/bpf/bpf-next/c/7e428638bd78

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#2 lore raw
In reply to Yonghong Song
Quoted 34 lines · Yonghong Song

Somehow recently I frequently hit the following test failure
with either ./test_progs or ./test_progs-cpuv4:
serial_test_ptr_untrusted:PASS:skel_open 0 nsec serial_test_ptr_untrusted:PASS:lsm_attach 0 nsec serial_test_ptr_untrusted:PASS:raw_tp_attach 0 nsec serial_test_ptr_untrusted:FAIL:cmp_tp_name unexpected cmp_tp_name: actual -115 != expected 0
#182 ptr_untrusted:FAIL

Further investigation found the failure is due to
bpf_probe_read_user_str()
where reading user-level string attr->raw_tracepoint.name is not successfully, most likely due to the
string itself still in disk and not populated into memory yet.

One solution is do a printf() call of the string before doing bpf syscall which will force the raw_tracepoint.name into memory. But I think a more robust solution is to use bpf_copy_from_user() which is used in sleepable program and can tolerate page fault, and the fix here used the latter approach.

---
 tools/testing/selftests/bpf/progs/test_ptr_untrusted.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/progs/test_ptr_untrusted.c +0 -0
index 4bdd65b5aa2d..2fdc44e76624 100644
@@ -6,13 +6,13 @@
 
 char tp_name[128];
 

tools/testing/selftests/bpf/progs/test_ptr_untrusted.c line 15

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_ptr_untrusted.c +3 -3
-SEC("lsm/bpf")
+SEC("lsm.s/bpf")
 int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
 {
        switch (cmd) {
        case BPF_RAW_TRACEPOINT_OPEN:
-               bpf_probe_read_user_str(tp_name, sizeof(tp_name) - 1,
-                                       (void *)attr->raw_tracepoint.name);
+               bpf_copy_from_user(tp_name, sizeof(tp_name) - 1,
+                                  (void *)attr->raw_tracepoint.name);

Should we also add bpf_copy_from_user_str (and bpf_copy_from_user_str_task) kfuncs to complete bpf_copy_from_user? This change is not strictly equivalent (though for tests it's fine, but in real-world apps it would be problematic).

Quoted 6 lines
tools/testing/selftests/bpf/progs/test_ptr_untrusted.c +0 -1
                break;
        default:
                break;
--

2.34.1

author reviewer
#3 lore raw
Quoted 34 lines · Andrii Nakryiko
Quoted 33 lines · Yonghong Song

Somehow recently I frequently hit the following test failure
with either ./test_progs or ./test_progs-cpuv4:
serial_test_ptr_untrusted:PASS:skel_open 0 nsec serial_test_ptr_untrusted:PASS:lsm_attach 0 nsec serial_test_ptr_untrusted:PASS:raw_tp_attach 0 nsec serial_test_ptr_untrusted:FAIL:cmp_tp_name unexpected cmp_tp_name: actual -115 != expected 0
#182 ptr_untrusted:FAIL

Further investigation found the failure is due to
bpf_probe_read_user_str()
where reading user-level string attr->raw_tracepoint.name is not successfully, most likely due to the
string itself still in disk and not populated into memory yet.

One solution is do a printf() call of the string before doing bpf syscall which will force the raw_tracepoint.name into memory. But I think a more robust solution is to use bpf_copy_from_user() which is used in sleepable program and can tolerate page fault, and the fix here used the latter approach.

---
  tools/testing/selftests/bpf/progs/test_ptr_untrusted.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/progs/test_ptr_untrusted.c +0 -0
index 4bdd65b5aa2d..2fdc44e76624 100644
@@ -6,13 +6,13 @@
 
  char tp_name[128];
 
Quoted 14 lines

tools/testing/selftests/bpf/progs/test_ptr_untrusted.c line 15

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_ptr_untrusted.c +3 -3
-SEC("lsm/bpf")
+SEC("lsm.s/bpf")
  int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
  {
         switch (cmd) {
         case BPF_RAW_TRACEPOINT_OPEN:
-               bpf_probe_read_user_str(tp_name, sizeof(tp_name) - 1,
-                                       (void *)attr->raw_tracepoint.name);
+               bpf_copy_from_user(tp_name, sizeof(tp_name) - 1,
+                                  (void *)attr->raw_tracepoint.name);

Should we also add bpf_copy_from_user_str (and bpf_copy_from_user_str_task) kfuncs to complete bpf_copy_from_user? This change is not strictly equivalent (though for tests it's fine, but in real-world apps it would be problematic).

Sounds a good idea. Let me do some investigations!

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/test_ptr_untrusted.c +0 -1
                 break;
         default:
                 break;
--

2.34.1