process.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 Benjamin Berg <benjamin@sipsolutions.net>
  4. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  5. * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  6. */
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <unistd.h>
  10. #include <sched.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <fcntl.h>
  14. #include <mem_user.h>
  15. #include <sys/mman.h>
  16. #include <sys/wait.h>
  17. #include <sys/stat.h>
  18. #include <sys/socket.h>
  19. #include <asm/unistd.h>
  20. #include <as-layout.h>
  21. #include <init.h>
  22. #include <kern_util.h>
  23. #include <mem.h>
  24. #include <os.h>
  25. #include <ptrace_user.h>
  26. #include <registers.h>
  27. #include <skas.h>
  28. #include <sysdep/stub.h>
  29. #include <sysdep/mcontext.h>
  30. #include <linux/futex.h>
  31. #include <linux/threads.h>
  32. #include <timetravel.h>
  33. #include <asm-generic/rwonce.h>
  34. #include "../internal.h"
  35. int is_skas_winch(int pid, int fd, void *data)
  36. {
  37. return pid == getpgrp();
  38. }
  39. static const char *ptrace_reg_name(int idx)
  40. {
  41. #define R(n) case HOST_##n: return #n
  42. switch (idx) {
  43. #ifdef __x86_64__
  44. R(BX);
  45. R(CX);
  46. R(DI);
  47. R(SI);
  48. R(DX);
  49. R(BP);
  50. R(AX);
  51. R(R8);
  52. R(R9);
  53. R(R10);
  54. R(R11);
  55. R(R12);
  56. R(R13);
  57. R(R14);
  58. R(R15);
  59. R(ORIG_AX);
  60. R(CS);
  61. R(SS);
  62. R(EFLAGS);
  63. #elif defined(__i386__)
  64. R(IP);
  65. R(SP);
  66. R(EFLAGS);
  67. R(AX);
  68. R(BX);
  69. R(CX);
  70. R(DX);
  71. R(SI);
  72. R(DI);
  73. R(BP);
  74. R(CS);
  75. R(SS);
  76. R(DS);
  77. R(FS);
  78. R(ES);
  79. R(GS);
  80. R(ORIG_AX);
  81. #endif
  82. }
  83. return "";
  84. }
  85. static int ptrace_dump_regs(int pid)
  86. {
  87. unsigned long regs[MAX_REG_NR];
  88. int i;
  89. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  90. return -errno;
  91. printk(UM_KERN_ERR "Stub registers -\n");
  92. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  93. const char *regname = ptrace_reg_name(i);
  94. printk(UM_KERN_ERR "\t%s\t(%2d): %lx\n", regname, i, regs[i]);
  95. }
  96. return 0;
  97. }
  98. /*
  99. * Signals that are OK to receive in the stub - we'll just continue it.
  100. * SIGWINCH will happen when UML is inside a detached screen.
  101. */
  102. #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
  103. /* Signals that the stub will finish with - anything else is an error */
  104. #define STUB_DONE_MASK (1 << SIGTRAP)
  105. void wait_stub_done(int pid)
  106. {
  107. int n, status, err;
  108. while (1) {
  109. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
  110. if ((n < 0) || !WIFSTOPPED(status))
  111. goto bad_wait;
  112. if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
  113. break;
  114. err = ptrace(PTRACE_CONT, pid, 0, 0);
  115. if (err) {
  116. printk(UM_KERN_ERR "%s : continue failed, errno = %d\n",
  117. __func__, errno);
  118. fatal_sigsegv();
  119. }
  120. }
  121. if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
  122. return;
  123. bad_wait:
  124. err = ptrace_dump_regs(pid);
  125. if (err)
  126. printk(UM_KERN_ERR "Failed to get registers from stub, errno = %d\n",
  127. -err);
  128. printk(UM_KERN_ERR "%s : failed to wait for SIGTRAP, pid = %d, n = %d, errno = %d, status = 0x%x\n",
  129. __func__, pid, n, errno, status);
  130. fatal_sigsegv();
  131. }
  132. void wait_stub_done_seccomp(struct mm_id *mm_idp, int running, int wait_sigsys)
  133. {
  134. struct stub_data *data = (void *)mm_idp->stack;
  135. int ret;
  136. do {
  137. const char byte = 0;
  138. struct iovec iov = {
  139. .iov_base = (void *)&byte,
  140. .iov_len = sizeof(byte),
  141. };
  142. union {
  143. char data[CMSG_SPACE(sizeof(mm_idp->syscall_fd_map))];
  144. struct cmsghdr align;
  145. } ctrl;
  146. struct msghdr msgh = {
  147. .msg_iov = &iov,
  148. .msg_iovlen = 1,
  149. };
  150. if (!running) {
  151. if (mm_idp->syscall_fd_num) {
  152. unsigned int fds_size =
  153. sizeof(int) * mm_idp->syscall_fd_num;
  154. struct cmsghdr *cmsg;
  155. msgh.msg_control = ctrl.data;
  156. msgh.msg_controllen = CMSG_SPACE(fds_size);
  157. cmsg = CMSG_FIRSTHDR(&msgh);
  158. cmsg->cmsg_level = SOL_SOCKET;
  159. cmsg->cmsg_type = SCM_RIGHTS;
  160. cmsg->cmsg_len = CMSG_LEN(fds_size);
  161. memcpy(CMSG_DATA(cmsg), mm_idp->syscall_fd_map,
  162. fds_size);
  163. CATCH_EINTR(syscall(__NR_sendmsg, mm_idp->sock,
  164. &msgh, 0));
  165. }
  166. data->signal = 0;
  167. data->futex = FUTEX_IN_CHILD;
  168. CATCH_EINTR(syscall(__NR_futex, &data->futex,
  169. FUTEX_WAKE, 1, NULL, NULL, 0));
  170. }
  171. do {
  172. /*
  173. * We need to check whether the child is still alive
  174. * before and after the FUTEX_WAIT call. Before, in
  175. * case it just died but we still updated data->futex
  176. * to FUTEX_IN_CHILD. And after, in case it died while
  177. * we were waiting (and SIGCHLD woke us up, see the
  178. * IRQ handler in mmu.c).
  179. *
  180. * Either way, if PID is negative, then we have no
  181. * choice but to kill the task.
  182. */
  183. if (__READ_ONCE(mm_idp->pid) < 0)
  184. goto out_kill;
  185. ret = syscall(__NR_futex, &data->futex,
  186. FUTEX_WAIT, FUTEX_IN_CHILD,
  187. NULL, NULL, 0);
  188. if (ret < 0 && errno != EINTR && errno != EAGAIN) {
  189. printk(UM_KERN_ERR "%s : FUTEX_WAIT failed, errno = %d\n",
  190. __func__, errno);
  191. goto out_kill;
  192. }
  193. } while (data->futex == FUTEX_IN_CHILD);
  194. if (__READ_ONCE(mm_idp->pid) < 0)
  195. goto out_kill;
  196. running = 0;
  197. /* We may receive a SIGALRM before SIGSYS, iterate again. */
  198. } while (wait_sigsys && data->signal == SIGALRM);
  199. if (data->mctx_offset > sizeof(data->sigstack) - sizeof(mcontext_t)) {
  200. printk(UM_KERN_ERR "%s : invalid mcontext offset", __func__);
  201. goto out_kill;
  202. }
  203. if (wait_sigsys && data->signal != SIGSYS) {
  204. printk(UM_KERN_ERR "%s : expected SIGSYS but got %d",
  205. __func__, data->signal);
  206. goto out_kill;
  207. }
  208. return;
  209. out_kill:
  210. printk(UM_KERN_ERR "%s : failed to wait for stub, pid = %d, errno = %d\n",
  211. __func__, mm_idp->pid, errno);
  212. /* This is not true inside start_userspace */
  213. if (current_mm_id() == mm_idp)
  214. fatal_sigsegv();
  215. }
  216. extern unsigned long current_stub_stack(void);
  217. static void get_skas_faultinfo(int pid, struct faultinfo *fi)
  218. {
  219. int err;
  220. err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
  221. if (err) {
  222. printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
  223. "errno = %d\n", pid, errno);
  224. fatal_sigsegv();
  225. }
  226. wait_stub_done(pid);
  227. /*
  228. * faultinfo is prepared by the stub_segv_handler at start of
  229. * the stub stack page. We just have to copy it.
  230. */
  231. memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
  232. }
  233. static void handle_trap(struct uml_pt_regs *regs)
  234. {
  235. if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
  236. fatal_sigsegv();
  237. handle_syscall(regs);
  238. }
  239. extern char __syscall_stub_start[];
  240. static int stub_exe_fd;
  241. struct tramp_data {
  242. struct stub_data *stub_data;
  243. /* 0 is inherited, 1 is the kernel side */
  244. int sockpair[2];
  245. };
  246. #ifndef CLOSE_RANGE_CLOEXEC
  247. #define CLOSE_RANGE_CLOEXEC (1U << 2)
  248. #endif
  249. static int userspace_tramp(void *data)
  250. {
  251. struct tramp_data *tramp_data = data;
  252. char *const argv[] = { "uml-userspace", NULL };
  253. unsigned long long offset;
  254. struct stub_init_data init_data = {
  255. .seccomp = using_seccomp,
  256. .stub_start = STUB_START,
  257. };
  258. int ret;
  259. if (using_seccomp) {
  260. init_data.signal_handler = STUB_CODE +
  261. (unsigned long) stub_signal_interrupt -
  262. (unsigned long) __syscall_stub_start;
  263. init_data.signal_restorer = STUB_CODE +
  264. (unsigned long) stub_signal_restorer -
  265. (unsigned long) __syscall_stub_start;
  266. } else {
  267. init_data.signal_handler = STUB_CODE +
  268. (unsigned long) stub_segv_handler -
  269. (unsigned long) __syscall_stub_start;
  270. init_data.signal_restorer = 0;
  271. }
  272. init_data.stub_code_fd = phys_mapping(uml_to_phys(__syscall_stub_start),
  273. &offset);
  274. init_data.stub_code_offset = MMAP_OFFSET(offset);
  275. init_data.stub_data_fd = phys_mapping(uml_to_phys(tramp_data->stub_data),
  276. &offset);
  277. init_data.stub_data_offset = MMAP_OFFSET(offset);
  278. /*
  279. * Avoid leaking unneeded FDs to the stub by setting CLOEXEC on all FDs
  280. * and then unsetting it on all memory related FDs.
  281. * This is not strictly necessary from a safety perspective.
  282. */
  283. syscall(__NR_close_range, 0, ~0U, CLOSE_RANGE_CLOEXEC);
  284. fcntl(init_data.stub_data_fd, F_SETFD, 0);
  285. /* dup2 signaling FD/socket to STDIN */
  286. if (dup2(tramp_data->sockpair[0], 0) < 0)
  287. exit(3);
  288. close(tramp_data->sockpair[0]);
  289. /* Write init_data and close write side */
  290. ret = write(tramp_data->sockpair[1], &init_data, sizeof(init_data));
  291. close(tramp_data->sockpair[1]);
  292. if (ret != sizeof(init_data))
  293. exit(4);
  294. /* Raw execveat for compatibility with older libc versions */
  295. syscall(__NR_execveat, stub_exe_fd, (unsigned long)"",
  296. (unsigned long)argv, NULL, AT_EMPTY_PATH);
  297. exit(5);
  298. }
  299. extern char stub_exe_start[];
  300. extern char stub_exe_end[];
  301. extern char *tempdir;
  302. #define STUB_EXE_NAME_TEMPLATE "/uml-userspace-XXXXXX"
  303. #ifndef MFD_EXEC
  304. #define MFD_EXEC 0x0010U
  305. #endif
  306. static int __init init_stub_exe_fd(void)
  307. {
  308. size_t written = 0;
  309. char *tmpfile = NULL;
  310. stub_exe_fd = memfd_create("uml-userspace",
  311. MFD_EXEC | MFD_CLOEXEC | MFD_ALLOW_SEALING);
  312. if (stub_exe_fd < 0) {
  313. printk(UM_KERN_INFO "Could not create executable memfd, using temporary file!");
  314. tmpfile = malloc(strlen(tempdir) +
  315. strlen(STUB_EXE_NAME_TEMPLATE) + 1);
  316. if (tmpfile == NULL)
  317. panic("Failed to allocate memory for stub binary name");
  318. strcpy(tmpfile, tempdir);
  319. strcat(tmpfile, STUB_EXE_NAME_TEMPLATE);
  320. stub_exe_fd = mkstemp(tmpfile);
  321. if (stub_exe_fd < 0)
  322. panic("Could not create temporary file for stub binary: %d",
  323. -errno);
  324. }
  325. while (written < stub_exe_end - stub_exe_start) {
  326. ssize_t res = write(stub_exe_fd, stub_exe_start + written,
  327. stub_exe_end - stub_exe_start - written);
  328. if (res < 0) {
  329. if (errno == EINTR)
  330. continue;
  331. if (tmpfile)
  332. unlink(tmpfile);
  333. panic("Failed write stub binary: %d", -errno);
  334. }
  335. written += res;
  336. }
  337. if (!tmpfile) {
  338. fcntl(stub_exe_fd, F_ADD_SEALS,
  339. F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL);
  340. } else {
  341. if (fchmod(stub_exe_fd, 00500) < 0) {
  342. unlink(tmpfile);
  343. panic("Could not make stub binary executable: %d",
  344. -errno);
  345. }
  346. close(stub_exe_fd);
  347. stub_exe_fd = open(tmpfile, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
  348. if (stub_exe_fd < 0) {
  349. unlink(tmpfile);
  350. panic("Could not reopen stub binary: %d", -errno);
  351. }
  352. unlink(tmpfile);
  353. free(tmpfile);
  354. }
  355. return 0;
  356. }
  357. __initcall(init_stub_exe_fd);
  358. int using_seccomp;
  359. /**
  360. * start_userspace() - prepare a new userspace process
  361. * @mm_id: The corresponding struct mm_id
  362. *
  363. * Setups a new temporary stack page that is used while userspace_tramp() runs
  364. * Clones the kernel process into a new userspace process, with FDs only.
  365. *
  366. * Return: When positive: the process id of the new userspace process,
  367. * when negative: an error number.
  368. * FIXME: can PIDs become negative?!
  369. */
  370. int start_userspace(struct mm_id *mm_id)
  371. {
  372. struct stub_data *proc_data = (void *)mm_id->stack;
  373. struct tramp_data tramp_data = {
  374. .stub_data = proc_data,
  375. };
  376. void *stack;
  377. unsigned long sp;
  378. int status, n, err;
  379. /* setup a temporary stack page */
  380. stack = mmap(NULL, UM_KERN_PAGE_SIZE,
  381. PROT_READ | PROT_WRITE | PROT_EXEC,
  382. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  383. if (stack == MAP_FAILED) {
  384. err = -errno;
  385. printk(UM_KERN_ERR "%s : mmap failed, errno = %d\n",
  386. __func__, errno);
  387. return err;
  388. }
  389. /* set stack pointer to the end of the stack page, so it can grow downwards */
  390. sp = (unsigned long)stack + UM_KERN_PAGE_SIZE;
  391. /* socket pair for init data and SECCOMP FD passing (no CLOEXEC here) */
  392. if (socketpair(AF_UNIX, SOCK_STREAM, 0, tramp_data.sockpair)) {
  393. err = -errno;
  394. printk(UM_KERN_ERR "%s : socketpair failed, errno = %d\n",
  395. __func__, errno);
  396. return err;
  397. }
  398. if (using_seccomp)
  399. proc_data->futex = FUTEX_IN_CHILD;
  400. mm_id->pid = clone(userspace_tramp, (void *) sp,
  401. CLONE_VFORK | CLONE_VM | SIGCHLD,
  402. (void *)&tramp_data);
  403. if (mm_id->pid < 0) {
  404. err = -errno;
  405. printk(UM_KERN_ERR "%s : clone failed, errno = %d\n",
  406. __func__, errno);
  407. goto out_close;
  408. }
  409. if (using_seccomp) {
  410. wait_stub_done_seccomp(mm_id, 1, 1);
  411. } else {
  412. do {
  413. CATCH_EINTR(n = waitpid(mm_id->pid, &status,
  414. WUNTRACED | __WALL));
  415. if (n < 0) {
  416. err = -errno;
  417. printk(UM_KERN_ERR "%s : wait failed, errno = %d\n",
  418. __func__, errno);
  419. goto out_kill;
  420. }
  421. } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
  422. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
  423. err = -EINVAL;
  424. printk(UM_KERN_ERR "%s : expected SIGSTOP, got status = %d\n",
  425. __func__, status);
  426. goto out_kill;
  427. }
  428. if (ptrace(PTRACE_SETOPTIONS, mm_id->pid, NULL,
  429. (void *) PTRACE_O_TRACESYSGOOD) < 0) {
  430. err = -errno;
  431. printk(UM_KERN_ERR "%s : PTRACE_SETOPTIONS failed, errno = %d\n",
  432. __func__, errno);
  433. goto out_kill;
  434. }
  435. }
  436. if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
  437. err = -errno;
  438. printk(UM_KERN_ERR "%s : munmap failed, errno = %d\n",
  439. __func__, errno);
  440. goto out_kill;
  441. }
  442. close(tramp_data.sockpair[0]);
  443. if (using_seccomp)
  444. mm_id->sock = tramp_data.sockpair[1];
  445. else
  446. close(tramp_data.sockpair[1]);
  447. return 0;
  448. out_kill:
  449. os_kill_ptraced_process(mm_id->pid, 1);
  450. out_close:
  451. close(tramp_data.sockpair[0]);
  452. close(tramp_data.sockpair[1]);
  453. mm_id->pid = -1;
  454. return err;
  455. }
  456. static int unscheduled_userspace_iterations;
  457. extern unsigned long tt_extra_sched_jiffies;
  458. void userspace(struct uml_pt_regs *regs)
  459. {
  460. int err, status, op;
  461. siginfo_t si_local;
  462. siginfo_t *si;
  463. int sig;
  464. /* Handle any immediate reschedules or signals */
  465. interrupt_end();
  466. while (1) {
  467. struct mm_id *mm_id = current_mm_id();
  468. /*
  469. * At any given time, only one CPU thread can enter the
  470. * turnstile to operate on the same stub process, including
  471. * executing stub system calls (mmap and munmap).
  472. */
  473. enter_turnstile(mm_id);
  474. /*
  475. * When we are in time-travel mode, userspace can theoretically
  476. * do a *lot* of work without being scheduled. The problem with
  477. * this is that it will prevent kernel bookkeeping (primarily
  478. * the RCU) from running and this can for example cause OOM
  479. * situations.
  480. *
  481. * This code accounts a jiffie against the scheduling clock
  482. * after the defined userspace iterations in the same thread.
  483. * By doing so the situation is effectively prevented.
  484. */
  485. if (time_travel_mode == TT_MODE_INFCPU ||
  486. time_travel_mode == TT_MODE_EXTERNAL) {
  487. #ifdef CONFIG_UML_MAX_USERSPACE_ITERATIONS
  488. if (CONFIG_UML_MAX_USERSPACE_ITERATIONS &&
  489. unscheduled_userspace_iterations++ >
  490. CONFIG_UML_MAX_USERSPACE_ITERATIONS) {
  491. tt_extra_sched_jiffies += 1;
  492. unscheduled_userspace_iterations = 0;
  493. }
  494. #endif
  495. }
  496. time_travel_print_bc_msg();
  497. current_mm_sync();
  498. if (using_seccomp) {
  499. struct stub_data *proc_data = (void *) mm_id->stack;
  500. err = set_stub_state(regs, proc_data, singlestepping());
  501. if (err) {
  502. printk(UM_KERN_ERR "%s - failed to set regs: %d",
  503. __func__, err);
  504. fatal_sigsegv();
  505. }
  506. /* Must have been reset by the syscall caller */
  507. if (proc_data->restart_wait != 0)
  508. panic("Programming error: Flag to only run syscalls in child was not cleared!");
  509. /* Mark pending syscalls for flushing */
  510. proc_data->syscall_data_len = mm_id->syscall_data_len;
  511. wait_stub_done_seccomp(mm_id, 0, 0);
  512. sig = proc_data->signal;
  513. if (sig == SIGTRAP && proc_data->err != 0) {
  514. printk(UM_KERN_ERR "%s - Error flushing stub syscalls",
  515. __func__);
  516. syscall_stub_dump_error(mm_id);
  517. mm_id->syscall_data_len = proc_data->err;
  518. fatal_sigsegv();
  519. }
  520. mm_id->syscall_data_len = 0;
  521. mm_id->syscall_fd_num = 0;
  522. err = get_stub_state(regs, proc_data, NULL);
  523. if (err) {
  524. printk(UM_KERN_ERR "%s - failed to get regs: %d",
  525. __func__, err);
  526. fatal_sigsegv();
  527. }
  528. if (proc_data->si_offset > sizeof(proc_data->sigstack) - sizeof(*si))
  529. panic("%s - Invalid siginfo offset from child", __func__);
  530. si = &si_local;
  531. memcpy(si, &proc_data->sigstack[proc_data->si_offset], sizeof(*si));
  532. regs->is_user = 1;
  533. /* Fill in ORIG_RAX and extract fault information */
  534. PT_SYSCALL_NR(regs->gp) = si->si_syscall;
  535. if (sig == SIGSEGV) {
  536. mcontext_t *mcontext = (void *)&proc_data->sigstack[proc_data->mctx_offset];
  537. GET_FAULTINFO_FROM_MC(regs->faultinfo, mcontext);
  538. }
  539. } else {
  540. int pid = mm_id->pid;
  541. /* Flush out any pending syscalls */
  542. err = syscall_stub_flush(mm_id);
  543. if (err) {
  544. if (err == -ENOMEM)
  545. report_enomem();
  546. printk(UM_KERN_ERR "%s - Error flushing stub syscalls: %d",
  547. __func__, -err);
  548. fatal_sigsegv();
  549. }
  550. /*
  551. * This can legitimately fail if the process loads a
  552. * bogus value into a segment register. It will
  553. * segfault and PTRACE_GETREGS will read that value
  554. * out of the process. However, PTRACE_SETREGS will
  555. * fail. In this case, there is nothing to do but
  556. * just kill the process.
  557. */
  558. if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
  559. printk(UM_KERN_ERR "%s - ptrace set regs failed, errno = %d\n",
  560. __func__, errno);
  561. fatal_sigsegv();
  562. }
  563. if (put_fp_registers(pid, regs->fp)) {
  564. printk(UM_KERN_ERR "%s - ptrace set fp regs failed, errno = %d\n",
  565. __func__, errno);
  566. fatal_sigsegv();
  567. }
  568. if (singlestepping())
  569. op = PTRACE_SYSEMU_SINGLESTEP;
  570. else
  571. op = PTRACE_SYSEMU;
  572. if (ptrace(op, pid, 0, 0)) {
  573. printk(UM_KERN_ERR "%s - ptrace continue failed, op = %d, errno = %d\n",
  574. __func__, op, errno);
  575. fatal_sigsegv();
  576. }
  577. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
  578. if (err < 0) {
  579. printk(UM_KERN_ERR "%s - wait failed, errno = %d\n",
  580. __func__, errno);
  581. fatal_sigsegv();
  582. }
  583. regs->is_user = 1;
  584. if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
  585. printk(UM_KERN_ERR "%s - PTRACE_GETREGS failed, errno = %d\n",
  586. __func__, errno);
  587. fatal_sigsegv();
  588. }
  589. if (get_fp_registers(pid, regs->fp)) {
  590. printk(UM_KERN_ERR "%s - get_fp_registers failed, errno = %d\n",
  591. __func__, errno);
  592. fatal_sigsegv();
  593. }
  594. if (WIFSTOPPED(status)) {
  595. sig = WSTOPSIG(status);
  596. /*
  597. * These signal handlers need the si argument
  598. * and SIGSEGV needs the faultinfo.
  599. * The SIGIO and SIGALARM handlers which constitute
  600. * the majority of invocations, do not use it.
  601. */
  602. switch (sig) {
  603. case SIGSEGV:
  604. get_skas_faultinfo(pid,
  605. &regs->faultinfo);
  606. fallthrough;
  607. case SIGTRAP:
  608. case SIGILL:
  609. case SIGBUS:
  610. case SIGFPE:
  611. case SIGWINCH:
  612. ptrace(PTRACE_GETSIGINFO, pid, 0,
  613. (struct siginfo *)&si_local);
  614. si = &si_local;
  615. break;
  616. default:
  617. si = NULL;
  618. break;
  619. }
  620. } else {
  621. sig = 0;
  622. }
  623. }
  624. exit_turnstile(mm_id);
  625. UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
  626. if (sig) {
  627. switch (sig) {
  628. case SIGSEGV:
  629. if (using_seccomp || PTRACE_FULL_FAULTINFO)
  630. (*sig_info[SIGSEGV])(SIGSEGV,
  631. (struct siginfo *)si,
  632. regs, NULL);
  633. else
  634. segv(regs->faultinfo, 0, 1, NULL, NULL);
  635. break;
  636. case SIGSYS:
  637. handle_syscall(regs);
  638. break;
  639. case SIGTRAP + 0x80:
  640. handle_trap(regs);
  641. break;
  642. case SIGTRAP:
  643. relay_signal(SIGTRAP, (struct siginfo *)si, regs, NULL);
  644. break;
  645. case SIGALRM:
  646. break;
  647. case SIGIO:
  648. case SIGILL:
  649. case SIGBUS:
  650. case SIGFPE:
  651. case SIGWINCH:
  652. block_signals_trace();
  653. (*sig_info[sig])(sig, (struct siginfo *)si, regs, NULL);
  654. unblock_signals_trace();
  655. break;
  656. default:
  657. printk(UM_KERN_ERR "%s - child stopped with signal %d\n",
  658. __func__, sig);
  659. fatal_sigsegv();
  660. }
  661. interrupt_end();
  662. /* Avoid -ERESTARTSYS handling in host */
  663. if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
  664. PT_SYSCALL_NR(regs->gp) = -1;
  665. }
  666. }
  667. }
  668. void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
  669. {
  670. (*buf)[0].JB_IP = (unsigned long) handler;
  671. (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
  672. sizeof(void *);
  673. }
  674. #define INIT_JMP_NEW_THREAD 0
  675. #define INIT_JMP_CALLBACK 1
  676. #define INIT_JMP_HALT 2
  677. #define INIT_JMP_REBOOT 3
  678. void switch_threads(jmp_buf *me, jmp_buf *you)
  679. {
  680. unscheduled_userspace_iterations = 0;
  681. if (UML_SETJMP(me) == 0)
  682. UML_LONGJMP(you, 1);
  683. }
  684. static jmp_buf initial_jmpbuf;
  685. static __thread void (*cb_proc)(void *arg);
  686. static __thread void *cb_arg;
  687. static __thread jmp_buf *cb_back;
  688. int start_idle_thread(void *stack, jmp_buf *switch_buf)
  689. {
  690. int n;
  691. set_handler(SIGWINCH);
  692. /*
  693. * Can't use UML_SETJMP or UML_LONGJMP here because they save
  694. * and restore signals, with the possible side-effect of
  695. * trying to handle any signals which came when they were
  696. * blocked, which can't be done on this stack.
  697. * Signals must be blocked when jumping back here and restored
  698. * after returning to the jumper.
  699. */
  700. n = setjmp(initial_jmpbuf);
  701. switch (n) {
  702. case INIT_JMP_NEW_THREAD:
  703. (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
  704. (*switch_buf)[0].JB_SP = (unsigned long) stack +
  705. UM_THREAD_SIZE - sizeof(void *);
  706. break;
  707. case INIT_JMP_CALLBACK:
  708. (*cb_proc)(cb_arg);
  709. longjmp(*cb_back, 1);
  710. break;
  711. case INIT_JMP_HALT:
  712. kmalloc_ok = 0;
  713. return 0;
  714. case INIT_JMP_REBOOT:
  715. kmalloc_ok = 0;
  716. return 1;
  717. default:
  718. printk(UM_KERN_ERR "Bad sigsetjmp return in %s - %d\n",
  719. __func__, n);
  720. fatal_sigsegv();
  721. }
  722. longjmp(*switch_buf, 1);
  723. /* unreachable */
  724. printk(UM_KERN_ERR "impossible long jump!");
  725. fatal_sigsegv();
  726. return 0;
  727. }
  728. void initial_thread_cb_skas(void (*proc)(void *), void *arg)
  729. {
  730. jmp_buf here;
  731. cb_proc = proc;
  732. cb_arg = arg;
  733. cb_back = &here;
  734. initial_jmpbuf_lock();
  735. if (UML_SETJMP(&here) == 0)
  736. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
  737. initial_jmpbuf_unlock();
  738. cb_proc = NULL;
  739. cb_arg = NULL;
  740. cb_back = NULL;
  741. }
  742. void halt_skas(void)
  743. {
  744. initial_jmpbuf_lock();
  745. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
  746. /* unreachable */
  747. }
  748. static bool noreboot;
  749. static int __init noreboot_cmd_param(char *str, int *add)
  750. {
  751. *add = 0;
  752. noreboot = true;
  753. return 0;
  754. }
  755. __uml_setup("noreboot", noreboot_cmd_param,
  756. "noreboot\n"
  757. " Rather than rebooting, exit always, akin to QEMU's -no-reboot option.\n"
  758. " This is useful if you're using CONFIG_PANIC_TIMEOUT in order to catch\n"
  759. " crashes in CI\n\n");
  760. void reboot_skas(void)
  761. {
  762. initial_jmpbuf_lock();
  763. UML_LONGJMP(&initial_jmpbuf, noreboot ? INIT_JMP_HALT : INIT_JMP_REBOOT);
  764. /* unreachable */
  765. }