start_up.c 12 KB

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