x86/shstk: Add ARCH_SHSTK_STATUS
authorRick Edgecombe <rick.p.edgecombe@intel.com>
Tue, 13 Jun 2023 00:11:08 +0000 (17:11 -0700)
committerDave Hansen <dave.hansen@linux.intel.com>
Wed, 2 Aug 2023 22:01:51 +0000 (15:01 -0700)
CRIU and GDB need to get the current shadow stack and WRSS enablement
status. This information is already available via /proc/pid/status, but
this is inconvenient for CRIU because it involves parsing the text output
in an area of the code where this is difficult. Provide a status
arch_prctl(), ARCH_SHSTK_STATUS for retrieving the status. Have arg2 be a
userspace address, and make the new arch_prctl simply copy the features
out to userspace.

Suggested-by: Mike Rapoport <rppt@kernel.org>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Mike Rapoport (IBM) <rppt@kernel.org>
Tested-by: Pengfei Xu <pengfei.xu@intel.com>
Tested-by: John Allen <john.allen@amd.com>
Tested-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/all/20230613001108.3040476-43-rick.p.edgecombe%40intel.com
Documentation/arch/x86/shstk.rst
arch/x86/include/asm/shstk.h
arch/x86/include/uapi/asm/prctl.h
arch/x86/kernel/process_64.c
arch/x86/kernel/shstk.c

index f3553cc8c758bad9175d8ef690c021fee736e850..60260e809baf6a567ce014e1df54256bc86f9cf2 100644 (file)
@@ -79,6 +79,11 @@ arch_prctl(ARCH_SHSTK_UNLOCK, unsigned long features)
     Unlock features. 'features' is a mask of all features to unlock. All
     bits set are processed, unset bits are ignored. Only works via ptrace.
 
+arch_prctl(ARCH_SHSTK_STATUS, unsigned long addr)
+    Copy the currently enabled features to the address passed in addr. The
+    features are described using the bits passed into the others in
+    'features'.
+
 The return values are as follows. On success, return 0. On error, errno can
 be::
 
@@ -86,6 +91,7 @@ be::
         -ENOTSUPP if the feature is not supported by the hardware or
          kernel.
         -EINVAL arguments (non existing feature, etc)
+        -EFAULT if could not copy information back to userspace
 
 The feature's bits supported are::
 
index ecb23a8ca47d28429fa50e38f7d9c5e906067019..42fee8959df7bee17e27eaa7990db2401478d1d5 100644 (file)
@@ -14,7 +14,7 @@ struct thread_shstk {
        u64     size;
 };
 
-long shstk_prctl(struct task_struct *task, int option, unsigned long features);
+long shstk_prctl(struct task_struct *task, int option, unsigned long arg2);
 void reset_thread_features(void);
 unsigned long shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags,
                                       unsigned long stack_size);
index 3189c4a96468a3c67447fe73269d620f7408421b..384e2cc6ac190d1d8683f1e657ef0b2be57c6a9f 100644 (file)
@@ -34,6 +34,7 @@
 #define ARCH_SHSTK_DISABLE             0x5002
 #define ARCH_SHSTK_LOCK                        0x5003
 #define ARCH_SHSTK_UNLOCK              0x5004
+#define ARCH_SHSTK_STATUS              0x5005
 
 /* ARCH_SHSTK_ features bits */
 #define ARCH_SHSTK_SHSTK               (1ULL <<  0)
index e6db21c470aa3a0c44f3c6135899ac9e2255671f..33b268747bb7bbce00e1b12b8e00928ce7305acc 100644 (file)
@@ -900,6 +900,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
        case ARCH_SHSTK_DISABLE:
        case ARCH_SHSTK_LOCK:
        case ARCH_SHSTK_UNLOCK:
+       case ARCH_SHSTK_STATUS:
                return shstk_prctl(task, option, arg2);
        default:
                ret = -EINVAL;
index d43b7a9c57ced004b9ea92af69ebd589a3a245cb..b26810c7cd1cfa601a334bb4241ef7e9e2925b41 100644 (file)
@@ -482,8 +482,14 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi
        return alloc_shstk(addr, aligned_size, size, set_tok);
 }
 
-long shstk_prctl(struct task_struct *task, int option, unsigned long features)
+long shstk_prctl(struct task_struct *task, int option, unsigned long arg2)
 {
+       unsigned long features = arg2;
+
+       if (option == ARCH_SHSTK_STATUS) {
+               return put_user(task->thread.features, (unsigned long __user *)arg2);
+       }
+
        if (option == ARCH_SHSTK_LOCK) {
                task->thread.features_locked |= features;
                return 0;