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