Skip to content

Commit 723871a

Browse files
committed
selftests/landlock: Split signal_scoping_threads tests
JIRA: https://issues.redhat.com/browse/RHEL-125143 Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git commit bbe7227 Author: Mickaël Salaün <mic@digikod.net> Date: Tue Mar 18 17:14:41 2025 +0100 selftests/landlock: Split signal_scoping_threads tests Split signal_scoping_threads tests into signal_scoping_thread_before and signal_scoping_thread_after. Use local variables for thread synchronization. Fix exported function. Replace some asserts with expects. Fixes: c899496 ("selftests/landlock: Test signal scoping for threads") Cc: Günther Noack <gnoack@google.com> Cc: Tahera Fahimi <fahimitahera@gmail.com> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20250318161443.279194-7-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net> Signed-off-by: Štěpán Horáček <shoracek@redhat.com>
1 parent e2790fe commit 723871a

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

tools/testing/selftests/landlock/scoped_signal_test.c

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,47 +249,66 @@ TEST_F(scoped_domains, check_access_signal)
249249
_metadata->exit_code = KSFT_FAIL;
250250
}
251251

252-
static int thread_pipe[2];
253-
254252
enum thread_return {
255253
THREAD_INVALID = 0,
256254
THREAD_SUCCESS = 1,
257255
THREAD_ERROR = 2,
258256
};
259257

260-
void *thread_func(void *arg)
258+
static void *thread_sync(void *arg)
261259
{
260+
const int pipe_read = *(int *)arg;
262261
char buf;
263262

264-
if (read(thread_pipe[0], &buf, 1) != 1)
263+
if (read(pipe_read, &buf, 1) != 1)
265264
return (void *)THREAD_ERROR;
266265

267266
return (void *)THREAD_SUCCESS;
268267
}
269268

270-
TEST(signal_scoping_threads)
269+
TEST(signal_scoping_thread_before)
271270
{
272-
pthread_t no_sandbox_thread, scoped_thread;
271+
pthread_t no_sandbox_thread;
273272
enum thread_return ret = THREAD_INVALID;
273+
int thread_pipe[2];
274274

275275
drop_caps(_metadata);
276276
ASSERT_EQ(0, pipe2(thread_pipe, O_CLOEXEC));
277277

278-
ASSERT_EQ(0,
279-
pthread_create(&no_sandbox_thread, NULL, thread_func, NULL));
278+
ASSERT_EQ(0, pthread_create(&no_sandbox_thread, NULL, thread_sync,
279+
&thread_pipe[0]));
280280

281-
/* Restricts the domain after creating the first thread. */
281+
/* Enforces restriction after creating the thread. */
282282
create_scoped_domain(_metadata, LANDLOCK_SCOPE_SIGNAL);
283283

284-
ASSERT_EQ(0, pthread_kill(no_sandbox_thread, 0));
285-
ASSERT_EQ(1, write(thread_pipe[1], ".", 1));
286-
287-
ASSERT_EQ(0, pthread_create(&scoped_thread, NULL, thread_func, NULL));
288-
ASSERT_EQ(0, pthread_kill(scoped_thread, 0));
289-
ASSERT_EQ(1, write(thread_pipe[1], ".", 1));
284+
EXPECT_EQ(0, pthread_kill(no_sandbox_thread, 0));
285+
EXPECT_EQ(1, write(thread_pipe[1], ".", 1));
290286

291287
EXPECT_EQ(0, pthread_join(no_sandbox_thread, (void **)&ret));
292288
EXPECT_EQ(THREAD_SUCCESS, ret);
289+
290+
EXPECT_EQ(0, close(thread_pipe[0]));
291+
EXPECT_EQ(0, close(thread_pipe[1]));
292+
}
293+
294+
TEST(signal_scoping_thread_after)
295+
{
296+
pthread_t scoped_thread;
297+
enum thread_return ret = THREAD_INVALID;
298+
int thread_pipe[2];
299+
300+
drop_caps(_metadata);
301+
ASSERT_EQ(0, pipe2(thread_pipe, O_CLOEXEC));
302+
303+
/* Enforces restriction before creating the thread. */
304+
create_scoped_domain(_metadata, LANDLOCK_SCOPE_SIGNAL);
305+
306+
ASSERT_EQ(0, pthread_create(&scoped_thread, NULL, thread_sync,
307+
&thread_pipe[0]));
308+
309+
EXPECT_EQ(0, pthread_kill(scoped_thread, 0));
310+
EXPECT_EQ(1, write(thread_pipe[1], ".", 1));
311+
293312
EXPECT_EQ(0, pthread_join(scoped_thread, (void **)&ret));
294313
EXPECT_EQ(THREAD_SUCCESS, ret);
295314

0 commit comments

Comments
 (0)