Browse Source

altstack: Restore alternate signal stack state

altstack relies on catching a SIGSEGV caused when overrunning the stack.
This means that the SEGV handler itself can't use the already overflowed
stack, and so we use sigaltstack() to assign the signal handler a different
stack.  On completion, altstack() clears the alternate signal stack.

However, it's possible that the calling program could be using
sigaltstack() for its own reasons, so it's more correct to restore the
sigaltstack() state to that from the beginning of the altstack() call.
This patch implements this behaviour.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson 9 years ago
parent
commit
15c555b335
1 changed files with 3 additions and 2 deletions
  1. 3 2
      ccan/altstack/altstack.c

+ 3 - 2
ccan/altstack/altstack.c

@@ -71,6 +71,7 @@ int altstack(rlim_t max, void *(*fn)(void *), void *arg, void **out)
 	struct rlimit rl_save;
 	struct sigaction sa_save;
 	int errno_save;
+	stack_t ss_save;
 
 	assert(max > 0 && fn);
 	#define ok(x, y) ({ long __r = (long) (x); if (__r == -1) { bang(#x); if (y) goto out; } __r; })
@@ -98,7 +99,7 @@ int altstack(rlim_t max, void *(*fn)(void *), void *arg, void **out)
 		stack_t ss = { .ss_sp = sigstk, .ss_size = sizeof(sigstk) };
 		struct sigaction sa = { .sa_handler = segvjmp, .sa_flags = SA_NODEFER|SA_RESETHAND|SA_ONSTACK };
 
-		ok(sigaltstack(&ss, 0), 1);
+		ok(sigaltstack(&ss, &ss_save), 1);
 		undo++;
 
 		sigemptyset(&sa.sa_mask);
@@ -131,7 +132,7 @@ out:
 	case 4:
 		ok(sigaction(SIGSEGV, &sa_save, 0), 0);
 	case 3:
-		ok(sigaltstack(&(stack_t) { .ss_flags = SS_DISABLE }, 0), 0);
+		ok(sigaltstack(&ss_save, 0), 0);
 	case 2:
 		ok(munmap(m, max), 0);
 	case 1: