start_up.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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.h"
  26. #include "skas_ptrace.h"
  27. static void ptrace_child(void)
  28. {
  29. int ret;
  30. /* Calling os_getpid because some libcs cached getpid incorrectly */
  31. int pid = os_getpid(), ppid = getppid();
  32. int sc_result;
  33. if (change_sig(SIGWINCH, 0) < 0 ||
  34. ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
  35. perror("ptrace");
  36. kill(pid, SIGKILL);
  37. }
  38. kill(pid, SIGSTOP);
  39. /*
  40. * This syscall will be intercepted by the parent. Don't call more than
  41. * once, please.
  42. */
  43. sc_result = os_getpid();
  44. if (sc_result == pid)
  45. /* Nothing modified by the parent, we are running normally. */
  46. ret = 1;
  47. else if (sc_result == ppid)
  48. /*
  49. * Expected in check_ptrace and check_sysemu when they succeed
  50. * in modifying the stack frame
  51. */
  52. ret = 0;
  53. else
  54. /* Serious trouble! This could be caused by a bug in host 2.6
  55. * SKAS3/2.6 patch before release -V6, together with a bug in
  56. * the UML code itself.
  57. */
  58. ret = 2;
  59. exit(ret);
  60. }
  61. static void fatal_perror(const char *str)
  62. {
  63. perror(str);
  64. exit(1);
  65. }
  66. static void fatal(char *fmt, ...)
  67. {
  68. va_list list;
  69. va_start(list, fmt);
  70. vfprintf(stderr, fmt, list);
  71. va_end(list);
  72. exit(1);
  73. }
  74. static void non_fatal(char *fmt, ...)
  75. {
  76. va_list list;
  77. va_start(list, fmt);
  78. vfprintf(stderr, fmt, list);
  79. va_end(list);
  80. }
  81. static int start_ptraced_child(void)
  82. {
  83. int pid, n, status;
  84. pid = fork();
  85. if (pid == 0)
  86. ptrace_child();
  87. else if (pid < 0)
  88. fatal_perror("start_ptraced_child : fork failed");
  89. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  90. if (n < 0)
  91. fatal_perror("check_ptrace : waitpid failed");
  92. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  93. fatal("check_ptrace : expected SIGSTOP, got status = %d",
  94. status);
  95. return pid;
  96. }
  97. /* When testing for SYSEMU support, if it is one of the broken versions, we
  98. * must just avoid using sysemu, not panic, but only if SYSEMU features are
  99. * broken.
  100. * So only for SYSEMU features we test mustpanic, while normal host features
  101. * must work anyway!
  102. */
  103. static int stop_ptraced_child(int pid, int exitcode, int mustexit)
  104. {
  105. int status, n, ret = 0;
  106. if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
  107. perror("stop_ptraced_child : ptrace failed");
  108. return -1;
  109. }
  110. CATCH_EINTR(n = waitpid(pid, &status, 0));
  111. if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
  112. int exit_with = WEXITSTATUS(status);
  113. if (exit_with == 2)
  114. non_fatal("check_ptrace : child exited with status 2. "
  115. "\nDisabling SYSEMU support.\n");
  116. non_fatal("check_ptrace : child exited with exitcode %d, while "
  117. "expecting %d; status 0x%x\n", exit_with,
  118. exitcode, status);
  119. if (mustexit)
  120. exit(1);
  121. ret = -1;
  122. }
  123. return ret;
  124. }
  125. /* Changed only during early boot */
  126. int ptrace_faultinfo;
  127. static int disable_ptrace_faultinfo;
  128. int ptrace_ldt;
  129. static int disable_ptrace_ldt;
  130. int proc_mm;
  131. static int disable_proc_mm;
  132. int have_switch_mm;
  133. static int disable_switch_mm;
  134. int skas_needs_stub;
  135. static int __init skas0_cmd_param(char *str, int* add)
  136. {
  137. disable_ptrace_faultinfo = 1;
  138. disable_ptrace_ldt = 1;
  139. disable_proc_mm = 1;
  140. disable_switch_mm = 1;
  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 and SKAS4 usage, so that SKAS0 is used\n\n");
  149. __uml_setup("mode=skas0", mode_skas0_cmd_param,
  150. "mode=skas0\n"
  151. " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
  152. /* Changed only during early boot */
  153. static int force_sysemu_disabled = 0;
  154. static int __init nosysemu_cmd_param(char *str, int* add)
  155. {
  156. force_sysemu_disabled = 1;
  157. return 0;
  158. }
  159. __uml_setup("nosysemu", nosysemu_cmd_param,
  160. "nosysemu\n"
  161. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  162. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  163. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  164. " To make it working, you need a kernel patch for your host, too.\n"
  165. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  166. " information.\n\n");
  167. static void __init check_sysemu(void)
  168. {
  169. unsigned long regs[MAX_REG_NR];
  170. int pid, n, status, count=0;
  171. non_fatal("Checking syscall emulation patch for ptrace...");
  172. sysemu_supported = 0;
  173. pid = start_ptraced_child();
  174. if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  175. goto fail;
  176. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  177. if (n < 0)
  178. fatal_perror("check_sysemu : wait failed");
  179. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  180. fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
  181. status);
  182. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  183. fatal_perror("check_sysemu : PTRACE_GETREGS failed");
  184. if (PT_SYSCALL_NR(regs) != __NR_getpid) {
  185. non_fatal("check_sysemu got system call number %d, "
  186. "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
  187. goto fail;
  188. }
  189. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
  190. if (n < 0) {
  191. non_fatal("check_sysemu : failed to modify system call "
  192. "return");
  193. goto fail;
  194. }
  195. if (stop_ptraced_child(pid, 0, 0) < 0)
  196. goto fail_stopped;
  197. sysemu_supported = 1;
  198. non_fatal("OK\n");
  199. set_using_sysemu(!force_sysemu_disabled);
  200. non_fatal("Checking advanced syscall emulation patch for ptrace...");
  201. pid = start_ptraced_child();
  202. if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  203. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  204. fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
  205. while (1) {
  206. count++;
  207. if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  208. goto fail;
  209. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  210. if (n < 0)
  211. fatal_perror("check_sysemu: wait failed");
  212. if (WIFSTOPPED(status) &&
  213. (WSTOPSIG(status) == (SIGTRAP|0x80))) {
  214. if (!count) {
  215. non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
  216. "doesn't singlestep");
  217. goto fail;
  218. }
  219. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  220. os_getpid());
  221. if (n < 0)
  222. fatal_perror("check_sysemu : failed to modify "
  223. "system call return");
  224. break;
  225. }
  226. else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  227. count++;
  228. else {
  229. non_fatal("check_sysemu: expected SIGTRAP or "
  230. "(SIGTRAP | 0x80), got status = %d\n",
  231. status);
  232. goto fail;
  233. }
  234. }
  235. if (stop_ptraced_child(pid, 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, 1, 0);
  244. fail_stopped:
  245. non_fatal("missing\n");
  246. }
  247. static void __init check_ptrace(void)
  248. {
  249. int pid, syscall, n, status;
  250. non_fatal("Checking that ptrace can change system call numbers...");
  251. pid = start_ptraced_child();
  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, 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. int pid;
  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. pid = start_ptraced_child();
  309. if (init_registers(pid))
  310. fatal("Failed to initialize default registers");
  311. stop_ptraced_child(pid, 1, 1);
  312. }
  313. static int __init noprocmm_cmd_param(char *str, int* add)
  314. {
  315. disable_proc_mm = 1;
  316. return 0;
  317. }
  318. __uml_setup("noprocmm", noprocmm_cmd_param,
  319. "noprocmm\n"
  320. " Turns off usage of /proc/mm, even if host supports it.\n"
  321. " To support /proc/mm, the host needs to be patched using\n"
  322. " the current skas3 patch.\n\n");
  323. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  324. {
  325. disable_ptrace_faultinfo = 1;
  326. return 0;
  327. }
  328. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  329. "noptracefaultinfo\n"
  330. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  331. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  332. " using the current skas3 patch.\n\n");
  333. static int __init noptraceldt_cmd_param(char *str, int* add)
  334. {
  335. disable_ptrace_ldt = 1;
  336. return 0;
  337. }
  338. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  339. "noptraceldt\n"
  340. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  341. " To support PTRACE_LDT, the host needs to be patched using\n"
  342. " the current skas3 patch.\n\n");
  343. static inline void check_skas3_ptrace_faultinfo(void)
  344. {
  345. struct ptrace_faultinfo fi;
  346. int pid, n;
  347. non_fatal(" - PTRACE_FAULTINFO...");
  348. pid = start_ptraced_child();
  349. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  350. if (n < 0) {
  351. if (errno == EIO)
  352. non_fatal("not found\n");
  353. else
  354. perror("not found");
  355. } else if (disable_ptrace_faultinfo)
  356. non_fatal("found but disabled on command line\n");
  357. else {
  358. ptrace_faultinfo = 1;
  359. non_fatal("found\n");
  360. }
  361. stop_ptraced_child(pid, 1, 1);
  362. }
  363. static inline void check_skas3_ptrace_ldt(void)
  364. {
  365. #ifdef PTRACE_LDT
  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();
  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. } else if (disable_ptrace_ldt)
  381. non_fatal("found, but use is disabled\n");
  382. else {
  383. ptrace_ldt = 1;
  384. non_fatal("found\n");
  385. }
  386. stop_ptraced_child(pid, 1, 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. perror("not found");
  394. else if (disable_proc_mm)
  395. non_fatal("found but disabled on command line\n");
  396. else {
  397. proc_mm = 1;
  398. non_fatal("found\n");
  399. }
  400. }
  401. void can_do_skas(void)
  402. {
  403. non_fatal("Checking for the skas3 patch in the host:\n");
  404. check_skas3_proc_mm();
  405. check_skas3_ptrace_faultinfo();
  406. check_skas3_ptrace_ldt();
  407. if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  408. skas_needs_stub = 1;
  409. }
  410. int __init parse_iomem(char *str, int *add)
  411. {
  412. struct iomem_region *new;
  413. struct stat64 buf;
  414. char *file, *driver;
  415. int fd, size;
  416. driver = str;
  417. file = strchr(str,',');
  418. if (file == NULL) {
  419. fprintf(stderr, "parse_iomem : failed to parse iomem\n");
  420. goto out;
  421. }
  422. *file = '\0';
  423. file++;
  424. fd = open(file, O_RDWR, 0);
  425. if (fd < 0) {
  426. perror("parse_iomem - Couldn't open io file");
  427. goto out;
  428. }
  429. if (fstat64(fd, &buf) < 0) {
  430. perror("parse_iomem - cannot stat_fd file");
  431. goto out_close;
  432. }
  433. new = malloc(sizeof(*new));
  434. if (new == NULL) {
  435. perror("Couldn't allocate iomem_region struct");
  436. goto out_close;
  437. }
  438. size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  439. *new = ((struct iomem_region) { .next = iomem_regions,
  440. .driver = driver,
  441. .fd = fd,
  442. .size = size,
  443. .phys = 0,
  444. .virt = 0 });
  445. iomem_regions = new;
  446. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  447. return 0;
  448. out_close:
  449. close(fd);
  450. out:
  451. return 1;
  452. }