Skip to contentSkip to conversation

make sigaltstack compatible with swapcontext()

PATCH In review Stas Sergeev 4 participants 15 messages Apr 14, 2016 – May 3, 2016

Subsystems (6)
x86 Kselftest MM core Scheduler +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover make sigaltstack compatible with swapcontext()

The following patches make it possible to use swapcontext() in a sighandler that works on sigaltstack.
The approach is inspired by Andy Lutomirski's suggestion that sigaltstack should disarm itself after saving into uc_stack:
https://lkml.org/lkml/2016/2/1/594

I add the SS_AUTODISARM flag that does exactly that. On sighandler exit, the sigaltstack is restored from uc_stack. Another possible name could be SS_ONESHOT, but, since it gets always re-enabled, I choose SS_AUTODISARM.

Changes since previous resend:
- Added missing CCs, thanks for the hint Ingo

Change since v5:
- Fix description of patch 4/4

Change since v4:
- Implement this Andy Lutomirski's suggestion:
https://lkml.org/lkml/2016/3/6/158
that allows the run-time probing of the existence of the flags added in the future.

[PATCH 1/4] [Cleanup] x86: signal: unify the sigaltstack check with
A clean-up patch that unifies x86's sigaltstack handling with other arches.
[PATCH 2/4] sigaltstack: preparations for adding new SS_xxx flags
Andy's suggested changes
[PATCH 3/4] sigaltstack: implement SS_AUTODISARM flag
This patch implements SS_AUTODISARM flag
[PATCH 4/4] selftests: Add test for
This patch adds the selftest code for new functionality

CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Shuah Khan <[email protected]>
CC Ingo Molnar <[email protected]>

Diffstat:

 arch/x86/kernel/signal.c                     |   23 +--
 include/linux/sched.h                        |    8 +
 include/linux/signal.h                       |    4 
 include/uapi/linux/signal.h                  |    7 +
 kernel/fork.c                                |    2 
 kernel/signal.c                              |   26 ++--
 tools/testing/selftests/Makefile             |    1 
 tools/testing/selftests/sigaltstack/Makefile |    8 +
 tools/testing/selftests/sigaltstack/sas.c    |  156 +++++++++++++++++++++++++++
 9 files changed, 208 insertions(+), 27 deletions(-)
#1 lore raw

PATCH 1/4 x86: signal: unify the sigaltstack check with other arches

Currently x86's get_sigframe() checks for "current->sas_ss_size" to determine whether there is a need to switch to sigaltstack. The common practice used by all other arches is to check for sas_ss_flags(sp) == 0

This patch makes the code consistent with other arches.
The slight complexity of the patch is added by the optimization on !sigstack check that was requested by Andy Lutomirski: sas_ss_flags(sp)==0 already implies that we are not on a sigstack, so the code is shuffled to avoid the duplicate checking.

This patch have no any user-visible impact. It is purely a cleanup.

CC Andy Lutomirski <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Borislav Petkov <[email protected]>
CC Brian Gerst <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Richard Weinberger <[email protected]>
CC Shuah Khan <[email protected]>
---
 arch/x86/kernel/signal.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
arch/x86/kernel/signal.c +11 -12
index cb6282c..285183b 100644
@@ -213,18 +213,17 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
 	if (config_enabled(CONFIG_X86_64))
 		sp -= 128;
 
-	if (!onsigstack) {
-		/* This is the X/Open sanctioned signal stack switching.  */
-		if (ka->sa.sa_flags & SA_ONSTACK) {
-			if (current->sas_ss_size)
-				sp = current->sas_ss_sp + current->sas_ss_size;
-		} else if (config_enabled(CONFIG_X86_32) &&
-			   (regs->ss & 0xffff) != __USER_DS &&
-			   !(ka->sa.sa_flags & SA_RESTORER) &&
-			   ka->sa.sa_restorer) {
-				/* This is the legacy signal stack switching. */
-				sp = (unsigned long) ka->sa.sa_restorer;
-		}
+	/* 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 (config_enabled(CONFIG_X86_32) &&
+		   !onsigstack &&
+		   (regs->ss & 0xffff) != __USER_DS &&
+		   !(ka->sa.sa_flags & SA_RESTORER) &&
+		   ka->sa.sa_restorer) {
+		/* This is the legacy signal stack switching. */
+		sp = (unsigned long) ka->sa.sa_restorer;
 	}
 
 	if (fpu->fpstate_active) {
Signature · 1 line
2.7.2
#2 lore raw

[tip:core/signals] signals/sigaltstack, x86/signals: Unify the x86 sigaltstack check with other architectures

signals/sigaltstack, x86/signals: Unify the x86 sigaltstack check with other architectures
Commit-ID:  0b4521e8cf1f582da3045ea460427ac2f741578f
Gitweb:     http://git.kernel.org/tip/0b4521e8cf1f582da3045ea460427ac2f741578f
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:02 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:58 +0200

signals/sigaltstack, x86/signals: Unify the x86 sigaltstack check with other architectures

Currently x86's get_sigframe() checks for "current->sas_ss_size" to determine whether there is a need to switch to sigaltstack. The common practice used by all other arches is to check for sas_ss_flags(sp) == 0

This patch makes the code consistent with other architectures.

The slight complexity of the patch is added by the optimization on !sigstack check that was requested by Andy Lutomirski: sas_ss_flags(sp)==0 already implies that we are not on a sigstack, so the code is shuffled to avoid the duplicate checking.

This patch should have no user-visible impact.

Cc Al Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 arch/x86/kernel/signal.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
arch/x86/kernel/signal.c +11 -12
index 548ddf7..3e84ef1 100644
@@ -248,18 +248,17 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
 	if (config_enabled(CONFIG_X86_64))
 		sp -= 128;
 
-	if (!onsigstack) {
-		/* This is the X/Open sanctioned signal stack switching.  */
-		if (ka->sa.sa_flags & SA_ONSTACK) {
-			if (current->sas_ss_size)
-				sp = current->sas_ss_sp + current->sas_ss_size;
-		} else if (config_enabled(CONFIG_X86_32) &&
-			   (regs->ss & 0xffff) != __USER_DS &&
-			   !(ka->sa.sa_flags & SA_RESTORER) &&
-			   ka->sa.sa_restorer) {
-				/* This is the legacy signal stack switching. */
-				sp = (unsigned long) ka->sa.sa_restorer;
-		}
+	/* 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 (config_enabled(CONFIG_X86_32) &&
+		   !onsigstack &&
+		   (regs->ss & 0xffff) != __USER_DS &&
+		   !(ka->sa.sa_flags & SA_RESTORER) &&
+		   ka->sa.sa_restorer) {
+		/* This is the legacy signal stack switching. */
+		sp = (unsigned long) ka->sa.sa_restorer;
 	}
 
 	if (fpu->fpstate_active) {
#3 lore raw

Re: [tip:core/signals] signals/sigaltstack, x86/signals: Unify the x86 sigaltstack check with other architectures

Quoted 14 lines · tip-bot for Stas Sergeev
Commit-ID:  0b4521e8cf1f582da3045ea460427ac2f741578f
Gitweb:     http://git.kernel.org/tip/0b4521e8cf1f582da3045ea460427ac2f741578f
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:02 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:58 +0200

signals/sigaltstack, x86/signals: Unify the x86 sigaltstack check with other architectures

Currently x86's get_sigframe() checks for "current->sas_ss_size" to determine whether there is a need to switch to sigaltstack. The common practice used by all other arches is to check for sas_ss_flags(sp) == 0

Quoted 8 lines

This patch makes the code consistent with other architectures.

The slight complexity of the patch is added by the optimization on !sigstack check that was requested by Andy Lutomirski: sas_ss_flags(sp)==0 already implies that we are not on a sigstack, so the code is shuffled to avoid the duplicate checking.

This patch should have no user-visible impact.

I'm late to the party, but LGTM.

--Andy

#4 lore raw

PATCH 2/4 sigaltstack: preparations for adding new SS_xxx flags

In reply to Stas Sergeev

This patch adds SS_FLAG_BITS - the mask that splits sigaltstack mode values and bit-flags. Since there is no bit-flags yet, the mask is defined to 0. The flags are added by subsequent patches. With every new flag, the mask should have the appropriate bit cleared.

This makes sure if some flag is tried on a kernel that doesn't support it, the EINVAL error will be returned, because such a flag will be treated as an invalid mode rather than the bit-flag. That way the existence of the particular features can be probed at run-time.

This change was suggested by Andy Lutomirski:
https://lkml.org/lkml/2016/3/6/158

CC Andrew Morton <[email protected]>
CC Oleg Nesterov <[email protected]>
CC "Peter Zijlstra (Intel)" <[email protected]>
CC "Amanieu d'Antras" <[email protected]>
CC Michal Hocko <[email protected]>
CC Richard Weinberger <[email protected]>
CC Vladimir Davydov <[email protected]>
CC Sasha Levin <[email protected]>
CC Ingo Molnar <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Shuah Khan <[email protected]>
---
 include/uapi/linux/signal.h |  3 +++
 kernel/signal.c             | 16 ++++++----------
 2 files changed, 9 insertions(+), 10 deletions(-)
include/uapi/linux/signal.h +3 -0
index e1bd50c2..7c73165 100644
@@ -7,4 +7,7 @@
 #define SS_ONSTACK	1
 #define SS_DISABLE	2
 
+/* mask for all SS_xxx flags */
+#define SS_FLAG_BITS	0
+
 #endif /* _UAPI_LINUX_SIGNAL_H */
kernel/signal.c +6 -10
index 0508544..9a24bc3 100644
@@ -3100,7 +3100,8 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
 	if (uss) {
 		void __user *ss_sp;
 		size_t ss_size;
-		int ss_flags;
+		unsigned ss_flags;
+		int ss_mode;
 
 		error = -EFAULT;
 		if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
@@ -3115,18 +3116,13 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
 		if (on_sig_stack(sp))
 			goto out;
 
+		ss_mode = ss_flags & ~SS_FLAG_BITS;
 		error = -EINVAL;
-		/*
-		 * Note - this code used to test ss_flags incorrectly:
-		 *  	  old code may have been written using ss_flags==0
-		 *	  to mean ss_flags==SS_ONSTACK (as this was the only
-		 *	  way that worked) - this fix preserves that older
-		 *	  mechanism.
-		 */
-		if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
+		if (ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
+				ss_mode != 0)
 			goto out;
 
-		if (ss_flags == SS_DISABLE) {
+		if (ss_mode == SS_DISABLE) {
 			ss_size = 0;
 			ss_sp = NULL;
 		} else {
Signature · 1 line
2.7.2
#5 lore raw

[tip:core/signals] signals/sigaltstack: Prepare to add new SS_xxx flags

signals/sigaltstack: Prepare to add new SS_xxx flags
Commit-ID:  407bc16ad1769f5cb8ad9555611cb198187ef4cd
Gitweb:     http://git.kernel.org/tip/407bc16ad1769f5cb8ad9555611cb198187ef4cd
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:03 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

signals/sigaltstack: Prepare to add new SS_xxx flags

This patch adds SS_FLAG_BITS - the mask that splits sigaltstack mode values and bit-flags. Since there is no bit-flags yet, the mask is defined to 0. The flags are added by subsequent patches. With every new flag, the mask should have the appropriate bit cleared.

This makes sure if some flag is tried on a kernel that doesn't support it, the -EINVAL error will be returned, because such a flag will be treated as an invalid mode rather than the bit-flag.

That way the existence of the particular features can be probed at run-time.

This change was suggested by Andy Lutomirski:

https://lkml.org/lkml/2016/3/6/158

Cc Al Viro <[email protected]>
Cc Amanieu d'Antras <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vladimir Davydov <[email protected]>
---
 include/uapi/linux/signal.h |  3 +++
 kernel/signal.c             | 16 ++++++----------
 2 files changed, 9 insertions(+), 10 deletions(-)
include/uapi/linux/signal.h +3 -0
index e1bd50c2..7c73165 100644
@@ -7,4 +7,7 @@
 #define SS_ONSTACK	1
 #define SS_DISABLE	2
 
+/* mask for all SS_xxx flags */
+#define SS_FLAG_BITS	0
+
 #endif /* _UAPI_LINUX_SIGNAL_H */
kernel/signal.c +6 -10
index aa9bf00..b1c6eb4 100644
@@ -3104,7 +3104,8 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
 	if (uss) {
 		void __user *ss_sp;
 		size_t ss_size;
-		int ss_flags;
+		unsigned ss_flags;
+		int ss_mode;
 
 		error = -EFAULT;
 		if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
@@ -3119,18 +3120,13 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
 		if (on_sig_stack(sp))
 			goto out;
 
+		ss_mode = ss_flags & ~SS_FLAG_BITS;
 		error = -EINVAL;
-		/*
-		 * Note - this code used to test ss_flags incorrectly:
-		 *  	  old code may have been written using ss_flags==0
-		 *	  to mean ss_flags==SS_ONSTACK (as this was the only
-		 *	  way that worked) - this fix preserves that older
-		 *	  mechanism.
-		 */
-		if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
+		if (ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
+				ss_mode != 0)
 			goto out;
 
-		if (ss_flags == SS_DISABLE) {
+		if (ss_mode == SS_DISABLE) {
 			ss_size = 0;
 			ss_sp = NULL;
 		} else {
maintainer
#6 lore raw

Re: [tip:core/signals] signals/sigaltstack: Prepare to add new SS_xxx flags

Quoted 18 lines · tip-bot for Stas Sergeev
Commit-ID:  407bc16ad1769f5cb8ad9555611cb198187ef4cd
Gitweb:     http://git.kernel.org/tip/407bc16ad1769f5cb8ad9555611cb198187ef4cd
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:03 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

signals/sigaltstack: Prepare to add new SS_xxx flags

This patch adds SS_FLAG_BITS - the mask that splits sigaltstack mode values and bit-flags. Since there is no bit-flags yet, the mask is defined to 0. The flags are added by subsequent patches. With every new flag, the mask should have the appropriate bit cleared.

This makes sure if some flag is tried on a kernel that doesn't support it, the -EINVAL error will be returned, because such a flag will be treated as an invalid mode rather than the bit-flag.

Quoted 6 lines

That way the existence of the particular features can be probed at run-time.

This change was suggested by Andy Lutomirski:

https://lkml.org/lkml/2016/3/6/158

Please use the message ID in the future

Message-ID: <CALCETrW=M1=n6R6dwOj-ks_=+14vf4rz0S3SFty-_GuwFFZU5A@mail.gmail.com>

to refer to public mails because random web services caching lkml tend to become unreliable or even disappear at some point.

I.e., see Link: format below for an example.

Quoted 24 lines
Cc Al Viro <[email protected]>
Cc Amanieu d'Antras <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vladimir Davydov <[email protected]>
Quoted 4 lines
---
 include/uapi/linux/signal.h |  3 +++
 kernel/signal.c             | 16 ++++++----------
 2 files changed, 9 insertions(+), 10 deletions(-)

Thanks.

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
#7 lore raw

Re: [tip:core/signals] signals/sigaltstack: Prepare to add new SS_xxx flags

In reply to tip-bot for Stas Sergeev
Quoted 18 lines · tip-bot for Stas Sergeev
Commit-ID:  407bc16ad1769f5cb8ad9555611cb198187ef4cd
Gitweb:     http://git.kernel.org/tip/407bc16ad1769f5cb8ad9555611cb198187ef4cd
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:03 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

signals/sigaltstack: Prepare to add new SS_xxx flags

This patch adds SS_FLAG_BITS - the mask that splits sigaltstack mode values and bit-flags. Since there is no bit-flags yet, the mask is defined to 0. The flags are added by subsequent patches. With every new flag, the mask should have the appropriate bit cleared.

This makes sure if some flag is tried on a kernel that doesn't support it, the -EINVAL error will be returned, because such a flag will be treated as an invalid mode rather than the bit-flag.

Quoted 6 lines

That way the existence of the particular features can be probed at run-time.

This change was suggested by Andy Lutomirski:

https://lkml.org/lkml/2016/3/6/158

LGTM.

#8 lore raw

Re: [tip:core/signals] signals/sigaltstack: Prepare to add new SS_xxx flags

Quoted 20 lines · Andy Lutomirski
Quoted 18 lines · tip-bot for Stas Sergeev
Commit-ID:  407bc16ad1769f5cb8ad9555611cb198187ef4cd
Gitweb:     http://git.kernel.org/tip/407bc16ad1769f5cb8ad9555611cb198187ef4cd
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:03 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

signals/sigaltstack: Prepare to add new SS_xxx flags

This patch adds SS_FLAG_BITS - the mask that splits sigaltstack mode values and bit-flags. Since there is no bit-flags yet, the mask is defined to 0. The flags are added by subsequent patches. With every new flag, the mask should have the appropriate bit cleared.

This makes sure if some flag is tried on a kernel that doesn't support it, the -EINVAL error will be returned, because such a flag will be treated as an invalid mode rather than the bit-flag.

Quoted 8 lines
Quoted 6 lines

That way the existence of the particular features can be probed at run-time.

This change was suggested by Andy Lutomirski:

https://lkml.org/lkml/2016/3/6/158

LGTM.

Correction: does not LGTM. In a sensible design, sigaltstack would report flags back to the caller. I will send a fix.

--Andy

#9 lore raw

PATCH 3/4 sigaltstack: implement SS_AUTODISARM flag

In reply to Stas Sergeev

This patch implements the SS_AUTODISARM flag that can be ORed with SS_ONSTACK when forming ss_flags.
When this flag is set, sigaltstack will be disabled when entering the signal handler; more precisely, after saving sas to uc_stack. When leaving the signal handler, the sigaltstack is restored by uc_stack.
When this flag is used, it is safe to switch from sighandler with swapcontext(). Without this flag, the subsequent signal will corrupt the state of the switched-away sighandler.

To detect the support of this functionality, one can do err = sigaltstack(SS_DISABLE | SS_AUTODISARM); if (err && errno == EINVAL) unsupported();

CC Ingo Molnar <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Richard Weinberger <[email protected]>
CC Andrew Morton <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Tejun Heo <[email protected]>
CC Heinrich Schuchardt <[email protected]>
CC Jason Low <[email protected]>
CC Andrea Arcangeli <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Konstantin Khlebnikov <[email protected]>
CC Josh Triplett <[email protected]>
CC "Eric W. Biederman" <[email protected]>
CC Aleksa Sarai <[email protected]>
CC "Amanieu d'Antras" <[email protected]>
CC Paul Moore <[email protected]>
CC Sasha Levin <[email protected]>
CC Palmer Dabbelt <[email protected]>
CC Vladimir Davydov <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Shuah Khan <[email protected]>
---
 include/linux/sched.h       |  8 ++++++++
 include/linux/signal.h      |  4 +++-
 include/uapi/linux/signal.h |  4 +++-
 kernel/fork.c               |  2 +-
 kernel/signal.c             | 10 ++++++++--
 5 files changed, 23 insertions(+), 5 deletions(-)
include/linux/sched.h +8 -0
index a10494a..26201cd 100644
@@ -1587,6 +1587,7 @@ struct task_struct {
 
 	unsigned long sas_ss_sp;
 	size_t sas_ss_size;
+	unsigned sas_ss_flags;
 
 	struct callback_head *task_works;
 
@@ -2573,6 +2574,13 @@ static inline int sas_ss_flags(unsigned long sp)
 	return on_sig_stack(sp) ? SS_ONSTACK : 0;
 }
 
+static inline void sas_ss_reset(struct task_struct *p)
+{
+	p->sas_ss_sp = 0;
+	p->sas_ss_size = 0;
+	p->sas_ss_flags = SS_DISABLE;
+}
+
 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
 {
 	if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
include/linux/signal.h +3 -1
index 92557bb..3fbe814 100644
@@ -432,8 +432,10 @@ int __save_altstack(stack_t __user *, unsigned long);
 	stack_t __user *__uss = uss; \
 	struct task_struct *t = current; \
 	put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
-	put_user_ex(sas_ss_flags(sp), &__uss->ss_flags); \
+	put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
 	put_user_ex(t->sas_ss_size, &__uss->ss_size); \
+	if (t->sas_ss_flags & SS_AUTODISARM) \
+		sas_ss_reset(t); \
 } while (0);
 
 #ifdef CONFIG_PROC_FS
include/uapi/linux/signal.h +3 -1
index 7c73165..7388260 100644
@@ -7,7 +7,9 @@
 #define SS_ONSTACK	1
 #define SS_DISABLE	2
 
+/* bit-flags */
+#define SS_AUTODISARM	(1 << 4)	/* disable sas during sighandling */
 /* mask for all SS_xxx flags */
-#define SS_FLAG_BITS	0
+#define SS_FLAG_BITS	SS_AUTODISARM
 
 #endif /* _UAPI_LINUX_SIGNAL_H */
kernel/fork.c +1 -1
index 2e391c7..68c8716 100644
@@ -1483,7 +1483,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	 * sigaltstack should be cleared when sharing the same VM
 	 */
 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
-		p->sas_ss_sp = p->sas_ss_size = 0;
+		sas_ss_reset(p);
 
 	/*
 	 * Syscall tracing and stepping should be turned off in the
kernel/signal.c +8 -2
index 9a24bc3..e1096ad 100644
@@ -3133,6 +3133,7 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
 
 		current->sas_ss_sp = (unsigned long) ss_sp;
 		current->sas_ss_size = ss_size;
+		current->sas_ss_flags = ss_flags;
 	}
 
 	error = 0;
@@ -3163,9 +3164,14 @@ int restore_altstack(const stack_t __user *uss)
 int __save_altstack(stack_t __user *uss, unsigned long sp)
 {
 	struct task_struct *t = current;
-	return  __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
-		__put_user(sas_ss_flags(sp), &uss->ss_flags) |
+	int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
+		__put_user(t->sas_ss_flags, &uss->ss_flags) |
 		__put_user(t->sas_ss_size, &uss->ss_size);
+	if (err)
+		return err;
+	if (t->sas_ss_flags & SS_AUTODISARM)
+		sas_ss_reset(t);
+	return 0;
 }
 
 #ifdef CONFIG_COMPAT
Signature · 1 line
2.7.2
#10 lore raw

[tip:core/signals] signals/sigaltstack: Implement SS_AUTODISARM flag

signals/sigaltstack: Implement SS_AUTODISARM flag
Commit-ID:  2a74213838104a41588d86fd5e8d344972891ace
Gitweb:     http://git.kernel.org/tip/2a74213838104a41588d86fd5e8d344972891ace
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:04 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

signals/sigaltstack: Implement SS_AUTODISARM flag

This patch implements the SS_AUTODISARM flag that can be OR-ed with SS_ONSTACK when forming ss_flags.

When this flag is set, sigaltstack will be disabled when entering the signal handler; more precisely, after saving sas to uc_stack. When leaving the signal handler, the sigaltstack is restored by uc_stack.

When this flag is used, it is safe to switch from sighandler with swapcontext(). Without this flag, the subsequent signal will corrupt the state of the switched-away sighandler.

To detect the support of this functionality, one can do:

err = sigaltstack(SS_DISABLE | SS_AUTODISARM);
if (err && errno == EINVAL)

	unsupported();
Cc Al Viro <[email protected]>
Cc Aleksa Sarai <[email protected]>
Cc Amanieu d'Antras <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Eric W. Biederman <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Jason Low <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Konstantin Khlebnikov <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Paul Moore <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vladimir Davydov <[email protected]>
---
 include/linux/sched.h       |  8 ++++++++
 include/linux/signal.h      |  4 +++-
 include/uapi/linux/signal.h |  4 +++-
 kernel/fork.c               |  2 +-
 kernel/signal.c             | 10 ++++++++--
 5 files changed, 23 insertions(+), 5 deletions(-)
include/linux/sched.h +8 -0
index 52c4847..2950c5c 100644
@@ -1596,6 +1596,7 @@ struct task_struct {
 
 	unsigned long sas_ss_sp;
 	size_t sas_ss_size;
+	unsigned sas_ss_flags;
 
 	struct callback_head *task_works;
 
@@ -2592,6 +2593,13 @@ static inline int sas_ss_flags(unsigned long sp)
 	return on_sig_stack(sp) ? SS_ONSTACK : 0;
 }
 
+static inline void sas_ss_reset(struct task_struct *p)
+{
+	p->sas_ss_sp = 0;
+	p->sas_ss_size = 0;
+	p->sas_ss_flags = SS_DISABLE;
+}
+
 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
 {
 	if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
include/linux/signal.h +3 -1
index 92557bb..3fbe814 100644
@@ -432,8 +432,10 @@ int __save_altstack(stack_t __user *, unsigned long);
 	stack_t __user *__uss = uss; \
 	struct task_struct *t = current; \
 	put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
-	put_user_ex(sas_ss_flags(sp), &__uss->ss_flags); \
+	put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
 	put_user_ex(t->sas_ss_size, &__uss->ss_size); \
+	if (t->sas_ss_flags & SS_AUTODISARM) \
+		sas_ss_reset(t); \
 } while (0);
 
 #ifdef CONFIG_PROC_FS
include/uapi/linux/signal.h +3 -1
index 7c73165..7388260 100644
@@ -7,7 +7,9 @@
 #define SS_ONSTACK	1
 #define SS_DISABLE	2
 
+/* bit-flags */
+#define SS_AUTODISARM	(1 << 4)	/* disable sas during sighandling */
 /* mask for all SS_xxx flags */
-#define SS_FLAG_BITS	0
+#define SS_FLAG_BITS	SS_AUTODISARM
 
 #endif /* _UAPI_LINUX_SIGNAL_H */
kernel/fork.c +1 -1
index d277e83..3e84515 100644
@@ -1494,7 +1494,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	 * sigaltstack should be cleared when sharing the same VM
 	 */
 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
-		p->sas_ss_sp = p->sas_ss_size = 0;
+		sas_ss_reset(p);
 
 	/*
 	 * Syscall tracing and stepping should be turned off in the
kernel/signal.c +8 -2
index b1c6eb4..bf97ea5 100644
@@ -3137,6 +3137,7 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
 
 		current->sas_ss_sp = (unsigned long) ss_sp;
 		current->sas_ss_size = ss_size;
+		current->sas_ss_flags = ss_flags;
 	}
 
 	error = 0;
@@ -3167,9 +3168,14 @@ int restore_altstack(const stack_t __user *uss)
 int __save_altstack(stack_t __user *uss, unsigned long sp)
 {
 	struct task_struct *t = current;
-	return  __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
-		__put_user(sas_ss_flags(sp), &uss->ss_flags) |
+	int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
+		__put_user(t->sas_ss_flags, &uss->ss_flags) |
 		__put_user(t->sas_ss_size, &uss->ss_size);
+	if (err)
+		return err;
+	if (t->sas_ss_flags & SS_AUTODISARM)
+		sas_ss_reset(t);
+	return 0;
 }
 
 #ifdef CONFIG_COMPAT
#11 lore raw

Re: [tip:core/signals] signals/sigaltstack: Implement SS_AUTODISARM flag

Quoted 21 lines · tip-bot for Stas Sergeev
Commit-ID:  2a74213838104a41588d86fd5e8d344972891ace
Gitweb:     http://git.kernel.org/tip/2a74213838104a41588d86fd5e8d344972891ace
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:04 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

signals/sigaltstack: Implement SS_AUTODISARM flag

This patch implements the SS_AUTODISARM flag that can be OR-ed with SS_ONSTACK when forming ss_flags.

When this flag is set, sigaltstack will be disabled when entering the signal handler; more precisely, after saving sas to uc_stack. When leaving the signal handler, the sigaltstack is restored by uc_stack.

When this flag is used, it is safe to switch from sighandler with swapcontext(). Without this flag, the subsequent signal will corrupt the state of the switched-away sighandler.

Quoted 5 lines

To detect the support of this functionality, one can do:

  err = sigaltstack(SS_DISABLE | SS_AUTODISARM);
  if (err && errno == EINVAL)
        unsupported();

One of my review comments from last time got lost, I think. I'll send a followup patch.

include/uapi/linux/signal.h line 11

Quoted 2 lines
include/uapi/linux/signal.h +2 -0
+/* bit-flags */
+#define SS_AUTODISARM  (1 << 4)        /* disable sas during sighandling */

Before this gets enshrined as ABI, could we perhaps change this to (1 << 31)? I don't see why we should pick a bit in the middle of the field as our first flag bit.

--Andy

#12 lore raw

PATCH 4/4 selftests: Add test for sigaltstack(SS_ONSTACK|SS_AUTODISARM)

In reply to Stas Sergeev

This patch adds the test case for SS_AUTODISARM flag. The test-case tries to set SS_AUTODISARM flag and checks if the nested signal corrupts the stack after swapcontext().

CC Shuah Khan <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Ingo Molnar <[email protected]>
CC Oleg Nesterov <[email protected]>
---
 tools/testing/selftests/Makefile             |   1 +
 tools/testing/selftests/sigaltstack/Makefile |   8 ++
 tools/testing/selftests/sigaltstack/sas.c    | 156 +++++++++++++++++++++++++++
 3 files changed, 165 insertions(+)
 create mode 100644 tools/testing/selftests/sigaltstack/Makefile
 create mode 100644 tools/testing/selftests/sigaltstack/sas.c
tools/testing/selftests/Makefile +1 -0
index b04afc3..ff9e5f2 100644
@@ -19,6 +19,7 @@ TARGETS += powerpc
 TARGETS += pstore
 TARGETS += ptrace
 TARGETS += seccomp
+TARGETS += sigaltstack
 TARGETS += size
 TARGETS += static_keys
 TARGETS += sysctl
tools/testing/selftests/sigaltstack/Makefile new file +8 -0
new file mode 100644
index 0000000..56af56e
@@ -0,0 +1,8 @@
+CFLAGS = -Wall
+BINARIES = sas
+all: $(BINARIES)
+
+include ../lib.mk
+
+clean:
+	rm -rf $(BINARIES)
tools/testing/selftests/sigaltstack/sas.c new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
2.7.2
#13 lore raw

[tip:core/signals] selftests/sigaltstack: Add new testcase for sigaltstack(SS_ONSTACK|SS_AUTODISARM)

selftests/sigaltstack: Add new testcase for sigaltstack(SS_ONSTACK|SS_AUTODISARM)
Commit-ID:  19fd2868e3671b446b13d135a44363182bbd319a
Gitweb:     http://git.kernel.org/tip/19fd2868e3671b446b13d135a44363182bbd319a
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:05 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

selftests/sigaltstack: Add new testcase for sigaltstack(SS_ONSTACK|SS_AUTODISARM)

This patch adds the test case for SS_AUTODISARM flag. The test-case tries to set SS_AUTODISARM flag and checks if the nested signal corrupts the stack after swapcontext().

Cc Al Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 tools/testing/selftests/Makefile             |   1 +
 tools/testing/selftests/sigaltstack/Makefile |   8 ++
 tools/testing/selftests/sigaltstack/sas.c    | 156 +++++++++++++++++++++++++++
 3 files changed, 165 insertions(+)
tools/testing/selftests/Makefile +1 -0
index b04afc3..ff9e5f2 100644
@@ -19,6 +19,7 @@ TARGETS += powerpc
 TARGETS += pstore
 TARGETS += ptrace
 TARGETS += seccomp
+TARGETS += sigaltstack
 TARGETS += size
 TARGETS += static_keys
 TARGETS += sysctl
tools/testing/selftests/sigaltstack/Makefile new file +8 -0
new file mode 100644
index 0000000..56af56e
@@ -0,0 +1,8 @@
+CFLAGS = -Wall
+BINARIES = sas
+all: $(BINARIES)
+
+include ../lib.mk
+
+clean:
+	rm -rf $(BINARIES)
tools/testing/selftests/sigaltstack/sas.c new file +156 -0

156 lines not on this page · raw message

#14 lore raw

Re: [tip:core/signals] selftests/sigaltstack: Add new testcase for sigaltstack(SS_ONSTACK|SS_AUTODISARM)

Quoted 14 lines · tip-bot for Stas Sergeev
Commit-ID:  19fd2868e3671b446b13d135a44363182bbd319a
Gitweb:     http://git.kernel.org/tip/19fd2868e3671b446b13d135a44363182bbd319a
Author:     Stas Sergeev <[email protected]>
AuthorDate: Thu, 14 Apr 2016 23:20:05 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 3 May 2016 08:37:59 +0200

selftests/sigaltstack: Add new testcase for sigaltstack(SS_ONSTACK|SS_AUTODISARM)

This patch adds the test case for SS_AUTODISARM flag. The test-case tries to set SS_AUTODISARM flag and checks if the nested signal corrupts the stack after swapcontext().

tools/testing/selftests/sigaltstack/sas.c line 121

Quoted 7 lines
tools/testing/selftests/sigaltstack/sas.c new file +7 -0
+       stk.ss_sp = sstack;
+       stk.ss_size = SIGSTKSZ;
+       stk.ss_flags = SS_ONSTACK | SS_AUTODISARM;
+       err = sigaltstack(&stk, NULL);
+       if (err) {
+               perror("[FAIL]\tsigaltstack(SS_ONSTACK | SS_AUTODISARM)");
+               stk.ss_flags = SS_ONSTACK;

This code path here is wrong. I'll send a fix.

--Andy