[RFC PATCH 3/4] x86/signal: Prevent an alternate stack overflow before a signal delivery
From: Chang S. Bae
Date: Tue Sep 29 2020 - 17:01:58 EST
The kernel pushes data on the userspace stack when entering a signal. If
using a sigaltstack(), the kernel precisely knows the user stack size.
When the kernel knows that the user stack is too small, avoid the overflow
and do an immediate SIGSEGV instead.
This overflow is known to occur on systems with large XSAVE state. The
effort to increase the size typically used for altstacks reduces the
frequency of these overflows, but this approach is still useful for legacy
binaries.
Here the kernel expects a bit conservative stack size (for 64-bit apps).
Legacy binaries used a too-small sigaltstack would be already overflowed
before this change, if they run on modern hardware.
Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
Reviewed-by: Len Brown <len.brown@xxxxxxxxx>
Cc: x86@xxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
arch/x86/ia32/ia32_signal.c | 11 ++++++++---
arch/x86/include/asm/sigframe.h | 2 ++
arch/x86/kernel/signal.c | 16 +++++++++++++++-
3 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c
index 81cf22398cd1..85abd9eb79d5 100644
--- a/arch/x86/ia32/ia32_signal.c
+++ b/arch/x86/ia32/ia32_signal.c
@@ -210,13 +210,18 @@ static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
sp = regs->sp;
/* This is the X/Open sanctioned signal stack switching. */
- if (ksig->ka.sa.sa_flags & SA_ONSTACK)
+ if (ksig->ka.sa.sa_flags & SA_ONSTACK) {
+ /* If the altstack might overflow, die with SIGSEGV: */
+ if (!altstack_size_ok(current))
+ return (void __user *)-1L;
+
sp = sigsp(sp, ksig);
/* This is the legacy signal stack switching. */
- else if (regs->ss != __USER32_DS &&
+ } else if (regs->ss != __USER32_DS &&
!(ksig->ka.sa.sa_flags & SA_RESTORER) &&
- ksig->ka.sa.sa_restorer)
+ ksig->ka.sa.sa_restorer) {
sp = (unsigned long) ksig->ka.sa.sa_restorer;
+ }
sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
*fpstate = (struct _fpstate_32 __user *) sp;
diff --git a/arch/x86/include/asm/sigframe.h b/arch/x86/include/asm/sigframe.h
index ac77f3f90bc9..c9f2f9ace76f 100644
--- a/arch/x86/include/asm/sigframe.h
+++ b/arch/x86/include/asm/sigframe.h
@@ -106,6 +106,8 @@ struct rt_sigframe_x32 {
#define SIZEOF_rt_sigframe_x32 0
#endif
+bool altstack_size_ok(struct task_struct *tsk);
+
void __init init_sigframe_size(void);
#endif /* _ASM_X86_SIGFRAME_H */
diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c
index 2a313d34ec49..c042236ef52e 100644
--- a/arch/x86/kernel/signal.c
+++ b/arch/x86/kernel/signal.c
@@ -246,8 +246,13 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
/* This is the X/Open sanctioned signal stack switching. */
if (ka->sa.sa_flags & SA_ONSTACK) {
- if (sas_ss_flags(sp) == 0)
+ if (sas_ss_flags(sp) == 0) {
+ /* If the altstack might overflow, die with SIGSEGV: */
+ if (!altstack_size_ok(current))
+ return (void __user *)-1L;
+
sp = current->sas_ss_sp + current->sas_ss_size;
+ }
} else if (IS_ENABLED(CONFIG_X86_32) &&
!onsigstack &&
regs->ss != __USER_DS &&
@@ -713,6 +718,15 @@ unsigned long get_sigframe_size(void)
return max_frame_size;
}
+bool altstack_size_ok(struct task_struct *tsk)
+{
+ /*
+ * Can this task's sigaltstack accommodate the largest
+ * signal frame the kernel might need?
+ */
+ return (tsk->sas_ss_size >= max_frame_size);
+}
+
static inline int is_ia32_compat_frame(struct ksignal *ksig)
{
return IS_ENABLED(CONFIG_IA32_EMULATION) &&
--
2.17.1