From 70e96fb917a1beea4035dbcf4171cf1c1139df81 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Thu, 25 Nov 2021 23:04:52 +0100 Subject: [PATCH] LibCore: Implement new ptrace_peekbuf wrapper for PT_PEEKBUF syscall --- Userland/Libraries/LibC/sys/ptrace.cpp | 9 +++++++++ Userland/Libraries/LibCore/System.cpp | 16 ++++++++++++++++ Userland/Libraries/LibCore/System.h | 1 + 3 files changed, 26 insertions(+) diff --git a/Userland/Libraries/LibC/sys/ptrace.cpp b/Userland/Libraries/LibC/sys/ptrace.cpp index 61c3e41e126..5b498d6e171 100644 --- a/Userland/Libraries/LibC/sys/ptrace.cpp +++ b/Userland/Libraries/LibC/sys/ptrace.cpp @@ -12,6 +12,15 @@ extern "C" { long ptrace(int request, pid_t tid, void* addr, void* data) { + if (request == PT_PEEKBUF) { + // PT_PEEKBUF cannot easily be correctly used through this function signature: + // The amount of data to be copied is not available. + // We could VERIFY() here, but to safeguard against ports that attempt to use + // the same number, let's claim that the Kernel just doesn't know the command. + // Use Core::System::ptrace_peekbuf instead. + return EINVAL; + } + // PT_PEEK needs special handling since the syscall wrapper // returns the peeked value as an int, which can be negative because of the cast. // When using PT_PEEK, the user can check if an error occurred diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 01cf0a24a75..15d84818862 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,21 @@ ErrorOr recvfd(int sockfd, int options) return Error::from_syscall("recvfd"sv, -errno); return fd; } + +ErrorOr ptrace_peekbuf(pid_t tid, void const* tracee_addr, Bytes destination_buf) +{ + Syscall::SC_ptrace_buf_params buf_params { + { destination_buf.data(), destination_buf.size() } + }; + Syscall::SC_ptrace_params params { + PT_PEEKBUF, + tid, + const_cast(tracee_addr), + (FlatPtr)&buf_params, + }; + int rc = syscall(SC_ptrace, ¶ms); + HANDLE_SYSCALL_RETURN_VALUE("ptrace_peekbuf", rc, {}); +} #endif ErrorOr sigaction(int signal, struct sigaction const* action, struct sigaction* old_action) diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 14b26db2e4e..801c1e5f617 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -19,6 +19,7 @@ ErrorOr unveil(StringView path, StringView permissions); ErrorOr> pipe2(int flags); ErrorOr sendfd(int sockfd, int fd); ErrorOr recvfd(int sockfd, int options); +ErrorOr ptrace_peekbuf(pid_t tid, void const* tracee_addr, Bytes destination_buf); #endif ErrorOr sigaction(int signal, struct sigaction const* action, struct sigaction* old_action);