start_up.c 13 KB

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