start_up.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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(const 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. int pid;
  290. /* Print out the core dump limits early */
  291. check_coredump_limit();
  292. check_ptrace();
  293. /* Need to check this early because mmapping happens before the
  294. * kernel is running.
  295. */
  296. check_tmpexec();
  297. pid = start_ptraced_child();
  298. if (init_registers(pid))
  299. fatal("Failed to initialize default registers");
  300. stop_ptraced_child(pid, 1, 1);
  301. }
  302. static int __init noprocmm_cmd_param(char *str, int* add)
  303. {
  304. proc_mm = 0;
  305. return 0;
  306. }
  307. __uml_setup("noprocmm", noprocmm_cmd_param,
  308. "noprocmm\n"
  309. " Turns off usage of /proc/mm, even if host supports it.\n"
  310. " To support /proc/mm, the host needs to be patched using\n"
  311. " the current skas3 patch.\n\n");
  312. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  313. {
  314. ptrace_faultinfo = 0;
  315. return 0;
  316. }
  317. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  318. "noptracefaultinfo\n"
  319. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  320. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  321. " using the current skas3 patch.\n\n");
  322. static int __init noptraceldt_cmd_param(char *str, int* add)
  323. {
  324. ptrace_ldt = 0;
  325. return 0;
  326. }
  327. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  328. "noptraceldt\n"
  329. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  330. " To support PTRACE_LDT, the host needs to be patched using\n"
  331. " the current skas3 patch.\n\n");
  332. static inline void check_skas3_ptrace_faultinfo(void)
  333. {
  334. struct ptrace_faultinfo fi;
  335. int pid, n;
  336. non_fatal(" - PTRACE_FAULTINFO...");
  337. pid = start_ptraced_child();
  338. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  339. if (n < 0) {
  340. ptrace_faultinfo = 0;
  341. if (errno == EIO)
  342. non_fatal("not found\n");
  343. else
  344. perror("not found");
  345. }
  346. else {
  347. if (!ptrace_faultinfo)
  348. non_fatal("found but disabled on command line\n");
  349. else
  350. non_fatal("found\n");
  351. }
  352. stop_ptraced_child(pid, 1, 1);
  353. }
  354. static inline void check_skas3_ptrace_ldt(void)
  355. {
  356. #ifdef PTRACE_LDT
  357. int pid, n;
  358. unsigned char ldtbuf[40];
  359. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  360. .func = 2, /* read default ldt */
  361. .ptr = ldtbuf,
  362. .bytecount = sizeof(ldtbuf)};
  363. non_fatal(" - PTRACE_LDT...");
  364. pid = start_ptraced_child();
  365. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  366. if (n < 0) {
  367. if (errno == EIO)
  368. non_fatal("not found\n");
  369. else {
  370. perror("not found");
  371. }
  372. ptrace_ldt = 0;
  373. }
  374. else {
  375. if (ptrace_ldt)
  376. non_fatal("found\n");
  377. else
  378. non_fatal("found, but use is disabled\n");
  379. }
  380. stop_ptraced_child(pid, 1, 1);
  381. #else
  382. /* PTRACE_LDT might be disabled via cmdline option.
  383. * We want to override this, else we might use the stub
  384. * without real need
  385. */
  386. ptrace_ldt = 1;
  387. #endif
  388. }
  389. static inline void check_skas3_proc_mm(void)
  390. {
  391. non_fatal(" - /proc/mm...");
  392. if (access("/proc/mm", W_OK) < 0) {
  393. proc_mm = 0;
  394. perror("not found");
  395. }
  396. else if (!proc_mm)
  397. non_fatal("found but disabled on command line\n");
  398. else non_fatal("found\n");
  399. }
  400. void can_do_skas(void)
  401. {
  402. non_fatal("Checking for the skas3 patch in the host:\n");
  403. check_skas3_proc_mm();
  404. check_skas3_ptrace_faultinfo();
  405. check_skas3_ptrace_ldt();
  406. if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  407. skas_needs_stub = 1;
  408. }
  409. int __init parse_iomem(char *str, int *add)
  410. {
  411. struct iomem_region *new;
  412. struct stat64 buf;
  413. char *file, *driver;
  414. int fd, size;
  415. driver = str;
  416. file = strchr(str,',');
  417. if (file == NULL) {
  418. printf("parse_iomem : failed to parse iomem\n");
  419. goto out;
  420. }
  421. *file = '\0';
  422. file++;
  423. fd = open(file, O_RDWR, 0);
  424. if (fd < 0) {
  425. perror("parse_iomem - Couldn't open io file");
  426. goto out;
  427. }
  428. if (fstat64(fd, &buf) < 0) {
  429. perror("parse_iomem - cannot stat_fd file");
  430. goto out_close;
  431. }
  432. new = malloc(sizeof(*new));
  433. if (new == NULL) {
  434. perror("Couldn't allocate iomem_region struct");
  435. goto out_close;
  436. }
  437. size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  438. *new = ((struct iomem_region) { .next = iomem_regions,
  439. .driver = driver,
  440. .fd = fd,
  441. .size = size,
  442. .phys = 0,
  443. .virt = 0 });
  444. iomem_regions = new;
  445. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  446. return 0;
  447. out_close:
  448. close(fd);
  449. out:
  450. return 1;
  451. }