Tests: Add a test verifying that ptrace self-attaching fails

This commit is contained in:
implicitfield
2026-01-23 19:20:31 +02:00
committed by Sönke Holz
parent 6115bd476d
commit ef2fe6e73e
2 changed files with 39 additions and 0 deletions

View File

@@ -50,6 +50,7 @@ set(LIBTEST_BASED_SOURCES
TestPosixFallocate.cpp
TestPosixSpawn.cpp
TestPrivateInodeVMObject.cpp
TestPtrace.cpp
TestKernelAlarm.cpp
TestKernelFilePermissions.cpp
TestKernelPledge.cpp

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2026, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Atomic.h>
#include <LibTest/TestCase.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <unistd.h>
static Atomic<bool> s_exit_thread = false;
static void* thread_entry(void*)
{
while (!s_exit_thread)
continue;
return nullptr;
}
TEST_CASE(ptrace_self_attach_fail)
{
pthread_t thread = 0;
VERIFY(pthread_create(&thread, nullptr, thread_entry, nullptr) == 0);
VERIFY(thread > 0);
int ptrace_return = ptrace(PT_ATTACH, thread, 0, 0);
int error = errno;
EXPECT_EQ(ptrace_return, -1);
EXPECT_EQ(error, EPERM);
s_exit_thread = true;
VERIFY(pthread_join(thread, nullptr) == 0);
}