1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4 */
5
6 /*\
7 * [Description]
8 *
9 * This test verifies that landlock_create_ruleset syscall fails with the right
10 * error codes:
11 *
12 * - EINVAL Unknown flags, or unknown access, or too small size
13 * - E2BIG size is too big
14 * - EFAULT attr was not a valid address
15 * - ENOMSG Empty accesses (i.e., attr->handled_access_fs is 0)
16 */
17
18 #include "landlock_common.h"
19
20 static struct tst_landlock_ruleset_attr_abi1 *ruleset_attr;
21 static struct tst_landlock_ruleset_attr_abi1 *null_attr;
22 static size_t rule_size;
23 static size_t rule_small_size;
24 static size_t rule_big_size;
25
26 static struct tcase {
27 struct tst_landlock_ruleset_attr_abi1 **attr;
28 uint64_t access_fs;
29 size_t *size;
30 uint32_t flags;
31 int exp_errno;
32 char *msg;
33 } tcases[] = {
34 {&ruleset_attr, -1, &rule_size, 0, EINVAL, "Unknown access"},
35 {&ruleset_attr, 0, &rule_small_size, 0, EINVAL, "Size is too small"},
36 {&ruleset_attr, 0, &rule_size, -1, EINVAL, "Unknown flags"},
37 {&ruleset_attr, 0, &rule_big_size, 0, E2BIG, "Size is too big"},
38 {&null_attr, 0, &rule_size, 0, EFAULT, "Invalid attr address"},
39 {&ruleset_attr, 0, &rule_size, 0, ENOMSG, "Empty accesses"},
40 };
41
run(unsigned int n)42 static void run(unsigned int n)
43 {
44 struct tcase *tc = &tcases[n];
45
46 if (*tc->attr)
47 (*tc->attr)->handled_access_fs = tc->access_fs;
48
49 TST_EXP_FAIL(tst_syscall(__NR_landlock_create_ruleset,
50 *tc->attr, *tc->size, tc->flags),
51 tc->exp_errno,
52 "%s",
53 tc->msg);
54
55 if (TST_RET >= 0)
56 SAFE_CLOSE(TST_RET);
57 }
58
setup(void)59 static void setup(void)
60 {
61 verify_landlock_is_enabled();
62
63 rule_size = sizeof(struct tst_landlock_ruleset_attr_abi1);
64 rule_small_size = rule_size - 1;
65
66 rule_big_size = SAFE_SYSCONF(_SC_PAGESIZE) + 1;
67 }
68
69 static struct tst_test test = {
70 .test = run,
71 .tcnt = ARRAY_SIZE(tcases),
72 .setup = setup,
73 .needs_root = 1,
74 .bufs = (struct tst_buffers []) {
75 {&ruleset_attr, .size = sizeof(struct tst_landlock_ruleset_attr_abi1)},
76 {},
77 },
78 .caps = (struct tst_cap []) {
79 TST_CAP(TST_CAP_REQ, CAP_SYS_ADMIN),
80 {}
81 },
82 };
83