Skip to contentSkip to conversation

make sigaltstack() compatible with swapcontext()

PATCH Superseded Stas Sergeev 3 participants 13 messages Feb 29, 2016 – Mar 6, 2016

Subsystems (5)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 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.

[PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag
This patch implements SS_AUTODISARM flag
[PATCH 2/2] selftests: Add test for sigaltstack(SS_AUTODISARM)
This patch adds the selftest code for new functionality

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

Diffstat:

 include/linux/sched.h                        |    8 +
 include/linux/signal.h                       |    4 
 include/uapi/linux/signal.h                  |    3 
 kernel/fork.c                                |    2 
 kernel/signal.c                              |   23 ++--
 tools/testing/selftests/Makefile             |    1 
 tools/testing/selftests/sigaltstack/Makefile |    8 +
 tools/testing/selftests/sigaltstack/sas.c    |  151 +++++++++++++++++++++++++++
 8 files changed, 187 insertions(+), 13 deletions(-)
#1 lore raw

PATCH v4 1/2 sigaltstack: implement SS_AUTODISARM flag

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.

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]>
---
 include/linux/sched.h       |  8 ++++++++
 include/linux/signal.h      |  4 +++-
 include/uapi/linux/signal.h |  3 +++
 kernel/fork.c               |  2 +-
 kernel/signal.c             | 23 ++++++++++++-----------
 5 files changed, 27 insertions(+), 13 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 -0
index e1bd50c2..4691bc5 100644
@@ -6,5 +6,8 @@
 
 #define SS_ONSTACK	1
 #define SS_DISABLE	2
+#define SS_VALMASK	0xf
+/* bit-flags */
+#define SS_AUTODISARM	(1 << 4)	/* disable sas during sighandling */
 
 #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 +12 -11
index 0508544..fab6bea 100644
@@ -3100,13 +3100,14 @@ 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;
+		unsigned ss_xflags;
 
 		error = -EFAULT;
 		if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
 			goto out;
 		error = __get_user(ss_sp, &uss->ss_sp) |
-			__get_user(ss_flags, &uss->ss_flags) |
+			__get_user(ss_xflags, &uss->ss_flags) |
 			__get_user(ss_size, &uss->ss_size);
 		if (error)
 			goto out;
@@ -3115,14 +3116,8 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
 		if (on_sig_stack(sp))
 			goto out;
 
+		ss_flags = ss_xflags & SS_VALMASK;
 		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)
 			goto out;
 
@@ -3137,6 +3132,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_xflags;
 	}
 
 	error = 0;
@@ -3167,9 +3163,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
#9 lore raw

PATCH v4 2/2 selftests: Add test for sigaltstack(SS_AUTODISARM)

In reply to Stas Sergeev

From: Stas Sergeev <[email protected]>

sigaltstack needs to be disabled before the signal handler can safely use swapcontext().
This patch adds the SS_AUTODISARM flag.
This flag disables the sigaltstack when entering the signal handler. When returning from signal handler, the sigaltstack is restored by uc_stack.

---
 tools/testing/selftests/Makefile             |   1 +
 tools/testing/selftests/sigaltstack/Makefile |   8 ++
 tools/testing/selftests/sigaltstack/sas.c    | 151 +++++++++++++++++++++++++++
 3 files changed, 160 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 +151 -0

151 lines not on this page · raw message

Signature · 1 line
2.7.2
#2 lore raw

Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

In reply to Stas Sergeev
Quoted 10 lines · 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.

This looks reasonable to me with one exception: how does a user program detect the presence of this feature? Anyone else have any thoughts?

#3 lore raw

Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

05.03.2016 01:22, Andy Lutomirski пишет:

Quoted 11 lines · Stas Sergeev
Quoted 10 lines

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.

Quoted 2 lines

This looks reasonable to me with one exception: how does a user program detect the presence of this feature?

Compile-time detection:
#ifdef SS_AUTODISARM
# I have this feature
...
#endif

Run-time detection:
int err = sigaltstack(SS_ONSTACK | SS_AUTODISARM); if (err == EINVAL) {
i_dont_have_this_feature = 1;
err = sigaltstack(SS_ONSTACK);
}

Note: if you want to keep such detection for the future additions, the mask can be enlarged to, say, ((1 << 24) - 1), and whenever someone adds a new flag, he can lower the mask by one bit.
But I think this would be an overkill in that particular case.

#4 lore raw

Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

05.03.2016 10:39, Stas Sergeev пишет:

Quoted 20 lines

05.03.2016 01:22, Andy Lutomirski пишет:

Quoted 11 lines · Stas Sergeev
Quoted 10 lines

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.

Quoted 2 lines

This looks reasonable to me with one exception: how does a user program detect the presence of this feature?

Compile-time detection:
#ifdef SS_AUTODISARM
# I have this feature
...
#endif

Quoted 3 lines

Run-time detection:
int err = sigaltstack(SS_ONSTACK | SS_AUTODISARM); if (err == EINVAL) {

I guess I mean here
if (err && errno == EINVAL)

#10 lore raw
In reply to Stas Sergeev
Quoted 2 lines · Stas Sergeev

The following patches make it possible to use swapcontext() in a sighandler that works on sigaltstack.

i don't think that's possible, the (obsolete) userspace *context functions cannot operate on kernel provided ucontext_t structs, so they are not usable in signal handlers.

at least not on most target archs.

so i don't understand how the tests can work portably.

Quoted 35 lines

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.

[PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag
This patch implements SS_AUTODISARM flag
[PATCH 2/2] selftests: Add test for sigaltstack(SS_AUTODISARM)
This patch adds the selftest code for new functionality

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

Diffstat:

 include/linux/sched.h                        |    8 +
 include/linux/signal.h                       |    4 
 include/uapi/linux/signal.h                  |    3 
 kernel/fork.c                                |    2 
 kernel/signal.c                              |   23 ++--
 tools/testing/selftests/Makefile             |    1 
 tools/testing/selftests/sigaltstack/Makefile |    8 +
 tools/testing/selftests/sigaltstack/sas.c    |  151 +++++++++++++++++++++++++++
 8 files changed, 187 insertions(+), 13 deletions(-)

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

In reply to Stas Sergeev
Quoted 89 lines · 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.

CC Ingo Molnar <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Richard Weinberger <richard-/[email protected]>
CC Andrew Morton <akpm-de/[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" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
CC Aleksa Sarai <cyphar-gVpy/LI/[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]>
---
 include/linux/sched.h       |  8 ++++++++
 include/linux/signal.h      |  4 +++-
 include/uapi/linux/signal.h |  3 +++
 kernel/fork.c               |  2 +-
 kernel/signal.c             | 23 ++++++++++++-----------
 5 files changed, 27 insertions(+), 13 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 +0 -0
index e1bd50c2..4691bc5 100644

include/uapi/linux/signal.h line 9

Quoted 4 lines
include/uapi/linux/signal.h +1 -0
@@ -6,5 +6,8 @@
 
 #define SS_ONSTACK     1
 #define SS_DISABLE     2
+#define SS_VALMASK     0xf

SS_MODE_MASK, perhaps?

Quoted 22 lines
kernel/signal.c +3 -2
index 0508544..fab6bea 100644
@@ -3100,13 +3100,14 @@ 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;
+               unsigned ss_xflags;
 
                error = -EFAULT;
                if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
                        goto out;
                error = __get_user(ss_sp, &uss->ss_sp) |
-                       __get_user(ss_flags, &uss->ss_flags) |
+                       __get_user(ss_xflags, &uss->ss_flags) |
                        __get_user(ss_size, &uss->ss_size);
                if (error)
                        goto out;

kernel/signal.c do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s · line 3119

Quoted 4 lines
kernel/signal.c +1 -0
@@ -3115,14 +3116,8 @@ do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long s
                if (on_sig_stack(sp))
                        goto out;
 
+               ss_flags = ss_xflags & SS_VALMASK;

This is poorly named. How about "ss_mode = ss_flags & SS_MODE_MASK";

--Andy

#6 lore raw

Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

Quoted 91 lines · Andy Lutomirski
Quoted 90 lines · 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.

CC Ingo Molnar <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Richard Weinberger <richard-/[email protected]>
CC Andrew Morton <akpm-de/[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" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
CC Aleksa Sarai <cyphar-gVpy/LI/[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]>
---
 include/linux/sched.h       |  8 ++++++++
 include/linux/signal.h      |  4 +++-
 include/uapi/linux/signal.h |  3 +++
 kernel/fork.c               |  2 +-
 kernel/signal.c             | 23 ++++++++++++-----------
 5 files changed, 27 insertions(+), 13 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 +0 -0
index e1bd50c2..4691bc5 100644
@@ -6,5 +6,8 @@
 
Quoted 5 lines

include/uapi/linux/signal.h line 9

Quoted 3 lines
include/uapi/linux/signal.h +1 -0
 #define SS_ONSTACK     1
 #define SS_DISABLE     2
+#define SS_VALMASK     0xf

SS_MODE_MASK, perhaps?

Actually, let's invert that.

#define SS_AUTODISARM (1U << 31)
#define SS_FLAG_BITS SS_AUTODISARM

ss_mode = ss_flags & ~SS_FLAG_BITS;

this way flag bits that are currently undefined will continue to trigger EINVAL.

--Andy

#7 lore raw

Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

06.03.2016 23:10, Andy Lutomirski пишет:

Quoted 96 lines · Andy Lutomirski
Quoted 91 lines · Stas Sergeev
Quoted 90 lines

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.

CC Ingo Molnar <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Richard Weinberger <richard-/[email protected]>
CC Andrew Morton <akpm-de/[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" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
CC Aleksa Sarai <cyphar-gVpy/LI/[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]>
---
  include/linux/sched.h       |  8 ++++++++
  include/linux/signal.h      |  4 +++-
  include/uapi/linux/signal.h |  3 +++
  kernel/fork.c               |  2 +-
  kernel/signal.c             | 23 ++++++++++++-----------
  5 files changed, 27 insertions(+), 13 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 +0 -0
index e1bd50c2..4691bc5 100644
@@ -6,5 +6,8 @@
 
Quoted 4 lines

include/uapi/linux/signal.h line 9

Quoted 3 lines
include/uapi/linux/signal.h +1 -0
  #define SS_ONSTACK     1
  #define SS_DISABLE     2
+#define SS_VALMASK     0xf

SS_MODE_MASK, perhaps?

Quoted 8 lines

Actually, let's invert that.

#define SS_AUTODISARM (1U << 31)
#define SS_FLAG_BITS SS_AUTODISARM

ss_mode = ss_flags & ~SS_FLAG_BITS;

this way flag bits that are currently undefined will continue to trigger EINVAL.

OK, will do.
Though I am still going to keep SS_AUTODISARM =(1 << 4) - it doesn't matter from what side we add new flags, does it?

#11 lore raw
In reply to Szabolcs Nagy

06.03.2016 23:02, Szabolcs Nagy пишет:

Quoted 6 lines · Stas Sergeev
Quoted 2 lines

The following patches make it possible to use swapcontext() in a sighandler that works on sigaltstack.

i don't think that's possible, the (obsolete) userspace *context functions cannot operate on kernel provided ucontext_t structs,

It doesn't have to.
It does the normal getcontext()/swapcontext() sequences,
as if it would be switching from any casual code place. Kernel-provided context structs are completely irrelevant. For more details please see the test-case I posted in that patch series. It shows what can be done and how.

Quoted 2 lines

so they are not usable in signal
handlers.

Please note the fact that swapcontext() also replaces the signal mask as part of the context switching. This is a very strong hint that it was intended to work with signal handlers. And in fact it perfectly does.
What I want is only to make it friendly to sigaltstack. I am not making it friendly to signal handlers in general, because it already is.

Quoted 3 lines

at least not on most target archs.

so i don't understand how the tests can work portably.

If you apply my patch series, you can immediately test it on any arch you want, thanks to the test-case my patch adds to the kernel. Please let me know on what arch it fails.

#12 lore raw
In reply to Szabolcs Nagy
Quoted 4 lines · Szabolcs Nagy
Quoted 2 lines · Stas Sergeev

The following patches make it possible to use swapcontext() in a sighandler that works on sigaltstack.

Quoted 9 lines

i don't think that's possible, the (obsolete) userspace *context functions cannot operate on kernel provided ucontext_t structs, so they are not usable in signal handlers.

at least not on most target archs.

so i don't understand how the tests can work portably.

i see the test does not use the kernel ucontext_t
and only synchronous context switch is done

sorry for the noise.

#8 lore raw

Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

In reply to Stas Sergeev
Quoted 111 lines · Stas Sergeev

06.03.2016 23:10, Andy Lutomirski пишет:

Quoted 109 lines
Quoted 96 lines · Andy Lutomirski
Quoted 94 lines · 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.

CC Ingo Molnar <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Richard Weinberger <richard-/[email protected]>
CC Andrew Morton <akpm-de/[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" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
CC Aleksa Sarai <cyphar-gVpy/LI/[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]>
---
  include/linux/sched.h       |  8 ++++++++
  include/linux/signal.h      |  4 +++-
  include/uapi/linux/signal.h |  3 +++
  kernel/fork.c               |  2 +-
  kernel/signal.c             | 23 ++++++++++++-----------
  5 files changed, 27 insertions(+), 13 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 +0 -0
index e1bd50c2..4691bc5 100644
@@ -6,5 +6,8 @@
 
Quoted 5 lines

include/uapi/linux/signal.h line 9

Quoted 3 lines
include/uapi/linux/signal.h +1 -0
  #define SS_ONSTACK     1
  #define SS_DISABLE     2
+#define SS_VALMASK     0xf

SS_MODE_MASK, perhaps?

Actually, let's invert that.

#define SS_AUTODISARM (1U << 31)
#define SS_FLAG_BITS SS_AUTODISARM

Quoted 8 lines
Quoted 4 lines

ss_mode = ss_flags & ~SS_FLAG_BITS;

this way flag bits that are currently undefined will continue to trigger EINVAL.

OK, will do.
Though I am still going to keep SS_AUTODISARM =(1 << 4) - it doesn't matter from what side we add new flags, does it?

Adding them starting with a high bit means that more low bits are available for use as mode numbers. Admittedly, this barely matters.

--Andy

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC