diff mbox series

[bpf-next,v3,4/4] selftests/bpf: Add tests for struct file related CRIB kfuncs

Message ID AM6PR03MB5848464E27435C30A51C8DDF99532@AM6PR03MB5848.eurprd03.prod.outlook.com (mailing list archive)
State New
Delegated to: BPF
Headers show
Series bpf/crib: Add open-coded style process file iterator and file related CRIB kfuncs | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-15 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-13 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc

Commit Message

Juntong Deng Nov. 6, 2024, 7:38 p.m. UTC
This patch adds test cases for struct file related CRIB kfuncs.

The test case for bpf_fget_task() is written based on
files_test_process(), so there should only be 3 opened files,
corresponding to file descriptors 0, 1, 2.

bpf_get_file_ops_type() currently only returns FILE_OPS_UNKNOWN,
so no test cases are needed for now.

In addition, this patch adds failure test cases where bpf programs
cannot pass the verifier due to untrusted pointer arguments.

Signed-off-by: Juntong Deng <juntong.deng@outlook.com>
---
 tools/testing/selftests/bpf/prog_tests/crib.c |  1 +
 .../testing/selftests/bpf/progs/crib_common.h |  4 ++
 .../selftests/bpf/progs/crib_files_failure.c  | 22 +++++++++
 .../selftests/bpf/progs/crib_files_success.c  | 46 +++++++++++++++++++
 4 files changed, 73 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/crib.c b/tools/testing/selftests/bpf/prog_tests/crib.c
index 48c5156504ad..5ef887e43170 100644
--- a/tools/testing/selftests/bpf/prog_tests/crib.c
+++ b/tools/testing/selftests/bpf/prog_tests/crib.c
@@ -108,6 +108,7 @@  static void run_files_success_test(const char *prog_name)
 
 static const char * const files_success_tests[] = {
 	"test_bpf_iter_task_file",
+	"test_bpf_fget_task",
 };
 
 void test_crib(void)
diff --git a/tools/testing/selftests/bpf/progs/crib_common.h b/tools/testing/selftests/bpf/progs/crib_common.h
index 93b8f9b1bdf8..0bc77d1b02b2 100644
--- a/tools/testing/selftests/bpf/progs/crib_common.h
+++ b/tools/testing/selftests/bpf/progs/crib_common.h
@@ -18,4 +18,8 @@  extern struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksy
 extern int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) __ksym;
 extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym;
 
+extern struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) __ksym;
+extern unsigned int bpf_get_file_ops_type(struct file *file) __ksym;
+extern void bpf_put_file(struct file *file) __ksym;
+
 #endif /* __CRIB_COMMON_H */
diff --git a/tools/testing/selftests/bpf/progs/crib_files_failure.c b/tools/testing/selftests/bpf/progs/crib_files_failure.c
index ebae01d87ff9..9360aad50c15 100644
--- a/tools/testing/selftests/bpf/progs/crib_files_failure.c
+++ b/tools/testing/selftests/bpf/progs/crib_files_failure.c
@@ -84,3 +84,25 @@  int bpf_iter_task_file_destroy_uninit_iter(void *ctx)
 
 	return 0;
 }
+
+SEC("syscall")
+__failure __msg("Possibly NULL pointer passed to trusted arg0")
+int bpf_fget_task_untrusted_file(void *ctx)
+{
+	struct task_struct *task = NULL;
+
+	bpf_fget_task(task, 1);
+
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("Possibly NULL pointer passed to trusted arg0")
+int bpf_get_file_ops_type_untrusted_file(void *ctx)
+{
+	struct file *file = NULL;
+
+	bpf_get_file_ops_type(file);
+
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/crib_files_success.c b/tools/testing/selftests/bpf/progs/crib_files_success.c
index 8de43dedbb02..f2e8becbfd04 100644
--- a/tools/testing/selftests/bpf/progs/crib_files_success.c
+++ b/tools/testing/selftests/bpf/progs/crib_files_success.c
@@ -71,3 +71,49 @@  int test_bpf_iter_task_file(void *ctx)
 	bpf_task_release(task);
 	return 0;
 }
+
+SEC("syscall")
+int test_bpf_fget_task(void *ctx)
+{
+	struct task_struct *task;
+	struct file *file;
+
+	task = bpf_task_from_vpid(pid);
+	if (task == NULL) {
+		err = 1;
+		return 0;
+	}
+
+	file = bpf_fget_task(task, 0);
+	if (file == NULL) {
+		err = 2;
+		goto cleanup;
+	}
+
+	bpf_put_file(file);
+
+	file = bpf_fget_task(task, 1);
+	if (file == NULL) {
+		err = 3;
+		goto cleanup;
+	}
+
+	bpf_put_file(file);
+
+	file = bpf_fget_task(task, 2);
+	if (file == NULL) {
+		err = 4;
+		goto cleanup;
+	}
+
+	bpf_put_file(file);
+
+	file = bpf_fget_task(task, 3);
+	if (file != NULL) {
+		err = 5;
+		bpf_put_file(file);
+	}
+cleanup:
+	bpf_task_release(task);
+	return 0;
+}