x86: signal: check signal stack overflow properly
PATCH 1/1 x86: signal: check signal stack overflow properly
From: Hiroshi Shimamoto <[email protected]>
Impact: cleanup
Check alternate signal stack overflow with proper stack pointer. The stack pointer of the next signal frame is different if that task has i387 state.
On x86_64, redzone would be included.
No need to check SA_ONSTACK if we're already using alternate signal stack.
--- arch/x86/kernel/signal.c | 48 +++++++++++++++++++++++++-------------------- 1 files changed, 27 insertions(+), 21 deletions(-)
arch/x86/kernel/signal.c +27 -21
@@ -211,31 +211,27 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, {/* Default to using normal stack */ unsigned long sp = regs->sp; +int onsigstack =on_sig_stack (sp);#ifdef CONFIG_X86_64 /* redzone */ sp -=128 ;#endif /* CONFIG_X86_64 */ -/* -* If we are on the alternate signal stack and would overflow it, don't. -* Return an always-bogus address instead so we will die with SIGSEGV. -*/ -if (on_sig_stack (sp) && !likely (on_sig_stack (sp - frame_size))) -return (void __user *)-1L ; - -/* This is the X/Open sanctioned signal stack switching. */ -if (ka->sa.sa_flags &SA_ONSTACK ) { -if (sas_ss_flags (sp) ==0 ) - sp = current->sas_ss_sp + current->sas_ss_size; - }else { +if (!onsigstack) { +/* This is the X/Open sanctioned signal stack switching. */ +if (ka->sa.sa_flags &SA_ONSTACK ) { +if (sas_ss_flags (sp) ==0 ) + sp = current->sas_ss_sp + current->sas_ss_size; + }else {#ifdef CONFIG_X86_32 -/* This is the legacy signal stack switching. */ -if ((regs->ss &0xffff ) != __USER_DS && - !(ka->sa.sa_flags &SA_RESTORER ) && - ka->sa.sa_restorer) - sp = (unsigned long ) ka->sa.sa_restorer; +/* This is the legacy signal stack switching. */ +if ((regs->ss &0xffff ) != __USER_DS && + !(ka->sa.sa_flags &SA_RESTORER ) && + ka->sa.sa_restorer) + sp = (unsigned long ) ka->sa.sa_restorer;#endif /* CONFIG_X86_32 */ + } }if (used_math ()) {
@@ -244,12 +240,22 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, sp =round_down (sp,64 );#endif /* CONFIG_X86_64 */ *fpstate = (void __user *)sp; - -if (save_i387_xstate (*fpstate) <0 ) -return (void __user *)-1L ; } -return (void __user *)align_sigframe (sp - frame_size); + sp =align_sigframe (sp - frame_size); + +/* +* If we are on the alternate signal stack and would overflow it, don't. +* Return an always-bogus address instead so we will die with SIGSEGV. +*/ +if (onsigstack && !likely (on_sig_stack (sp))) +return (void __user *)-1L ; + +/* save i387 state */ +if (used_math () &&save_i387_xstate (*fpstate) <0 ) +return (void __user *)-1L ; + +return (void __user *)sp; }#ifdef CONFIG_X86_32
Signature · 1 line
1.6.1.2
Commit-ID: 14fc9fbc700dc95b4f46ebd588169324fe6deff8 Gitweb: http://git.kernel.org/tip/14fc9fbc700dc95b4f46ebd588169324fe6deff8 Author: Hiroshi Shimamoto <[email protected]> AuthorDate: Thu, 19 Mar 2009 10:56:29 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 20 Mar 2009 19:01:31 +0100
x86: signal: check signal stack overflow properly
Impact: cleanup
Check alternate signal stack overflow with proper stack pointer. The stack pointer of the next signal frame is different if that task has i387 state.
On x86_64, redzone would be included.
No need to check SA_ONSTACK if we're already using alternate signal stack.
LKML-Reference: <[email protected]>
--- arch/x86/kernel/signal.c | 48 +++++++++++++++++++++++++-------------------- 1 files changed, 27 insertions(+), 21 deletions(-)
arch/x86/kernel/signal.c +27 -21
@@ -211,31 +211,27 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, {/* Default to using normal stack */ unsigned long sp = regs->sp; +int onsigstack =on_sig_stack (sp);#ifdef CONFIG_X86_64 /* redzone */ sp -=128 ;#endif /* CONFIG_X86_64 */ -/* -* If we are on the alternate signal stack and would overflow it, don't. -* Return an always-bogus address instead so we will die with SIGSEGV. -*/ -if (on_sig_stack (sp) && !likely (on_sig_stack (sp - frame_size))) -return (void __user *)-1L ; - -/* This is the X/Open sanctioned signal stack switching. */ -if (ka->sa.sa_flags &SA_ONSTACK ) { -if (sas_ss_flags (sp) ==0 ) - sp = current->sas_ss_sp + current->sas_ss_size; - }else { +if (!onsigstack) { +/* This is the X/Open sanctioned signal stack switching. */ +if (ka->sa.sa_flags &SA_ONSTACK ) { +if (sas_ss_flags (sp) ==0 ) + sp = current->sas_ss_sp + current->sas_ss_size; + }else {#ifdef CONFIG_X86_32 -/* This is the legacy signal stack switching. */ -if ((regs->ss &0xffff ) != __USER_DS && - !(ka->sa.sa_flags &SA_RESTORER ) && - ka->sa.sa_restorer) - sp = (unsigned long ) ka->sa.sa_restorer; +/* This is the legacy signal stack switching. */ +if ((regs->ss &0xffff ) != __USER_DS && + !(ka->sa.sa_flags &SA_RESTORER ) && + ka->sa.sa_restorer) + sp = (unsigned long ) ka->sa.sa_restorer;#endif /* CONFIG_X86_32 */ + } }if (used_math ()) {
@@ -244,12 +240,22 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, sp =round_down (sp,64 );#endif /* CONFIG_X86_64 */ *fpstate = (void __user *)sp; - -if (save_i387_xstate (*fpstate) <0 ) -return (void __user *)-1L ; } -return (void __user *)align_sigframe (sp - frame_size); + sp =align_sigframe (sp - frame_size); + +/* +* If we are on the alternate signal stack and would overflow it, don't. +* Return an always-bogus address instead so we will die with SIGSEGV. +*/ +if (onsigstack && !likely (on_sig_stack (sp))) +return (void __user *)-1L ; + +/* save i387 state */ +if (used_math () &&save_i387_xstate (*fpstate) <0 ) +return (void __user *)-1L ; + +return (void __user *)sp; }#ifdef CONFIG_X86_32
Quoted 8 lines · Hiroshi Shimamoto
Commit-ID: 14fc9fbc700dc95b4f46ebd588169324fe6deff8 Gitweb: http://git.kernel.org/tip/14fc9fbc700dc95b4f46ebd588169324fe6deff8 Author: Hiroshi Shimamoto <[email protected]> AuthorDate: Thu, 19 Mar 2009 10:56:29 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 20 Mar 2009 19:01:31 +0100
Quoted 7 lines
x86: signal: check signal stack overflow properly
Impact: cleanup
Check alternate signal stack overflow with proper stack pointer. The stack pointer of the next signal frame is different if that task has i387 state.
I think the patch is correct but I have a minor question,
Quoted 1 line
No need to check
SA_ONSTACKif we're already using alternate signal stack.
Yes, but this also mean that we don't need sas_ss_flags() under "if (!onsigstack)",
Quoted 18 lines
arch/x86/kernel/signal.c +1
-7@@ -211,31 +211,27 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, {/* Default to using normal stack */ unsigned long sp = regs->sp; +int onsigstack =on_sig_stack (sp);#ifdef CONFIG_X86_64 /* redzone */ sp -=128 ;#endif /* CONFIG_X86_64 */ -/* -* If we are on the alternate signal stack and would overflow it, don't. -* Return an always-bogus address instead so we will die with SIGSEGV. -*/ -if (on_sig_stack (sp) && !likely (on_sig_stack (sp - frame_size))) -return (void __user *)-1L ; -
arch/x86/kernel/signal.c get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, · line 225
Quoted 10 lines
arch/x86/kernel/signal.c +5
-5-/* This is the X/Open sanctioned signal stack switching. */ -if (ka->sa.sa_flags &SA_ONSTACK ) { -if (sas_ss_flags (sp) ==0 ) - sp = current->sas_ss_sp + current->sas_ss_size; - }else { +if (!onsigstack) { +/* This is the X/Open sanctioned signal stack switching. */ +if (ka->sa.sa_flags &SA_ONSTACK ) { +if (sas_ss_flags (sp) ==0 ) + sp = current->sas_ss_sp + current->sas_ss_size;
We can use "->sas_ss_size != 0" instead and avoid the unnecessary sas_ss_flags()->on_sig_stack() check.
Please note that afaics sas_ss_flags()->on_sig_stack() is actually wrong because we already adjusted "sp" above for redzone.
Suppose that on_sig_stack(regs->sp) = F, but "sp - 128" falls into the altstack. In that case SA_ONSTACK won't switch the stack.
Of course, this is only theoretical, but still.
Oleg.
Quoted 55 lines · Oleg Nesterov
Quoted 7 lines · Hiroshi Shimamoto
Commit-ID: 14fc9fbc700dc95b4f46ebd588169324fe6deff8 Gitweb: http://git.kernel.org/tip/14fc9fbc700dc95b4f46ebd588169324fe6deff8 Author: Hiroshi Shimamoto <[email protected]> AuthorDate: Thu, 19 Mar 2009 10:56:29 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 20 Mar 2009 19:01:31 +0100Quoted 7 lines
x86: signal: check signal stack overflow properly
Impact: cleanup
Check alternate signal stack overflow with proper stack pointer. The stack pointer of the next signal frame is different if that task has i387 state.
I think the patch is correct but I have a minor question,
Quoted 1 line
No need to check
SA_ONSTACKif we're already using alternate signal stack.Yes, but this also mean that we don't need
sas_ss_flags()under "if (!onsigstack)",Quoted 18 lines
(quoted diff) +1
-7@@ -211,31 +211,27 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, { /* Default to using normal stack */ unsigned long sp = regs->sp; + int onsigstack = on_sig_stack(sp); #ifdef CONFIG_X86_64 /* redzone */ sp -= 128; #endif /* CONFIG_X86_64 */ - /* - * If we are on the alternate signal stack and would overflow it, don't. - * Return an always-bogus address instead so we will die with SIGSEGV. - */ - if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) - return (void __user *) -1L; -Review get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, · line 225
Quoted 10 lines
(quoted diff) +5
-5- /* This is the X/Open sanctioned signal stack switching. */ - if (ka->sa.sa_flags & SA_ONSTACK) { - if (sas_ss_flags(sp) == 0) - sp = current->sas_ss_sp + current->sas_ss_size; - } else { + if (!onsigstack) { + /* This is the X/Open sanctioned signal stack switching. */ + if (ka->sa.sa_flags & SA_ONSTACK) { + if (sas_ss_flags(sp) == 0) + sp = current->sas_ss_sp + current->sas_ss_size;We can use "->sas_ss_size != 0" instead and avoid the unnecessary sas_ss_flags()->on_sig_stack() check.
Quoted 7 lines
Please note that afaics sas_ss_flags()->on_sig_stack() is actually wrong because we already adjusted "sp" above for redzone.
Suppose that
on_sig_stack(regs->sp)= F, but "sp - 128" falls into the altstack. In that caseSA_ONSTACKwon't switch the stack.Of course, this is only theoretical, but still.
Hi Oleg,
Thanks for pointing out it.
I made a patch you suggested.
I haven't tested enough this patch, sorry.
Thanks,
Hiroshi
========
From: Hiroshi Shimamoto <[email protected]>
Subject: [PATCH] x86: signal: check sas_ss_size instead of sas_ss_flags()
Impact: fix redundant and incorrect check
Checking on_sig_stack() in sas_ss_flags() at get_sigframe() is redundant and not correct on 64 bit. To check sas_ss_size is enough.
--- arch/x86/kernel/signal.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/signal.c +1 -1
@@ -221,7 +221,7 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,if (!onsigstack) {/* This is the X/Open sanctioned signal stack switching. */ if (ka->sa.sa_flags &SA_ONSTACK ) { -if (sas_ss_flags (sp) ==0 ) +if (current->sas_ss_size) sp = current->sas_ss_sp + current->sas_ss_size; }else {#ifdef CONFIG_X86_32
Signature · 1 line
1.6.1.2
[tip:x86/urgent] x86: signal: check sas_ss_size instead of sas_ss_flags()
Commit-ID: 0f8f308925ebe0480bd9831d32963ee0b885e24b Gitweb: http://git.kernel.org/tip/0f8f308925ebe0480bd9831d32963ee0b885e24b Author: Hiroshi Shimamoto <[email protected]> AuthorDate: Thu, 26 Mar 2009 10:03:08 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 1 Apr 2009 17:13:17 +0200
x86: signal: check sas_ss_size instead of sas_ss_flags()
Impact: fix redundant and incorrect check
Oleg Nesterov noticed wrt commit:
14fc9fb: x86: signal: check signal stack overflow properly
Quoted 4 lines
Quoted 1 line
No need to check
SA_ONSTACKif we're already using alternate signal stack.Yes, but this also mean that we don't need
sas_ss_flags()under "if (!onsigstack)",
Checking on_sig_stack() in sas_ss_flags() at get_sigframe() is redundant and not correct on 64 bit. To check sas_ss_size is enough.
LKML-Reference: <[email protected]>
--- arch/x86/kernel/signal.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/signal.c +1 -1
@@ -221,7 +221,7 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,if (!onsigstack) {/* This is the X/Open sanctioned signal stack switching. */ if (ka->sa.sa_flags &SA_ONSTACK ) { -if (sas_ss_flags (sp) ==0 ) +if (current->sas_ss_size) sp = current->sas_ss_sp + current->sas_ss_size; }else {#ifdef CONFIG_X86_32
Hiroshi-san,
I am getting few sparse warnings from your commited lines. If possible, please run sparse before sending patch.
Quoted 41 lines · Hiroshi Shimamoto
Commit-ID: 14fc9fbc700dc95b4f46ebd588169324fe6deff8 Gitweb: http://git.kernel.org/tip/14fc9fbc700dc95b4f46ebd588169324fe6deff8 Author: Hiroshi Shimamoto <[email protected]> AuthorDate: Thu, 19 Mar 2009 10:56:29 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 20 Mar 2009 19:01:31 +0100x86: signal: check signal stack overflow properly
Impact: cleanup
Check alternate signal stack overflow with proper stack pointer. The stack pointer of the next signal frame is different if that task has i387 state.
On
x86_64, redzone would be included.No need to check
SA_ONSTACKif we're already using alternate signal stack.Signed-off-by Hiroshi Shimamoto <[email protected]>Cc Roland McGrath <[email protected]>LKML-Reference: <[email protected]>
Signed-off-by Ingo Molnar <[email protected]>--- arch/x86/kernel/signal.c | 48 +++++++++++++++++++++++++-------------------- 1 files changed, 27 insertions(+), 21 deletions(-)arch/x86/kernel/signal.c +0
-3@@ -244,12 +240,22 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, sp =round_down (sp,64 );#endif /* CONFIG_X86_64 */ *fpstate = (void __user *)sp; - -if (save_i387_xstate (*fpstate) <0 ) -return (void __user *)-1L ; }
arch/x86/kernel/signal.c get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, · line 252
Quoted 10 lines
arch/x86/kernel/signal.c +8
-1-return (void __user *)align_sigframe (sp - frame_size); + sp =align_sigframe (sp - frame_size); + +/* +* If we are on the alternate signal stack and would overflow it, don't. +* Return an always-bogus address instead so we will die with SIGSEGV. +*/ +if (onsigstack && !likely (on_sig_stack (sp))) +return (void __user *)-1L ;
arch/x86/kernel/signal.c:251:11: warning: cast adds address space to expression (<asn:1>)
arch/x86/kernel/signal.c get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, · line 256
Quoted 4 lines
arch/x86/kernel/signal.c +4
-0+ +/* save i387 state */ +if (used_math () &&save_i387_xstate (*fpstate) <0 ) +return (void __user *)-1L ;
arch/x86/kernel/signal.c:255:11: warning: cast adds address space to expression (<asn:1>)
arch/x86/kernel/signal.c get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, · line 261
Quoted 5 lines
arch/x86/kernel/signal.c +2
-0+ +return (void __user *)sp; }#ifdef CONFIG_X86_32
4a612048 arch/x86/kernel/signal_32.c (Hiroshi Shimamoto 2008-11-11 19:09:29 -0800 326) err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: incorrect type in argument 1 (different address spaces)arch/x86/kernel/signal.c:326:9: expected void const volatile [noderef] <asn:1>*<noident>arch/x86/kernel/signal.c:326:9: got unsigned long long [usertype] *<noident>arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast adds address space to expression (<asn:1>) arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast adds address space to expression (<asn:1>) arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast adds address space to expression (<asn:1>) arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression arch/x86/kernel/signal.c:326:9: warning: cast removes address space of expression
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 359) put_user_try {
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 360) put_user_ex(sig, &frame->sig);
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 361) put_user_ex(&frame->info, &frame->pinfo);arch/x86/kernel/signal.c:361:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:361:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:361:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:361:3: warning: cast removes address space of expression
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 362) put_user_ex(&frame->uc, &frame->puc);
arch/x86/kernel/signal.c:362:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:362:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:362:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:362:3: warning: cast removes address space of expression
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 363) err |= copy_siginfo_to_user(&frame->info, info);98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 364)
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 379) /* Set up to return from userspace. */ 98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 380) restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_si
arch/x86/kernel/signal.c:380:12: warning: incorrect type in assignment (different address spaces)arch/x86/kernel/signal.c:380:12: expected void [noderef] <asn:1>*restorerarch/x86/kernel/signal.c:380:12: got void *<noident>
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 381) if (ka->sa.sa_flags & SA_RESTORER) 98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 382) restorer = ka->sa.sa_restorer; 98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 383) put_user_ex(restorer, &frame->pretcode);
arch/x86/kernel/signal.c:383:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:383:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:383:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:383:3: warning: cast removes address space of expression
98e3d45e arch/x86/kernel/signal.c (Hiroshi Shimamoto 2009-01-23 15:50:10 -0800 392) put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode)
arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: incorrect type in argument 1 (different address spaces)arch/x86/kernel/signal.c:392:3: expected void const volatile [noderef] <asn:1>*<noident>arch/x86/kernel/signal.c:392:3: got unsigned long long [usertype] *<noident>arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast adds address space to expression (<asn:1>) arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast adds address space to expression (<asn:1>) arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast adds address space to expression (<asn:1>) arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression arch/x86/kernel/signal.c:392:3: warning: cast removes address space of expression
--
JSR