1 // SPDX-License-Identifier: GPL-2.0
3 * Linux Security Module infrastructure tests
4 * Tests for the lsm_list_modules system call
6 * Copyright © 2022 Casey Schaufler <casey@schaufler-ca.com>
10 #include <linux/lsm.h>
14 #include <sys/types.h>
15 #include "../kselftest_harness.h"
18 TEST(size_null_lsm_list_modules
)
20 const long page_size
= sysconf(_SC_PAGESIZE
);
21 __u64
*syscall_lsms
= calloc(page_size
, 1);
23 ASSERT_NE(NULL
, syscall_lsms
);
25 ASSERT_EQ(-1, lsm_list_modules(syscall_lsms
, NULL
, 0));
26 ASSERT_EQ(EFAULT
, errno
);
31 TEST(ids_null_lsm_list_modules
)
33 const long page_size
= sysconf(_SC_PAGESIZE
);
34 __u32 size
= page_size
;
37 ASSERT_EQ(-1, lsm_list_modules(NULL
, &size
, 0));
38 ASSERT_EQ(EFAULT
, errno
);
42 TEST(size_too_small_lsm_list_modules
)
44 const long page_size
= sysconf(_SC_PAGESIZE
);
45 __u64
*syscall_lsms
= calloc(page_size
, 1);
48 ASSERT_NE(NULL
, syscall_lsms
);
50 ASSERT_EQ(-1, lsm_list_modules(syscall_lsms
, &size
, 0));
51 ASSERT_EQ(E2BIG
, errno
);
57 TEST(flags_set_lsm_list_modules
)
59 const long page_size
= sysconf(_SC_PAGESIZE
);
60 __u64
*syscall_lsms
= calloc(page_size
, 1);
61 __u32 size
= page_size
;
63 ASSERT_NE(NULL
, syscall_lsms
);
65 ASSERT_EQ(-1, lsm_list_modules(syscall_lsms
, &size
, 7));
66 ASSERT_EQ(EINVAL
, errno
);
67 ASSERT_EQ(page_size
, size
);
72 TEST(correct_lsm_list_modules
)
74 const long page_size
= sysconf(_SC_PAGESIZE
);
75 __u32 size
= page_size
;
76 __u64
*syscall_lsms
= calloc(page_size
, 1);
77 char *sysfs_lsms
= calloc(page_size
, 1);
83 ASSERT_NE(NULL
, sysfs_lsms
);
84 ASSERT_NE(NULL
, syscall_lsms
);
85 ASSERT_EQ(0, read_sysfs_lsms(sysfs_lsms
, page_size
));
87 count
= lsm_list_modules(syscall_lsms
, &size
, 0);
90 for (i
= 0; i
< count
; i
++) {
91 switch (syscall_lsms
[i
]) {
92 case LSM_ID_CAPABILITY
:
104 case LSM_ID_APPARMOR
:
113 case LSM_ID_SAFESETID
:
116 case LSM_ID_LOCKDOWN
:
122 case LSM_ID_LANDLOCK
:
138 ASSERT_EQ(0, strncmp(cp
, name
, strlen(name
)));
139 cp
+= strlen(name
) + 1;