فهرست منبع

LibCore: Implement new ptrace_peekbuf wrapper for PT_PEEKBUF syscall

Ben Wiederhake 3 سال پیش
والد
کامیت
70e96fb917
3فایلهای تغییر یافته به همراه26 افزوده شده و 0 حذف شده
  1. 9 0
      Userland/Libraries/LibC/sys/ptrace.cpp
  2. 16 0
      Userland/Libraries/LibCore/System.cpp
  3. 1 0
      Userland/Libraries/LibCore/System.h

+ 9 - 0
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

+ 16 - 0
Userland/Libraries/LibCore/System.cpp

@@ -12,6 +12,7 @@
 #include <stdarg.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
+#include <sys/ptrace.h>
 #include <sys/socket.h>
 #include <termios.h>
 #include <unistd.h>
@@ -67,6 +68,21 @@ ErrorOr<int> recvfd(int sockfd, int options)
         return Error::from_syscall("recvfd"sv, -errno);
     return fd;
 }
+
+ErrorOr<void> 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<void*>(tracee_addr),
+        (FlatPtr)&buf_params,
+    };
+    int rc = syscall(SC_ptrace, &params);
+    HANDLE_SYSCALL_RETURN_VALUE("ptrace_peekbuf", rc, {});
+}
 #endif
 
 ErrorOr<void> sigaction(int signal, struct sigaction const* action, struct sigaction* old_action)

+ 1 - 0
Userland/Libraries/LibCore/System.h

@@ -19,6 +19,7 @@ ErrorOr<void> unveil(StringView path, StringView permissions);
 ErrorOr<Array<int, 2>> pipe2(int flags);
 ErrorOr<void> sendfd(int sockfd, int fd);
 ErrorOr<int> recvfd(int sockfd, int options);
+ErrorOr<void> ptrace_peekbuf(pid_t tid, void const* tracee_addr, Bytes destination_buf);
 #endif
 
 ErrorOr<void> sigaction(int signal, struct sigaction const* action, struct sigaction* old_action);