Linux Security Modules development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Cai Xinchen" <caixinchen1@huawei.com>
Cc: linux-security-module@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH -next 9/9] selftests/lsm: add lockdown securityfs test
Date: Tue, 03 Nov 2026 08:02:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-194791@kernel.org> (raw)
In-Reply-To: <20261103082421.3033195-10-caixinchen1@huawei.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] selftests/lsm: duplicated setup/teardown and double open in lockdown_test.c
--

commit 5cebacd6e5feb5064c2370aa4d99aad209688038
Author: Cai Xinchen <caixinchen1@huawei.com>

selftests/lsm: add lockdown securityfs test

This commit adds a kselftest for the lockdown securityfs interface to
check the read format, reject unknown levels, and verify that an already
locked down kernel rejects the current or a lower level. It also moves
the file reading logic in common.c into a shared read_file() helper.

> diff --git a/tools/testing/selftests/lsm/lockdown_test.c b/tools/testing/selftests/lsm/lockdown_test.c
> new file mode 100644
> index 0000000000000..35109833da373
> --- /dev/null
> +++ b/tools/testing/selftests/lsm/lockdown_test.c
> @@ -0,0 +1,176 @@

[ ... ]

> +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;
> +}

[ ... ]

> +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);
> +}

[Severity: Low]
This isn't a bug, but lockdown_invalid_write, lockdown_no_downgrade, and
lockdown_same_level_rejected duplicate the same initial read_file() and
SKIP() checks, root check, status_writable() check, active level parsing,
and trailing read_file() plus ASSERT_STREQ(before, after) check.

Additionally, in each of those write tests, status_writable() opens
LOCKDOWN_PATH with O_WRONLY and immediately closes it, only for
write_status() to open and close LOCKDOWN_PATH with O_WRONLY a second
time.

Could this use FIXTURE(), FIXTURE_SETUP(), FIXTURE_TEARDOWN(), and
TEST_F() from kselftest_harness.h (or a shared setup/write helper) to
avoid the duplicated prerequisite/post-condition blocks and redundant
file opens?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261103082421.3033195-1-caixinchen1@huawei.com?part=9

      reply	other threads:[~2026-11-03  8:02 UTC|newest]

Thread overview: 19+ 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  7:54   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 2/9] lockdown: remove useless decrement operation Cai Xinchen
2026-11-03  7:54   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 3/9] lockdown: bound the status string with scnprintf Cai Xinchen
2026-11-03  7:54   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 4/9] lockdown: select SECURITYFS Cai Xinchen
2026-11-03  7:54   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 5/9] lsm: remove unused lockdown audit data Cai Xinchen
2026-11-03  7:55   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 6/9] lockdown: drop an unused include and simplify secfs init Cai Xinchen
2026-11-03  7:55   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 7/9] lockdown: move lockdown_reasons next to its only user Cai Xinchen
2026-11-03  7:55   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 8/9] Documentation/ABI: document the lockdown securityfs file Cai Xinchen
2026-11-03  7:59   ` sashiko-bot
2026-11-03  8:24 ` [PATCH -next 9/9] selftests/lsm: add lockdown securityfs test Cai Xinchen
2026-11-03  8:02   ` sashiko-bot [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=sashiko-outbox-194791@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=caixinchen1@huawei.com \
    --cc=linux-security-module@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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