Skip to contentSkip to conversation

LSM testing suite

PATCH Superseded Andrea Cervesato 3 participants 16 messages Jan 10, 2025 – Apr 28, 2025

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover LSM testing suite

Testing the following syscalls which are providing support for the LSM communication added in kernel 6.8:

- lsm_get_self_attr
- lsm_set_self_attr
- lsm_list_modules

Please consider the following documentation as reference:
https://docs.kernel.org/userspace-api/lsm.html

---
Changes in v3:
- lsm_common.h cleanup
- better overall check for supported LSM(s) when using LSM_ATTR_CURRENT
- simplify tests using more common functions

Changes in v2:
- correctly fallback <linux/lsm.h> include
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrea Cervesato (7):
Add fallback definitions of LSM syscalls
Add lsm_get_self_attr01 test
Add lsm_get_self_attr02 test
Add lsm_get_self_attr03 test
Add lsm_list_modules01 test
Add lsm_list_modules02 test
Add lsm_set_self_attr01 test

 configure.ac                                       |   3 +-
 include/lapi/lsm.h                                 | 173 +++++++++++++++++++++
 runtest/syscalls                                   |   7 +
 testcases/kernel/syscalls/lsm/.gitignore           |   6 +
 testcases/kernel/syscalls/lsm/Makefile             |   7 +
 testcases/kernel/syscalls/lsm/lsm_common.h         |  82 ++++++++++
 .../kernel/syscalls/lsm/lsm_get_self_attr01.c      |  94 +++++++++++
 .../kernel/syscalls/lsm/lsm_get_self_attr02.c      |  55 +++++++
 .../kernel/syscalls/lsm/lsm_get_self_attr03.c      |  70 +++++++++
 testcases/kernel/syscalls/lsm/lsm_list_modules01.c |  77 +++++++++
 testcases/kernel/syscalls/lsm/lsm_list_modules02.c | 138 ++++++++++++++++
 .../kernel/syscalls/lsm/lsm_set_self_attr01.c      | 112 +++++++++++++
 12 files changed, 823 insertions(+), 1 deletion(-)

---
base-commit: 8a64790a3c05e522e02c1fc9af24c020544a20fb
change-id: 20241105-lsm-5da2f6c2a1e9

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v2 1/7 Add fallback definitions of LSM syscalls

From: Andrea Cervesato <[email protected]>

Fallback definition for the following syscalls:

- lsm_get_self_attr
- lsm_set_self_attr
- lsm_list_modules

---
 configure.ac       |   3 +-
 include/lapi/lsm.h | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 175 insertions(+), 1 deletion(-)
configure.ac +2 -1
index 6992d75ca300ccc4cc21a45a916f6b3be1a3b8fe..99c00d1b9b4cda48cb0ce07af03b97855dcdfd36 100644
@@ -62,6 +62,7 @@ AC_CHECK_HEADERS_ONCE([ \
     linux/ioprio.h \
     linux/keyctl.h \
     linux/landlock.h \
+    linux/lsm.h \
     linux/mempolicy.h \
     linux/module.h \
     linux/mount.h \
@@ -196,7 +197,7 @@ AC_CHECK_TYPES([struct ipc64_perm],,,[#include <sys/ipcbuf.h>])
 AC_CHECK_TYPES([struct loop_config],,,[#include <linux/loop.h>])
 AC_CHECK_TYPES([struct landlock_path_beneath_attr],,,[#include <linux/landlock.h>])
 AC_CHECK_TYPES([struct landlock_net_port_attr],,,[#include <linux/landlock.h>])
-
+AC_CHECK_TYPES([struct lsm_ctx],,,[#include <linux/lsm.h>])
 AC_CHECK_TYPES([struct mmsghdr],,,[
 #define _GNU_SOURCE
 #include <sys/types.h>
include/lapi/lsm.h new file +173 -0

173 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH v2 1/7] Add fallback definitions of LSM syscalls

Hi!

include/lapi/lsm.h line 13

Quoted 13 lines
include/lapi/lsm.h new file +13 -0
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+#ifndef LAPI_LSM_H__
+#define LAPI_LSM_H__
+
+#include "config.h"
+
+#ifdef HAVE_LINUX_LSM_H
+#include <linux/lsm.h>
+#else

It's kind of broken to have an else branch here...

Quoted 22 lines
include/lapi/lsm.h new file +22 -0
+#include <stdint.h>
+#include "lapi/syscalls.h"
+
+#ifndef HAVE_STRUCT_LSM_CTX
+
+/**
+ * struct lsm_ctx - LSM context information
+ * @id: the LSM id number, see LSM_ID_XXX
+ * @flags: LSM specific flags
+ * @len: length of the lsm_ctx struct, @ctx and any other data or padding
+ * @ctx_len: the size of @ctx
+ * @ctx: the LSM context value
+ *
+ * The @len field MUST be equal to the size of the lsm_ctx struct
+ * plus any additional padding and/or data placed after @ctx.
+ *
+ * In all cases @ctx_len MUST be equal to the length of @ctx.
+ * If @ctx is a string value it should be nul terminated with
+ * @ctx_len equal to `strlen(@ctx) + 1`.  Binary values are
+ * supported.
+ *
+ * The @flags and @ctx fields SHOULD only be interpreted by the

include/lapi/lsm.h line 46

Quoted 10 lines
include/lapi/lsm.h new file +10 -0
+ * LSM specified by @id; they MUST be set to zero/0 when not used.
+ */
+struct lsm_ctx {
+	uint64_t id;
+	uint64_t flags;
+	uint64_t len;
+	uint64_t ctx_len;
+	uint8_t ctx[4096];
+};
+#endif

... when most of the entries are guarded by their own ifdefs.

So I would be for dropping the else and just making sure that everyting else is guarded by ifdef. This is only way how to make sure that we have all defintions on older variants of linux/lsm.h that may miss some of the ifdefs that are currently in the #else branch.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v2 2/7 Add lsm_get_self_attr01 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Verify that lsm_get_self_attr syscall is raising errors when invalid data is provided.

---
 runtest/syscalls                                   |  2 +
 testcases/kernel/syscalls/lsm/.gitignore           |  1 +
 testcases/kernel/syscalls/lsm/Makefile             |  7 ++
 testcases/kernel/syscalls/lsm/lsm_common.h         | 82 +++++++++++++++++++
 .../kernel/syscalls/lsm/lsm_get_self_attr01.c      | 94 ++++++++++++++++++++++
 5 files changed, 186 insertions(+)
runtest/syscalls +2 -0
index ded035ee82d0e97c67cc1e7c487b010634b2d1a0..77ed36b2d6877bce15f29e263582e7d81f804f92 100644
@@ -756,6 +756,8 @@ lseek02 lseek02
 lseek07 lseek07
 lseek11 lseek11
 
+lsm_get_self_attr01 lsm_get_self_attr01
+
 lstat01 lstat01
 lstat01_64 lstat01_64
 lstat02 lstat02
testcases/kernel/syscalls/lsm/.gitignore new file +1 -0
new file mode 100644
index 0000000000000000000000000000000000000000..49f4a9263349ce633b8decb8fff1dd1d2111cf49
@@ -0,0 +1 @@
+lsm_get_self_attr01
testcases/kernel/syscalls/lsm/Makefile new file +7 -0
new file mode 100644
index 0000000000000000000000000000000000000000..8cf1b9024d8bdebe72408c90fef4b8b84ce9dc4b
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/lsm/lsm_common.h new file +82 -0

82 lines not on this page · raw message

testcases/kernel/syscalls/lsm/lsm_get_self_attr01.c new file +94 -0

94 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 2/7
#4 lore raw

Re: [LTP] [PATCH v2 2/7] Add lsm_get_self_attr01 test

Hi!

Quoted 53 lines
---
 runtest/syscalls                                   |  2 +
 testcases/kernel/syscalls/lsm/.gitignore           |  1 +
 testcases/kernel/syscalls/lsm/Makefile             |  7 ++
 testcases/kernel/syscalls/lsm/lsm_common.h         | 82 +++++++++++++++++++
 .../kernel/syscalls/lsm/lsm_get_self_attr01.c      | 94 ++++++++++++++++++++++
 5 files changed, 186 insertions(+)
runtest/syscalls +2 -0
index ded035ee82d0e97c67cc1e7c487b010634b2d1a0..77ed36b2d6877bce15f29e263582e7d81f804f92 100644
@@ -756,6 +756,8 @@ lseek02 lseek02
 lseek07 lseek07
 lseek11 lseek11
 
+lsm_get_self_attr01 lsm_get_self_attr01
+
 lstat01 lstat01
 lstat01_64 lstat01_64
 lstat02 lstat02
testcases/kernel/syscalls/lsm/.gitignore new file +1 -0
new file mode 100644
index 0000000000000000000000000000000000000000..49f4a9263349ce633b8decb8fff1dd1d2111cf49
@@ -0,0 +1 @@
+lsm_get_self_attr01
testcases/kernel/syscalls/lsm/Makefile new file +7 -0
new file mode 100644
index 0000000000000000000000000000000000000000..8cf1b9024d8bdebe72408c90fef4b8b84ce9dc4b
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/lsm/lsm_common.h new file +4 -0
new file mode 100644
index 0000000000000000000000000000000000000000..ba4762bd8132b585b832ec171c2646ce20312351
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+ */

testcases/kernel/syscalls/lsm/lsm_common.h line 14

Quoted 10 lines
testcases/kernel/syscalls/lsm/lsm_common.h new file +10 -0
+
+#ifndef LSM_GET_SELF_ATTR_H
+#define LSM_GET_SELF_ATTR_H
+
+#include "tst_test.h"
+#include "lapi/lsm.h"
+
+static inline struct lsm_ctx *next_ctx(struct lsm_ctx *tctx)
+{
+	return (void *)tctx + sizeof(*tctx) + tctx->ctx_len;

^
Technically this should be char * as pointer arithmetics on void * is a GNU extension.

Quoted 30 lines
testcases/kernel/syscalls/lsm/lsm_common.h new file +30 -0
+}
+
+static inline void read_proc_attr(const char *attr, char *val, const size_t size)
+{
+	int fd;
+	char *ptr;
+	char path[BUFSIZ];
+
+	memset(val, 0, size);
+	memset(path, 0, BUFSIZ);
+
+	snprintf(path, BUFSIZ, "/proc/self/attr/%s", attr);
+
+	tst_res(TINFO, "Reading %s", path);
+
+	fd = SAFE_OPEN(path, O_RDONLY);
+
+	if (read(fd, val, size) > 0) {
+		ptr = strchr(val, '\n');
+		if (ptr)
+			*ptr = '\0';
+	}
+
+	SAFE_CLOSE(fd);
+}
+
+static inline int verify_enabled_lsm(const char *name)
+{
+	int fd;
+	char *ptr;

testcases/kernel/syscalls/lsm/lsm_common.h line 54

Quoted 10 lines
testcases/kernel/syscalls/lsm/lsm_common.h new file +10 -0
+	char data[BUFSIZ];
+
+	fd = SAFE_OPEN("/sys/kernel/security/lsm", O_RDONLY);
+	SAFE_READ(0, fd, data, BUFSIZ);
+	SAFE_CLOSE(fd);
+
+	ptr = strtok(data, ",");
+	while (ptr != NULL) {
+		if (!strcmp(ptr, name)) {
+			tst_res(TINFO, "%s is running", name);
                                              ^
					      enabled?
Quoted 118 lines
testcases/kernel/syscalls/lsm/lsm_common.h new file +28 -0
+			return 1;
+		}
+
+		ptr = strtok(NULL, ",");
+	}
+
+	return 0;
+}
+
+static inline uint32_t verify_supported_attr_current(void)
+{
+	uint32_t lsm_count = 0;
+
+	if (verify_enabled_lsm("selinux"))
+		lsm_count++;
+
+	if (verify_enabled_lsm("apparmor"))
+		lsm_count++;
+
+	if (verify_enabled_lsm("smack"))
+		lsm_count++;
+
+	if (!lsm_count)
+		tst_brk(TCONF, "LSM_ATTR_CURRENT is not supported by any LSM");
+
+	return lsm_count;
+}
+#endif
testcases/kernel/syscalls/lsm/lsm_get_self_attr01.c new file +84 -0
new file mode 100644
index 0000000000000000000000000000000000000000..5f601f8b3245e81931c0b8f1475d375962eb53ff
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that lsm_get_self_attr syscall is raising errors when invalid data is
+ * provided.
+ */
+
+#include "lsm_common.h"
+
+static struct lsm_ctx *ctx;
+static uint32_t ctx_size;
+static uint32_t ctx_size_small;
+
+static struct tcase {
+	int attr;
+	struct lsm_ctx **ctx;
+	uint32_t *size;
+	uint32_t flags;
+	int exp_err;
+	char *msg;
+} tcases[] = {
+	{
+		.attr = LSM_ATTR_CURRENT,
+		.ctx = &ctx,
+		.exp_err = EINVAL,
+		.msg = "size is NULL",
+	},
+	{
+		.attr = LSM_ATTR_CURRENT,
+		.ctx = &ctx,
+		.size = &ctx_size,
+		.flags = LSM_FLAG_SINGLE | (LSM_FLAG_SINGLE << 1),
+		.exp_err = EINVAL,
+		.msg = "flags is invalid",
+	},
+	{
+		.attr = LSM_ATTR_CURRENT,
+		.ctx = &ctx,
+		.size = &ctx_size_small,
+		.exp_err = E2BIG,
+		.msg = "size is too smal",
+	},
+	{
+		.attr = LSM_ATTR_CURRENT,
+		.ctx = &ctx,
+		.size = &ctx_size,
+		.flags = LSM_FLAG_SINGLE,
+		.exp_err = EINVAL,
+		.msg = "flags force to use ctx attributes",
+	},
+	{
+		.attr = LSM_ATTR_CURRENT | LSM_ATTR_PREV,
+		.ctx = &ctx,
+		.size = &ctx_size,
+		.flags = 0,
+		.exp_err = EOPNOTSUPP,
+		.msg = "flags overset",
+	}
+};
+
+static void run(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	memset(ctx, 0, sizeof(struct lsm_ctx));
+	ctx_size = sizeof(struct lsm_ctx);
+	ctx_size_small = 1;
+
+	TST_EXP_FAIL(lsm_get_self_attr(
+		tc->attr, *tc->ctx, tc->size, tc->flags),
+		tc->exp_err,
+		"%s", tc->msg);
+}
+
+static void setup(void)
+{
+	verify_supported_attr_current();
+}
+

testcases/kernel/syscalls/lsm/lsm_get_self_attr01.c line 94

Quoted 10 lines
testcases/kernel/syscalls/lsm/lsm_get_self_attr01.c new file +10 -0
+static struct tst_test test = {
+	.setup = setup,
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+	.min_kver = "6.8",
+	.bufs = (struct tst_buffers[]) {
+		{&ctx, .size = sizeof(struct lsm_ctx)},
+		{}
+	},
+};

The rest looks good:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v2 3/7 Add lsm_get_self_attr02 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Verify that lsm_get_self_attr syscall is acting correctly when ctx is NULL. The syscall can behave in different ways according to the current system status:

- if any LSM is running inside the system, the syscall will pass and it will provide a size as big as the attribute
- if no LSM(s) are running inside the system, the syscall will fail with -1 return code and it will provide EOPNOTSUPP errno

---
 runtest/syscalls                                   |  1 +
 testcases/kernel/syscalls/lsm/.gitignore           |  1 +
 .../kernel/syscalls/lsm/lsm_get_self_attr02.c      | 55 ++++++++++++++++++++++
 3 files changed, 57 insertions(+)
runtest/syscalls +1 -0
index 77ed36b2d6877bce15f29e263582e7d81f804f92..63855dd786a62fe017882549c5edcc715d9e7e18 100644
@@ -757,6 +757,7 @@ lseek07 lseek07
 lseek11 lseek11
 
 lsm_get_self_attr01 lsm_get_self_attr01
+lsm_get_self_attr02 lsm_get_self_attr02
 
 lstat01 lstat01
 lstat01_64 lstat01_64
testcases/kernel/syscalls/lsm/.gitignore +1 -0
index 49f4a9263349ce633b8decb8fff1dd1d2111cf49..9f7c9b00b026a377f1b36f483ac2c1a0adba6249 100644
@@ -1 +1,2 @@
 lsm_get_self_attr01
+lsm_get_self_attr02
testcases/kernel/syscalls/lsm/lsm_get_self_attr02.c new file +55 -0

55 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH v2 3/7] Add lsm_get_self_attr02 test

Hi!

testcases/kernel/syscalls/lsm/lsm_get_self_attr02.c line 48

Quoted 12 lines
testcases/kernel/syscalls/lsm/lsm_get_self_attr02.c new file +12 -0
+static void setup(void)
+{
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+
+	if (verify_enabled_lsm("selinux"))
+		lsm_count++;
+
+	if (verify_enabled_lsm("apparmor"))
+		lsm_count++;
+
+	if (verify_enabled_lsm("smack"))
+		lsm_count++;

This is the same as we have in the common.h (minus the TCONF there) maybe it would make more sense to split the function in common.h into two. One that would just return the number of lsm and second that would also call TCONF if it's zero. That way we could reuse the code here and keep the number of places with hardcoded security module names smaller.

Quoted 14 lines
testcases/kernel/syscalls/lsm/lsm_get_self_attr02.c new file +7 -0
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.min_kver = "6.8",
+};
Signature · 6 lines
-- 
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

PATCH v2 4/7 Add lsm_get_self_attr03 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Verify that LSM_ATTR_CURRENT attribute is correctly recognizing the current, active security context of the process. This is done by checking that /proc/self/attr/current matches with the obtained value.

---
 runtest/syscalls                                   |  1 +
 testcases/kernel/syscalls/lsm/.gitignore           |  1 +
 .../kernel/syscalls/lsm/lsm_get_self_attr03.c      | 70 ++++++++++++++++++++++
 3 files changed, 72 insertions(+)
runtest/syscalls +1 -0
index 63855dd786a62fe017882549c5edcc715d9e7e18..56eb6449c11102f0148215f8a1efbea51fad6a27 100644
@@ -758,6 +758,7 @@ lseek11 lseek11
 
 lsm_get_self_attr01 lsm_get_self_attr01
 lsm_get_self_attr02 lsm_get_self_attr02
+lsm_get_self_attr03 lsm_get_self_attr03
 
 lstat01 lstat01
 lstat01_64 lstat01_64
testcases/kernel/syscalls/lsm/.gitignore +1 -0
index 9f7c9b00b026a377f1b36f483ac2c1a0adba6249..19956fdf8b9952b4850c3a20826e29ec67ea3560 100644
@@ -1,2 +1,3 @@
 lsm_get_self_attr01
 lsm_get_self_attr02
+lsm_get_self_attr03
testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c new file +70 -0

70 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] [PATCH v2 4/7] Add lsm_get_self_attr03 test

Hi!

Quoted 25 lines

index 0000000000000000000000000000000000000000..b432006650bff697eb60eb7ad6fdd62dbe0eb984

testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c new file +21 -0
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that LSM_ATTR_CURRENT attribute is correctly recognizing
+ * the current, active security context of the process. This is done by
+ * checking that /proc/self/attr/current matches with the obtained value.
+ */
+
+#include "lsm_common.h"
+
+static struct lsm_ctx *ctx;
+static uint32_t page_size;
+
+static void run(void)
+{
+	tst_res(TINFO, "Verifying 'LSM_ATTR_CURRENT' attribute");

testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c line 31

Quoted 10 lines
testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c new file +10 -0
+
+	uint32_t count;
+	uint32_t size = page_size;
+	char attr[size];
+
+	memset(attr, 0, size);
+	memset(ctx, 0, sizeof(struct lsm_ctx));
+
+	count = TST_EXP_POSITIVE(
+		lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, 0));

^ This does not look right.

Shouldn't we pass size initialized to size of the ctx->ctx[] there?

You are mixing the size of the attr[] array and the size of the ctx into a single variable which isn't right at all. The attr[] size can also be much smaller, since it's just a single string.

Looking back at the lapi/lsm.h the definition of the lsm_ctx structure is wrong there too. You define the ctx[] as an array of 4096 bytes. That is wrong because it does not match the system definition where it's defined as a flexible array.

The correct solution is to:

1) change the ctx[4096] in the lapi/lsm.h to just ctx[]
(so that we match the system definition)
2) define CTX_DATA_SIZE to something sensible, e.g. 4096
3) the size for allocation of the ctx must be sizeof(struct lsm_ctx) + CTX_DATA_SIZE (since the size passed to the syscall is the size of the ctx flexible array)
4) the size passed to the lsm_get_self_attr() must be then initialized to CTX_DATA_SIZE

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

Re: [LTP] [PATCH v2 4/7] Add lsm_get_self_attr03 test

Hi Cyril,

Quoted 8 lines · Cyril Hrubis

testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c line 31

Quoted 3 lines
testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c new file +3 -0
+
+	count = TST_EXP_POSITIVE(
+		lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, 0));

^
This does not look right.

Quoted 5 lines

Shouldn't we pass size initialized to size of the ctx->ctx[] there?

You are mixing the size of the attr[] array and the size of the ctx into a single variable which isn't right at all. The attr[] size can also be much smaller, since it's just a single string.

This is how kselftests are working as well, so I'm a bit confused. According to the documentation of lsm_get_self_attr:

 * @size: pointer to the size of space available to receive the data

I guess kselftests are giving a bare minimum size according to the memory pages.

Andrea

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Re: [LTP] [PATCH v2 4/7] Add lsm_get_self_attr03 test

Hi!

Quoted 13 lines
Quoted 7 lines

testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c line 31

Quoted 2 lines
testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c new file +2 -0
+	count = TST_EXP_POSITIVE(
+		lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, 0));

^
This does not look right.

Quoted 5 lines

Shouldn't we pass size initialized to size of the ctx->ctx[] there?

You are mixing the size of the attr[] array and the size of the ctx into a single variable which isn't right at all. The attr[] size can also be much smaller, since it's just a single string.

Quoted 7 lines

This is how kselftests are working as well, so I'm a bit confused. According to the documentation of lsm_get_self_attr:

 * @size: pointer to the size of space available to receive the data

I guess kselftests are giving a bare minimum size according to the memory pages.

No the kselftests work, your code does not. The kselftests use the struct lsm_ctx from /usr/include/linux/lsm.h:

struct lsm_ctx {
        __u64 id;
        __u64 flags;
        __u64 len;
        __u64 ctx_len;
        __u8 ctx[] __counted_by(ctx_len);
};

Where the ctx array is dynamically sized.

While your fallback declares the structure with the ctx to be statically sized (lapi/lsm.h):

struct lsm_ctx {
       uint64_t id;
       uint64_t flags;
       uint64_t len;
       uint64_t ctx_len;
       uint8_t ctx[4096];
};

This is simply wrong because:

- the deifitiontions does not match
- the size of ctx->ctx in case from the system definition is 0
- and page_size != 4k in all cases

So the only good solution is to:

- match the definition of the fallback to the system structure
- define the size in the header somewhere
- use the defined size both for allocation and size initialization

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

PATCH v2 5/7 Add lsm_list_modules01 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Verify that lsm_list_modules syscall is raising errors when invalid data is provided.

---
 runtest/syscalls                                   |  1 +
 testcases/kernel/syscalls/lsm/.gitignore           |  1 +
 testcases/kernel/syscalls/lsm/lsm_list_modules01.c | 77 ++++++++++++++++++++++
 3 files changed, 79 insertions(+)
runtest/syscalls +1 -0
index 56eb6449c11102f0148215f8a1efbea51fad6a27..ae1f430e3dcfebb5167990fcef45b1f3658f6dba 100644
@@ -759,6 +759,7 @@ lseek11 lseek11
 lsm_get_self_attr01 lsm_get_self_attr01
 lsm_get_self_attr02 lsm_get_self_attr02
 lsm_get_self_attr03 lsm_get_self_attr03
+lsm_list_modules01 lsm_list_modules01
 
 lstat01 lstat01
 lstat01_64 lstat01_64
testcases/kernel/syscalls/lsm/.gitignore +1 -0
index 19956fdf8b9952b4850c3a20826e29ec67ea3560..501d332549a84cceb9741346bdb8b83eb02467c5 100644
@@ -1,3 +1,4 @@
 lsm_get_self_attr01
 lsm_get_self_attr02
 lsm_get_self_attr03
+lsm_list_modules01
testcases/kernel/syscalls/lsm/lsm_list_modules01.c new file +77 -0

77 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

PATCH v2 6/7 Add lsm_list_modules02 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Verify that lsm_list_modules syscall is correctly recognizing LSM(s) enabled inside the system.

---
 runtest/syscalls                                   |   1 +
 testcases/kernel/syscalls/lsm/.gitignore           |   1 +
 testcases/kernel/syscalls/lsm/lsm_list_modules02.c | 138 +++++++++++++++++++++
 3 files changed, 140 insertions(+)
runtest/syscalls +1 -0
index ae1f430e3dcfebb5167990fcef45b1f3658f6dba..71c644b2da0e55df40ec753c5c31eeb9c474e146 100644
@@ -760,6 +760,7 @@ lsm_get_self_attr01 lsm_get_self_attr01
 lsm_get_self_attr02 lsm_get_self_attr02
 lsm_get_self_attr03 lsm_get_self_attr03
 lsm_list_modules01 lsm_list_modules01
+lsm_list_modules02 lsm_list_modules02
 
 lstat01 lstat01
 lstat01_64 lstat01_64
testcases/kernel/syscalls/lsm/.gitignore +1 -0
index 501d332549a84cceb9741346bdb8b83eb02467c5..766f81fd1c74a10001862f142c02ba251e666ef2 100644
@@ -2,3 +2,4 @@ lsm_get_self_attr01
 lsm_get_self_attr02
 lsm_get_self_attr03
 lsm_list_modules01
+lsm_list_modules02
testcases/kernel/syscalls/lsm/lsm_list_modules02.c new file +138 -0

138 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

Re: [LTP] [PATCH v2 6/7] Add lsm_list_modules02 test

Hi!

Quoted 9 lines
testcases/kernel/syscalls/lsm/lsm_list_modules02.c new file +9 -0
+#include "lsm_common.h"
+
+#define MAX_LSM_NUM 32
+
+static char lsm_names[MAX_LSM_NUM][BUFSIZ];
+static size_t lsm_names_count;
+static uint32_t page_size;
+static uint64_t *ids;
+static uint32_t *size;

testcases/kernel/syscalls/lsm/lsm_list_modules02.c line 37

Quoted 10 lines
testcases/kernel/syscalls/lsm/lsm_list_modules02.c new file +10 -0
+
+static void run(void)
+{
+	uint32_t lsm_num;
+	size_t count;
+
+	memset(ids, 0, sizeof(uint64_t) * MAX_LSM_NUM);
+	*size = page_size;
+
+	lsm_num = TST_EXP_POSITIVE(lsm_list_modules(ids, size, 0));

If we want to pass the size as page_size here the buffer passed to the syscall would have to be page_size in size. The whole point of passing the size here is to tell the kernel the size of the buffer it can use and it's a pointer because kernel will return the size of the buffer it used back.

Quoted 43 lines
testcases/kernel/syscalls/lsm/lsm_list_modules02.c new file +43 -0
+
+	TST_EXP_EQ_LI(lsm_num, lsm_names_count);
+	TST_EXP_EQ_LI(*size, lsm_num * sizeof(uint64_t));
+
+	for (uint32_t i = 0; i < lsm_num; i++) {
+		char *name = NULL;
+
+		switch (ids[i]) {
+		case LSM_ID_CAPABILITY:
+			name = "capability";
+			break;
+		case LSM_ID_SELINUX:
+			name = "selinux";
+			break;
+		case LSM_ID_SMACK:
+			name = "smack";
+			break;
+		case LSM_ID_TOMOYO:
+			name = "tomoyo";
+			break;
+		case LSM_ID_APPARMOR:
+			name = "apparmor";
+			break;
+		case LSM_ID_YAMA:
+			name = "yama";
+			break;
+		case LSM_ID_LOADPIN:
+			name = "loadpin";
+			break;
+		case LSM_ID_SAFESETID:
+			name = "safesetid";
+			break;
+		case LSM_ID_LOCKDOWN:
+			name = "lockdown";
+			break;
+		case LSM_ID_BPF:
+			name = "bpf";
+			break;
+		case LSM_ID_LANDLOCK:
+			name = "landlock";
+			break;
+		case LSM_ID_IMA:
+			name = "ima";

testcases/kernel/syscalls/lsm/lsm_list_modules02.c line 90

Quoted 10 lines
testcases/kernel/syscalls/lsm/lsm_list_modules02.c new file +10 -0
+			break;
+		case LSM_ID_EVM:
+			name = "evm";
+			break;
+		case LSM_ID_IPE:
+			name = "ipe";
+			break;
+		default:
+			break;
+		}

I guess that it may be also a good idea to have an array where we would record which LSM we have seen in the buffer to make sure that we didn't get any of them twice.

I suppose that we can turn the lsm_names into a structure and add counters there with:

struct lsm_name {
	int cnt;
	char name[MAX_LSM_NAME];
};

struct lsm_name lsm_names[MAX_LSM_NUM];

testcases/kernel/syscalls/lsm/lsm_list_modules02.c line 97

Quoted 6 lines
testcases/kernel/syscalls/lsm/lsm_list_modules02.c new file +6 -0
+		if (!name)
+			tst_brk(TBROK, "Unsupported LSM: %lu", ids[i]);
+
+		for (count = 0; count < lsm_names_count; count++) {
+			if (!strcmp(name, lsm_names[count])) {
+				tst_res(TPASS, "'%s' is enabled", name);

And then we can (assuming we zero the counters at the start of this function) do:

				if (lsm_names[count].cnt)
					tst_res(TFAIL, "Duplicated LSM entry %s", lsm_names[count].name);

				lsm_names[count].cnt++;
Quoted 48 lines
testcases/kernel/syscalls/lsm/lsm_list_modules02.c new file +41 -0
+				break;
+			}
+		}
+
+		if (count >= lsm_names_count)
+			tst_res(TFAIL, "'%s' has not been found", name);
+	}
+}
+
+static void setup(void)
+{
+	int fd;
+	char *ptr;
+	char data[BUFSIZ];
+
+	memset(data, 0, BUFSIZ);
+
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+	fd = SAFE_OPEN("/sys/kernel/security/lsm", O_RDONLY);
+	SAFE_READ(0, fd, data, BUFSIZ);
+	SAFE_CLOSE(fd);
+
+	ptr = strtok(data, ",");
+
+	while (ptr != NULL) {
+		strcpy(lsm_names[lsm_names_count], ptr);
+		ptr = strtok(NULL, ",");
+		lsm_names_count++;
+	}
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.min_kver = "6.8",
+	.bufs = (struct tst_buffers []) {
+		{&ids, .size = sizeof(uint64_t) * MAX_LSM_NUM},
+		{&size, .size = sizeof(uint32_t)},
+		{},
+	},
+};
Signature · 6 lines
-- 
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw

PATCH v2 7/7 Add lsm_set_self_attr01 test

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Verify that lsm_set_self_attr syscall is raising errors when invalid data is provided.

---
 runtest/syscalls                                   |   1 +
 testcases/kernel/syscalls/lsm/.gitignore           |   1 +
 .../kernel/syscalls/lsm/lsm_set_self_attr01.c      | 112 +++++++++++++++++++++
 3 files changed, 114 insertions(+)
runtest/syscalls +1 -0
index 71c644b2da0e55df40ec753c5c31eeb9c474e146..4bbf0f8948f7076202d0231ffba909d242f78098 100644
@@ -761,6 +761,7 @@ lsm_get_self_attr02 lsm_get_self_attr02
 lsm_get_self_attr03 lsm_get_self_attr03
 lsm_list_modules01 lsm_list_modules01
 lsm_list_modules02 lsm_list_modules02
+lsm_set_self_attr01 lsm_set_self_attr01
 
 lstat01 lstat01
 lstat01_64 lstat01_64
testcases/kernel/syscalls/lsm/.gitignore +1 -0
index 766f81fd1c74a10001862f142c02ba251e666ef2..467f07cec5443393d231bbb98880b7183635dd9d 100644
@@ -3,3 +3,4 @@ lsm_get_self_attr02
 lsm_get_self_attr03
 lsm_list_modules01
 lsm_list_modules02
+lsm_set_self_attr01
testcases/kernel/syscalls/lsm/lsm_set_self_attr01.c new file +112 -0

112 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw

Re: [LTP] [PATCH v2 7/7] Add lsm_set_self_attr01 test

Hi!

Quoted 19 lines
testcases/kernel/syscalls/lsm/lsm_set_self_attr01.c new file +19 -0
+static void run(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	/* just in case lsm_set_self_attr() pass , we won't change
+	 * LSM configuration for the following process
+	 */
+	memcpy(ctx, ctx_orig, sizeof(struct lsm_ctx));
+
+	ctx_size = page_size;
+	ctx_size_small = 1;
+	ctx_size_big = ctx_size + 1;
+
+	TST_EXP_FAIL(lsm_set_self_attr(tc->attr, *tc->ctx, *tc->size, tc->flags),
+	      tc->exp_errno,
+	      "%s", tc->msg);
+}
+
+static void setup(void)

testcases/kernel/syscalls/lsm/lsm_set_self_attr01.c line 97

Quoted 10 lines
testcases/kernel/syscalls/lsm/lsm_set_self_attr01.c new file +10 -0
+{
+	int ret;
+	uint32_t size;
+
+	verify_supported_attr_current();
+
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+	size = page_size;
+
+	ret = lsm_get_self_attr(LSM_ATTR_CURRENT, ctx_orig, &size, 0);

Here as well, the handling of the size is wrong.

Quoted 22 lines
testcases/kernel/syscalls/lsm/lsm_set_self_attr01.c new file +15 -0
+	if (ret < 0)
+		tst_brk(TBROK, "Can't read LSM current attribute");
+}
+
+static struct tst_test test = {
+	.test = run,
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.min_kver = "6.8",
+	.bufs = (struct tst_buffers[]) {
+		{&ctx, .size = sizeof(struct lsm_ctx)},
+		{&ctx_orig, .size = sizeof(struct lsm_ctx)},
+		{}
+	},
+};
Signature · 6 lines
-- 
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp