start_up.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <pty.h>
  6. #include <stdio.h>
  7. #include <stddef.h>
  8. #include <stdarg.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <sched.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <sys/time.h>
  17. #include <sys/wait.h>
  18. #include <sys/mman.h>
  19. #include <sys/resource.h>
  20. #include <asm/unistd.h>
  21. #include <asm/page.h>
  22. #include <sys/types.h>
  23. #include "kern_util.h"
  24. #include "user.h"
  25. #include "signal_kern.h"
  26. #include "sysdep/ptrace.h"
  27. #include "sysdep/sigcontext.h"
  28. #include "irq_user.h"
  29. #include "ptrace_user.h"
  30. #include "mem_user.h"
  31. #include "init.h"
  32. #include "os.h"
  33. #include "uml-config.h"
  34. #include "choose-mode.h"
  35. #include "mode.h"
  36. #include "tempfile.h"
  37. #include "kern_constants.h"
  38. #ifdef UML_CONFIG_MODE_SKAS
  39. #include "skas.h"
  40. #include "skas_ptrace.h"
  41. #include "registers.h"
  42. #endif
  43. static int ptrace_child(void *arg)
  44. {
  45. int ret;
  46. int pid = os_getpid(), ppid = getppid();
  47. int sc_result;
  48. change_sig(SIGWINCH, 0);
  49. if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
  50. perror("ptrace");
  51. os_kill_process(pid, 0);
  52. }
  53. kill(pid, SIGSTOP);
  54. /*This syscall will be intercepted by the parent. Don't call more than
  55. * once, please.*/
  56. sc_result = os_getpid();
  57. if (sc_result == pid)
  58. ret = 1; /*Nothing modified by the parent, we are running
  59. normally.*/
  60. else if (sc_result == ppid)
  61. ret = 0; /*Expected in check_ptrace and check_sysemu when they
  62. succeed in modifying the stack frame*/
  63. else
  64. ret = 2; /*Serious trouble! This could be caused by a bug in
  65. host 2.6 SKAS3/2.6 patch before release -V6, together
  66. with a bug in the UML code itself.*/
  67. _exit(ret);
  68. }
  69. static void fatal_perror(char *str)
  70. {
  71. perror(str);
  72. exit(1);
  73. }
  74. static void fatal(char *fmt, ...)
  75. {
  76. va_list list;
  77. va_start(list, fmt);
  78. vprintf(fmt, list);
  79. va_end(list);
  80. fflush(stdout);
  81. exit(1);
  82. }
  83. static void non_fatal(char *fmt, ...)
  84. {
  85. va_list list;
  86. va_start(list, fmt);
  87. vprintf(fmt, list);
  88. va_end(list);
  89. fflush(stdout);
  90. }
  91. static int start_ptraced_child(void **stack_out)
  92. {
  93. void *stack;
  94. unsigned long sp;
  95. int pid, n, status;
  96. stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
  97. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  98. if(stack == MAP_FAILED)
  99. fatal_perror("check_ptrace : mmap failed");
  100. sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
  101. pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
  102. if(pid < 0)
  103. fatal_perror("start_ptraced_child : clone failed");
  104. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  105. if(n < 0)
  106. fatal_perror("check_ptrace : clone failed");
  107. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  108. fatal("check_ptrace : expected SIGSTOP, got status = %d",
  109. status);
  110. *stack_out = stack;
  111. return pid;
  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, void *stack, int exitcode,
  120. int mustexit)
  121. {
  122. int status, n, ret = 0;
  123. if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
  124. fatal_perror("stop_ptraced_child : ptrace failed");
  125. CATCH_EINTR(n = waitpid(pid, &status, 0));
  126. if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
  127. int exit_with = WEXITSTATUS(status);
  128. if (exit_with == 2)
  129. non_fatal("check_ptrace : child exited with status 2. "
  130. "\nDisabling SYSEMU support.\n");
  131. 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);
  136. ret = -1;
  137. }
  138. if(munmap(stack, PAGE_SIZE) < 0)
  139. fatal_perror("check_ptrace : munmap failed");
  140. return ret;
  141. }
  142. /* Changed only during early boot */
  143. int ptrace_faultinfo = 1;
  144. int ptrace_ldt = 1;
  145. int proc_mm = 1;
  146. int skas_needs_stub = 0;
  147. static int __init skas0_cmd_param(char *str, int* add)
  148. {
  149. ptrace_faultinfo = proc_mm = 0;
  150. return 0;
  151. }
  152. /* The two __uml_setup would conflict, without this stupid alias. */
  153. static int __init mode_skas0_cmd_param(char *str, int* add)
  154. __attribute__((alias("skas0_cmd_param")));
  155. __uml_setup("skas0", skas0_cmd_param,
  156. "skas0\n"
  157. " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
  158. " you specify mode=tt.\n\n");
  159. __uml_setup("mode=skas0", mode_skas0_cmd_param,
  160. "mode=skas0\n"
  161. " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
  162. " specify mode=tt. Note that this was recently added - on \n"
  163. " older kernels you must use simply \"skas0\".\n\n");
  164. /* Changed only during early boot */
  165. static int force_sysemu_disabled = 0;
  166. static int __init nosysemu_cmd_param(char *str, int* add)
  167. {
  168. force_sysemu_disabled = 1;
  169. return 0;
  170. }
  171. __uml_setup("nosysemu", nosysemu_cmd_param,
  172. "nosysemu\n"
  173. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  174. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  175. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  176. " To make it working, you need a kernel patch for your host, too.\n"
  177. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  178. " information.\n\n");
  179. static void __init check_sysemu(void)
  180. {
  181. void *stack;
  182. unsigned long regs[MAX_REG_NR];
  183. int pid, n, status, count=0;
  184. non_fatal("Checking syscall emulation patch for ptrace...");
  185. sysemu_supported = 0;
  186. pid = start_ptraced_child(&stack);
  187. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  188. goto fail;
  189. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  190. if (n < 0)
  191. fatal_perror("check_sysemu : wait failed");
  192. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  193. fatal("check_sysemu : expected SIGTRAP, got status = %d",
  194. status);
  195. if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  196. fatal_perror("check_sysemu : PTRACE_GETREGS failed");
  197. if(PT_SYSCALL_NR(regs) != __NR_getpid){
  198. non_fatal("check_sysemu got system call number %d, "
  199. "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
  200. goto fail;
  201. }
  202. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
  203. if(n < 0){
  204. non_fatal("check_sysemu : failed to modify system call "
  205. "return");
  206. goto fail;
  207. }
  208. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  209. goto fail_stopped;
  210. sysemu_supported = 1;
  211. non_fatal("OK\n");
  212. set_using_sysemu(!force_sysemu_disabled);
  213. non_fatal("Checking advanced syscall emulation patch for ptrace...");
  214. pid = start_ptraced_child(&stack);
  215. if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  216. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  217. fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
  218. while(1){
  219. count++;
  220. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  221. goto fail;
  222. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  223. if(n < 0)
  224. fatal_perror("check_ptrace : wait failed");
  225. if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
  226. if (!count)
  227. fatal("check_ptrace : SYSEMU_SINGLESTEP "
  228. "doesn't singlestep");
  229. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  230. os_getpid());
  231. if(n < 0)
  232. fatal_perror("check_sysemu : failed to modify "
  233. "system call return");
  234. break;
  235. }
  236. else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  237. count++;
  238. else
  239. fatal("check_ptrace : expected SIGTRAP or "
  240. "(SIGTRAP | 0x80), got status = %d", status);
  241. }
  242. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  243. goto fail_stopped;
  244. sysemu_supported = 2;
  245. non_fatal("OK\n");
  246. if ( !force_sysemu_disabled )
  247. set_using_sysemu(sysemu_supported);
  248. return;
  249. fail:
  250. stop_ptraced_child(pid, stack, 1, 0);
  251. fail_stopped:
  252. non_fatal("missing\n");
  253. }
  254. static void __init check_ptrace(void)
  255. {
  256. void *stack;
  257. int pid, syscall, n, status;
  258. non_fatal("Checking that ptrace can change system call numbers...");
  259. pid = start_ptraced_child(&stack);
  260. if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  261. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  262. fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
  263. while(1){
  264. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  265. fatal_perror("check_ptrace : ptrace failed");
  266. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  267. if(n < 0)
  268. fatal_perror("check_ptrace : wait failed");
  269. if(!WIFSTOPPED(status) ||
  270. (WSTOPSIG(status) != (SIGTRAP | 0x80)))
  271. fatal("check_ptrace : expected (SIGTRAP|0x80), "
  272. "got status = %d", status);
  273. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  274. 0);
  275. if(syscall == __NR_getpid){
  276. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  277. __NR_getppid);
  278. if(n < 0)
  279. fatal_perror("check_ptrace : failed to modify "
  280. "system call");
  281. break;
  282. }
  283. }
  284. stop_ptraced_child(pid, stack, 0, 1);
  285. non_fatal("OK\n");
  286. check_sysemu();
  287. }
  288. extern void check_tmpexec(void);
  289. static void __init check_coredump_limit(void)
  290. {
  291. struct rlimit lim;
  292. int err = getrlimit(RLIMIT_CORE, &lim);
  293. if(err){
  294. perror("Getting core dump limit");
  295. return;
  296. }
  297. printf("Core dump limits :\n\tsoft - ");
  298. if(lim.rlim_cur == RLIM_INFINITY)
  299. printf("NONE\n");
  300. else printf("%lu\n", lim.rlim_cur);
  301. printf("\thard - ");
  302. if(lim.rlim_max == RLIM_INFINITY)
  303. printf("NONE\n");
  304. else printf("%lu\n", lim.rlim_max);
  305. }
  306. void __init os_early_checks(void)
  307. {
  308. /* Print out the core dump limits early */
  309. check_coredump_limit();
  310. check_ptrace();
  311. /* Need to check this early because mmapping happens before the
  312. * kernel is running.
  313. */
  314. check_tmpexec();
  315. }
  316. static int __init noprocmm_cmd_param(char *str, int* add)
  317. {
  318. proc_mm = 0;
  319. return 0;
  320. }
  321. __uml_setup("noprocmm", noprocmm_cmd_param,
  322. "noprocmm\n"
  323. " Turns off usage of /proc/mm, even if host supports it.\n"
  324. " To support /proc/mm, the host needs to be patched using\n"
  325. " the current skas3 patch.\n\n");
  326. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  327. {
  328. ptrace_faultinfo = 0;
  329. return 0;
  330. }
  331. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  332. "noptracefaultinfo\n"
  333. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  334. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  335. " using the current skas3 patch.\n\n");
  336. static int __init noptraceldt_cmd_param(char *str, int* add)
  337. {
  338. ptrace_ldt = 0;
  339. return 0;
  340. }
  341. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  342. "noptraceldt\n"
  343. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  344. " To support PTRACE_LDT, the host needs to be patched using\n"
  345. " the current skas3 patch.\n\n");
  346. #ifdef UML_CONFIG_MODE_SKAS
  347. static inline void check_skas3_ptrace_faultinfo(void)
  348. {
  349. struct ptrace_faultinfo fi;
  350. void *stack;
  351. int pid, n;
  352. non_fatal(" - PTRACE_FAULTINFO...");
  353. pid = start_ptraced_child(&stack);
  354. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  355. if (n < 0) {
  356. ptrace_faultinfo = 0;
  357. if(errno == EIO)
  358. non_fatal("not found\n");
  359. else
  360. perror("not found");
  361. }
  362. else {
  363. if (!ptrace_faultinfo)
  364. non_fatal("found but disabled on command line\n");
  365. else
  366. non_fatal("found\n");
  367. }
  368. init_registers(pid);
  369. stop_ptraced_child(pid, stack, 1, 1);
  370. }
  371. static inline void check_skas3_ptrace_ldt(void)
  372. {
  373. #ifdef PTRACE_LDT
  374. void *stack;
  375. int pid, n;
  376. unsigned char ldtbuf[40];
  377. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  378. .func = 2, /* read default ldt */
  379. .ptr = ldtbuf,
  380. .bytecount = sizeof(ldtbuf)};
  381. non_fatal(" - PTRACE_LDT...");
  382. pid = start_ptraced_child(&stack);
  383. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  384. if (n < 0) {
  385. if(errno == EIO)
  386. non_fatal("not found\n");
  387. else {
  388. perror("not found");
  389. }
  390. ptrace_ldt = 0;
  391. }
  392. else {
  393. if(ptrace_ldt)
  394. non_fatal("found\n");
  395. else
  396. non_fatal("found, but use is disabled\n");
  397. }
  398. stop_ptraced_child(pid, stack, 1, 1);
  399. #else
  400. /* PTRACE_LDT might be disabled via cmdline option.
  401. * We want to override this, else we might use the stub
  402. * without real need
  403. */
  404. ptrace_ldt = 1;
  405. #endif
  406. }
  407. static inline void check_skas3_proc_mm(void)
  408. {
  409. non_fatal(" - /proc/mm...");
  410. if (access("/proc/mm", W_OK) < 0) {
  411. proc_mm = 0;
  412. perror("not found");
  413. }
  414. else {
  415. if (!proc_mm)
  416. non_fatal("found but disabled on command line\n");
  417. else
  418. non_fatal("found\n");
  419. }
  420. }
  421. int can_do_skas(void)
  422. {
  423. non_fatal("Checking for the skas3 patch in the host:\n");
  424. check_skas3_proc_mm();
  425. check_skas3_ptrace_faultinfo();
  426. check_skas3_ptrace_ldt();
  427. if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  428. skas_needs_stub = 1;
  429. return 1;
  430. }
  431. #else
  432. int can_do_skas(void)
  433. {
  434. return 0;
  435. }
  436. #endif
  437. int __init parse_iomem(char *str, int *add)
  438. {
  439. struct iomem_region *new;
  440. struct stat64 buf;
  441. char *file, *driver;
  442. int fd, size;
  443. driver = str;
  444. file = strchr(str,',');
  445. if(file == NULL){
  446. printf("parse_iomem : failed to parse iomem\n");
  447. goto out;
  448. }
  449. *file = '\0';
  450. file++;
  451. fd = open(file, O_RDWR, 0);
  452. if(fd < 0){
  453. os_print_error(fd, "parse_iomem - Couldn't open io file");
  454. goto out;
  455. }
  456. if(fstat64(fd, &buf) < 0){
  457. perror("parse_iomem - cannot stat_fd file");
  458. goto out_close;
  459. }
  460. new = malloc(sizeof(*new));
  461. if(new == NULL){
  462. perror("Couldn't allocate iomem_region struct");
  463. goto out_close;
  464. }
  465. size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  466. *new = ((struct iomem_region) { .next = iomem_regions,
  467. .driver = driver,
  468. .fd = fd,
  469. .size = size,
  470. .phys = 0,
  471. .virt = 0 });
  472. iomem_regions = new;
  473. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  474. return 0;
  475. out_close:
  476. close(fd);
  477. out:
  478. return 1;
  479. }