start_up.c 12 KB

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