Skip to contentSkip to conversation

selftests/bpf: Fix stat probe in d_path test

PATCH Superseded Jiri Olsa 4 participants 6 messages Sep 16, 2020 – Sep 18, 2020

Subsystems (8)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
Acked-by on v1 1/1
#1 lore raw
In reply to Jiri Olsa
Quoted 10 lines · Jiri Olsa

Some kernels builds might inline vfs_getattr call within fstat syscall code path, so fentry/vfs_getattr trampoline is not called.

Alexei suggested [1] we should use security_inode_getattr instead, because it's less likely to get inlined.

Adding security_inode_getattr to the d_path allowed list and switching the stat trampoline to security_inode_getattr.

Quoted 7 lines

Adding flags that indicate trampolines were called and failing the test if any of them got missed, so it's easier to identify the issue next time.

[1] https://lore.kernel.org/bpf/CAADnVQJ0FchoPqNWm+dEppyij-MOvvEG_trEfyrHdabtcEuZGg@mail.gmail.com/

Fixes e4d1af4b16f8 ("selftests/bpf: Add test for d_path helper")
Quoted 23 lines
---
 kernel/trace/bpf_trace.c                        | 1 +
 tools/testing/selftests/bpf/prog_tests/d_path.c | 6 ++++++
 tools/testing/selftests/bpf/progs/test_d_path.c | 9 ++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +1 -0
index b2a5380eb187..1001c053ebb3 100644
@@ -1122,6 +1122,7 @@ BTF_ID(func, vfs_truncate)
 BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
+BTF_ID(func, security_inode_getattr)
 BTF_ID(func, filp_close)
 BTF_SET_END(btf_allowlist_d_path)
 
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index fc12e0d445ff..f507f1a6fa3a 100644

tools/testing/selftests/bpf/prog_tests/d_path.c void test_d_path(void) · line 126

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +4 -0
@@ -120,6 +120,12 @@ void test_d_path(void)
        if (err < 0)
                goto cleanup;
 
+       if (CHECK(!bss->called_stat || !bss->called_close,
+                 "check",
+                 "failed to call trampolines called_stat %d, bss->called_close %d\n",
+                  bss->called_stat, bss->called_close))

optional:

maybe it's better to add two separate checks with specific error messages?

"stat", "trampoline for security_inode_getattr was not called\n" "close", "trampoline for filp_close was not called\n"

I think this would make the output more readable.

- KP

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +2 -0
+               goto cleanup;
+
        for (int i = 0; i < MAX_FILES; i++) {
                CHECK(strncmp(src.paths[i], bss->paths_stat[i], MAX_PATH_LEN),
                      "check",

[...]

Quoted 6 lines
        if (pid != my_pid)
                return 0;

--
2.26.2

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 selftests/bpf: Fix stat probe in d_path test

Some kernels builds might inline vfs_getattr call within fstat syscall code path, so fentry/vfs_getattr trampoline is not called.

Alexei suggested [1] we should use security_inode_getattr instead, because it's less likely to get inlined.

Adding security_inode_getattr to the d_path allowed list and switching the stat trampoline to security_inode_getattr.

Adding flags that indicate trampolines were called and failing the test if any of them got missed, so it's easier to identify the issue next time.

[1] https://lore.kernel.org/bpf/CAADnVQJ0FchoPqNWm+dEppyij-MOvvEG_trEfyrHdabtcEuZGg@mail.gmail.com/

Fixes e4d1af4b16f8 ("selftests/bpf: Add test for d_path helper")
---
 kernel/trace/bpf_trace.c                        | 1 +
 tools/testing/selftests/bpf/prog_tests/d_path.c | 6 ++++++
 tools/testing/selftests/bpf/progs/test_d_path.c | 9 ++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +1 -0
index b2a5380eb187..1001c053ebb3 100644
@@ -1122,6 +1122,7 @@ BTF_ID(func, vfs_truncate)
 BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
+BTF_ID(func, security_inode_getattr)
 BTF_ID(func, filp_close)
 BTF_SET_END(btf_allowlist_d_path)
 
tools/testing/selftests/bpf/prog_tests/d_path.c +6 -0
index fc12e0d445ff..f507f1a6fa3a 100644
@@ -120,6 +120,12 @@ void test_d_path(void)
 	if (err < 0)
 		goto cleanup;
 
+	if (CHECK(!bss->called_stat || !bss->called_close,
+		  "check",
+		  "failed to call trampolines called_stat %d, bss->called_close %d\n",
+		   bss->called_stat, bss->called_close))
+		goto cleanup;
+
 	for (int i = 0; i < MAX_FILES; i++) {
 		CHECK(strncmp(src.paths[i], bss->paths_stat[i], MAX_PATH_LEN),
 		      "check",
tools/testing/selftests/bpf/progs/test_d_path.c +8 -1
index 61f007855649..84e1f883f97b 100644
@@ -15,7 +15,10 @@ char paths_close[MAX_FILES][MAX_PATH_LEN] = {};
 int rets_stat[MAX_FILES] = {};
 int rets_close[MAX_FILES] = {};
 
-SEC("fentry/vfs_getattr")
+int called_stat = 0;
+int called_close = 0;
+
+SEC("fentry/security_inode_getattr")
 int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
 	     __u32 request_mask, unsigned int query_flags)
 {
@@ -23,6 +26,8 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
 	__u32 cnt = cnt_stat;
 	int ret;
 
+	called_stat = 1;
+
 	if (pid != my_pid)
 		return 0;
 
@@ -42,6 +47,8 @@ int BPF_PROG(prog_close, struct file *file, void *id)
 	__u32 cnt = cnt_close;
 	int ret;
 
+	called_close = 1;
+
 	if (pid != my_pid)
 		return 0;
 
Signature · 1 line
2.26.2
#2 lore raw
Quoted 27 lines · Jiri Olsa

Some kernels builds might inline vfs_getattr call within fstat syscall code path, so fentry/vfs_getattr trampoline is not called.

Alexei suggested [1] we should use security_inode_getattr instead, because it's less likely to get inlined.

Adding security_inode_getattr to the d_path allowed list and switching the stat trampoline to security_inode_getattr.

Adding flags that indicate trampolines were called and failing the test if any of them got missed, so it's easier to identify the issue next time.

[1] https://lore.kernel.org/bpf/CAADnVQJ0FchoPqNWm+dEppyij-MOvvEG_trEfyrHdabtcEuZGg@mail.gmail.com/

Fixes e4d1af4b16f8 ("selftests/bpf: Add test for d_path helper")
---
 kernel/trace/bpf_trace.c                        | 1 +
 tools/testing/selftests/bpf/prog_tests/d_path.c | 6 ++++++
 tools/testing/selftests/bpf/progs/test_d_path.c | 9 ++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +0 -0
index b2a5380eb187..1001c053ebb3 100644

kernel/trace/bpf_trace.c BTF_ID(func, vfs_truncate) · line 1127

Quoted 6 lines
kernel/trace/bpf_trace.c +1 -0
@@ -1122,6 +1122,7 @@ BTF_ID(func, vfs_truncate)
 BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
+BTF_ID(func, security_inode_getattr)
 BTF_ID(func, filp_close)
 BTF_SET_END(btf_allowlist_d_path)

I think it's concealing the problem instead of fixing it. bpf is difficult to use for many reasons. Let's not make it harder. The users will have a very hard time debugging why vfs_getattr bpf probe is not called in all cases.
Let's replace:
vfs_truncate -> security_path_truncate
vfs_fallocate -> security_file_permission
vfs_getattr -> security_inode_getattr

For dentry_open also add security_file_open. dentry_open and filp_close are in its own files, so unlikely to be inlined.
Ideally resolve_btfids would parse dwarf info and check whether any of the funcs in allowlist were inlined. That would be more reliable, but not pretty to drag libdw dependency into resolve_btfids.

tools/testing/selftests/bpf/prog_tests/d_path.c void test_d_path(void) · line 123

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +1 -0
index fc12e0d445ff..f507f1a6fa3a 100644
@@ -120,6 +120,12 @@ void test_d_path(void)
 	if (err < 0)
 		goto cleanup;
 
+	if (CHECK(!bss->called_stat || !bss->called_close,

+1 to KP's comment.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +4 -0
+		  "check",
+		  "failed to call trampolines called_stat %d, bss->called_close %d\n",
+		   bss->called_stat, bss->called_close))
+		goto cleanup;
#3 lore raw
Quoted 44 lines · Alexei Starovoitov
Quoted 27 lines · Jiri Olsa

Some kernels builds might inline vfs_getattr call within fstat syscall code path, so fentry/vfs_getattr trampoline is not called.

Alexei suggested [1] we should use security_inode_getattr instead, because it's less likely to get inlined.

Adding security_inode_getattr to the d_path allowed list and switching the stat trampoline to security_inode_getattr.

Adding flags that indicate trampolines were called and failing the test if any of them got missed, so it's easier to identify the issue next time.

[1] https://lore.kernel.org/bpf/CAADnVQJ0FchoPqNWm+dEppyij-MOvvEG_trEfyrHdabtcEuZGg@mail.gmail.com/

Fixes e4d1af4b16f8 ("selftests/bpf: Add test for d_path helper")
---
 kernel/trace/bpf_trace.c                        | 1 +
 tools/testing/selftests/bpf/prog_tests/d_path.c | 6 ++++++
 tools/testing/selftests/bpf/progs/test_d_path.c | 9 ++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +0 -0
index b2a5380eb187..1001c053ebb3 100644

kernel/trace/bpf_trace.c BTF_ID(func, vfs_truncate) · line 1127

Quoted 6 lines
kernel/trace/bpf_trace.c +1 -0
@@ -1122,6 +1122,7 @@ BTF_ID(func, vfs_truncate)
 BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
+BTF_ID(func, security_inode_getattr)
 BTF_ID(func, filp_close)
 BTF_SET_END(btf_allowlist_d_path)

I think it's concealing the problem instead of fixing it. bpf is difficult to use for many reasons. Let's not make it harder. The users will have a very hard time debugging why vfs_getattr bpf probe is not called in all cases.
Let's replace:
vfs_truncate -> security_path_truncate
vfs_fallocate -> security_file_permission
vfs_getattr -> security_inode_getattr

Quoted 3 lines

For dentry_open also add security_file_open.
dentry_open and filp_close are in its own files,
so unlikely to be inlined.

ok

Quoted 4 lines

Ideally resolve_btfids would parse dwarf info and check whether any of the funcs in allowlist were inlined. That would be more reliable, but not pretty to drag libdw dependency into resolve_btfids.

hm, we could add some check to perf|bpftrace that would show you all the places where function is called from and if it was inlined or is a regular call.. so user is aware what probe calls to expect

Quoted 10 lines
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index fc12e0d445ff..f507f1a6fa3a 100644
@@ -120,6 +120,12 @@ void test_d_path(void)
 	if (err < 0)
 		goto cleanup;
 
Quoted 3 lines

tools/testing/selftests/bpf/prog_tests/d_path.c void test_d_path(void) · line 123

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/d_path.c +1 -0
+	if (CHECK(!bss->called_stat || !bss->called_close,

+1 to KP's comment.

ok

thanks,
jirka

#4 lore raw
Quoted 10 lines · Jiri Olsa
Quoted 5 lines

Ideally resolve_btfids would parse dwarf info and check whether any of the funcs in allowlist were inlined. That would be more reliable, but not pretty to drag libdw dependency into resolve_btfids.

hm, we could add some check to perf|bpftrace that would show you all the places where function is called from and if it was inlined or is a regular call.. so user is aware what probe calls to expect

The check like this belongs in some library,
but making libbpf depend on dwarf is not great.
I think we're at the point where we need to break libbpf into many libraries. This one could be called libbpftrace. It would potentially include symbolizer and other dwarf related operations.
Such inlining check would be good to do not only for d_path allowlist, but for any kprobe/fentry function.

#5 lore raw
Quoted 12 lines · Alexei Starovoitov
Quoted 10 lines · Jiri Olsa
Quoted 5 lines

Ideally resolve_btfids would parse dwarf info and check whether any of the funcs in allowlist were inlined. That would be more reliable, but not pretty to drag libdw dependency into resolve_btfids.

hm, we could add some check to perf|bpftrace that would show you all the places where function is called from and if it was inlined or is a regular call.. so user is aware what probe calls to expect

Quoted 6 lines

The check like this belongs in some library,
but making libbpf depend on dwarf is not great.
I think we're at the point where we need to break libbpf into many libraries. This one could be called libbpftrace. It would potentially include symbolizer and other dwarf related operations.

ok

Quoted 2 lines

Such inlining check would be good to do not only for d_path allowlist, but for any kprobe/fentry function.

yes, that's what I meant

jirka