start_up.c 13 KB

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