blob: abfc094c3c4920278b4a478d18d98bc8bc6407a7 [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>
Jeff Dike0f80bc82005-09-16 19:27:50 -070022#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "kern_util.h"
24#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "signal_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "sysdep/ptrace.h"
27#include "sysdep/sigcontext.h"
28#include "irq_user.h"
29#include "ptrace_user.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070030#include "mem_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "init.h"
32#include "os.h"
33#include "uml-config.h"
Jeff Diked67b5692005-07-07 17:56:49 -070034#include "tempfile.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070035#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "skas.h"
37#include "skas_ptrace.h"
38#include "registers.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jeff Dikeb85e9682005-07-28 21:16:01 -070040static int ptrace_child(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 int ret;
43 int pid = os_getpid(), ppid = getppid();
44 int sc_result;
45
Jeff Dike43b00fd2006-02-07 12:58:42 -080046 change_sig(SIGWINCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
48 perror("ptrace");
49 os_kill_process(pid, 0);
50 }
Jeff Dike73c8f4442007-02-10 01:44:20 -080051 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 /*This syscall will be intercepted by the parent. Don't call more than
54 * once, please.*/
55 sc_result = os_getpid();
56
57 if (sc_result == pid)
58 ret = 1; /*Nothing modified by the parent, we are running
59 normally.*/
60 else if (sc_result == ppid)
61 ret = 0; /*Expected in check_ptrace and check_sysemu when they
62 succeed in modifying the stack frame*/
63 else
64 ret = 2; /*Serious trouble! This could be caused by a bug in
65 host 2.6 SKAS3/2.6 patch before release -V6, together
66 with a bug in the UML code itself.*/
67 _exit(ret);
68}
69
Jeff Dike3a150e12007-02-10 01:44:28 -080070static void fatal_perror(char *str)
71{
72 perror(str);
73 exit(1);
74}
75
76static void fatal(char *fmt, ...)
77{
78 va_list list;
79
80 va_start(list, fmt);
81 vprintf(fmt, list);
82 va_end(list);
83 fflush(stdout);
84
85 exit(1);
86}
87
88static void non_fatal(char *fmt, ...)
89{
90 va_list list;
91
92 va_start(list, fmt);
93 vprintf(fmt, list);
94 va_end(list);
95 fflush(stdout);
96}
97
Jeff Dikeb85e9682005-07-28 21:16:01 -070098static int start_ptraced_child(void **stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700100 void *stack;
101 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int pid, n, status;
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700103
Jeff Dikec539ab72007-06-16 10:16:09 -0700104 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
105 PROT_READ | PROT_WRITE | PROT_EXEC,
Jeff Dikeb85e9682005-07-28 21:16:01 -0700106 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
107 if(stack == MAP_FAILED)
Jeff Dike3a150e12007-02-10 01:44:28 -0800108 fatal_perror("check_ptrace : mmap failed");
Jeff Dikec539ab72007-06-16 10:16:09 -0700109 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700110 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if(pid < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800112 fatal_perror("start_ptraced_child : clone failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
114 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800115 fatal_perror("check_ptrace : clone failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800117 fatal("check_ptrace : expected SIGSTOP, got status = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 status);
119
Jeff Dikeb85e9682005-07-28 21:16:01 -0700120 *stack_out = stack;
Jeff Dike9eae9b12007-02-10 01:44:20 -0800121 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700124/* When testing for SYSEMU support, if it is one of the broken versions, we
125 * must just avoid using sysemu, not panic, but only if SYSEMU features are
126 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700128 * must work anyway!
129 */
130static int stop_ptraced_child(int pid, void *stack, int exitcode,
Jeff Dike3a150e12007-02-10 01:44:28 -0800131 int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int status, n, ret = 0;
134
135 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800136 fatal_perror("stop_ptraced_child : ptrace failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 CATCH_EINTR(n = waitpid(pid, &status, 0));
138 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
139 int exit_with = WEXITSTATUS(status);
140 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800141 non_fatal("check_ptrace : child exited with status 2. "
Jeff Dikecf6aced2007-05-23 13:57:40 -0700142 "\nDisabling SYSEMU support.\n");
Jeff Dike3a150e12007-02-10 01:44:28 -0800143 non_fatal("check_ptrace : child exited with exitcode %d, while "
144 "expecting %d; status 0x%x\n", exit_with,
145 exitcode, status);
146 if (mustexit)
147 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ret = -1;
149 }
150
Jeff Dikec539ab72007-06-16 10:16:09 -0700151 if(munmap(stack, UM_KERN_PAGE_SIZE) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800152 fatal_perror("check_ptrace : munmap failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return ret;
154}
155
Jeff Dike7242a402007-02-10 01:44:19 -0800156/* Changed only during early boot */
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700157int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800158int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700159int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800160int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700161
162static int __init skas0_cmd_param(char *str, int* add)
163{
164 ptrace_faultinfo = proc_mm = 0;
165 return 0;
166}
167
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200168/* The two __uml_setup would conflict, without this stupid alias. */
169
170static int __init mode_skas0_cmd_param(char *str, int* add)
171 __attribute__((alias("skas0_cmd_param")));
172
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700173__uml_setup("skas0", skas0_cmd_param,
174 "skas0\n"
175 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
176 " you specify mode=tt.\n\n");
177
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200178__uml_setup("mode=skas0", mode_skas0_cmd_param,
179 "mode=skas0\n"
180 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
181 " specify mode=tt. Note that this was recently added - on \n"
182 " older kernels you must use simply \"skas0\".\n\n");
183
Jeff Dike7242a402007-02-10 01:44:19 -0800184/* Changed only during early boot */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700185static int force_sysemu_disabled = 0;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int __init nosysemu_cmd_param(char *str, int* add)
188{
189 force_sysemu_disabled = 1;
190 return 0;
191}
192
193__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700194"nosysemu\n"
195" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
196" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
197" behaviour of ptrace() and helps reducing host context switch rate.\n"
198" To make it working, you need a kernel patch for your host, too.\n"
199" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
200" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202static void __init check_sysemu(void)
203{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700204 void *stack;
Jeff Dikecf6aced2007-05-23 13:57:40 -0700205 unsigned long regs[MAX_REG_NR];
Jeff Dike9eae9b12007-02-10 01:44:20 -0800206 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jeff Dike3a150e12007-02-10 01:44:28 -0800208 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sysemu_supported = 0;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700210 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
213 goto fail;
214
215 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
216 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800217 fatal_perror("check_sysemu : wait failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800219 fatal("check_sysemu : expected SIGTRAP, got status = %d",
220 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jeff Dikecf6aced2007-05-23 13:57:40 -0700222 if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
223 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
224 if(PT_SYSCALL_NR(regs) != __NR_getpid){
225 non_fatal("check_sysemu got system call number %d, "
226 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
227 goto fail;
228 }
229
230 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
231 if(n < 0){
232 non_fatal("check_sysemu : failed to modify system call "
233 "return");
234 goto fail;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jeff Dikeb85e9682005-07-28 21:16:01 -0700237 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto fail_stopped;
239
240 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800241 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 set_using_sysemu(!force_sysemu_disabled);
243
Jeff Dike3a150e12007-02-10 01:44:28 -0800244 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700245 pid = start_ptraced_child(&stack);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700246
Jeff Dike3a150e12007-02-10 01:44:28 -0800247 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
248 (void *) PTRACE_O_TRACESYSGOOD) < 0))
249 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 while(1){
252 count++;
253 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
254 goto fail;
255 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
256 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800257 fatal_perror("check_ptrace : wait failed");
258
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700259 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!count)
Jeff Dike3a150e12007-02-10 01:44:28 -0800261 fatal("check_ptrace : SYSEMU_SINGLESTEP "
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700262 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
264 os_getpid());
265 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800266 fatal_perror("check_sysemu : failed to modify "
267 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269 }
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700270 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
271 count++;
272 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800273 fatal("check_ptrace : expected SIGTRAP or "
274 "(SIGTRAP | 0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700276 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto fail_stopped;
278
279 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800280 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if ( !force_sysemu_disabled )
283 set_using_sysemu(sysemu_supported);
284 return;
285
286fail:
Jeff Dikeb85e9682005-07-28 21:16:01 -0700287 stop_ptraced_child(pid, stack, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800289 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700292static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700294 void *stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int pid, syscall, n, status;
296
Jeff Dike3a150e12007-02-10 01:44:28 -0800297 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700298 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Jeff Dike3a150e12007-02-10 01:44:28 -0800300 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
301 (void *) PTRACE_O_TRACESYSGOOD) < 0))
302 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 while(1){
305 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800306 fatal_perror("check_ptrace : ptrace failed");
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
309 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800310 fatal_perror("check_ptrace : wait failed");
311
312 if(!WIFSTOPPED(status) ||
313 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
314 fatal("check_ptrace : expected (SIGTRAP|0x80), "
315 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
318 0);
319 if(syscall == __NR_getpid){
320 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
321 __NR_getppid);
322 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800323 fatal_perror("check_ptrace : failed to modify "
324 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 }
327 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700328 stop_ptraced_child(pid, stack, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800329 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 check_sysemu();
331}
332
Rob Landley966a082f2006-04-18 22:21:43 -0700333extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700334
Jeff Dike36e45462007-05-06 14:51:11 -0700335static void __init check_coredump_limit(void)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700336{
337 struct rlimit lim;
338 int err = getrlimit(RLIMIT_CORE, &lim);
339
340 if(err){
341 perror("Getting core dump limit");
342 return;
343 }
344
345 printf("Core dump limits :\n\tsoft - ");
346 if(lim.rlim_cur == RLIM_INFINITY)
347 printf("NONE\n");
348 else printf("%lu\n", lim.rlim_cur);
349
350 printf("\thard - ");
351 if(lim.rlim_max == RLIM_INFINITY)
352 printf("NONE\n");
353 else printf("%lu\n", lim.rlim_max);
354}
355
Jeff Dike36e45462007-05-06 14:51:11 -0700356void __init os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Jeff Dike1d94cda2007-05-06 14:51:00 -0700358 /* Print out the core dump limits early */
359 check_coredump_limit();
360
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700361 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700362
363 /* Need to check this early because mmapping happens before the
364 * kernel is running.
365 */
366 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700369static int __init noprocmm_cmd_param(char *str, int* add)
370{
371 proc_mm = 0;
372 return 0;
373}
374
375__uml_setup("noprocmm", noprocmm_cmd_param,
376"noprocmm\n"
377" Turns off usage of /proc/mm, even if host supports it.\n"
378" To support /proc/mm, the host needs to be patched using\n"
379" the current skas3 patch.\n\n");
380
381static int __init noptracefaultinfo_cmd_param(char *str, int* add)
382{
383 ptrace_faultinfo = 0;
384 return 0;
385}
386
387__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
388"noptracefaultinfo\n"
389" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
390" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
391" using the current skas3 patch.\n\n");
392
Bodo Stroesser858259c2005-11-07 00:58:55 -0800393static int __init noptraceldt_cmd_param(char *str, int* add)
394{
395 ptrace_ldt = 0;
396 return 0;
397}
398
399__uml_setup("noptraceldt", noptraceldt_cmd_param,
400"noptraceldt\n"
401" Turns off usage of PTRACE_LDT, even if host supports it.\n"
402" To support PTRACE_LDT, the host needs to be patched using\n"
403" the current skas3 patch.\n\n");
404
Bodo Stroesser858259c2005-11-07 00:58:55 -0800405static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct ptrace_faultinfo fi;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700408 void *stack;
Jeff Diked67b5692005-07-07 17:56:49 -0700409 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jeff Dike3a150e12007-02-10 01:44:28 -0800411 non_fatal(" - PTRACE_FAULTINFO...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700412 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
415 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700416 ptrace_faultinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if(errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800418 non_fatal("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700419 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700421 }
422 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700423 if (!ptrace_faultinfo)
Jeff Dike3a150e12007-02-10 01:44:28 -0800424 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700425 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800426 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 init_registers(pid);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700430 stop_ptraced_child(pid, stack, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Bodo Stroesser858259c2005-11-07 00:58:55 -0800433static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800435#ifdef PTRACE_LDT
436 void *stack;
437 int pid, n;
438 unsigned char ldtbuf[40];
439 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
440 .func = 2, /* read default ldt */
441 .ptr = ldtbuf,
442 .bytecount = sizeof(ldtbuf)};
443
Jeff Dike3a150e12007-02-10 01:44:28 -0800444 non_fatal(" - PTRACE_LDT...");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800445 pid = start_ptraced_child(&stack);
446
447 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
448 if (n < 0) {
449 if(errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800450 non_fatal("not found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800451 else {
452 perror("not found");
453 }
454 ptrace_ldt = 0;
455 }
456 else {
457 if(ptrace_ldt)
Jeff Dike3a150e12007-02-10 01:44:28 -0800458 non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800459 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800460 non_fatal("found, but use is disabled\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800461 }
462
463 stop_ptraced_child(pid, stack, 1, 1);
464#else
465 /* PTRACE_LDT might be disabled via cmdline option.
466 * We want to override this, else we might use the stub
467 * without real need
468 */
469 ptrace_ldt = 1;
470#endif
471}
472
473static inline void check_skas3_proc_mm(void)
474{
Jeff Dike3a150e12007-02-10 01:44:28 -0800475 non_fatal(" - /proc/mm...");
Jeff Dike73c8f4442007-02-10 01:44:20 -0800476 if (access("/proc/mm", W_OK) < 0) {
Jeff Dike9eae9b12007-02-10 01:44:20 -0800477 proc_mm = 0;
Jeff Dike3a150e12007-02-10 01:44:28 -0800478 perror("not found");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700479 }
480 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700481 if (!proc_mm)
Jeff Dike3a150e12007-02-10 01:44:28 -0800482 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700483 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800484 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800486}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Bodo Stroesser858259c2005-11-07 00:58:55 -0800488int can_do_skas(void)
489{
Jeff Dike3a150e12007-02-10 01:44:28 -0800490 non_fatal("Checking for the skas3 patch in the host:\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800491
492 check_skas3_proc_mm();
493 check_skas3_ptrace_faultinfo();
494 check_skas3_ptrace_ldt();
495
496 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
497 skas_needs_stub = 1;
498
Jeff Diked67b5692005-07-07 17:56:49 -0700499 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
Jeff Dike0f80bc82005-09-16 19:27:50 -0700501
Jeff Dike0f80bc82005-09-16 19:27:50 -0700502int __init parse_iomem(char *str, int *add)
503{
504 struct iomem_region *new;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800505 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700506 char *file, *driver;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800507 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700508
509 driver = str;
510 file = strchr(str,',');
511 if(file == NULL){
512 printf("parse_iomem : failed to parse iomem\n");
513 goto out;
514 }
515 *file = '\0';
516 file++;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800517 fd = open(file, O_RDWR, 0);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700518 if(fd < 0){
519 os_print_error(fd, "parse_iomem - Couldn't open io file");
520 goto out;
521 }
522
Jeff Dike73c8f4442007-02-10 01:44:20 -0800523 if(fstat64(fd, &buf) < 0){
524 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700525 goto out_close;
526 }
527
528 new = malloc(sizeof(*new));
529 if(new == NULL){
530 perror("Couldn't allocate iomem_region struct");
531 goto out_close;
532 }
533
Jeff Dike73c8f4442007-02-10 01:44:20 -0800534 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700535
536 *new = ((struct iomem_region) { .next = iomem_regions,
537 .driver = driver,
538 .fd = fd,
539 .size = size,
540 .phys = 0,
541 .virt = 0 });
542 iomem_regions = new;
543 iomem_size += new->size + UM_KERN_PAGE_SIZE;
544
Jeff Dike9eae9b12007-02-10 01:44:20 -0800545 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700546 out_close:
Jeff Dike73c8f4442007-02-10 01:44:20 -0800547 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700548 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800549 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700550}