Skip to content

Commit a496488

Browse files
l0kodgregkh
authored andcommitted
selftests/landlock: Drain stale audit records on init
commit 3647a49 upstream. Non-audit Landlock tests generate audit records as side effects when audit_enabled is non-zero (e.g. from boot configuration). These records accumulate in the kernel audit backlog while no audit daemon socket is open. When the next test opens a new netlink socket and registers as the audit daemon, the stale backlog is delivered, causing baseline record count checks to fail spuriously. Fix this by draining all pending records in audit_init() right after setting the receive timeout. The 1-usec SO_RCVTIMEO causes audit_recv() to return -EAGAIN once the backlog is empty, naturally terminating the drain loop. Domain deallocation records are emitted asynchronously from a work queue, so they may still arrive after the drain. Remove records.domain == 0 checks that are not preceded by audit_match_record() calls, which would otherwise consume stale records before the count. Document this constraint above audit_count_records(). Increasing the drain timeout to catch in-flight deallocation records was considered but rejected: a longer timeout adds latency to every audit_init() call even when no stale record is pending, and any fixed timeout is still not guaranteed to catch all records under load. Removing the unprotected checks is simpler and avoids the spurious failures. Cc: Günther Noack <gnoack@google.com> Cc: stable@vger.kernel.org Fixes: 6a500b2 ("selftests/landlock: Add tests for audit flags and domain IDs") Reviewed-by: Günther Noack <gnoack3000@gmail.com> Link: https://lore.kernel.org/r/20260402192608.1458252-4-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 2fcde49 commit a496488

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

tools/testing/selftests/landlock/audit.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@ struct audit_records {
338338
size_t domain;
339339
};
340340

341+
/*
342+
* WARNING: Do not assert records.domain == 0 without a preceding
343+
* audit_match_record() call. Domain deallocation records are emitted
344+
* asynchronously from kworker threads and can arrive after the drain in
345+
* audit_init(), corrupting the domain count. A preceding audit_match_record()
346+
* call consumes stale records while scanning, making the assertion safe in
347+
* practice because stale deallocation records arrive before the expected access
348+
* records.
349+
*/
341350
static int audit_count_records(int audit_fd, struct audit_records *records)
342351
{
343352
struct audit_message msg;
@@ -391,6 +400,16 @@ static int audit_init(void)
391400
if (err)
392401
return -errno;
393402

403+
/*
404+
* Drains stale audit records that accumulated in the kernel backlog
405+
* while no audit daemon socket was open. This happens when non-audit
406+
* Landlock tests generate records while audit_enabled is non-zero (e.g.
407+
* from boot configuration), or when domain deallocation records arrive
408+
* asynchronously after a previous test's socket was closed.
409+
*/
410+
while (audit_recv(fd, NULL) == 0)
411+
;
412+
394413
return fd;
395414
}
396415

tools/testing/selftests/landlock/audit_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ TEST_F(audit_flags, signal)
500500
} else {
501501
EXPECT_EQ(1, records.access);
502502
}
503-
EXPECT_EQ(0, records.domain);
504503

505504
/* Updates filter rules to match the drop record. */
506505
set_cap(_metadata, CAP_AUDIT_CONTROL);
@@ -689,7 +688,6 @@ TEST_F(audit_exec, signal_and_open)
689688
/* Tests that there was no denial until now. */
690689
EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
691690
EXPECT_EQ(0, records.access);
692-
EXPECT_EQ(0, records.domain);
693691

694692
/*
695693
* Wait for the child to do a first denied action by layer1 and

tools/testing/selftests/landlock/ptrace_test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ TEST_F(audit, trace)
486486
/* Makes sure there is no superfluous logged records. */
487487
EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
488488
EXPECT_EQ(0, records.access);
489-
EXPECT_EQ(0, records.domain);
490489

491490
yama_ptrace_scope = get_yama_ptrace_scope();
492491
ASSERT_LE(0, yama_ptrace_scope);

tools/testing/selftests/landlock/scoped_abstract_unix_test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ TEST_F(scoped_audit, connect_to_child)
312312
/* Makes sure there is no superfluous logged records. */
313313
EXPECT_EQ(0, audit_count_records(self->audit_fd, &records));
314314
EXPECT_EQ(0, records.access);
315-
EXPECT_EQ(0, records.domain);
316315

317316
ASSERT_EQ(0, pipe2(pipe_child, O_CLOEXEC));
318317
ASSERT_EQ(0, pipe2(pipe_parent, O_CLOEXEC));

0 commit comments

Comments
 (0)