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