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"
25 #define SS_AUTODISARM (1U << 31)
28 #ifndef AT_MINSIGSTKSZ
29 #define AT_MINSIGSTKSZ 51
32 static unsigned int stack_size;
33 static void *sstack, *ustack;
34 static ucontext_t uc, sc;
35 static const char *msg = "[OK]\tStack preserved";
36 static const char *msg2 = "[FAIL]\tStack corrupted";
42 void my_usr1(int sig, siginfo_t *si, void *u)
50 register unsigned long sp asm("%15");
52 register unsigned long sp asm("sp");
55 if (sp < (unsigned long)sstack ||
56 sp >= (unsigned long)sstack + stack_size) {
57 ksft_exit_fail_msg("SP is not on sigaltstack\n");
59 /* put some data on stack. other sighandler will try to overwrite it */
62 p = (struct stk_data *)(aa + 512);
65 ksft_print_msg("[RUN]\tsignal USR1\n");
66 err = sigaltstack(NULL, &stk);
68 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
71 if (stk.ss_flags != SS_DISABLE)
72 ksft_test_result_fail("tss_flags=%x, should be SS_DISABLE\n",
75 ksft_test_result_pass(
76 "sigaltstack is disabled in sighandler\n");
77 swapcontext(&sc, &uc);
78 ksft_print_msg("%s\n", p->msg);
80 ksft_exit_fail_msg("[RUN]\tAborting\n");
85 void my_usr2(int sig, siginfo_t *si, void *u)
90 ksft_print_msg("[RUN]\tsignal USR2\n");
92 /* dont run valgrind on this */
93 /* try to find the data stored by previous sighandler */
94 p = memmem(aa, 1024, msg, strlen(msg));
96 ksft_test_result_fail("sigaltstack re-used\n");
97 /* corrupt the data */
99 /* tell other sighandler that his data is corrupted */
104 static void switch_fn(void)
106 ksft_print_msg("[RUN]\tswitched to user ctx\n");
113 struct sigaction act;
117 /* Make sure more than the required minimum. */
118 stack_size = getauxval(AT_MINSIGSTKSZ) + SIGSTKSZ;
119 ksft_print_msg("[NOTE]\tthe stack size is %lu\n", stack_size);
124 sigemptyset(&act.sa_mask);
125 act.sa_flags = SA_ONSTACK | SA_SIGINFO;
126 act.sa_sigaction = my_usr1;
127 sigaction(SIGUSR1, &act, NULL);
128 act.sa_sigaction = my_usr2;
129 sigaction(SIGUSR2, &act, NULL);
130 sstack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
131 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
132 if (sstack == MAP_FAILED) {
133 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
137 err = sigaltstack(NULL, &stk);
139 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
142 if (stk.ss_flags == SS_DISABLE) {
143 ksft_test_result_pass(
144 "Initial sigaltstack state was SS_DISABLE\n");
146 ksft_exit_fail_msg("Initial sigaltstack state was %x; "
147 "should have been SS_DISABLE\n", stk.ss_flags);
152 stk.ss_size = stack_size;
153 stk.ss_flags = SS_ONSTACK | SS_AUTODISARM;
154 err = sigaltstack(&stk, NULL);
156 if (errno == EINVAL) {
157 ksft_test_result_skip(
158 "[NOTE]\tThe running kernel doesn't support SS_AUTODISARM\n");
160 * If test cases for the !SS_AUTODISARM variant were
161 * added, we could still run them. We don't have any
162 * test cases like that yet, so just exit and report
168 "sigaltstack(SS_ONSTACK | SS_AUTODISARM) %s\n",
174 ustack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
175 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
176 if (ustack == MAP_FAILED) {
177 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
182 uc.uc_stack.ss_sp = ustack;
183 uc.uc_stack.ss_size = stack_size;
184 makecontext(&uc, switch_fn, 0);
187 err = sigaltstack(NULL, &stk);
189 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
192 if (stk.ss_flags != SS_AUTODISARM) {
193 ksft_exit_fail_msg("ss_flags=%x, should be SS_AUTODISARM\n",
197 ksft_test_result_pass(
198 "sigaltstack is still SS_AUTODISARM after signal\n");