blob: 46f613975c199ebc2a323b14c399074190c9cd3f [file] [log] [blame]
Gennady Sharapov60d339f62005-09-03 15:57:47 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Jeff Dike8e367062006-03-27 01:14:32 -08006#include <pty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <stdio.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07008#include <stddef.h>
9#include <stdarg.h>
10#include <stdlib.h>
11#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <unistd.h>
13#include <signal.h>
14#include <sched.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070015#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <sys/time.h>
18#include <sys/wait.h>
19#include <sys/mman.h>
Jeff Dike1d94cda2007-05-06 14:51:00 -070020#include <sys/resource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/unistd.h>
22#include <asm/page.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070023#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "kern_util.h"
25#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "signal_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "sysdep/ptrace.h"
28#include "sysdep/sigcontext.h"
29#include "irq_user.h"
30#include "ptrace_user.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070031#include "mem_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "init.h"
33#include "os.h"
34#include "uml-config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "choose-mode.h"
36#include "mode.h"
Jeff Diked67b5692005-07-07 17:56:49 -070037#include "tempfile.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070038#include "kern_constants.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#ifdef UML_CONFIG_MODE_SKAS
41#include "skas.h"
42#include "skas_ptrace.h"
43#include "registers.h"
44#endif
45
Jeff Dikeb85e9682005-07-28 21:16:01 -070046static int ptrace_child(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 int ret;
49 int pid = os_getpid(), ppid = getppid();
50 int sc_result;
51
Jeff Dike43b00fd2006-02-07 12:58:42 -080052 change_sig(SIGWINCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
54 perror("ptrace");
55 os_kill_process(pid, 0);
56 }
Jeff Dike73c8f4442007-02-10 01:44:20 -080057 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /*This syscall will be intercepted by the parent. Don't call more than
60 * once, please.*/
61 sc_result = os_getpid();
62
63 if (sc_result == pid)
64 ret = 1; /*Nothing modified by the parent, we are running
65 normally.*/
66 else if (sc_result == ppid)
67 ret = 0; /*Expected in check_ptrace and check_sysemu when they
68 succeed in modifying the stack frame*/
69 else
70 ret = 2; /*Serious trouble! This could be caused by a bug in
71 host 2.6 SKAS3/2.6 patch before release -V6, together
72 with a bug in the UML code itself.*/
73 _exit(ret);
74}
75
Jeff Dike3a150e12007-02-10 01:44:28 -080076static void fatal_perror(char *str)
77{
78 perror(str);
79 exit(1);
80}
81
82static void fatal(char *fmt, ...)
83{
84 va_list list;
85
86 va_start(list, fmt);
87 vprintf(fmt, list);
88 va_end(list);
89 fflush(stdout);
90
91 exit(1);
92}
93
94static void non_fatal(char *fmt, ...)
95{
96 va_list list;
97
98 va_start(list, fmt);
99 vprintf(fmt, list);
100 va_end(list);
101 fflush(stdout);
102}
103
Jeff Dikeb85e9682005-07-28 21:16:01 -0700104static int start_ptraced_child(void **stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700106 void *stack;
107 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int pid, n, status;
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700109
Jeff Dikec539ab72007-06-16 10:16:09 -0700110 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
111 PROT_READ | PROT_WRITE | PROT_EXEC,
Jeff Dikeb85e9682005-07-28 21:16:01 -0700112 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
113 if(stack == MAP_FAILED)
Jeff Dike3a150e12007-02-10 01:44:28 -0800114 fatal_perror("check_ptrace : mmap failed");
Jeff Dikec539ab72007-06-16 10:16:09 -0700115 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700116 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if(pid < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800118 fatal_perror("start_ptraced_child : clone failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
120 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800121 fatal_perror("check_ptrace : clone failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800123 fatal("check_ptrace : expected SIGSTOP, got status = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 status);
125
Jeff Dikeb85e9682005-07-28 21:16:01 -0700126 *stack_out = stack;
Jeff Dike9eae9b12007-02-10 01:44:20 -0800127 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700130/* When testing for SYSEMU support, if it is one of the broken versions, we
131 * must just avoid using sysemu, not panic, but only if SYSEMU features are
132 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700134 * must work anyway!
135 */
136static int stop_ptraced_child(int pid, void *stack, int exitcode,
Jeff Dike3a150e12007-02-10 01:44:28 -0800137 int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int status, n, ret = 0;
140
141 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800142 fatal_perror("stop_ptraced_child : ptrace failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 CATCH_EINTR(n = waitpid(pid, &status, 0));
144 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
145 int exit_with = WEXITSTATUS(status);
146 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800147 non_fatal("check_ptrace : child exited with status 2. "
Jeff Dikecf6aced2007-05-23 13:57:40 -0700148 "\nDisabling SYSEMU support.\n");
Jeff Dike3a150e12007-02-10 01:44:28 -0800149 non_fatal("check_ptrace : child exited with exitcode %d, while "
150 "expecting %d; status 0x%x\n", exit_with,
151 exitcode, status);
152 if (mustexit)
153 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ret = -1;
155 }
156
Jeff Dikec539ab72007-06-16 10:16:09 -0700157 if(munmap(stack, UM_KERN_PAGE_SIZE) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800158 fatal_perror("check_ptrace : munmap failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return ret;
160}
161
Jeff Dike7242a402007-02-10 01:44:19 -0800162/* Changed only during early boot */
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700163int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800164int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700165int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800166int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700167
168static int __init skas0_cmd_param(char *str, int* add)
169{
170 ptrace_faultinfo = proc_mm = 0;
171 return 0;
172}
173
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200174/* The two __uml_setup would conflict, without this stupid alias. */
175
176static int __init mode_skas0_cmd_param(char *str, int* add)
177 __attribute__((alias("skas0_cmd_param")));
178
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700179__uml_setup("skas0", skas0_cmd_param,
180 "skas0\n"
181 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
182 " you specify mode=tt.\n\n");
183
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200184__uml_setup("mode=skas0", mode_skas0_cmd_param,
185 "mode=skas0\n"
186 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
187 " specify mode=tt. Note that this was recently added - on \n"
188 " older kernels you must use simply \"skas0\".\n\n");
189
Jeff Dike7242a402007-02-10 01:44:19 -0800190/* Changed only during early boot */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700191static int force_sysemu_disabled = 0;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int __init nosysemu_cmd_param(char *str, int* add)
194{
195 force_sysemu_disabled = 1;
196 return 0;
197}
198
199__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700200"nosysemu\n"
201" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
202" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
203" behaviour of ptrace() and helps reducing host context switch rate.\n"
204" To make it working, you need a kernel patch for your host, too.\n"
205" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
206" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208static void __init check_sysemu(void)
209{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700210 void *stack;
Jeff Dikecf6aced2007-05-23 13:57:40 -0700211 unsigned long regs[MAX_REG_NR];
Jeff Dike9eae9b12007-02-10 01:44:20 -0800212 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jeff Dike3a150e12007-02-10 01:44:28 -0800214 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 sysemu_supported = 0;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700216 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
219 goto fail;
220
221 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
222 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800223 fatal_perror("check_sysemu : wait failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800225 fatal("check_sysemu : expected SIGTRAP, got status = %d",
226 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Jeff Dikecf6aced2007-05-23 13:57:40 -0700228 if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
229 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
230 if(PT_SYSCALL_NR(regs) != __NR_getpid){
231 non_fatal("check_sysemu got system call number %d, "
232 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
233 goto fail;
234 }
235
236 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
237 if(n < 0){
238 non_fatal("check_sysemu : failed to modify system call "
239 "return");
240 goto fail;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jeff Dikeb85e9682005-07-28 21:16:01 -0700243 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto fail_stopped;
245
246 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800247 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 set_using_sysemu(!force_sysemu_disabled);
249
Jeff Dike3a150e12007-02-10 01:44:28 -0800250 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700251 pid = start_ptraced_child(&stack);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700252
Jeff Dike3a150e12007-02-10 01:44:28 -0800253 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
254 (void *) PTRACE_O_TRACESYSGOOD) < 0))
255 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 while(1){
258 count++;
259 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
260 goto fail;
261 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
262 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800263 fatal_perror("check_ptrace : wait failed");
264
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700265 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!count)
Jeff Dike3a150e12007-02-10 01:44:28 -0800267 fatal("check_ptrace : SYSEMU_SINGLESTEP "
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700268 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
270 os_getpid());
271 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800272 fatal_perror("check_sysemu : failed to modify "
273 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 }
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700276 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
277 count++;
278 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800279 fatal("check_ptrace : expected SIGTRAP or "
280 "(SIGTRAP | 0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700282 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto fail_stopped;
284
285 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800286 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if ( !force_sysemu_disabled )
289 set_using_sysemu(sysemu_supported);
290 return;
291
292fail:
Jeff Dikeb85e9682005-07-28 21:16:01 -0700293 stop_ptraced_child(pid, stack, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800295 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700298static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700300 void *stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int pid, syscall, n, status;
302
Jeff Dike3a150e12007-02-10 01:44:28 -0800303 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700304 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Jeff Dike3a150e12007-02-10 01:44:28 -0800306 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
307 (void *) PTRACE_O_TRACESYSGOOD) < 0))
308 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 while(1){
311 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800312 fatal_perror("check_ptrace : ptrace failed");
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
315 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800316 fatal_perror("check_ptrace : wait failed");
317
318 if(!WIFSTOPPED(status) ||
319 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
320 fatal("check_ptrace : expected (SIGTRAP|0x80), "
321 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
324 0);
325 if(syscall == __NR_getpid){
326 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
327 __NR_getppid);
328 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800329 fatal_perror("check_ptrace : failed to modify "
330 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 break;
332 }
333 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700334 stop_ptraced_child(pid, stack, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800335 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 check_sysemu();
337}
338
Rob Landley966a082f2006-04-18 22:21:43 -0700339extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700340
Jeff Dike36e45462007-05-06 14:51:11 -0700341static void __init check_coredump_limit(void)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700342{
343 struct rlimit lim;
344 int err = getrlimit(RLIMIT_CORE, &lim);
345
346 if(err){
347 perror("Getting core dump limit");
348 return;
349 }
350
351 printf("Core dump limits :\n\tsoft - ");
352 if(lim.rlim_cur == RLIM_INFINITY)
353 printf("NONE\n");
354 else printf("%lu\n", lim.rlim_cur);
355
356 printf("\thard - ");
357 if(lim.rlim_max == RLIM_INFINITY)
358 printf("NONE\n");
359 else printf("%lu\n", lim.rlim_max);
360}
361
Jeff Dike36e45462007-05-06 14:51:11 -0700362void __init os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Jeff Dike1d94cda2007-05-06 14:51:00 -0700364 /* Print out the core dump limits early */
365 check_coredump_limit();
366
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700367 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700368
369 /* Need to check this early because mmapping happens before the
370 * kernel is running.
371 */
372 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700375static int __init noprocmm_cmd_param(char *str, int* add)
376{
377 proc_mm = 0;
378 return 0;
379}
380
381__uml_setup("noprocmm", noprocmm_cmd_param,
382"noprocmm\n"
383" Turns off usage of /proc/mm, even if host supports it.\n"
384" To support /proc/mm, the host needs to be patched using\n"
385" the current skas3 patch.\n\n");
386
387static int __init noptracefaultinfo_cmd_param(char *str, int* add)
388{
389 ptrace_faultinfo = 0;
390 return 0;
391}
392
393__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
394"noptracefaultinfo\n"
395" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
396" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
397" using the current skas3 patch.\n\n");
398
Bodo Stroesser858259c2005-11-07 00:58:55 -0800399static int __init noptraceldt_cmd_param(char *str, int* add)
400{
401 ptrace_ldt = 0;
402 return 0;
403}
404
405__uml_setup("noptraceldt", noptraceldt_cmd_param,
406"noptraceldt\n"
407" Turns off usage of PTRACE_LDT, even if host supports it.\n"
408" To support PTRACE_LDT, the host needs to be patched using\n"
409" the current skas3 patch.\n\n");
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#ifdef UML_CONFIG_MODE_SKAS
Bodo Stroesser858259c2005-11-07 00:58:55 -0800412static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct ptrace_faultinfo fi;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700415 void *stack;
Jeff Diked67b5692005-07-07 17:56:49 -0700416 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Jeff Dike3a150e12007-02-10 01:44:28 -0800418 non_fatal(" - PTRACE_FAULTINFO...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700419 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
422 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700423 ptrace_faultinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if(errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800425 non_fatal("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700426 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700428 }
429 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700430 if (!ptrace_faultinfo)
Jeff Dike3a150e12007-02-10 01:44:28 -0800431 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700432 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800433 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 init_registers(pid);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700437 stop_ptraced_child(pid, stack, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Bodo Stroesser858259c2005-11-07 00:58:55 -0800440static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800442#ifdef PTRACE_LDT
443 void *stack;
444 int pid, n;
445 unsigned char ldtbuf[40];
446 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
447 .func = 2, /* read default ldt */
448 .ptr = ldtbuf,
449 .bytecount = sizeof(ldtbuf)};
450
Jeff Dike3a150e12007-02-10 01:44:28 -0800451 non_fatal(" - PTRACE_LDT...");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800452 pid = start_ptraced_child(&stack);
453
454 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
455 if (n < 0) {
456 if(errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800457 non_fatal("not found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800458 else {
459 perror("not found");
460 }
461 ptrace_ldt = 0;
462 }
463 else {
464 if(ptrace_ldt)
Jeff Dike3a150e12007-02-10 01:44:28 -0800465 non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800466 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800467 non_fatal("found, but use is disabled\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800468 }
469
470 stop_ptraced_child(pid, stack, 1, 1);
471#else
472 /* PTRACE_LDT might be disabled via cmdline option.
473 * We want to override this, else we might use the stub
474 * without real need
475 */
476 ptrace_ldt = 1;
477#endif
478}
479
480static inline void check_skas3_proc_mm(void)
481{
Jeff Dike3a150e12007-02-10 01:44:28 -0800482 non_fatal(" - /proc/mm...");
Jeff Dike73c8f4442007-02-10 01:44:20 -0800483 if (access("/proc/mm", W_OK) < 0) {
Jeff Dike9eae9b12007-02-10 01:44:20 -0800484 proc_mm = 0;
Jeff Dike3a150e12007-02-10 01:44:28 -0800485 perror("not found");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700486 }
487 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700488 if (!proc_mm)
Jeff Dike3a150e12007-02-10 01:44:28 -0800489 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700490 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800491 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Bodo Stroesser858259c2005-11-07 00:58:55 -0800495int can_do_skas(void)
496{
Jeff Dike3a150e12007-02-10 01:44:28 -0800497 non_fatal("Checking for the skas3 patch in the host:\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800498
499 check_skas3_proc_mm();
500 check_skas3_ptrace_faultinfo();
501 check_skas3_ptrace_ldt();
502
503 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
504 skas_needs_stub = 1;
505
Jeff Diked67b5692005-07-07 17:56:49 -0700506 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508#else
509int can_do_skas(void)
510{
Jeff Dike9eae9b12007-02-10 01:44:20 -0800511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513#endif
Jeff Dike0f80bc82005-09-16 19:27:50 -0700514
Jeff Dike0f80bc82005-09-16 19:27:50 -0700515int __init parse_iomem(char *str, int *add)
516{
517 struct iomem_region *new;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800518 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700519 char *file, *driver;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800520 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700521
522 driver = str;
523 file = strchr(str,',');
524 if(file == NULL){
525 printf("parse_iomem : failed to parse iomem\n");
526 goto out;
527 }
528 *file = '\0';
529 file++;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800530 fd = open(file, O_RDWR, 0);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700531 if(fd < 0){
532 os_print_error(fd, "parse_iomem - Couldn't open io file");
533 goto out;
534 }
535
Jeff Dike73c8f4442007-02-10 01:44:20 -0800536 if(fstat64(fd, &buf) < 0){
537 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700538 goto out_close;
539 }
540
541 new = malloc(sizeof(*new));
542 if(new == NULL){
543 perror("Couldn't allocate iomem_region struct");
544 goto out_close;
545 }
546
Jeff Dike73c8f4442007-02-10 01:44:20 -0800547 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700548
549 *new = ((struct iomem_region) { .next = iomem_regions,
550 .driver = driver,
551 .fd = fd,
552 .size = size,
553 .phys = 0,
554 .virt = 0 });
555 iomem_regions = new;
556 iomem_size += new->size + UM_KERN_PAGE_SIZE;
557
Jeff Dike9eae9b12007-02-10 01:44:20 -0800558 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700559 out_close:
Jeff Dike73c8f4442007-02-10 01:44:20 -0800560 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700561 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800562 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700563}