diff mbox series

[RFC,bpf-next,2/2] selftests/bpf: Add selftest for bpf_current_capable

Message ID 20230814143341.3767-3-laoar.shao@gmail.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series bpf: Add a new kfunc bpf_current_capable | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 3 maintainers not CCed: mykolal@fb.com shuah@kernel.org linux-kselftest@vger.kernel.org
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 81 exceeds 80 columns WARNING: line length of 83 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc

Commit Message

Yafang Shao Aug. 14, 2023, 2:33 p.m. UTC
Add testcase for the newly added bpf_current_capable.

The result:
  $ tools/testing/selftests/bpf/test_progs --name=bpf_current_cap
  #13      bpf_current_cap:OK
  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 .../selftests/bpf/prog_tests/bpf_current_cap.c     | 80 ++++++++++++++++++++++
 .../selftests/bpf/progs/test_bpf_current_cap.c     | 37 ++++++++++
 2 files changed, 117 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_current_cap.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_bpf_current_cap.c
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_current_cap.c b/tools/testing/selftests/bpf/prog_tests/bpf_current_cap.c
new file mode 100644
index 0000000..6952908
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_current_cap.c
@@ -0,0 +1,80 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */
+
+#include <linux/perf_event.h>
+#include <linux/bpf.h>
+#include <test_progs.h>
+#include "cap_helpers.h"
+#include "test_bpf_current_cap.skel.h"
+
+void serial_test_bpf_current_cap(void)
+{
+	struct test_bpf_current_cap *skel;
+	struct perf_event_attr attr = {};
+	int pfd, link_fd, err;
+	__u64 caps = 0;
+
+	skel = test_bpf_current_cap__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	attr.size = sizeof(attr);
+	attr.type = PERF_TYPE_SOFTWARE;
+	attr.config = PERF_COUNT_SW_CPU_CLOCK;
+	attr.freq = 1;
+	attr.sample_freq = 1000;
+	pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
+	if (!ASSERT_GE(pfd, 0, "perf_event_open"))
+		goto cleanup;
+
+	/* In case CAP_BPF and CAP_PERFMON is not set */
+	err = cap_enable_effective(1ULL << CAP_BPF | 1ULL << CAP_PERFMON, &caps);
+	if (!ASSERT_OK(err, "set_cap_bpf_perfmon"))
+		goto close_perf;
+
+	err = cap_disable_effective(1ULL << CAP_SYS_ADMIN, NULL);
+	if (!ASSERT_OK(err, "disable_cap_sys_admin"))
+		goto restore_cap;
+
+	link_fd = bpf_link_create(bpf_program__fd(skel->progs.perf_event_run), pfd,
+				  BPF_PERF_EVENT, NULL);
+	if (!ASSERT_GE(link_fd, 0, "link_create_without_lsm"))
+		goto restore_cap;
+	close(link_fd);
+	ASSERT_EQ(skel->bss->cap_sys_admin, false, "cap_sys_admin_init_value");
+	ASSERT_EQ(skel->bss->cap_bpf, false, "cap_bpf_init_value");
+	ASSERT_EQ(skel->bss->cap_perfmon, false, "cap_perfmon_init_value");
+
+	skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run);
+	if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach"))
+		goto restore_cap;
+
+	link_fd = bpf_link_create(bpf_program__fd(skel->progs.perf_event_run), pfd,
+				  BPF_PERF_EVENT, NULL);
+	if (!ASSERT_LE(link_fd, 0, "link_create_without_sys_admin"))
+		goto restore_cap;
+	ASSERT_EQ(skel->bss->cap_sys_admin, false, "cap_sys_admin_disable");
+	ASSERT_EQ(skel->bss->cap_bpf, true, "cap_bpf_enable");
+	ASSERT_EQ(skel->bss->cap_perfmon, true, "cap_perfmon_enable");
+
+	err = cap_enable_effective(1ULL << CAP_SYS_ADMIN, NULL);
+	if (!ASSERT_OK(err, "enable_cap_sys_admin"))
+		goto restore_cap;
+
+	link_fd = bpf_link_create(bpf_program__fd(skel->progs.perf_event_run), pfd,
+				  BPF_PERF_EVENT, NULL);
+	if (!ASSERT_GE(link_fd, 0, "link_create_with_sys_admin"))
+		goto restore_cap;
+	close(link_fd);
+	ASSERT_EQ(skel->bss->cap_sys_admin, true, "cap_sys_admin_enable");
+	ASSERT_EQ(skel->bss->cap_bpf, true, "cap_bpf_enable");
+	ASSERT_EQ(skel->bss->cap_perfmon, true, "cap_perfmon_enable");
+
+restore_cap:
+	if (caps)
+		cap_enable_effective(caps, NULL);
+close_perf:
+	close(pfd);
+cleanup:
+	test_bpf_current_cap__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_bpf_current_cap.c b/tools/testing/selftests/bpf/progs/test_bpf_current_cap.c
new file mode 100644
index 0000000..6dd97a1
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_bpf_current_cap.c
@@ -0,0 +1,37 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#define CAP_SYS_ADMIN	21
+#define CAP_PERFMON	38
+#define CAP_BPF		39
+
+extern bool bpf_current_capable(int cap) __ksym;
+bool cap_sys_admin, cap_bpf, cap_perfmon;
+
+int link_create_audit(union bpf_attr *attr)
+{
+	cap_bpf = bpf_current_capable(CAP_BPF);
+	cap_perfmon = bpf_current_capable(CAP_PERFMON);
+	cap_sys_admin = bpf_current_capable(CAP_SYS_ADMIN);
+	return cap_sys_admin ? 0 : -1;
+}
+
+SEC("lsm/bpf")
+int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
+{
+	if (cmd != BPF_LINK_CREATE)
+		return 0;
+	return link_create_audit(attr);
+}
+
+SEC("perf_event")
+int BPF_PROG(perf_event_run)
+{
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";