Blame SOURCES/valgrind-3.15.0-pkey.patch

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