start_up.c 13 KB

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