Blame SOURCES/valgrind-3.15.0-pkey.patch

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