Blame


1 8951cb19 2022-07-01 op #include <linux/landlock.h>
2 8951cb19 2022-07-01 op #include <linux/prctl.h>
3 8951cb19 2022-07-01 op #include <stdlib.h>
4 8951cb19 2022-07-01 op #include <sys/prctl.h>
5 8951cb19 2022-07-01 op #include <sys/syscall.h>
6 8951cb19 2022-07-01 op #include <unistd.h>
7 8951cb19 2022-07-01 op #include <stdint.h>
8 8951cb19 2022-07-01 op
9 8951cb19 2022-07-01 op #ifndef landlock_create_ruleset
10 8951cb19 2022-07-01 op static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
11 8951cb19 2022-07-01 op const size_t size, const __u32 flags)
12 8951cb19 2022-07-01 op {
13 8951cb19 2022-07-01 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
14 8951cb19 2022-07-01 op }
15 8951cb19 2022-07-01 op #endif
16 8951cb19 2022-07-01 op
17 8951cb19 2022-07-01 op #ifndef landlock_restrict_self
18 8951cb19 2022-07-01 op static inline int landlock_restrict_self(const int ruleset_fd,
19 8951cb19 2022-07-01 op const __u32 flags)
20 8951cb19 2022-07-01 op {
21 8951cb19 2022-07-01 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
22 8951cb19 2022-07-01 op }
23 8951cb19 2022-07-01 op #endif
24 8951cb19 2022-07-01 op
25 8951cb19 2022-07-01 op int
26 8951cb19 2022-07-01 op main(void)
27 8951cb19 2022-07-01 op {
28 8951cb19 2022-07-01 op uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
29 8951cb19 2022-07-01 op struct landlock_ruleset_attr rules = {
30 8951cb19 2022-07-01 op .handled_access_fs = mask
31 8951cb19 2022-07-01 op };
32 8951cb19 2022-07-01 op int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
33 8951cb19 2022-07-01 op
34 8951cb19 2022-07-01 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
35 8951cb19 2022-07-01 op return 1;
36 8951cb19 2022-07-01 op return landlock_restrict_self(fd, 0) ? 1 : 0;
37 8951cb19 2022-07-01 op }