x86/shstk: Use __force when casting away __user

Message ID 20240109224619.1013899-1-rick.p.edgecombe@intel.com
State New
Headers
Series x86/shstk: Use __force when casting away __user |

Commit Message

Edgecombe, Rick P Jan. 9, 2024, 10:46 p.m. UTC
  In setup_signal_shadow_stack() the kernel needs to push the restorer
address to the shadow stack. This involves writing the value of the
restorer pointer to the shadow stack. Since the restorer is defined as a
__user in struct k_sigaction, the __user needs to be casted away to read
the value. It is safe to do, because nothing is being dereferenced, and
the de-__user-ed value is not stashed in an accessible local variable
where it might accidentally be used for another purpose.

However, sparse warns about casting away __user. So use __force to
silence sparse and add a comment to explain why it is ok.

Fixes: 05e36022c054 ("x86/shstk: Handle signals for shadow stack")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202401080003.duO4RmjK-lkp@intel.com/
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
 arch/x86/kernel/shstk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
  

Comments

Kees Cook Jan. 9, 2024, 11:01 p.m. UTC | #1
On Tue, Jan 09, 2024 at 02:46:19PM -0800, Rick Edgecombe wrote:
> In setup_signal_shadow_stack() the kernel needs to push the restorer
> address to the shadow stack. This involves writing the value of the
> restorer pointer to the shadow stack. Since the restorer is defined as a
> __user in struct k_sigaction, the __user needs to be casted away to read
> the value. It is safe to do, because nothing is being dereferenced, and
> the de-__user-ed value is not stashed in an accessible local variable
> where it might accidentally be used for another purpose.
> 
> However, sparse warns about casting away __user. So use __force to
> silence sparse and add a comment to explain why it is ok.
> 
> Fixes: 05e36022c054 ("x86/shstk: Handle signals for shadow stack")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202401080003.duO4RmjK-lkp@intel.com/
> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>

Seems fine to me. Thanks!

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

Patch

diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c
index 59e15dd8d0f8..7cc294482011 100644
--- a/arch/x86/kernel/shstk.c
+++ b/arch/x86/kernel/shstk.c
@@ -367,7 +367,11 @@  int setup_signal_shadow_stack(struct ksignal *ksig)
 
 	/* Push restorer address */
 	ssp -= SS_FRAME_SIZE;
-	err = write_user_shstk_64((u64 __user *)ssp, (u64)restorer);
+	/*
+	 * Use __force because this is just writing the address of the pointer
+	 * to the shadow stack, not dereferencing it.
+	 */
+	err = write_user_shstk_64((u64 __user *)ssp, (u64 __force)restorer);
 	if (unlikely(err))
 		return -EFAULT;