[PATCH bpf v1 4/5] selftests/bpf: Add selftest for PTR_TO_BTF_ID NULL + off case

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

 



While at it, also try breaking bpf_sock_from_file, since it doesn't
check its argument for NULL in the first place. With our fix, both
shouldn't crash.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx>
---
 .../selftests/bpf/prog_tests/d_path_crash.c   | 19 ++++++++++++++
 .../selftests/bpf/progs/d_path_crash.c        | 26 +++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/d_path_crash.c
 create mode 100644 tools/testing/selftests/bpf/progs/d_path_crash.c

diff --git a/tools/testing/selftests/bpf/prog_tests/d_path_crash.c b/tools/testing/selftests/bpf/prog_tests/d_path_crash.c
new file mode 100644
index 000000000000..b1ee705d2108
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/d_path_crash.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "d_path_crash.skel.h"
+
+void test_d_path_crash(void)
+{
+	struct d_path_crash *skel;
+
+	skel = d_path_crash__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "d_path_crash__open_and_load"))
+		return;
+	skel->bss->pid = getpid();
+	ASSERT_OK(d_path_crash__attach(skel), "d_path__attach");
+	close(open("/dev/null", O_RDONLY));
+	d_path_crash__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/d_path_crash.c b/tools/testing/selftests/bpf/progs/d_path_crash.c
new file mode 100644
index 000000000000..a4b1a8b200f3
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/d_path_crash.c
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+
+int pid = 0;
+
+SEC("lsm/file_open")
+int BPF_PROG(lsm_file_open, struct file *file)
+{
+	struct task_struct *current = bpf_get_current_task_btf();
+	unsigned long *val, l;
+	char buf[64] = {};
+	struct file *f;
+
+	if (current->tgid != pid)
+		return 0;
+
+	f = current->files->fd_array[63];
+	bpf_d_path(&f->f_path, buf, sizeof(buf));
+	/* If we survived, let's try our luck here */
+	bpf_sock_from_file(f);
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.35.1




[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