diff mbox series

[v4,4/7] Add lsm_get_self_attr03 test

Message ID 20250429-lsm-v4-4-602b7097e722@suse.com
State Superseded
Headers show
Series LSM testing suite | expand

Checks

Context Check Description
ltpci/debian_stable_s390x-linux-gnu-gcc_s390x success success
ltpci/debian_stable_powerpc64le-linux-gnu-gcc_ppc64el success success
ltpci/debian_stable_aarch64-linux-gnu-gcc_arm64 success success
ltpci/debian_stable_gcc success success
ltpci/debian_stable_gcc success success
ltpci/ubuntu_jammy_gcc success success
ltpci/ubuntu_bionic_gcc success success
ltpci/debian_oldstable_clang success success
ltpci/debian_testing_gcc success success
ltpci/fedora_latest_clang success success
ltpci/alpine_latest_gcc success success
ltpci/quay-io-centos-centos_stream9_gcc success success
ltpci/opensuse-archive_42-2_gcc success success
ltpci/debian_oldstable_gcc success success
ltpci/opensuse-leap_latest_gcc success success
ltpci/debian_testing_clang success success

Commit Message

Andrea Cervesato April 29, 2025, 7:18 a.m. UTC
From: Andrea Cervesato <andrea.cervesato@suse.com>

Verify that LSM_ATTR_CURRENT attribute is correctly recognizing
the current, active security context of the process. This is done by
checking that /proc/self/attr/current matches with the obtained value.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                                   |  1 +
 testcases/kernel/syscalls/lsm/.gitignore           |  1 +
 .../kernel/syscalls/lsm/lsm_get_self_attr03.c      | 68 ++++++++++++++++++++++
 3 files changed, 70 insertions(+)

Comments

Cyril Hrubis June 2, 2025, 12:46 p.m. UTC | #1
Hi!
> Verify that LSM_ATTR_CURRENT attribute is correctly recognizing
> the current, active security context of the process. This is done by
> checking that /proc/self/attr/current matches with the obtained value.
> 
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  runtest/syscalls                                   |  1 +
>  testcases/kernel/syscalls/lsm/.gitignore           |  1 +
>  .../kernel/syscalls/lsm/lsm_get_self_attr03.c      | 68 ++++++++++++++++++++++
>  3 files changed, 70 insertions(+)
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 73b6b98c7748f5ed31ad23d7464f1ab4fbc5f42e..d45cda4082ed87bf674ca34d315af9c162a41fe9 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -761,6 +761,7 @@ lseek11 lseek11
>  
>  lsm_get_self_attr01 lsm_get_self_attr01
>  lsm_get_self_attr02 lsm_get_self_attr02
> +lsm_get_self_attr03 lsm_get_self_attr03
>  
>  lstat01 lstat01
>  lstat01_64 lstat01_64
> diff --git a/testcases/kernel/syscalls/lsm/.gitignore b/testcases/kernel/syscalls/lsm/.gitignore
> index 9f7c9b00b026a377f1b36f483ac2c1a0adba6249..19956fdf8b9952b4850c3a20826e29ec67ea3560 100644
> --- a/testcases/kernel/syscalls/lsm/.gitignore
> +++ b/testcases/kernel/syscalls/lsm/.gitignore
> @@ -1,2 +1,3 @@
>  lsm_get_self_attr01
>  lsm_get_self_attr02
> +lsm_get_self_attr03
> diff --git a/testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c b/testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..3b767b94c025e350b9cc83d9bf2dc3061b3c6a1c
> --- /dev/null
> +++ b/testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c
> @@ -0,0 +1,68 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * Verify that LSM_ATTR_CURRENT attribute is correctly recognizing
> + * the current, active security context of the process. This is done by
> + * checking that /proc/self/attr/current matches with the obtained value.
> + */
> +
> +#include "lsm_common.h"
> +
> +static struct lsm_ctx *ctx;
> +static uint32_t page_size;
> +
> +static void run(void)
> +{
> +	tst_res(TINFO, "Verifying 'LSM_ATTR_CURRENT' attribute");
> +
> +	uint32_t count;
> +	uint32_t size = page_size;
> +	char attr[size];
> +
> +	memset(attr, 0, size);
> +	memset(ctx, 0, LSM_CTX_SIZE_DEFAULT);
> +
> +	count = TST_EXP_POSITIVE(
> +		lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, 0));
> +
> +	if (TST_RET == -1)
> +		return;
> +
> +	if (!count) {
> +		tst_res(TFAIL, "Can't read any attribute");
> +		return;
> +	}
> +
> +	read_proc_attr("current", attr, page_size);
> +
> +	TST_EXP_EQ_STR(attr, (char *)ctx->ctx);
> +
> +	struct lsm_ctx *next = ctx;
                                ^
				next_ctx(next)

Otherwise we will fail the check below.

> +	for (uint32_t i = 1; i < count; i++) {
> +		TST_EXP_EXPR(strcmp(attr, (char *)next->ctx) != 0,
> +			"Attribute and next LSM context must be different");
> +
> +		next = next_ctx(next);
> +	}

Have you actually tried this on a machine with more than one LSM active?
Andrea Cervesato June 2, 2025, 1:18 p.m. UTC | #2
On 6/2/25 14:46, Cyril Hrubis wrote:
> 				next_ctx(next)
>
> Otherwise we will fail the check below.
Right.
>
>> +	for (uint32_t i = 1; i < count; i++) {
>> +		TST_EXP_EXPR(strcmp(attr, (char *)next->ctx) != 0,
>> +			"Attribute and next LSM context must be different");
>> +
>> +		next = next_ctx(next);
>> +	}
> Have you actually tried this on a machine with more than one LSM active?
Fixed, also I think I we to check if "/sys/kernel/security/lsm" exists. 
I guess it doesn't exist if no LSM are present.

- Andrea
Cyril Hrubis June 2, 2025, 2:38 p.m. UTC | #3
Hi!
> > 				next_ctx(next)
> >
> > Otherwise we will fail the check below.
> Right.

Feel free to add my Reviewed-by: with that fixed.

> >> +	for (uint32_t i = 1; i < count; i++) {
> >> +		TST_EXP_EXPR(strcmp(attr, (char *)next->ctx) != 0,
> >> +			"Attribute and next LSM context must be different");
> >> +
> >> +		next = next_ctx(next);
> >> +	}
> > Have you actually tried this on a machine with more than one LSM active?
> Fixed, also I think I we to check if "/sys/kernel/security/lsm" exists. 
> I guess it doesn't exist if no LSM are present.

We do call verify_supported_attr_current(); in the setup, that should be
enough. What I was asking for was if you ever tested this code on a
mach9ine where the count > 1 so that the loop actually triggered.
Andrea Cervesato June 2, 2025, 4:35 p.m. UTC | #4
On 6/2/25 16:38, Cyril Hrubis wrote:
> Hi!
>>> 				next_ctx(next)
>>>
>>> Otherwise we will fail the check below.
>> Right.
> Feel free to add my Reviewed-by: with that fixed.
Ok
>
>>>> +	for (uint32_t i = 1; i < count; i++) {
>>>> +		TST_EXP_EXPR(strcmp(attr, (char *)next->ctx) != 0,
>>>> +			"Attribute and next LSM context must be different");
>>>> +
>>>> +		next = next_ctx(next);
>>>> +	}
>>> Have you actually tried this on a machine with more than one LSM active?
>> Fixed, also I think I we to check if "/sys/kernel/security/lsm" exists.
>> I guess it doesn't exist if no LSM are present.
> We do call verify_supported_attr_current(); in the setup, that should be
> enough. What I was asking for was if you ever tested this code on a
> mach9ine where the count > 1 so that the loop actually triggered.
>
I modify verify_supported_attr_current() in order to check if 
/sys/kernel/security/lsm exists.
And yes, I tested the code on a VM with 7 LSM(s).

Gonna send the next patch and then merge.

Thanks,
Andrea
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 73b6b98c7748f5ed31ad23d7464f1ab4fbc5f42e..d45cda4082ed87bf674ca34d315af9c162a41fe9 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -761,6 +761,7 @@  lseek11 lseek11
 
 lsm_get_self_attr01 lsm_get_self_attr01
 lsm_get_self_attr02 lsm_get_self_attr02
+lsm_get_self_attr03 lsm_get_self_attr03
 
 lstat01 lstat01
 lstat01_64 lstat01_64
diff --git a/testcases/kernel/syscalls/lsm/.gitignore b/testcases/kernel/syscalls/lsm/.gitignore
index 9f7c9b00b026a377f1b36f483ac2c1a0adba6249..19956fdf8b9952b4850c3a20826e29ec67ea3560 100644
--- a/testcases/kernel/syscalls/lsm/.gitignore
+++ b/testcases/kernel/syscalls/lsm/.gitignore
@@ -1,2 +1,3 @@ 
 lsm_get_self_attr01
 lsm_get_self_attr02
+lsm_get_self_attr03
diff --git a/testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c b/testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c
new file mode 100644
index 0000000000000000000000000000000000000000..3b767b94c025e350b9cc83d9bf2dc3061b3c6a1c
--- /dev/null
+++ b/testcases/kernel/syscalls/lsm/lsm_get_self_attr03.c
@@ -0,0 +1,68 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * Verify that LSM_ATTR_CURRENT attribute is correctly recognizing
+ * the current, active security context of the process. This is done by
+ * checking that /proc/self/attr/current matches with the obtained value.
+ */
+
+#include "lsm_common.h"
+
+static struct lsm_ctx *ctx;
+static uint32_t page_size;
+
+static void run(void)
+{
+	tst_res(TINFO, "Verifying 'LSM_ATTR_CURRENT' attribute");
+
+	uint32_t count;
+	uint32_t size = page_size;
+	char attr[size];
+
+	memset(attr, 0, size);
+	memset(ctx, 0, LSM_CTX_SIZE_DEFAULT);
+
+	count = TST_EXP_POSITIVE(
+		lsm_get_self_attr(LSM_ATTR_CURRENT, ctx, &size, 0));
+
+	if (TST_RET == -1)
+		return;
+
+	if (!count) {
+		tst_res(TFAIL, "Can't read any attribute");
+		return;
+	}
+
+	read_proc_attr("current", attr, page_size);
+
+	TST_EXP_EQ_STR(attr, (char *)ctx->ctx);
+
+	struct lsm_ctx *next = ctx;
+
+	for (uint32_t i = 1; i < count; i++) {
+		TST_EXP_EXPR(strcmp(attr, (char *)next->ctx) != 0,
+			"Attribute and next LSM context must be different");
+
+		next = next_ctx(next);
+	}
+}
+
+static void setup(void)
+{
+	verify_supported_attr_current();
+
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.min_kver = "6.8",
+	.bufs = (struct tst_buffers[]) {
+		{&ctx, .size = LSM_CTX_SIZE_DEFAULT},
+		{}
+	},
+};