1 |
gbeauche |
1.1 |
/* |
2 |
|
|
* paranoia.cpp - Check undocumented features of the Darwin kernel that |
3 |
|
|
* SheepShaver relies upon |
4 |
|
|
* |
5 |
|
|
* SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig |
6 |
|
|
* |
7 |
|
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* This program is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with this program; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include <unistd.h> |
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <stdlib.h> |
25 |
|
|
#include <string.h> |
26 |
|
|
#include <signal.h> |
27 |
|
|
#include <errno.h> |
28 |
|
|
#include <sys/types.h> |
29 |
|
|
#include <sys/ucontext.h> |
30 |
|
|
|
31 |
|
|
#include "sysdeps.h" |
32 |
|
|
#include "main.h" |
33 |
|
|
#include "user_strings.h" |
34 |
|
|
|
35 |
|
|
#define DEBUG 1 |
36 |
|
|
#include "debug.h" |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
// Constants |
40 |
|
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
41 |
|
|
|
42 |
|
|
// Prototypes |
43 |
|
|
extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
44 |
|
|
extern "C" void *get_sp(void); |
45 |
|
|
extern "C" void set_r2(uint32 val); |
46 |
|
|
extern void paranoia_check(void); |
47 |
|
|
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp); |
48 |
|
|
|
49 |
|
|
// Global variables |
50 |
|
|
static void *sig_stack = NULL; |
51 |
|
|
|
52 |
|
|
static int err = 0; |
53 |
|
|
static void *sig_sp = NULL; |
54 |
|
|
static void *sig_r4 = NULL; |
55 |
|
|
static int sig_sc_signal = 0; |
56 |
|
|
static void *sig_sc_regs = NULL; |
57 |
|
|
static uint32 sig_r2 = 0; |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
void paranoia_check(void) |
61 |
|
|
{ |
62 |
|
|
char str[256]; |
63 |
|
|
|
64 |
|
|
D(bug("Paranoia checks...\n")); |
65 |
|
|
|
66 |
|
|
// Create and install stack for signal handler |
67 |
|
|
sig_stack = malloc(SIG_STACK_SIZE); |
68 |
|
|
if (sig_stack == NULL) { |
69 |
|
|
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
70 |
|
|
exit(1); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
struct sigaltstack old_stack; |
74 |
|
|
struct sigaltstack new_stack; |
75 |
|
|
new_stack.ss_sp = (char *)sig_stack; |
76 |
|
|
new_stack.ss_flags = 0; |
77 |
|
|
new_stack.ss_size = SIG_STACK_SIZE; |
78 |
|
|
if (sigaltstack(&new_stack, &old_stack) < 0) { |
79 |
|
|
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
80 |
|
|
ErrorAlert(str); |
81 |
|
|
exit(1); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
// Install SIGUSR2 signal handler |
85 |
|
|
static struct sigaction old_action; |
86 |
|
|
static struct sigaction sigusr2_action; |
87 |
|
|
sigemptyset(&sigusr2_action.sa_mask); |
88 |
|
|
sigusr2_action.sa_sigaction = sigusr2_handler; |
89 |
|
|
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
90 |
|
|
if (sigaction(SIGUSR2, &sigusr2_action, &old_action) < 0) { |
91 |
|
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
92 |
|
|
ErrorAlert(str); |
93 |
|
|
exit(1); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
// Raise SIGUSR2 |
97 |
|
|
set_r2(0xaffebad5); |
98 |
|
|
raise(SIGUSR2); |
99 |
|
|
|
100 |
|
|
// Check error code |
101 |
|
|
switch (err) { |
102 |
|
|
case 1: |
103 |
|
|
printf("FATAL: sigaltstack() doesn't seem to work (sp in signal handler was %08lx, expected %08lx..%08lx)\n", (uint32)sig_sp, (uint32)sig_stack, (uint32)sig_stack + SIG_STACK_SIZE); |
104 |
|
|
break; |
105 |
|
|
case 2: |
106 |
|
|
printf("FATAL: r4 in signal handler (%08lx) doesn't point to stack\n", (uint32)sig_r4); |
107 |
|
|
break; |
108 |
|
|
case 3: |
109 |
|
|
printf("FATAL: r4 in signal handler doesn't seem to point to a ucontext_t (signal number was %d, expected %d)", sig_sc_signal, SIGUSR2); |
110 |
|
|
break; |
111 |
|
|
case 4: |
112 |
|
|
printf("FATAL: sc->regs in signal handler (%08lx) doesn't point to stack\n", (uint32)sig_sc_regs); |
113 |
|
|
break; |
114 |
|
|
case 5: |
115 |
|
|
printf("FATAL: sc->regs->gpr[2] in signal handler (%08lx) doesn't have expected value (%08x)\n", (uint32)sig_r2, 0xaffebad5); |
116 |
|
|
break; |
117 |
|
|
} |
118 |
|
|
if (err) { |
119 |
|
|
printf("Maybe you need a different kernel?\n"); |
120 |
|
|
exit(1); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
// Clean up |
124 |
|
|
D(bug("...passed\n")); |
125 |
|
|
sigaction(SIGUSR2, &old_action, NULL); |
126 |
|
|
sigaltstack(&old_stack, NULL); |
127 |
|
|
free(sig_stack); |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
|
131 |
|
|
static void sigusr2_handler(int sig, siginfo_t *sip, void *scp) |
132 |
|
|
{ |
133 |
|
|
ucontext_t *ucp = (ucontext_t *)scp; |
134 |
|
|
|
135 |
|
|
// Check whether sigaltstack works |
136 |
|
|
sig_sp = get_sp(); |
137 |
|
|
if (sig_sp < sig_stack || sig_sp >= ((uint8 *)sig_stack + SIG_STACK_SIZE)) { |
138 |
|
|
err = 1; |
139 |
|
|
return; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
// Check whether r4 points to info on the stack |
143 |
|
|
sig_r4 = ucp; |
144 |
|
|
if (sig_r4 < sig_stack || sig_r4 >= ((uint8 *)sig_stack + SIG_STACK_SIZE)) { |
145 |
|
|
err = 2; |
146 |
|
|
return; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
// Check whether context regs points to info on the stack |
150 |
|
|
sig_sc_regs = &ucp->uc_mcontext->ss.r0; |
151 |
|
|
if (sig_sc_regs < sig_stack || sig_sc_regs >= ((uint8 *)sig_stack + SIG_STACK_SIZE)) { |
152 |
|
|
err = 4; |
153 |
|
|
return; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
// Check whether r2 still holds the value we set it to |
157 |
|
|
sig_r2 = ucp->uc_mcontext->ss.r2; |
158 |
|
|
if (sig_r2 != 0xaffebad5) { |
159 |
|
|
err = 5; |
160 |
|
|
return; |
161 |
|
|
} |
162 |
|
|
} |