1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020 Google LLC.
7 #include <test_progs.h>
16 char *CMD_ARGS[] = {"true", NULL};
18 #define GET_PAGE_ADDR(ADDR, PAGE_SIZE) \
19 (char *)(((unsigned long) (ADDR + PAGE_SIZE)) & ~(PAGE_SIZE-1))
21 static int duration = 0;
23 int stack_mprotect(void)
29 sz = sysconf(_SC_PAGESIZE);
34 ret = mprotect(GET_PAGE_ADDR(buf, sz), sz,
35 PROT_READ | PROT_WRITE | PROT_EXEC);
39 int exec_cmd(int *monitored_pid)
41 int child_pid, child_status;
45 *monitored_pid = getpid();
46 execvp(CMD_ARGS[0], CMD_ARGS);
48 } else if (child_pid > 0) {
49 waitpid(child_pid, &child_status, 0);
56 static int test_lsm(struct lsm *skel)
58 struct bpf_link *link;
62 err = lsm__attach(skel);
63 if (CHECK(err, "attach", "lsm attach failed: %d\n", err))
66 /* Check that already linked program can't be attached again. */
67 link = bpf_program__attach(skel->progs.test_int_hook);
68 if (CHECK(!IS_ERR(link), "attach_link",
69 "re-attach without detach should not succeed"))
72 err = exec_cmd(&skel->bss->monitored_pid);
73 if (CHECK(err < 0, "exec_cmd", "err %d errno %d\n", err, errno))
76 CHECK(skel->bss->bprm_count != 1, "bprm_count", "bprm_count = %d\n",
77 skel->bss->bprm_count);
79 skel->bss->monitored_pid = getpid();
81 err = stack_mprotect();
82 if (CHECK(errno != EPERM, "stack_mprotect", "want err=EPERM, got %d\n",
86 CHECK(skel->bss->mprotect_count != 1, "mprotect_count",
87 "mprotect_count = %d\n", skel->bss->mprotect_count);
89 syscall(__NR_setdomainname, &buf, -2L);
90 syscall(__NR_setdomainname, 0, -3L);
91 syscall(__NR_setdomainname, ~0L, -4L);
93 CHECK(skel->bss->copy_test != 3, "copy_test",
94 "copy_test = %d\n", skel->bss->copy_test);
98 skel->bss->copy_test = 0;
99 skel->bss->bprm_count = 0;
100 skel->bss->mprotect_count = 0;
104 void test_test_lsm(void)
106 struct lsm *skel = NULL;
109 skel = lsm__open_and_load();
110 if (CHECK(!skel, "lsm_skel_load", "lsm skeleton failed\n"))
113 err = test_lsm(skel);
114 if (CHECK(err, "test_lsm", "first attach failed\n"))
117 err = test_lsm(skel);
118 CHECK(err, "test_lsm", "second attach failed\n");