1*8566a01bSbluhm /* $OpenBSD: callstack.c,v 1.1 2019/09/23 08:34:07 bluhm Exp $ */
2*8566a01bSbluhm /*
3*8566a01bSbluhm * Copyright (c) 2018 Todd Mortimer <mortimer@openbsd.org>
4*8566a01bSbluhm * Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org>
5*8566a01bSbluhm *
6*8566a01bSbluhm * Permission to use, copy, modify, and distribute this software for any
7*8566a01bSbluhm * purpose with or without fee is hereby granted, provided that the above
8*8566a01bSbluhm * copyright notice and this permission notice appear in all copies.
9*8566a01bSbluhm *
10*8566a01bSbluhm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*8566a01bSbluhm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*8566a01bSbluhm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*8566a01bSbluhm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*8566a01bSbluhm * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*8566a01bSbluhm * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*8566a01bSbluhm * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*8566a01bSbluhm */
18*8566a01bSbluhm
19*8566a01bSbluhm #include <err.h>
20*8566a01bSbluhm #include <stdlib.h>
21*8566a01bSbluhm #include <signal.h>
22*8566a01bSbluhm #include <unistd.h>
23*8566a01bSbluhm
24*8566a01bSbluhm #include "pivot.h"
25*8566a01bSbluhm
26*8566a01bSbluhm void handler(int);
27*8566a01bSbluhm void doexit(void);
28*8566a01bSbluhm
29*8566a01bSbluhm int
main(int argc,char * argv[])30*8566a01bSbluhm main(int argc, char *argv[])
31*8566a01bSbluhm {
32*8566a01bSbluhm stack_t ss;
33*8566a01bSbluhm struct sigaction act;
34*8566a01bSbluhm void (**newstack)(void);
35*8566a01bSbluhm long pagesize;
36*8566a01bSbluhm
37*8566a01bSbluhm ss.ss_sp = malloc(SIGSTKSZ);
38*8566a01bSbluhm if (ss.ss_sp == NULL)
39*8566a01bSbluhm err(1, "malloc sigstack");
40*8566a01bSbluhm ss.ss_size = SIGSTKSZ;
41*8566a01bSbluhm ss.ss_flags = 0;
42*8566a01bSbluhm if (sigaltstack(&ss, NULL) == -1)
43*8566a01bSbluhm err(1, "sigaltstack");
44*8566a01bSbluhm
45*8566a01bSbluhm act.sa_handler = handler;
46*8566a01bSbluhm sigemptyset(&act.sa_mask);
47*8566a01bSbluhm act.sa_flags = SA_ONSTACK;
48*8566a01bSbluhm
49*8566a01bSbluhm /* set up an alt stack on the heap that just calls doexit */
50*8566a01bSbluhm pagesize = sysconf(_SC_PAGESIZE);
51*8566a01bSbluhm if (pagesize == -1)
52*8566a01bSbluhm err(1, "sysconf");
53*8566a01bSbluhm newstack = malloc(pagesize > SIGSTKSZ ? pagesize : SIGSTKSZ);
54*8566a01bSbluhm if (newstack == NULL)
55*8566a01bSbluhm err(1, "malloc newstack");
56*8566a01bSbluhm /* allow stack to change half a page up and down. */
57*8566a01bSbluhm newstack[pagesize/sizeof(*newstack)/2] = doexit;
58*8566a01bSbluhm
59*8566a01bSbluhm if (sigaction(SIGSEGV, &act, NULL) == -1)
60*8566a01bSbluhm err(1, "sigaction");
61*8566a01bSbluhm pivot(&newstack[pagesize/sizeof(*newstack)/2]);
62*8566a01bSbluhm return 3;
63*8566a01bSbluhm }
64*8566a01bSbluhm
65*8566a01bSbluhm void
handler(int signum)66*8566a01bSbluhm handler(int signum)
67*8566a01bSbluhm {
68*8566a01bSbluhm _exit(0);
69*8566a01bSbluhm }
70*8566a01bSbluhm
71*8566a01bSbluhm void
doexit(void)72*8566a01bSbluhm doexit(void)
73*8566a01bSbluhm {
74*8566a01bSbluhm exit(2);
75*8566a01bSbluhm }
76