start_up.c 13 KB

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