1 // SPDX-License-Identifier: GPL-2.0
5 * test sigaltstack(SS_ONSTACK | SS_AUTODISARM)
6 * If that succeeds, then swapcontext() can be used inside sighandler safely.
22 #include "../kselftest.h"
23 #include "current_stack_pointer.h"
26 #define SS_AUTODISARM (1U << 31)
29 #ifndef AT_MINSIGSTKSZ
30 #define AT_MINSIGSTKSZ 51
33 static unsigned int stack_size;
34 static void *sstack, *ustack;
35 static ucontext_t uc, sc;
36 static const char *msg = "[OK]\tStack preserved";
37 static const char *msg2 = "[FAIL]\tStack corrupted";
43 void my_usr1(int sig, siginfo_t *si, void *u)
50 if (sp < (unsigned long)sstack ||
51 sp >= (unsigned long)sstack + stack_size) {
52 ksft_exit_fail_msg("SP is not on sigaltstack\n");
54 /* put some data on stack. other sighandler will try to overwrite it */
57 p = (struct stk_data *)(aa + 512);
60 ksft_print_msg("[RUN]\tsignal USR1\n");
61 err = sigaltstack(NULL, &stk);
63 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
66 if (stk.ss_flags != SS_DISABLE)
67 ksft_test_result_fail("tss_flags=%x, should be SS_DISABLE\n",
70 ksft_test_result_pass(
71 "sigaltstack is disabled in sighandler\n");
72 swapcontext(&sc, &uc);
73 ksft_print_msg("%s\n", p->msg);
75 ksft_exit_fail_msg("[RUN]\tAborting\n");
80 void my_usr2(int sig, siginfo_t *si, void *u)
85 ksft_print_msg("[RUN]\tsignal USR2\n");
87 /* dont run valgrind on this */
88 /* try to find the data stored by previous sighandler */
89 p = memmem(aa, 1024, msg, strlen(msg));
91 ksft_test_result_fail("sigaltstack re-used\n");
92 /* corrupt the data */
94 /* tell other sighandler that his data is corrupted */
99 static void switch_fn(void)
101 ksft_print_msg("[RUN]\tswitched to user ctx\n");
108 struct sigaction act;
112 /* Make sure more than the required minimum. */
113 stack_size = getauxval(AT_MINSIGSTKSZ) + SIGSTKSZ;
114 ksft_print_msg("[NOTE]\tthe stack size is %lu\n", stack_size);
119 sigemptyset(&act.sa_mask);
120 act.sa_flags = SA_ONSTACK | SA_SIGINFO;
121 act.sa_sigaction = my_usr1;
122 sigaction(SIGUSR1, &act, NULL);
123 act.sa_sigaction = my_usr2;
124 sigaction(SIGUSR2, &act, NULL);
125 sstack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
126 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
127 if (sstack == MAP_FAILED) {
128 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
132 err = sigaltstack(NULL, &stk);
134 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
137 if (stk.ss_flags == SS_DISABLE) {
138 ksft_test_result_pass(
139 "Initial sigaltstack state was SS_DISABLE\n");
141 ksft_exit_fail_msg("Initial sigaltstack state was %x; "
142 "should have been SS_DISABLE\n", stk.ss_flags);
147 stk.ss_size = stack_size;
148 stk.ss_flags = SS_ONSTACK | SS_AUTODISARM;
149 err = sigaltstack(&stk, NULL);
151 if (errno == EINVAL) {
152 ksft_test_result_skip(
153 "[NOTE]\tThe running kernel doesn't support SS_AUTODISARM\n");
155 * If test cases for the !SS_AUTODISARM variant were
156 * added, we could still run them. We don't have any
157 * test cases like that yet, so just exit and report
163 "sigaltstack(SS_ONSTACK | SS_AUTODISARM) %s\n",
169 ustack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
170 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
171 if (ustack == MAP_FAILED) {
172 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
177 uc.uc_stack.ss_sp = ustack;
178 uc.uc_stack.ss_size = stack_size;
179 makecontext(&uc, switch_fn, 0);
182 err = sigaltstack(NULL, &stk);
184 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
187 if (stk.ss_flags != SS_AUTODISARM) {
188 ksft_exit_fail_msg("ss_flags=%x, should be SS_AUTODISARM\n",
192 ksft_test_result_pass(
193 "sigaltstack is still SS_AUTODISARM after signal\n");