Skip to contentSkip to conversation

landlock: Selftest fixes and cleanups

PATCH New Günther Noack 2 participants 55 messages Nov 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover landlock: Selftest fixes and cleanups

A collection of Landlock Selftest fixes
=======================================

This patch set is bundled together to be slightly less spammy, but the patches are mostly independent. The commits are ordered such that the simpler and more orthogonal patches come earlier.

The patch set is LLM-assisted (and that turned out to be very useful for spotting errors). Needless to say, I have verified all commits by hand, and reworked them where necessary.

The last few patches are a bit more cross-cutting and probably more prone to merge conflicts, as they correct cases where we have been using EXPECT and ASSERT in the wrong way (details in the commit messages). I believe that the rationale for each of these is reasonable, but I am happy to discuss.

Motivation
==========

LLMs (and humans ;-)) have an unfortunate tendency to pick up and replicate bad coding style patterns, so I hope that a broader cleanup of the test style will avoid this spreading further.

–Günther

---
Changes in v2:
- Rebased onto master (v7.4-rc1-132-g05441d697f7f).
- Dropped the "Assisted-by: LLM" trailers (not required any more).
- Applied the same ASSERT/EXPECT rules to the test code that was added in the meantime (cap_test.c and ns_test.c, plus new checks in base_test.c and trace_test.c):
- "Assert tracefs_extract_field() results that are used": also the new handled_permissions check in trace.create_ruleset.
- "Assert audit initialization in fixture setups": also the cap_audit, ns_audit and ns_proc_open fixtures.
- "Assert audit_count_records() before using its result": also in cap_test.c and ns_test.c.
- "Use ASSERT for syscall failures before errno checks": also in cap_test.c and ns_test.c.
- "Use EXPECT for checks that later steps do not need": also the errno and WEXITSTATUS() checks in cap_test.c and ns_test.c, and the errno check in base_test.c's ruleset_with_unknown_permission.
- Dropped the note about depending on Wang Yan's snprintf() fix, which has landed as commit 8e4c736cffdd ("selftests/landlock: Fix snprintf truncation checks in test files").
- Link to v1: https://patch.msgid.link/[email protected]

To: Mickaël Salaün <[email protected]>
To: Günther Noack <[email protected]>
To: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Günther Noack (27):
selftests/landlock: Propagate grandchild failures in unix_scoping selftests/landlock: Propagate EXPECT failures from forked children selftests/landlock: Check the thread result in audit.thread selftests/landlock: Zero-initialize the buffer in inconsistent_attr selftests/landlock: Fix the openat() success check in fs_bench selftests/landlock: Fix the reported clock tick rate in fs_bench selftests/landlock: Fix the ruleset_fd check in empty_or_same_ruleset selftests/landlock: Handle a missing control message in recv_fd() selftests/landlock: Assert tracefs_extract_field() results that are used selftests/landlock: Assert audit initialization in fixture setups selftests/landlock: Assert helper results that are used afterwards selftests/landlock: Assert audit_count_records() before using its result selftests/landlock: Fix memory leak in audit_init_filter_exe() selftests/landlock: Make audit_message large enough for any exe filter selftests/landlock: Close leaked file descriptors selftests/landlock: Check errno for combined VERSION and ERRATA flags selftests/landlock: Fix SIGURG handler setup in scoped_signal_test selftests/landlock: Fix regex_escape() error handling in matches_log_fs_extra() selftests/landlock: Fix off-by-one in regex_escape() selftests/landlock: Fix the snprintf() error check in matches_log_fs_extra() selftests/landlock: Use _exit() in the named_pipe_ioctl child selftests/landlock: Fix messages and comments in helper programs selftests/landlock: Fix stale comments in common.h and audit.h selftests/landlock: Use ASSERT for syscall failures before errno checks selftests/landlock: Assert WIFEXITED() before checking the exit status selftests/landlock: Use EXPECT for closing FDs and stopping threads selftests/landlock: Use EXPECT for checks that later steps do not need

 tools/testing/selftests/landlock/audit.h           |   20 +-
 tools/testing/selftests/landlock/audit_test.c      |   56 +-
 tools/testing/selftests/landlock/base_test.c       |  192 +--
 tools/testing/selftests/landlock/cap_test.c        |   70 +-
 tools/testing/selftests/landlock/common.h          |   32 +-
 tools/testing/selftests/landlock/fs_bench.c        |    5 +-
 tools/testing/selftests/landlock/fs_test.c         | 1300 ++++++++++----------
 tools/testing/selftests/landlock/net_test.c        |   64 +-
 tools/testing/selftests/landlock/ns_test.c         |  124 +-
 tools/testing/selftests/landlock/ptrace_test.c     |   44 +-
 .../selftests/landlock/sandbox-and-launch.c        |    6 +-
 .../selftests/landlock/scoped_abstract_unix_test.c |   45 +-
 .../selftests/landlock/scoped_signal_test.c        |   23 +-
 tools/testing/selftests/landlock/scoped_test.c     |    2 +-
 tools/testing/selftests/landlock/trace_fs_test.c   |    8 +-
 tools/testing/selftests/landlock/trace_test.c      |   28 +-
 tools/testing/selftests/landlock/tsync_test.c      |   22 +-
 .../testing/selftests/landlock/wait-pipe-sandbox.c |   16 +-
 tools/testing/selftests/landlock/wait-pipe.c       |    2 +-
 19 files changed, 1051 insertions(+), 1008 deletions(-)

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261104-selftests-refactor-4741be042e4f

Best regards,

Signature · 1 line
Günther Noack <[email protected]>
#1 lore raw

PATCH v2 1/27 selftests/landlock: Propagate grandchild failures in unix_scoping

scoped_vs_unscoped.unix_scoping starts a child process and a grandchild process. Add the missing check for the grandchild's exit status and propagate it upwards.

---
 tools/testing/selftests/landlock/scoped_abstract_unix_test.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/landlock/scoped_abstract_unix_test.c +5 -0
index 5dc0debacb2a..e006dba61652 100644
@@ -608,6 +608,11 @@ TEST_F(scoped_vs_unscoped, unix_scoping)
 		ASSERT_EQ(grand_child, waitpid(grand_child, &status, 0));
 		EXPECT_EQ(0, close(stream_server_child));
 		EXPECT_EQ(0, close(dgram_server_child));
+
+		if (WIFSIGNALED(status) || !WIFEXITED(status) ||
+		    WEXITSTATUS(status) != EXIT_SUCCESS)
+			_metadata->exit_code = KSFT_FAIL;
+		_exit(_metadata->exit_code);
 		return;
 	}
 	EXPECT_EQ(0, close(pipe_parent[0]));
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v2 01/27] selftests/landlock: Propagate grandchild failures in unix_scoping

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

scoped_vs_unscoped.unix_scoping starts a child process and a grandchild process. Add the missing check for the grandchild's exit status and propagate it upwards.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/27 selftests/landlock: Propagate EXPECT failures from forked children

In reply to Günther Noack

A failed ASSERT in a forked child calls abort(), so the parent sees the child die from a signal and the test fails. A failed EXPECT only sets _metadata->exit_code, and a child that exits with a fixed status of 0 hides that failure from the parent. The child in trace_fown.deny_scope_fown has this problem.

Exit with _metadata->exit_code in that child. Do the same in the audit.log_subdomains_off_fork children. They only use ASSERT for now, but this keeps them correct when a check becomes an EXPECT.

---
 tools/testing/selftests/landlock/audit_test.c         | 4 ++--
 tools/testing/selftests/landlock/scoped_signal_test.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/landlock/audit_test.c +2 -2
index 4bea8c880a4d..502a0825d877 100644
@@ -314,7 +314,7 @@ TEST_F(audit, log_subdomains_off_fork)
 		ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
 		ASSERT_EQ(-1, kill(getppid(), 0));
 		ASSERT_EQ(EPERM, errno);
-		_exit(0);
+		_exit(_metadata->exit_code);
 		return;
 	}
 
@@ -347,7 +347,7 @@ TEST_F(audit, log_subdomains_off_fork)
 		ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
 		ASSERT_EQ(-1, kill(getppid(), 0));
 		ASSERT_EQ(EPERM, errno);
-		_exit(0);
+		_exit(_metadata->exit_code);
 		return;
 	}
 
tools/testing/selftests/landlock/scoped_signal_test.c +1 -1
index 259cdcc8aa5c..47307dd2e242 100644
@@ -1090,7 +1090,7 @@ TEST_F(trace_fown, deny_scope_fown)
 		ASSERT_EQ(1, write(pipe_child[1], ".", 1));
 		EXPECT_EQ(0, close(pipe_child[1]));
 
-		_exit(0);
+		_exit(_metadata->exit_code);
 		return;
 	}
 	EXPECT_EQ(0, close(pipe_parent[0]));
Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH v2 02/27] selftests/landlock: Propagate EXPECT failures from forked children

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

A failed ASSERT in a forked child calls abort(), so the parent sees the child die from a signal and the test fails. A failed EXPECT only sets _metadata->exit_code, and a child that exits with a fixed status of 0 hides that failure from the parent. The child in trace_fown.deny_scope_fown has this problem.

Exit with _metadata->exit_code in that child. Do the same in the audit.log_subdomains_off_fork children. They only use ASSERT for now, but this keeps them correct when a check becomes an EXPECT.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH v2 3/27 selftests/landlock: Check the thread result in audit.thread

In reply to Günther Noack

thread_audit_test() reports failures through its return value, but audit.thread joins the thread with a NULL result pointer, so these failures go unnoticed.

Check the returned value, as the other thread-based audit tests do.

---
 tools/testing/selftests/landlock/audit_test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/landlock/audit_test.c +3 -1
index 502a0825d877..81cb31aa32ad 100644
@@ -227,6 +227,7 @@ TEST_F(audit, thread)
 	__u64 allocated_dom = 2;
 	__u64 deallocated_dom = 3;
 	pthread_t thread;
+	void *thread_ret;
 	int pipe_child[2], pipe_parent[2];
 	char buffer;
 	struct thread_data child_data;
@@ -270,7 +271,8 @@ TEST_F(audit, thread)
 	/* Signals the thread to exit, which will generate a domain deallocation. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
 	EXPECT_EQ(0, close(pipe_parent[1]));
-	ASSERT_EQ(0, pthread_join(thread, NULL));
+	ASSERT_EQ(0, pthread_join(thread, &thread_ret));
+	EXPECT_EQ(NULL, thread_ret);
 
 	EXPECT_EQ(0, matches_log_domain_deallocated(
 			     self->audit_fd, 1, denial_dom, &deallocated_dom));
Signature · 1 line
2.56.0
#6 lore raw

Re: [PATCH v2 03/27] selftests/landlock: Check the thread result in audit.thread

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

thread_audit_test() reports failures through its return value, but audit.thread joins the thread with a NULL result pointer, so these failures go unnoticed.

Check the returned value, as the other thread-based audit tests do.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

PATCH v2 4/27 selftests/landlock: Zero-initialize the buffer in inconsistent_attr

In reply to Günther Noack

This buffer is passed to landlock_create_ruleset() a bit further down, expecting an ENOMSG error. This is only guaranteed when the buffer is zero-initialized.

---
 tools/testing/selftests/landlock/base_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/base_test.c +1 -1
index 89c0d96607de..0bf8632b725f 100644
@@ -25,7 +25,7 @@
 TEST(inconsistent_attr)
 {
 	const long page_size = sysconf(_SC_PAGESIZE);
-	char *const buf = malloc(page_size + 1);
+	char *const buf = calloc(1, page_size + 1);
 	struct landlock_ruleset_attr *const ruleset_attr = (void *)buf;
 
 	ASSERT_NE(NULL, buf);
Signature · 1 line
2.56.0
#8 lore raw

Re: [PATCH v2 04/27] selftests/landlock: Zero-initialize the buffer in inconsistent_attr

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

This buffer is passed to landlock_create_ruleset() a bit further down, expecting an ENOMSG error. This is only guaranteed when the buffer is zero-initialized.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw

PATCH v2 5/27 selftests/landlock: Fix the openat() success check in fs_bench

In reply to Günther Noack

fs_bench expects openat() to be denied when Landlock is enabled, but only treats a returned FD of 0 as success. Any other valid FD falls through to the errno check, which then reports a stale errno value.

Check for any non-negative FD instead.

---
 tools/testing/selftests/landlock/fs_bench.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/fs_bench.c +1 -1
index d13a88dcd1ed..78d1efcb2d20 100644
@@ -189,7 +189,7 @@ int main(int argc, char *argv[])
 		fd = openat(curr, "file.txt", O_CREAT | O_TRUNC | O_WRONLY,
 			    0600);
 		if (use_landlock) {
-			if (fd == 0)
+			if (fd >= 0)
 				errx(1, "openat succeeded, expected EACCES");
 			if (errno != EACCES)
 				err(1, "openat expected EACCES, but got");
Signature · 1 line
2.56.0
#10 lore raw

Re: [PATCH v2 05/27] selftests/landlock: Fix the openat() success check in fs_bench

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

fs_bench expects openat() to be denied when Landlock is enabled, but only treats a returned FD of 0 as success. Any other valid FD falls through to the errno check, which then reports a stale errno value.

Check for any non-negative FD instead.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#11 lore raw

PATCH v2 6/27 selftests/landlock: Fix the reported clock tick rate in fs_bench

In reply to Günther Noack

Since time is measured using times(2), ticks per second are obtained using sysconf(_SC_CLK_TCK) instead of CLOCKS_PER_SEC. (c.f. times(2) man page)

---
 tools/testing/selftests/landlock/fs_bench.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/landlock/fs_bench.c +1 -2
index 78d1efcb2d20..05082562d272 100644
@@ -32,7 +32,6 @@
 #include <sys/prctl.h>
 #include <sys/stat.h>
 #include <sys/times.h>
-#include <time.h>
 #include <unistd.h>
 
 #include "wrappers.h"
@@ -206,7 +205,7 @@ int main(int argc, char *argv[])
 	       end_time.tms_stime - start_time.tms_stime);
 	printf("User  : %ld clocks\n",
 	       end_time.tms_utime - start_time.tms_utime);
-	printf("Clocks per second: %ld\n", CLOCKS_PER_SEC);
+	printf("Clocks per second: %ld\n", sysconf(_SC_CLK_TCK));
 
 	close(curr);
 
Signature · 1 line
2.56.0
#12 lore raw

Re: [PATCH v2 06/27] selftests/landlock: Fix the reported clock tick rate in fs_bench

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

Since time is measured using times(2), ticks per second are obtained using sysconf(_SC_CLK_TCK) instead of CLOCKS_PER_SEC. (c.f. times(2) man page)

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#13 lore raw

PATCH v2 7/27 selftests/landlock: Fix the ruleset_fd check in empty_or_same_ruleset

In reply to Günther Noack

ASSERT_LE(-1, ruleset_fd) is always true, so the test does not check that creating a ruleset with an empty handled_access_fs fails. Check for -1 instead.

---
 tools/testing/selftests/landlock/fs_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/fs_test.c +1 -1
index f725882fa4a7..7d399c097c93 100644
@@ -1456,7 +1456,7 @@ TEST_F_FORK(layout1, empty_or_same_ruleset)
 	/* Tests empty handled_access_fs. */
 	ruleset_fd =
 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
-	ASSERT_LE(-1, ruleset_fd);
+	ASSERT_EQ(-1, ruleset_fd);
 	ASSERT_EQ(ENOMSG, errno);
 
 	/* Enforces policy which denies read access to all files. */
Signature · 1 line
2.56.0
#14 lore raw

Re: [PATCH v2 07/27] selftests/landlock: Fix the ruleset_fd check in empty_or_same_ruleset

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

ASSERT_LE(-1, ruleset_fd) is always true, so the test does not check that creating a ruleset with an empty handled_access_fs fails. Check for -1 instead.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#15 lore raw

PATCH v2 8/27 selftests/landlock: Handle a missing control message in recv_fd()

In reply to Günther Noack

CMSG_FIRSTHDR() returns NULL when recvmsg(2) did not receive any ancillary data, e.g. when the peer closed the socket without sending an FD. recv_fd() dereferences the result unconditionally and crashes instead of returning an error.

Return -EIO in that case.

---
 tools/testing/selftests/landlock/common.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/common.h +1 -1
index e29406900c04..6c2aa913ff11 100644
@@ -171,7 +171,7 @@ static int __maybe_unused recv_fd(int usock)
 		return -errno;
 
 	cmsg = CMSG_FIRSTHDR(&msg);
-	if (cmsg->cmsg_len != CMSG_LEN(sizeof(fd_rx)))
+	if (!cmsg || cmsg->cmsg_len != CMSG_LEN(sizeof(fd_rx)))
 		return -EIO;
 
 	memcpy(&fd_rx, CMSG_DATA(cmsg), sizeof(fd_rx));
Signature · 1 line
2.56.0
#16 lore raw

Re: [PATCH v2 08/27] selftests/landlock: Handle a missing control message in recv_fd()

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

CMSG_FIRSTHDR() returns NULL when recvmsg(2) did not receive any ancillary data, e.g. when the peer closed the socket without sending an FD. recv_fd() dereferences the result unconditionally and crashes instead of returning an error.

Return -EIO in that case.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#17 lore raw

PATCH v2 9/27 selftests/landlock: Assert tracefs_extract_field() results that are used

In reply to Günther Noack

Several trace tests check tracefs_extract_field() with EXPECT_EQ() and then compare the extracted field. If the extraction fails, the field buffer is left uninitialized and the following comparison reads it.

Use ASSERT_EQ() for these calls, as the other trace tests already do.

---
 tools/testing/selftests/landlock/trace_test.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
tools/testing/selftests/landlock/trace_test.c +11 -11
index cb49ce8dbeda..1795d41b779f 100644
@@ -175,25 +175,25 @@ TEST_F(trace, create_ruleset)
 	}
 
 	/* Verify handled_fs matches what we requested. */
-	EXPECT_EQ(0,
+	ASSERT_EQ(0,
 		  tracefs_extract_field(buf, REGEX_CREATE_RULESET(TRACE_TASK),
 					"handled_fs", field, sizeof(field)));
 	EXPECT_STREQ("read_file", field);
 
 	/* Verify handled_net matches. */
-	EXPECT_EQ(0,
+	ASSERT_EQ(0,
 		  tracefs_extract_field(buf, REGEX_CREATE_RULESET(TRACE_TASK),
 					"handled_net", field, sizeof(field)));
 	EXPECT_STREQ("bind_tcp", field);
 
 	/* Verify that no permission is handled. */
-	EXPECT_EQ(0, tracefs_extract_field(
+	ASSERT_EQ(0, tracefs_extract_field(
 			     buf, REGEX_CREATE_RULESET(TRACE_TASK),
 			     "handled_permissions", field, sizeof(field)));
 	EXPECT_STREQ("", field);
 
 	/* Verify version is 0 at creation (no rules added yet). */
-	EXPECT_EQ(0,
+	ASSERT_EQ(0,
 		  tracefs_extract_field(buf, REGEX_CREATE_RULESET(TRACE_TASK),
 					"ruleset", field, sizeof(field)));
 	/* Format is <hex>.<dec>; version is after the dot. */
@@ -363,7 +363,7 @@ TEST_F(trace, create_domain)
 	}
 
 	/* Extract the domain ID from create_domain. */
-	EXPECT_EQ(0, tracefs_extract_field(buf, REGEX_CREATE_DOMAIN(TRACE_TASK),
+	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CREATE_DOMAIN(TRACE_TASK),
 					   "domain", domain_id,
 					   sizeof(domain_id)));
 
@@ -371,7 +371,7 @@ TEST_F(trace, create_domain)
 	EXPECT_NE(0, strcmp(domain_id, "0"));
 
 	/* Verify parent=0 (first restriction, no prior domain). */
-	EXPECT_EQ(0, tracefs_extract_field(buf, REGEX_CREATE_DOMAIN(TRACE_TASK),
+	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CREATE_DOMAIN(TRACE_TASK),
 					   "parent", parent_id,
 					   sizeof(parent_id)));
 	EXPECT_STREQ("0", parent_id);
@@ -387,7 +387,7 @@ TEST_F(trace, create_domain)
 		TH_LOG("Expected check_rule_inode events\n%s", buf);
 	}
 
-	EXPECT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK),
+	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_CHECK_RULE_FS(TRACE_TASK),
 					   "domain", check_domain,
 					   sizeof(check_domain)));
 	EXPECT_STREQ(domain_id, check_domain);
@@ -621,7 +621,7 @@ TEST_F(trace, free_domain)
 	}
 
 	/* Verify denials count matches the single denial we triggered. */
-	EXPECT_EQ(0, tracefs_extract_field(buf, REGEX_FREE_DOMAIN(KWORKER_TASK),
+	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_FREE_DOMAIN(KWORKER_TASK),
 					   "denials", denials_field,
 					   sizeof(denials_field)));
 	EXPECT_STREQ("1", denials_field);
@@ -906,7 +906,7 @@ TEST_F(trace, non_audit_visible_denial_counting)
 	{
 		TH_LOG("free_domain event not found after 10 retries");
 	}
-	EXPECT_EQ(0, tracefs_extract_field(buf, REGEX_FREE_DOMAIN(KWORKER_TASK),
+	ASSERT_EQ(0, tracefs_extract_field(buf, REGEX_FREE_DOMAIN(KWORKER_TASK),
 					   "denials", denials_field,
 					   sizeof(denials_field)));
 	EXPECT_STREQ("1", denials_field);
@@ -955,7 +955,7 @@ TEST_F(trace, add_rule_net_port_fields)
 	 * (landlock_net_port_attr.port).  On little-endian, htons(8080) is
 	 * 36895, so this comparison catches byte-order bugs.
 	 */
-	EXPECT_EQ(0, tracefs_extract_field(buf,
+	ASSERT_EQ(0, tracefs_extract_field(buf,
 					   REGEX_ADD_RULE_NET_PORT(TRACE_TASK),
 					   "port", field, sizeof(field)));
 	EXPECT_STREQ("8080", field);
@@ -965,7 +965,7 @@ TEST_F(trace, add_rule_net_port_fields)
 	 * net access bits are unhandled because the ruleset only handles
 	 * BIND_TCP).
 	 */
-	EXPECT_EQ(0, tracefs_extract_field(
+	ASSERT_EQ(0, tracefs_extract_field(
 			     buf, REGEX_ADD_RULE_NET_PORT(TRACE_TASK),
 			     "access_rights", field, sizeof(field)));
 	EXPECT_STREQ("bind_tcp|connect_tcp|bind_udp|connect_send_udp", field);
Signature · 1 line
2.56.0
#18 lore raw

Re: [PATCH v2 09/27] selftests/landlock: Assert tracefs_extract_field() results that are used

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

Several trace tests check tracefs_extract_field() with EXPECT_EQ() and then compare the extracted field. If the extraction fails, the field buffer is left uninitialized and the following comparison reads it.

Use ASSERT_EQ() for these calls, as the other trace tests already do.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#19 lore raw

PATCH v2 10/27 selftests/landlock: Assert audit initialization in fixture setups

In reply to Günther Noack

The audit fixtures check the result of audit_init_with_exe_filter() and audit_init() with EXPECT_LE() and continue with a negative audit FD when the initialization fails, e.g. when auditd holds the audit socket. Every following audit check then fails with a less useful message.

Use ASSERT_*() for the audit FD and for the audit_exec filter initialization, whose result is used by the following calls.

---
 tools/testing/selftests/landlock/audit_test.c                | 8 ++++----
 tools/testing/selftests/landlock/cap_test.c                  | 2 +-
 tools/testing/selftests/landlock/fs_test.c                   | 6 +++---
 tools/testing/selftests/landlock/net_test.c                  | 2 +-
 tools/testing/selftests/landlock/ns_test.c                   | 4 ++--
 tools/testing/selftests/landlock/ptrace_test.c               | 2 +-
 tools/testing/selftests/landlock/scoped_abstract_unix_test.c | 2 +-
 7 files changed, 13 insertions(+), 13 deletions(-)
tools/testing/selftests/landlock/audit_test.c +4 -4
index 81cb31aa32ad..63b1b2a04eee 100644
@@ -49,7 +49,7 @@ FIXTURE_SETUP(audit)
 	disable_caps(_metadata);
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd)
+	ASSERT_LE(0, self->audit_fd)
 	{
 		const char *error_msg;
 
@@ -652,7 +652,7 @@ FIXTURE_SETUP(audit_flags)
 	disable_caps(_metadata);
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd)
+	ASSERT_LE(0, self->audit_fd)
 	{
 		const char *error_msg;
 
@@ -841,7 +841,7 @@ FIXTURE_SETUP(audit_exec)
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 
 	self->audit_fd = audit_init();
-	EXPECT_LE(0, self->audit_fd)
+	ASSERT_LE(0, self->audit_fd)
 	{
 		const char *error_msg;
 
@@ -854,7 +854,7 @@ FIXTURE_SETUP(audit_exec)
 	}
 
 	/* Applies test filter for the bin_wait_pipe_sandbox program. */
-	EXPECT_EQ(0, audit_init_filter_exe(&self->audit_filter,
+	ASSERT_EQ(0, audit_init_filter_exe(&self->audit_filter,
 					   bin_wait_pipe_sandbox));
 	EXPECT_EQ(0, audit_filter_exe(self->audit_fd, &self->audit_filter,
 				      AUDIT_ADD_RULE));
tools/testing/selftests/landlock/cap_test.c +1 -1
index 3d7fd02e21ed..7b3b724e4406 100644
@@ -775,7 +775,7 @@ FIXTURE_SETUP(cap_audit)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	clear_cap(_metadata, CAP_AUDIT_CONTROL);
 }
 
tools/testing/selftests/landlock/fs_test.c +3 -3
index 7d399c097c93..7ff94755b029 100644
@@ -7504,7 +7504,7 @@ FIXTURE_SETUP(audit_layout1)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	disable_caps(_metadata);
 }
 
@@ -8028,7 +8028,7 @@ FIXTURE_SETUP(audit_quiet_layout1)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	clear_cap(_metadata, CAP_AUDIT_CONTROL);
 
 	if (getenv("DEBUG_QUIET_TESTS"))
@@ -9605,7 +9605,7 @@ FIXTURE_SETUP(audit_quiet_rename)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	clear_cap(_metadata, CAP_AUDIT_CONTROL);
 
 	if (getenv("DEBUG_QUIET_TESTS"))
tools/testing/selftests/landlock/net_test.c +1 -1
index 50b637354b51..c1b6e288c965 100644
@@ -2957,7 +2957,7 @@ FIXTURE_SETUP(audit)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	disable_caps(_metadata);
 };
 
tools/testing/selftests/landlock/ns_test.c +2 -2
index f5d4b86edd30..6491b53742f1 100644
@@ -1944,7 +1944,7 @@ FIXTURE_SETUP(ns_audit)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	clear_cap(_metadata, CAP_AUDIT_CONTROL);
 }
 
@@ -2596,7 +2596,7 @@ FIXTURE_SETUP(ns_proc_open)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	clear_cap(_metadata, CAP_AUDIT_CONTROL);
 }
 
tools/testing/selftests/landlock/ptrace_test.c +1 -1
index e0e7f95e889b..10371c1830ad 100644
@@ -323,7 +323,7 @@ FIXTURE_SETUP(audit)
 	disable_caps(_metadata);
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	clear_cap(_metadata, CAP_AUDIT_CONTROL);
 }
 
tools/testing/selftests/landlock/scoped_abstract_unix_test.c +1 -1
index e006dba61652..5e8224623780 100644
@@ -288,7 +288,7 @@ FIXTURE_SETUP(scoped_audit)
 
 	set_cap(_metadata, CAP_AUDIT_CONTROL);
 	self->audit_fd = audit_init_with_exe_filter(&self->audit_filter);
-	EXPECT_LE(0, self->audit_fd);
+	ASSERT_LE(0, self->audit_fd);
 	drop_caps(_metadata);
 }
 
Signature · 1 line
2.56.0
#20 lore raw

Re: [PATCH v2 10/27] selftests/landlock: Assert audit initialization in fixture setups

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

The audit fixtures check the result of audit_init_with_exe_filter() and audit_init() with EXPECT_LE() and continue with a negative audit FD when the initialization fails, e.g. when auditd holds the audit socket. Every following audit check then fails with a less useful message.

Use ASSERT_*() for the audit FD and for the audit_exec filter initialization, whose result is used by the following calls.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#21 lore raw

PATCH v2 11/27 selftests/landlock: Assert helper results that are used afterwards

In reply to Günther Noack

Some helpers check a result with EXPECT_*() and then use it:
cap_get_proc() in _init_caps() and _change_cap(), and landlock_create_ruleset() in drop_access_rights(), create_domain() and create_fs_domain(). On failure they pass NULL or -1 to the next calls, which then fail with misleading messages.

Use ASSERT_*() for these checks, as create_scoped_domain() already does.

---
 tools/testing/selftests/landlock/common.h                    | 6 +++---
 tools/testing/selftests/landlock/ptrace_test.c               | 2 +-
 tools/testing/selftests/landlock/scoped_abstract_unix_test.c | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/landlock/common.h +3 -3
index 6c2aa913ff11..a662a68bee7b 100644
@@ -55,7 +55,7 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
 		EXPECT_EQ(0, cap_set_secbits(noroot));
 
 	cap_p = cap_get_proc();
-	EXPECT_NE(NULL, cap_p);
+	ASSERT_NE(NULL, cap_p);
 	EXPECT_NE(-1, cap_clear(cap_p));
 	if (!drop_all) {
 		EXPECT_NE(-1, cap_set_flag(cap_p, CAP_PERMITTED,
@@ -91,7 +91,7 @@ static void _change_cap(struct __test_metadata *const _metadata,
 	cap_t cap_p;
 
 	cap_p = cap_get_proc();
-	EXPECT_NE(NULL, cap_p);
+	ASSERT_NE(NULL, cap_p);
 	EXPECT_NE(-1, cap_set_flag(cap_p, flag, 1, &cap, value));
 	EXPECT_NE(-1, cap_set_proc(cap_p))
 	{
@@ -227,7 +227,7 @@ drop_access_rights(struct __test_metadata *const _metadata,
 
 	ruleset_fd =
 		landlock_create_ruleset(ruleset_attr, sizeof(*ruleset_attr), 0);
-	EXPECT_LE(0, ruleset_fd)
+	ASSERT_LE(0, ruleset_fd)
 	{
 		TH_LOG("Failed to create a ruleset: %s", strerror(errno));
 	}
tools/testing/selftests/landlock/ptrace_test.c +1 -1
index 10371c1830ad..b951889cd6ca 100644
@@ -37,7 +37,7 @@ static void create_domain(struct __test_metadata *const _metadata)
 
 	ruleset_fd =
 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
-	EXPECT_LE(0, ruleset_fd)
+	ASSERT_LE(0, ruleset_fd)
 	{
 		TH_LOG("Failed to create a ruleset: %s", strerror(errno));
 	}
tools/testing/selftests/landlock/scoped_abstract_unix_test.c +1 -1
index 5e8224623780..55a1599819c8 100644
@@ -40,7 +40,7 @@ static void create_fs_domain(struct __test_metadata *const _metadata)
 
 	ruleset_fd =
 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
-	EXPECT_LE(0, ruleset_fd)
+	ASSERT_LE(0, ruleset_fd)
 	{
 		TH_LOG("Failed to create a ruleset: %s", strerror(errno));
 	}
Signature · 1 line
2.56.0
#22 lore raw

Re: [PATCH v2 11/27] selftests/landlock: Assert helper results that are used afterwards

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Some helpers check a result with EXPECT_*() and then use it:
cap_get_proc() in _init_caps() and _change_cap(), and landlock_create_ruleset() in drop_access_rights(), create_domain() and create_fs_domain(). On failure they pass NULL or -1 to the next calls, which then fail with misleading messages.

Use ASSERT_*() for these checks, as create_scoped_domain() already does.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#23 lore raw

PATCH v2 12/27 selftests/landlock: Assert audit_count_records() before using its result

In reply to Günther Noack

audit_count_records() fills in the record counts that are checked right after it, but its return value is checked with EXPECT. On failure, the counts may be left uninitialized and the following checks are meaningless. Use ASSERT instead.

---
 tools/testing/selftests/landlock/audit_test.c      | 20 ++---
 tools/testing/selftests/landlock/cap_test.c        | 16 ++--
 tools/testing/selftests/landlock/fs_test.c         | 86 +++++++++++-----------
 tools/testing/selftests/landlock/net_test.c        | 22 +++---
 tools/testing/selftests/landlock/ns_test.c         | 26 +++----
 tools/testing/selftests/landlock/ptrace_test.c     |  6 +-
 .../selftests/landlock/scoped_abstract_unix_test.c |  4 +-
 7 files changed, 90 insertions(+), 90 deletions(-)
tools/testing/selftests/landlock/audit_test.c +10 -10
index 63b1b2a04eee..325fbb9ca297 100644
@@ -329,7 +329,7 @@ TEST_F(audit, log_subdomains_off_fork)
 					NULL));
 
 	/* Drains any remaining records (e.g. domain allocation). */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 
 	/*
 	 * Mutes subdomain logs without creating a domain.  The parent's
@@ -361,7 +361,7 @@ TEST_F(audit, log_subdomains_off_fork)
 	EXPECT_EQ(-EAGAIN, matches_log_signal(_metadata, self->audit_fd,
 					      getpid(), NULL));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	EXPECT_EQ(0, close(ruleset_fd));
@@ -487,7 +487,7 @@ TEST_F(audit, log_subdomains_off_tsync)
 					child_data.parent_pid, NULL));
 
 	/* Drains any remaining records (e.g. domain allocation). */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 
 	/*
 	 * Mutes subdomain logs and propagates to the sibling thread via TSYNC,
@@ -509,7 +509,7 @@ TEST_F(audit, log_subdomains_off_tsync)
 	EXPECT_EQ(-EAGAIN, matches_log_signal(_metadata, self->audit_fd,
 					      child_data.parent_pid, NULL));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	EXPECT_EQ(0, close(pipe_child[0]));
@@ -571,7 +571,7 @@ TEST_F(audit, tsync_override_log_subdomains_off)
 					      child_data.parent_pid, NULL));
 
 	/* Drains any remaining records. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	/*
@@ -740,7 +740,7 @@ TEST_F(audit_flags, signal)
 		EXPECT_EQ(EPERM, errno);
 
 		/* Makes sure there is no superfluous logged records. */
-		EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+		ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 		if (!expect_audit) {
 			EXPECT_EQ(0, records.access);
 		} else {
@@ -929,7 +929,7 @@ TEST_F(audit_exec, signal_and_open)
 	EXPECT_EQ(1, read(pipe_child[0], &buf_parent, 1));
 
 	/* Tests that there was no denial until now. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 
@@ -948,7 +948,7 @@ TEST_F(audit_exec, signal_and_open)
 	}
 
 	/* Checks that we didn't miss anything. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	/*
@@ -972,7 +972,7 @@ TEST_F(audit_exec, signal_and_open)
 	}
 
 	/* Checks that we didn't miss anything. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	/* Waits for the child to terminate. */
@@ -995,7 +995,7 @@ TEST_F(audit_exec, signal_and_open)
 	}
 
 	/* Checks that we didn't miss anything. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
tools/testing/selftests/landlock/cap_test.c +8 -8
index 7b3b724e4406..5a7dbd26f92f 100644
@@ -823,7 +823,7 @@ TEST_F(cap_audit, denied)
 	 */
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -846,7 +846,7 @@ TEST_F(cap_audit, allowed)
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
 	/* No records: allowed operations never trigger audit logging. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -872,7 +872,7 @@ TEST_F(cap_audit, allow_and_quiet_same_member)
 	/*
 	 * Quiet is inert for the allowed member: nothing is denied or logged.
 	 */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -916,7 +916,7 @@ TEST_F(cap_audit, quieted)
 	EXPECT_EQ(0, matches_log_cap(self->audit_fd, CAP_SYS_ADMIN));
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -962,7 +962,7 @@ TEST_F(cap_audit, quiet_youngest_layer_wins)
 	EXPECT_EQ(0, matches_log_cap(self->audit_fd, CAP_SYS_CHROOT));
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -994,7 +994,7 @@ TEST_F(cap_audit, quiet_unknown_bit_no_effect)
 	EXPECT_EQ(0, matches_log_cap(self->audit_fd, CAP_SYS_CHROOT));
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -1051,7 +1051,7 @@ TEST_F(cap_audit, noaudit_probe_not_logged)
 	EXPECT_EQ(0, close(fd));
 
 	/* The denied noaudit probe leaves no audit record nor denial. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	/*
@@ -1066,7 +1066,7 @@ TEST_F(cap_audit, noaudit_probe_not_logged)
 	EXPECT_EQ(0, matches_log_cap(self->audit_fd, CAP_SYS_ADMIN));
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
tools/testing/selftests/landlock/fs_test.c +43 -43

344 lines not on this page · raw message

tools/testing/selftests/landlock/net_test.c +11 -11
index c1b6e288c965..563e2214390b 100644
@@ -3004,7 +3004,7 @@ TEST_F(audit, bind)
 	EXPECT_EQ(0, matches_auditlog(self->audit_fd, audit_evt, "saddr",
 				      variant->addr, "src", self->srv0.port));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(1, records.domain);
 
@@ -3015,7 +3015,7 @@ TEST_F(audit, bind)
 	ASSERT_LE(0, sock_fd);
 	EXPECT_EQ(-EACCES, bind_variant(sock_fd, &self->srv2));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 
@@ -3065,7 +3065,7 @@ TEST_F(audit, connect)
 	EXPECT_EQ(0, matches_auditlog(self->audit_fd, audit_evt, "daddr",
 				      variant->addr, "dest", self->srv0.port));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(1, records.domain);
 
@@ -3075,7 +3075,7 @@ TEST_F(audit, connect)
 
 		EXPECT_EQ(0, matches_auditlog(self->audit_fd, "net\\.bind_udp",
 					      NULL, NULL, NULL, 0));
-		EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+		ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 		EXPECT_EQ(0, records.access);
 		EXPECT_EQ(0, records.domain);
 	}
@@ -3087,7 +3087,7 @@ TEST_F(audit, connect)
 	ASSERT_LE(0, sock_fd);
 	EXPECT_EQ(-EACCES, connect_variant(sock_fd, &self->srv2));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 
@@ -3136,7 +3136,7 @@ TEST_F(audit, connect_quiet_bind)
 	EXPECT_EQ(0, matches_auditlog(self->audit_fd, audit_evt, "daddr",
 				      variant->addr, "dest", self->srv2.port));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	EXPECT_EQ(0, close(sock_fd));
@@ -3155,7 +3155,7 @@ TEST_F(audit, connect_quiet_bind)
 	EXPECT_EQ(-EACCES, connect_variant(sock_fd, &self->srv2));
 
 	/* Quieted - no logs expected. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	EXPECT_EQ(0, close(sock_fd));
@@ -3226,7 +3226,7 @@ TEST_F(audit, connect_bound)
 					       variant->addr, self->srv0.port,
 					       srv_remote.port));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(1, records.domain);
 
@@ -3266,7 +3266,7 @@ TEST_F(audit, sendmsg)
 				      "daddr", variant->addr, "dest",
 				      self->srv0.port));
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(1, records.domain);
 
@@ -3274,7 +3274,7 @@ TEST_F(audit, sendmsg)
 	EXPECT_EQ(-EACCES, sendto_variant(sock_fd, &self->srv1, "A", 1, 0));
 	EXPECT_EQ(0, matches_auditlog(self->audit_fd, "net\\.bind_udp", NULL,
 				      NULL, NULL, 0));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 
@@ -3282,7 +3282,7 @@ TEST_F(audit, sendmsg)
 		  sendto_variant(sock_fd, &self->unspec_srv0, "B", 1, 0));
 	EXPECT_EQ(0, matches_auditlog(self->audit_fd, "net\\.connect_send_udp",
 				      "daddr", NULL, "dest", 0));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 
tools/testing/selftests/landlock/ns_test.c +13 -13
index 6491b53742f1..57d03233bf6d 100644
@@ -1984,7 +1984,7 @@ TEST_F(ns_audit, create_denied)
 	 */
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -2005,7 +2005,7 @@ TEST_F(ns_audit, create_allowed)
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
 	/* No records: allowed operations never trigger audit logging. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2030,7 +2030,7 @@ TEST_F(ns_audit, setns_allowed)
 	EXPECT_EQ(0, close(ns_fd));
 
 	/* No records: allowed setns never triggers audit logging. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2068,7 +2068,7 @@ TEST_F(ns_audit, setns_denied)
 	 */
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -2094,7 +2094,7 @@ TEST_F(ns_audit, create_quieted)
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
 	/* The denial is suppressed: no access record. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2123,7 +2123,7 @@ TEST_F(ns_audit, setns_quieted)
 	EXPECT_EQ(0, close(ns_fd));
 
 	/* The denial is suppressed: no access record. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2155,7 +2155,7 @@ TEST_F(ns_audit, quiet_is_per_member)
 	EXPECT_EQ(0, matches_log_ns_create(self->audit_fd, CLONE_NEWUTS));
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -2187,7 +2187,7 @@ TEST_F(ns_audit, quiet_unknown_bit_no_effect)
 	EXPECT_EQ(0, matches_log_ns_create(self->audit_fd, CLONE_NEWUTS));
 	EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
 						  &domain_id));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
@@ -2212,7 +2212,7 @@ TEST_F(ns_audit, allow_and_quiet)
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2238,7 +2238,7 @@ TEST_F(ns_audit, allow_and_quiet_same_member)
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
 	/* Nothing denied, nothing logged. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2272,7 +2272,7 @@ TEST_F(ns_audit, quiet_youngest_layer_wins)
 
 	/* The child layer denies and does not quiet: logged. */
 	EXPECT_EQ(0, matches_log_ns_create(self->audit_fd, CLONE_NEWUTS));
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2307,7 +2307,7 @@ TEST_F(ns_audit, quiet_parent_only_denying_layer)
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
 	/* The parent quiets and is the youngest denying layer: suppressed. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 }
 
@@ -2638,7 +2638,7 @@ TEST_F(ns_proc_open, open_unrestricted)
 	}
 
 	/* No Landlock denial. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	EXPECT_EQ(0, close(fd));
tools/testing/selftests/landlock/ptrace_test.c +3 -3
index b951889cd6ca..eb36d4d1ceaf 100644
@@ -343,7 +343,7 @@ TEST_F(audit, trace)
 	struct audit_records records;
 
 	/* Makes sure there is no superfluous logged records. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 
@@ -381,7 +381,7 @@ TEST_F(audit, trace)
 		EXPECT_EQ(0, matches_log_ptrace(_metadata, self->audit_fd,
 						getpid()));
 
-		EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+		ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 		EXPECT_EQ(0, records.access);
 		/* Checks for a domain creation. */
 		EXPECT_EQ(1, records.domain);
@@ -428,7 +428,7 @@ TEST_F(audit, trace)
 		_metadata->exit_code = KSFT_FAIL;
 
 	/* Makes sure there is no superfluous logged records. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 }
tools/testing/selftests/landlock/scoped_abstract_unix_test.c +2 -2
index 55a1599819c8..67fcc3380238 100644
@@ -360,7 +360,7 @@ TEST_F(scoped_audit, connect_to_child)
 		!(variant->quiet_scoped & LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET);
 
 	/* Makes sure there is no superfluous logged records. */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 	EXPECT_EQ(0, records.domain);
 
@@ -429,7 +429,7 @@ TEST_F(scoped_audit, connect_to_child)
 	}
 
 	/* No other logs */
-	EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
+	ASSERT_EQ(0, audit_count_records(self->audit_fd, &records));
 	EXPECT_EQ(0, records.access);
 
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
Signature · 1 line
2.56.0
#24 lore raw

Re: [PATCH v2 12/27] selftests/landlock: Assert audit_count_records() before using its result

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

audit_count_records() fills in the record counts that are checked right after it, but its return value is checked with EXPECT. On failure, the counts may be left uninitialized and the following checks are meaningless. Use ASSERT instead.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#25 lore raw

PATCH v2 13/27 selftests/landlock: Fix memory leak in audit_init_filter_exe()

In reply to Günther Noack

audit_init_filter_exe() does not free the buffer returned by realpath(3) when the path is too long.

---
 tools/testing/selftests/landlock/audit.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/landlock/audit.h +3 -1
index d428ce802f49..1e533b4e26db 100644
@@ -507,8 +507,10 @@ static int audit_init_filter_exe(struct audit_filter *filter, const char *path)
 
 	/* No need for the terminating NULL byte. */
 	filter->exe_len = strlen(absolute_path);
-	if (filter->exe_len > sizeof(filter->exe))
+	if (filter->exe_len > sizeof(filter->exe)) {
+		free(absolute_path);
 		return -E2BIG;
+	}
 
 	memcpy(filter->exe, absolute_path, filter->exe_len);
 	free(absolute_path);
Signature · 1 line
2.56.0
#26 lore raw

Re: [PATCH v2 13/27] selftests/landlock: Fix memory leak in audit_init_filter_exe()

Sashiko has reviewed this patch and found no issues.
Quoted 4 lines

audit_init_filter_exe() does not free the buffer returned by realpath(3) when the path is too long.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#27 lore raw

PATCH v2 14/27 selftests/landlock: Make audit_message large enough for any exe filter

In reply to Günther Noack

audit_filter_exe() copies the filtered executable path after the audit_rule_data header in struct audit_message. The message buffer was sized as PATH_MAX + 200 bytes with received records in mind, but struct audit_rule_data alone takes 1040 bytes. That leaves only 3256 bytes for the path, while the kernel accepts up to PATH_MAX bytes, so a long path overflows the message.

Size the buffer for an audit_rule_data followed by a PATH_MAX string. This only makes the buffer larger, so received records still fit.

Also check that the request fits in the message and return -E2BIG otherwise, in case exe_len does not match the filter's buffer.

---
 tools/testing/selftests/landlock/audit.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/landlock/audit.h +8 -1
index 1e533b4e26db..173dcd1088db 100644
@@ -41,7 +41,11 @@ struct audit_message {
 		struct audit_features features;
 		struct audit_rule_data rule;
 		struct nlmsgerr err;
-		char data[PATH_MAX + 200];
+		/*
+		 * Large enough for an audit_rule_data followed by a PATH_MAX
+		 * string (see audit_filter_exe()), and for received records.
+		 */
+		char data[sizeof(struct audit_rule_data) + PATH_MAX];
 	};
 };
 
@@ -171,6 +175,9 @@ static int audit_filter_exe(const int audit_fd,
 	if (filter->record_type != AUDIT_EXE)
 		return -EINVAL;
 
+	if (msg.header.nlmsg_len > sizeof(msg))
+		return -E2BIG;
+
 	memcpy(msg.rule.buf, filter->exe, filter->exe_len);
 	return audit_request(audit_fd, &msg, NULL);
 }
Signature · 1 line
2.56.0
#28 lore raw

Re: [PATCH v2 14/27] selftests/landlock: Make audit_message large enough for any exe filter

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

audit_filter_exe() copies the filtered executable path after the audit_rule_data header in struct audit_message. The message buffer was sized as PATH_MAX + 200 bytes with received records in mind, but struct audit_rule_data alone takes 1040 bytes. That leaves only 3256 bytes for the path, while the kernel accepts up to PATH_MAX bytes, so a long path overflows the message.

Quoted 7 lines

Size the buffer for an audit_rule_data followed by a PATH_MAX string. This only makes the buffer larger, so received records still fit.

Also check that the request fits in the message and return -E2BIG otherwise, in case exe_len does not match the filter's buffer.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#29 lore raw

PATCH v2 15/27 selftests/landlock: Close leaked file descriptors

In reply to Günther Noack

Several tests do not close file descriptors they open: the /dev/null FD in restrict_self_fd and restrict_self_fd_flags, and the parent's ends of the synchronization pipes in various ptrace, audit, signal and abstract UNIX socket tests.

Close them once they are not needed anymore.

---
 tools/testing/selftests/landlock/audit_test.c                |  2 ++
 tools/testing/selftests/landlock/base_test.c                 |  4 ++++
 tools/testing/selftests/landlock/ptrace_test.c               |  4 ++++
 tools/testing/selftests/landlock/scoped_abstract_unix_test.c | 10 ++++++++++
 tools/testing/selftests/landlock/scoped_signal_test.c        |  1 +
 5 files changed, 21 insertions(+)
tools/testing/selftests/landlock/audit_test.c +2 -0
index 325fbb9ca297..b38d0c5d5869 100644
@@ -977,6 +977,8 @@ TEST_F(audit_exec, signal_and_open)
 
 	/* Waits for the child to terminate. */
 	EXPECT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(1, WIFEXITED(status));
 	ASSERT_EQ(0, WEXITSTATUS(status));
tools/testing/selftests/landlock/base_test.c +4 -0
index 0bf8632b725f..a6d355807dab 100644
@@ -352,6 +352,8 @@ TEST(restrict_self_fd)
 
 	EXPECT_EQ(-1, landlock_restrict_self(fd, 0));
 	EXPECT_EQ(EBADFD, errno);
+
+	EXPECT_EQ(0, close(fd));
 }
 
 TEST(restrict_self_fd_flags)
@@ -373,6 +375,8 @@ TEST(restrict_self_fd_flags)
 	EXPECT_EQ(-1, landlock_restrict_self(
 			      fd, LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS));
 	EXPECT_EQ(EBADFD, errno);
+
+	EXPECT_EQ(0, close(fd));
 }
 
 TEST(restrict_self_flags)
tools/testing/selftests/landlock/ptrace_test.c +4 -0
index eb36d4d1ceaf..4fd2b440aa45 100644
@@ -288,6 +288,8 @@ TEST_F(scoped_domains, trace)
 
 	/* Signals that the parent PTRACE_ATTACH test is done. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 
 	if (WIFSIGNALED(status) || !WIFEXITED(status) ||
@@ -422,6 +424,8 @@ TEST_F(audit, trace)
 
 	/* Signals that the parent PTRACE_ATTACH test is done. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	if (WIFSIGNALED(status) || !WIFEXITED(status) ||
 	    WEXITSTATUS(status) != EXIT_SUCCESS)
tools/testing/selftests/landlock/scoped_abstract_unix_test.c +10 -0
index 67fcc3380238..df41da5f2ab0 100644
@@ -156,6 +156,7 @@ TEST_F(scoped_domains, connect_to_parent)
 
 	/* Signals to child that the parent is listening. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	EXPECT_EQ(0, close(stream_server));
@@ -263,6 +264,8 @@ TEST_F(scoped_domains, connect_to_child)
 		EXPECT_EQ(EPERM, errno_dgram);
 	}
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	EXPECT_EQ(0, close(stream_client));
 	EXPECT_EQ(0, close(dgram_client));
 
@@ -433,6 +436,8 @@ TEST_F(scoped_audit, connect_to_child)
 	EXPECT_EQ(0, records.access);
 
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 	EXPECT_EQ(0, close(dgram_client));
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
@@ -637,6 +642,7 @@ TEST_F(scoped_vs_unscoped, unix_scoping)
 	ASSERT_EQ(0, listen(stream_server_parent, backlog));
 
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	EXPECT_EQ(0, close(stream_server_parent));
 	EXPECT_EQ(0, close(dgram_server_parent));
@@ -779,6 +785,7 @@ TEST_F(outside_socket, socket_with_different_domain)
 	} else {
 		server_socket = socket(AF_UNIX, variant->type, 0);
 	}
+	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_LE(0, server_socket);
 
 	/* Server always has a domain. */
@@ -791,6 +798,7 @@ TEST_F(outside_socket, socket_with_different_domain)
 
 	/* Signals to child that the parent is listening. */
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	EXPECT_EQ(0, close(server_socket));
@@ -1139,6 +1147,8 @@ TEST(datagram_sockets)
 	 */
 	ASSERT_EQ(1, read(pipe_child[0], &buf, 1));
 	ASSERT_EQ(1, recv(server_conn_socket, &buf, 1, 0));
+	EXPECT_EQ(0, close(pipe_parent[1]));
+	EXPECT_EQ(0, close(pipe_child[0]));
 
 	/* Waits for all tests to finish. */
 	ASSERT_EQ(child, waitpid(child, &status, 0));
tools/testing/selftests/landlock/scoped_signal_test.c +1 -0
index 47307dd2e242..0f88a6af9c72 100644
@@ -1120,6 +1120,7 @@ TEST_F(trace_fown, deny_scope_fown)
 	ASSERT_EQ(0, fcntl(recv_socket, F_SETOWN, child));
 
 	ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
+	EXPECT_EQ(0, close(pipe_parent[1]));
 
 	/* Waits for the child to send MSG_OOB. */
 	ASSERT_EQ(1, read(pipe_child[0], &buffer_parent, 1));
Signature · 1 line
2.56.0
#30 lore raw

Re: [PATCH v2 15/27] selftests/landlock: Close leaked file descriptors

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Several tests do not close file descriptors they open: the /dev/null FD in restrict_self_fd and restrict_self_fd_flags, and the parent's ends of the synchronization pipes in various ptrace, audit, signal and abstract UNIX socket tests.

Close them once they are not needed anymore.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#31 lore raw

PATCH v2 16/27 selftests/landlock: Check errno for combined VERSION and ERRATA flags

In reply to Günther Noack

The errata test checks that combining LANDLOCK_CREATE_RULESET_VERSION and LANDLOCK_CREATE_RULESET_ERRATA fails, but not the resulting errno, unlike the other checks in this test.

---
 tools/testing/selftests/landlock/base_test.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/landlock/base_test.c +2 -0
index a6d355807dab..9a1d5b8aacb8 100644
@@ -136,6 +136,8 @@ TEST(errata)
 			      NULL, 0,
 			      LANDLOCK_CREATE_RULESET_VERSION |
 				      LANDLOCK_CREATE_RULESET_ERRATA));
+	ASSERT_EQ(EINVAL, errno);
+
 	ASSERT_EQ(-1, landlock_create_ruleset(NULL, 0,
 					      LANDLOCK_CREATE_RULESET_ERRATA |
 						      1 << 31));
Signature · 1 line
2.56.0
#32 lore raw

Re: [PATCH v2 16/27] selftests/landlock: Check errno for combined VERSION and ERRATA flags

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

The errata test checks that combining LANDLOCK_CREATE_RULESET_VERSION and LANDLOCK_CREATE_RULESET_ERRATA fails, but not the resulting errno, unlike the other checks in this test.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#33 lore raw

PATCH v2 17/27 selftests/landlock: Fix SIGURG handler setup in scoped_signal_test

In reply to Günther Noack

Drop SA_SIGINFO, and rename the function to setup_sigurg_handler() without the parameter.

* Drop the SA_SIGINFO flag: This flag implies that we use the three-argument sa_sigaction signal handlers, which is not the case here. (c.f. sigaction(2))
* Remove the "int signal" argument and rename the function:
The function was already specific to SIGURG and this is better communicated through a different name.

---
 tools/testing/selftests/landlock/scoped_signal_test.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/landlock/scoped_signal_test.c +7 -7
index 0f88a6af9c72..66fec3822812 100644
@@ -392,7 +392,7 @@ static void handle_sigurg(int sig)
 		signal_received = -1;
 }
 
-static int setup_signal_handler(int signal)
+static int setup_sigurg_handler(void)
 {
 	struct sigaction sa = {
 		.sa_handler = handle_sigurg,
@@ -401,7 +401,7 @@ static int setup_signal_handler(int signal)
 	if (sigemptyset(&sa.sa_mask))
 		return -1;
 
-	sa.sa_flags = SA_SIGINFO | SA_RESTART;
+	sa.sa_flags = SA_RESTART;
 	return sigaction(SIGURG, &sa, NULL);
 }
 
@@ -506,7 +506,7 @@ TEST_F(fown, sigurg_socket)
 		EXPECT_EQ(0, close(pipe_parent[1]));
 		EXPECT_EQ(0, close(pipe_child[0]));
 
-		ASSERT_EQ(0, setup_signal_handler(SIGURG));
+		ASSERT_EQ(0, setup_sigurg_handler());
 		client_socket = socket(AF_UNIX, SOCK_STREAM, 0);
 		ASSERT_LE(0, client_socket);
 
@@ -612,7 +612,7 @@ TEST(sigio_to_pgid_members)
 	ASSERT_EQ(0, setpgid(0, 0));
 
 	/* The non-sandboxed parent is the protected (out-of-domain) target. */
-	ASSERT_EQ(0, setup_signal_handler(SIGURG));
+	ASSERT_EQ(0, setup_sigurg_handler());
 	signal_received = 0;
 
 	ASSERT_EQ(0, pipe2(trigger, O_CLOEXEC));
@@ -629,7 +629,7 @@ TEST(sigio_to_pgid_members)
 		EXPECT_EQ(0, close(sync_child[0]));
 
 		/* In-domain positive control: the child must be signaled. */
-		ASSERT_EQ(0, setup_signal_handler(SIGURG));
+		ASSERT_EQ(0, setup_sigurg_handler());
 		signal_received = 0;
 
 		create_scoped_domain(_metadata, LANDLOCK_SCOPE_SIGNAL);
@@ -737,7 +737,7 @@ TEST(sigio_to_pgid_self)
 	ASSERT_EQ(0, setpgid(0, 0));
 
 	/* The non-sandboxed thread-group leader is the SIGIO target. */
-	ASSERT_EQ(0, setup_signal_handler(SIGURG));
+	ASSERT_EQ(0, setup_sigurg_handler());
 	signal_received = 0;
 
 	ASSERT_EQ(0, pipe2(trigger, O_CLOEXEC));
@@ -1062,7 +1062,7 @@ TEST_F(trace_fown, deny_scope_fown)
 		EXPECT_EQ(0, close(pipe_parent[1]));
 		EXPECT_EQ(0, close(pipe_child[0]));
 
-		ASSERT_EQ(0, setup_signal_handler(SIGURG));
+		ASSERT_EQ(0, setup_sigurg_handler());
 		client_socket = socket(AF_UNIX, SOCK_STREAM, 0);
 		ASSERT_LE(0, client_socket);
 
Signature · 1 line
2.56.0
#34 lore raw

Re: [PATCH v2 17/27] selftests/landlock: Fix SIGURG handler setup in scoped_signal_test

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

Drop SA_SIGINFO, and rename the function to setup_sigurg_handler() without the parameter.

* Drop the SA_SIGINFO flag: This flag implies that we use the three-argument sa_sigaction signal handlers, which is not the case here. (c.f. sigaction(2))
* Remove the "int signal" argument and rename the function:
The function was already specific to SIGURG and this is better communicated through a different name.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#35 lore raw

PATCH v2 18/27 selftests/landlock: Fix regex_escape() error handling in matches_log_fs_extra()

In reply to Günther Noack

Use a more conventional error handling interface in regex_escape():
Return a ssize_t, which is either the number of bytes in the output, or return -1 and set errno.

Before this change, the (log_match_cursor < 0) check in the call site was never true because log_match_cursor is a pointer.

---
 tools/testing/selftests/landlock/common.h  | 20 ++++++++++++--------
 tools/testing/selftests/landlock/fs_test.c | 12 +++++++-----
 2 files changed, 19 insertions(+), 13 deletions(-)
tools/testing/selftests/landlock/common.h +12 -8
index a662a68bee7b..dcfdcfecde86 100644
@@ -277,11 +277,11 @@ static void __maybe_unused set_unix_address(struct service_fixture *const srv,
  * Escapes characters that have special meaning in POSIX Basic Regular
  * Expressions: $ * . [ \ ] ^
  *
- * Returns a pointer to the NUL terminator in @dst (cursor-style API for
- * chaining), or (char *)-ENOMEM if the buffer is too small.
+ * Returns the length of the escaped string (excluding the NUL terminator), or
+ * -1 with errno set to ENOMEM if the buffer is too small.
  */
-static __maybe_unused char *regex_escape(const char *const src, char *dst,
-					 size_t dst_size)
+static __maybe_unused ssize_t regex_escape(const char *const src, char *dst,
+					   size_t dst_size)
 {
 	char *d = dst;
 
@@ -295,21 +295,25 @@ static __maybe_unused char *regex_escape(const char *const src, char *dst,
 		case ']':
 		case '^':
 			if (d >= dst + dst_size - 2)
-				return (char *)-ENOMEM;
+				goto err_nomem;
 
 			*d++ = '\\';
 			*d++ = *s;
 			break;
 		default:
 			if (d >= dst + dst_size - 1)
-				return (char *)-ENOMEM;
+				goto err_nomem;
 
 			*d++ = *s;
 		}
 	}
 	if (d >= dst + dst_size - 1)
-		return (char *)-ENOMEM;
+		goto err_nomem;
 
 	*d = '\0';
-	return d;
+	return d - dst;
+
+err_nomem:
+	errno = ENOMEM;
+	return -1;
 }
tools/testing/selftests/landlock/fs_test.c +7 -5
index 100ef02b5d1a..10a386a573dd 100644
@@ -7450,6 +7450,7 @@ static int matches_log_fs_extra(struct __test_metadata *const _metadata,
 	char log_match[log_match_remaining];
 	char *log_match_cursor = log_match;
 	size_t chunk_len;
+	ssize_t escaped_len;
 
 	chunk_len = snprintf(log_match_cursor, log_match_remaining,
 			     REGEX_LANDLOCK_PREFIX " blockers=%s path=\"",
@@ -7467,13 +7468,14 @@ static int matches_log_fs_extra(struct __test_metadata *const _metadata,
 
 	log_match_remaining -= chunk_len;
 	log_match_cursor += chunk_len;
-	log_match_cursor = regex_escape(absolute_path, log_match_cursor,
-					log_match_remaining);
+	escaped_len = regex_escape(absolute_path, log_match_cursor,
+				   log_match_remaining);
 	free(absolute_path);
-	if (log_match_cursor < 0)
-		return (long long)log_match_cursor;
+	if (escaped_len < 0)
+		return -errno;
 
-	log_match_remaining -= log_match_cursor - log_match;
+	log_match_remaining -= escaped_len;
+	log_match_cursor += escaped_len;
 	chunk_len = snprintf(log_match_cursor, log_match_remaining,
 			     "\" dev=\"[^\"]\\+\" ino=[0-9]\\+%s$",
 			     extra ?: "");
Signature · 1 line
2.56.0
#36 lore raw

Re: [PATCH v2 18/27] selftests/landlock: Fix regex_escape() error handling in matches_log_fs_extra()

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Use a more conventional error handling interface in regex_escape():
Return a ssize_t, which is either the number of bytes in the output, or return -1 and set errno.

Before this change, the (log_match_cursor < 0) check in the call site was never true because log_match_cursor is a pointer.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
#37 lore raw

PATCH v2 19/27 selftests/landlock: Fix off-by-one in regex_escape()

In reply to Günther Noack

The final check in regex_escape() was off by one byte and should require one byte less to put the NUL byte.

---
 tools/testing/selftests/landlock/common.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/common.h +1 -1
index dcfdcfecde86..0df8ae88d4fa 100644
@@ -307,7 +307,7 @@ static __maybe_unused ssize_t regex_escape(const char *const src, char *dst,
 			*d++ = *s;
 		}
 	}
-	if (d >= dst + dst_size - 1)
+	if (d >= dst + dst_size)
 		goto err_nomem;
 
 	*d = '\0';
Signature · 1 line
2.56.0
#38 lore raw

Re: [PATCH v2 19/27] selftests/landlock: Fix off-by-one in regex_escape()

Sashiko has reviewed this patch and found no issues.
Quoted 4 lines

The final check in regex_escape() was off by one byte and should require one byte less to put the NUL byte.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
#39 lore raw

PATCH v2 20/27 selftests/landlock: Fix the snprintf() error check in matches_log_fs_extra()

In reply to Günther Noack

By storing the snprintf result in a size_t, the chunk_len < 0 check could never be true. Store it in an int instead, matching the return type of snprintf().

Fixes 316d06b01130 ("selftests/landlock: Add audit tests for filesystem")
---
 tools/testing/selftests/landlock/fs_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/fs_test.c +1 -1
index 10a386a573dd..1e94a3171e02 100644
@@ -7449,7 +7449,7 @@ static int matches_log_fs_extra(struct __test_metadata *const _metadata,
 				     (extra ? strlen(extra) : 0) + 1;
 	char log_match[log_match_remaining];
 	char *log_match_cursor = log_match;
-	size_t chunk_len;
+	int chunk_len;
 	ssize_t escaped_len;
 
 	chunk_len = snprintf(log_match_cursor, log_match_remaining,
Signature · 1 line
2.56.0
#40 lore raw

Re: [PATCH v2 20/27] selftests/landlock: Fix the snprintf() error check in matches_log_fs_extra()

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

By storing the snprintf result in a size_t, the chunk_len < 0 check could never be true. Store it in an int instead, matching the return type of snprintf().

Fixes 316d06b01130 ("selftests/landlock: Add audit tests for filesystem")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
#41 lore raw

PATCH v2 21/27 selftests/landlock: Use _exit() in the named_pipe_ioctl child

In reply to Günther Noack

In the named_pipe_ioctl test, use _exit(2) like in other places.

---
 tools/testing/selftests/landlock/fs_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/fs_test.c +1 -1
index 1e94a3171e02..78b2bf06b6ee 100644
@@ -4272,7 +4272,7 @@ TEST_F_FORK(layout1, named_pipe_ioctl)
 	if (child_pid == 0) {
 		fd = open(path, O_WRONLY);
 		close(fd);
-		exit(0);
+		_exit(0);
 	}
 
 	fd = open(path, O_RDONLY);
Signature · 1 line
2.56.0
#42 lore raw

Re: [PATCH v2 21/27] selftests/landlock: Use _exit() in the named_pipe_ioctl child

Sashiko has reviewed this patch and found no issues.
Quoted 3 lines

In the named_pipe_ioctl test, use _exit(2) like in other places.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
#43 lore raw

PATCH v2 22/27 selftests/landlock: Fix messages and comments in helper programs

In reply to Günther Noack

* sandbox-and-launch: Describe the actual argument order.
* sandbox-and-launch, wait-pipe, wait-pipe-sandbox: Report failed read(2) calls as read failures, not write failures.
* wait-pipe-sandbox: Terminate error messages with a newline, and fix comments naming "." instead of the opened "/".

---
 tools/testing/selftests/landlock/sandbox-and-launch.c |  6 +++---
 tools/testing/selftests/landlock/wait-pipe-sandbox.c  | 16 ++++++++--------
 tools/testing/selftests/landlock/wait-pipe.c          |  2 +-
 3 files changed, 12 insertions(+), 12 deletions(-)
tools/testing/selftests/landlock/sandbox-and-launch.c +3 -3
index 3e32e1a51ac5..8b33ddadc63e 100644
@@ -25,8 +25,8 @@ int main(int argc, char *argv[])
 	char buf;
 
 	/*
-	 * The first argument must be the file descriptor number of a pipe.
-	 * The second argument must be the program to execute.
+	 * The first argument must be the program to execute.  The second and
+	 * third arguments must be the file descriptor numbers of two pipes.
 	 */
 	if (argc != 4) {
 		fprintf(stderr, "Wrong number of arguments (not three)\n");
@@ -67,7 +67,7 @@ int main(int argc, char *argv[])
 
 	/* Waits for the parent to try to umount. */
 	if (read(pipe_parent, &buf, 1) != 1) {
-		perror("Failed to write to the third argument");
+		perror("Failed to read from the third argument");
 		return 1;
 	}
 
tools/testing/selftests/landlock/wait-pipe-sandbox.c +8 -8
index 87dbc9164430..dbc0c2747e9f 100644
@@ -31,7 +31,7 @@ static int sync_with(int pipe_child, int pipe_parent)
 
 	/* Waits for the parent do its test. */
 	if (read(pipe_parent, &buf, 1) != 1) {
-		perror("Failed to write to the second argument");
+		perror("Failed to read from the second argument");
 		return 1;
 	}
 
@@ -65,7 +65,7 @@ int main(int argc, char *argv[])
 
 	/* Tries to send a signal, denied by layer1. */
 	if (!kill(getppid(), 0)) {
-		fprintf(stderr, "Successfully sent a signal to the parent");
+		fprintf(stderr, "Successfully sent a signal to the parent\n");
 		return 1;
 	}
 
@@ -88,13 +88,13 @@ int main(int argc, char *argv[])
 
 	/* Tries to send a signal, denied by layer1. */
 	if (!kill(getppid(), 0)) {
-		fprintf(stderr, "Successfully sent a signal to the parent");
+		fprintf(stderr, "Successfully sent a signal to the parent\n");
 		return 1;
 	}
 
-	/* Tries to open ., denied by layer2. */
+	/* Tries to open /, denied by layer2. */
 	if (open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC) >= 0) {
-		fprintf(stderr, "Successfully opened /");
+		fprintf(stderr, "Successfully opened /\n");
 		return 1;
 	}
 
@@ -115,15 +115,15 @@ int main(int argc, char *argv[])
 	}
 	close(ruleset_fd);
 
-	/* Tries to open ., denied by layer2. */
+	/* Tries to open /, denied by layer2. */
 	if (open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC) >= 0) {
-		fprintf(stderr, "Successfully opened /");
+		fprintf(stderr, "Successfully opened /\n");
 		return 1;
 	}
 
 	/* Tries to send a signal, denied by layer3. */
 	if (!kill(getppid(), 0)) {
-		fprintf(stderr, "Successfully sent a signal to the parent");
+		fprintf(stderr, "Successfully sent a signal to the parent\n");
 		return 1;
 	}
 
tools/testing/selftests/landlock/wait-pipe.c +1 -1
index 0dbcd260a0fa..3cfa4dce5f35 100644
@@ -34,7 +34,7 @@ int main(int argc, char *argv[])
 
 	/* Waits for the parent do its test. */
 	if (read(pipe_parent, &buf, 1) != 1) {
-		perror("Failed to write to the second argument");
+		perror("Failed to read from the second argument");
 		return 1;
 	}
 
Signature · 1 line
2.56.0
#44 lore raw

Re: [PATCH v2 22/27] selftests/landlock: Fix messages and comments in helper programs

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

* sandbox-and-launch: Describe the actual argument order.
* sandbox-and-launch, wait-pipe, wait-pipe-sandbox: Report failed read(2) calls as read failures, not write failures.
* wait-pipe-sandbox: Terminate error messages with a newline, and fix comments naming "." instead of the opened "/".

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
#45 lore raw

PATCH v2 23/27 selftests/landlock: Fix stale comments in common.h and audit.h

In reply to Günther Noack

* _init_caps() lists more than three capabilities.
* A 64-bit hexadecimal domain ID has at most 16 digits, not 17. Size the buffer accordingly and drop a redundant domain_id check in audit_match_record().

---
 tools/testing/selftests/landlock/audit.h  | 7 +++----
 tools/testing/selftests/landlock/common.h | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
tools/testing/selftests/landlock/audit.h +3 -4
index 173dcd1088db..d23bb2ad3a30 100644
@@ -274,15 +274,14 @@ static int audit_match_record(int audit_fd, const __u16 type,
 		*domain_id = 0;
 		if (matches[1].rm_so != -1) {
 			int match_len = matches[1].rm_eo - matches[1].rm_so;
-			/* The maximal characters of a 2^64 hexadecimal number is 17. */
-			char dom_id[18];
+			/* A 64-bit hexadecimal number has at most 16 digits. */
+			char dom_id[17];
 
 			if (match_len > 0 && match_len < sizeof(dom_id)) {
 				memcpy(dom_id, msg.data + matches[1].rm_so,
 				       match_len);
 				dom_id[match_len] = '\0';
-				if (domain_id)
-					*domain_id = strtoull(dom_id, NULL, 16);
+				*domain_id = strtoull(dom_id, NULL, 16);
 			}
 		}
 	}
tools/testing/selftests/landlock/common.h +1 -1
index 0df8ae88d4fa..66d68a92c6cd 100644
@@ -36,7 +36,7 @@ static const char bin_wait_pipe_sandbox[] = "./wait-pipe-sandbox";
 static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
 {
 	cap_t cap_p;
-	/* Only these three capabilities are useful for the tests. */
+	/* Only these capabilities are useful for the tests. */
 	const cap_value_t caps[] = {
 		/* clang-format off */
 		CAP_AUDIT_CONTROL,
Signature · 1 line
2.56.0
#46 lore raw

Re: [PATCH v2 23/27] selftests/landlock: Fix stale comments in common.h and audit.h

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

* _init_caps() lists more than three capabilities.
* A 64-bit hexadecimal domain ID has at most 16 digits, not 17. Size the buffer accordingly and drop a redundant domain_id check in audit_match_record().

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
#47 lore raw

PATCH v2 24/27 selftests/landlock: Use ASSERT for syscall failures before errno checks

In reply to Günther Noack

In cases where a syscall is expected to fail, but unexpectedly succeeds, the resulting errno is undefined and we should not proceed to check the errno. Replace EXPECT* with ASSERT* macros for syscall invocations where this is the case, so that we abort the tests early in such cases.

In the errata test, check errno with EXPECT after the combined VERSION and ERRATA flags, since nothing depends on it.

---
 tools/testing/selftests/landlock/audit_test.c      |  8 +--
 tools/testing/selftests/landlock/base_test.c       | 40 ++++++-------
 tools/testing/selftests/landlock/cap_test.c        | 30 +++++-----
 tools/testing/selftests/landlock/fs_test.c         | 50 ++++++++--------
 tools/testing/selftests/landlock/net_test.c        | 24 ++++----
 tools/testing/selftests/landlock/ns_test.c         | 68 +++++++++++-----------
 tools/testing/selftests/landlock/ptrace_test.c     | 14 ++---
 .../selftests/landlock/scoped_abstract_unix_test.c | 14 ++---
 8 files changed, 124 insertions(+), 124 deletions(-)
tools/testing/selftests/landlock/audit_test.c +4 -4
index b38d0c5d5869..392e75ddfa3c 100644
@@ -100,7 +100,7 @@ TEST_F(audit, layers)
 			EXPECT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
 
 			/* Creates a denial to get the domain ID. */
-			EXPECT_EQ(-1, kill(getppid(), 0));
+			ASSERT_EQ(-1, kill(getppid(), 0));
 			EXPECT_EQ(EPERM, errno);
 			EXPECT_EQ(0,
 				  matches_log_signal(_metadata, self->audit_fd,
@@ -119,7 +119,7 @@ TEST_F(audit, layers)
 		}
 
 		/* Checks that we reached the maximum number of layers. */
-		EXPECT_EQ(-1, landlock_restrict_self(ruleset_fd, 0));
+		ASSERT_EQ(-1, landlock_restrict_self(ruleset_fd, 0));
 		EXPECT_EQ(E2BIG, errno);
 
 		/* Updates filter rules to match the drop record. */
@@ -711,7 +711,7 @@ TEST_F(audit_flags, signal)
 		EXPECT_EQ(0, close(ruleset_fd));
 
 		/* First signal checks to test log entries. */
-		EXPECT_EQ(-1, kill(getppid(), 0));
+		ASSERT_EQ(-1, kill(getppid(), 0));
 		EXPECT_EQ(EPERM, errno);
 
 		if (!expect_audit) {
@@ -736,7 +736,7 @@ TEST_F(audit_flags, signal)
 		}
 
 		/* Second signal checks to test audit_count_records(). */
-		EXPECT_EQ(-1, kill(getppid(), 0));
+		ASSERT_EQ(-1, kill(getppid(), 0));
 		EXPECT_EQ(EPERM, errno);
 
 		/* Makes sure there is no superfluous logged records. */
tools/testing/selftests/landlock/base_test.c +20 -20
index 9a1d5b8aacb8..b1a729b94e7b 100644
@@ -136,7 +136,7 @@ TEST(errata)
 			      NULL, 0,
 			      LANDLOCK_CREATE_RULESET_VERSION |
 				      LANDLOCK_CREATE_RULESET_ERRATA));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 
 	ASSERT_EQ(-1, landlock_create_ruleset(NULL, 0,
 					      LANDLOCK_CREATE_RULESET_ERRATA |
@@ -352,7 +352,7 @@ TEST(restrict_self_fd)
 	fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
 	ASSERT_LE(0, fd);
 
-	EXPECT_EQ(-1, landlock_restrict_self(fd, 0));
+	ASSERT_EQ(-1, landlock_restrict_self(fd, 0));
 	EXPECT_EQ(EBADFD, errno);
 
 	EXPECT_EQ(0, close(fd));
@@ -369,12 +369,12 @@ TEST(restrict_self_fd_flags)
 	 * LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF accepts -1 but not any file
 	 * descriptor.
 	 */
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      fd, LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF));
 	EXPECT_EQ(EBADFD, errno);
 
 	/* LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS requires a ruleset FD. */
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      fd, LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS));
 	EXPECT_EQ(EBADFD, errno);
 
@@ -387,40 +387,40 @@ TEST(restrict_self_flags)
 
 	/* Tests invalid flag combinations. */
 
-	EXPECT_EQ(-1, landlock_restrict_self(-1, last_flag << 1));
+	ASSERT_EQ(-1, landlock_restrict_self(-1, last_flag << 1));
 	EXPECT_EQ(EINVAL, errno);
 
-	EXPECT_EQ(-1, landlock_restrict_self(-1, -1));
+	ASSERT_EQ(-1, landlock_restrict_self(-1, -1));
 	EXPECT_EQ(EINVAL, errno);
 
 	/* Tests valid flag combinations. */
 
-	EXPECT_EQ(-1, landlock_restrict_self(-1, 0));
+	ASSERT_EQ(-1, landlock_restrict_self(-1, 0));
 	EXPECT_EQ(EBADF, errno);
 
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      -1, LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF));
 	EXPECT_EQ(EBADF, errno);
 
-	EXPECT_EQ(-1,
+	ASSERT_EQ(-1,
 		  landlock_restrict_self(
 			  -1,
 			  LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF |
 				  LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF));
 	EXPECT_EQ(EBADF, errno);
 
-	EXPECT_EQ(-1,
+	ASSERT_EQ(-1,
 		  landlock_restrict_self(
 			  -1,
 			  LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
 				  LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF));
 	EXPECT_EQ(EBADF, errno);
 
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      -1, LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON));
 	EXPECT_EQ(EBADF, errno);
 
-	EXPECT_EQ(-1,
+	ASSERT_EQ(-1,
 		  landlock_restrict_self(
 			  -1, LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF |
 				      LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON));
@@ -428,18 +428,18 @@ TEST(restrict_self_flags)
 
 	/* LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS requires a ruleset FD. */
 
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      -1, LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS));
 	EXPECT_EQ(EBADF, errno);
 
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      -1, LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF |
 					  LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS));
 	EXPECT_EQ(EBADF, errno);
 
 	/* Tests with an invalid ruleset_fd. */
 
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      -2, LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF));
 	EXPECT_EQ(EBADF, errno);
 
@@ -465,7 +465,7 @@ TEST(restrict_self_no_new_privs)
 	ASSERT_EQ(0, prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
 
 	/* Checks that a failed call does not set no_new_privs. */
-	EXPECT_EQ(-1, landlock_restrict_self(
+	ASSERT_EQ(-1, landlock_restrict_self(
 			      -1, LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS));
 	EXPECT_EQ(EBADF, errno);
 	EXPECT_EQ(0, prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
@@ -596,7 +596,7 @@ TEST(cred_transfer)
 	EXPECT_EQ(0, close(ruleset_fd));
 
 	/* Checks ruleset enforcement. */
-	EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+	ASSERT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
 	EXPECT_EQ(EACCES, errno);
 
 	/* Needed for KEYCTL_SESSION_TO_PARENT permission checks */
@@ -610,7 +610,7 @@ TEST(cred_transfer)
 	ASSERT_LE(0, child);
 	if (child == 0) {
 		/* Checks ruleset enforcement. */
-		EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+		ASSERT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
 		EXPECT_EQ(EACCES, errno);
 
 		/*
@@ -629,7 +629,7 @@ TEST(cred_transfer)
 				     0, 0, 0));
 
 		/* Re-checks ruleset enforcement. */
-		EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+		ASSERT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
 		EXPECT_EQ(EACCES, errno);
 
 		_exit(_metadata->exit_code);
@@ -641,7 +641,7 @@ TEST(cred_transfer)
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/* Re-checks ruleset enforcement. */
-	EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+	ASSERT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
 	EXPECT_EQ(EACCES, errno);
 }
 
tools/testing/selftests/landlock/cap_test.c +15 -15
index 5a7dbd26f92f..8e6e92450b13 100644
@@ -243,7 +243,7 @@ TEST(add_rule_unknown_no_runtime_effect)
 	 * CAP_SYS_ADMIN.
 	 */
 	set_cap(_metadata, CAP_SYS_ADMIN);
-	EXPECT_EQ(-1, sethostname("test", 4));
+	ASSERT_EQ(-1, sethostname("test", 4));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 }
@@ -366,7 +366,7 @@ TEST_F(cap_enforce, use)
 	/* Test CAP_SYS_ADMIN via sethostname. */
 	set_cap(_metadata, CAP_SYS_ADMIN);
 	if (variant->expected_sysadmin) {
-		EXPECT_EQ(-1, sethostname("test", 4));
+		ASSERT_EQ(-1, sethostname("test", 4));
 		EXPECT_EQ(variant->expected_sysadmin, errno);
 	} else {
 		EXPECT_EQ(0, sethostname("test", 4));
@@ -376,7 +376,7 @@ TEST_F(cap_enforce, use)
 	/* Test CAP_SYS_CHROOT via chroot. */
 	set_cap(_metadata, CAP_SYS_CHROOT);
 	if (variant->expected_chroot) {
-		EXPECT_EQ(-1, chroot("/"));
+		ASSERT_EQ(-1, chroot("/"));
 		EXPECT_EQ(variant->expected_chroot, errno);
 	} else {
 		EXPECT_EQ(0, chroot("/"));
@@ -532,7 +532,7 @@ TEST_F(cap_stacking, two_layers)
 	/* Test CAP_SYS_ADMIN via sethostname. */
 	set_cap(_metadata, CAP_SYS_ADMIN);
 	if (variant->expected_sysadmin) {
-		EXPECT_EQ(-1, sethostname("test", 4));
+		ASSERT_EQ(-1, sethostname("test", 4));
 		EXPECT_EQ(variant->expected_sysadmin, errno);
 	} else {
 		EXPECT_EQ(0, sethostname("test", 4));
@@ -542,7 +542,7 @@ TEST_F(cap_stacking, two_layers)
 	/* Test CAP_SYS_CHROOT via chroot. */
 	set_cap(_metadata, CAP_SYS_CHROOT);
 	if (variant->expected_chroot) {
-		EXPECT_EQ(-1, chroot("/"));
+		ASSERT_EQ(-1, chroot("/"));
 		EXPECT_EQ(variant->expected_chroot, errno);
 	} else {
 		EXPECT_EQ(0, chroot("/"));
@@ -586,7 +586,7 @@ TEST(cap_without_nnp)
 	 * CAP_SYS_ADMIN is still in effective set but Landlock denies it:
 	 * cap_capable() returns 0, then hook_capable() returns -EPERM.
 	 */
-	EXPECT_EQ(-1, sethostname("test", 4));
+	ASSERT_EQ(-1, sethostname("test", 4));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
@@ -654,7 +654,7 @@ TEST(cap_userns_ownership_bypass)
 		 * (from user NS creation), so cap_capable() returns 0.  But
 		 * Landlock denies because no rule allows CAP_SYS_CHROOT.
 		 */
-		EXPECT_EQ(-1, chroot("/"));
+		ASSERT_EQ(-1, chroot("/"));
 		EXPECT_EQ(EPERM, errno);
 
 		_exit(_metadata->exit_code);
@@ -810,7 +810,7 @@ TEST_F(cap_audit, denied)
 
 	/* Deny CAP_SYS_CHROOT (no allow rule). */
 	set_cap(_metadata, CAP_SYS_CHROOT);
-	EXPECT_EQ(-1, chroot("/"));
+	ASSERT_EQ(-1, chroot("/"));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_CHROOT);
 
@@ -896,7 +896,7 @@ TEST_F(cap_audit, quieted)
 	EXPECT_EQ(0, close(ruleset_fd));
 
 	set_cap(_metadata, CAP_SYS_CHROOT);
-	EXPECT_EQ(-1, chroot("/"));
+	ASSERT_EQ(-1, chroot("/"));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_CHROOT);
 
@@ -905,7 +905,7 @@ TEST_F(cap_audit, quieted)
 	 * denial IS logged.  This proves quieting is per-member, not global.
 	 */
 	set_cap(_metadata, CAP_SYS_ADMIN);
-	EXPECT_EQ(-1, sethostname("test", 4));
+	ASSERT_EQ(-1, sethostname("test", 4));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
@@ -952,7 +952,7 @@ TEST_F(cap_audit, quiet_youngest_layer_wins)
 	EXPECT_EQ(0, close(ruleset_fd));
 
 	set_cap(_metadata, CAP_SYS_CHROOT);
-	EXPECT_EQ(-1, chroot("/"));
+	ASSERT_EQ(-1, chroot("/"));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_CHROOT);
 
@@ -986,7 +986,7 @@ TEST_F(cap_audit, quiet_unknown_bit_no_effect)
 	EXPECT_EQ(0, close(ruleset_fd));
 
 	set_cap(_metadata, CAP_SYS_CHROOT);
-	EXPECT_EQ(-1, chroot("/"));
+	ASSERT_EQ(-1, chroot("/"));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_CHROOT);
 
@@ -1059,7 +1059,7 @@ TEST_F(cap_audit, noaudit_probe_not_logged)
 	 * domain IS logged.  This proves the empty record set above is caused
 	 * by CAP_OPT_NOAUDIT, not by an always-allow or always-silent bug.
 	 */
-	EXPECT_EQ(-1, sethostname("test", 4));
+	ASSERT_EQ(-1, sethostname("test", 4));
 	EXPECT_EQ(EPERM, errno);
 	clear_cap(_metadata, CAP_SYS_ADMIN);
 
@@ -1271,12 +1271,12 @@ static void exercise_cap_trace_denial(struct __test_metadata *const _metadata,
 		EXPECT_EQ(0, close(fd));
 
 		/* Control: an ordinary denial in the same domain is traced. */
-		EXPECT_EQ(-1, chroot("/"));
+		ASSERT_EQ(-1, chroot("/"));
 		EXPECT_EQ(EPERM, errno);
 		clear_cap(_metadata, CAP_SYS_ADMIN);
 		clear_cap(_metadata, CAP_SYS_CHROOT);
 	} else {
-		EXPECT_EQ(-1, chroot("/"));
+		ASSERT_EQ(-1, chroot("/"));
 		EXPECT_EQ(EPERM, errno);
 		clear_cap(_metadata, CAP_SYS_CHROOT);
 	}
tools/testing/selftests/landlock/fs_test.c +25 -25

195 lines not on this page · raw message

tools/testing/selftests/landlock/net_test.c +12 -12
index 563e2214390b..51bcb4740dde 100644
@@ -1361,11 +1361,11 @@ TEST_F(protocol, tcp_fastopen)
 	}
 
 	if (restricted) {
-		EXPECT_EQ(-1, read(client_fd, &buf, 1));
+		ASSERT_EQ(-1, read(client_fd, &buf, 1));
 		EXPECT_EQ(ENOTCONN, errno);
 	} else if (self->srv0.protocol.domain == AF_UNIX &&
 		   self->srv0.protocol.type == SOCK_STREAM) {
-		EXPECT_EQ(-1, read(client_fd, &buf, 1));
+		ASSERT_EQ(-1, read(client_fd, &buf, 1));
 		EXPECT_EQ(EINVAL, errno);
 	} else {
 		EXPECT_EQ(1, read(client_fd, &buf, 1));
@@ -2261,7 +2261,7 @@ TEST_F(mini, ruleset_with_unknown_access)
 			.handled_access_net = access_mask,
 		};
 
-		EXPECT_EQ(-1, landlock_create_ruleset(&ruleset_attr,
+		ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr,
 						      sizeof(ruleset_attr), 0));
 		EXPECT_EQ(EINVAL, errno);
 	}
@@ -2284,7 +2284,7 @@ TEST_F(mini, rule_with_unknown_access)
 
 	for (access = 1ULL << 63; access != ACCESS_LAST; access >>= 1) {
 		net_port.allowed_access = access;
-		EXPECT_EQ(-1,
+		ASSERT_EQ(-1,
 			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 					    &net_port, 0));
 		EXPECT_EQ(EINVAL, errno);
@@ -2316,7 +2316,7 @@ TEST_F(mini, rule_with_unhandled_access)
 		if (access == ruleset_attr.handled_access_net) {
 			EXPECT_EQ(0, err);
 		} else {
-			EXPECT_EQ(-1, err);
+			ASSERT_EQ(-1, err);
 			EXPECT_EQ(EINVAL, errno);
 		}
 	}
@@ -2349,12 +2349,12 @@ TEST_F(mini, inval)
 	ASSERT_LE(0, ruleset_fd);
 
 	/* Checks unhandled allowed_access. */
-	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 					&tcp_bind_connect, 0));
 	EXPECT_EQ(EINVAL, errno);
 
 	/* Checks zero access value. */
-	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 					&tcp_denied, 0));
 	EXPECT_EQ(ENOMSG, errno);
 
@@ -2414,15 +2414,15 @@ TEST_F(mini, tcp_port_overflow)
 	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 				       &port_max_bind, 0));
 
-	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 					&port_overflow1, 0));
 	EXPECT_EQ(EINVAL, errno);
 
-	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 					&port_overflow2, 0));
 	EXPECT_EQ(EINVAL, errno);
 
-	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 					&port_overflow3, 0));
 	EXPECT_EQ(EINVAL, errno);
 
@@ -2430,7 +2430,7 @@ TEST_F(mini, tcp_port_overflow)
 	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 				       &port_max_connect, 0));
 
-	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
 					&port_overflow4, 0));
 	EXPECT_EQ(EINVAL, errno);
 
@@ -2549,7 +2549,7 @@ TEST_F(ipv4_tcp, with_fs)
 	EXPECT_EQ(0, close(dir_fd));
 
 	dir_fd = open("/", O_RDONLY);
-	EXPECT_EQ(-1, dir_fd);
+	ASSERT_EQ(-1, dir_fd);
 	EXPECT_EQ(EACCES, errno);
 
 	/* Tests port binding. */
tools/testing/selftests/landlock/ns_test.c +34 -34

272 lines not on this page · raw message

tools/testing/selftests/landlock/ptrace_test.c +7 -7
index 4fd2b440aa45..ea8e4be6659e 100644
@@ -204,7 +204,7 @@ TEST_F(scoped_domains, trace)
 		if (can_trace_parent) {
 			EXPECT_EQ(0, ret);
 		} else {
-			EXPECT_EQ(-1, ret);
+			ASSERT_EQ(-1, ret);
 			EXPECT_EQ(EPERM, errno);
 		}
 		if (ret == 0) {
@@ -218,7 +218,7 @@ TEST_F(scoped_domains, trace)
 		if (can_trace_child) {
 			EXPECT_EQ(0, ret);
 		} else {
-			EXPECT_EQ(-1, ret);
+			ASSERT_EQ(-1, ret);
 			EXPECT_EQ(EPERM, errno);
 		}
 
@@ -259,7 +259,7 @@ TEST_F(scoped_domains, trace)
 		ASSERT_EQ(0, ptrace(PTRACE_DETACH, child, NULL, 0));
 	} else {
 		/* The child should not be traced by the parent. */
-		EXPECT_EQ(-1, ptrace(PTRACE_DETACH, child, NULL, 0));
+		ASSERT_EQ(-1, ptrace(PTRACE_DETACH, child, NULL, 0));
 		EXPECT_EQ(ESRCH, errno);
 	}
 
@@ -276,7 +276,7 @@ TEST_F(scoped_domains, trace)
 	if (can_trace_child) {
 		EXPECT_EQ(0, ret);
 	} else {
-		EXPECT_EQ(-1, ret);
+		ASSERT_EQ(-1, ret);
 		EXPECT_EQ(EPERM, errno);
 	}
 
@@ -377,7 +377,7 @@ TEST_F(audit, trace)
 		ASSERT_EQ(1, read(pipe_parent[0], &buf_child, 1));
 
 		/* Tests child PTRACE_TRACEME. */
-		EXPECT_EQ(-1, ptrace(PTRACE_TRACEME));
+		ASSERT_EQ(-1, ptrace(PTRACE_TRACEME));
 		EXPECT_EQ(EPERM, errno);
 		/* We should see the child process. */
 		EXPECT_EQ(0, matches_log_ptrace(_metadata, self->audit_fd,
@@ -414,11 +414,11 @@ TEST_F(audit, trace)
 	ASSERT_EQ(1, read(pipe_child[0], &buf_parent, 1));
 
 	/* The child should not be traced by the parent. */
-	EXPECT_EQ(-1, ptrace(PTRACE_DETACH, child, NULL, 0));
+	ASSERT_EQ(-1, ptrace(PTRACE_DETACH, child, NULL, 0));
 	EXPECT_EQ(ESRCH, errno);
 
 	/* Tests PTRACE_ATTACH on the child. */
-	EXPECT_EQ(-1, ptrace(PTRACE_ATTACH, child, NULL, 0));
+	ASSERT_EQ(-1, ptrace(PTRACE_ATTACH, child, NULL, 0));
 	EXPECT_EQ(EPERM, errno);
 	EXPECT_EQ(0, matches_log_ptrace(_metadata, self->audit_fd, child));
 
tools/testing/selftests/landlock/scoped_abstract_unix_test.c +7 -7
index df41da5f2ab0..4c7f9c102d50 100644
@@ -122,7 +122,7 @@ TEST_F(scoped_domains, connect_to_parent)
 		if (can_connect_to_parent) {
 			EXPECT_EQ(0, err);
 		} else {
-			EXPECT_EQ(-1, err);
+			ASSERT_EQ(-1, err);
 			EXPECT_EQ(EPERM, errno);
 		}
 		EXPECT_EQ(0, close(stream_client));
@@ -132,7 +132,7 @@ TEST_F(scoped_domains, connect_to_parent)
 		if (can_connect_to_parent) {
 			EXPECT_EQ(0, err);
 		} else {
-			EXPECT_EQ(-1, err);
+			ASSERT_EQ(-1, err);
 			EXPECT_EQ(EPERM, errno);
 		}
 		EXPECT_EQ(0, close(dgram_client));
@@ -417,7 +417,7 @@ TEST_F(scoped_audit, connect_to_child)
 	ASSERT_EQ(1, read(pipe_child[0], &buf, 1));
 	err_dgram = connect(dgram_client, &self->dgram_address.unix_addr,
 			    self->dgram_address.unix_addr_len);
-	EXPECT_EQ(-1, err_dgram);
+	ASSERT_EQ(-1, err_dgram);
 	EXPECT_EQ(EPERM, errno);
 
 	if (should_audit) {
@@ -762,7 +762,7 @@ TEST_F(outside_socket, socket_with_different_domain)
 		if (variant->child_socket) {
 			EXPECT_EQ(0, err);
 		} else {
-			EXPECT_EQ(-1, err);
+			ASSERT_EQ(-1, err);
 			EXPECT_EQ(EPERM, errno);
 		}
 		EXPECT_EQ(0, close(client_socket));
@@ -939,7 +939,7 @@ TEST_F(various_address_sockets, scoped_pathname_sockets)
 			      &stream_abstract_addr.unix_addr,
 			      stream_abstract_addr.unix_addr_len);
 		if (variant->domain == SCOPE_SANDBOX) {
-			EXPECT_EQ(-1, err);
+			ASSERT_EQ(-1, err);
 			EXPECT_EQ(EPERM, errno);
 		} else {
 			EXPECT_EQ(0, err);
@@ -954,7 +954,7 @@ TEST_F(various_address_sockets, scoped_pathname_sockets)
 			     &dgram_abstract_addr.unix_addr,
 			     dgram_abstract_addr.unix_addr_len);
 		if (variant->domain == SCOPE_SANDBOX) {
-			EXPECT_EQ(-1, err);
+			ASSERT_EQ(-1, err);
 			EXPECT_EQ(EPERM, errno);
 		} else {
 			EXPECT_EQ(1, err);
@@ -965,7 +965,7 @@ TEST_F(various_address_sockets, scoped_pathname_sockets)
 			      &dgram_abstract_addr.unix_addr,
 			      dgram_abstract_addr.unix_addr_len);
 		if (variant->domain == SCOPE_SANDBOX) {
-			EXPECT_EQ(-1, err);
+			ASSERT_EQ(-1, err);
 			EXPECT_EQ(EPERM, errno);
 		} else {
 			EXPECT_EQ(0, err);
Signature · 1 line
2.56.0
#48 lore raw

Re: [PATCH v2 24/27] selftests/landlock: Use ASSERT for syscall failures before errno checks

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/landlock: ASSERT_EQ() after PTRACE_ATTACH skips detach and stops peer --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/landlock: ASSERT_EQ() after PTRACE_ATTACH skips detach and stops peer --

commit 9f0b61a144fcc6c9f95be698e621be787a64a44b
Author: Günther Noack <[email protected]>

selftests/landlock: Use ASSERT for syscall failures before errno checks

This commit replaces EXPECT_EQ() with ASSERT_EQ() for syscall return value checks in Landlock selftests so tests abort before checking errno when a syscall expected to fail unexpectedly succeeds.

Quoted 13 lines
tools/testing/selftests/landlock/ptrace_test.c +1 -1
index 4fd2b440aa455..ea8e4be6659e9 100644
@@ -204,7 +204,7 @@ TEST_F(scoped_domains, trace)
 		if (can_trace_parent) {
 			EXPECT_EQ(0, ret);
 		} else {
-			EXPECT_EQ(-1, ret);
+			ASSERT_EQ(-1, ret);
 			EXPECT_EQ(EPERM, errno);
 		}
 		if (ret == 0) {

[ ... ]

tools/testing/selftests/landlock/ptrace_test.c TEST_F(scoped_domains, trace) · line 281

Quoted 8 lines
tools/testing/selftests/landlock/ptrace_test.c +1 -1
@@ -276,7 +276,7 @@ TEST_F(scoped_domains, trace)
 	if (can_trace_child) {
 		EXPECT_EQ(0, ret);
 	} else {
-		EXPECT_EQ(-1, ret);
+		ASSERT_EQ(-1, ret);
 		EXPECT_EQ(EPERM, errno);
 	}

[Severity: Medium]
As reported by Sashiko on v1 [1], in TEST_F(scoped_domains, trace) in tools/testing/selftests/landlock/ptrace_test.c, if ptrace(PTRACE_ATTACH, ...) unexpectedly succeeds (ret == 0) when can_trace_parent or can_trace_child is false, the target process is sent SIGSTOP by the kernel.

Previously, with EXPECT_EQ(-1, ret), execution continued into the following cleanup block even on an unexpected attach:

	if (ret == 0) {
		ASSERT_EQ(parent, waitpid(parent, &status, 0));
		ASSERT_EQ(1, WIFSTOPPED(status));
		ASSERT_EQ(0, ptrace(PTRACE_DETACH, parent, NULL, 0));
	}

which waited for the stop and detached the tracee cleanly.

With ASSERT_EQ(-1, ret), doesn't an unexpected attach success abort the tracer before reaching the if (ret == 0) block, leaving the tracee in TASK_STOPPED from the pending SIGSTOP (and causing the parent test process to hang until the kselftest harness timeout when the child attaches to the parent)?

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#49 lore raw

PATCH v2 25/27 selftests/landlock: Assert WIFEXITED() before checking the exit status

In reply to Günther Noack

WEXITSTATUS() is only meaningful if WIFEXITED() was true (see wait(2)). Check WIFEXITED() with ASSERT, as most tests already do, so that a signaled child does not also produce a misleading exit status failure.

---
 tools/testing/selftests/landlock/base_test.c |  2 +-
 tools/testing/selftests/landlock/fs_test.c   | 10 +++++-----
 tools/testing/selftests/landlock/net_test.c  | 14 +++++++-------
 3 files changed, 13 insertions(+), 13 deletions(-)
tools/testing/selftests/landlock/base_test.c +1 -1
index b1a729b94e7b..4cb30b943aa8 100644
@@ -637,7 +637,7 @@ TEST(cred_transfer)
 	}
 
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/* Re-checks ruleset enforcement. */
tools/testing/selftests/landlock/fs_test.c +5 -5
index fe1ca9c2d949..183dfc42895c 100644
@@ -4640,7 +4640,7 @@ static void test_connect_to_parent(struct __test_metadata *const _metadata,
 
 	/* Wait for child. */
 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/* Clean up. */
@@ -4739,7 +4739,7 @@ static void test_connect_to_child(struct __test_metadata *const _metadata,
 
 	/* Wait for child. */
 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 }
 
@@ -5409,7 +5409,7 @@ TEST_F_FORK(layout1_bind, path_disconnected_rename)
 	}
 
 	EXPECT_EQ(child_pid, waitpid(child_pid, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	ASSERT_EQ(0, rename(dir_s4d2, dir_s1d3))
@@ -5431,7 +5431,7 @@ TEST_F_FORK(layout1_bind, path_disconnected_rename)
 	}
 
 	EXPECT_EQ(child_pid, waitpid(child_pid, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/*
@@ -7985,7 +7985,7 @@ TEST_F(audit_layout1, resolve_unix)
 	}
 
 	ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	EXPECT_EQ(0, matches_log_fs_extra(_metadata, self->audit_fd,
tools/testing/selftests/landlock/net_test.c +7 -7
index 51bcb4740dde..e194fd78d8a1 100644
@@ -903,7 +903,7 @@ static void test_bind_and_connect(struct __test_metadata *const _metadata,
 	}
 
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/* Closes connection, if any. */
@@ -1274,7 +1274,7 @@ TEST_F(protocol, connect_unspec)
 	}
 
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/* Closes connection, if any. */
@@ -1373,7 +1373,7 @@ TEST_F(protocol, tcp_fastopen)
 	}
 
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	if (client_fd != bind_fd)
@@ -1528,7 +1528,7 @@ TEST_F(protocol, sendmsg_dgram)
 		_exit(_metadata->exit_code);
 	}
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/*
@@ -1571,7 +1571,7 @@ TEST_F(protocol, sendmsg_dgram)
 		return;
 	}
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/*
@@ -1642,7 +1642,7 @@ TEST_F(protocol, sendmsg_dgram)
 		return;
 	}
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	/*
@@ -1677,7 +1677,7 @@ TEST_F(protocol, sendmsg_dgram)
 		_exit(_metadata->exit_code);
 	}
 	EXPECT_EQ(child, waitpid(child, &status, 0));
-	EXPECT_EQ(1, WIFEXITED(status));
+	ASSERT_EQ(1, WIFEXITED(status));
 	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 }
 
Signature · 1 line
2.56.0
#50 lore raw

Re: [PATCH v2 25/27] selftests/landlock: Assert WIFEXITED() before checking the exit status

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

WEXITSTATUS() is only meaningful if WIFEXITED() was true (see wait(2)). Check WIFEXITED() with ASSERT, as most tests already do, so that a signaled child does not also produce a misleading exit status failure.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
#51 lore raw

PATCH v2 26/27 selftests/landlock: Use EXPECT for closing FDs and stopping threads

In reply to Günther Noack

Closing FDs and cancelling or joining threads whose results are not used is teardown, whether it happens at the end of a test or in the middle of it. No later check depends on it, so a failure should be reported without aborting the test. Use EXPECT for these calls, as most tests already do. The same applies to removing the FIFO and reaping the child at the end of named_pipe_ioctl.

The close(0) in ioctl_error() stays an ASSERT, as the following IOCTL relies on FD 0 being closed.

---
 tools/testing/selftests/landlock/base_test.c     |  40 ++++-----
 tools/testing/selftests/landlock/fs_test.c       | 106 +++++++++++------------
 tools/testing/selftests/landlock/ptrace_test.c   |  16 ++--
 tools/testing/selftests/landlock/trace_fs_test.c |   8 +-
 tools/testing/selftests/landlock/trace_test.c    |   6 +-
 tools/testing/selftests/landlock/tsync_test.c    |  22 ++---
 6 files changed, 99 insertions(+), 99 deletions(-)
tools/testing/selftests/landlock/base_test.c +20 -20
index 4cb30b943aa8..46af291edaef 100644
@@ -203,7 +203,7 @@ TEST(create_ruleset_checks_ordering)
 	ruleset_fd =
 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
 	ASSERT_LE(0, ruleset_fd);
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 }
 
 /* Tests ordering of syscall argument checks. */
@@ -249,8 +249,8 @@ TEST(add_rule_checks_ordering)
 	ASSERT_LE(0, path_beneath_attr.parent_fd);
 	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
 				       &path_beneath_attr, 0));
-	ASSERT_EQ(0, close(path_beneath_attr.parent_fd));
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(path_beneath_attr.parent_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 }
 
 /* Tests ordering of syscall argument and permission checks. */
@@ -272,7 +272,7 @@ TEST(restrict_self_checks_ordering)
 	ASSERT_LE(0, path_beneath_attr.parent_fd);
 	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
 				       &path_beneath_attr, 0));
-	ASSERT_EQ(0, close(path_beneath_attr.parent_fd));
+	EXPECT_EQ(0, close(path_beneath_attr.parent_fd));
 
 	/* Checks unprivileged enforcement without no_new_privs. */
 	drop_caps(_metadata);
@@ -307,7 +307,7 @@ TEST(restrict_self_checks_ordering)
 
 	/* Checks valid call. */
 	ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 }
 
 TEST(restrict_self_max_layers)
@@ -328,7 +328,7 @@ TEST(restrict_self_max_layers)
 	ASSERT_LE(0, path_beneath_attr.parent_fd);
 	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
 				       &path_beneath_attr, 0));
-	ASSERT_EQ(0, close(path_beneath_attr.parent_fd));
+	EXPECT_EQ(0, close(path_beneath_attr.parent_fd));
 
 	/* Enforces the maximum number of allowed layers. */
 	for (int i = 0; i < LANDLOCK_MAX_NUM_LAYERS; i++)
@@ -342,7 +342,7 @@ TEST(restrict_self_max_layers)
 
 	/* Checks that the failed call did not set no_new_privs. */
 	ASSERT_EQ(0, prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 }
 
 TEST(restrict_self_fd)
@@ -496,7 +496,7 @@ TEST(ruleset_fd_io)
 	ASSERT_EQ(-1, read(ruleset_fd, &buf, 1));
 	ASSERT_EQ(EINVAL, errno);
 
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 }
 
 /* Tests enforcement of a ruleset FD transferred through a UNIX socket. */
@@ -525,14 +525,14 @@ TEST(ruleset_fd_transfer)
 	ASSERT_EQ(0,
 		  landlock_add_rule(ruleset_fd_tx, LANDLOCK_RULE_PATH_BENEATH,
 				    &path_beneath_attr, 0));
-	ASSERT_EQ(0, close(path_beneath_attr.parent_fd));
+	EXPECT_EQ(0, close(path_beneath_attr.parent_fd));
 
 	/* Sends the ruleset FD over a socketpair and then close it. */
 	ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
 				socket_fds));
 	ASSERT_EQ(0, send_fd(socket_fds[0], ruleset_fd_tx));
-	ASSERT_EQ(0, close(socket_fds[0]));
-	ASSERT_EQ(0, close(ruleset_fd_tx));
+	EXPECT_EQ(0, close(socket_fds[0]));
+	EXPECT_EQ(0, close(ruleset_fd_tx));
 
 	child = fork();
 	ASSERT_LE(0, child);
@@ -540,32 +540,32 @@ TEST(ruleset_fd_transfer)
 		const int ruleset_fd_rx = recv_fd(socket_fds[1]);
 
 		ASSERT_LE(0, ruleset_fd_rx);
-		ASSERT_EQ(0, close(socket_fds[1]));
+		EXPECT_EQ(0, close(socket_fds[1]));
 
 		/* Enforces the received ruleset on the child. */
 		ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
 		ASSERT_EQ(0, landlock_restrict_self(ruleset_fd_rx, 0));
-		ASSERT_EQ(0, close(ruleset_fd_rx));
+		EXPECT_EQ(0, close(ruleset_fd_rx));
 
 		/* Checks that the ruleset enforcement. */
 		ASSERT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
 		ASSERT_EQ(EACCES, errno);
 		dir_fd = open("/tmp", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
 		ASSERT_LE(0, dir_fd);
-		ASSERT_EQ(0, close(dir_fd));
+		EXPECT_EQ(0, close(dir_fd));
 		_exit(_metadata->exit_code);
 		return;
 	}
 
-	ASSERT_EQ(0, close(socket_fds[1]));
+	EXPECT_EQ(0, close(socket_fds[1]));
 
 	/* Checks that the parent is unrestricted. */
 	dir_fd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
 	ASSERT_LE(0, dir_fd);
-	ASSERT_EQ(0, close(dir_fd));
+	EXPECT_EQ(0, close(dir_fd));
 	dir_fd = open("/tmp", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
 	ASSERT_LE(0, dir_fd);
-	ASSERT_EQ(0, close(dir_fd));
+	EXPECT_EQ(0, close(dir_fd));
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(1, WIFEXITED(status));
@@ -670,9 +670,9 @@ TEST(useless_quiet_rule_fs)
 	ASSERT_EQ(EINVAL, errno);
 
 	/* Check that the rule had not been added. */
-	ASSERT_EQ(0, close(root_fd));
+	EXPECT_EQ(0, close(root_fd));
 	enforce_ruleset(_metadata, ruleset_fd);
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 
 	ASSERT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
 	ASSERT_EQ(EACCES, errno);
@@ -700,7 +700,7 @@ TEST(useless_quiet_rule_net)
 				    &net_port_attr, LANDLOCK_ADD_RULE_QUIET));
 	ASSERT_EQ(EINVAL, errno);
 
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 }
 
 TEST(invalid_quiet_bits_1)
tools/testing/selftests/landlock/fs_test.c +53 -53

343 lines not on this page · raw message

tools/testing/selftests/landlock/ptrace_test.c +8 -8
index ea8e4be6659e..f253c6a3a436 100644
@@ -183,8 +183,8 @@ TEST_F(scoped_domains, trace)
 	if (child == 0) {
 		char buf_child;
 
-		ASSERT_EQ(0, close(pipe_parent[1]));
-		ASSERT_EQ(0, close(pipe_child[0]));
+		EXPECT_EQ(0, close(pipe_parent[1]));
+		EXPECT_EQ(0, close(pipe_child[0]));
 		if (variant->domain_child)
 			create_domain(_metadata);
 
@@ -238,8 +238,8 @@ TEST_F(scoped_domains, trace)
 		return;
 	}
 
-	ASSERT_EQ(0, close(pipe_child[1]));
-	ASSERT_EQ(0, close(pipe_parent[0]));
+	EXPECT_EQ(0, close(pipe_child[1]));
+	EXPECT_EQ(0, close(pipe_parent[0]));
 	if (variant->domain_parent)
 		create_domain(_metadata);
 
@@ -370,8 +370,8 @@ TEST_F(audit, trace)
 	if (child == 0) {
 		char buf_child;
 
-		ASSERT_EQ(0, close(pipe_parent[1]));
-		ASSERT_EQ(0, close(pipe_child[0]));
+		EXPECT_EQ(0, close(pipe_parent[1]));
+		EXPECT_EQ(0, close(pipe_child[0]));
 
 		/* Waits for the parent to be in a domain, if any. */
 		ASSERT_EQ(1, read(pipe_parent[0], &buf_child, 1));
@@ -400,8 +400,8 @@ TEST_F(audit, trace)
 		return;
 	}
 
-	ASSERT_EQ(0, close(pipe_child[1]));
-	ASSERT_EQ(0, close(pipe_parent[0]));
+	EXPECT_EQ(0, close(pipe_child[1]));
+	EXPECT_EQ(0, close(pipe_parent[0]));
 	create_domain(_metadata);
 
 	/* Signals that the parent is in a domain. */
tools/testing/selftests/landlock/trace_fs_test.c +4 -4
index 64014ade3a0e..06161aa6d5d7 100644
@@ -210,8 +210,8 @@ TEST_F(trace_fs, add_rule_path_beneath)
 
 	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
 				       &path_beneath, 0));
-	ASSERT_EQ(0, close(path_beneath.parent_fd));
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(path_beneath.parent_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 
 	buf = tracefs_read_buf();
 	ASSERT_NE(NULL, buf);
@@ -274,8 +274,8 @@ TEST_F(trace_fs, add_rule_path_beneath_escaped_path_overflow)
 
 	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
 				       &path_beneath, 0));
-	ASSERT_EQ(0, close(path_beneath.parent_fd));
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(path_beneath.parent_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 
 	buf = tracefs_read_buf();
 	ASSERT_NE(NULL, buf);
tools/testing/selftests/landlock/trace_test.c +3 -3
index 1795d41b779f..0effe5d6d973 100644
@@ -163,7 +163,7 @@ TEST_F(trace, create_ruleset)
 	ruleset_fd =
 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
 	ASSERT_LE(0, ruleset_fd);
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 
 	buf = tracefs_read_buf();
 	ASSERT_NE(NULL, buf);
@@ -514,7 +514,7 @@ TEST_F(trace, add_rule_invalid_fd)
 	/* Invalid ruleset fd (-1). */
 	ASSERT_EQ(-1, landlock_add_rule(-1, LANDLOCK_RULE_PATH_BENEATH,
 					&path_beneath, 0));
-	ASSERT_EQ(0, close(path_beneath.parent_fd));
+	EXPECT_EQ(0, close(path_beneath.parent_fd));
 
 	buf = tracefs_read_buf();
 	ASSERT_NE(NULL, buf);
@@ -550,7 +550,7 @@ TEST_F(trace, create_domain_invalid)
 	/* Unknown flags. */
 	ASSERT_EQ(-1, landlock_restrict_self(ruleset_fd, -1));
 
-	ASSERT_EQ(0, close(ruleset_fd));
+	EXPECT_EQ(0, close(ruleset_fd));
 
 	buf = tracefs_read_buf();
 	ASSERT_NE(NULL, buf);
tools/testing/selftests/landlock/tsync_test.c +11 -11
index 2b53596c986e..aff973ba4735 100644
@@ -152,10 +152,10 @@ TEST_F(multi_threaded, restrict)
 	EXPECT_EQ(variant->expected_no_new_privs,
 		  prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
 
-	ASSERT_EQ(0, pthread_cancel(t1));
-	ASSERT_EQ(0, pthread_cancel(t2));
-	ASSERT_EQ(0, pthread_join(t1, NULL));
-	ASSERT_EQ(0, pthread_join(t2, NULL));
+	EXPECT_EQ(0, pthread_cancel(t1));
+	EXPECT_EQ(0, pthread_cancel(t2));
+	EXPECT_EQ(0, pthread_join(t1, NULL));
+	EXPECT_EQ(0, pthread_join(t2, NULL));
 
 	/* Checks the no_new_privs state of the sibling threads. */
 	EXPECT_EQ(variant->expected_no_new_privs, no_new_privs1);
@@ -184,10 +184,10 @@ TEST(multi_threaded_success_despite_diverging_domains)
 	EXPECT_EQ(0, landlock_restrict_self(ruleset_fd,
 					    LANDLOCK_RESTRICT_SELF_TSYNC));
 
-	ASSERT_EQ(0, pthread_cancel(t1));
-	ASSERT_EQ(0, pthread_cancel(t2));
-	ASSERT_EQ(0, pthread_join(t1, NULL));
-	ASSERT_EQ(0, pthread_join(t2, NULL));
+	EXPECT_EQ(0, pthread_cancel(t1));
+	EXPECT_EQ(0, pthread_cancel(t2));
+	EXPECT_EQ(0, pthread_join(t1, NULL));
+	EXPECT_EQ(0, pthread_join(t2, NULL));
 	EXPECT_EQ(0, close(ruleset_fd));
 }
 
@@ -309,11 +309,11 @@ TEST(tsync_interrupt)
 					    LANDLOCK_RESTRICT_SELF_TSYNC));
 
 	sd.stop = true;
-	ASSERT_EQ(0, pthread_join(signaler, NULL));
+	EXPECT_EQ(0, pthread_join(signaler, NULL));
 
 	for (i = 0; i < NUM_IDLE_THREADS; i++) {
-		ASSERT_EQ(0, pthread_cancel(threads[i]));
-		ASSERT_EQ(0, pthread_join(threads[i], NULL));
+		EXPECT_EQ(0, pthread_cancel(threads[i]));
+		EXPECT_EQ(0, pthread_join(threads[i], NULL));
 	}
 
 	EXPECT_EQ(0, close(ruleset_fd));
Signature · 1 line
2.56.0
#52 lore raw

Re: [PATCH v2 26/27] selftests/landlock: Use EXPECT for closing FDs and stopping threads

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/landlock: EXPECT_EQ before reading thread output in restrict() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/landlock: EXPECT_EQ before reading thread output in restrict() --

commit 85f196c7c04b9c3b9c11f5b1ad1ee130ba7d4079
Author: Günther Noack <[email protected]>

selftests/landlock: Use EXPECT for closing FDs and stopping threads

This commit changes ASSERT_EQ to EXPECT_EQ across Landlock selftests when closing file descriptors, cancelling or joining threads whose results are unused, and cleaning up at the end of named_pipe_ioctl.

Quoted 9 lines
tools/testing/selftests/landlock/tsync_test.c +0 -1
index 2b53596c986e0..aff973ba4735b 100644
@@ -152,10 +152,10 @@ TEST_F(multi_threaded, restrict)
 	EXPECT_EQ(variant->expected_no_new_privs,
 		  prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
 
-	ASSERT_EQ(0, pthread_cancel(t1));

tools/testing/selftests/landlock/tsync_test.c TEST_F(multi_threaded, restrict) · line 161

Quoted 10 lines
tools/testing/selftests/landlock/tsync_test.c +4 -3
-	ASSERT_EQ(0, pthread_cancel(t2));
-	ASSERT_EQ(0, pthread_join(t1, NULL));
-	ASSERT_EQ(0, pthread_join(t2, NULL));
+	EXPECT_EQ(0, pthread_cancel(t1));
+	EXPECT_EQ(0, pthread_cancel(t2));
+	EXPECT_EQ(0, pthread_join(t1, NULL));
+	EXPECT_EQ(0, pthread_join(t2, NULL));
 
 	/* Checks the no_new_privs state of the sibling threads. */
 	EXPECT_EQ(variant->expected_no_new_privs, no_new_privs1);

[Severity: Medium]
In TEST_F(multi_threaded, restrict), no_new_privs1 and no_new_privs2 are declared uninitialized on the stack and passed to the idle() threads:

tools/testing/selftests/landlock/tsync_test.c:restrict() {
	...
	bool no_new_privs1, no_new_privs2;

	ASSERT_EQ(0, pthread_create(&t1, NULL, idle, &no_new_privs1));
	ASSERT_EQ(0, pthread_create(&t2, NULL, idle, &no_new_privs2));
	...
}

In idle(), those variables are only populated when the cancellation cleanup handler store_no_new_privs() runs upon cancellation:

tools/testing/selftests/landlock/tsync_test.c:idle() {
	pthread_cleanup_push(store_no_new_privs, data);

	while (true)
		sleep(1);

	pthread_cleanup_pop(1);
}

Since the subsequent checks on no_new_privs1 and no_new_privs2 depend on pthread_cancel() and pthread_join() succeeding to populate and synchronize those values, should pthread_cancel() and pthread_join() remain ASSERT_EQ here so that a failure does not fall through into reading uninitialized stack variables?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
#53 lore raw

PATCH v2 27/27 selftests/landlock: Use EXPECT for checks that later steps do not need

In reply to Günther Noack

ASSERT aborts the test, so it should only be used when continuing after a failure is pointless. Use EXPECT for checks whose outcome nothing later in the test depends on:

* errno values after a failed syscall,
* test_open() and test_open_rel(), which leave no state behind,
* WEXITSTATUS() of a child that has exited,
* matches_log_fs() and matches_log_fs_extra(),
* the record counts returned by audit_count_records().

Keep ASSERT for operations whose side effects later checks depend on, such as renames, links and file removals.

---
 tools/testing/selftests/landlock/audit_test.c      |   10 +-
 tools/testing/selftests/landlock/base_test.c       |  104 +-
 tools/testing/selftests/landlock/cap_test.c        |   22 +-
 tools/testing/selftests/landlock/fs_test.c         | 1024 ++++++++++----------
 tools/testing/selftests/landlock/net_test.c        |    2 +-
 tools/testing/selftests/landlock/ns_test.c         |   26 +-
 .../selftests/landlock/scoped_abstract_unix_test.c |    8 +-
 .../selftests/landlock/scoped_signal_test.c        |    6 +-
 tools/testing/selftests/landlock/scoped_test.c     |    2 +-
 9 files changed, 602 insertions(+), 602 deletions(-)
tools/testing/selftests/landlock/audit_test.c +5 -5
index 392e75ddfa3c..23ab3a696299 100644
@@ -315,14 +315,14 @@ TEST_F(audit, log_subdomains_off_fork)
 	if (child == 0) {
 		ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
 		ASSERT_EQ(-1, kill(getppid(), 0));
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 		_exit(_metadata->exit_code);
 		return;
 	}
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(true, WIFEXITED(status));
-	ASSERT_EQ(0, WEXITSTATUS(status));
+	EXPECT_EQ(0, WEXITSTATUS(status));
 
 	/* The denial must be logged (baseline). */
 	EXPECT_EQ(0, matches_log_signal(_metadata, self->audit_fd, getpid(),
@@ -348,14 +348,14 @@ TEST_F(audit, log_subdomains_off_fork)
 	if (child == 0) {
 		ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
 		ASSERT_EQ(-1, kill(getppid(), 0));
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 		_exit(_metadata->exit_code);
 		return;
 	}
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(true, WIFEXITED(status));
-	ASSERT_EQ(0, WEXITSTATUS(status));
+	EXPECT_EQ(0, WEXITSTATUS(status));
 
 	/* No denial record should appear. */
 	EXPECT_EQ(-EAGAIN, matches_log_signal(_metadata, self->audit_fd,
@@ -981,7 +981,7 @@ TEST_F(audit_exec, signal_and_open)
 	EXPECT_EQ(0, close(pipe_child[0]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(1, WIFEXITED(status));
-	ASSERT_EQ(0, WEXITSTATUS(status));
+	EXPECT_EQ(0, WEXITSTATUS(status));
 
 	/* Tests that the audit record only matches the child. */
 	if (!(variant->restrict_flags &
tools/testing/selftests/landlock/base_test.c +52 -52

306 lines not on this page · raw message

tools/testing/selftests/landlock/cap_test.c +11 -11
index 8e6e92450b13..2b5767f70e26 100644
@@ -80,14 +80,14 @@ TEST(add_rule_bad_attr)
 	/* Invalid rule pointer returns EFAULT. */
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					NULL, 0));
-	ASSERT_EQ(EFAULT, errno);
+	EXPECT_EQ(EFAULT, errno);
 
 	/* Empty permissions selector returns ENOMSG. */
 	attr.permissions = 0;
 	attr.allowed_capabilities = (1ULL << CAP_NET_RAW);
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&attr, 0));
-	ASSERT_EQ(ENOMSG, errno);
+	EXPECT_EQ(ENOMSG, errno);
 
 	/* Useless rule: neither allowed nor quiet capabilities set. */
 	attr.permissions = LANDLOCK_PERMISSION_CAPABILITY_USE;
@@ -95,7 +95,7 @@ TEST(add_rule_bad_attr)
 	attr.quiet_capabilities = 0;
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&attr, 0));
-	ASSERT_EQ(ENOMSG, errno);
+	EXPECT_EQ(ENOMSG, errno);
 
 	/* Quiet-only rule (empty allowed set) is legal. */
 	attr.permissions = LANDLOCK_PERMISSION_CAPABILITY_USE;
@@ -117,14 +117,14 @@ TEST(add_rule_bad_attr)
 	attr.allowed_capabilities = (1ULL << CAP_NET_RAW);
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 
 	/* permissions with wrong type. */
 	attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE;
 	attr.allowed_capabilities = (1ULL << CAP_NET_RAW);
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 
 	/*
 	 * Unknown capability bits (e.g. bit 63) are silently accepted for
@@ -145,7 +145,7 @@ TEST(add_rule_bad_attr)
 	attr.allowed_capabilities = (1ULL << CAP_NET_RAW);
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&attr, LANDLOCK_ADD_RULE_QUIET));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 
 	EXPECT_EQ(0, close(ruleset_fd));
 
@@ -161,7 +161,7 @@ TEST(add_rule_bad_attr)
 	attr.allowed_capabilities = (1ULL << CAP_NET_RAW);
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 	EXPECT_EQ(0, close(ruleset_fd));
 }
 
@@ -705,7 +705,7 @@ TEST(cap_target_userns_independent)
 	EXPECT_EQ(0, close(sockets[0]));
 	ASSERT_EQ(target, waitpid(target, &status, 0));
 	ASSERT_TRUE(WIFEXITED(status));
-	ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
+	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	set_cap(_metadata, CAP_SYS_ADMIN);
 	for (int sandboxed = 0; sandboxed <= 1; sandboxed++) {
@@ -1154,10 +1154,10 @@ TEST_F(cap_trace, rule_events)
 				       1ULL << CAP_SYS_CHROOT));
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&invalid_attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY,
 					&valid_attr, LANDLOCK_ADD_RULE_QUIET));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 	ASSERT_EQ(0, add_cap_rule_full(ruleset_fd, 1ULL << CAP_SYS_ADMIN,
 				       1ULL << CAP_SYS_CHROOT));
 	ASSERT_EQ(0, add_cap_rule(ruleset_fd, 63));
@@ -1303,7 +1303,7 @@ static void test_cap_trace_denial(struct __test_metadata *const _metadata,
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_TRUE(WIFEXITED(status));
-	ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
+	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 
 	buf = tracefs_read_buf();
 	ASSERT_NE(NULL, buf);
tools/testing/selftests/landlock/fs_test.c +512 -512

2,244 lines not on this page · raw message

tools/testing/selftests/landlock/net_test.c +1 -1
index e194fd78d8a1..31e113873d66 100644
@@ -3720,7 +3720,7 @@ TEST_F(trace_net_address, deny_access_net)
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_TRUE(WIFEXITED(status));
-	ASSERT_EQ(0, WEXITSTATUS(status));
+	EXPECT_EQ(0, WEXITSTATUS(status));
 
 	buf = tracefs_read_buf();
 	ASSERT_NE(NULL, buf);
tools/testing/selftests/landlock/ns_test.c +13 -13
index 96b1264c4353..5c93f7010bbd 100644
@@ -121,14 +121,14 @@ TEST(add_rule_bad_attr)
 	/* Invalid rule pointer returns EFAULT. */
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					NULL, 0));
-	ASSERT_EQ(EFAULT, errno);
+	EXPECT_EQ(EFAULT, errno);
 
 	/* Empty permissions selector returns ENOMSG. */
 	attr.permissions = 0;
 	attr.allowed_namespace_types = CLONE_NEWUTS;
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&attr, 0));
-	ASSERT_EQ(ENOMSG, errno);
+	EXPECT_EQ(ENOMSG, errno);
 
 	/* Valid namespace selector plus an extra unhandled selector bit. */
 	attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE |
@@ -136,14 +136,14 @@ TEST(add_rule_bad_attr)
 	attr.allowed_namespace_types = CLONE_NEWUTS;
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 
 	/* permissions with wrong type. */
 	attr.permissions = LANDLOCK_PERMISSION_CAPABILITY_USE;
 	attr.allowed_namespace_types = CLONE_NEWUTS;
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 
 	/*
 	 * Unknown namespace bits (e.g. bit 63) are silently accepted for
@@ -160,7 +160,7 @@ TEST(add_rule_bad_attr)
 	attr.quiet_namespace_types = 0;
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&attr, 0));
-	ASSERT_EQ(ENOMSG, errno);
+	EXPECT_EQ(ENOMSG, errno);
 
 	/* Quiet-only rule (empty allowed set) is legal. */
 	attr.permissions = LANDLOCK_PERMISSION_NAMESPACE_USE;
@@ -201,7 +201,7 @@ TEST(add_rule_bad_attr)
 	attr.allowed_namespace_types = CLONE_NEWUTS;
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&attr, LANDLOCK_ADD_RULE_QUIET));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 
 	EXPECT_EQ(0, close(ruleset_fd));
 
@@ -217,7 +217,7 @@ TEST(add_rule_bad_attr)
 	attr.allowed_namespace_types = CLONE_NEWUTS;
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 	EXPECT_EQ(0, close(ruleset_fd));
 }
 
@@ -759,7 +759,7 @@ TEST_F(ns_create, clone3)
 		ASSERT_LE(0, pid);
 		ASSERT_EQ(pid, waitpid(pid, &status, 0));
 		ASSERT_EQ(1, WIFEXITED(status));
-		ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
+		EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 	}
 
 	if (!variant->drop_all_caps &&
@@ -1307,7 +1307,7 @@ TEST_F(ns_create_multi_flag, unshare)
 
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(1, WIFEXITED(status));
-	ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
+	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 }
 
 /* clang-format off */
@@ -1438,7 +1438,7 @@ TEST_F(setns_cross_process, setns)
 	EXPECT_EQ(0, close(pipe_parent[1]));
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(1, WIFEXITED(status));
-	ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
+	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 }
 
 /*
@@ -1660,7 +1660,7 @@ TEST(two_permissions_combined_unshare_allowed)
 	}
 	ASSERT_EQ(child, waitpid(child, &status, 0));
 	ASSERT_EQ(1, WIFEXITED(status));
-	ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
+	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
 }
 
 /*
@@ -2393,10 +2393,10 @@ TEST_F(ns_trace, rule_events)
 	ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, CLONE_NEWUTS, CLONE_NEWNET));
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&invalid_attr, 0));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 	ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE,
 					&valid_attr, LANDLOCK_ADD_RULE_QUIET));
-	ASSERT_EQ(EINVAL, errno);
+	EXPECT_EQ(EINVAL, errno);
 	ASSERT_EQ(0, add_ns_rule_full(ruleset_fd, CLONE_NEWUTS, CLONE_NEWNET));
 	ASSERT_EQ(0, add_ns_rule(ruleset_fd, 1ULL << 63));
 	EXPECT_EQ(0, close(ruleset_fd));
tools/testing/selftests/landlock/scoped_abstract_unix_test.c +4 -4
index 4c7f9c102d50..9aa1d375ef51 100644
@@ -1114,7 +1114,7 @@ TEST(datagram_sockets)
 		ASSERT_EQ(-1, sendto(client_unconn_socket, ".", 1, 0,
 				     &non_connected_addr.unix_addr,
 				     non_connected_addr.unix_addr_len));
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 		ASSERT_EQ(1, write(pipe_child[1], ".", 1));
 
 		EXPECT_EQ(0, close(client_conn_socket));
@@ -1196,17 +1196,17 @@ TEST(self_connect)
 		ASSERT_EQ(-1,
 			  connect(connected_socket, &connected_addr.unix_addr,
 				  connected_addr.unix_addr_len));
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 
 		ASSERT_EQ(-1, sendto(connected_socket, ".", 1, 0,
 				     &connected_addr.unix_addr,
 				     connected_addr.unix_addr_len));
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 
 		ASSERT_EQ(-1, sendto(non_connected_socket, ".", 1, 0,
 				     &non_connected_addr.unix_addr,
 				     non_connected_addr.unix_addr_len));
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 
 		EXPECT_EQ(0, close(connected_socket));
 		EXPECT_EQ(0, close(non_connected_socket));
tools/testing/selftests/landlock/scoped_signal_test.c +3 -3
index 66fec3822812..f9c270a1107a 100644
@@ -124,7 +124,7 @@ TEST_F(scoping_signals, send_sig_to_parent)
 		 */
 		err = kill(parent, variant->sig);
 		ASSERT_EQ(-1, err);
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 
 		/*
 		 * No matter of the domain, a process should be able to
@@ -216,7 +216,7 @@ TEST_F(scoped_domains, check_access_signal)
 			ASSERT_EQ(0, err);
 		} else {
 			ASSERT_EQ(-1, err);
-			ASSERT_EQ(EPERM, errno);
+			EXPECT_EQ(EPERM, errno);
 		}
 		/*
 		 * No matter of the domain, a process should be able to
@@ -241,7 +241,7 @@ TEST_F(scoped_domains, check_access_signal)
 		ASSERT_EQ(0, err);
 	} else {
 		ASSERT_EQ(-1, err);
-		ASSERT_EQ(EPERM, errno);
+		EXPECT_EQ(EPERM, errno);
 	}
 	ASSERT_EQ(0, raise(0));
 
tools/testing/selftests/landlock/scoped_test.c +1 -1
index b90f76ed0d9c..4d8f79170892 100644
@@ -26,7 +26,7 @@ TEST(ruleset_with_unknown_scope)
 
 		ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr,
 						      sizeof(ruleset_attr), 0));
-		ASSERT_EQ(EINVAL, errno);
+		EXPECT_EQ(EINVAL, errno);
 	}
 }
 
Signature · 1 line
2.56.0
#54 lore raw

Re: [PATCH v2 27/27] selftests/landlock: Use EXPECT for checks that later steps do not need

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

ASSERT aborts the test, so it should only be used when continuing after a failure is pointless. Use EXPECT for checks whose outcome nothing later in the test depends on:

* errno values after a failed syscall,
* test_open() and test_open_rel(), which leave no state behind,
* WEXITSTATUS() of a child that has exited,
* matches_log_fs() and matches_log_fs_extra(),
* the record counts returned by audit_count_records().

Quoted 4 lines

Keep ASSERT for operations whose side effects later checks depend on, such as renames, links and file removals.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27