Re: [PATCH bpf-next] bpf/selftests: Test bpf_d_path on rdonly_mem.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 12/21/21 12:16 PM, Hao Luo wrote:
On Mon, Dec 20, 2021 at 8:28 PM Yonghong Song <yhs@xxxxxx> wrote:



On 12/20/21 12:12 PM, Hao Luo wrote:
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.

Signed-off-by: Hao Luo <haoluo@xxxxxxxxxx>
---
   .../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

diff --git a/tools/testing/selftests/bpf/prog_tests/d_path.c b/tools/testing/selftests/bpf/prog_tests/d_path.c
index 0a577a248d34..f8d8c5a5dfba 100644
--- a/tools/testing/selftests/bpf/prog_tests/d_path.c
+++ b/tools/testing/selftests/bpf/prog_tests/d_path.c
@@ -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;
   }

[...]
+
+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.


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!


+     __u32 cpu;
+
+     cpu = bpf_get_smp_processor_id();
+     active = (char *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);

int *

+     if (active) {
+             /* FAIL here! 'active' is a rdonly_mem. bpf helpers that

'active' points to readonly memory.


Ack.

[...]



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux