start_up.c 13 KB

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