Blame SOURCES/valgrind-3.15.0-pkey.patch

058efa
commit b064131bdf099d3647b4501e5d15391e1e9623e6
058efa
Author: Mark Wielaard <mark@klomp.org>
058efa
Date:   Thu May 30 00:29:58 2019 +0200
058efa
058efa
    linux x86 and amd64 memory protection key syscalls.
058efa
    
058efa
    This implements minimal support for the pkey_alloc, pkey_free and
058efa
    pkey_mprotect syscalls. pkey_alloc will simply indicate that pkeys
058efa
    are not supported. pkey_free always fails. pkey_mprotect works just
058efa
    like mprotect if the special pkey -1 is provided.
058efa
    
058efa
    https://bugs.kde.org/show_bug.cgi?id=408091
058efa
058efa
diff --git a/coregrind/m_syswrap/priv_syswrap-generic.h b/coregrind/m_syswrap/priv_syswrap-generic.h
058efa
index 88530f0..3e1c8b6 100644
058efa
--- a/coregrind/m_syswrap/priv_syswrap-generic.h
058efa
+++ b/coregrind/m_syswrap/priv_syswrap-generic.h
058efa
@@ -106,6 +106,10 @@ extern Bool
058efa
 ML_(handle_auxv_open)(SyscallStatus *status, const HChar *filename,
058efa
                       int flags);
058efa
 
058efa
+/* Helper function for generic mprotect and linux pkey_mprotect. */
058efa
+extern void handle_sys_mprotect (ThreadId tid, SyscallStatus *status,
058efa
+                                 Addr *addr, SizeT *len, Int *prot);
058efa
+
058efa
 DECL_TEMPLATE(generic, sys_ni_syscall);            // * P -- unimplemented
058efa
 DECL_TEMPLATE(generic, sys_exit);
058efa
 DECL_TEMPLATE(generic, sys_fork);
058efa
diff --git a/coregrind/m_syswrap/priv_syswrap-linux.h b/coregrind/m_syswrap/priv_syswrap-linux.h
058efa
index 5cf5407..2471524 100644
058efa
--- a/coregrind/m_syswrap/priv_syswrap-linux.h
058efa
+++ b/coregrind/m_syswrap/priv_syswrap-linux.h
058efa
@@ -299,6 +299,11 @@ DECL_TEMPLATE(linux, sys_bpf);
058efa
 // Linux-specific (new in Linux 4.11)
058efa
 DECL_TEMPLATE(linux, sys_statx);
058efa
 
058efa
+// Linux-specific memory protection key syscalls (since Linux 4.9)
058efa
+DECL_TEMPLATE(linux, sys_pkey_alloc);
058efa
+DECL_TEMPLATE(linux, sys_pkey_free);
058efa
+DECL_TEMPLATE(linux, sys_pkey_mprotect);
058efa
+
058efa
 /* ---------------------------------------------------------------------
058efa
    Wrappers for sockets and ipc-ery.  These are split into standalone
058efa
    procedures because x86-linux hides them inside multiplexors
058efa
diff --git a/coregrind/m_syswrap/syswrap-amd64-linux.c b/coregrind/m_syswrap/syswrap-amd64-linux.c
058efa
index d4fe413..2d6b95f 100644
058efa
--- a/coregrind/m_syswrap/syswrap-amd64-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-amd64-linux.c
058efa
@@ -863,6 +863,10 @@ static SyscallTableEntry syscall_table[] = {
058efa
    LINX_(__NR_membarrier,        sys_membarrier),        // 324
058efa
 
058efa
    LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 326
058efa
+
058efa
+   LINXY(__NR_pkey_mprotect,     sys_pkey_mprotect),     // 329
058efa
+   LINX_(__NR_pkey_alloc,        sys_pkey_alloc),        // 330
058efa
+   LINX_(__NR_pkey_free,         sys_pkey_free),         // 331
058efa
 };
058efa
 
058efa
 SyscallTableEntry* ML_(get_linux_syscall_entry) ( UInt sysno )
058efa
diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c
058efa
index 0b64919..01191f6 100644
058efa
--- a/coregrind/m_syswrap/syswrap-generic.c
058efa
+++ b/coregrind/m_syswrap/syswrap-generic.c
058efa
@@ -3842,12 +3842,28 @@ PRE(sys_mprotect)
058efa
    PRE_REG_READ3(long, "mprotect",
058efa
                  unsigned long, addr, vki_size_t, len, unsigned long, prot);
058efa
 
058efa
-   if (!ML_(valid_client_addr)(ARG1, ARG2, tid, "mprotect")) {
058efa
+   Addr addr = ARG1;
058efa
+   SizeT len = ARG2;
058efa
+   Int prot  = ARG3;
058efa
+
058efa
+   handle_sys_mprotect (tid, status, &addr, &len, &prot;;
058efa
+
058efa
+   ARG1 = addr;
058efa
+   ARG2 = len;
058efa
+   ARG3 = prot;
058efa
+}
058efa
+/* This will be called from the generic mprotect, or the linux specific
058efa
+   pkey_mprotect. Pass pointers to ARG1, ARG2 and ARG3 as addr, len and prot,
058efa
+   they might be adjusted and have to assigned back to ARG1, ARG2 and ARG3.  */
058efa
+void handle_sys_mprotect(ThreadId tid, SyscallStatus* status,
058efa
+                         Addr *addr, SizeT *len, Int *prot)
058efa
+{
058efa
+   if (!ML_(valid_client_addr)(*addr, *len, tid, "mprotect")) {
058efa
       SET_STATUS_Failure( VKI_ENOMEM );
058efa
    } 
058efa
 #if defined(VKI_PROT_GROWSDOWN)
058efa
    else 
058efa
-   if (ARG3 & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP)) {
058efa
+   if (*prot & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP)) {
058efa
       /* Deal with mprotects on growable stack areas.
058efa
 
058efa
          The critical files to understand all this are mm/mprotect.c
058efa
@@ -3862,8 +3878,8 @@ PRE(sys_mprotect)
058efa
 
058efa
          The sanity check provided by the kernel is that the vma must
058efa
          have the VM_GROWSDOWN/VM_GROWSUP flag set as appropriate.  */
058efa
-      UInt grows = ARG3 & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP);
058efa
-      NSegment const *aseg = VG_(am_find_nsegment)(ARG1);
058efa
+      UInt grows = *prot & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP);
058efa
+      NSegment const *aseg = VG_(am_find_nsegment)(*addr);
058efa
       NSegment const *rseg;
058efa
 
058efa
       vg_assert(aseg);
058efa
@@ -3874,10 +3890,10 @@ PRE(sys_mprotect)
058efa
              && rseg->kind == SkResvn
058efa
              && rseg->smode == SmUpper
058efa
              && rseg->end+1 == aseg->start) {
058efa
-            Addr end = ARG1 + ARG2;
058efa
-            ARG1 = aseg->start;
058efa
-            ARG2 = end - aseg->start;
058efa
-            ARG3 &= ~VKI_PROT_GROWSDOWN;
058efa
+            Addr end = *addr + *len;
058efa
+            *addr = aseg->start;
058efa
+            *len = end - aseg->start;
058efa
+            *prot &= ~VKI_PROT_GROWSDOWN;
058efa
          } else {
058efa
             SET_STATUS_Failure( VKI_EINVAL );
058efa
          }
058efa
@@ -3887,8 +3903,8 @@ PRE(sys_mprotect)
058efa
              && rseg->kind == SkResvn
058efa
              && rseg->smode == SmLower
058efa
              && aseg->end+1 == rseg->start) {
058efa
-            ARG2 = aseg->end - ARG1 + 1;
058efa
-            ARG3 &= ~VKI_PROT_GROWSUP;
058efa
+            *len = aseg->end - *addr + 1;
058efa
+            *prot &= ~VKI_PROT_GROWSUP;
058efa
          } else {
058efa
             SET_STATUS_Failure( VKI_EINVAL );
058efa
          }
058efa
diff --git a/coregrind/m_syswrap/syswrap-linux.c b/coregrind/m_syswrap/syswrap-linux.c
058efa
index 810ca24..5452b8d 100644
058efa
--- a/coregrind/m_syswrap/syswrap-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-linux.c
058efa
@@ -12120,6 +12120,76 @@ PRE(sys_copy_file_range)
058efa
   }
058efa
 }
058efa
 
058efa
+PRE(sys_pkey_alloc)
058efa
+{
058efa
+  PRINT("pkey_alloc (%lu, %lu)", ARG1, ARG2);
058efa
+
058efa
+  PRE_REG_READ2(long, "pkey_alloc",
058efa
+                unsigned long, "flags",
058efa
+                unsigned long, "access_rights");
058efa
+
058efa
+  /* The kernel says: pkey_alloc() is always safe to call regardless of
058efa
+     whether or not the operating system supports protection keys.  It can be
058efa
+     used in lieu of any other mechanism for detecting pkey support and will
058efa
+     simply fail with the error ENOSPC if the operating system has no pkey
058efa
+     support.
058efa
+
058efa
+     So we simply always return ENOSPC to signal memory protection keys are
058efa
+     not supported under valgrind, unless there are unknown flags, then we
058efa
+     return EINVAL. */
058efa
+  unsigned long pkey_flags = ARG1;
058efa
+  if (pkey_flags != 0)
058efa
+     SET_STATUS_Failure( VKI_EINVAL );
058efa
+  else
058efa
+     SET_STATUS_Failure( VKI_ENOSPC );
058efa
+}
058efa
+
058efa
+PRE(sys_pkey_free)
058efa
+{
058efa
+  PRINT("pkey_free (%" FMT_REGWORD "u )", ARG1);
058efa
+
058efa
+  PRE_REG_READ1(long, "pkey_free",
058efa
+                unsigned long, "pkey");
058efa
+
058efa
+  /* Since pkey_alloc () can never succeed, see above, freeing any pkey is
058efa
+     always an error.  */
058efa
+  SET_STATUS_Failure( VKI_EINVAL );
058efa
+}
058efa
+
058efa
+PRE(sys_pkey_mprotect)
058efa
+{
058efa
+   PRINT("sys_pkey_mprotect ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u, %"
058efa
+         FMT_REGWORD "u %" FMT_REGWORD "u )", ARG1, ARG2, ARG3, ARG4);
058efa
+   PRE_REG_READ4(long, "pkey_mprotect",
058efa
+                 unsigned long, addr, vki_size_t, len, unsigned long, prot,
058efa
+                 unsigned long, pkey);
058efa
+
058efa
+   Addr  addr = ARG1;
058efa
+   SizeT len  = ARG2;
058efa
+   Int   prot = ARG3;
058efa
+   Int   pkey = ARG4;
058efa
+
058efa
+   /* Since pkey_alloc () can never succeed, see above, any pkey is
058efa
+      invalid. Except for -1, then pkey_mprotect acts just like mprotect.  */
058efa
+   if (pkey != -1)
058efa
+      SET_STATUS_Failure( VKI_EINVAL );
058efa
+   else
058efa
+      handle_sys_mprotect (tid, status, &addr, &len, &prot;;
058efa
+
058efa
+   ARG1 = addr;
058efa
+   ARG2 = len;
058efa
+   ARG3 = prot;
058efa
+}
058efa
+
058efa
+POST(sys_pkey_mprotect)
058efa
+{
058efa
+   Addr  addr = ARG1;
058efa
+   SizeT len  = ARG2;
058efa
+   Int   prot = ARG3;
058efa
+
058efa
+   ML_(notify_core_and_tool_of_mprotect)(addr, len, prot);
058efa
+}
058efa
+
058efa
 
058efa
 #undef PRE
058efa
 #undef POST
058efa
diff --git a/coregrind/m_syswrap/syswrap-x86-linux.c b/coregrind/m_syswrap/syswrap-x86-linux.c
058efa
index ad54cf6..3829fa4 100644
058efa
--- a/coregrind/m_syswrap/syswrap-x86-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-x86-linux.c
058efa
@@ -1608,6 +1608,9 @@ static SyscallTableEntry syscall_table[] = {
058efa
 
058efa
    LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 377
058efa
 
058efa
+   LINXY(__NR_pkey_mprotect,     sys_pkey_mprotect),    // 380
058efa
+   LINX_(__NR_pkey_alloc,        sys_pkey_alloc),       // 381
058efa
+   LINX_(__NR_pkey_free,         sys_pkey_free),        // 382
058efa
    LINXY(__NR_statx,             sys_statx),            // 383
058efa
 
058efa
    /* Explicitly not supported on i386 yet. */