1 /*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sched.h>
13 #include <signal.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/wait.h>
18 #include <sys/time.h>
19 #include <sys/resource.h>
20 #include <asm/unistd.h>
21 #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>
28
ptrace_child(void)29 static void ptrace_child(void)
30 {
31 int ret;
32 /* Calling os_getpid because some libcs cached getpid incorrectly */
33 int pid = os_getpid(), ppid = getppid();
34 int sc_result;
35
36 if (change_sig(SIGWINCH, 0) < 0 ||
37 ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
38 perror("ptrace");
39 kill(pid, SIGKILL);
40 }
41 kill(pid, SIGSTOP);
42
43 /*
44 * This syscall will be intercepted by the parent. Don't call more than
45 * once, please.
46 */
47 sc_result = os_getpid();
48
49 if (sc_result == pid)
50 /* Nothing modified by the parent, we are running normally. */
51 ret = 1;
52 else if (sc_result == ppid)
53 /*
54 * Expected in check_ptrace and check_sysemu when they succeed
55 * in modifying the stack frame
56 */
57 ret = 0;
58 else
59 /* 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;
64
65 exit(ret);
66 }
67
fatal_perror(const char * str)68 static void fatal_perror(const char *str)
69 {
70 perror(str);
71 exit(1);
72 }
73
fatal(char * fmt,...)74 static void fatal(char *fmt, ...)
75 {
76 va_list list;
77
78 va_start(list, fmt);
79 vfprintf(stderr, fmt, list);
80 va_end(list);
81
82 exit(1);
83 }
84
non_fatal(char * fmt,...)85 static void non_fatal(char *fmt, ...)
86 {
87 va_list list;
88
89 va_start(list, fmt);
90 vfprintf(stderr, fmt, list);
91 va_end(list);
92 }
93
start_ptraced_child(void)94 static int start_ptraced_child(void)
95 {
96 int pid, n, status;
97
98 fflush(stdout);
99
100 pid = fork();
101 if (pid == 0)
102 ptrace_child();
103 else if (pid < 0)
104 fatal_perror("start_ptraced_child : fork failed");
105
106 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
107 if (n < 0)
108 fatal_perror("check_ptrace : waitpid failed");
109 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
110 fatal("check_ptrace : expected SIGSTOP, got status = %d",
111 status);
112
113 return pid;
114 }
115
116 /* When testing for SYSEMU support, if it is one of the broken versions, we
117 * must just avoid using sysemu, not panic, but only if SYSEMU features are
118 * broken.
119 * So only for SYSEMU features we test mustpanic, while normal host features
120 * must work anyway!
121 */
stop_ptraced_child(int pid,int exitcode,int mustexit)122 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
123 {
124 int status, n, ret = 0;
125
126 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
127 perror("stop_ptraced_child : ptrace failed");
128 return -1;
129 }
130 CATCH_EINTR(n = waitpid(pid, &status, 0));
131 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
132 int exit_with = WEXITSTATUS(status);
133 if (exit_with == 2)
134 non_fatal("check_ptrace : child exited with status 2. "
135 "\nDisabling SYSEMU support.\n");
136 non_fatal("check_ptrace : child exited with exitcode %d, while "
137 "expecting %d; status 0x%x\n", exit_with,
138 exitcode, status);
139 if (mustexit)
140 exit(1);
141 ret = -1;
142 }
143
144 return ret;
145 }
146
147 /* Changed only during early boot */
148 int ptrace_faultinfo;
149 static int disable_ptrace_faultinfo;
150
151 int ptrace_ldt;
152 static int disable_ptrace_ldt;
153
154 int proc_mm;
155 static int disable_proc_mm;
156
157 int have_switch_mm;
158 static int disable_switch_mm;
159
160 int skas_needs_stub;
161
skas0_cmd_param(char * str,int * add)162 static int __init skas0_cmd_param(char *str, int* add)
163 {
164 disable_ptrace_faultinfo = 1;
165 disable_ptrace_ldt = 1;
166 disable_proc_mm = 1;
167 disable_switch_mm = 1;
168
169 return 0;
170 }
171
172 /* The two __uml_setup would conflict, without this stupid alias. */
173
174 static int __init mode_skas0_cmd_param(char *str, int* add)
175 __attribute__((alias("skas0_cmd_param")));
176
177 __uml_setup("skas0", skas0_cmd_param,
178 "skas0\n"
179 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
180
181 __uml_setup("mode=skas0", mode_skas0_cmd_param,
182 "mode=skas0\n"
183 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
184
185 /* Changed only during early boot */
186 static int force_sysemu_disabled = 0;
187
nosysemu_cmd_param(char * str,int * add)188 static int __init nosysemu_cmd_param(char *str, int* add)
189 {
190 force_sysemu_disabled = 1;
191 return 0;
192 }
193
194 __uml_setup("nosysemu", nosysemu_cmd_param,
195 "nosysemu\n"
196 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
197 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
198 " behaviour of ptrace() and helps reducing host context switch rate.\n"
199 " To make it working, you need a kernel patch for your host, too.\n"
200 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
201 " information.\n\n");
202
check_sysemu(void)203 static void __init check_sysemu(void)
204 {
205 unsigned long regs[MAX_REG_NR];
206 int pid, n, status, count=0;
207
208 non_fatal("Checking syscall emulation patch for ptrace...");
209 sysemu_supported = 0;
210 pid = start_ptraced_child();
211
212 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
213 goto fail;
214
215 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
216 if (n < 0)
217 fatal_perror("check_sysemu : wait failed");
218 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
219 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
220 status);
221
222 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
223 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
224 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
225 non_fatal("check_sysemu got system call number %d, "
226 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
227 goto fail;
228 }
229
230 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
231 if (n < 0) {
232 non_fatal("check_sysemu : failed to modify system call "
233 "return");
234 goto fail;
235 }
236
237 if (stop_ptraced_child(pid, 0, 0) < 0)
238 goto fail_stopped;
239
240 sysemu_supported = 1;
241 non_fatal("OK\n");
242 set_using_sysemu(!force_sysemu_disabled);
243
244 non_fatal("Checking advanced syscall emulation patch for ptrace...");
245 pid = start_ptraced_child();
246
247 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
248 (void *) PTRACE_O_TRACESYSGOOD) < 0))
249 fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
250
251 while (1) {
252 count++;
253 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
254 goto fail;
255 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
256 if (n < 0)
257 fatal_perror("check_sysemu: wait failed");
258
259 if (WIFSTOPPED(status) &&
260 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
261 if (!count) {
262 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
263 "doesn't singlestep");
264 goto fail;
265 }
266 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
267 os_getpid());
268 if (n < 0)
269 fatal_perror("check_sysemu : failed to modify "
270 "system call return");
271 break;
272 }
273 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
274 count++;
275 else {
276 non_fatal("check_sysemu: expected SIGTRAP or "
277 "(SIGTRAP | 0x80), got status = %d\n",
278 status);
279 goto fail;
280 }
281 }
282 if (stop_ptraced_child(pid, 0, 0) < 0)
283 goto fail_stopped;
284
285 sysemu_supported = 2;
286 non_fatal("OK\n");
287
288 if (!force_sysemu_disabled)
289 set_using_sysemu(sysemu_supported);
290 return;
291
292 fail:
293 stop_ptraced_child(pid, 1, 0);
294 fail_stopped:
295 non_fatal("missing\n");
296 }
297
check_ptrace(void)298 static void __init check_ptrace(void)
299 {
300 int pid, syscall, n, status;
301
302 non_fatal("Checking that ptrace can change system call numbers...");
303 pid = start_ptraced_child();
304
305 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
306 (void *) PTRACE_O_TRACESYSGOOD) < 0))
307 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
308
309 while (1) {
310 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
311 fatal_perror("check_ptrace : ptrace failed");
312
313 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
314 if (n < 0)
315 fatal_perror("check_ptrace : wait failed");
316
317 if (!WIFSTOPPED(status) ||
318 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
319 fatal("check_ptrace : expected (SIGTRAP|0x80), "
320 "got status = %d", status);
321
322 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
323 0);
324 if (syscall == __NR_getpid) {
325 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
326 __NR_getppid);
327 if (n < 0)
328 fatal_perror("check_ptrace : failed to modify "
329 "system call");
330 break;
331 }
332 }
333 stop_ptraced_child(pid, 0, 1);
334 non_fatal("OK\n");
335 check_sysemu();
336 }
337
338 extern void check_tmpexec(void);
339
check_coredump_limit(void)340 static void __init check_coredump_limit(void)
341 {
342 struct rlimit lim;
343 int err = getrlimit(RLIMIT_CORE, &lim);
344
345 if (err) {
346 perror("Getting core dump limit");
347 return;
348 }
349
350 printf("Core dump limits :\n\tsoft - ");
351 if (lim.rlim_cur == RLIM_INFINITY)
352 printf("NONE\n");
353 else printf("%lu\n", lim.rlim_cur);
354
355 printf("\thard - ");
356 if (lim.rlim_max == RLIM_INFINITY)
357 printf("NONE\n");
358 else printf("%lu\n", lim.rlim_max);
359 }
360
os_early_checks(void)361 void __init os_early_checks(void)
362 {
363 int pid;
364
365 /* Print out the core dump limits early */
366 check_coredump_limit();
367
368 check_ptrace();
369
370 /* Need to check this early because mmapping happens before the
371 * kernel is running.
372 */
373 check_tmpexec();
374
375 pid = start_ptraced_child();
376 if (init_registers(pid))
377 fatal("Failed to initialize default registers");
378 stop_ptraced_child(pid, 1, 1);
379 }
380
noprocmm_cmd_param(char * str,int * add)381 static int __init noprocmm_cmd_param(char *str, int* add)
382 {
383 disable_proc_mm = 1;
384 return 0;
385 }
386
387 __uml_setup("noprocmm", noprocmm_cmd_param,
388 "noprocmm\n"
389 " Turns off usage of /proc/mm, even if host supports it.\n"
390 " To support /proc/mm, the host needs to be patched using\n"
391 " the current skas3 patch.\n\n");
392
noptracefaultinfo_cmd_param(char * str,int * add)393 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
394 {
395 disable_ptrace_faultinfo = 1;
396 return 0;
397 }
398
399 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
400 "noptracefaultinfo\n"
401 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
402 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
403 " using the current skas3 patch.\n\n");
404
noptraceldt_cmd_param(char * str,int * add)405 static int __init noptraceldt_cmd_param(char *str, int* add)
406 {
407 disable_ptrace_ldt = 1;
408 return 0;
409 }
410
411 __uml_setup("noptraceldt", noptraceldt_cmd_param,
412 "noptraceldt\n"
413 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
414 " To support PTRACE_LDT, the host needs to be patched using\n"
415 " the current skas3 patch.\n\n");
416
check_skas3_ptrace_faultinfo(void)417 static inline void check_skas3_ptrace_faultinfo(void)
418 {
419 struct ptrace_faultinfo fi;
420 int pid, n;
421
422 non_fatal(" - PTRACE_FAULTINFO...");
423 pid = start_ptraced_child();
424
425 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
426 if (n < 0) {
427 if (errno == EIO)
428 non_fatal("not found\n");
429 else
430 perror("not found");
431 } else if (disable_ptrace_faultinfo)
432 non_fatal("found but disabled on command line\n");
433 else {
434 ptrace_faultinfo = 1;
435 non_fatal("found\n");
436 }
437
438 stop_ptraced_child(pid, 1, 1);
439 }
440
check_skas3_ptrace_ldt(void)441 static inline void check_skas3_ptrace_ldt(void)
442 {
443 #ifdef PTRACE_LDT
444 int pid, n;
445 unsigned char ldtbuf[40];
446 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
447 .func = 2, /* read default ldt */
448 .ptr = ldtbuf,
449 .bytecount = sizeof(ldtbuf)};
450
451 non_fatal(" - PTRACE_LDT...");
452 pid = start_ptraced_child();
453
454 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
455 if (n < 0) {
456 if (errno == EIO)
457 non_fatal("not found\n");
458 else
459 perror("not found");
460 } else if (disable_ptrace_ldt)
461 non_fatal("found, but use is disabled\n");
462 else {
463 ptrace_ldt = 1;
464 non_fatal("found\n");
465 }
466
467 stop_ptraced_child(pid, 1, 1);
468 #endif
469 }
470
check_skas3_proc_mm(void)471 static inline void check_skas3_proc_mm(void)
472 {
473 non_fatal(" - /proc/mm...");
474 if (access("/proc/mm", W_OK) < 0)
475 perror("not found");
476 else if (disable_proc_mm)
477 non_fatal("found but disabled on command line\n");
478 else {
479 proc_mm = 1;
480 non_fatal("found\n");
481 }
482 }
483
can_do_skas(void)484 void can_do_skas(void)
485 {
486 non_fatal("Checking for the skas3 patch in the host:\n");
487
488 check_skas3_proc_mm();
489 check_skas3_ptrace_faultinfo();
490 check_skas3_ptrace_ldt();
491
492 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
493 skas_needs_stub = 1;
494 }
495
parse_iomem(char * str,int * add)496 int __init parse_iomem(char *str, int *add)
497 {
498 struct iomem_region *new;
499 struct stat64 buf;
500 char *file, *driver;
501 int fd, size;
502
503 driver = str;
504 file = strchr(str,',');
505 if (file == NULL) {
506 fprintf(stderr, "parse_iomem : failed to parse iomem\n");
507 goto out;
508 }
509 *file = '\0';
510 file++;
511 fd = open(file, O_RDWR, 0);
512 if (fd < 0) {
513 perror("parse_iomem - Couldn't open io file");
514 goto out;
515 }
516
517 if (fstat64(fd, &buf) < 0) {
518 perror("parse_iomem - cannot stat_fd file");
519 goto out_close;
520 }
521
522 new = malloc(sizeof(*new));
523 if (new == NULL) {
524 perror("Couldn't allocate iomem_region struct");
525 goto out_close;
526 }
527
528 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
529
530 *new = ((struct iomem_region) { .next = iomem_regions,
531 .driver = driver,
532 .fd = fd,
533 .size = size,
534 .phys = 0,
535 .virt = 0 });
536 iomem_regions = new;
537 iomem_size += new->size + UM_KERN_PAGE_SIZE;
538
539 return 0;
540 out_close:
541 close(fd);
542 out:
543 return 1;
544 }
545