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