mirror of
https://github.com/SerenityOS/serenity
synced 2026-04-25 17:15:42 +02:00
Tests: Add a test verifying that ptrace self-attaching fails
This commit is contained in:
committed by
Sönke Holz
parent
6115bd476d
commit
ef2fe6e73e
@@ -50,6 +50,7 @@ set(LIBTEST_BASED_SOURCES
|
||||
TestPosixFallocate.cpp
|
||||
TestPosixSpawn.cpp
|
||||
TestPrivateInodeVMObject.cpp
|
||||
TestPtrace.cpp
|
||||
TestKernelAlarm.cpp
|
||||
TestKernelFilePermissions.cpp
|
||||
TestKernelPledge.cpp
|
||||
|
||||
38
Tests/Kernel/TestPtrace.cpp
Normal file
38
Tests/Kernel/TestPtrace.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user