1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
* $OpenBSD: sigret.c,v 1.5 2003/07/31 21:48:04 deraadt Exp $
*
* Public Domain
*
* Playing games with sigreturn. Check if calling sigreturn from a
* signal handler screws anything up.
*
* Run with:
* -a: use an alternate signal stack
*
* -b: call sigreturn from outside of a signal handler
* An error is OK
*
* -c: clobber the sigcontext before calling sigreturn
* the program should die
*
* -f: don't use sigreturn -- fall through the signal handler
* -c, and -i options ignored when used
*
* -i: call sigreturn from a function called by the signal handler
*
*/
#include <sys/time.h>
#include <err.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
* sigalarm occurs 50 times/second. Stop running after 10 seconds
* (100 interrupts).
*/
#define MAX_INTERRUPTS 500
int failed;
int altstack;
int badcall;
int clobbercall;
int fallthru;
int indirect;
volatile int count;
struct sigcontext gscp;
int gscp_loaded;
static void
usage(const char * err, ...)
{
extern const char * __progname;
if (err) {
va_list ap;
va_start(ap, err);
vwarnx(err, ap);
va_end(ap);
}
fprintf(stderr, "usage: %s [-abcfi]\n", __progname);
exit(1);
}
static void
indirect_return(struct sigcontext * scp)
{
sigreturn(scp);
}
static void
sig_handler(int sig, siginfo_t *blah, void *x)
{
struct sigcontext * scp = x;
count++;
if (!fallthru) {
if (clobbercall)
memset(scp, 0, sizeof *scp);
if (indirect)
indirect_return(scp);
else if (badcall) {
gscp = *scp;
gscp_loaded = 1;
} else
sigreturn(scp);
}
}
static void
test2(char *fmt)
{
char *ofmt = fmt;
if (gscp_loaded) {
gscp_loaded = 0;
sigreturn(&gscp);
}
for (; *fmt; fmt++)
switch (*fmt) {
case 'i':
case 'c':
case 'l':
case 'p':
break;
default:
failed = 1;
fprintf(stderr,
"unexpected character 0x%02x `%c' in %s: count %d\n",
*fmt, *fmt, ofmt, count);
}
}
int
main(int argc, char * argv[])
{
extern char *optarg;
extern int optind;
int opt;
struct sigaction act;
struct sigaltstack ss;
while ((opt = getopt(argc, argv, "abcfi")) != -1) {
switch (opt) {
case 'a':
/* use sigaltstack */
altstack = 1;
break;
case 'b':
/* call outside of sig_handler */
badcall = 1;
break;
case 'c':
/* force error by munging sigcontext */
clobbercall = 1;
break;
case 'f':
/* don't use sigreturn */
fallthru = 1;
break;
case 'i':
/* call sigreturn indirectly */
indirect = 1;
break;
}
}
/* make sure there is no other cruft left on the command line */
if (optind != argc)
usage("unknown argument -- %s", argv[ optind ]);
if (altstack) {
if ((ss.ss_sp = malloc(SIGSTKSZ)) == NULL)
errx(1, "ss_sp malloc");
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss,0) == -1)
err(1, "sigaltstack");
}
sigfillset(&act.sa_mask);
act.sa_sigaction = sig_handler;
act.sa_flags = SA_RESTART;
if (altstack)
act.sa_flags |= SA_ONSTACK;
sigaction(SIGALRM, &act, NULL);
ualarm(10000, 10000);
while (count < MAX_INTERRUPTS)
test2("iclp");
return failed;
}
|