start_up.c 13 KB

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