From: Cai Xinchen <caixinchen1@huawei.com>
To: <nicolas.bouchinet@oss.cyber.gouv.fr>, <xiujianfeng@huawei.com>,
<paul@paul-moore.com>, <jmorris@namei.org>, <serge@hallyn.com>,
<shuah@kernel.org>, <kalevi@kolttonen.fi>,
<caixinchen1@huawei.com>, <maguicai@kylinos.cn>
Cc: <linux-security-module@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <linux-kselftest@vger.kernel.org>,
<bpf@vger.kernel.org>, <lujialin4@huawei.com>
Subject: [PATCH v4 8/8] selftests/lsm: add lockdown securityfs test
Date: Mon, 30 Nov 2026 16:29:14 +0800 [thread overview]
Message-ID: <20261130082914.129495-9-caixinchen1@huawei.com> (raw)
In-Reply-To: <20261130082914.129495-1-caixinchen1@huawei.com>
Add a kselftest for the lockdown securityfs interface. It checks the
exact read format documented in
Documentation/ABI/testing/securityfs-lockdown, rejects unknown levels,
verifies that an already locked down kernel neither accepts the current
level nor a lower one, checks that the level can be upgraded, and that
an unprivileged writer fails at open() time with EACCES. The tests
that write skip themselves when lockdown is not enabled, when not run
as root, or when the file cannot be opened for writing; the read-only
tests only skip themselves when lockdown is not enabled, and the test
that expects EACCES skips itself when run as root or when the task
holds CAP_DAC_OVERRIDE.
The upgrade test irreversibly raises the lockdown level, so the test is
built but not run by default; run "make -C tools/testing/selftests/lsm
run_lockdown_test" to execute it, preferably on a disposable test
system. It runs before the tests
that expect an already locked down kernel, so they do not run at the
none level.
Enable CONFIG_SECURITY_LOCKDOWN_LSM and its EARLY variant in the
selftest config fragment, so that the lockdown LSM is ordered in even
when another fragment pins CONFIG_LSM without it, and the test has
something to exercise.
Use the shared file read helper from common.c instead of duplicating
it, and share the read checks between the tests through a fixture
setup. The root and writability checks run in the tests that write,
so that the read-only tests also work when run unprivileged.
Suggested-by: Guicai Ma <maguicai@kylinos.cn>
Signed-off-by: Cai Xinchen <caixinchen1@huawei.com>
---
tools/testing/selftests/lsm/Makefile | 8 +
tools/testing/selftests/lsm/common.c | 47 +++-
tools/testing/selftests/lsm/common.h | 2 +
tools/testing/selftests/lsm/config | 2 +
tools/testing/selftests/lsm/lockdown_test.c | 272 ++++++++++++++++++++
5 files changed, 322 insertions(+), 9 deletions(-)
create mode 100644 tools/testing/selftests/lsm/lockdown_test.c
diff --git a/tools/testing/selftests/lsm/Makefile b/tools/testing/selftests/lsm/Makefile
index 3f80c0bc093d..a569366a5661 100644
--- a/tools/testing/selftests/lsm/Makefile
+++ b/tools/testing/selftests/lsm/Makefile
@@ -8,10 +8,18 @@ LOCAL_HDRS += common.h
TEST_GEN_PROGS := lsm_get_self_attr_test lsm_list_modules_test \
lsm_set_self_attr_test
+# lockdown_test changes the machine's lockdown level, so do not run it
+# by default.
+TEST_GEN_PROGS_EXTENDED := lockdown_test
+
include ../lib.mk
$(OUTPUT)/lsm_get_self_attr_test: lsm_get_self_attr_test.c common.c
$(OUTPUT)/lsm_set_self_attr_test: lsm_set_self_attr_test.c common.c
$(OUTPUT)/lsm_list_modules_test: lsm_list_modules_test.c common.c
+$(OUTPUT)/lockdown_test: lockdown_test.c common.c
+
+run_lockdown_test: all
+ $(call RUN_TESTS, $(OUTPUT)/lockdown_test)
EXTRA_CLEAN = $(OUTPUT)/common.o
diff --git a/tools/testing/selftests/lsm/common.c b/tools/testing/selftests/lsm/common.c
index 927dce4f04cb..aa01203a65f9 100644
--- a/tools/testing/selftests/lsm/common.c
+++ b/tools/testing/selftests/lsm/common.c
@@ -7,6 +7,7 @@
#define _GNU_SOURCE
#include <linux/lsm.h>
+#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
@@ -50,23 +51,51 @@ int read_proc_attr(const char *attr, char *value, size_t size)
return 0;
}
-int read_sysfs_lsms(char *lsms, size_t size)
+int read_file(const char *path, char *buf, size_t size)
{
- FILE *fp;
- size_t red;
+ ssize_t len;
+ int fd;
+
+ if (!size) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -1;
- fp = fopen("/sys/kernel/security/lsm", "r");
- if (fp == NULL)
+ len = read(fd, buf, size);
+ if (len < 0) {
+ int err = errno;
+
+ close(fd);
+ errno = err;
return -1;
- red = fread(lsms, 1, size, fp);
- fclose(fp);
+ }
+
+ close(fd);
- if (red <= 0 || red == size)
+ /*
+ * An empty file is not a valid status string, and a completely
+ * filled buffer means the string did not fit: no room is left
+ * for the terminating NUL. Fail in both cases, like the
+ * previous read_sysfs_lsms() implementation did.
+ */
+ if (!len || len == (ssize_t)size) {
+ errno = EOVERFLOW;
return -1;
- lsms[red] = '\0';
+ }
+
+ buf[len] = '\0';
return 0;
}
+int read_sysfs_lsms(char *lsms, size_t size)
+{
+ return read_file("/sys/kernel/security/lsm", lsms, size);
+}
+
int attr_lsm_count(void)
{
char *names = calloc(sysconf(_SC_PAGESIZE), 1);
diff --git a/tools/testing/selftests/lsm/common.h b/tools/testing/selftests/lsm/common.h
index 06d12110d241..2f824de2d1ad 100644
--- a/tools/testing/selftests/lsm/common.h
+++ b/tools/testing/selftests/lsm/common.h
@@ -31,3 +31,5 @@ static inline int lsm_list_modules(__u64 *ids, __u32 *size, __u32 flags)
extern int read_proc_attr(const char *attr, char *value, size_t size);
extern int read_sysfs_lsms(char *lsms, size_t size);
int attr_lsm_count(void);
+
+extern int read_file(const char *path, char *buf, size_t size);
diff --git a/tools/testing/selftests/lsm/config b/tools/testing/selftests/lsm/config
index 1c0c4c020f9c..b895f64f301f 100644
--- a/tools/testing/selftests/lsm/config
+++ b/tools/testing/selftests/lsm/config
@@ -1,3 +1,5 @@
CONFIG_SYSFS=y
CONFIG_SECURITY=y
CONFIG_SECURITYFS=y
+CONFIG_SECURITY_LOCKDOWN_LSM=y
+CONFIG_SECURITY_LOCKDOWN_LSM_EARLY=y
diff --git a/tools/testing/selftests/lsm/lockdown_test.c b/tools/testing/selftests/lsm/lockdown_test.c
new file mode 100644
index 000000000000..d9f8696a687b
--- /dev/null
+++ b/tools/testing/selftests/lsm/lockdown_test.c
@@ -0,0 +1,272 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Linux Security Module infrastructure tests
+ * Tests for the lockdown securityfs interface
+ */
+
+#define _GNU_SOURCE
+#include <linux/capability.h>
+#include <linux/lsm.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include "kselftest_harness.h"
+#include "common.h"
+
+#define LOCKDOWN_PATH "/sys/kernel/security/lockdown"
+
+static int write_status(const char *value)
+{
+ ssize_t len;
+ int fd, err;
+
+ fd = open(LOCKDOWN_PATH, O_WRONLY);
+ if (fd < 0)
+ return -1;
+
+ len = write(fd, value, strlen(value));
+ err = errno;
+ close(fd);
+ if (len < 0) {
+ errno = err;
+ return -1;
+ }
+
+ return 0;
+}
+
+static int status_writable(void)
+{
+ int fd;
+
+ fd = open(LOCKDOWN_PATH, O_WRONLY);
+ if (fd < 0)
+ return -1;
+
+ close(fd);
+ return 0;
+}
+
+static int has_cap_dac_override(void)
+{
+ struct __user_cap_header_struct hdr = {
+ .version = _LINUX_CAPABILITY_VERSION_3,
+ };
+ struct __user_cap_data_struct data[2];
+
+ if (syscall(SYS_capget, &hdr, data))
+ return -1;
+
+ return !!(data[0].effective & (1U << CAP_DAC_OVERRIDE));
+}
+
+static int get_active_level(const char *buf, char *level, size_t size)
+{
+ const char *start, *end;
+ size_t len;
+
+ start = strchr(buf, '[');
+ if (!start)
+ return -1;
+ end = strchr(start, ']');
+ if (!end)
+ return -1;
+
+ len = end - start - 1;
+ if (len == 0 || len >= size)
+ return -1;
+
+ memcpy(level, start + 1, len);
+ level[len] = '\0';
+ return 0;
+}
+
+static int read_lockdown_status(char *buf, size_t size)
+{
+ if (!read_file(LOCKDOWN_PATH, buf, size))
+ return 0;
+
+ return errno == ENOENT ? 1 : -1;
+}
+
+FIXTURE(lockdown)
+{
+ char status[128];
+ char level[32];
+};
+
+FIXTURE_SETUP(lockdown)
+{
+ int ret;
+
+ ret = read_lockdown_status(self->status, sizeof(self->status));
+ if (ret) {
+ if (ret > 0)
+ SKIP(return, "%s is not present", LOCKDOWN_PATH);
+ TH_LOG("cannot read %s: %s", LOCKDOWN_PATH, strerror(errno));
+ ASSERT_EQ(0, ret);
+ }
+
+ ASSERT_EQ(0, get_active_level(self->status, self->level,
+ sizeof(self->level)));
+}
+
+FIXTURE_TEARDOWN(lockdown)
+{
+}
+
+/*
+ * The format is documented in Documentation/ABI/testing/securityfs-lockdown:
+ * every level in ascending order, the active one bracketed, separated by
+ * single spaces, terminated by a newline.
+ */
+TEST_F(lockdown, read_format)
+{
+ const char *expected = NULL;
+
+ if (!strcmp(self->level, "none")) {
+ expected = "[none] integrity confidentiality\n";
+ } else if (!strcmp(self->level, "integrity")) {
+ expected = "none [integrity] confidentiality\n";
+ } else if (!strcmp(self->level, "confidentiality")) {
+ expected = "none integrity [confidentiality]\n";
+ } else {
+ TH_LOG("unknown active lockdown level: %s", self->level);
+ ASSERT_TRUE(false);
+ }
+
+ ASSERT_STREQ(expected, self->status);
+}
+
+/*
+ * The status file is created with mode 0644: anyone may read it, but
+ * an unprivileged writer fails at open() time with EACCES. A task
+ * holding CAP_DAC_OVERRIDE can open it regardless of the mode, so the
+ * capability, not the uid, decides what counts as unprivileged here.
+ */
+TEST_F(lockdown, unprivileged_open_eacces)
+{
+ int fd;
+
+ if (geteuid() == 0)
+ SKIP(return, "requires a non-root user");
+
+ fd = open(LOCKDOWN_PATH, O_WRONLY);
+ if (fd >= 0) {
+ int cap;
+
+ close(fd);
+ cap = has_cap_dac_override();
+ if (cap < 0)
+ SKIP(return, "cannot determine capabilities: %s",
+ strerror(errno));
+ if (cap)
+ SKIP(return, "task holds CAP_DAC_OVERRIDE");
+ TH_LOG("unprivileged open of %s unexpectedly succeeded",
+ LOCKDOWN_PATH);
+ ASSERT_TRUE(false);
+ }
+ ASSERT_LT(fd, 0);
+ if (errno == EROFS)
+ SKIP(return, "%s is on a read-only filesystem", LOCKDOWN_PATH);
+
+ ASSERT_EQ(EACCES, errno);
+}
+
+TEST_F(lockdown, invalid_write)
+{
+ char after[128];
+
+ if (geteuid() != 0)
+ SKIP(return, "requires root");
+ if (status_writable())
+ SKIP(return, "cannot open %s for writing: %s",
+ LOCKDOWN_PATH, strerror(errno));
+
+ errno = 0;
+ ASSERT_EQ(-1, write_status("invalid-level"));
+ ASSERT_EQ(EINVAL, errno);
+
+ ASSERT_EQ(0, read_file(LOCKDOWN_PATH, after, sizeof(after)));
+ ASSERT_STREQ(self->status, after);
+}
+
+/*
+ * This test raises the lockdown level and cannot undo it, so it runs
+ * before the tests that expect an already locked down kernel.
+ */
+TEST_F(lockdown, upgrade)
+{
+ const char *next;
+ char after[128], level[32];
+
+ if (geteuid() != 0)
+ SKIP(return, "requires root");
+ if (status_writable())
+ SKIP(return, "cannot open %s for writing: %s",
+ LOCKDOWN_PATH, strerror(errno));
+
+ if (!strcmp(self->level, "none"))
+ next = "integrity";
+ else if (!strcmp(self->level, "integrity"))
+ next = "confidentiality";
+ else
+ SKIP(return, "kernel is already at the highest level");
+
+ errno = 0;
+ ASSERT_EQ(0, write_status(next));
+
+ ASSERT_EQ(0, read_file(LOCKDOWN_PATH, after, sizeof(after)));
+ ASSERT_EQ(0, get_active_level(after, level, sizeof(level)));
+ ASSERT_STREQ(next, level);
+}
+
+TEST_F(lockdown, same_level_rejected)
+{
+ char after[128];
+
+ if (geteuid() != 0)
+ SKIP(return, "requires root");
+ if (status_writable())
+ SKIP(return, "cannot open %s for writing: %s",
+ LOCKDOWN_PATH, strerror(errno));
+
+ errno = 0;
+ ASSERT_EQ(-1, write_status(self->level));
+ ASSERT_EQ(EPERM, errno);
+
+ ASSERT_EQ(0, read_file(LOCKDOWN_PATH, after, sizeof(after)));
+ ASSERT_STREQ(self->status, after);
+}
+
+TEST_F(lockdown, no_downgrade)
+{
+ const char *lower;
+ char after[128];
+
+ if (geteuid() != 0)
+ SKIP(return, "requires root");
+ if (status_writable())
+ SKIP(return, "cannot open %s for writing: %s",
+ LOCKDOWN_PATH, strerror(errno));
+
+ if (!strcmp(self->level, "confidentiality"))
+ lower = "integrity";
+ else if (!strcmp(self->level, "integrity"))
+ lower = "none";
+ else
+ SKIP(return, "kernel is not locked down, nothing to downgrade");
+
+ errno = 0;
+ ASSERT_EQ(-1, write_status(lower));
+ ASSERT_EQ(EPERM, errno);
+
+ ASSERT_EQ(0, read_file(LOCKDOWN_PATH, after, sizeof(after)));
+ ASSERT_STREQ(self->status, after);
+}
+
+TEST_HARNESS_MAIN
--
2.18.0.huawei.25
prev parent reply other threads:[~2026-11-30 7:58 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 8:29 [PATCH v4 0/8] lockdown: fix the securityfs interface and add tests Cai Xinchen
2026-11-30 8:29 ` [PATCH v4 1/8] lockdown: Add break in lockdown_write Cai Xinchen
2026-11-30 8:03 ` sashiko-bot
2026-11-30 8:29 ` [PATCH v4 2/8] lockdown: remove useless decrement operation Cai Xinchen
2026-11-30 8:29 ` [PATCH v4 3/8] lockdown: select SECURITYFS Cai Xinchen
2026-11-30 8:01 ` sashiko-bot
2026-11-30 8:29 ` [PATCH v4 4/8] lsm: remove unused lockdown audit data Cai Xinchen
2026-11-30 8:29 ` [PATCH v4 5/8] lockdown: drop an unused include and move lockdown_reasons Cai Xinchen
2026-11-30 8:29 ` [PATCH v4 6/8] lockdown: bound the status string with scnprintf Cai Xinchen
2026-11-30 8:29 ` [PATCH v4 7/8] Documentation/ABI: document the lockdown securityfs file Cai Xinchen
2026-11-30 8:29 ` Cai Xinchen [this message]
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=20261130082914.129495-9-caixinchen1@huawei.com \
--to=caixinchen1@huawei.com \
--cc=bpf@vger.kernel.org \
--cc=jmorris@namei.org \
--cc=kalevi@kolttonen.fi \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=lujialin4@huawei.com \
--cc=maguicai@kylinos.cn \
--cc=nicolas.bouchinet@oss.cyber.gouv.fr \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.com \
--cc=shuah@kernel.org \
--cc=xiujianfeng@huawei.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox