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>, <caixinchen1@huawei.com>
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 -next 9/9] selftests/lsm: add lockdown securityfs test
Date: Tue, 3 Nov 2026 16:24:21 +0800 [thread overview]
Message-ID: <20261103082421.3033195-10-caixinchen1@huawei.com> (raw)
In-Reply-To: <20261103082421.3033195-1-caixinchen1@huawei.com>
Add a kselftest for the lockdown securityfs interface. It checks the
read format, rejects unknown levels, and verifies that an already locked
down kernel neither accepts the current level nor a lower one. The test
skips itself when lockdown is not enabled, when not run as root, or when
the file cannot be opened for writing, and it never escalates lockdown
on the test machine.
Use the shared file read helper from common.c instead of duplicating it.
Signed-off-by: Cai Xinchen <caixinchen1@huawei.com>
---
tools/testing/selftests/lsm/Makefile | 3 +-
tools/testing/selftests/lsm/common.c | 35 +++-
tools/testing/selftests/lsm/common.h | 2 +
tools/testing/selftests/lsm/lockdown_test.c | 176 ++++++++++++++++++++
4 files changed, 206 insertions(+), 10 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..737e8686bb7d 100644
--- a/tools/testing/selftests/lsm/Makefile
+++ b/tools/testing/selftests/lsm/Makefile
@@ -6,12 +6,13 @@ CFLAGS += -Wall -O2 $(KHDR_INCLUDES)
LOCAL_HDRS += common.h
TEST_GEN_PROGS := lsm_get_self_attr_test lsm_list_modules_test \
- lsm_set_self_attr_test
+ lsm_set_self_attr_test 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
EXTRA_CLEAN = $(OUTPUT)/common.o
diff --git a/tools/testing/selftests/lsm/common.c b/tools/testing/selftests/lsm/common.c
index 927dce4f04cb..e7d5cdb10137 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,39 @@ 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;
- fp = fopen("/sys/kernel/security/lsm", "r");
- if (fp == NULL)
+ if (!size) {
+ errno = EINVAL;
return -1;
- red = fread(lsms, 1, size, fp);
- fclose(fp);
+ }
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ len = read(fd, buf, size - 1);
+ if (len < 0) {
+ int err = errno;
- if (red <= 0 || red == size)
+ close(fd);
+ errno = err;
return -1;
- lsms[red] = '\0';
+ }
+
+ close(fd);
+ 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/lockdown_test.c b/tools/testing/selftests/lsm/lockdown_test.c
new file mode 100644
index 000000000000..35109833da37
--- /dev/null
+++ b/tools/testing/selftests/lsm/lockdown_test.c
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Linux Security Module infrastructure tests
+ * Tests for the lockdown securityfs interface
+ */
+
+#define _GNU_SOURCE
+#include <linux/lsm.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.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 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;
+}
+
+TEST(lockdown_read_format)
+{
+ char buf[128], level[32];
+
+ if (read_file(LOCKDOWN_PATH, buf, sizeof(buf))) {
+ if (errno == ENOENT)
+ SKIP(return, "%s is not present", LOCKDOWN_PATH);
+ SKIP(return, "cannot read %s: %s", LOCKDOWN_PATH,
+ strerror(errno));
+ }
+
+ ASSERT_EQ(0, get_active_level(buf, level, sizeof(level)));
+ ASSERT_TRUE(!strcmp(level, "none") ||
+ !strcmp(level, "integrity") ||
+ !strcmp(level, "confidentiality"));
+
+ ASSERT_NE(NULL, strstr(buf, "none"));
+ ASSERT_NE(NULL, strstr(buf, "integrity"));
+ ASSERT_NE(NULL, strstr(buf, "confidentiality"));
+}
+
+TEST(lockdown_invalid_write)
+{
+ char before[128], after[128];
+
+ if (read_file(LOCKDOWN_PATH, before, sizeof(before))) {
+ if (errno == ENOENT)
+ SKIP(return, "%s is not present", LOCKDOWN_PATH);
+ SKIP(return, "cannot read %s: %s", LOCKDOWN_PATH,
+ strerror(errno));
+ }
+ 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(before, after);
+}
+
+TEST(lockdown_no_downgrade)
+{
+ char before[128], after[128], level[32];
+ const char *lower;
+
+ if (read_file(LOCKDOWN_PATH, before, sizeof(before))) {
+ if (errno == ENOENT)
+ SKIP(return, "%s is not present", LOCKDOWN_PATH);
+ SKIP(return, "cannot read %s: %s", LOCKDOWN_PATH,
+ strerror(errno));
+ }
+ if (geteuid() != 0)
+ SKIP(return, "requires root");
+ if (status_writable())
+ SKIP(return, "cannot open %s for writing: %s",
+ LOCKDOWN_PATH, strerror(errno));
+ ASSERT_EQ(0, get_active_level(before, level, sizeof(level)));
+
+ if (!strcmp(level, "confidentiality"))
+ lower = "integrity";
+ else if (!strcmp(level, "integrity"))
+ lower = "none";
+ else
+ SKIP(return, "kernel is not locked down, refusing to escalate");
+
+ errno = 0;
+ ASSERT_EQ(-1, write_status(lower));
+ ASSERT_EQ(EPERM, errno);
+
+ ASSERT_EQ(0, read_file(LOCKDOWN_PATH, after, sizeof(after)));
+ ASSERT_STREQ(before, after);
+}
+
+TEST(lockdown_same_level_rejected)
+{
+ char before[128], after[128], level[32];
+
+ if (read_file(LOCKDOWN_PATH, before, sizeof(before))) {
+ if (errno == ENOENT)
+ SKIP(return, "%s is not present", LOCKDOWN_PATH);
+ SKIP(return, "cannot read %s: %s", LOCKDOWN_PATH,
+ strerror(errno));
+ }
+ if (geteuid() != 0)
+ SKIP(return, "requires root");
+ if (status_writable())
+ SKIP(return, "cannot open %s for writing: %s",
+ LOCKDOWN_PATH, strerror(errno));
+ ASSERT_EQ(0, get_active_level(before, level, sizeof(level)));
+
+ errno = 0;
+ ASSERT_EQ(-1, write_status(level));
+ ASSERT_EQ(EPERM, errno);
+
+ ASSERT_EQ(0, read_file(LOCKDOWN_PATH, after, sizeof(after)));
+ ASSERT_STREQ(before, after);
+}
+
+TEST_HARNESS_MAIN
--
2.18.0.huawei.25
next prev parent reply other threads:[~2026-11-03 7:51 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 8:24 [PATCH -next 0/9] lockdown: fix the securityfs interface and add tests Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 1/9] lockdown: Add break in lockdown_write Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 2/9] lockdown: remove useless decrement operation Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 3/9] lockdown: bound the status string with scnprintf Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 4/9] lockdown: select SECURITYFS Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 5/9] lsm: remove unused lockdown audit data Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 6/9] lockdown: drop an unused include and simplify secfs init Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 7/9] lockdown: move lockdown_reasons next to its only user Cai Xinchen
2026-11-03 8:24 ` [PATCH -next 8/9] Documentation/ABI: document the lockdown securityfs file Cai Xinchen
2026-11-03 8:24 ` Cai Xinchen [this message]
2026-11-06 19:55 ` [PATCH -next 0/9] lockdown: fix the securityfs interface and add tests Serge E. Hallyn
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=20261103082421.3033195-10-caixinchen1@huawei.com \
--to=caixinchen1@huawei.com \
--cc=bpf@vger.kernel.org \
--cc=jmorris@namei.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=lujialin4@huawei.com \
--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
all inboxes | Powered by JetHome®