Add selftests for the three system calls supporting the LSM infrastructure. This set of tests is limited by the differences in access policy enforced by the existing security modules.
Unified · Split
| @@ -19513,6 +19513,7 @@ W: http://kernsec.org/ · 19512 unmodified lines | ||
| 19513 | 19513 | T: git git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/lsm.git |
| 19514 | 19514 | F: include/uapi/linux/lsm.h |
| 19515 | 19515 | F: security/ |
| 19516 | +F: tools/testing/selftests/lsm/ | |
| 19516 | 19517 | X: security/selinux/ |
| 19517 | 19518 | |
| 19518 | 19519 | SELINUX SECURITY MODULE |
| @@ -43,6 +43,7 @@ TARGETS += landlock · 42 unmodified lines | ||
| 43 | 43 | TARGETS += lib |
| 44 | 44 | TARGETS += livepatch |
| 45 | 45 | TARGETS += lkdtm |
| 46 | +TARGETS += lsm | |
| 46 | 47 | TARGETS += membarrier |
| 47 | 48 | TARGETS += memfd |
| 48 | 49 | TARGETS += memory-hotplug |
| @@ -0,0 +1 @@ | ||
| 1 | +/*_test | |
| @@ -0,0 +1,17 @@ | ||
| 1 | +# SPDX-License-Identifier: GPL-2.0 | |
| 2 | +# | |
| 3 | +# First run: make -C ../../../.. headers_install | |
| 4 | + | |
| 5 | +CFLAGS += -Wall -O2 $(KHDR_INCLUDES) | |
| 6 | +LOCAL_HDRS += common.h | |
| 7 | + | |
| 8 | +TEST_GEN_PROGS := lsm_get_self_attr_test lsm_list_modules_test \ | |
| 9 | + lsm_set_self_attr_test | |
| 10 | + | |
| 11 | +include ../lib.mk | |
| 12 | + | |
| 13 | +$(OUTPUT)/lsm_get_self_attr_test: lsm_get_self_attr_test.c common.c | |
| 14 | +$(OUTPUT)/lsm_set_self_attr_test: lsm_set_self_attr_test.c common.c | |
| 15 | +$(OUTPUT)/lsm_list_modules_test: lsm_list_modules_test.c common.c | |
| 16 | + | |
| 17 | +EXTRA_CLEAN = $(OUTPUT)/common.o | |
| @@ -0,0 +1,89 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | +/* | |
| 3 | + * Linux Security Module infrastructure tests | |
| 4 | + * | |
| 5 | + * Copyright © 2023 Casey Schaufler <casey@schaufler-ca.com> | |
| 6 | + */ | |
| 7 | + | |
| 8 | +#define _GNU_SOURCE | |
| 9 | +#include <linux/lsm.h> | |
| 10 | +#include <fcntl.h> | |
| 11 | +#include <string.h> | |
| 12 | +#include <stdio.h> | |
| 13 | +#include <stdlib.h> | |
| 14 | +#include <unistd.h> | |
| 15 | +#include <sys/types.h> | |
| 16 | +#include "common.h" | |
| 17 | + | |
| 18 | +#define PROCATTR "/proc/self/attr/" | |
| 19 | + | |
| 20 | +int read_proc_attr(const char *attr, char *value, size_t size) | |
| 21 | +{ | |
| 22 | + int fd; | |
| 23 | + int len; | |
| 24 | + char *path; | |
| 25 | + | |
| 26 | + len = strlen(PROCATTR) + strlen(attr) + 1; | |
| 27 | + path = calloc(len, 1); | |
| 28 | + if (path == NULL) | |
| 29 | + return -1; | |
| 30 | + sprintf(path, "%s%s", PROCATTR, attr); | |
| 31 | + | |
| 32 | + fd = open(path, O_RDONLY); | |
| 33 | + free(path); | |
| 34 | + | |
| 35 | + if (fd < 0) | |
| 36 | + return -1; | |
| 37 | + len = read(fd, value, size); | |
| 38 | + | |
| 39 | + close(fd); | |
| 40 | + | |
| 41 | + /* Ensure value is terminated */ | |
| 42 | + if (len <= 0 || len == size) | |
| 43 | + return -1; | |
| 44 | + value[len] = '\0'; | |
| 45 | + | |
| 46 | + path = strchr(value, '\n'); | |
| 47 | + if (path) | |
| 48 | + *path = '\0'; | |
| 49 | + | |
| 50 | + return 0; | |
| 51 | +} | |
| 52 | + | |
| 53 | +int read_sysfs_lsms(char *lsms, size_t size) | |
| 54 | +{ | |
| 55 | + FILE *fp; | |
| 56 | + size_t red; | |
| 57 | + | |
| 58 | + fp = fopen("/sys/kernel/security/lsm", "r"); | |
| 59 | + if (fp == NULL) | |
| 60 | + return -1; | |
| 61 | + red = fread(lsms, 1, size, fp); | |
| 62 | + fclose(fp); | |
| 63 | + | |
| 64 | + if (red <= 0 || red == size) | |
| 65 | + return -1; | |
| 66 | + lsms[red] = '\0'; | |
| 67 | + return 0; | |
| 68 | +} | |
| 69 | + | |
| 70 | +int attr_lsm_count(void) | |
| 71 | +{ | |
| 72 | + char *names = calloc(sysconf(_SC_PAGESIZE), 1); | |
| 73 | + int count = 0; | |
| 74 | + | |
| 75 | + if (!names) | |
| 76 | + return 0; | |
| 77 | + | |
| 78 | + if (read_sysfs_lsms(names, sysconf(_SC_PAGESIZE))) | |
| 79 | + return 0; | |
| 80 | + | |
| 81 | + if (strstr(names, "selinux")) | |
| 82 | + count++; | |
| 83 | + if (strstr(names, "smack")) | |
| 84 | + count++; | |
| 85 | + if (strstr(names, "apparmor")) | |
| 86 | + count++; | |
| 87 | + | |
| 88 | + return count; | |
| 89 | +} | |
| @@ -0,0 +1,33 @@ | ||
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ | |
| 2 | +/* | |
| 3 | + * Linux Security Module infrastructure tests | |
| 4 | + * | |
| 5 | + * Copyright © 2023 Casey Schaufler <casey@schaufler-ca.com> | |
| 6 | + */ | |
| 7 | + | |
| 8 | +#ifndef lsm_get_self_attr | |
| 9 | +static inline int lsm_get_self_attr(unsigned int attr, struct lsm_ctx *ctx, | |
| 10 | + size_t *size, __u32 flags) | |
| 11 | +{ | |
| 12 | + return syscall(__NR_lsm_get_self_attr, attr, ctx, size, flags); | |
| 13 | +} | |
| 14 | +#endif | |
| 15 | + | |
| 16 | +#ifndef lsm_set_self_attr | |
| 17 | +static inline int lsm_set_self_attr(unsigned int attr, struct lsm_ctx *ctx, | |
| 18 | + size_t size, __u32 flags) | |
| 19 | +{ | |
| 20 | + return syscall(__NR_lsm_set_self_attr, attr, ctx, size, flags); | |
| 21 | +} | |
| 22 | +#endif | |
| 23 | + | |
| 24 | +#ifndef lsm_list_modules | |
| 25 | +static inline int lsm_list_modules(__u64 *ids, size_t *size, __u32 flags) | |
| 26 | +{ | |
| 27 | + return syscall(__NR_lsm_list_modules, ids, size, flags); | |
| 28 | +} | |
| 29 | +#endif | |
| 30 | + | |
| 31 | +extern int read_proc_attr(const char *attr, char *value, size_t size); | |
| 32 | +extern int read_sysfs_lsms(char *lsms, size_t size); | |
| 33 | +int attr_lsm_count(void); | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +CONFIG_SYSFS=y | |
| 2 | +CONFIG_SECURITY=y | |
| 3 | +CONFIG_SECURITYFS=y | |
| @@ -0,0 +1,275 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | +/* | |
| 3 | + * Linux Security Module infrastructure tests | |
| 4 | + * Tests for the lsm_get_self_attr system call | |
| 5 | + * | |
| 6 | + * Copyright © 2022 Casey Schaufler <casey@schaufler-ca.com> | |
| 7 | + */ | |
| 8 | + | |
| 9 | +#define _GNU_SOURCE | |
| 10 | +#include <linux/lsm.h> | |
| 11 | +#include <fcntl.h> | |
| 12 | +#include <string.h> | |
| 13 | +#include <stdio.h> | |
| 14 | +#include <unistd.h> | |
| 15 | +#include <sys/types.h> | |
| 16 | +#include "../kselftest_harness.h" | |
| 17 | +#include "common.h" | |
| 18 | + | |
| 19 | +static struct lsm_ctx *next_ctx(struct lsm_ctx *ctxp) | |
| 20 | +{ | |
| 21 | + void *vp; | |
| 22 | + | |
| 23 | + vp = (void *)ctxp + sizeof(*ctxp) + ctxp->ctx_len; | |
| 24 | + return (struct lsm_ctx *)vp; | |
| 25 | +} | |
| 26 | + | |
| 27 | +TEST(size_null_lsm_get_self_attr) | |
| 28 | +{ | |
| 29 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 30 | + struct lsm_ctx *ctx = calloc(page_size, 1); | |
| 31 | + | |
| 32 | + ASSERT_NE(NULL, ctx); | |
| 33 | + errno = 0; | |
| 34 | + ASSERT_EQ(-1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, NULL, 0)); | |
| 35 | + ASSERT_EQ(EINVAL, errno); | |
| 36 | + | |
| 37 | + free(ctx); | |
| 38 | +} | |
| 39 | + | |
| 40 | +TEST(ctx_null_lsm_get_self_attr) | |
| 41 | +{ | |
| 42 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 43 | + size_t size = page_size; | |
| 44 | + int rc; | |
| 45 | + | |
| 46 | + rc = lsm_get_self_attr(LSM_ATTR_CURRENT, NULL, &size, 0); | |
| 47 | + | |
| 48 | + if (attr_lsm_count()) { | |
| 49 | + ASSERT_NE(-1, rc); | |
| 50 | + ASSERT_NE(1, size); | |
| 51 | + } else { | |
| 52 | + ASSERT_EQ(-1, rc); | |
| 53 | + } | |
| 54 | +} | |
| 55 | + | |
| 56 | +TEST(size_too_small_lsm_get_self_attr) | |
| 57 | +{ | |
| 58 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 59 | + struct lsm_ctx *ctx = calloc(page_size, 1); | |
| 60 | + size_t size = 1; | |
| 61 | + | |
| 62 | + ASSERT_NE(NULL, ctx); | |
| 63 | + errno = 0; | |
| 64 | + ASSERT_EQ(-1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, 0)); | |
| 65 | + if (attr_lsm_count()) { | |
| 66 | + ASSERT_EQ(E2BIG, errno); | |
| 67 | + } else { | |
| 68 | + ASSERT_EQ(EOPNOTSUPP, errno); | |
| 69 | + } | |
| 70 | + ASSERT_NE(1, size); | |
| 71 | + | |
| 72 | + free(ctx); | |
| 73 | +} | |
| 74 | + | |
| 75 | +TEST(flags_zero_lsm_get_self_attr) | |
| 76 | +{ | |
| 77 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 78 | + struct lsm_ctx *ctx = calloc(page_size, 1); | |
| 79 | + __u64 *syscall_lsms = calloc(page_size, 1); | |
| 80 | + size_t size; | |
| 81 | + int lsmcount; | |
| 82 | + int i; | |
| 83 | + | |
| 84 | + ASSERT_NE(NULL, ctx); | |
| 85 | + errno = 0; | |
| 86 | + size = page_size; | |
| 87 | + ASSERT_EQ(-1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, | |
| 88 | + LSM_FLAG_SINGLE)); | |
| 89 | + ASSERT_EQ(EINVAL, errno); | |
| 90 | + ASSERT_EQ(page_size, size); | |
| 91 | + | |
| 92 | + lsmcount = syscall(__NR_lsm_list_modules, syscall_lsms, &size, 0); | |
| 93 | + ASSERT_LE(1, lsmcount); | |
| 94 | + ASSERT_NE(NULL, syscall_lsms); | |
| 95 | + | |
| 96 | + for (i = 0; i < lsmcount; i++) { | |
| 97 | + errno = 0; | |
| 98 | + size = page_size; | |
| 99 | + ctx->id = syscall_lsms[i]; | |
| 100 | + | |
| 101 | + if (syscall_lsms[i] == LSM_ID_SELINUX || | |
| 102 | + syscall_lsms[i] == LSM_ID_SMACK || | |
| 103 | + syscall_lsms[i] == LSM_ID_APPARMOR) { | |
| 104 | + ASSERT_EQ(1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, | |
| 105 | + &size, LSM_FLAG_SINGLE)); | |
| 106 | + } else { | |
| 107 | + ASSERT_EQ(-1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, | |
| 108 | + &size, | |
| 109 | + LSM_FLAG_SINGLE)); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + free(ctx); | |
| 114 | +} | |
| 115 | + | |
| 116 | +TEST(flags_overset_lsm_get_self_attr) | |
| 117 | +{ | |
| 118 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 119 | + struct lsm_ctx *ctx = calloc(page_size, 1); | |
| 120 | + size_t size; | |
| 121 | + | |
| 122 | + ASSERT_NE(NULL, ctx); | |
| 123 | + | |
| 124 | + errno = 0; | |
| 125 | + size = page_size; | |
| 126 | + ASSERT_EQ(-1, lsm_get_self_attr(LSM_ATTR_CURRENT | LSM_ATTR_PREV, ctx, | |
| 127 | + &size, 0)); | |
| 128 | + ASSERT_EQ(EOPNOTSUPP, errno); | |
| 129 | + | |
| 130 | + errno = 0; | |
| 131 | + size = page_size; | |
| 132 | + ASSERT_EQ(-1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, | |
| 133 | + LSM_FLAG_SINGLE | | |
| 134 | + (LSM_FLAG_SINGLE << 1))); | |
| 135 | + ASSERT_EQ(EINVAL, errno); | |
| 136 | + | |
| 137 | + free(ctx); | |
| 138 | +} | |
| 139 | + | |
| 140 | +TEST(basic_lsm_get_self_attr) | |
| 141 | +{ | |
| 142 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 143 | + size_t size = page_size; | |
| 144 | + struct lsm_ctx *ctx = calloc(page_size, 1); | |
| 145 | + struct lsm_ctx *tctx = NULL; | |
| 146 | + __u64 *syscall_lsms = calloc(page_size, 1); | |
| 147 | + char *attr = calloc(page_size, 1); | |
| 148 | + int cnt_current = 0; | |
| 149 | + int cnt_exec = 0; | |
| 150 | + int cnt_fscreate = 0; | |
| 151 | + int cnt_keycreate = 0; | |
| 152 | + int cnt_prev = 0; | |
| 153 | + int cnt_sockcreate = 0; | |
| 154 | + int lsmcount; | |
| 155 | + int count; | |
| 156 | + int i; | |
| 157 | + | |
| 158 | + ASSERT_NE(NULL, ctx); | |
| 159 | + ASSERT_NE(NULL, syscall_lsms); | |
| 160 | + | |
| 161 | + lsmcount = syscall(__NR_lsm_list_modules, syscall_lsms, &size, 0); | |
| 162 | + ASSERT_LE(1, lsmcount); | |
| 163 | + | |
| 164 | + for (i = 0; i < lsmcount; i++) { | |
| 165 | + switch (syscall_lsms[i]) { | |
| 166 | + case LSM_ID_SELINUX: | |
| 167 | + cnt_current++; | |
| 168 | + cnt_exec++; | |
| 169 | + cnt_fscreate++; | |
| 170 | + cnt_keycreate++; | |
| 171 | + cnt_prev++; | |
| 172 | + cnt_sockcreate++; | |
| 173 | + break; | |
| 174 | + case LSM_ID_SMACK: | |
| 175 | + cnt_current++; | |
| 176 | + break; | |
| 177 | + case LSM_ID_APPARMOR: | |
| 178 | + cnt_current++; | |
| 179 | + cnt_exec++; | |
| 180 | + cnt_prev++; | |
| 181 | + break; | |
| 182 | + default: | |
| 183 | + break; | |
| 184 | + } | |
| 185 | + } | |
| 186 | + | |
| 187 | + if (cnt_current) { | |
| 188 | + size = page_size; | |
| 189 | + count = lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, 0); | |
| 190 | + ASSERT_EQ(cnt_current, count); | |
| 191 | + tctx = ctx; | |
| 192 | + ASSERT_EQ(0, read_proc_attr("current", attr, page_size)); | |
| 193 | + ASSERT_EQ(0, strcmp((char *)tctx->ctx, attr)); | |
| 194 | + for (i = 1; i < count; i++) { | |
| 195 | + tctx = next_ctx(tctx); | |
| 196 | + ASSERT_NE(0, strcmp((char *)tctx->ctx, attr)); | |
| 197 | + } | |
| 198 | + } | |
| 199 | + if (cnt_exec) { | |
| 200 | + size = page_size; | |
| 201 | + count = lsm_get_self_attr(LSM_ATTR_EXEC, ctx, &size, 0); | |
| 202 | + ASSERT_GE(cnt_exec, count); | |
| 203 | + if (count > 0) { | |
| 204 | + tctx = ctx; | |
| 205 | + if (read_proc_attr("exec", attr, page_size) == 0) | |
| 206 | + ASSERT_EQ(0, strcmp((char *)tctx->ctx, attr)); | |
| 207 | + } | |
| 208 | + for (i = 1; i < count; i++) { | |
| 209 | + tctx = next_ctx(tctx); | |
| 210 | + ASSERT_NE(0, strcmp((char *)tctx->ctx, attr)); | |
| 211 | + } | |
| 212 | + } | |
| 213 | + if (cnt_fscreate) { | |
| 214 | + size = page_size; | |
| 215 | + count = lsm_get_self_attr(LSM_ATTR_FSCREATE, ctx, &size, 0); | |
| 216 | + ASSERT_GE(cnt_fscreate, count); | |
| 217 | + if (count > 0) { | |
| 218 | + tctx = ctx; | |
| 219 | + if (read_proc_attr("fscreate", attr, page_size) == 0) | |
| 220 | + ASSERT_EQ(0, strcmp((char *)tctx->ctx, attr)); | |
| 221 | + } | |
| 222 | + for (i = 1; i < count; i++) { | |
| 223 | + tctx = next_ctx(tctx); | |
| 224 | + ASSERT_NE(0, strcmp((char *)tctx->ctx, attr)); | |
| 225 | + } | |
| 226 | + } | |
| 227 | + if (cnt_keycreate) { | |
| 228 | + size = page_size; | |
| 229 | + count = lsm_get_self_attr(LSM_ATTR_KEYCREATE, ctx, &size, 0); | |
| 230 | + ASSERT_GE(cnt_keycreate, count); | |
| 231 | + if (count > 0) { | |
| 232 | + tctx = ctx; | |
| 233 | + if (read_proc_attr("keycreate", attr, page_size) == 0) | |
| 234 | + ASSERT_EQ(0, strcmp((char *)tctx->ctx, attr)); | |
| 235 | + } | |
| 236 | + for (i = 1; i < count; i++) { | |
| 237 | + tctx = next_ctx(tctx); | |
| 238 | + ASSERT_NE(0, strcmp((char *)tctx->ctx, attr)); | |
| 239 | + } | |
| 240 | + } | |
| 241 | + if (cnt_prev) { | |
| 242 | + size = page_size; | |
| 243 | + count = lsm_get_self_attr(LSM_ATTR_PREV, ctx, &size, 0); | |
| 244 | + ASSERT_GE(cnt_prev, count); | |
| 245 | + if (count > 0) { | |
| 246 | + tctx = ctx; | |
| 247 | + ASSERT_EQ(0, read_proc_attr("prev", attr, page_size)); | |
| 248 | + ASSERT_EQ(0, strcmp((char *)tctx->ctx, attr)); | |
| 249 | + for (i = 1; i < count; i++) { | |
| 250 | + tctx = next_ctx(tctx); | |
| 251 | + ASSERT_NE(0, strcmp((char *)tctx->ctx, attr)); | |
| 252 | + } | |
| 253 | + } | |
| 254 | + } | |
| 255 | + if (cnt_sockcreate) { | |
| 256 | + size = page_size; | |
| 257 | + count = lsm_get_self_attr(LSM_ATTR_SOCKCREATE, ctx, &size, 0); | |
| 258 | + ASSERT_GE(cnt_sockcreate, count); | |
| 259 | + if (count > 0) { | |
| 260 | + tctx = ctx; | |
| 261 | + if (read_proc_attr("sockcreate", attr, page_size) == 0) | |
| 262 | + ASSERT_EQ(0, strcmp((char *)tctx->ctx, attr)); | |
| 263 | + } | |
| 264 | + for (i = 1; i < count; i++) { | |
| 265 | + tctx = next_ctx(tctx); | |
| 266 | + ASSERT_NE(0, strcmp((char *)tctx->ctx, attr)); | |
| 267 | + } | |
| 268 | + } | |
| 269 | + | |
| 270 | + free(ctx); | |
| 271 | + free(attr); | |
| 272 | + free(syscall_lsms); | |
| 273 | +} | |
| 274 | + | |
| 275 | +TEST_HARNESS_MAIN | |
| @@ -0,0 +1,140 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | +/* | |
| 3 | + * Linux Security Module infrastructure tests | |
| 4 | + * Tests for the lsm_list_modules system call | |
| 5 | + * | |
| 6 | + * Copyright © 2022 Casey Schaufler <casey@schaufler-ca.com> | |
| 7 | + */ | |
| 8 | + | |
| 9 | +#define _GNU_SOURCE | |
| 10 | +#include <linux/lsm.h> | |
| 11 | +#include <string.h> | |
| 12 | +#include <stdio.h> | |
| 13 | +#include <unistd.h> | |
| 14 | +#include <sys/types.h> | |
| 15 | +#include "../kselftest_harness.h" | |
| 16 | +#include "common.h" | |
| 17 | + | |
| 18 | +TEST(size_null_lsm_list_modules) | |
| 19 | +{ | |
| 20 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 21 | + __u64 *syscall_lsms = calloc(page_size, 1); | |
| 22 | + | |
| 23 | + ASSERT_NE(NULL, syscall_lsms); | |
| 24 | + errno = 0; | |
| 25 | + ASSERT_EQ(-1, lsm_list_modules(syscall_lsms, NULL, 0)); | |
| 26 | + ASSERT_EQ(EFAULT, errno); | |
| 27 | + | |
| 28 | + free(syscall_lsms); | |
| 29 | +} | |
| 30 | + | |
| 31 | +TEST(ids_null_lsm_list_modules) | |
| 32 | +{ | |
| 33 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 34 | + size_t size = page_size; | |
| 35 | + | |
| 36 | + errno = 0; | |
| 37 | + ASSERT_EQ(-1, lsm_list_modules(NULL, &size, 0)); | |
| 38 | + ASSERT_EQ(EFAULT, errno); | |
| 39 | + ASSERT_NE(1, size); | |
| 40 | +} | |
| 41 | + | |
| 42 | +TEST(size_too_small_lsm_list_modules) | |
| 43 | +{ | |
| 44 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 45 | + __u64 *syscall_lsms = calloc(page_size, 1); | |
| 46 | + size_t size = 1; | |
| 47 | + | |
| 48 | + ASSERT_NE(NULL, syscall_lsms); | |
| 49 | + errno = 0; | |
| 50 | + ASSERT_EQ(-1, lsm_list_modules(syscall_lsms, &size, 0)); | |
| 51 | + ASSERT_EQ(E2BIG, errno); | |
| 52 | + ASSERT_NE(1, size); | |
| 53 | + | |
| 54 | + free(syscall_lsms); | |
| 55 | +} | |
| 56 | + | |
| 57 | +TEST(flags_set_lsm_list_modules) | |
| 58 | +{ | |
| 59 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 60 | + __u64 *syscall_lsms = calloc(page_size, 1); | |
| 61 | + size_t size = page_size; | |
| 62 | + | |
| 63 | + ASSERT_NE(NULL, syscall_lsms); | |
| 64 | + errno = 0; | |
| 65 | + ASSERT_EQ(-1, lsm_list_modules(syscall_lsms, &size, 7)); | |
| 66 | + ASSERT_EQ(EINVAL, errno); | |
| 67 | + ASSERT_EQ(page_size, size); | |
| 68 | + | |
| 69 | + free(syscall_lsms); | |
| 70 | +} | |
| 71 | + | |
| 72 | +TEST(correct_lsm_list_modules) | |
| 73 | +{ | |
| 74 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 75 | + size_t size = page_size; | |
| 76 | + __u64 *syscall_lsms = calloc(page_size, 1); | |
| 77 | + char *sysfs_lsms = calloc(page_size, 1); | |
| 78 | + char *name; | |
| 79 | + char *cp; | |
| 80 | + int count; | |
| 81 | + int i; | |
| 82 | + | |
| 83 | + ASSERT_NE(NULL, sysfs_lsms); | |
| 84 | + ASSERT_NE(NULL, syscall_lsms); | |
| 85 | + ASSERT_EQ(0, read_sysfs_lsms(sysfs_lsms, page_size)); | |
| 86 | + | |
| 87 | + count = lsm_list_modules(syscall_lsms, &size, 0); | |
| 88 | + ASSERT_LE(1, count); | |
| 89 | + cp = sysfs_lsms; | |
| 90 | + for (i = 0; i < count; i++) { | |
| 91 | + switch (syscall_lsms[i]) { | |
| 92 | + case LSM_ID_CAPABILITY: | |
| 93 | + name = "capability"; | |
| 94 | + break; | |
| 95 | + case LSM_ID_SELINUX: | |
| 96 | + name = "selinux"; | |
| 97 | + break; | |
| 98 | + case LSM_ID_SMACK: | |
| 99 | + name = "smack"; | |
| 100 | + break; | |
| 101 | + case LSM_ID_TOMOYO: | |
| 102 | + name = "tomoyo"; | |
| 103 | + break; | |
| 104 | + case LSM_ID_IMA: | |
| 105 | + name = "ima"; | |
| 106 | + break; | |
| 107 | + case LSM_ID_APPARMOR: | |
| 108 | + name = "apparmor"; | |
| 109 | + break; | |
| 110 | + case LSM_ID_YAMA: | |
| 111 | + name = "yama"; | |
| 112 | + break; | |
| 113 | + case LSM_ID_LOADPIN: | |
| 114 | + name = "loadpin"; | |
| 115 | + break; | |
| 116 | + case LSM_ID_SAFESETID: | |
| 117 | + name = "safesetid"; | |
| 118 | + break; | |
| 119 | + case LSM_ID_LOCKDOWN: | |
| 120 | + name = "lockdown"; | |
| 121 | + break; | |
| 122 | + case LSM_ID_BPF: | |
| 123 | + name = "bpf"; | |
| 124 | + break; | |
| 125 | + case LSM_ID_LANDLOCK: | |
| 126 | + name = "landlock"; | |
| 127 | + break; | |
| 128 | + default: | |
| 129 | + name = "INVALID"; | |
| 130 | + break; | |
| 131 | + } | |
| 132 | + ASSERT_EQ(0, strncmp(cp, name, strlen(name))); | |
| 133 | + cp += strlen(name) + 1; | |
| 134 | + } | |
| 135 | + | |
| 136 | + free(sysfs_lsms); | |
| 137 | + free(syscall_lsms); | |
| 138 | +} | |
| 139 | + | |
| 140 | +TEST_HARNESS_MAIN | |
| @@ -0,0 +1,74 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | +/* | |
| 3 | + * Linux Security Module infrastructure tests | |
| 4 | + * Tests for the lsm_set_self_attr system call | |
| 5 | + * | |
| 6 | + * Copyright © 2022 Casey Schaufler <casey@schaufler-ca.com> | |
| 7 | + */ | |
| 8 | + | |
| 9 | +#define _GNU_SOURCE | |
| 10 | +#include <linux/lsm.h> | |
| 11 | +#include <string.h> | |
| 12 | +#include <stdio.h> | |
| 13 | +#include <unistd.h> | |
| 14 | +#include <sys/types.h> | |
| 15 | +#include "../kselftest_harness.h" | |
| 16 | +#include "common.h" | |
| 17 | + | |
| 18 | +TEST(ctx_null_lsm_set_self_attr) | |
| 19 | +{ | |
| 20 | + ASSERT_EQ(-1, lsm_set_self_attr(LSM_ATTR_CURRENT, NULL, | |
| 21 | + sizeof(struct lsm_ctx), 0)); | |
| 22 | +} | |
| 23 | + | |
| 24 | +TEST(size_too_small_lsm_set_self_attr) | |
| 25 | +{ | |
| 26 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 27 | + struct lsm_ctx *ctx = calloc(page_size, 1); | |
| 28 | + size_t size = page_size; | |
| 29 | + | |
| 30 | + ASSERT_NE(NULL, ctx); | |
| 31 | + if (attr_lsm_count()) { | |
| 32 | + ASSERT_LE(1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, | |
| 33 | + 0)); | |
| 34 | + } | |
| 35 | + ASSERT_EQ(-1, lsm_set_self_attr(LSM_ATTR_CURRENT, ctx, 1, 0)); | |
| 36 | + | |
| 37 | + free(ctx); | |
| 38 | +} | |
| 39 | + | |
| 40 | +TEST(flags_zero_lsm_set_self_attr) | |
| 41 | +{ | |
| 42 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 43 | + struct lsm_ctx *ctx = calloc(page_size, 1); | |
| 44 | + size_t size = page_size; | |
| 45 | + | |
| 46 | + ASSERT_NE(NULL, ctx); | |
| 47 | + if (attr_lsm_count()) { | |
| 48 | + ASSERT_LE(1, lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, | |
| 49 | + 0)); | |
| 50 | + } | |
| 51 | + ASSERT_EQ(-1, lsm_set_self_attr(LSM_ATTR_CURRENT, ctx, size, 1)); | |
| 52 | + | |
| 53 | + free(ctx); | |
| 54 | +} | |
| 55 | + | |
| 56 | +TEST(flags_overset_lsm_set_self_attr) | |
| 57 | +{ | |
| 58 | + const long page_size = sysconf(_SC_PAGESIZE); | |
| 59 | + char *ctx = calloc(page_size, 1); | |
| 60 | + size_t size = page_size; | |
| 61 | + struct lsm_ctx *tctx = (struct lsm_ctx *)ctx; | |
| 62 | + | |
| 63 | + ASSERT_NE(NULL, ctx); | |
| 64 | + if (attr_lsm_count()) { | |
| 65 | + ASSERT_LE(1, lsm_get_self_attr(LSM_ATTR_CURRENT, tctx, &size, | |
| 66 | + 0)); | |
| 67 | + } | |
| 68 | + ASSERT_EQ(-1, lsm_set_self_attr(LSM_ATTR_CURRENT | LSM_ATTR_PREV, tctx, | |
| 69 | + size, 0)); | |
| 70 | + | |
| 71 | + free(ctx); | |
| 72 | +} | |
| 73 | + | |
| 74 | +TEST_HARNESS_MAIN | |