start_up.c 13 KB

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