From: Andrea Cervesato <andrea.cervesato@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v5 2/7] Add lsm_get_self_attr01 test
Date: Mon, 02 Jun 2025 18:41:25 +0200 [thread overview]
Message-ID: <20250602-lsm-v5-2-5c0dd01df3c4@suse.com> (raw)
In-Reply-To: <20250602-lsm-v5-0-5c0dd01df3c4@suse.com>
From: Andrea Cervesato <andrea.cervesato@suse.com>
Verify that lsm_get_self_attr syscall is raising errors when invalid
data is provided.
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
runtest/syscalls | 2 +
testcases/kernel/syscalls/lsm/.gitignore | 1 +
testcases/kernel/syscalls/lsm/Makefile | 7 ++
testcases/kernel/syscalls/lsm/lsm_common.h | 96 ++++++++++++++++++++++
.../kernel/syscalls/lsm/lsm_get_self_attr01.c | 92 +++++++++++++++++++++
5 files changed, 198 insertions(+)
diff --git a/runtest/syscalls b/runtest/syscalls
index e7bc7b27b604e0f0f69b6bad99955662c6c58a91..f141baa8110aa4e701a808f72a19f2bad46da2d6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -758,6 +758,8 @@ lseek02 lseek02
lseek07 lseek07
lseek11 lseek11
+lsm_get_self_attr01 lsm_get_self_attr01
+
lstat01 lstat01
lstat01_64 lstat01_64
lstat02 lstat02
diff --git a/testcases/kernel/syscalls/lsm/.gitignore b/testcases/kernel/syscalls/lsm/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..49f4a9263349ce633b8decb8fff1dd1d2111cf49
--- /dev/null
+++ b/testcases/kernel/syscalls/lsm/.gitignore
@@ -0,0 +1 @@
+lsm_get_self_attr01
diff --git a/testcases/kernel/syscalls/lsm/Makefile b/testcases/kernel/syscalls/lsm/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..8cf1b9024d8bdebe72408c90fef4b8b84ce9dc4b
--- /dev/null
+++ b/testcases/kernel/syscalls/lsm/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+
+top_srcdir ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/lsm/lsm_common.h b/testcases/kernel/syscalls/lsm/lsm_common.h
new file mode 100644
index 0000000000000000000000000000000000000000..549f2d49b0b9290c4d75c87025911a81f4fa3c19
--- /dev/null
+++ b/testcases/kernel/syscalls/lsm/lsm_common.h
@@ -0,0 +1,96 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+#ifndef LSM_GET_SELF_ATTR_H
+#define LSM_GET_SELF_ATTR_H
+
+#include "tst_test.h"
+#include "lapi/lsm.h"
+
+#define LSM_SYS_FILE "/sys/kernel/security/lsm"
+
+static inline struct lsm_ctx *next_ctx(struct lsm_ctx *tctx)
+{
+ return (struct lsm_ctx *)((char *)tctx + sizeof(*tctx) + tctx->ctx_len);
+}
+
+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;
+ char data[BUFSIZ];
+
+ if (access(LSM_SYS_FILE, F_OK))
+ tst_brk(TCONF, "%s file is not present", LSM_SYS_FILE);
+
+ fd = SAFE_OPEN(LSM_SYS_FILE, 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 enabled", name);
+ return 1;
+ }
+
+ ptr = strtok(NULL, ",");
+ }
+
+ return 0;
+}
+
+static inline uint32_t count_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++;
+
+ return lsm_count;
+}
+
+static inline uint32_t verify_supported_attr_current(void)
+{
+ uint32_t lsm_count;
+
+ lsm_count = count_supported_attr_current();
+
+ if (!lsm_count)
+ tst_brk(TCONF, "LSM_ATTR_CURRENT is not supported by any LSM");
+
+ return lsm_count;
+}
+#endif
diff --git a/testcases/kernel/syscalls/lsm/lsm_get_self_attr01.c b/testcases/kernel/syscalls/lsm/lsm_get_self_attr01.c
new file mode 100644
index 0000000000000000000000000000000000000000..ec272b9374e4240b6d0a0cb5b06aba112e8ea2d2
--- /dev/null
+++ b/testcases/kernel/syscalls/lsm/lsm_get_self_attr01.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * 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, LSM_CTX_SIZE_DEFAULT);
+ ctx_size = LSM_CTX_SIZE_DEFAULT;
+ 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();
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test = run,
+ .tcnt = ARRAY_SIZE(tcases),
+ .min_kver = "6.8",
+ .bufs = (struct tst_buffers[]) {
+ {&ctx, .size = LSM_CTX_SIZE_DEFAULT},
+ {}
+ },
+};
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2025-06-02 16:42 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-02 16:41 [LTP] [PATCH v5 0/7] LSM testing suite Andrea Cervesato
2025-06-02 16:41 ` [LTP] [PATCH v5 1/7] Add fallback definitions of LSM syscalls Andrea Cervesato
2025-06-02 16:41 ` Andrea Cervesato [this message]
2025-06-02 16:48 ` [LTP] [PATCH v5 2/7] Add lsm_get_self_attr01 test Andrea Cervesato via ltp
2025-06-02 16:41 ` [LTP] [PATCH v5 3/7] Add lsm_get_self_attr02 test Andrea Cervesato
2025-06-02 16:41 ` [LTP] [PATCH v5 4/7] Add lsm_get_self_attr03 test Andrea Cervesato
2025-06-03 10:31 ` Cyril Hrubis
2025-06-02 16:41 ` [LTP] [PATCH v5 5/7] Add lsm_list_modules01 test Andrea Cervesato
2025-06-02 16:41 ` [LTP] [PATCH v5 6/7] Add lsm_list_modules02 test Andrea Cervesato
2025-06-02 16:41 ` [LTP] [PATCH v5 7/7] Add lsm_set_self_attr01 test Andrea Cervesato
2025-06-03 10:52 ` [LTP] [PATCH v5 0/7] LSM testing suite Andrea Cervesato via ltp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250602-lsm-v5-2-5c0dd01df3c4@suse.com \
--to=andrea.cervesato@suse.de \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.