1/* Make sure longjmp fortification catches bad signal stacks.
2 Copyright (C) 2013-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <setjmp.h>
20#include <signal.h>
21#include <string.h>
22
23static char buf[SIGSTKSZ * 4];
24static jmp_buf jb;
25
26static void
27handler (int sig)
28{
29 if (sig == SIGUSR1)
30 {
31 if (setjmp (jb) != 0)
32 {
33 puts ("setjmp should not have been called");
34 kill (pid: getpid (), SIGTERM);
35 }
36 }
37 else if (sig == SIGABRT)
38 {
39 /* Yeah it worked. */
40 _exit (0);
41 }
42}
43
44static int
45do_test (void)
46{
47 stack_t ss;
48
49 set_fortify_handler (handler);
50
51 /* Create a valid signal stack and enable it. */
52 ss.ss_sp = buf;
53 ss.ss_size = sizeof (buf);
54 ss.ss_flags = 0;
55 if (sigaltstack (ss: &ss, NULL) < 0)
56 {
57 printf ("first sigaltstack failed: %m\n");
58 return 1;
59 }
60
61 /* Trigger the signal handler which will create a jmpbuf that points to the
62 end of the signal stack. */
63 signal (SIGUSR1, handler: handler);
64 kill (pid: getpid (), SIGUSR1);
65
66 /* Shrink the signal stack so the jmpbuf is now invalid.
67 We adjust the start & end to handle stacks that grow up & down. */
68 ss.ss_sp = buf + sizeof (buf) / 2;
69 ss.ss_size = sizeof (buf) / 4;
70 if (sigaltstack (ss: &ss, NULL) < 0)
71 {
72 printf ("second sigaltstack failed: %m\n");
73 return 1;
74 }
75
76 /* This should fail. */
77 longjmp (jb, 1);
78
79 puts ("longjmp returned and shouldn't");
80 return 1;
81}
82
83#include <support/test-driver.c>
84

source code of glibc/debug/tst-longjmp_chk3.c