1 /*
2 * Check decoding of landlock_restrict_self syscall.
3 *
4 * Copyright (c) 2021 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * All rights reserved.
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include "tests.h"
11 #include "scno.h"
12
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <unistd.h>
17
18 #ifndef SKIP_IF_PROC_IS_UNAVAILABLE
19 # define SKIP_IF_PROC_IS_UNAVAILABLE
20 #endif
21
22 #ifndef RULESET_FD
23 # define RULESET_FD 7
24 #endif
25 #ifndef RULESET_FD_STR
26 # define RULESET_FD_STR "7"
27 #endif
28
29 static const char *errstr;
30
31 static long
32 sys_landlock_restrict_self(int ruleset_fd, unsigned int flags)
33 {
34 static const kernel_ulong_t fill =
35 (kernel_ulong_t) 0xd1efaced00000000ULL;
36 kernel_ulong_t arg1 = fill | (unsigned int) ruleset_fd;
37 kernel_ulong_t arg2 = fill | flags;
38 kernel_ulong_t arg3 = fill | 0xcaffeedc;
39 kernel_ulong_t arg4 = fill | 0xbadceded;
40 kernel_ulong_t arg5 = fill | 0xdecaffed;
41 kernel_ulong_t arg6 = fill | 0xdeefaced;
42
43 long rc = syscall(__NR_landlock_restrict_self,
44 arg1, arg2, arg3, arg4, arg5, arg6);
45 errstr = sprintrc(rc);
46 return rc;
47 }
48
49 int
50 main(void)
51 {
52 SKIP_IF_PROC_IS_UNAVAILABLE;
53
54 /* Valid attr ptr */
55 static const struct {
56 uint64_t val;
57 const char *str;
58 } ruleset_fd_vals[] = {
59 { ARG_STR(-1) },
60 { ARG_STR(9409) },
61 { RULESET_FD, RULESET_FD_STR },
62 };
63 static const struct {
64 int val;
65 const char *str;
66 } flags_vals[] = {
67 { ARG_STR(0) },
68 { ARG_STR(0x1) },
69 { ARG_STR(0xffffffff) },
70 };
71 for (size_t i = 0; i < ARRAY_SIZE(ruleset_fd_vals); i++) {
72 for (size_t j = 0; j < ARRAY_SIZE(flags_vals); j++) {
73 sys_landlock_restrict_self(ruleset_fd_vals[i].val,
74 flags_vals[j].val);
75 printf("landlock_restrict_self(%s, %s) = %s\n",
76 ruleset_fd_vals[i].str, flags_vals[j].str,
77 errstr);
78 }
79 }
80
81 puts("+++ exited with 0 +++");
82 return 0;
83 }