blob: 7b81f6c08a5eddda2aaab95ac4df4f5fd729f88a [file] [log] [blame]
Gennady Sharapov60d339f62005-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 Dike73c8f4442007-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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 _exit(ret);
64}
65
Jeff Dike3a150e12007-02-10 01:44:28 -080066static void fatal_perror(char *str)
67{
68 perror(str);
69 exit(1);
70}
71
72static void fatal(char *fmt, ...)
73{
74 va_list list;
75
76 va_start(list, fmt);
77 vprintf(fmt, list);
78 va_end(list);
79 fflush(stdout);
80
81 exit(1);
82}
83
84static void non_fatal(char *fmt, ...)
85{
86 va_list list;
87
88 va_start(list, fmt);
89 vprintf(fmt, list);
90 va_end(list);
91 fflush(stdout);
92}
93
Jeff Dike3cdaf452007-10-16 01:27:09 -070094static int start_ptraced_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int pid, n, status;
Gennady Sharapov60d339f62005-09-03 15:57:47 -070097
Jeff Dike3cdaf452007-10-16 01:27:09 -070098 pid = fork();
99 if (pid == 0)
100 ptrace_child();
101 else if (pid < 0)
102 fatal_perror("start_ptraced_child : fork failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700105 if (n < 0)
Jeff Dike3cdaf452007-10-16 01:27:09 -0700106 fatal_perror("check_ptrace : waitpid failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700107 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800108 fatal("check_ptrace : expected SIGSTOP, got status = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 status);
110
Jeff Dike9eae9b12007-02-10 01:44:20 -0800111 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700114/* When testing for SYSEMU support, if it is one of the broken versions, we
115 * must just avoid using sysemu, not panic, but only if SYSEMU features are
116 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700118 * must work anyway!
119 */
Jeff Dike3cdaf452007-10-16 01:27:09 -0700120static int stop_ptraced_child(int pid, int exitcode, int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 int status, n, ret = 0;
123
Jeff Dikeba180fd2007-10-16 01:27:00 -0700124 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800125 fatal_perror("stop_ptraced_child : ptrace failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 CATCH_EINTR(n = waitpid(pid, &status, 0));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700127 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int exit_with = WEXITSTATUS(status);
129 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800130 non_fatal("check_ptrace : child exited with status 2. "
Jeff Dikecf6aced2007-05-23 13:57:40 -0700131 "\nDisabling SYSEMU support.\n");
Jeff Dike3a150e12007-02-10 01:44:28 -0800132 non_fatal("check_ptrace : child exited with exitcode %d, while "
133 "expecting %d; status 0x%x\n", exit_with,
134 exitcode, status);
135 if (mustexit)
136 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 ret = -1;
138 }
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return ret;
141}
142
Jeff Dike7242a402007-02-10 01:44:19 -0800143/* Changed only during early boot */
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700144int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800145int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700146int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800147int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700148
149static int __init skas0_cmd_param(char *str, int* add)
150{
151 ptrace_faultinfo = proc_mm = 0;
152 return 0;
153}
154
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200155/* The two __uml_setup would conflict, without this stupid alias. */
156
157static int __init mode_skas0_cmd_param(char *str, int* add)
158 __attribute__((alias("skas0_cmd_param")));
159
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700160__uml_setup("skas0", skas0_cmd_param,
161 "skas0\n"
162 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
163 " you specify mode=tt.\n\n");
164
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200165__uml_setup("mode=skas0", mode_skas0_cmd_param,
166 "mode=skas0\n"
167 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
168 " specify mode=tt. Note that this was recently added - on \n"
169 " older kernels you must use simply \"skas0\".\n\n");
170
Jeff Dike7242a402007-02-10 01:44:19 -0800171/* Changed only during early boot */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700172static int force_sysemu_disabled = 0;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int __init nosysemu_cmd_param(char *str, int* add)
175{
176 force_sysemu_disabled = 1;
177 return 0;
178}
179
180__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700181"nosysemu\n"
182" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
183" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
184" behaviour of ptrace() and helps reducing host context switch rate.\n"
185" To make it working, you need a kernel patch for your host, too.\n"
186" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
187" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189static void __init check_sysemu(void)
190{
Jeff Dikecf6aced2007-05-23 13:57:40 -0700191 unsigned long regs[MAX_REG_NR];
Jeff Dike9eae9b12007-02-10 01:44:20 -0800192 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jeff Dike3a150e12007-02-10 01:44:28 -0800194 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 sysemu_supported = 0;
Jeff Dike3cdaf452007-10-16 01:27:09 -0700196 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jeff Dikeba180fd2007-10-16 01:27:00 -0700198 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto fail;
200
201 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
202 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800203 fatal_perror("check_sysemu : wait failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700204 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800205 fatal("check_sysemu : expected SIGTRAP, got status = %d",
206 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
Jeff Dikecf6aced2007-05-23 13:57:40 -0700209 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700210 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700211 non_fatal("check_sysemu got system call number %d, "
212 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
213 goto fail;
214 }
215
216 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700217 if (n < 0) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700218 non_fatal("check_sysemu : failed to modify system call "
219 "return");
220 goto fail;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Jeff Dike3cdaf452007-10-16 01:27:09 -0700223 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto fail_stopped;
225
226 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800227 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 set_using_sysemu(!force_sysemu_disabled);
229
Jeff Dike3a150e12007-02-10 01:44:28 -0800230 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700231 pid = start_ptraced_child();
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700232
Jeff Dikeba180fd2007-10-16 01:27:00 -0700233 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800234 (void *) PTRACE_O_TRACESYSGOOD) < 0))
235 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700236
Jeff Dikeba180fd2007-10-16 01:27:00 -0700237 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 count++;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700239 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto fail;
241 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700242 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800243 fatal_perror("check_ptrace : wait failed");
244
Jeff Dikeba180fd2007-10-16 01:27:00 -0700245 if (WIFSTOPPED(status) &&
246 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!count)
Jeff Dike3a150e12007-02-10 01:44:28 -0800248 fatal("check_ptrace : SYSEMU_SINGLESTEP "
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700249 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
251 os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700252 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800253 fatal_perror("check_sysemu : failed to modify "
254 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700257 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700258 count++;
259 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800260 fatal("check_ptrace : expected SIGTRAP or "
261 "(SIGTRAP | 0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700263 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto fail_stopped;
265
266 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800267 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jeff Dikeba180fd2007-10-16 01:27:00 -0700269 if (!force_sysemu_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 set_using_sysemu(sysemu_supported);
271 return;
272
273fail:
Jeff Dike3cdaf452007-10-16 01:27:09 -0700274 stop_ptraced_child(pid, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800276 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700279static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int pid, syscall, n, status;
282
Jeff Dike3a150e12007-02-10 01:44:28 -0800283 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700284 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jeff Dikeba180fd2007-10-16 01:27:00 -0700286 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800287 (void *) PTRACE_O_TRACESYSGOOD) < 0))
288 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Jeff Dikeba180fd2007-10-16 01:27:00 -0700290 while (1) {
291 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800292 fatal_perror("check_ptrace : ptrace failed");
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700295 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800296 fatal_perror("check_ptrace : wait failed");
297
Jeff Dikeba180fd2007-10-16 01:27:00 -0700298 if (!WIFSTOPPED(status) ||
Jeff Dike3a150e12007-02-10 01:44:28 -0800299 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
300 fatal("check_ptrace : expected (SIGTRAP|0x80), "
301 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
304 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700305 if (syscall == __NR_getpid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
307 __NR_getppid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700308 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800309 fatal_perror("check_ptrace : failed to modify "
310 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 }
313 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700314 stop_ptraced_child(pid, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800315 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 check_sysemu();
317}
318
Rob Landley966a082f2006-04-18 22:21:43 -0700319extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700320
Jeff Dike36e45462007-05-06 14:51:11 -0700321static void __init check_coredump_limit(void)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700322{
323 struct rlimit lim;
324 int err = getrlimit(RLIMIT_CORE, &lim);
325
Jeff Dikeba180fd2007-10-16 01:27:00 -0700326 if (err) {
Jeff Dike1d94cda2007-05-06 14:51:00 -0700327 perror("Getting core dump limit");
328 return;
329 }
330
331 printf("Core dump limits :\n\tsoft - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700332 if (lim.rlim_cur == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700333 printf("NONE\n");
334 else printf("%lu\n", lim.rlim_cur);
335
336 printf("\thard - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700337 if (lim.rlim_max == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700338 printf("NONE\n");
339 else printf("%lu\n", lim.rlim_max);
340}
341
Jeff Dike36e45462007-05-06 14:51:11 -0700342void __init os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Jeff Dike1d94cda2007-05-06 14:51:00 -0700344 /* Print out the core dump limits early */
345 check_coredump_limit();
346
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700347 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700348
349 /* Need to check this early because mmapping happens before the
350 * kernel is running.
351 */
352 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700355static int __init noprocmm_cmd_param(char *str, int* add)
356{
357 proc_mm = 0;
358 return 0;
359}
360
361__uml_setup("noprocmm", noprocmm_cmd_param,
362"noprocmm\n"
363" Turns off usage of /proc/mm, even if host supports it.\n"
364" To support /proc/mm, the host needs to be patched using\n"
365" the current skas3 patch.\n\n");
366
367static int __init noptracefaultinfo_cmd_param(char *str, int* add)
368{
369 ptrace_faultinfo = 0;
370 return 0;
371}
372
373__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
374"noptracefaultinfo\n"
375" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
376" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
377" using the current skas3 patch.\n\n");
378
Bodo Stroesser858259c2005-11-07 00:58:55 -0800379static int __init noptraceldt_cmd_param(char *str, int* add)
380{
381 ptrace_ldt = 0;
382 return 0;
383}
384
385__uml_setup("noptraceldt", noptraceldt_cmd_param,
386"noptraceldt\n"
387" Turns off usage of PTRACE_LDT, even if host supports it.\n"
388" To support PTRACE_LDT, the host needs to be patched using\n"
389" the current skas3 patch.\n\n");
390
Bodo Stroesser858259c2005-11-07 00:58:55 -0800391static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct ptrace_faultinfo fi;
Jeff Diked67b5692005-07-07 17:56:49 -0700394 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jeff Dike3a150e12007-02-10 01:44:28 -0800396 non_fatal(" - PTRACE_FAULTINFO...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700397 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
400 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700401 ptrace_faultinfo = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700402 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800403 non_fatal("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700404 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700406 }
407 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700408 if (!ptrace_faultinfo)
Jeff Dike3a150e12007-02-10 01:44:28 -0800409 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700410 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800411 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 init_registers(pid);
Jeff Dike3cdaf452007-10-16 01:27:09 -0700415 stop_ptraced_child(pid, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Bodo Stroesser858259c2005-11-07 00:58:55 -0800418static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800420#ifdef PTRACE_LDT
Bodo Stroesser858259c2005-11-07 00:58:55 -0800421 int pid, n;
422 unsigned char ldtbuf[40];
423 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
424 .func = 2, /* read default ldt */
425 .ptr = ldtbuf,
426 .bytecount = sizeof(ldtbuf)};
427
Jeff Dike3a150e12007-02-10 01:44:28 -0800428 non_fatal(" - PTRACE_LDT...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700429 pid = start_ptraced_child();
Bodo Stroesser858259c2005-11-07 00:58:55 -0800430
431 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
432 if (n < 0) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700433 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800434 non_fatal("not found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800435 else {
436 perror("not found");
437 }
438 ptrace_ldt = 0;
439 }
440 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700441 if (ptrace_ldt)
Jeff Dike3a150e12007-02-10 01:44:28 -0800442 non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800443 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800444 non_fatal("found, but use is disabled\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800445 }
446
Jeff Dike3cdaf452007-10-16 01:27:09 -0700447 stop_ptraced_child(pid, 1, 1);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800448#else
449 /* PTRACE_LDT might be disabled via cmdline option.
450 * We want to override this, else we might use the stub
451 * without real need
452 */
453 ptrace_ldt = 1;
454#endif
455}
456
457static inline void check_skas3_proc_mm(void)
458{
Jeff Dike3a150e12007-02-10 01:44:28 -0800459 non_fatal(" - /proc/mm...");
Jeff Dike73c8f4442007-02-10 01:44:20 -0800460 if (access("/proc/mm", W_OK) < 0) {
Jeff Dike9eae9b12007-02-10 01:44:20 -0800461 proc_mm = 0;
Jeff Dike3a150e12007-02-10 01:44:28 -0800462 perror("not found");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700463 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700464 else if (!proc_mm)
465 non_fatal("found but disabled on command line\n");
466 else non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Bodo Stroesser858259c2005-11-07 00:58:55 -0800469int can_do_skas(void)
470{
Jeff Dike3a150e12007-02-10 01:44:28 -0800471 non_fatal("Checking for the skas3 patch in the host:\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800472
473 check_skas3_proc_mm();
474 check_skas3_ptrace_faultinfo();
475 check_skas3_ptrace_ldt();
476
Jeff Dikeba180fd2007-10-16 01:27:00 -0700477 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800478 skas_needs_stub = 1;
479
Jeff Diked67b5692005-07-07 17:56:49 -0700480 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
Jeff Dike0f80bc82005-09-16 19:27:50 -0700482
Jeff Dike0f80bc82005-09-16 19:27:50 -0700483int __init parse_iomem(char *str, int *add)
484{
485 struct iomem_region *new;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800486 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700487 char *file, *driver;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800488 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700489
490 driver = str;
491 file = strchr(str,',');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700492 if (file == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700493 printf("parse_iomem : failed to parse iomem\n");
494 goto out;
495 }
496 *file = '\0';
497 file++;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800498 fd = open(file, O_RDWR, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700499 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700500 perror("parse_iomem - Couldn't open io file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700501 goto out;
502 }
503
Jeff Dikeba180fd2007-10-16 01:27:00 -0700504 if (fstat64(fd, &buf) < 0) {
Jeff Dike73c8f4442007-02-10 01:44:20 -0800505 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700506 goto out_close;
507 }
508
509 new = malloc(sizeof(*new));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700510 if (new == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700511 perror("Couldn't allocate iomem_region struct");
512 goto out_close;
513 }
514
Jeff Dike73c8f4442007-02-10 01:44:20 -0800515 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700516
517 *new = ((struct iomem_region) { .next = iomem_regions,
518 .driver = driver,
519 .fd = fd,
520 .size = size,
521 .phys = 0,
522 .virt = 0 });
523 iomem_regions = new;
524 iomem_size += new->size + UM_KERN_PAGE_SIZE;
525
Jeff Dike9eae9b12007-02-10 01:44:20 -0800526 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700527 out_close:
Jeff Dike73c8f4442007-02-10 01:44:20 -0800528 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700529 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800530 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700531}