bpf/selftests: Test bpf_d_path on rdonly_mem.
Subsystems (3)
PATCH 1/1 bpf/selftests: Test bpf_d_path on rdonly_mem.
The second parameter of bpf_d_path() can only accept writable memories. rdonly_mem obtained from bpf_per_cpu_ptr() can not be passed into bpf_d_path for modification. This patch adds a selftest to verify this behavior.
--- .../testing/selftests/bpf/prog_tests/d_path.c | 22 +++++++++++++- .../bpf/progs/test_d_path_check_rdonly_mem.c | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c
tools/testing/selftests/bpf/prog_tests/d_path.c +21 -1
@@ -9,6 +9,7 @@#define MAX_FILES 7#include "test_d_path.skel.h" +#include "test_d_path_check_rdonly_mem.skel.h" static int duration;
@@ -99,7 +100,7 @@ static int trigger_fstat_events(pid_t pid)return ret; } -void test_d_path (void ) +static void test_d_path_basic (void ) {struct test_d_path__bss *bss;struct test_d_path *skel;
@@ -155,3 +156,22 @@ void test_d_path(void) cleanup:test_d_path__destroy (skel); } + +static void test_d_path_check_rdonly_mem (void ) +{ +struct test_d_path_check_rdonly_mem *skel; + + skel =test_d_path_check_rdonly_mem__open_and_load (); +ASSERT_ERR_PTR (skel,"unexpected load of a prog using d_path to write rdonly_mem\n" ); + +test_d_path_check_rdonly_mem__destroy (skel); +} + +void test_d_path (void ) +{ +if (test__start_subtest ("basic" )) +test_d_path_basic (); + +if (test__start_subtest ("check_rdonly_mem" )) +test_d_path_check_rdonly_mem (); +}
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ + +#include "vmlinux.h" + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +extern const int bpf_prog_active __ksym; + +SEC("fentry/security_inode_getattr") +int BPF_PROG (d_path_check_rdonly_mem ,struct path *path,struct kstat *stat, +__u32 request_mask,unsigned int query_flags) +{ +char *active; +__u32 cpu; + + cpu =bpf_get_smp_processor_id (); + active = (char *)bpf_per_cpu_ptr (&bpf_prog_active, cpu); +if (active) { +/* FAIL here! 'active' is a rdonly_mem. bpf helpers that +* update its arguments can not write into it. +*/ +bpf_d_path (path, active,sizeof (int )); + } +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
Quoted 36 lines · Hao Luo
The second parameter of
bpf_d_path()can only accept writable memories.rdonly_memobtained frombpf_per_cpu_ptr()can not be passed intobpf_d_pathfor modification. This patch adds a selftest to verify this behavior.Signed-off-by Hao Luo <[email protected]>--- .../testing/selftests/bpf/prog_tests/d_path.c | 22 +++++++++++++- .../bpf/progs/test_d_path_check_rdonly_mem.c | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.ctools/testing/selftests/bpf/prog_tests/d_path.c +2
-1@@ -9,6 +9,7 @@#define MAX_FILES 7#include "test_d_path.skel.h" +#include "test_d_path_check_rdonly_mem.skel.h" static int duration;@@ -99,7 +100,7 @@ static int trigger_fstat_events(pid_t pid)return ret; } -void test_d_path (void ) +static void test_d_path_basic (void ) {struct test_d_path__bss *bss;struct test_d_path *skel;@@ -155,3 +156,22 @@ void test_d_path(void) cleanup:test_d_path__destroy (skel);
tools/testing/selftests/bpf/prog_tests/d_path.c void test_d_path(void) · line 167
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +9
-0} + +static void test_d_path_check_rdonly_mem (void ) +{ +struct test_d_path_check_rdonly_mem *skel; + + skel =test_d_path_check_rdonly_mem__open_and_load (); +ASSERT_ERR_PTR (skel,"unexpected load of a prog using d_path to write rdonly_mem\n" ); + +test_d_path_check_rdonly_mem__destroy (skel);
You shouldn't call test_d_path_check_rdonly_mem__destroy(skel) if skel is an ERR_PTR. Maybe
if (!ASSERT_ERR_PTR(...)) test_d_path_check_rdonly_mem__destroy(skel);
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +10
-0+} + +void test_d_path (void ) +{ +if (test__start_subtest ("basic" )) +test_d_path_basic (); + +if (test__start_subtest ("check_rdonly_mem" )) +test_d_path_check_rdonly_mem (); +}tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +0
-0
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 6
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +6
-0@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ + +#include "vmlinux.h" + +#include "vmlinux.h"
duplicated vmlinux.h.
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 16
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +10
-0+#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +extern const int bpf_prog_active __ksym; + +SEC("fentry/security_inode_getattr") +int BPF_PROG (d_path_check_rdonly_mem ,struct path *path,struct kstat *stat, +__u32 request_mask,unsigned int query_flags) +{ +char *active;
int *active?
It may not matter since the program is rejected by the kernel but with making it conforms to kernel definition we have one less thing to worry about the verification.
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 20
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +4
-0+__u32 cpu; + + cpu =bpf_get_smp_processor_id (); + active = (char *)bpf_per_cpu_ptr (&bpf_prog_active, cpu);
int *
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 22
Quoted 2 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +2
-0+if (active) { +/* FAIL here! 'active' is a rdonly_mem. bpf helpers that
'active' points to readonly memory.
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +8
-0+* update its arguments can not write into it. +*/ +bpf_d_path (path, active,sizeof (int )); + } +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Quoted 53 lines · Yonghong Song
Quoted 36 lines · Hao Luo
The second parameter of
bpf_d_path()can only accept writable memories.rdonly_memobtained frombpf_per_cpu_ptr()can not be passed intobpf_d_pathfor modification. This patch adds a selftest to verify this behavior.Signed-off-by Hao Luo <[email protected]>--- .../testing/selftests/bpf/prog_tests/d_path.c | 22 +++++++++++++- .../bpf/progs/test_d_path_check_rdonly_mem.c | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.ctools/testing/selftests/bpf/prog_tests/d_path.c +2
-1@@ -9,6 +9,7 @@#define MAX_FILES 7#include "test_d_path.skel.h" +#include "test_d_path_check_rdonly_mem.skel.h" static int duration;@@ -99,7 +100,7 @@ static int trigger_fstat_events(pid_t pid)return ret; } -void test_d_path (void ) +static void test_d_path_basic (void ) {struct test_d_path__bss *bss;struct test_d_path *skel;@@ -155,3 +156,22 @@ void test_d_path(void) cleanup:test_d_path__destroy (skel);tools/testing/selftests/bpf/prog_tests/d_path.c void test_d_path(void) · line 167
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +9
-0} + +static void test_d_path_check_rdonly_mem (void ) +{ +struct test_d_path_check_rdonly_mem *skel; + + skel =test_d_path_check_rdonly_mem__open_and_load (); +ASSERT_ERR_PTR (skel,"unexpected load of a prog using d_path to write rdonly_mem\n" ); + +test_d_path_check_rdonly_mem__destroy (skel);You shouldn't call
test_d_path_check_rdonly_mem__destroy(skel)if skel is anERR_PTR. Maybe
Quoted 3 lines
if (!ASSERT_ERR_PTR(...))
test_d_path_check_rdonly_mem__destroy(skel);Ack. Will change that.
I don't know if it's only me: I find it confusing when figuring out what ASSERT_ERR_PTR(ptr) returns. Is the returned value 'ptr'? or 'ptr != NULL'? or 'err != 0'? I used to think ASSERT-like function/macro returns nothing.
I noticed that xxx__destroy has a check for NULL, so I put the destroy function unconditionally.
Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +10
-0+} + +void test_d_path (void ) +{ +if (test__start_subtest ("basic" )) +test_d_path_basic (); + +if (test__start_subtest ("check_rdonly_mem" )) +test_d_path_check_rdonly_mem (); +}tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +0
-0@@ -0,0 +1,30 @@
Quoted 9 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 6
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +6
-0+// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ + +#include "vmlinux.h" + +#include "vmlinux.h" duplicated
vmlinux.h.
Thanks. Will fix that.
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +7
-0+#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +extern const int bpf_prog_active __ksym; + +SEC("fentry/security_inode_getattr") +int BPF_PROG(d_path_check_rdonly_mem, struct path *path, struct kstat *stat,
Quoted 9 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 16
Quoted 3 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +3
-0+ __u32 request_mask, unsigned int query_flags) +{ + char *active;int *active?
It may not matter since the program is rejected by the kernel but with making it conforms to kernel definition we have one less thing to worry about the verification.
Because bpf_d_path() accepts 'char *' instead of 'int *', I need to cast 'active' to 'char *' somewhere, otherwise the compiler will issue a warning. To combine with your comment, maybe the following:
int *active;
active = (int *)bpf_per_cpu_ptr(...);
...bpf_d_path(path, (char *)active, sizeof(int));
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 20
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +4
-0+__u32 cpu; + + cpu =bpf_get_smp_processor_id (); + active = (char *)bpf_per_cpu_ptr (&bpf_prog_active, cpu);
Quoted 7 lines
int *
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 22
Quoted 2 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +2
-0+if (active) { +/* FAIL here! 'active' is a rdonly_mem. bpf helpers that 'active' points to readonly memory.
Ack.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +8
-0+* update its arguments can not write into it. +*/ +bpf_d_path (path, active,sizeof (int )); + } +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Quoted 33 lines · Hao Luo
Quoted 32 lines · Yonghong Song
Quoted 28 lines · Hao Luo
The second parameter of
bpf_d_path()can only accept writable memories.rdonly_memobtained frombpf_per_cpu_ptr()can not be passed intobpf_d_pathfor modification. This patch adds a selftest to verify this behavior.Signed-off-by Hao Luo <[email protected]>--- .../testing/selftests/bpf/prog_tests/d_path.c | 22 +++++++++++++- .../bpf/progs/test_d_path_check_rdonly_mem.c | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.ctools/testing/selftests/bpf/prog_tests/d_path.c +1
-0@@ -9,6 +9,7 @@#define MAX_FILES 7#include "test_d_path.skel.h" +#include "test_d_path_check_rdonly_mem.skel.h" static int duration;@@ -99,7 +100,7 @@ static int trigger_fstat_events(pid_t pid)return ret; }
[...]
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +5
-0+ +extern const int bpf_prog_active __ksym; + +SEC("fentry/security_inode_getattr") +int BPF_PROG(d_path_check_rdonly_mem, struct path *path, struct kstat *stat,Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/d_path.c
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +3
-0+ __u32 request_mask, unsigned int query_flags) +{ + char *active;int *active?
It may not matter since the program is rejected by the kernel but with making it conforms to kernel definition we have one less thing to worry about the verification.
Quoted 8 lines
Because
bpf_d_path()accepts 'char *' instead of 'int *', I need to cast 'active' to 'char *' somewhere, otherwise the compiler will issue a warning. To combine with your comment, maybe the following:int *active;
active = (int *)bpf_per_cpu_ptr(...);
...bpf_d_path(path, (char *)active, sizeof(int));
This is fine. Thanks!
Quoted 16 lines
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/d_path.c
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +4
-0+__u32 cpu; + + cpu =bpf_get_smp_processor_id (); + active = (char *)bpf_per_cpu_ptr (&bpf_prog_active, cpu);Quoted 7 lines
int *
tools/testing/selftests/bpf/prog_tests/d_path.c
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +2
-0+if (active) { +/* FAIL here! 'active' is a rdonly_mem. bpf helpers that 'active' points to readonly memory.
Ack.
[...]
Quoted 59 lines · Hao Luo
Quoted 53 lines · Yonghong Song
Quoted 36 lines · Hao Luo
The second parameter of
bpf_d_path()can only accept writable memories.rdonly_memobtained frombpf_per_cpu_ptr()can not be passed intobpf_d_pathfor modification. This patch adds a selftest to verify this behavior.Signed-off-by Hao Luo <[email protected]>--- .../testing/selftests/bpf/prog_tests/d_path.c | 22 +++++++++++++- .../bpf/progs/test_d_path_check_rdonly_mem.c | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.ctools/testing/selftests/bpf/prog_tests/d_path.c +2
-1@@ -9,6 +9,7 @@#define MAX_FILES 7#include "test_d_path.skel.h" +#include "test_d_path_check_rdonly_mem.skel.h" static int duration;@@ -99,7 +100,7 @@ static int trigger_fstat_events(pid_t pid)return ret; } -void test_d_path (void ) +static void test_d_path_basic (void ) {struct test_d_path__bss *bss;struct test_d_path *skel;@@ -155,3 +156,22 @@ void test_d_path(void) cleanup:test_d_path__destroy (skel);tools/testing/selftests/bpf/prog_tests/d_path.c void test_d_path(void) · line 167
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +9
-0} + +static void test_d_path_check_rdonly_mem (void ) +{ +struct test_d_path_check_rdonly_mem *skel; + + skel =test_d_path_check_rdonly_mem__open_and_load (); +ASSERT_ERR_PTR (skel,"unexpected load of a prog using d_path to write rdonly_mem\n" ); + +test_d_path_check_rdonly_mem__destroy (skel);You shouldn't call
test_d_path_check_rdonly_mem__destroy(skel)if skel is anERR_PTR. MaybeQuoted 3 lines
if (!ASSERT_ERR_PTR(...)) test_d_path_check_rdonly_mem__destroy(skel);
Quoted 1 line
Ack. Will change that.
no need, __destroy() handles NULLs and ERR_PTR just fine, the way you wrote it is totally correct (that's a deliberate nice feature of libbpf's "destructor" APIs)
Quoted 6 lines
I don't know if it's only me: I find it confusing when figuring out what
ASSERT_ERR_PTR(ptr)returns. Is the returned value 'ptr'? or 'ptr != NULL'? or 'err != 0'? I used to think ASSERT-like function/macro returns nothing.
You haven't looked at many other selftests, I presume. All the ASSERT_xxx() macros return true/false depending whether the assertion holds or not. ASSERT_ERR_PTR() checks that ptr *is* erroneous (which is NULL and ERR_PTR). If it's not, it returns false. So
if (!ASSERT_ERR_PTR(ptr, "short_descriptor"))
/* do something if assertion failed */
is a common pattern.
Note also "short_descriptor", it's not supposed to be a long descriptive sentences, it's sort of a "codename" of the particular check. It's not illegal to use space-separated sentence, but better to keep it short and identifier-like.
Quoted 48 lines
I noticed that
xxx__destroyhas a check for NULL, so I put the destroy function unconditionally.Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +10
-0+} + +void test_d_path (void ) +{ +if (test__start_subtest ("basic" )) +test_d_path_basic (); + +if (test__start_subtest ("check_rdonly_mem" )) +test_d_path_check_rdonly_mem (); +}tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +0
-0@@ -0,0 +1,30 @@Quoted 9 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 6
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +6
-0+// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ + +#include "vmlinux.h" + +#include "vmlinux.h" duplicated
vmlinux.h.Thanks. Will fix that.
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +7
-0+#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +extern const int bpf_prog_active __ksym; + +SEC("fentry/security_inode_getattr") +int BPF_PROG(d_path_check_rdonly_mem, struct path *path, struct kstat *stat,Quoted 9 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 16
Quoted 3 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +3
-0+ __u32 request_mask, unsigned int query_flags) +{ + char *active;int *active?
It may not matter since the program is rejected by the kernel but with making it conforms to kernel definition we have one less thing to worry about the verification.
Quoted 9 lines
Because
bpf_d_path()accepts 'char *' instead of 'int *', I need to cast 'active' to 'char *' somewhere, otherwise the compiler will issue a warning. To combine with your comment, maybe the following:int *active;
active = (int *)bpf_per_cpu_ptr(...);
...bpf_d_path(path, (char *)active, sizeof(int));
why not void *?
Quoted 23 lines
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 20
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +4
-0+__u32 cpu; + + cpu =bpf_get_smp_processor_id (); + active = (char *)bpf_per_cpu_ptr (&bpf_prog_active, cpu);Quoted 7 lines
int *
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 22
Quoted 2 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +2
-0+if (active) { +/* FAIL here! 'active' is a rdonly_mem. bpf helpers that 'active' points to readonly memory.
Ack.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +8
-0+* update its arguments can not write into it. +*/ +bpf_d_path (path, active,sizeof (int )); + } +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Quoted 61 lines · Andrii Nakryiko
Quoted 59 lines · Hao Luo
Quoted 56 lines · Yonghong Song
Quoted 36 lines · Hao Luo
The second parameter of
bpf_d_path()can only accept writable memories.rdonly_memobtained frombpf_per_cpu_ptr()can not be passed intobpf_d_pathfor modification. This patch adds a selftest to verify this behavior.Signed-off-by Hao Luo <[email protected]>--- .../testing/selftests/bpf/prog_tests/d_path.c | 22 +++++++++++++- .../bpf/progs/test_d_path_check_rdonly_mem.c | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.ctools/testing/selftests/bpf/prog_tests/d_path.c +2
-1@@ -9,6 +9,7 @@#define MAX_FILES 7#include "test_d_path.skel.h" +#include "test_d_path_check_rdonly_mem.skel.h" static int duration;@@ -99,7 +100,7 @@ static int trigger_fstat_events(pid_t pid)return ret; } -void test_d_path (void ) +static void test_d_path_basic (void ) {struct test_d_path__bss *bss;struct test_d_path *skel;@@ -155,3 +156,22 @@ void test_d_path(void) cleanup:test_d_path__destroy (skel);tools/testing/selftests/bpf/prog_tests/d_path.c void test_d_path(void) · line 167
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +9
-0} + +static void test_d_path_check_rdonly_mem (void ) +{ +struct test_d_path_check_rdonly_mem *skel; + + skel =test_d_path_check_rdonly_mem__open_and_load (); +ASSERT_ERR_PTR (skel,"unexpected load of a prog using d_path to write rdonly_mem\n" ); + +test_d_path_check_rdonly_mem__destroy (skel);You shouldn't call
test_d_path_check_rdonly_mem__destroy(skel)if skel is anERR_PTR. Maybeif (!ASSERT_ERR_PTR(...)) test_d_path_check_rdonly_mem__destroy(skel);
Quoted 6 lines
Quoted 1 line
Ack. Will change that.
no need,
__destroy()handles NULLs andERR_PTRjust fine, the way you wrote it is totally correct (that's a deliberate nice feature of libbpf's "destructor" APIs)
Yep. That's also my understanding.
Quoted 15 lines
Quoted 6 lines
I don't know if it's only me: I find it confusing when figuring out what
ASSERT_ERR_PTR(ptr)returns. Is the returned value 'ptr'? or 'ptr != NULL'? or 'err != 0'? I used to think ASSERT-like function/macro returns nothing.You haven't looked at many other selftests, I presume. All the
ASSERT_xxx()macros return true/false depending whether the assertion holds or not.ASSERT_ERR_PTR()checks that ptr *is* erroneous (which is NULL andERR_PTR). If it's not, it returns false. Soif (!ASSERT_ERR_PTR(ptr, "
short_descriptor"))
/* do something if assertion failed */
Quoted 7 lines
is a common pattern.
Note also "
short_descriptor", it's not supposed to be a long descriptive sentences, it's sort of a "codename" of the particular check. It's not illegal to use space-separated sentence, but better to keep it short and identifier-like.
I see. Thanks for the explanation.
Quoted 52 lines
Quoted 52 lines
I noticed that
xxx__destroyhas a check for NULL, so I put the destroy function unconditionally.Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +10
-0+} + +void test_d_path (void ) +{ +if (test__start_subtest ("basic" )) +test_d_path_basic (); + +if (test__start_subtest ("check_rdonly_mem" )) +test_d_path_check_rdonly_mem (); +}tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +0
-0@@ -0,0 +1,30 @@Quoted 9 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 6
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +6
-0+// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ + +#include "vmlinux.h" + +#include "vmlinux.h" duplicated
vmlinux.h.Thanks. Will fix that.
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +7
-0+#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +extern const int bpf_prog_active __ksym; + +SEC("fentry/security_inode_getattr") +int BPF_PROG(d_path_check_rdonly_mem, struct path *path, struct kstat *stat,Quoted 9 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 16
Quoted 3 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +3
-0+ __u32 request_mask, unsigned int query_flags) +{ + char *active;int *active?
It may not matter since the program is rejected by the kernel but with making it conforms to kernel definition we have one less thing to worry about the verification.Because
bpf_d_path()accepts 'char *' instead of 'int *', I need to cast 'active' to 'char *' somewhere, otherwise the compiler will issue a warning. To combine with your comment, maybe the following:
Quoted 8 lines
Quoted 5 lines
int *active;
active = (int *)bpf_per_cpu_ptr(...);
...bpf_d_path(path, (char *)active, sizeof(int));why not
void *?
'void *' works. Just haven't thought about that.
Quoted 23 lines
Quoted 23 lines
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 20
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +4
-0+__u32 cpu; + + cpu =bpf_get_smp_processor_id (); + active = (char *)bpf_per_cpu_ptr (&bpf_prog_active, cpu);Quoted 7 lines
int *
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c line 22
Quoted 2 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +2
-0+if (active) { +/* FAIL here! 'active' is a rdonly_mem. bpf helpers that 'active' points to readonly memory.
Ack.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_d_path_check_rdonly_mem.c new file +8
-0+* update its arguments can not write into it. +*/ +bpf_d_path (path, active,sizeof (int )); + } +return 0 ; +} + +char _license[] SEC("license") = "GPL";