Skip to content

Commit c899496

Browse files
tahifahimil0kod
authored andcommitted
selftests/landlock: Test signal scoping for threads
Expand the signal scoping tests with pthread_kill(3). Test if a scoped thread can send signal to a process in the same scoped domain, or a non-sandboxed thread. Signed-off-by: Tahera Fahimi <[email protected]> Link: https://lore.kernel.org/r/c15e9eafbb2da1210e46ba8db7b8907f5ea11009.1725657728.git.fahimitahera@gmail.com [mic: Improve commit message] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent ea29236 commit c899496

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tools/testing/selftests/landlock/scoped_signal_test.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <errno.h>
1010
#include <fcntl.h>
1111
#include <linux/landlock.h>
12+
#include <pthread.h>
1213
#include <signal.h>
1314
#include <sys/prctl.h>
1415
#include <sys/types.h>
@@ -248,4 +249,52 @@ TEST_F(scoped_domains, check_access_signal)
248249
_metadata->exit_code = KSFT_FAIL;
249250
}
250251

252+
static int thread_pipe[2];
253+
254+
enum thread_return {
255+
THREAD_INVALID = 0,
256+
THREAD_SUCCESS = 1,
257+
THREAD_ERROR = 2,
258+
};
259+
260+
void *thread_func(void *arg)
261+
{
262+
char buf;
263+
264+
if (read(thread_pipe[0], &buf, 1) != 1)
265+
return (void *)THREAD_ERROR;
266+
267+
return (void *)THREAD_SUCCESS;
268+
}
269+
270+
TEST(signal_scoping_threads)
271+
{
272+
pthread_t no_sandbox_thread, scoped_thread;
273+
enum thread_return ret = THREAD_INVALID;
274+
275+
drop_caps(_metadata);
276+
ASSERT_EQ(0, pipe2(thread_pipe, O_CLOEXEC));
277+
278+
ASSERT_EQ(0,
279+
pthread_create(&no_sandbox_thread, NULL, thread_func, NULL));
280+
281+
/* Restricts the domain after creating the first thread. */
282+
create_scoped_domain(_metadata, LANDLOCK_SCOPE_SIGNAL);
283+
284+
ASSERT_EQ(EPERM, 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));
290+
291+
EXPECT_EQ(0, pthread_join(no_sandbox_thread, (void **)&ret));
292+
EXPECT_EQ(THREAD_SUCCESS, ret);
293+
EXPECT_EQ(0, pthread_join(scoped_thread, (void **)&ret));
294+
EXPECT_EQ(THREAD_SUCCESS, ret);
295+
296+
EXPECT_EQ(0, close(thread_pipe[0]));
297+
EXPECT_EQ(0, close(thread_pipe[1]));
298+
}
299+
251300
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)