start_up.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. "Serious trouble happening! Try updating "
  131. "your host skas patch!\nDisabling SYSEMU "
  132. "support.");
  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. if(munmap(stack, PAGE_SIZE) < 0)
  141. fatal_perror("check_ptrace : munmap failed");
  142. return ret;
  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. static int __init skas0_cmd_param(char *str, int* add)
  150. {
  151. ptrace_faultinfo = proc_mm = 0;
  152. return 0;
  153. }
  154. /* The two __uml_setup would conflict, without this stupid alias. */
  155. static int __init mode_skas0_cmd_param(char *str, int* add)
  156. __attribute__((alias("skas0_cmd_param")));
  157. __uml_setup("skas0", skas0_cmd_param,
  158. "skas0\n"
  159. " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
  160. " you specify mode=tt.\n\n");
  161. __uml_setup("mode=skas0", mode_skas0_cmd_param,
  162. "mode=skas0\n"
  163. " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
  164. " specify mode=tt. Note that this was recently added - on \n"
  165. " older kernels you must use simply \"skas0\".\n\n");
  166. /* Changed only during early boot */
  167. static int force_sysemu_disabled = 0;
  168. static int __init nosysemu_cmd_param(char *str, int* add)
  169. {
  170. force_sysemu_disabled = 1;
  171. return 0;
  172. }
  173. __uml_setup("nosysemu", nosysemu_cmd_param,
  174. "nosysemu\n"
  175. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  176. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  177. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  178. " To make it working, you need a kernel patch for your host, too.\n"
  179. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  180. " information.\n\n");
  181. static void __init check_sysemu(void)
  182. {
  183. void *stack;
  184. int pid, n, status, count=0;
  185. non_fatal("Checking syscall emulation patch for ptrace...");
  186. sysemu_supported = 0;
  187. pid = start_ptraced_child(&stack);
  188. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  189. goto fail;
  190. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  191. if (n < 0)
  192. fatal_perror("check_sysemu : wait failed");
  193. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  194. fatal("check_sysemu : expected SIGTRAP, got status = %d",
  195. status);
  196. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  197. os_getpid());
  198. if(n < 0)
  199. fatal_perror("check_sysemu : failed to modify system call "
  200. "return");
  201. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  202. goto fail_stopped;
  203. sysemu_supported = 1;
  204. non_fatal("OK\n");
  205. set_using_sysemu(!force_sysemu_disabled);
  206. non_fatal("Checking advanced syscall emulation patch for ptrace...");
  207. pid = start_ptraced_child(&stack);
  208. if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  209. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  210. fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
  211. while(1){
  212. count++;
  213. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  214. goto fail;
  215. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  216. if(n < 0)
  217. fatal_perror("check_ptrace : wait failed");
  218. if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
  219. if (!count)
  220. fatal("check_ptrace : SYSEMU_SINGLESTEP "
  221. "doesn't singlestep");
  222. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  223. os_getpid());
  224. if(n < 0)
  225. fatal_perror("check_sysemu : failed to modify "
  226. "system call return");
  227. break;
  228. }
  229. else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  230. count++;
  231. else
  232. fatal("check_ptrace : expected SIGTRAP or "
  233. "(SIGTRAP | 0x80), got status = %d", status);
  234. }
  235. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  236. goto fail_stopped;
  237. sysemu_supported = 2;
  238. non_fatal("OK\n");
  239. if ( !force_sysemu_disabled )
  240. set_using_sysemu(sysemu_supported);
  241. return;
  242. fail:
  243. stop_ptraced_child(pid, stack, 1, 0);
  244. fail_stopped:
  245. non_fatal("missing\n");
  246. }
  247. static void __init check_ptrace(void)
  248. {
  249. void *stack;
  250. int pid, syscall, n, status;
  251. non_fatal("Checking that ptrace can change system call numbers...");
  252. pid = start_ptraced_child(&stack);
  253. if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  254. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  255. fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
  256. while(1){
  257. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  258. fatal_perror("check_ptrace : ptrace failed");
  259. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  260. if(n < 0)
  261. fatal_perror("check_ptrace : wait failed");
  262. if(!WIFSTOPPED(status) ||
  263. (WSTOPSIG(status) != (SIGTRAP | 0x80)))
  264. fatal("check_ptrace : expected (SIGTRAP|0x80), "
  265. "got status = %d", status);
  266. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  267. 0);
  268. if(syscall == __NR_getpid){
  269. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  270. __NR_getppid);
  271. if(n < 0)
  272. fatal_perror("check_ptrace : failed to modify "
  273. "system call");
  274. break;
  275. }
  276. }
  277. stop_ptraced_child(pid, stack, 0, 1);
  278. non_fatal("OK\n");
  279. check_sysemu();
  280. }
  281. extern void check_tmpexec(void);
  282. static void __init check_coredump_limit(void)
  283. {
  284. struct rlimit lim;
  285. int err = getrlimit(RLIMIT_CORE, &lim);
  286. if(err){
  287. perror("Getting core dump limit");
  288. return;
  289. }
  290. printf("Core dump limits :\n\tsoft - ");
  291. if(lim.rlim_cur == RLIM_INFINITY)
  292. printf("NONE\n");
  293. else printf("%lu\n", lim.rlim_cur);
  294. printf("\thard - ");
  295. if(lim.rlim_max == RLIM_INFINITY)
  296. printf("NONE\n");
  297. else printf("%lu\n", lim.rlim_max);
  298. }
  299. void __init os_early_checks(void)
  300. {
  301. /* Print out the core dump limits early */
  302. check_coredump_limit();
  303. check_ptrace();
  304. /* Need to check this early because mmapping happens before the
  305. * kernel is running.
  306. */
  307. check_tmpexec();
  308. }
  309. static int __init noprocmm_cmd_param(char *str, int* add)
  310. {
  311. proc_mm = 0;
  312. return 0;
  313. }
  314. __uml_setup("noprocmm", noprocmm_cmd_param,
  315. "noprocmm\n"
  316. " Turns off usage of /proc/mm, even if host supports it.\n"
  317. " To support /proc/mm, the host needs to be patched using\n"
  318. " the current skas3 patch.\n\n");
  319. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  320. {
  321. ptrace_faultinfo = 0;
  322. return 0;
  323. }
  324. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  325. "noptracefaultinfo\n"
  326. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  327. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  328. " using the current skas3 patch.\n\n");
  329. static int __init noptraceldt_cmd_param(char *str, int* add)
  330. {
  331. ptrace_ldt = 0;
  332. return 0;
  333. }
  334. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  335. "noptraceldt\n"
  336. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  337. " To support PTRACE_LDT, the host needs to be patched using\n"
  338. " the current skas3 patch.\n\n");
  339. #ifdef UML_CONFIG_MODE_SKAS
  340. static inline void check_skas3_ptrace_faultinfo(void)
  341. {
  342. struct ptrace_faultinfo fi;
  343. void *stack;
  344. int pid, n;
  345. non_fatal(" - PTRACE_FAULTINFO...");
  346. pid = start_ptraced_child(&stack);
  347. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  348. if (n < 0) {
  349. ptrace_faultinfo = 0;
  350. if(errno == EIO)
  351. non_fatal("not found\n");
  352. else
  353. perror("not found");
  354. }
  355. else {
  356. if (!ptrace_faultinfo)
  357. non_fatal("found but disabled on command line\n");
  358. else
  359. non_fatal("found\n");
  360. }
  361. init_registers(pid);
  362. stop_ptraced_child(pid, stack, 1, 1);
  363. }
  364. static inline void check_skas3_ptrace_ldt(void)
  365. {
  366. #ifdef PTRACE_LDT
  367. void *stack;
  368. int pid, n;
  369. unsigned char ldtbuf[40];
  370. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  371. .func = 2, /* read default ldt */
  372. .ptr = ldtbuf,
  373. .bytecount = sizeof(ldtbuf)};
  374. non_fatal(" - PTRACE_LDT...");
  375. pid = start_ptraced_child(&stack);
  376. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  377. if (n < 0) {
  378. if(errno == EIO)
  379. non_fatal("not found\n");
  380. else {
  381. perror("not found");
  382. }
  383. ptrace_ldt = 0;
  384. }
  385. else {
  386. if(ptrace_ldt)
  387. non_fatal("found\n");
  388. else
  389. non_fatal("found, but use is disabled\n");
  390. }
  391. stop_ptraced_child(pid, stack, 1, 1);
  392. #else
  393. /* PTRACE_LDT might be disabled via cmdline option.
  394. * We want to override this, else we might use the stub
  395. * without real need
  396. */
  397. ptrace_ldt = 1;
  398. #endif
  399. }
  400. static inline void check_skas3_proc_mm(void)
  401. {
  402. non_fatal(" - /proc/mm...");
  403. if (access("/proc/mm", W_OK) < 0) {
  404. proc_mm = 0;
  405. perror("not found");
  406. }
  407. else {
  408. if (!proc_mm)
  409. non_fatal("found but disabled on command line\n");
  410. else
  411. non_fatal("found\n");
  412. }
  413. }
  414. int can_do_skas(void)
  415. {
  416. non_fatal("Checking for the skas3 patch in the host:\n");
  417. check_skas3_proc_mm();
  418. check_skas3_ptrace_faultinfo();
  419. check_skas3_ptrace_ldt();
  420. if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  421. skas_needs_stub = 1;
  422. return 1;
  423. }
  424. #else
  425. int can_do_skas(void)
  426. {
  427. return 0;
  428. }
  429. #endif
  430. int __init parse_iomem(char *str, int *add)
  431. {
  432. struct iomem_region *new;
  433. struct stat64 buf;
  434. char *file, *driver;
  435. int fd, size;
  436. driver = str;
  437. file = strchr(str,',');
  438. if(file == NULL){
  439. printf("parse_iomem : failed to parse iomem\n");
  440. goto out;
  441. }
  442. *file = '\0';
  443. file++;
  444. fd = open(file, O_RDWR, 0);
  445. if(fd < 0){
  446. os_print_error(fd, "parse_iomem - Couldn't open io file");
  447. goto out;
  448. }
  449. if(fstat64(fd, &buf) < 0){
  450. perror("parse_iomem - cannot stat_fd file");
  451. goto out_close;
  452. }
  453. new = malloc(sizeof(*new));
  454. if(new == NULL){
  455. perror("Couldn't allocate iomem_region struct");
  456. goto out_close;
  457. }
  458. size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  459. *new = ((struct iomem_region) { .next = iomem_regions,
  460. .driver = driver,
  461. .fd = fd,
  462. .size = size,
  463. .phys = 0,
  464. .virt = 0 });
  465. iomem_regions = new;
  466. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  467. return 0;
  468. out_close:
  469. close(fd);
  470. out:
  471. return 1;
  472. }