blob: 337518c5042a5cb4895e389202e6a4c5bc9654b2 [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/stat.h>
17#include <sys/wait.h>
Sergei Trofimovichfdfa4c92012-12-30 01:37:30 +030018#include <sys/time.h>
19#include <sys/resource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/unistd.h>
Al Viro37185b32012-10-08 03:27:32 +010021#include <init.h>
22#include <os.h>
23#include <mem_user.h>
24#include <ptrace_user.h>
25#include <registers.h>
26#include <skas.h>
27#include <skas_ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
WANG Cong626c59f2008-04-28 02:13:53 -070029static void ptrace_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 int ret;
Jeff Dike512b6fb2007-10-16 01:27:11 -070032 /* Calling os_getpid because some libcs cached getpid incorrectly */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int pid = os_getpid(), ppid = getppid();
34 int sc_result;
35
WANG Cong626c59f2008-04-28 02:13:53 -070036 if (change_sig(SIGWINCH, 0) < 0 ||
37 ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 perror("ptrace");
Jeff Dike512b6fb2007-10-16 01:27:11 -070039 kill(pid, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 }
Jeff Dike73c8f4442007-02-10 01:44:20 -080041 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jeff Dikeba180fd2007-10-16 01:27:00 -070043 /*
44 * This syscall will be intercepted by the parent. Don't call more than
45 * once, please.
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 sc_result = os_getpid();
48
49 if (sc_result == pid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070050 /* Nothing modified by the parent, we are running normally. */
51 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 else if (sc_result == ppid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070053 /*
54 * Expected in check_ptrace and check_sysemu when they succeed
55 * in modifying the stack frame
56 */
57 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 else
Jeff Dikeba180fd2007-10-16 01:27:00 -070059 /* Serious trouble! This could be caused by a bug in host 2.6
60 * SKAS3/2.6 patch before release -V6, together with a bug in
61 * the UML code itself.
62 */
63 ret = 2;
Jeff Dikebf8fde72008-02-04 22:31:04 -080064
65 exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
WANG Congc9a30722008-02-04 22:30:35 -080068static void fatal_perror(const char *str)
Jeff Dike3a150e12007-02-10 01:44:28 -080069{
70 perror(str);
71 exit(1);
72}
73
74static void fatal(char *fmt, ...)
75{
76 va_list list;
77
78 va_start(list, fmt);
WANG Cong626c59f2008-04-28 02:13:53 -070079 vfprintf(stderr, fmt, list);
Jeff Dike3a150e12007-02-10 01:44:28 -080080 va_end(list);
Jeff Dike3a150e12007-02-10 01:44:28 -080081
82 exit(1);
83}
84
85static void non_fatal(char *fmt, ...)
86{
87 va_list list;
88
89 va_start(list, fmt);
WANG Cong626c59f2008-04-28 02:13:53 -070090 vfprintf(stderr, fmt, list);
Jeff Dike3a150e12007-02-10 01:44:28 -080091 va_end(list);
Jeff Dike3a150e12007-02-10 01:44:28 -080092}
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 Dikef1ef9162008-06-12 15:21:41 -0700124 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
125 perror("stop_ptraced_child : ptrace failed");
126 return -1;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 CATCH_EINTR(n = waitpid(pid, &status, 0));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int exit_with = WEXITSTATUS(status);
131 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800132 non_fatal("check_ptrace : child exited with status 2. "
Jeff Dikecf6aced2007-05-23 13:57:40 -0700133 "\nDisabling SYSEMU support.\n");
Jeff Dike3a150e12007-02-10 01:44:28 -0800134 non_fatal("check_ptrace : child exited with exitcode %d, while "
135 "expecting %d; status 0x%x\n", exit_with,
136 exitcode, status);
137 if (mustexit)
138 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 ret = -1;
140 }
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret;
143}
144
Jeff Dike7242a402007-02-10 01:44:19 -0800145/* Changed only during early boot */
Jeff Dike53c25872008-05-12 14:01:47 -0700146int ptrace_faultinfo;
147static int disable_ptrace_faultinfo;
148
149int ptrace_ldt;
150static int disable_ptrace_ldt;
151
152int proc_mm;
153static int disable_proc_mm;
154
155int have_switch_mm;
156static int disable_switch_mm;
157
158int skas_needs_stub;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700159
160static int __init skas0_cmd_param(char *str, int* add)
161{
Jeff Dike53c25872008-05-12 14:01:47 -0700162 disable_ptrace_faultinfo = 1;
163 disable_ptrace_ldt = 1;
164 disable_proc_mm = 1;
165 disable_switch_mm = 1;
166
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700167 return 0;
168}
169
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200170/* The two __uml_setup would conflict, without this stupid alias. */
171
172static int __init mode_skas0_cmd_param(char *str, int* add)
173 __attribute__((alias("skas0_cmd_param")));
174
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700175__uml_setup("skas0", skas0_cmd_param,
Jeff Dike53c25872008-05-12 14:01:47 -0700176"skas0\n"
177" Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700178
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200179__uml_setup("mode=skas0", mode_skas0_cmd_param,
Jeff Dike53c25872008-05-12 14:01:47 -0700180"mode=skas0\n"
181" Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200182
Jeff Dike7242a402007-02-10 01:44:19 -0800183/* Changed only during early boot */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700184static int force_sysemu_disabled = 0;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static int __init nosysemu_cmd_param(char *str, int* add)
187{
188 force_sysemu_disabled = 1;
189 return 0;
190}
191
192__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700193"nosysemu\n"
194" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
195" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
196" behaviour of ptrace() and helps reducing host context switch rate.\n"
197" To make it working, you need a kernel patch for your host, too.\n"
198" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
199" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201static void __init check_sysemu(void)
202{
Jeff Dikecf6aced2007-05-23 13:57:40 -0700203 unsigned long regs[MAX_REG_NR];
Jeff Dike9eae9b12007-02-10 01:44:20 -0800204 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jeff Dike3a150e12007-02-10 01:44:28 -0800206 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 sysemu_supported = 0;
Jeff Dike3cdaf452007-10-16 01:27:09 -0700208 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Jeff Dikeba180fd2007-10-16 01:27:00 -0700210 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto fail;
212
213 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
214 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800215 fatal_perror("check_sysemu : wait failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700216 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dikef1ef9162008-06-12 15:21:41 -0700217 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
Jeff Dike3a150e12007-02-10 01:44:28 -0800218 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jeff Dikeba180fd2007-10-16 01:27:00 -0700220 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
Jeff Dikecf6aced2007-05-23 13:57:40 -0700221 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700222 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700223 non_fatal("check_sysemu got system call number %d, "
224 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
225 goto fail;
226 }
227
Al Viro966e8032011-08-18 20:12:19 +0100228 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700229 if (n < 0) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700230 non_fatal("check_sysemu : failed to modify system call "
231 "return");
232 goto fail;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jeff Dike3cdaf452007-10-16 01:27:09 -0700235 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto fail_stopped;
237
238 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800239 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 set_using_sysemu(!force_sysemu_disabled);
241
Jeff Dike3a150e12007-02-10 01:44:28 -0800242 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700243 pid = start_ptraced_child();
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700244
Jeff Dikeba180fd2007-10-16 01:27:00 -0700245 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800246 (void *) PTRACE_O_TRACESYSGOOD) < 0))
WANG Cong50629102009-03-31 15:23:41 -0700247 fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700248
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 count++;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700251 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto fail;
253 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700254 if (n < 0)
WANG Cong50629102009-03-31 15:23:41 -0700255 fatal_perror("check_sysemu: wait failed");
Jeff Dike3a150e12007-02-10 01:44:28 -0800256
Jeff Dikeba180fd2007-10-16 01:27:00 -0700257 if (WIFSTOPPED(status) &&
258 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
Jeff Dikef1ef9162008-06-12 15:21:41 -0700259 if (!count) {
WANG Cong50629102009-03-31 15:23:41 -0700260 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
Jeff Dikef1ef9162008-06-12 15:21:41 -0700261 "doesn't singlestep");
262 goto fail;
263 }
Al Viro966e8032011-08-18 20:12:19 +0100264 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700266 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800267 fatal_perror("check_sysemu : failed to modify "
268 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
270 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700271 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700272 count++;
Jeff Dikef1ef9162008-06-12 15:21:41 -0700273 else {
WANG Cong50629102009-03-31 15:23:41 -0700274 non_fatal("check_sysemu: expected SIGTRAP or "
Jeff Dikef1ef9162008-06-12 15:21:41 -0700275 "(SIGTRAP | 0x80), got status = %d\n",
276 status);
277 goto fail;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700280 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto fail_stopped;
282
283 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800284 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jeff Dikeba180fd2007-10-16 01:27:00 -0700286 if (!force_sysemu_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 set_using_sysemu(sysemu_supported);
288 return;
289
290fail:
Jeff Dike3cdaf452007-10-16 01:27:09 -0700291 stop_ptraced_child(pid, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800293 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700296static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int pid, syscall, n, status;
299
Jeff Dike3a150e12007-02-10 01:44:28 -0800300 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700301 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Jeff Dikeba180fd2007-10-16 01:27:00 -0700303 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800304 (void *) PTRACE_O_TRACESYSGOOD) < 0))
305 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Jeff Dikeba180fd2007-10-16 01:27:00 -0700307 while (1) {
308 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800309 fatal_perror("check_ptrace : ptrace failed");
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700312 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800313 fatal_perror("check_ptrace : wait failed");
314
Jeff Dikeba180fd2007-10-16 01:27:00 -0700315 if (!WIFSTOPPED(status) ||
Jeff Dike3a150e12007-02-10 01:44:28 -0800316 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
317 fatal("check_ptrace : expected (SIGTRAP|0x80), "
318 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700319
Al Viro966e8032011-08-18 20:12:19 +0100320 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700322 if (syscall == __NR_getpid) {
Al Viro966e8032011-08-18 20:12:19 +0100323 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 __NR_getppid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700325 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800326 fatal_perror("check_ptrace : failed to modify "
327 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 }
330 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700331 stop_ptraced_child(pid, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800332 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 check_sysemu();
334}
335
Rob Landley966a082f2006-04-18 22:21:43 -0700336extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700337
Jeff Dike36e45462007-05-06 14:51:11 -0700338static void __init check_coredump_limit(void)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700339{
340 struct rlimit lim;
341 int err = getrlimit(RLIMIT_CORE, &lim);
342
Jeff Dikeba180fd2007-10-16 01:27:00 -0700343 if (err) {
Jeff Dike1d94cda2007-05-06 14:51:00 -0700344 perror("Getting core dump limit");
345 return;
346 }
347
348 printf("Core dump limits :\n\tsoft - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700349 if (lim.rlim_cur == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700350 printf("NONE\n");
351 else printf("%lu\n", lim.rlim_cur);
352
353 printf("\thard - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700354 if (lim.rlim_max == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700355 printf("NONE\n");
356 else printf("%lu\n", lim.rlim_max);
357}
358
Jeff Dike36e45462007-05-06 14:51:11 -0700359void __init os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jeff Dike576c0132008-02-04 22:31:22 -0800361 int pid;
362
Jeff Dike1d94cda2007-05-06 14:51:00 -0700363 /* Print out the core dump limits early */
364 check_coredump_limit();
365
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700366 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700367
368 /* Need to check this early because mmapping happens before the
369 * kernel is running.
370 */
371 check_tmpexec();
Jeff Dike576c0132008-02-04 22:31:22 -0800372
373 pid = start_ptraced_child();
374 if (init_registers(pid))
375 fatal("Failed to initialize default registers");
376 stop_ptraced_child(pid, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700379static int __init noprocmm_cmd_param(char *str, int* add)
380{
Jeff Dike53c25872008-05-12 14:01:47 -0700381 disable_proc_mm = 1;
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700382 return 0;
383}
384
385__uml_setup("noprocmm", noprocmm_cmd_param,
386"noprocmm\n"
387" Turns off usage of /proc/mm, even if host supports it.\n"
388" To support /proc/mm, the host needs to be patched using\n"
389" the current skas3 patch.\n\n");
390
391static int __init noptracefaultinfo_cmd_param(char *str, int* add)
392{
Jeff Dike53c25872008-05-12 14:01:47 -0700393 disable_ptrace_faultinfo = 1;
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700394 return 0;
395}
396
397__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
398"noptracefaultinfo\n"
399" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
400" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
401" using the current skas3 patch.\n\n");
402
Bodo Stroesser858259c2005-11-07 00:58:55 -0800403static int __init noptraceldt_cmd_param(char *str, int* add)
404{
Jeff Dike53c25872008-05-12 14:01:47 -0700405 disable_ptrace_ldt = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800406 return 0;
407}
408
409__uml_setup("noptraceldt", noptraceldt_cmd_param,
410"noptraceldt\n"
411" Turns off usage of PTRACE_LDT, even if host supports it.\n"
412" To support PTRACE_LDT, the host needs to be patched using\n"
413" the current skas3 patch.\n\n");
414
Bodo Stroesser858259c2005-11-07 00:58:55 -0800415static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct ptrace_faultinfo fi;
Jeff Diked67b5692005-07-07 17:56:49 -0700418 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jeff Dike3a150e12007-02-10 01:44:28 -0800420 non_fatal(" - PTRACE_FAULTINFO...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700421 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
424 if (n < 0) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700425 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800426 non_fatal("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700427 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 perror("not found");
Jeff Dike53c25872008-05-12 14:01:47 -0700429 } else if (disable_ptrace_faultinfo)
430 non_fatal("found but disabled on command line\n");
Jeff Diked67b5692005-07-07 17:56:49 -0700431 else {
Jeff Dike53c25872008-05-12 14:01:47 -0700432 ptrace_faultinfo = 1;
433 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Jeff Dike3cdaf452007-10-16 01:27:09 -0700436 stop_ptraced_child(pid, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Bodo Stroesser858259c2005-11-07 00:58:55 -0800439static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800441#ifdef PTRACE_LDT
Bodo Stroesser858259c2005-11-07 00:58:55 -0800442 int pid, n;
443 unsigned char ldtbuf[40];
444 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
445 .func = 2, /* read default ldt */
446 .ptr = ldtbuf,
447 .bytecount = sizeof(ldtbuf)};
448
Jeff Dike3a150e12007-02-10 01:44:28 -0800449 non_fatal(" - PTRACE_LDT...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700450 pid = start_ptraced_child();
Bodo Stroesser858259c2005-11-07 00:58:55 -0800451
452 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
453 if (n < 0) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700454 if (errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800455 non_fatal("not found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800456 else
Jeff Dike53c25872008-05-12 14:01:47 -0700457 perror("not found");
458 } else if (disable_ptrace_ldt)
459 non_fatal("found, but use is disabled\n");
460 else {
461 ptrace_ldt = 1;
462 non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800463 }
464
Jeff Dike3cdaf452007-10-16 01:27:09 -0700465 stop_ptraced_child(pid, 1, 1);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800466#endif
467}
468
469static inline void check_skas3_proc_mm(void)
470{
Jeff Dike3a150e12007-02-10 01:44:28 -0800471 non_fatal(" - /proc/mm...");
Jeff Dike53c25872008-05-12 14:01:47 -0700472 if (access("/proc/mm", W_OK) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800473 perror("not found");
Jeff Dike53c25872008-05-12 14:01:47 -0700474 else if (disable_proc_mm)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700475 non_fatal("found but disabled on command line\n");
Jeff Dike53c25872008-05-12 14:01:47 -0700476 else {
477 proc_mm = 1;
478 non_fatal("found\n");
479 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800480}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Karol Swietlicki6b7e9672008-02-04 22:30:50 -0800482void can_do_skas(void)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800483{
Jeff Dike3a150e12007-02-10 01:44:28 -0800484 non_fatal("Checking for the skas3 patch in the host:\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800485
486 check_skas3_proc_mm();
487 check_skas3_ptrace_faultinfo();
488 check_skas3_ptrace_ldt();
489
Jeff Dikeba180fd2007-10-16 01:27:00 -0700490 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800491 skas_needs_stub = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Jeff Dike0f80bc82005-09-16 19:27:50 -0700493
Jeff Dike0f80bc82005-09-16 19:27:50 -0700494int __init parse_iomem(char *str, int *add)
495{
496 struct iomem_region *new;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800497 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700498 char *file, *driver;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800499 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700500
501 driver = str;
502 file = strchr(str,',');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700503 if (file == NULL) {
WANG Cong626c59f2008-04-28 02:13:53 -0700504 fprintf(stderr, "parse_iomem : failed to parse iomem\n");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700505 goto out;
506 }
507 *file = '\0';
508 file++;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800509 fd = open(file, O_RDWR, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700510 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700511 perror("parse_iomem - Couldn't open io file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700512 goto out;
513 }
514
Jeff Dikeba180fd2007-10-16 01:27:00 -0700515 if (fstat64(fd, &buf) < 0) {
Jeff Dike73c8f4442007-02-10 01:44:20 -0800516 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700517 goto out_close;
518 }
519
520 new = malloc(sizeof(*new));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700521 if (new == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700522 perror("Couldn't allocate iomem_region struct");
523 goto out_close;
524 }
525
Jeff Dike73c8f4442007-02-10 01:44:20 -0800526 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700527
528 *new = ((struct iomem_region) { .next = iomem_regions,
529 .driver = driver,
530 .fd = fd,
531 .size = size,
532 .phys = 0,
533 .virt = 0 });
534 iomem_regions = new;
535 iomem_size += new->size + UM_KERN_PAGE_SIZE;
536
Jeff Dike9eae9b12007-02-10 01:44:20 -0800537 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700538 out_close:
Jeff Dike73c8f4442007-02-10 01:44:20 -0800539 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700540 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800541 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700542}