blob: c5c36dbe819b3f9163dc61ff56450b6d134fda59 [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;
31 int pid = os_getpid(), ppid = getppid();
32 int sc_result;
33
Jeff Dike43b00fd2006-02-07 12:58:42 -080034 change_sig(SIGWINCH, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070035 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 perror("ptrace");
37 os_kill_process(pid, 0);
38 }
Jeff Dike73c8f442007-02-10 01:44:20 -080039 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jeff Dikeba180fd2007-10-16 01:27:00 -070041 /*
42 * This syscall will be intercepted by the parent. Don't call more than
43 * once, please.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 sc_result = os_getpid();
46
47 if (sc_result == pid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070048 /* Nothing modified by the parent, we are running normally. */
49 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 else if (sc_result == ppid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070051 /*
52 * Expected in check_ptrace and check_sysemu when they succeed
53 * in modifying the stack frame
54 */
55 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 else
Jeff Dikeba180fd2007-10-16 01:27:00 -070057 /* Serious trouble! This could be caused by a bug in host 2.6
58 * SKAS3/2.6 patch before release -V6, together with a bug in
59 * the UML code itself.
60 */
61 ret = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 _exit(ret);
63}
64
Jeff Dike3a150e12007-02-10 01:44:28 -080065static void fatal_perror(char *str)
66{
67 perror(str);
68 exit(1);
69}
70
71static void fatal(char *fmt, ...)
72{
73 va_list list;
74
75 va_start(list, fmt);
76 vprintf(fmt, list);
77 va_end(list);
78 fflush(stdout);
79
80 exit(1);
81}
82
83static void non_fatal(char *fmt, ...)
84{
85 va_list list;
86
87 va_start(list, fmt);
88 vprintf(fmt, list);
89 va_end(list);
90 fflush(stdout);
91}
92
Jeff Dike3cdaf452007-10-16 01:27:09 -070093static int start_ptraced_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int pid, n, status;
Gennady Sharapov60d339f2005-09-03 15:57:47 -070096
Jeff Dike3cdaf452007-10-16 01:27:09 -070097 pid = fork();
98 if (pid == 0)
99 ptrace_child();
100 else if (pid < 0)
101 fatal_perror("start_ptraced_child : fork failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700104 if (n < 0)
Jeff Dike3cdaf452007-10-16 01:27:09 -0700105 fatal_perror("check_ptrace : waitpid failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700106 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800107 fatal("check_ptrace : expected SIGSTOP, got status = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 status);
109
Jeff Dike9eae9b12007-02-10 01:44:20 -0800110 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700113/* When testing for SYSEMU support, if it is one of the broken versions, we
114 * must just avoid using sysemu, not panic, but only if SYSEMU features are
115 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700117 * must work anyway!
118 */
Jeff Dike3cdaf452007-10-16 01:27:09 -0700119static int stop_ptraced_child(int pid, int exitcode, int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 int status, n, ret = 0;
122
Jeff Dikeba180fd2007-10-16 01:27:00 -0700123 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800124 fatal_perror("stop_ptraced_child : ptrace failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 CATCH_EINTR(n = waitpid(pid, &status, 0));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700126 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int exit_with = WEXITSTATUS(status);
128 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800129 non_fatal("check_ptrace : child exited with status 2. "
Jeff Dikecf6aced2007-05-23 13:57:40 -0700130 "\nDisabling SYSEMU support.\n");
Jeff Dike3a150e12007-02-10 01:44:28 -0800131 non_fatal("check_ptrace : child exited with exitcode %d, while "
132 "expecting %d; status 0x%x\n", exit_with,
133 exitcode, status);
134 if (mustexit)
135 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 ret = -1;
137 }
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return ret;
140}
141
Jeff Dike7242a402007-02-10 01:44:19 -0800142/* Changed only during early boot */
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700143int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800144int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700145int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800146int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700147
148static int __init skas0_cmd_param(char *str, int* add)
149{
150 ptrace_faultinfo = proc_mm = 0;
151 return 0;
152}
153
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200154/* The two __uml_setup would conflict, without this stupid alias. */
155
156static int __init mode_skas0_cmd_param(char *str, int* add)
157 __attribute__((alias("skas0_cmd_param")));
158
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700159__uml_setup("skas0", skas0_cmd_param,
160 "skas0\n"
161 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
162 " you specify mode=tt.\n\n");
163
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200164__uml_setup("mode=skas0", mode_skas0_cmd_param,
165 "mode=skas0\n"
166 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
167 " specify mode=tt. Note that this was recently added - on \n"
168 " older kernels you must use simply \"skas0\".\n\n");
169
Jeff Dike7242a402007-02-10 01:44:19 -0800170/* Changed only during early boot */
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700171static int force_sysemu_disabled = 0;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int __init nosysemu_cmd_param(char *str, int* add)
174{
175 force_sysemu_disabled = 1;
176 return 0;
177}
178
179__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700180"nosysemu\n"
181" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
182" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
183" behaviour of ptrace() and helps reducing host context switch rate.\n"
184" To make it working, you need a kernel patch for your host, too.\n"
185" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
186" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188static void __init check_sysemu(void)
189{
Jeff Dikecf6aced2007-05-23 13:57:40 -0700190 unsigned long regs[MAX_REG_NR];
Jeff Dike9eae9b12007-02-10 01:44:20 -0800191 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jeff Dike3a150e12007-02-10 01:44:28 -0800193 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 sysemu_supported = 0;
Jeff Dike3cdaf452007-10-16 01:27:09 -0700195 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jeff Dikeba180fd2007-10-16 01:27:00 -0700197 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto fail;
199
200 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
201 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800202 fatal_perror("check_sysemu : wait failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700203 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800204 fatal("check_sysemu : expected SIGTRAP, got status = %d",
205 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jeff Dikeba180fd2007-10-16 01:27:00 -0700207 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
Jeff Dikecf6aced2007-05-23 13:57:40 -0700208 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700209 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700210 non_fatal("check_sysemu got system call number %d, "
211 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
212 goto fail;
213 }
214
215 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700216 if (n < 0) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700217 non_fatal("check_sysemu : failed to modify system call "
218 "return");
219 goto fail;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jeff Dike3cdaf452007-10-16 01:27:09 -0700222 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 goto fail_stopped;
224
225 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800226 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 set_using_sysemu(!force_sysemu_disabled);
228
Jeff Dike3a150e12007-02-10 01:44:28 -0800229 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700230 pid = start_ptraced_child();
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700231
Jeff Dikeba180fd2007-10-16 01:27:00 -0700232 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800233 (void *) PTRACE_O_TRACESYSGOOD) < 0))
234 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700235
Jeff Dikeba180fd2007-10-16 01:27:00 -0700236 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 count++;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700238 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto fail;
240 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700241 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800242 fatal_perror("check_ptrace : wait failed");
243
Jeff Dikeba180fd2007-10-16 01:27:00 -0700244 if (WIFSTOPPED(status) &&
245 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!count)
Jeff Dike3a150e12007-02-10 01:44:28 -0800247 fatal("check_ptrace : SYSEMU_SINGLESTEP "
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700248 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
250 os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700251 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800252 fatal_perror("check_sysemu : failed to modify "
253 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700256 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700257 count++;
258 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800259 fatal("check_ptrace : expected SIGTRAP or "
260 "(SIGTRAP | 0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700262 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto fail_stopped;
264
265 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800266 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jeff Dikeba180fd2007-10-16 01:27:00 -0700268 if (!force_sysemu_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 set_using_sysemu(sysemu_supported);
270 return;
271
272fail:
Jeff Dike3cdaf452007-10-16 01:27:09 -0700273 stop_ptraced_child(pid, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800275 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700278static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int pid, syscall, n, status;
281
Jeff Dike3a150e12007-02-10 01:44:28 -0800282 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700283 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jeff Dikeba180fd2007-10-16 01:27:00 -0700285 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800286 (void *) PTRACE_O_TRACESYSGOOD) < 0))
287 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Jeff Dikeba180fd2007-10-16 01:27:00 -0700289 while (1) {
290 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800291 fatal_perror("check_ptrace : ptrace failed");
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700294 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800295 fatal_perror("check_ptrace : wait failed");
296
Jeff Dikeba180fd2007-10-16 01:27:00 -0700297 if (!WIFSTOPPED(status) ||
Jeff Dike3a150e12007-02-10 01:44:28 -0800298 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
299 fatal("check_ptrace : expected (SIGTRAP|0x80), "
300 "got status = %d", status);
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
303 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700304 if (syscall == __NR_getpid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
306 __NR_getppid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700307 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800308 fatal_perror("check_ptrace : failed to modify "
309 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 }
312 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700313 stop_ptraced_child(pid, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800314 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 check_sysemu();
316}
317
Rob Landley966a0822006-04-18 22:21:43 -0700318extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700319
Jeff Dike36e45462007-05-06 14:51:11 -0700320static void __init check_coredump_limit(void)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700321{
322 struct rlimit lim;
323 int err = getrlimit(RLIMIT_CORE, &lim);
324
Jeff Dikeba180fd2007-10-16 01:27:00 -0700325 if (err) {
Jeff Dike1d94cda2007-05-06 14:51:00 -0700326 perror("Getting core dump limit");
327 return;
328 }
329
330 printf("Core dump limits :\n\tsoft - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700331 if (lim.rlim_cur == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700332 printf("NONE\n");
333 else printf("%lu\n", lim.rlim_cur);
334
335 printf("\thard - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700336 if (lim.rlim_max == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700337 printf("NONE\n");
338 else printf("%lu\n", lim.rlim_max);
339}
340
Jeff Dike36e45462007-05-06 14:51:11 -0700341void __init os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Jeff Dike1d94cda2007-05-06 14:51:00 -0700343 /* Print out the core dump limits early */
344 check_coredump_limit();
345
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700346 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700347
348 /* Need to check this early because mmapping happens before the
349 * kernel is running.
350 */
351 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700354static int __init noprocmm_cmd_param(char *str, int* add)
355{
356 proc_mm = 0;
357 return 0;
358}
359
360__uml_setup("noprocmm", noprocmm_cmd_param,
361"noprocmm\n"
362" Turns off usage of /proc/mm, even if host supports it.\n"
363" To support /proc/mm, the host needs to be patched using\n"
364" the current skas3 patch.\n\n");
365
366static int __init noptracefaultinfo_cmd_param(char *str, int* add)
367{
368 ptrace_faultinfo = 0;
369 return 0;
370}
371
372__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
373"noptracefaultinfo\n"
374" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
375" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
376" using the current skas3 patch.\n\n");
377
Bodo Stroesser858259c2005-11-07 00:58:55 -0800378static int __init noptraceldt_cmd_param(char *str, int* add)
379{
380 ptrace_ldt = 0;
381 return 0;
382}
383
384__uml_setup("noptraceldt", noptraceldt_cmd_param,
385"noptraceldt\n"
386" Turns off usage of PTRACE_LDT, even if host supports it.\n"
387" To support PTRACE_LDT, the host needs to be patched using\n"
388" the current skas3 patch.\n\n");
389
Bodo Stroesser858259c2005-11-07 00:58:55 -0800390static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct ptrace_faultinfo fi;
Jeff Diked67b5692005-07-07 17:56:49 -0700393 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jeff Dike3a150e12007-02-10 01:44:28 -0800395 non_fatal(" - PTRACE_FAULTINFO...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700396 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
399 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700400 ptrace_faultinfo = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700401 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800402 non_fatal("not found\n");
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700403 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700405 }
406 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700407 if (!ptrace_faultinfo)
Jeff Dike3a150e12007-02-10 01:44:28 -0800408 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700409 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800410 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 init_registers(pid);
Jeff Dike3cdaf452007-10-16 01:27:09 -0700414 stop_ptraced_child(pid, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Bodo Stroesser858259c2005-11-07 00:58:55 -0800417static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800419#ifdef PTRACE_LDT
Bodo Stroesser858259c2005-11-07 00:58:55 -0800420 int pid, n;
421 unsigned char ldtbuf[40];
422 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
423 .func = 2, /* read default ldt */
424 .ptr = ldtbuf,
425 .bytecount = sizeof(ldtbuf)};
426
Jeff Dike3a150e12007-02-10 01:44:28 -0800427 non_fatal(" - PTRACE_LDT...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700428 pid = start_ptraced_child();
Bodo Stroesser858259c2005-11-07 00:58:55 -0800429
430 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
431 if (n < 0) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700432 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800433 non_fatal("not found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800434 else {
435 perror("not found");
436 }
437 ptrace_ldt = 0;
438 }
439 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700440 if (ptrace_ldt)
Jeff Dike3a150e12007-02-10 01:44:28 -0800441 non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800442 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800443 non_fatal("found, but use is disabled\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800444 }
445
Jeff Dike3cdaf452007-10-16 01:27:09 -0700446 stop_ptraced_child(pid, 1, 1);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800447#else
448 /* PTRACE_LDT might be disabled via cmdline option.
449 * We want to override this, else we might use the stub
450 * without real need
451 */
452 ptrace_ldt = 1;
453#endif
454}
455
456static inline void check_skas3_proc_mm(void)
457{
Jeff Dike3a150e12007-02-10 01:44:28 -0800458 non_fatal(" - /proc/mm...");
Jeff Dike73c8f442007-02-10 01:44:20 -0800459 if (access("/proc/mm", W_OK) < 0) {
Jeff Dike9eae9b12007-02-10 01:44:20 -0800460 proc_mm = 0;
Jeff Dike3a150e12007-02-10 01:44:28 -0800461 perror("not found");
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700462 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700463 else if (!proc_mm)
464 non_fatal("found but disabled on command line\n");
465 else non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800466}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Bodo Stroesser858259c2005-11-07 00:58:55 -0800468int can_do_skas(void)
469{
Jeff Dike3a150e12007-02-10 01:44:28 -0800470 non_fatal("Checking for the skas3 patch in the host:\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800471
472 check_skas3_proc_mm();
473 check_skas3_ptrace_faultinfo();
474 check_skas3_ptrace_ldt();
475
Jeff Dikeba180fd2007-10-16 01:27:00 -0700476 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800477 skas_needs_stub = 1;
478
Jeff Diked67b5692005-07-07 17:56:49 -0700479 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
Jeff Dike0f80bc82005-09-16 19:27:50 -0700481
Jeff Dike0f80bc82005-09-16 19:27:50 -0700482int __init parse_iomem(char *str, int *add)
483{
484 struct iomem_region *new;
Jeff Dike73c8f442007-02-10 01:44:20 -0800485 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700486 char *file, *driver;
Jeff Dike73c8f442007-02-10 01:44:20 -0800487 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700488
489 driver = str;
490 file = strchr(str,',');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700491 if (file == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700492 printf("parse_iomem : failed to parse iomem\n");
493 goto out;
494 }
495 *file = '\0';
496 file++;
Jeff Dike73c8f442007-02-10 01:44:20 -0800497 fd = open(file, O_RDWR, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700498 if (fd < 0) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700499 os_print_error(fd, "parse_iomem - Couldn't open io file");
500 goto out;
501 }
502
Jeff Dikeba180fd2007-10-16 01:27:00 -0700503 if (fstat64(fd, &buf) < 0) {
Jeff Dike73c8f442007-02-10 01:44:20 -0800504 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700505 goto out_close;
506 }
507
508 new = malloc(sizeof(*new));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700509 if (new == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700510 perror("Couldn't allocate iomem_region struct");
511 goto out_close;
512 }
513
Jeff Dike73c8f442007-02-10 01:44:20 -0800514 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700515
516 *new = ((struct iomem_region) { .next = iomem_regions,
517 .driver = driver,
518 .fd = fd,
519 .size = size,
520 .phys = 0,
521 .virt = 0 });
522 iomem_regions = new;
523 iomem_size += new->size + UM_KERN_PAGE_SIZE;
524
Jeff Dike9eae9b12007-02-10 01:44:20 -0800525 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700526 out_close:
Jeff Dike73c8f442007-02-10 01:44:20 -0800527 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700528 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800529 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700530}