blob: b616e15638fbc8c1aa5a8bdf710862715124de42 [file] [log] [blame]
Gennady Sharapov60d339f2005-09-03 15:57:47 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdio.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07007#include <stdlib.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07008#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <errno.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include <fcntl.h>
12#include <sched.h>
13#include <signal.h>
14#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <sys/mman.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070016#include <sys/ptrace.h>
17#include <sys/stat.h>
18#include <sys/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "init.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070021#include "kern_constants.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070022#include "os.h"
23#include "mem_user.h"
24#include "ptrace_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "registers.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070026#include "skas_ptrace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Jeff Dike3cdaf452007-10-16 01:27:09 -070028static int ptrace_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 int ret;
Jeff Dike512b6fb2007-10-16 01:27:11 -070031 /* Calling os_getpid because some libcs cached getpid incorrectly */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 int pid = os_getpid(), ppid = getppid();
33 int sc_result;
34
Jeff Dike43b00fd2006-02-07 12:58:42 -080035 change_sig(SIGWINCH, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070036 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 perror("ptrace");
Jeff Dike512b6fb2007-10-16 01:27:11 -070038 kill(pid, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
Jeff Dike73c8f442007-02-10 01:44:20 -080040 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jeff Dikeba180fd2007-10-16 01:27:00 -070042 /*
43 * This syscall will be intercepted by the parent. Don't call more than
44 * once, please.
45 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 sc_result = os_getpid();
47
48 if (sc_result == pid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070049 /* Nothing modified by the parent, we are running normally. */
50 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 else if (sc_result == ppid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070052 /*
53 * Expected in check_ptrace and check_sysemu when they succeed
54 * in modifying the stack frame
55 */
56 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 else
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 /* Serious trouble! This could be caused by a bug in host 2.6
59 * SKAS3/2.6 patch before release -V6, together with a bug in
60 * the UML code itself.
61 */
62 ret = 2;
Jeff Dikebf8fde72008-02-04 22:31:04 -080063
64 exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
WANG Congc9a30722008-02-04 22:30:35 -080067static void fatal_perror(const char *str)
Jeff Dike3a150e12007-02-10 01:44:28 -080068{
69 perror(str);
70 exit(1);
71}
72
73static void fatal(char *fmt, ...)
74{
75 va_list list;
76
77 va_start(list, fmt);
78 vprintf(fmt, list);
79 va_end(list);
80 fflush(stdout);
81
82 exit(1);
83}
84
85static void non_fatal(char *fmt, ...)
86{
87 va_list list;
88
89 va_start(list, fmt);
90 vprintf(fmt, list);
91 va_end(list);
92 fflush(stdout);
93}
94
Jeff Dike3cdaf452007-10-16 01:27:09 -070095static int start_ptraced_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int pid, n, status;
Gennady Sharapov60d339f2005-09-03 15:57:47 -070098
Jeff Dike3cdaf452007-10-16 01:27:09 -070099 pid = fork();
100 if (pid == 0)
101 ptrace_child();
102 else if (pid < 0)
103 fatal_perror("start_ptraced_child : fork failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700106 if (n < 0)
Jeff Dike3cdaf452007-10-16 01:27:09 -0700107 fatal_perror("check_ptrace : waitpid failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700108 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800109 fatal("check_ptrace : expected SIGSTOP, got status = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 status);
111
Jeff Dike9eae9b12007-02-10 01:44:20 -0800112 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700115/* When testing for SYSEMU support, if it is one of the broken versions, we
116 * must just avoid using sysemu, not panic, but only if SYSEMU features are
117 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700119 * must work anyway!
120 */
Jeff Dike3cdaf452007-10-16 01:27:09 -0700121static int stop_ptraced_child(int pid, int exitcode, int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 int status, n, ret = 0;
124
Jeff Dikeba180fd2007-10-16 01:27:00 -0700125 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800126 fatal_perror("stop_ptraced_child : ptrace failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 CATCH_EINTR(n = waitpid(pid, &status, 0));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700128 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int exit_with = WEXITSTATUS(status);
130 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800131 non_fatal("check_ptrace : child exited with status 2. "
Jeff Dikecf6aced2007-05-23 13:57:40 -0700132 "\nDisabling SYSEMU support.\n");
Jeff Dike3a150e12007-02-10 01:44:28 -0800133 non_fatal("check_ptrace : child exited with exitcode %d, while "
134 "expecting %d; status 0x%x\n", exit_with,
135 exitcode, status);
136 if (mustexit)
137 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 ret = -1;
139 }
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return ret;
142}
143
Jeff Dike7242a402007-02-10 01:44:19 -0800144/* Changed only during early boot */
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700145int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800146int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700147int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800148int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700149
150static int __init skas0_cmd_param(char *str, int* add)
151{
152 ptrace_faultinfo = proc_mm = 0;
153 return 0;
154}
155
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200156/* The two __uml_setup would conflict, without this stupid alias. */
157
158static int __init mode_skas0_cmd_param(char *str, int* add)
159 __attribute__((alias("skas0_cmd_param")));
160
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700161__uml_setup("skas0", skas0_cmd_param,
162 "skas0\n"
163 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
164 " you specify mode=tt.\n\n");
165
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200166__uml_setup("mode=skas0", mode_skas0_cmd_param,
167 "mode=skas0\n"
168 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
169 " specify mode=tt. Note that this was recently added - on \n"
170 " older kernels you must use simply \"skas0\".\n\n");
171
Jeff Dike7242a402007-02-10 01:44:19 -0800172/* Changed only during early boot */
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700173static int force_sysemu_disabled = 0;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static int __init nosysemu_cmd_param(char *str, int* add)
176{
177 force_sysemu_disabled = 1;
178 return 0;
179}
180
181__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700182"nosysemu\n"
183" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
184" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
185" behaviour of ptrace() and helps reducing host context switch rate.\n"
186" To make it working, you need a kernel patch for your host, too.\n"
187" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
188" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190static void __init check_sysemu(void)
191{
Jeff Dikecf6aced2007-05-23 13:57:40 -0700192 unsigned long regs[MAX_REG_NR];
Jeff Dike9eae9b12007-02-10 01:44:20 -0800193 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jeff Dike3a150e12007-02-10 01:44:28 -0800195 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 sysemu_supported = 0;
Jeff Dike3cdaf452007-10-16 01:27:09 -0700197 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jeff Dikeba180fd2007-10-16 01:27:00 -0700199 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto fail;
201
202 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
203 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800204 fatal_perror("check_sysemu : wait failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700205 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800206 fatal("check_sysemu : expected SIGTRAP, got status = %d",
207 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jeff Dikeba180fd2007-10-16 01:27:00 -0700209 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
Jeff Dikecf6aced2007-05-23 13:57:40 -0700210 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700211 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700212 non_fatal("check_sysemu got system call number %d, "
213 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
214 goto fail;
215 }
216
217 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700218 if (n < 0) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700219 non_fatal("check_sysemu : failed to modify system call "
220 "return");
221 goto fail;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jeff Dike3cdaf452007-10-16 01:27:09 -0700224 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 goto fail_stopped;
226
227 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800228 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 set_using_sysemu(!force_sysemu_disabled);
230
Jeff Dike3a150e12007-02-10 01:44:28 -0800231 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700232 pid = start_ptraced_child();
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700233
Jeff Dikeba180fd2007-10-16 01:27:00 -0700234 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800235 (void *) PTRACE_O_TRACESYSGOOD) < 0))
236 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700237
Jeff Dikeba180fd2007-10-16 01:27:00 -0700238 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 count++;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700240 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto fail;
242 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700243 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800244 fatal_perror("check_ptrace : wait failed");
245
Jeff Dikeba180fd2007-10-16 01:27:00 -0700246 if (WIFSTOPPED(status) &&
247 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!count)
Jeff Dike3a150e12007-02-10 01:44:28 -0800249 fatal("check_ptrace : SYSEMU_SINGLESTEP "
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700250 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
252 os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700253 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800254 fatal_perror("check_sysemu : failed to modify "
255 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700258 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700259 count++;
260 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800261 fatal("check_ptrace : expected SIGTRAP or "
262 "(SIGTRAP | 0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700264 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto fail_stopped;
266
267 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800268 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jeff Dikeba180fd2007-10-16 01:27:00 -0700270 if (!force_sysemu_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 set_using_sysemu(sysemu_supported);
272 return;
273
274fail:
Jeff Dike3cdaf452007-10-16 01:27:09 -0700275 stop_ptraced_child(pid, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800277 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700280static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int pid, syscall, n, status;
283
Jeff Dike3a150e12007-02-10 01:44:28 -0800284 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700285 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jeff Dikeba180fd2007-10-16 01:27:00 -0700287 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800288 (void *) PTRACE_O_TRACESYSGOOD) < 0))
289 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jeff Dikeba180fd2007-10-16 01:27:00 -0700291 while (1) {
292 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800293 fatal_perror("check_ptrace : ptrace failed");
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700296 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800297 fatal_perror("check_ptrace : wait failed");
298
Jeff Dikeba180fd2007-10-16 01:27:00 -0700299 if (!WIFSTOPPED(status) ||
Jeff Dike3a150e12007-02-10 01:44:28 -0800300 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
301 fatal("check_ptrace : expected (SIGTRAP|0x80), "
302 "got status = %d", status);
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
305 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700306 if (syscall == __NR_getpid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
308 __NR_getppid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700309 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800310 fatal_perror("check_ptrace : failed to modify "
311 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
313 }
314 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700315 stop_ptraced_child(pid, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800316 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 check_sysemu();
318}
319
Rob Landley966a0822006-04-18 22:21:43 -0700320extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700321
Jeff Dike36e45462007-05-06 14:51:11 -0700322static void __init check_coredump_limit(void)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700323{
324 struct rlimit lim;
325 int err = getrlimit(RLIMIT_CORE, &lim);
326
Jeff Dikeba180fd2007-10-16 01:27:00 -0700327 if (err) {
Jeff Dike1d94cda2007-05-06 14:51:00 -0700328 perror("Getting core dump limit");
329 return;
330 }
331
332 printf("Core dump limits :\n\tsoft - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700333 if (lim.rlim_cur == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700334 printf("NONE\n");
335 else printf("%lu\n", lim.rlim_cur);
336
337 printf("\thard - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700338 if (lim.rlim_max == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700339 printf("NONE\n");
340 else printf("%lu\n", lim.rlim_max);
341}
342
Jeff Dike36e45462007-05-06 14:51:11 -0700343void __init os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Jeff Dike576c0132008-02-04 22:31:22 -0800345 int pid;
346
Jeff Dike1d94cda2007-05-06 14:51:00 -0700347 /* Print out the core dump limits early */
348 check_coredump_limit();
349
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700350 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700351
352 /* Need to check this early because mmapping happens before the
353 * kernel is running.
354 */
355 check_tmpexec();
Jeff Dike576c0132008-02-04 22:31:22 -0800356
357 pid = start_ptraced_child();
358 if (init_registers(pid))
359 fatal("Failed to initialize default registers");
360 stop_ptraced_child(pid, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700363static int __init noprocmm_cmd_param(char *str, int* add)
364{
365 proc_mm = 0;
366 return 0;
367}
368
369__uml_setup("noprocmm", noprocmm_cmd_param,
370"noprocmm\n"
371" Turns off usage of /proc/mm, even if host supports it.\n"
372" To support /proc/mm, the host needs to be patched using\n"
373" the current skas3 patch.\n\n");
374
375static int __init noptracefaultinfo_cmd_param(char *str, int* add)
376{
377 ptrace_faultinfo = 0;
378 return 0;
379}
380
381__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
382"noptracefaultinfo\n"
383" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
384" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
385" using the current skas3 patch.\n\n");
386
Bodo Stroesser858259c2005-11-07 00:58:55 -0800387static int __init noptraceldt_cmd_param(char *str, int* add)
388{
389 ptrace_ldt = 0;
390 return 0;
391}
392
393__uml_setup("noptraceldt", noptraceldt_cmd_param,
394"noptraceldt\n"
395" Turns off usage of PTRACE_LDT, even if host supports it.\n"
396" To support PTRACE_LDT, the host needs to be patched using\n"
397" the current skas3 patch.\n\n");
398
Bodo Stroesser858259c2005-11-07 00:58:55 -0800399static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct ptrace_faultinfo fi;
Jeff Diked67b5692005-07-07 17:56:49 -0700402 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jeff Dike3a150e12007-02-10 01:44:28 -0800404 non_fatal(" - PTRACE_FAULTINFO...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700405 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
408 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700409 ptrace_faultinfo = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700410 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800411 non_fatal("not found\n");
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700412 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700414 }
415 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700416 if (!ptrace_faultinfo)
Jeff Dike3a150e12007-02-10 01:44:28 -0800417 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700418 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800419 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Jeff Dike3cdaf452007-10-16 01:27:09 -0700422 stop_ptraced_child(pid, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Bodo Stroesser858259c2005-11-07 00:58:55 -0800425static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800427#ifdef PTRACE_LDT
Bodo Stroesser858259c2005-11-07 00:58:55 -0800428 int pid, n;
429 unsigned char ldtbuf[40];
430 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
431 .func = 2, /* read default ldt */
432 .ptr = ldtbuf,
433 .bytecount = sizeof(ldtbuf)};
434
Jeff Dike3a150e12007-02-10 01:44:28 -0800435 non_fatal(" - PTRACE_LDT...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700436 pid = start_ptraced_child();
Bodo Stroesser858259c2005-11-07 00:58:55 -0800437
438 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
439 if (n < 0) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700440 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800441 non_fatal("not found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800442 else {
443 perror("not found");
444 }
445 ptrace_ldt = 0;
446 }
447 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700448 if (ptrace_ldt)
Jeff Dike3a150e12007-02-10 01:44:28 -0800449 non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800450 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800451 non_fatal("found, but use is disabled\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800452 }
453
Jeff Dike3cdaf452007-10-16 01:27:09 -0700454 stop_ptraced_child(pid, 1, 1);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800455#else
456 /* PTRACE_LDT might be disabled via cmdline option.
457 * We want to override this, else we might use the stub
458 * without real need
459 */
460 ptrace_ldt = 1;
461#endif
462}
463
464static inline void check_skas3_proc_mm(void)
465{
Jeff Dike3a150e12007-02-10 01:44:28 -0800466 non_fatal(" - /proc/mm...");
Jeff Dike73c8f442007-02-10 01:44:20 -0800467 if (access("/proc/mm", W_OK) < 0) {
Jeff Dike9eae9b12007-02-10 01:44:20 -0800468 proc_mm = 0;
Jeff Dike3a150e12007-02-10 01:44:28 -0800469 perror("not found");
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700470 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700471 else if (!proc_mm)
472 non_fatal("found but disabled on command line\n");
473 else non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Karol Swietlicki6b7e9672008-02-04 22:30:50 -0800476void can_do_skas(void)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800477{
Jeff Dike3a150e12007-02-10 01:44:28 -0800478 non_fatal("Checking for the skas3 patch in the host:\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800479
480 check_skas3_proc_mm();
481 check_skas3_ptrace_faultinfo();
482 check_skas3_ptrace_ldt();
483
Jeff Dikeba180fd2007-10-16 01:27:00 -0700484 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800485 skas_needs_stub = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Jeff Dike0f80bc82005-09-16 19:27:50 -0700487
Jeff Dike0f80bc82005-09-16 19:27:50 -0700488int __init parse_iomem(char *str, int *add)
489{
490 struct iomem_region *new;
Jeff Dike73c8f442007-02-10 01:44:20 -0800491 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700492 char *file, *driver;
Jeff Dike73c8f442007-02-10 01:44:20 -0800493 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700494
495 driver = str;
496 file = strchr(str,',');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700497 if (file == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700498 printf("parse_iomem : failed to parse iomem\n");
499 goto out;
500 }
501 *file = '\0';
502 file++;
Jeff Dike73c8f442007-02-10 01:44:20 -0800503 fd = open(file, O_RDWR, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700504 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700505 perror("parse_iomem - Couldn't open io file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700506 goto out;
507 }
508
Jeff Dikeba180fd2007-10-16 01:27:00 -0700509 if (fstat64(fd, &buf) < 0) {
Jeff Dike73c8f442007-02-10 01:44:20 -0800510 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700511 goto out_close;
512 }
513
514 new = malloc(sizeof(*new));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700515 if (new == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700516 perror("Couldn't allocate iomem_region struct");
517 goto out_close;
518 }
519
Jeff Dike73c8f442007-02-10 01:44:20 -0800520 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700521
522 *new = ((struct iomem_region) { .next = iomem_regions,
523 .driver = driver,
524 .fd = fd,
525 .size = size,
526 .phys = 0,
527 .virt = 0 });
528 iomem_regions = new;
529 iomem_size += new->size + UM_KERN_PAGE_SIZE;
530
Jeff Dike9eae9b12007-02-10 01:44:20 -0800531 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700532 out_close:
Jeff Dike73c8f442007-02-10 01:44:20 -0800533 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700534 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800535 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700536}