1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020 Google LLC.
7 #include <test_progs.h>
15 #include "lsm_tailcall.skel.h"
17 char *CMD_ARGS[] = {"true", NULL};
19 #define GET_PAGE_ADDR(ADDR, PAGE_SIZE) \
20 (char *)(((unsigned long) (ADDR + PAGE_SIZE)) & ~(PAGE_SIZE-1))
22 int stack_mprotect(void)
28 sz = sysconf(_SC_PAGESIZE);
33 ret = mprotect(GET_PAGE_ADDR(buf, sz), sz,
34 PROT_READ | PROT_WRITE | PROT_EXEC);
38 int exec_cmd(int *monitored_pid)
40 int child_pid, child_status;
44 *monitored_pid = getpid();
45 execvp(CMD_ARGS[0], CMD_ARGS);
47 } else if (child_pid > 0) {
48 waitpid(child_pid, &child_status, 0);
55 static int test_lsm(struct lsm *skel)
57 struct bpf_link *link;
61 err = lsm__attach(skel);
62 if (!ASSERT_OK(err, "attach"))
65 /* Check that already linked program can't be attached again. */
66 link = bpf_program__attach(skel->progs.test_int_hook);
67 if (!ASSERT_ERR_PTR(link, "attach_link"))
70 err = exec_cmd(&skel->bss->monitored_pid);
71 if (!ASSERT_OK(err, "exec_cmd"))
74 ASSERT_EQ(skel->bss->bprm_count, 1, "bprm_count");
76 skel->bss->monitored_pid = getpid();
78 err = stack_mprotect();
79 if (!ASSERT_EQ(err, -1, "stack_mprotect") ||
80 !ASSERT_EQ(errno, EPERM, "stack_mprotect"))
83 ASSERT_EQ(skel->bss->mprotect_count, 1, "mprotect_count");
85 syscall(__NR_setdomainname, &buf, -2L);
86 syscall(__NR_setdomainname, 0, -3L);
87 syscall(__NR_setdomainname, ~0L, -4L);
89 ASSERT_EQ(skel->bss->copy_test, 3, "copy_test");
93 skel->bss->copy_test = 0;
94 skel->bss->bprm_count = 0;
95 skel->bss->mprotect_count = 0;
99 static void test_lsm_basic(void)
101 struct lsm *skel = NULL;
104 skel = lsm__open_and_load();
105 if (!ASSERT_OK_PTR(skel, "lsm_skel_load"))
108 err = test_lsm(skel);
109 if (!ASSERT_OK(err, "test_lsm_first_attach"))
112 err = test_lsm(skel);
113 ASSERT_OK(err, "test_lsm_second_attach");
119 static void test_lsm_tailcall(void)
121 struct lsm_tailcall *skel = NULL;
125 skel = lsm_tailcall__open_and_load();
126 if (!ASSERT_OK_PTR(skel, "lsm_tailcall__skel_load"))
129 map_fd = bpf_map__fd(skel->maps.jmp_table);
130 if (CHECK_FAIL(map_fd < 0))
133 prog_fd = bpf_program__fd(skel->progs.lsm_file_permission_prog);
134 if (CHECK_FAIL(prog_fd < 0))
138 err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
139 if (CHECK_FAIL(!err))
142 prog_fd = bpf_program__fd(skel->progs.lsm_file_alloc_security_prog);
143 if (CHECK_FAIL(prog_fd < 0))
146 err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
151 lsm_tailcall__destroy(skel);
154 void test_test_lsm(void)
156 if (test__start_subtest("lsm_basic"))
158 if (test__start_subtest("lsm_tailcall"))