Skip to content

Commit 6b45664

Browse files
committed
selftests/landlock: Add PID tests for audit records
Add audit.thread tests to check that the PID tied to a domain is not a thread ID but the thread group ID. These new tests would not pass without the previous TGID fix. Extend matches_log_domain_allocated() to check against the PID that created the domain. Test coverage for security/landlock is 93.6% of 1524 lines according to gcc/gcov-14. Cc: Christian Brauner <brauner@kernel.org> Cc: Günther Noack <gnoack@google.com> Cc: Paul Moore <paul@paul-moore.com> Link: https://lore.kernel.org/r/20250410171725.1265860-3-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent e4a0f9e commit 6b45664

File tree

3 files changed

+141
-10
lines changed

3 files changed

+141
-10
lines changed

tools/testing/selftests/landlock/audit.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,22 @@ static int audit_match_record(int audit_fd, const __u16 type,
300300
return err;
301301
}
302302

303-
static int __maybe_unused matches_log_domain_allocated(int audit_fd,
303+
static int __maybe_unused matches_log_domain_allocated(int audit_fd, pid_t pid,
304304
__u64 *domain_id)
305305
{
306-
return audit_match_record(
307-
audit_fd, AUDIT_LANDLOCK_DOMAIN,
308-
REGEX_LANDLOCK_PREFIX
309-
" status=allocated mode=enforcing pid=[0-9]\\+ uid=[0-9]\\+"
310-
" exe=\"[^\"]\\+\" comm=\".*_test\"$",
311-
domain_id);
306+
static const char log_template[] = REGEX_LANDLOCK_PREFIX
307+
" status=allocated mode=enforcing pid=%d uid=[0-9]\\+"
308+
" exe=\"[^\"]\\+\" comm=\".*_test\"$";
309+
char log_match[sizeof(log_template) + 10];
310+
int log_match_len;
311+
312+
log_match_len =
313+
snprintf(log_match, sizeof(log_match), log_template, pid);
314+
if (log_match_len > sizeof(log_match))
315+
return -E2BIG;
316+
317+
return audit_match_record(audit_fd, AUDIT_LANDLOCK_DOMAIN, log_match,
318+
domain_id);
312319
}
313320

314321
static int __maybe_unused matches_log_domain_deallocated(

tools/testing/selftests/landlock/audit_test.c

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <errno.h>
1010
#include <limits.h>
1111
#include <linux/landlock.h>
12+
#include <pthread.h>
1213
#include <stdlib.h>
1314
#include <sys/mount.h>
1415
#include <sys/prctl.h>
@@ -104,7 +105,8 @@ TEST_F(audit, layers)
104105
matches_log_signal(_metadata, self->audit_fd,
105106
getppid(), &denial_dom));
106107
EXPECT_EQ(0, matches_log_domain_allocated(
107-
self->audit_fd, &allocated_dom));
108+
self->audit_fd, getpid(),
109+
&allocated_dom));
108110
EXPECT_NE(denial_dom, 1);
109111
EXPECT_NE(denial_dom, 0);
110112
EXPECT_EQ(denial_dom, allocated_dom);
@@ -156,6 +158,126 @@ TEST_F(audit, layers)
156158
EXPECT_EQ(0, close(ruleset_fd));
157159
}
158160

161+
struct thread_data {
162+
pid_t parent_pid;
163+
int ruleset_fd, pipe_child, pipe_parent;
164+
};
165+
166+
static void *thread_audit_test(void *arg)
167+
{
168+
const struct thread_data *data = (struct thread_data *)arg;
169+
uintptr_t err = 0;
170+
char buffer;
171+
172+
/* TGID and TID are different for a second thread. */
173+
if (getpid() == gettid()) {
174+
err = 1;
175+
goto out;
176+
}
177+
178+
if (landlock_restrict_self(data->ruleset_fd, 0)) {
179+
err = 2;
180+
goto out;
181+
}
182+
183+
if (close(data->ruleset_fd)) {
184+
err = 3;
185+
goto out;
186+
}
187+
188+
/* Creates a denial to get the domain ID. */
189+
if (kill(data->parent_pid, 0) != -1) {
190+
err = 4;
191+
goto out;
192+
}
193+
194+
if (EPERM != errno) {
195+
err = 5;
196+
goto out;
197+
}
198+
199+
/* Signals the parent to read denial logs. */
200+
if (write(data->pipe_child, ".", 1) != 1) {
201+
err = 6;
202+
goto out;
203+
}
204+
205+
/* Waits for the parent to update audit filters. */
206+
if (read(data->pipe_parent, &buffer, 1) != 1) {
207+
err = 7;
208+
goto out;
209+
}
210+
211+
out:
212+
close(data->pipe_child);
213+
close(data->pipe_parent);
214+
return (void *)err;
215+
}
216+
217+
/* Checks that the PID tied to a domain is not a TID but the TGID. */
218+
TEST_F(audit, thread)
219+
{
220+
const struct landlock_ruleset_attr ruleset_attr = {
221+
.scoped = LANDLOCK_SCOPE_SIGNAL,
222+
};
223+
__u64 denial_dom = 1;
224+
__u64 allocated_dom = 2;
225+
__u64 deallocated_dom = 3;
226+
pthread_t thread;
227+
int pipe_child[2], pipe_parent[2];
228+
char buffer;
229+
struct thread_data child_data;
230+
231+
child_data.parent_pid = getppid();
232+
ASSERT_EQ(0, pipe2(pipe_child, O_CLOEXEC));
233+
child_data.pipe_child = pipe_child[1];
234+
ASSERT_EQ(0, pipe2(pipe_parent, O_CLOEXEC));
235+
child_data.pipe_parent = pipe_parent[0];
236+
child_data.ruleset_fd =
237+
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
238+
ASSERT_LE(0, child_data.ruleset_fd);
239+
240+
/* TGID and TID are the same for the initial thread . */
241+
EXPECT_EQ(getpid(), gettid());
242+
EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
243+
ASSERT_EQ(0, pthread_create(&thread, NULL, thread_audit_test,
244+
&child_data));
245+
246+
/* Waits for the child to generate a denial. */
247+
ASSERT_EQ(1, read(pipe_child[0], &buffer, 1));
248+
EXPECT_EQ(0, close(pipe_child[0]));
249+
250+
/* Matches the signal log to get the domain ID. */
251+
EXPECT_EQ(0, matches_log_signal(_metadata, self->audit_fd,
252+
child_data.parent_pid, &denial_dom));
253+
EXPECT_NE(denial_dom, 1);
254+
EXPECT_NE(denial_dom, 0);
255+
256+
EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, getpid(),
257+
&allocated_dom));
258+
EXPECT_EQ(denial_dom, allocated_dom);
259+
260+
/* Updates filter rules to match the drop record. */
261+
set_cap(_metadata, CAP_AUDIT_CONTROL);
262+
EXPECT_EQ(0, audit_filter_drop(self->audit_fd, AUDIT_ADD_RULE));
263+
EXPECT_EQ(0, audit_filter_exe(self->audit_fd, &self->audit_filter,
264+
AUDIT_DEL_RULE));
265+
clear_cap(_metadata, CAP_AUDIT_CONTROL);
266+
267+
/* Signals the thread to exit, which will generate a domain deallocation. */
268+
ASSERT_EQ(1, write(pipe_parent[1], ".", 1));
269+
EXPECT_EQ(0, close(pipe_parent[1]));
270+
ASSERT_EQ(0, pthread_join(thread, NULL));
271+
272+
EXPECT_EQ(0, setsockopt(self->audit_fd, SOL_SOCKET, SO_RCVTIMEO,
273+
&audit_tv_dom_drop, sizeof(audit_tv_dom_drop)));
274+
EXPECT_EQ(0, matches_log_domain_deallocated(self->audit_fd, 1,
275+
&deallocated_dom));
276+
EXPECT_EQ(denial_dom, deallocated_dom);
277+
EXPECT_EQ(0, setsockopt(self->audit_fd, SOL_SOCKET, SO_RCVTIMEO,
278+
&audit_tv_default, sizeof(audit_tv_default)));
279+
}
280+
159281
FIXTURE(audit_flags)
160282
{
161283
struct audit_filter audit_filter;
@@ -270,7 +392,8 @@ TEST_F(audit_flags, signal)
270392

271393
/* Checks domain information records. */
272394
EXPECT_EQ(0, matches_log_domain_allocated(
273-
self->audit_fd, &allocated_dom));
395+
self->audit_fd, getpid(),
396+
&allocated_dom));
274397
EXPECT_NE(*self->domain_id, 1);
275398
EXPECT_NE(*self->domain_id, 0);
276399
EXPECT_EQ(*self->domain_id, allocated_dom);

tools/testing/selftests/landlock/fs_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5964,7 +5964,8 @@ TEST_F(audit_layout1, refer_handled)
59645964
EXPECT_EQ(EXDEV, errno);
59655965
EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer",
59665966
dir_s1d1));
5967-
EXPECT_EQ(0, matches_log_domain_allocated(self->audit_fd, NULL));
5967+
EXPECT_EQ(0,
5968+
matches_log_domain_allocated(self->audit_fd, getpid(), NULL));
59685969
EXPECT_EQ(0, matches_log_fs(_metadata, self->audit_fd, "fs\\.refer",
59695970
dir_s1d3));
59705971

0 commit comments

Comments
 (0)