1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020 Google LLC.
7 #include <test_progs.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 void test_test_lsm(void)
57 struct lsm
*skel
= NULL
;
58 int err
, duration
= 0;
61 skel
= lsm__open_and_load();
62 if (CHECK(!skel
, "skel_load", "lsm skeleton failed\n"))
65 err
= lsm__attach(skel
);
66 if (CHECK(err
, "attach", "lsm attach failed: %d\n", err
))
69 err
= exec_cmd(&skel
->bss
->monitored_pid
);
70 if (CHECK(err
< 0, "exec_cmd", "err %d errno %d\n", err
, errno
))
73 CHECK(skel
->bss
->bprm_count
!= 1, "bprm_count", "bprm_count = %d\n",
74 skel
->bss
->bprm_count
);
76 skel
->bss
->monitored_pid
= getpid();
78 err
= stack_mprotect();
79 if (CHECK(errno
!= EPERM
, "stack_mprotect", "want err=EPERM, got %d\n",
83 CHECK(skel
->bss
->mprotect_count
!= 1, "mprotect_count",
84 "mprotect_count = %d\n", skel
->bss
->mprotect_count
);
86 syscall(__NR_setdomainname
, &buf
, -2L);
87 syscall(__NR_setdomainname
, 0, -3L);
88 syscall(__NR_setdomainname
, ~0L, -4L);
90 CHECK(skel
->bss
->copy_test
!= 3, "copy_test",
91 "copy_test = %d\n", skel
->bss
->copy_test
);