Re: [PATCH v4 8/8] selftests/lsm: add lockdown securityfs test
From: Nicolas Bouchinet
Date: Mon Nov 30 2026 - 09:53:47 EST
On Mon, 30 Nov 2026 16:29:14 +0800, Cai Xinchen <caixinchen1@xxxxxxxxxx> wrote:
> diff --git a/tools/testing/selftests/lsm/common.c b/tools/testing/selftests/lsm/common.c
> index 927dce4f04cb2..aa01203a65f9c 100644
> --- a/tools/testing/selftests/lsm/common.c
> +++ b/tools/testing/selftests/lsm/common.c
> @@ -50,23 +51,51 @@ int read_proc_attr(const char *attr, char *value, size_t size)
> [ ... skip 38 lines ... ]
> + * 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;
EOVERFLOW is not correct in this case.
>
> diff --git a/tools/testing/selftests/lsm/lockdown_test.c b/tools/testing/selftests/lsm/lockdown_test.c
> new file mode 100644
> index 0000000000000..d9f8696a687bc
> --- /dev/null
> +++ b/tools/testing/selftests/lsm/lockdown_test.c
> @@ -0,0 +1,272 @@
> [ ... skip 137 lines ... ]
> + TH_LOG("unknown active lockdown level: %s", self->level);
> + ASSERT_TRUE(false);
> + }
> +
> + ASSERT_STREQ(expected, self->status);
> +}
Not sure this test is worth-it as stand-alone. Although, you could use
this simple pattern with static strings to test current lockdown level
in your other tests instead of the current `get_active_level()` function
and it would be sufficient.
An easy way to achieve this is like in lockdown code is through an enum,
with NONE, INTEGRITY and CONFIDENTIALITY + an array with matching
expected strings for each enum value (i.e "[none] integrity
confidentiality").
Your `get_current_level()` could simply be a for loop iterating over the
array + strcmp and returning the index matching the current active
level.
Something like this :
```C
enum lockdown_level {
NONE,
INTEGRITY,
CONFIDENTIALITY,
};
const char *const lockdown_active_states[CONFIDENTIALITY + 1] = {
[NONE] = "[none] integrity confidentiality",
[INTEGRITY] = "none [integrity] confidentiality",
[CONFIDENTIALITY] = "none integrity [confidentiality]",
};
// Replace with your file read.
const char current[] = "[none] integrity confidentiality";
int get_active_level() {
for (int i = 0; i < 3; i++)
if (!strcmp(current, lockdown_active_states[i]))
return i;
return -1;
}
```
Thus much of your tests could compare to the enum value and not against
strings. Keep those tests concise and simple.
> [ ... skip 29 lines ... ]
> + ASSERT_LT(fd, 0);
> + if (errno == EROFS)
> + SKIP(return, "%s is on a read-only filesystem", LOCKDOWN_PATH);
> +
> + ASSERT_EQ(EACCES, errno);
> +}
We should be checking that the lockdown securityfs file_ops are working as
expected (read works, write works, ...).
This test checks that the file mode and capabilities are working as
expected and this is really not in our scope. Please remove it,
including `has_cap_dac_override`.
> [ ... skip 34 lines ... ]
> + 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");
You should change it to "lockdown is already at the highest level".
--
Nicolas Bouchinet <nicolas.bouchinet@xxxxxxxxxxxxxxxxx>