]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
x86/pkeys: Add helper functions to update PKRU on the sigframe
authorAruna Ramakrishna <aruna.ramakrishna@oracle.com>
Fri, 2 Aug 2024 06:13:15 +0000 (06:13 +0000)
committerThomas Gleixner <tglx@linutronix.de>
Fri, 2 Aug 2024 12:12:21 +0000 (14:12 +0200)
In the case where a user thread sets up an alternate signal stack protected
by the default PKEY (i.e. PKEY 0), while the thread's stack is protected by
a non-zero PKEY, both these PKEYS have to be enabled in the PKRU register
for the signal to be delivered to the application correctly. However, the
PKRU value restored after handling the signal must not enable this extra
PKEY (i.e. PKEY 0) - i.e., the PKRU value in the sigframe has to be
overwritten with the user-defined value.

Add helper functions that will update PKRU value in the sigframe after
XSAVE.

Note that sig_prepare_pkru() makes no assumption about which PKEY could
be used to protect the altstack (i.e. it may not be part of init_pkru),
and so enables all PKEYS.

No functional change.

Signed-off-by: Aruna Ramakrishna <aruna.ramakrishna@oracle.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/all/20240802061318.2140081-3-aruna.ramakrishna@oracle.com
arch/x86/kernel/fpu/signal.c
arch/x86/kernel/fpu/xstate.c
arch/x86/kernel/fpu/xstate.h
arch/x86/kernel/signal.c

index 2b3b9e140dd41b6671af6701ed2145c2f92f0201..931c5469d7f3d18c0b721728bce479f85d24082c 100644 (file)
@@ -63,6 +63,16 @@ setfx:
        return true;
 }
 
+/*
+ * Update the value of PKRU register that was already pushed onto the signal frame.
+ */
+static inline int update_pkru_in_sigframe(struct xregs_state __user *buf, u32 pkru)
+{
+       if (unlikely(!cpu_feature_enabled(X86_FEATURE_OSPKE)))
+               return 0;
+       return __put_user(pkru, (unsigned int __user *)get_xsave_addr_user(buf, XFEATURE_PKRU));
+}
+
 /*
  * Signal frame handlers.
  */
index c5a026fee5e061a463133ef2e78baaeef32500d1..fa7628bb541be661a619fee0921648d52af18947 100644 (file)
@@ -993,6 +993,19 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
 }
 EXPORT_SYMBOL_GPL(get_xsave_addr);
 
+/*
+ * Given an xstate feature nr, calculate where in the xsave buffer the state is.
+ * The xsave buffer should be in standard format, not compacted (e.g. user mode
+ * signal frames).
+ */
+void __user *get_xsave_addr_user(struct xregs_state __user *xsave, int xfeature_nr)
+{
+       if (WARN_ON_ONCE(!xfeature_enabled(xfeature_nr)))
+               return NULL;
+
+       return (void __user *)xsave + xstate_offsets[xfeature_nr];
+}
+
 #ifdef CONFIG_ARCH_HAS_PKEYS
 
 /*
index 2ee0b9c53dccc42a4eb0ba9774cd2979e06a342b..5f057e50df816d67bf3e46bd5ee1083aa6c6548e 100644 (file)
@@ -54,6 +54,8 @@ extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void
 extern void fpu__init_cpu_xstate(void);
 extern void fpu__init_system_xstate(unsigned int legacy_size);
 
+extern void __user *get_xsave_addr_user(struct xregs_state __user *xsave, int xfeature_nr);
+
 static inline u64 xfeatures_mask_supervisor(void)
 {
        return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
index 1f1e8e0ac5a341c76203bec919bcfd942bfec002..9dc77ad03a0e98fa69261e1246885185a0e5e043 100644 (file)
@@ -60,6 +60,24 @@ static inline int is_x32_frame(struct ksignal *ksig)
                ksig->ka.sa.sa_flags & SA_X32_ABI;
 }
 
+/*
+ * Enable all pkeys temporarily, so as to ensure that both the current
+ * execution stack as well as the alternate signal stack are writeable.
+ * The application can use any of the available pkeys to protect the
+ * alternate signal stack, and we don't know which one it is, so enable
+ * all. The PKRU register will be reset to init_pkru later in the flow,
+ * in fpu__clear_user_states(), and it is the application's responsibility
+ * to enable the appropriate pkey as the first step in the signal handler
+ * so that the handler does not segfault.
+ */
+static inline u32 sig_prepare_pkru(void)
+{
+       u32 orig_pkru = read_pkru();
+
+       write_pkru(0);
+       return orig_pkru;
+}
+
 /*
  * Set up a signal frame.
  */