1 /* sigaltstack wrappers.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <support/xsignal.h>
20 #include <support/support.h>
21 #include <support/xunistd.h>
22 #include <support/check.h>
27 #include <sys/param.h> /* roundup, MAX */
30 # define MAP_NORESERVE 0
36 /* The "cookie" returned by xalloc_sigstack points to one of these
40 void *alloc_base
; /* Base address of the complete allocation. */
41 size_t alloc_size
; /* Size of the complete allocation. */
42 stack_t alt_stack
; /* The address and size of the stack itself. */
43 stack_t old_stack
; /* The previous signal stack. */
47 xalloc_sigstack (size_t size
)
49 size_t pagesize
= sysconf (_SC_PAGESIZE
);
51 FAIL_EXIT1 ("sysconf (_SC_PAGESIZE): %m\n");
53 /* Always supply at least MINSIGSTKSZ space; passing 0 as size means
54 only that much space. No matter what the number is, round it up
55 to a whole number of pages. */
56 size_t stacksize
= roundup (size
+ MINSIGSTKSZ
, pagesize
);
58 /* The guard bands need to be large enough to intercept offset
59 accesses from a stack address that might otherwise hit another
60 mapping. Make them at least twice as big as the stack itself, to
61 defend against an offset by the entire size of a large
62 stack-allocated array. The minimum is 1MiB, which is arbitrarily
63 chosen to be larger than any "typical" wild pointer offset.
64 Again, no matter what the number is, round it up to a whole
66 size_t guardsize
= roundup (MAX (2 * stacksize
, 1024 * 1024), pagesize
);
68 struct sigstack_desc
*desc
= xmalloc (sizeof (struct sigstack_desc
));
69 desc
->alloc_size
= guardsize
+ stacksize
+ guardsize
;
70 /* Use MAP_NORESERVE so that RAM will not be wasted on the guard
71 bands; touch all the pages of the actual stack before returning,
72 so we know they are allocated. */
73 desc
->alloc_base
= xmmap (0,
76 MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_NORESERVE
|MAP_STACK
,
79 xmprotect (desc
->alloc_base
, guardsize
, PROT_NONE
);
80 xmprotect (desc
->alloc_base
+ guardsize
+ stacksize
, guardsize
, PROT_NONE
);
81 memset (desc
->alloc_base
+ guardsize
, 0xA5, stacksize
);
83 desc
->alt_stack
.ss_sp
= desc
->alloc_base
+ guardsize
;
84 desc
->alt_stack
.ss_flags
= 0;
85 desc
->alt_stack
.ss_size
= stacksize
;
87 if (sigaltstack (&desc
->alt_stack
, &desc
->old_stack
))
88 FAIL_EXIT1 ("sigaltstack (new stack: sp=%p, size=%zu, flags=%u): %m\n",
89 desc
->alt_stack
.ss_sp
, desc
->alt_stack
.ss_size
,
90 desc
->alt_stack
.ss_flags
);
96 xfree_sigstack (void *stack
)
98 struct sigstack_desc
*desc
= stack
;
100 if (sigaltstack (&desc
->old_stack
, 0))
101 FAIL_EXIT1 ("sigaltstack (restore old stack: sp=%p, size=%zu, flags=%u): "
102 "%m\n", desc
->old_stack
.ss_sp
, desc
->old_stack
.ss_size
,
103 desc
->old_stack
.ss_flags
);
104 xmunmap (desc
->alloc_base
, desc
->alloc_size
);
109 xget_sigstack_location (const void *stack
, unsigned char **addrp
, size_t *sizep
)
111 const struct sigstack_desc
*desc
= stack
;
112 *addrp
= desc
->alt_stack
.ss_sp
;
113 *sizep
= desc
->alt_stack
.ss_size
;