landlock: Selftest fixes and cleanups
Subsystems (3)
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.
When applying this patch set, please base it on a branch that has Wang Yan's snprintf() patch in it [1], which I also support as a selftest cleanup.
–Günther
[1] https://lore.kernel.org/all/[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 | 190 +-- 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 +- .../testing/selftests/landlock/ptrace_test.c | 44 +- .../selftests/landlock/sandbox-and-launch.c | 6 +- .../landlock/scoped_abstract_unix_test.c | 45 +- .../selftests/landlock/scoped_signal_test.c | 23 +- .../testing/selftests/landlock/scoped_test.c | 2 +- .../selftests/landlock/trace_fs_test.c | 8 +- tools/testing/selftests/landlock/trace_test.c | 26 +- tools/testing/selftests/landlock/tsync_test.c | 22 +- .../selftests/landlock/wait-pipe-sandbox.c | 16 +- tools/testing/selftests/landlock/wait-pipe.c | 2 +- 17 files changed, 952 insertions(+), 909 deletions(-)
Signature · 1 line
2.56.0
PATCH 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
@@ -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
[email protected]
AI reviewRe: [PATCH 01/27] selftests/landlock: Propagate grandchild failures in unix_scoping
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 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.
Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/27 selftests/landlock: Propagate EXPECT failures from forked children
In reply to Günther NoackA 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
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 02/27] selftests/landlock: Propagate EXPECT failures from forked children
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 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.
Quoted 6 lines
Exit with
_metadata->exit_codein 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.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/27 selftests/landlock: Check the thread result in audit.thread
In reply to Günther Noackthread_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
@@ -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
[email protected]
AI reviewRe: [PATCH 03/27] selftests/landlock: Check the thread result in audit.thread
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 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.
Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/27 selftests/landlock: Zero-initialize the buffer in inconsistent_attr
In reply to Günther NoackThis 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
@@ -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
[email protected]
AI reviewRe: [PATCH 04/27] selftests/landlock: Zero-initialize the buffer in inconsistent_attr
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 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.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH 5/27 selftests/landlock: Fix the openat() success check in fs_bench
In reply to Günther Noackfs_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
@@ -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
[email protected]
AI reviewRe: [PATCH 05/27] selftests/landlock: Fix the openat() success check in fs_bench
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
fs_benchexpectsopenat()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.
Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH 6/27 selftests/landlock: Fix the reported clock tick rate in fs_bench
In reply to Günther NoackSince 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
@@ -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
[email protected]
AI reviewRe: [PATCH 06/27] selftests/landlock: Fix the reported clock tick rate in fs_bench
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Since time is measured using times(2), ticks per second are obtained using
sysconf(_SC_CLK_TCK)instead ofCLOCKS_PER_SEC. (c.f. times(2) man page)Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH 7/27 selftests/landlock: Fix the ruleset_fd check in empty_or_same_ruleset
In reply to Günther NoackASSERT_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
@@ -1463,7 +1463,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
[email protected]
AI reviewRe: [PATCH 07/27] selftests/landlock: Fix the ruleset_fd check in empty_or_same_ruleset
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
ASSERT_LE(-1, ruleset_fd)is always true, so the test does not check that creating a ruleset with an emptyhandled_access_fsfails. Check for -1 instead.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH 8/27 selftests/landlock: Handle a missing control message in recv_fd()
In reply to Günther NoackCMSG_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
@@ -158,7 +158,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
[email protected]
AI reviewRe: [PATCH 08/27] selftests/landlock: Handle a missing control message in recv_fd()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 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
-EIOin that case.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH 9/27 selftests/landlock: Assert tracefs_extract_field() results that are used
In reply to Günther NoackSeveral 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 | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
tools/testing/selftests/landlock/trace_test.c +10 -10
@@ -175,19 +175,19 @@ 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 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. */
@@ -357,7 +357,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)));
@@ -365,7 +365,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);
@@ -381,7 +381,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);
@@ -615,7 +615,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);
@@ -900,7 +900,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);
@@ -949,7 +949,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);
@@ -959,7 +959,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
[email protected]
AI reviewRe: [PATCH 09/27] selftests/landlock: Assert tracefs_extract_field() results that are used
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
Several trace tests check
tracefs_extract_field()withEXPECT_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.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH 10/27 selftests/landlock: Assert audit initialization in fixture setups
In reply to Günther NoackThe 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/fs_test.c | 6 +++--- tools/testing/selftests/landlock/net_test.c | 2 +- tools/testing/selftests/landlock/ptrace_test.c | 2 +- .../selftests/landlock/scoped_abstract_unix_test.c | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-)
tools/testing/selftests/landlock/audit_test.c +4 -4
@@ -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/fs_test.c +3 -3
@@ -7508,7 +7508,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); }
@@ -8031,7 +8031,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" ))
@@ -9608,7 +9608,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
@@ -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/ptrace_test.c +1 -1
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 10/27] selftests/landlock: Assert audit initialization in fixture setups
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests: cascading test failures due to skipped teardown on ASSERT_LE abort --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests: cascading test failures due to skipped teardown on ASSERT_LE abort --
commit 924f6036b77ebee776959fad4d5c2f2f2ab050ad
Author: Günther Noack <[email protected]>
selftests/landlock: Assert audit initialization in fixture setups
This commit replaces EXPECT_* with ASSERT_* macros for audit initialization in Landlock tests to prevent tests from proceeding with an invalid audit file descriptor when initialization fails.
tools/testing/selftests/landlock/fs_test.c FIXTURE_SETUP(audit_layout1) · line 7511
Quoted 10 lines
tools/testing/selftests/landlock/fs_test.c +1
-1@@ -7508,7 +7508,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);
[Severity: Medium]
Will this change cause cascading test failures when audit initialization fails?
When ASSERT_LE fails (for example, if auditd is running), it aborts the fixture setup immediately. In kselftest_harness, this causes the child process to exit before clearing _metadata->no_teardown.
In the parent process, the teardown wrapper checks !__atomic_test_and_set(_metadata->no_teardown, __ATOMIC_RELAXED). Because no_teardown remains true, the parent entirely skips FIXTURE_TEARDOWN_PARENT().
Since prepare_layout() created TMP_DIR on the host filesystem prior to the setup failure, skipping the teardown leaks TMP_DIR.
Subsequent tests that use prepare_layout() will call create_directory(..., TMP_DIR), which asserts that mkdir(path, 0700) == 0. The leftover directory causes mkdir to return -1 with EEXIST, triggering another assertion failure.
Does the teardown need to be called even if audit_init_with_exe_filter() fails, or should we retain EXPECT_LE so the setup completes and no_teardown is cleared?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH 11/27 selftests/landlock: Assert helper results that are used afterwards
In reply to Günther NoackSome 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 +- .../testing/selftests/landlock/scoped_abstract_unix_test.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/landlock/common.h +3 -3
@@ -53,7 +53,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,
@@ -89,7 +89,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)) {
@@ -214,7 +214,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
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 11/27] selftests/landlock: Assert helper results that are used afterwards
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
Some helpers check a result with EXPECT_*() and then use it:
cap_get_proc()in_init_caps()and_change_cap(), andlandlock_create_ruleset()indrop_access_rights(),create_domain()andcreate_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.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
PATCH 12/27 selftests/landlock: Assert audit_count_records() before using its result
In reply to Günther Noackaudit_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/fs_test.c | 86 +++++++++---------- tools/testing/selftests/landlock/net_test.c | 22 ++--- .../testing/selftests/landlock/ptrace_test.c | 6 +- .../landlock/scoped_abstract_unix_test.c | 4 +- 5 files changed, 69 insertions(+), 69 deletions(-)
tools/testing/selftests/landlock/audit_test.c +10 -10
@@ -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/fs_test.c +43 -43
344 lines not on this page · raw message
tools/testing/selftests/landlock/net_test.c +11 -11
@@ -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/ptrace_test.c +3 -3
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 12/27] selftests/landlock: Assert audit_count_records() before using its result
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 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.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH 13/27 selftests/landlock: Fix memory leak in audit_init_filter_exe()
In reply to Günther Noackaudit_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
@@ -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
[email protected]
AI reviewRe: [PATCH 13/27] selftests/landlock: Fix memory leak in audit_init_filter_exe()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
audit_init_filter_exe()does not free the buffer returned by realpath(3) when the path is too long.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
PATCH 14/27 selftests/landlock: Make audit_message large enough for any exe filter
In reply to Günther Noackaudit_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
@@ -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
[email protected]
AI reviewRe: [PATCH 14/27] selftests/landlock: Make audit_message large enough for any exe filter
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/landlock: OOB read in regexec due to missing null terminator --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/landlock: OOB read in regexec due to missing null terminator --
commit fcf4b6b95bf6821e426c4d029e2300800d7024cd
Author: Günther Noack <[email protected]>
selftests/landlock: Make audit_message large enough for any exe filter
This commit increases the size of the data array in struct audit_message to accommodate a full audit_rule_data structure and a PATH_MAX string. It also adds a bounds check in audit_filter_exe() to return -E2BIG if the request size exceeds the new message buffer size.
tools/testing/selftests/landlock/audit.h struct audit_message { · line 48
Quoted 13 lines
tools/testing/selftests/landlock/audit.h +5
-1@@ -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 ];
[Severity: Medium]
Could this lead to an out-of-bounds read during a selftest run?
The buffer size was increased here, but there is no explicit space reserved for, or assignment of, a null terminator.
If kauditd sends an audit message equal to or larger than sizeof(*msg) (for example, if the executable path is extremely long and hex-encoded), the recvfrom() call in audit_recv() will truncate it and read exactly that many bytes:
tools/testing/selftests/landlock/audit.h:audit_recv() {
...
err = recvfrom(fd, msg, sizeof(*msg), 0,
(struct sockaddr *)&addr, &addrlen);
...
}Because the raw payload lacks a null terminator and fills the buffer entirely, msg.data will not be null-terminated. Later, regexec() in audit_match_record() assumes msg.data is a valid C string:
tools/testing/selftests/landlock/audit.h:audit_match_record() {
...
ret = regexec(®ex, msg.data, ARRAY_SIZE(matches), matches, 0);
...
}Since regexec() expects a null-terminated string, passing the completely filled buffer could result in an out-of-bounds read on the stack, potentially crashing the test. Should we ensure the received payload is null-terminated, or perhaps reserve an extra byte for it in the buffer?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
PATCH 15/27 selftests/landlock: Close leaked file descriptors
In reply to Günther NoackSeveral 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 ++++ .../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
@@ -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
@@ -333,6 +333,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)
@@ -354,6 +356,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
@@ -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
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 15/27] selftests/landlock: Close leaked file descriptors
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
Several tests do not close file descriptors they open: the
/dev/nullFD inrestrict_self_fdandrestrict_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.
Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
PATCH 16/27 selftests/landlock: Check errno for combined VERSION and ERRATA flags
In reply to Günther NoackThe 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
@@ -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
[email protected]
AI reviewRe: [PATCH 16/27] selftests/landlock: Check errno for combined VERSION and ERRATA flags
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
The errata test checks that combining
LANDLOCK_CREATE_RULESET_VERSIONandLANDLOCK_CREATE_RULESET_ERRATAfails, but not the resulting errno, unlike the other checks in this test.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
PATCH 17/27 selftests/landlock: Fix SIGURG handler setup in scoped_signal_test
In reply to Günther NoackDrop 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.
--- .../selftests/landlock/scoped_signal_test.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/landlock/scoped_signal_test.c +7 -7
@@ -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
[email protected]
AI reviewRe: [PATCH 17/27] selftests/landlock: Fix SIGURG handler setup in scoped_signal_test
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
Drop
SA_SIGINFO, and rename the function tosetup_sigurg_handler()without the parameter.* Drop the
SA_SIGINFOflag: This flag implies that we use the three-argumentsa_sigactionsignal 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.
Quoted 2 lines
Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
PATCH 18/27 selftests/landlock: Fix regex_escape() error handling in matches_log_fs_extra()
In reply to Günther NoackUse 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
@@ -264,11 +264,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;
@@ -282,21 +282,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
@@ -7454,6 +7454,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=\"",
@@ -7471,13 +7472,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
[email protected]
AI reviewRe: [PATCH 18/27] selftests/landlock: Fix regex_escape() error handling in matches_log_fs_extra()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests: errno clobbering by free() in matches_log_fs_extra() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests: errno clobbering by free() in matches_log_fs_extra() --
commit fde0a2f164054d717ba379b4c41a67fff69162e2
Author: Günther Noack <[email protected]> Date: Thu Oct 8 14:25:47 2026 +0000
selftests/landlock: Fix regex_escape() error handling in matches_log_fs_extra()
This commit updates regex_escape() to use a more conventional error handling interface. It now returns a ssize_t containing either the number of bytes in the output, or -1 with errno set on failure, and updates the call site accordingly.
Quoted 4 lines
tools/testing/selftests/landlock/fs_test.c +0
-0
[ ... ]
tools/testing/selftests/landlock/fs_test.c static int matches_log_fs_extra(struct __test_metadata *const _metadata, · line 7479
Quoted 13 lines
tools/testing/selftests/landlock/fs_test.c +4
-4@@ -7471,13 +7472,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;
[Severity: Medium]
Could this free() call clobber errno before it is returned?
If regex_escape() fails due to an insufficient buffer, it returns -1 and sets errno to ENOMEM. The code in matches_log_fs_extra() then unconditionally calls free(absolute_path) before checking escaped_len and reading errno.
Since libc's free() is not strictly guaranteed to preserve errno, and may clobber it during internal operations like unmap, the original ENOMEM could be lost. This might cause the function to return an incorrect error code or 0 (success) instead of -ENOMEM, masking the test failure.
Would it be safer to cache errno into a local variable before calling free(), or move the free() call after the error check?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
PATCH 19/27 selftests/landlock: Fix off-by-one in regex_escape()
In reply to Günther NoackThe 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
@@ -294,7 +294,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
[email protected]
AI reviewRe: [PATCH 19/27] selftests/landlock: Fix off-by-one in regex_escape()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
The final check in
regex_escape()was off by one byte and should require one byte less to put the NUL byte.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
PATCH 20/27 selftests/landlock: Fix the snprintf() error check in matches_log_fs_extra()
In reply to Günther NoackBy 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().
--- tools/testing/selftests/landlock/fs_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/fs_test.c +1 -1
@@ -7453,7 +7453,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
[email protected]
AI reviewRe: [PATCH 20/27] selftests/landlock: Fix the snprintf() error check in matches_log_fs_extra()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
By storing the snprintf result in a
size_t, thechunk_len< 0 check could never be true. Store it in an int instead, matching the return type ofsnprintf().Fixes 316d06b01130 ("selftests/landlock: Add audit tests for filesystem")Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
PATCH 21/27 selftests/landlock: Use _exit() in the named_pipe_ioctl child
In reply to Günther NoackIn 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
@@ -4276,7 +4276,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
[email protected]
AI reviewRe: [PATCH 21/27] selftests/landlock: Use _exit() in the named_pipe_ioctl child
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
In the
named_pipe_ioctltest, use_exit(2)like in other places.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
PATCH 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 "/".
--- .../selftests/landlock/sandbox-and-launch.c | 6 +++--- .../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
@@ -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
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 22/27] selftests/landlock: Fix messages and comments in helper programs
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 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 "/".Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
PATCH 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
@@ -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
@@ -34,7 +34,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
[email protected]
AI reviewRe: [PATCH 23/27] selftests/landlock: Fix stale comments in common.h and audit.h
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 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 redundantdomain_idcheck inaudit_match_record().Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
PATCH 24/27 selftests/landlock: Use ASSERT for syscall failures before errno checks
In reply to Günther NoackIn 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/fs_test.c | 50 +++++++++---------- tools/testing/selftests/landlock/net_test.c | 24 ++++----- .../testing/selftests/landlock/ptrace_test.c | 14 +++--- .../landlock/scoped_abstract_unix_test.c | 14 +++--- 6 files changed, 75 insertions(+), 75 deletions(-)
tools/testing/selftests/landlock/audit_test.c +4 -4
@@ -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
@@ -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 |
@@ -333,7 +333,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));
@@ -350,12 +350,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);
@@ -368,40 +368,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 ));
@@ -409,18 +409,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);
@@ -446,7 +446,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 ));
@@ -577,7 +577,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 */
@@ -591,7 +591,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);/*
@@ -610,7 +610,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);
@@ -622,7 +622,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/fs_test.c +25 -25
195 lines not on this page · raw message
tools/testing/selftests/landlock/net_test.c +12 -12
@@ -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/ptrace_test.c +7 -7
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 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: ASSERT_EQ bypasses ptrace cleanup, causing test hang --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests: ASSERT_EQ bypasses ptrace cleanup, causing test hang --
commit f35a2df1559c7e7b062080b5d4fda3a0df26e0e3
Author: Günther Noack <[email protected]>
selftests/landlock: Use ASSERT for syscall failures before errno checks
This commit replaces EXPECT* with ASSERT* for syscall failures where succeeding would result in an undefined errno. This ensures that the tests abort early rather than checking undefined state.
tools/testing/selftests/landlock/ptrace_test.c TEST_F(scoped_domains, trace) · line 207
Quoted 10 lines
tools/testing/selftests/landlock/ptrace_test.c +1
-1@@ -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);
[Severity: Medium]
Will this new ASSERT_EQ bypass the ptrace cleanup on unexpected test failures?
If ptrace(PTRACE_ATTACH) unexpectedly succeeds when it is expected to fail, it sends a SIGSTOP to the tracee.
With the change from EXPECT_EQ(-1, ret) to ASSERT_EQ(-1, ret), an unexpected success where ret == 0 causes the test process to abort immediately.
Does this early abort bypass the subsequent if (ret == 0) block that performs waitpid() and ptrace(PTRACE_DETACH) to clean up the stopped tracee?
Because the tracer dies without detaching or waking the tracee, the tracee remains in a stopped state (SIGSTOP).
If the tracee is the parent test process, could the kselftest harness's waitpid() block indefinitely, causing the entire test suite to hang instead of cleanly reporting a failure?
Quoted 7 lines
tools/testing/selftests/landlock/ptrace_test.c +0
-0EXPECT_EQ (EPERM , errno); }if (ret ==0 ) {tools/testing/selftests/landlock/ptrace_test.c +0
-0ASSERT_EQ (parent,waitpid (parent, &status,0 ));ASSERT_EQ (1 ,WIFSTOPPED (status));ASSERT_EQ (0 ,ptrace (PTRACE_DETACH , parent,NULL ,0 )); }
[ ... ]
tools/testing/selftests/landlock/ptrace_test.c TEST_F(scoped_domains, trace) · line 279
Quoted 6 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);
[Severity: Medium]
Similarly, if the parent's PTRACE_ATTACH on the child unexpectedly succeeds, would the parent abort and leave the child stopped and orphaned?
Quoted 8 lines
tools/testing/selftests/landlock/ptrace_test.c +0
-0EXPECT_EQ (EPERM , errno); }tools/testing/selftests/landlock/ptrace_test.c +0
-0if (ret ==0 ) {ASSERT_EQ (child,waitpid (child, &status,0 ));ASSERT_EQ (1 ,WIFSTOPPED (status));ASSERT_EQ (0 ,ptrace (PTRACE_DETACH , child,NULL ,0 )); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
PATCH 25/27 selftests/landlock: Assert WIFEXITED() before checking the exit status
In reply to Günther NoackWEXITSTATUS() 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
@@ -618,7 +618,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
@@ -4644,7 +4644,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. */
@@ -4743,7 +4743,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)); }
@@ -5413,7 +5413,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))
@@ -5435,7 +5435,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));/*
@@ -7988,7 +7988,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
@@ -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
[email protected]
AI reviewRe: [PATCH 25/27] selftests/landlock: Assert WIFEXITED() before checking the exit status
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
WEXITSTATUS()is only meaningful ifWIFEXITED()was true (see wait(2)). CheckWIFEXITED()with ASSERT, as most tests already do, so that a signaled child does not also produce a misleading exit status failure.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
PATCH 26/27 selftests/landlock: Use EXPECT for closing FDs and stopping threads
In reply to Günther NoackClosing 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 +++++++++--------- .../testing/selftests/landlock/ptrace_test.c | 16 +-- .../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
@@ -184,7 +184,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. */
@@ -230,8 +230,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. */
@@ -253,7 +253,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);
@@ -288,7 +288,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)
@@ -309,7 +309,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++)
@@ -323,7 +323,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)
@@ -477,7 +477,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. */
@@ -506,14 +506,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);
@@ -521,32 +521,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));
@@ -651,9 +651,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);
@@ -681,7 +681,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
@@ -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
@@ -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
@@ -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);
@@ -508,7 +508,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);
@@ -544,7 +544,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
@@ -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
[email protected]
AI reviewRe: [PATCH 26/27] selftests/landlock: Use EXPECT for closing FDs and stopping threads
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
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.
Quoted 5 lines
The close(0) in
ioctl_error()stays an ASSERT, as the following IOCTL relies on FD 0 being closed.Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
PATCH 27/27 selftests/landlock: Use EXPECT for checks that later steps do not need
In reply to Günther NoackASSERT 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 | 102 +- tools/testing/selftests/landlock/fs_test.c | 1024 ++++++++--------- tools/testing/selftests/landlock/net_test.c | 2 +- .../landlock/scoped_abstract_unix_test.c | 8 +- .../selftests/landlock/scoped_signal_test.c | 6 +- .../testing/selftests/landlock/scoped_test.c | 2 +- 7 files changed, 577 insertions(+), 577 deletions(-)
tools/testing/selftests/landlock/audit_test.c +5 -5
@@ -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 +51 -51
298 lines not on this page · raw message
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
@@ -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/scoped_abstract_unix_test.c +4 -4
@@ -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
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH 27/27] selftests/landlock: Use EXPECT for checks that later steps do not need
Sashiko has reviewed this patch and found no issues. It looks great!
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()andtest_open_rel(), which leave no state behind,
*WEXITSTATUS()of a child that has exited,
*matches_log_fs()andmatches_log_fs_extra(),
* the record counts returned byaudit_count_records().
Quoted 5 lines
Keep ASSERT for operations whose side effects later checks depend on, such as renames, links and file removals.
Assisted-by LLMSigned-off-by Günther Noack <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27
Quoted 5 lines · Günther Noack
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.
Minor post scriptum:
That's the last four patches. They are large, but largely mechanical refactorings. If it merge-conflicts with another selftest refactoring, it should be possible to replicate equivalent patches relatively easily with the help of an LLM. If you prefer to go that way for the last patches, that would also be fine with me.
–Günther