Skip to contentSkip to conversation

signal(x86_32): Improve the signal stack overflow check

PATCH Applied Shi Weihua 7 participants 22 messages Feb 18, 2008 – Feb 20, 2008 x86

lore
Share & tools
lore

Shi Weihua

author
#0 lore raw

PATCH 1/5 signal(x86_32): Improve the signal stack overflow check

We need to check for stack overflow only when the signal is on stack. So we can improve the patch "http://lkml.org/lkml/2007/11/27/101" as following.

---

arch/x86/kernel/signal_32.c +12 -8
@@ -299,17 +299,21 @@ get_sigframe(struct k_sigaction *ka, str
 	/* Default to using normal stack */
 	sp = regs->sp;
 
-	/*
-	 * 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)
+		int onstack = sas_ss_flags(sp);
+
+		if (onstack == 0)
 			sp = current->sas_ss_sp + current->sas_ss_size;
+		else if (onstack == SS_ONSTACK) {
+			/*
+			 * 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 (!likely(on_sig_stack(sp - frame_size)))
+				return (void __user *) -1L;
+		}
 	}
 
 	/* This is the legacy signal stack switching. */
#3 lore raw

This change looks bogus to me. Before I get to the content, there is a nit that annoys me. You changed the punctuation in my comment so that it no longer means what it did, and now the comment is nonsensical. I don't demand decent English from hackers of any linguistic bent, but please don't louse up the coherent sentences I wrote when moving them down ten lines.

Please elaborate on the rationale that justifies this change. I don't see it at all.

If you are already on the signal stack, it doesn't matter whether the signal that just arrived has SA_ONSTACK set or not. If you are going to overflow the stack with the new signal frame, we want to prevent that clobberation regardless.

Thanks,
Roland

author
#4 lore raw

Roland McGrath wrote::

Quoted 8 lines

This change looks bogus to me. Before I get to the content, there is a nit that annoys me. You changed the punctuation in my comment so that it no longer means what it did, and now the comment is nonsensical. I don't demand decent English from hackers of any linguistic bent, but please don't louse up the coherent sentences I wrote when moving them down ten lines.

Please elaborate on the rationale that justifies this change. I don't see it at all.

I have corrected the comment in the latest patch which has been apllied by Ingo. Please refer to http://lkml.org/lkml/2008/2/18/575 and http://lkml.org/lkml/2008/2/19/119 .

Thanks.
Shi Weihua

Quoted 12 lines

If you are already on the signal stack, it doesn't matter whether the signal that just arrived has SA_ONSTACK set or not. If you are going to overflow the stack with the new signal frame, we want to prevent that clobberation regardless.

Thanks,
Roland

author
#6 lore raw

Roland McGrath wrote::

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Please elaborate on the rationale that justifies this change. I don't see it at all.

I have corrected the comment in the latest patch which has been apllied by Ingo. Please refer to http://lkml.org/lkml/2008/2/18/575 and http://lkml.org/lkml/2008/2/19/119 .

You have yet to say anything to justify your change.

You mean the comment?

I mean I made a mistake about the comment so I changed it, and then Valdis.Kletnieks pointed it out for me. And late I sent a revised patch with your original comment untouched.

Sorry for my poor English. :(

Thanks
Shi Weihua

#7 lore raw
Quoted 1 line

You mean the comment?

No, that is trivial and already corrected. I mean the substance of your most recent patch. I described why I think it is wrong. You did not respond.

author
#8 lore raw

Roland McGrath wrote::

Quoted 4 lines
Quoted 1 line

You mean the comment?

No, that is trivial and already corrected. I mean the substance of your most recent patch. I described why I think it is wrong. You did not respond.

I spent some time read you mail carefully and dig into the code again.

And yes, you are right. It's possible that SA_ONSTACK has been cleared before the second signal on the same stack comes.

So this patch is wrong :( . I will revise the other 4 patches.

Sorry for the noise.

#9 lore raw
Quoted 4 lines

I spent some time read you mail carefully and dig into the code again.

And yes, you are right. It's possible that SA_ONSTACK has been cleared before the second signal on the same stack comes.

It's not necessary for SA_ONSTACK to have "been cleared", by which I assume you mean a sigaction call with SA_ONSTACK not set in sa_flags. That is indeed possible, but it's not the only case your patch broke. It can just be a different signal whose sigaction never had SA_ONSTACK, when you are still on the signal stack from an earlier signal that did have SA_ONSTACK.

Quoted 1 line

So this patch is wrong :( . I will revise the other 4 patches.

For 2 and 3, I would rather just wait until we unify signal.c anyway.

Thanks,
Roland

author
#10 lore raw

Roland McGrath wrote::

Quoted 10 lines
Quoted 4 lines

I spent some time read you mail carefully and dig into the code again.

And yes, you are right. It's possible that SA_ONSTACK has been cleared before the second signal on the same stack comes.

It's not necessary for SA_ONSTACK to have "been cleared", by which I assume you mean a sigaction call with SA_ONSTACK not set in sa_flags. That is indeed possible, but it's not the only case your patch broke. It can just be a different signal whose sigaction never had SA_ONSTACK, when you are still on the signal stack from an earlier signal that did have SA_ONSTACK.

Thanks for your explanation.

Quoted 4 lines
Quoted 2 lines

So this patch is wrong :( . I will revise the other 4 patches.

For 2 and 3, I would rather just wait until we unify signal.c anyway.

Ok. I see.

#11 lore raw
In reply to Roland McGrath
Quoted 11 lines · Roland McGrath
Quoted 4 lines

I spent some time read you mail carefully and dig into the code again.

And yes, you are right. It's possible that SA_ONSTACK has been cleared before the second signal on the same stack comes.

It's not necessary for SA_ONSTACK to have "been cleared", by which I assume you mean a sigaction call with SA_ONSTACK not set in sa_flags. That is indeed possible, but it's not the only case your patch broke. It can just be a different signal whose sigaction never had SA_ONSTACK, when you are still on the signal stack from an earlier signal that did have SA_ONSTACK.

Quoted 4 lines
Quoted 1 line

So this patch is wrong :( . I will revise the other 4 patches.

For 2 and 3, I would rather just wait until we unify signal.c anyway.

I've been looking at that, at the same time a bunch of ia32/signal.c looks like it can go away.

Harvey

#12 lore raw
Quoted 2 lines

I've been looking at that, at the same time a bunch of ia32/signal.c looks like it can go away.

Yes, I meant the 3 into 1 unification.

Thanks,
Roland

#13 lore raw
In reply to Roland McGrath
Quoted 12 lines · Roland McGrath
Quoted 4 lines

I spent some time read you mail carefully and dig into the code again.

And yes, you are right. It's possible that SA_ONSTACK has been cleared before the second signal on the same stack comes.

It's not necessary for SA_ONSTACK to have "been cleared", by which I assume you mean a sigaction call with SA_ONSTACK not set in sa_flags. That is indeed possible, but it's not the only case your patch broke. It can just be a different signal whose sigaction never had SA_ONSTACK, when you are still on the signal stack from an earlier signal that did have SA_ONSTACK.

Quoted 3 lines
Quoted 1 line

So this patch is wrong :( . I will revise the other 4 patches.

For 2 and 3, I would rather just wait until we unify signal.c anyway.

ok, i've removed these patches from x86.git#testing for now:

Subject: x86: improve the signal stack overflow logic, 32-bit
Subject: x86: add a signal stack overflow check, 64-bit
Subject: x86: add signal stack overflow check, 32-bit

and will wait for a resubmission and an Ack from Roland.

Ingo

#14 lore raw
In reply to Shi Weihua

arch/x86/kernel/signal_32.c get_sigframe(struct k_sigaction *ka, str · line 303

Quoted 2 lines · Shi Weihua
arch/x86/kernel/signal_32.c +0 -2
-	/*
-	 * If we are on the alternate signal stack and would overflow it, don't.

notice this ^

arch/x86/kernel/signal_32.c get_sigframe(struct k_sigaction *ka, str · line 311

Quoted 4 lines
arch/x86/kernel/signal_32.c +2 -1
-	 * Return an always-bogus address instead so we will die with SIGSEGV.
 
+			 * If we are on the alternate signal stack and would
+			 * overflow it, don't return an always-bogus address

missing here ^

arch/x86/kernel/signal_32.c get_sigframe(struct k_sigaction *ka, str · line 312

Quoted 1 line
arch/x86/kernel/signal_32.c +1 -0
+			 * instead so we will die with SIGSEGV.

"don't. Return" is a vastly different semantic than "don't return".

I think the same comment error was cut-n-pasted into all 5 patches...

author
#15 lore raw

[email protected] wrote::

Quoted 7 lines · Shi Weihua

arch/x86/kernel/signal_32.c get_sigframe(struct k_sigaction *ka, str · line 303

Quoted 3 lines
arch/x86/kernel/signal_32.c +0 -2
-	/*
-	 * If we are on the alternate signal stack and would overflow it, don't.

notice this ^

Quoted 1 line
arch/x86/kernel/signal_32.c +0 -1
-	 * Return an always-bogus address instead so we will die with SIGSEGV.
Quoted 9 lines

arch/x86/kernel/signal_32.c get_sigframe(struct k_sigaction *ka, str · line 311

Quoted 2 lines
arch/x86/kernel/signal_32.c +2 -0
+			 * If we are on the alternate signal stack and would
+			 * overflow it, don't return an always-bogus address

missing here ^

arch/x86/kernel/signal_32.c get_sigframe(struct k_sigaction *ka, str · line 312

Quoted 1 line
arch/x86/kernel/signal_32.c +1 -0
+			 * instead so we will die with SIGSEGV.

"don't. Return" is a vastly different semantic than "don't return".

I think the same comment error was cut-n-pasted into all 5 patches...

Sorry, it's my mistake.
I will correct the all 5 patches. Thanks.

author
#16 lore raw
In reply to Shi Weihua

We need to check for stack overflow only when the signal is on stack. So we can improve the patch "http://lkml.org/lkml/2007/11/27/101" as following.

---

The previous patch has a comment mistake. Now I correct it.

---

arch/x86/kernel/signal_32.c +12 -8
@@ -299,17 +299,21 @@ get_sigframe(struct k_sigaction *ka, str
 	/* Default to using normal stack */
 	sp = regs->sp;
 
-	/*
-	 * 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)
+		int onstack = sas_ss_flags(sp);
+
+		if (onstack == 0)
 			sp = current->sas_ss_sp + current->sas_ss_size;
+		else if (onstack == SS_ONSTACK) {
+			/*
+			 * 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 (!likely(on_sig_stack(sp - frame_size)))
+				return (void __user *) -1L;
+		}
 	}
 
 	/* This is the legacy signal stack switching. */
#18 lore raw
Quoted 8 lines · Ingo Molnar
Quoted 3 lines · Shi Weihua

We need to check for stack overflow only when the signal is on stack. So we can improve the patch "http://lkml.org/lkml/2007/11/27/101" as following.

thanks, applied.

These patches change the behaviour of programs that longjmp out of a signal handler on an alternate stack, don't they?

I'm interested to know what gave you confidence that changing that behaviour won't break existing working programs.

Paul.

#19 lore raw
Quoted 12 lines · Paul Mackerras
Quoted 4 lines · Ingo Molnar
Quoted 1 line · Shi Weihua

We need to check for stack overflow only when the signal is on stack. So we can improve the patch "http://lkml.org/lkml/2007/11/27/101" as following.

thanks, applied.

These patches change the behaviour of programs that longjmp out of a signal handler on an alternate stack, don't they?

I'm interested to know what gave you confidence that changing that behaviour won't break existing working programs.

Shouldn't such programs use sigsetjmp/siglongjmp, which should reset the signal stack state?

-hpa

#20 lore raw
Quoted 2 lines

Shouldn't such programs use sigsetjmp/siglongjmp, which should reset the signal stack state?

That is not really related. The distinction doesn't really exist for programs using the normal API (setjmp is sigsetjmp(,1)). What siglongjmp guarantees handled is signal mask changes, not sigaltstack.

Thanks,
Roland

#21 lore raw
In reply to Paul Mackerras
Quoted 2 lines

These patches change the behaviour of programs that longjmp out of a signal handler on an alternate stack, don't they?

No, they don't. Shi Weihua's original patch had that problem.

Thanks,
Roland