Skip to content

Commit d55d6db

Browse files
committed
Tests: Add a test verifying that ptrace self-attaching fails
1 parent 13efcb5 commit d55d6db

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Tests/Kernel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ set(LIBTEST_BASED_SOURCES
5050
TestPosixFallocate.cpp
5151
TestPosixSpawn.cpp
5252
TestPrivateInodeVMObject.cpp
53+
TestPtrace.cpp
5354
TestKernelAlarm.cpp
5455
TestKernelFilePermissions.cpp
5556
TestKernelPledge.cpp

Tests/Kernel/TestPtrace.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2026, the SerenityOS developers.
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <AK/Atomic.h>
8+
#include <LibTest/TestCase.h>
9+
#include <errno.h>
10+
#include <pthread.h>
11+
#include <signal.h>
12+
#include <sys/ptrace.h>
13+
#include <unistd.h>
14+
15+
static Atomic<bool> s_exit_thread = false;
16+
17+
static void* thread_entry(void*)
18+
{
19+
while (!s_exit_thread)
20+
continue;
21+
22+
return nullptr;
23+
}
24+
25+
TEST_CASE(ptrace_self_attach_fail)
26+
{
27+
pthread_t thread = 0;
28+
VERIFY(pthread_create(&thread, nullptr, thread_entry, nullptr) == 0);
29+
VERIFY(thread > 0);
30+
31+
int ptrace_return = ptrace(PT_ATTACH, thread, 0, 0);
32+
int error = errno;
33+
EXPECT_EQ(ptrace_return, -1);
34+
EXPECT_EQ(error, EPERM);
35+
36+
s_exit_thread = true;
37+
VERIFY(pthread_join(thread, nullptr) == 0);
38+
}

0 commit comments

Comments
 (0)