[v4,39/39] x86/shstk: Add ARCH_SHSTK_STATUS

Message ID 20221203003606.6838-40-rick.p.edgecombe@intel.com
State New
Headers
Series Shadow stacks for userspace |

Commit Message

Edgecombe, Rick P Dec. 3, 2022, 12:36 a.m. UTC
  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 incovienent 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.

Tested-by: Pengfei Xu <pengfei.xu@intel.com>
Requested-by: Mike Rapoport <rppt@kernel.org>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---

v4:
 - New patch

 Documentation/x86/shstk.rst       | 6 ++++++
 arch/x86/include/asm/shstk.h      | 4 ++--
 arch/x86/include/uapi/asm/prctl.h | 1 +
 arch/x86/kernel/process_64.c      | 1 +
 arch/x86/kernel/shstk.c           | 8 +++++++-
 5 files changed, 17 insertions(+), 3 deletions(-)
  

Comments

Kees Cook Dec. 3, 2022, 2:57 a.m. UTC | #1
On Fri, Dec 02, 2022 at 04:36:06PM -0800, Rick Edgecombe wrote:
> 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 incovienent 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.
> 
> Tested-by: Pengfei Xu <pengfei.xu@intel.com>
> Requested-by: Mike Rapoport <rppt@kernel.org>
> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
> ---
> 
> v4:
>  - New patch
> 
>  Documentation/x86/shstk.rst       | 6 ++++++
>  arch/x86/include/asm/shstk.h      | 4 ++--
>  arch/x86/include/uapi/asm/prctl.h | 1 +
>  arch/x86/kernel/process_64.c      | 1 +
>  arch/x86/kernel/shstk.c           | 8 +++++++-
>  5 files changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst
> index 0d7d1ccfff06..b3eb87046c27 100644
> --- a/Documentation/x86/shstk.rst
> +++ b/Documentation/x86/shstk.rst
> @@ -77,6 +77,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 following:
>      On success, return 0. On error, errno can be::
>  
> @@ -84,6 +89,7 @@ The return values are as following:
>          -EOPNOTSUPP if the feature is not supported by the hardware or
>           disabled by kernel parameter.
>          -EINVAL arguments (non existing feature, etc)
> +        -EFAULT if could not copy information back to userspace
>  
>  The feature's bits supported are::
>  
> diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h
> index c82f22fd5e6d..23bfb63c597d 100644
> --- a/arch/x86/include/asm/shstk.h
> +++ b/arch/x86/include/asm/shstk.h
> @@ -15,7 +15,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);
>  int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags,
>  			     unsigned long stack_size,
> @@ -31,7 +31,7 @@ static inline bool shstk_enabled(void)
>  }
>  #else
>  static inline long shstk_prctl(struct task_struct *task, int option,
> -			     unsigned long features) { return -EINVAL; }
> +			     unsigned long arg2) { return -EINVAL; }
>  static inline void reset_thread_features(void) {}
>  static inline int shstk_alloc_thread_stack(struct task_struct *p,
>  					   unsigned long clone_flags,
> diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h
> index 0c95688cf58e..abe3fe6db6d2 100644
> --- a/arch/x86/include/uapi/asm/prctl.h
> +++ b/arch/x86/include/uapi/asm/prctl.h
> @@ -31,6 +31,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)
> diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
> index 2be6e01fb144..5dcf5426241b 100644
> --- a/arch/x86/kernel/process_64.c
> +++ b/arch/x86/kernel/process_64.c
> @@ -922,6 +922,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;
> diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c
> index 95579f7bace3..05f8dcc19dbc 100644
> --- a/arch/x86/kernel/shstk.c
> +++ b/arch/x86/kernel/shstk.c
> @@ -448,8 +448,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);
> +	}

Doesn't need the braces, but that's fine by me.

Reviewed-by: Kees Cook <keescook@chromium.org>

> +
>  	if (option == ARCH_SHSTK_LOCK) {
>  		task->thread.features_locked |= features;
>  		return 0;
> -- 
> 2.17.1
>
  

Patch

diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst
index 0d7d1ccfff06..b3eb87046c27 100644
--- a/Documentation/x86/shstk.rst
+++ b/Documentation/x86/shstk.rst
@@ -77,6 +77,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 following:
     On success, return 0. On error, errno can be::
 
@@ -84,6 +89,7 @@  The return values are as following:
         -EOPNOTSUPP if the feature is not supported by the hardware or
          disabled by kernel parameter.
         -EINVAL arguments (non existing feature, etc)
+        -EFAULT if could not copy information back to userspace
 
 The feature's bits supported are::
 
diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h
index c82f22fd5e6d..23bfb63c597d 100644
--- a/arch/x86/include/asm/shstk.h
+++ b/arch/x86/include/asm/shstk.h
@@ -15,7 +15,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);
 int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags,
 			     unsigned long stack_size,
@@ -31,7 +31,7 @@  static inline bool shstk_enabled(void)
 }
 #else
 static inline long shstk_prctl(struct task_struct *task, int option,
-			     unsigned long features) { return -EINVAL; }
+			     unsigned long arg2) { return -EINVAL; }
 static inline void reset_thread_features(void) {}
 static inline int shstk_alloc_thread_stack(struct task_struct *p,
 					   unsigned long clone_flags,
diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h
index 0c95688cf58e..abe3fe6db6d2 100644
--- a/arch/x86/include/uapi/asm/prctl.h
+++ b/arch/x86/include/uapi/asm/prctl.h
@@ -31,6 +31,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)
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 2be6e01fb144..5dcf5426241b 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -922,6 +922,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;
diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c
index 95579f7bace3..05f8dcc19dbc 100644
--- a/arch/x86/kernel/shstk.c
+++ b/arch/x86/kernel/shstk.c
@@ -448,8 +448,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;