staticint ptrace_dump_regs(int pid)
{ unsignedlong regs[MAX_REG_NR]; int i;
if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) return -errno;
printk(UM_KERN_ERR "Stub registers -\n"); for (i = 0; i < ARRAY_SIZE(regs); i++) { constchar *regname = ptrace_reg_name(i);
printk(UM_KERN_ERR "\t%s\t(%2d): %lx\n", regname, i, regs[i]);
}
return 0;
}
/* * Signals that are OK to receive in the stub - we'll just continue it. * SIGWINCH will happen when UML is inside a detached screen.
*/ #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
/* Signals that the stub will finish with - anything else is an error */ #define STUB_DONE_MASK (1 << SIGTRAP)
void wait_stub_done(int pid)
{ int n, status, err;
while (1) {
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL)); if ((n < 0) || !WIFSTOPPED(status)) goto bad_wait;
if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0) break;
if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0) return;
bad_wait:
err = ptrace_dump_regs(pid); if (err)
printk(UM_KERN_ERR "Failed to get registers from stub, errno = %d\n",
-err);
printk(UM_KERN_ERR "%s : failed to wait for SIGTRAP, pid = %d, n = %d, errno = %d, status = 0x%x\n",
__func__, pid, n, errno, status);
fatal_sigsegv();
}
void wait_stub_done_seccomp(struct mm_id *mm_idp, int running, int wait_sigsys)
{ struct stub_data *data = (void *)mm_idp->stack; int ret;
do { /* * We need to check whether the child is still alive * before and after the FUTEX_WAIT call. Before, in * case it just died but we still updated data->futex * to FUTEX_IN_CHILD. And after, in case it died while * we were waiting (and SIGCHLD woke us up, see the * IRQ handler in mmu.c). * * Either way, if PID is negative, then we have no * choice but to kill the task.
*/ if (__READ_ONCE(mm_idp->pid) < 0) goto out_kill;
/* * faultinfo is prepared by the stub_segv_handler at start of * the stub stack page. We just have to copy it.
*/
memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
}
/* * Avoid leaking unneeded FDs to the stub by setting CLOEXEC on all FDs * and then unsetting it on all memory related FDs. * This is not strictly necessary from a safety perspective.
*/
syscall(__NR_close_range, 0, ~0U, CLOSE_RANGE_CLOEXEC);
fcntl(init_data.stub_data_fd, F_SETFD, 0);
/* In SECCOMP mode, these FDs are passed when needed */ if (!using_seccomp) { for (iomem = iomem_regions; iomem; iomem = iomem->next)
fcntl(iomem->fd, F_SETFD, 0);
}
/* dup2 signaling FD/socket to STDIN */ if (dup2(tramp_data->sockpair[0], 0) < 0) exit(3);
close(tramp_data->sockpair[0]);
/* Write init_data and close write side */
ret = write(tramp_data->sockpair[1], &init_data, sizeof(init_data));
close(tramp_data->sockpair[1]);
if (ret != sizeof(init_data)) exit(4);
/* Raw execveat for compatibility with older libc versions */
syscall(__NR_execveat, stub_exe_fd, (unsignedlong)"",
(unsignedlong)argv, NULL, AT_EMPTY_PATH);
/** * start_userspace() - prepare a new userspace process * @mm_id: The corresponding struct mm_id * * Setups a new temporary stack page that is used while userspace_tramp() runs * Clones the kernel process into a new userspace process, with FDs only. * * Return: When positive: the process id of the new userspace process, * when negative: an error number. * FIXME: can PIDs become negative?!
*/ int start_userspace(struct mm_id *mm_id)
{ struct stub_data *proc_data = (void *)mm_id->stack; struct tramp_data tramp_data = {
.stub_data = proc_data,
}; void *stack; unsignedlong sp; int status, n, err;
void userspace(struct uml_pt_regs *regs)
{ int err, status, op;
siginfo_t si_ptrace;
siginfo_t *si; int sig;
/* Handle any immediate reschedules or signals */
interrupt_end();
while (1) { struct mm_id *mm_id = current_mm_id();
/* * When we are in time-travel mode, userspace can theoretically * do a *lot* of work without being scheduled. The problem with * this is that it will prevent kernel bookkeeping (primarily * the RCU) from running and this can for example cause OOM * situations. * * This code accounts a jiffie against the scheduling clock * after the defined userspace iterations in the same thread. * By doing so the situation is effectively prevented.
*/ if (time_travel_mode == TT_MODE_INFCPU ||
time_travel_mode == TT_MODE_EXTERNAL) { #ifdef CONFIG_UML_MAX_USERSPACE_ITERATIONS if (CONFIG_UML_MAX_USERSPACE_ITERATIONS &&
unscheduled_userspace_iterations++ >
CONFIG_UML_MAX_USERSPACE_ITERATIONS) {
tt_extra_sched_jiffies += 1;
unscheduled_userspace_iterations = 0;
} #endif
}
time_travel_print_bc_msg();
current_mm_sync();
if (using_seccomp) { struct stub_data *proc_data = (void *) mm_id->stack;
err = set_stub_state(regs, proc_data, singlestepping()); if (err) {
printk(UM_KERN_ERR "%s - failed to set regs: %d",
__func__, err);
fatal_sigsegv();
}
/* Must have been reset by the syscall caller */ if (proc_data->restart_wait != 0)
panic("Programming error: Flag to only run syscalls in child was not cleared!");
/* Mark pending syscalls for flushing */
proc_data->syscall_data_len = mm_id->syscall_data_len;
err = get_stub_state(regs, proc_data, NULL); if (err) {
printk(UM_KERN_ERR "%s - failed to get regs: %d",
__func__, err);
fatal_sigsegv();
}
if (proc_data->si_offset > sizeof(proc_data->sigstack) - sizeof(*si))
panic("%s - Invalid siginfo offset from child",
__func__);
si = (void *)&proc_data->sigstack[proc_data->si_offset];
regs->is_user = 1;
/* Fill in ORIG_RAX and extract fault information */
PT_SYSCALL_NR(regs->gp) = si->si_syscall; if (sig == SIGSEGV) {
mcontext_t *mcontext = (void *)&proc_data->sigstack[proc_data->mctx_offset];
GET_FAULTINFO_FROM_MC(regs->faultinfo, mcontext);
}
} else { int pid = mm_id->pid;
/* Flush out any pending syscalls */
err = syscall_stub_flush(mm_id); if (err) { if (err == -ENOMEM)
report_enomem();
/* * This can legitimately fail if the process loads a * bogus value into a segment register. It will * segfault and PTRACE_GETREGS will read that value * out of the process. However, PTRACE_SETREGS will * fail. In this case, there is nothing to do but * just kill the process.
*/ if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
printk(UM_KERN_ERR "%s - ptrace set regs failed, errno = %d\n",
__func__, errno);
fatal_sigsegv();
}
if (put_fp_registers(pid, regs->fp)) {
printk(UM_KERN_ERR "%s - ptrace set fp regs failed, errno = %d\n",
__func__, errno);
fatal_sigsegv();
}
if (singlestepping())
op = PTRACE_SYSEMU_SINGLESTEP; else
op = PTRACE_SYSEMU;
/* * These signal handlers need the si argument * and SIGSEGV needs the faultinfo. * The SIGIO and SIGALARM handlers which constitute * the majority of invocations, do not use it.
*/ switch (sig) { case SIGSEGV:
get_skas_faultinfo(pid,
®s->faultinfo);
fallthrough; case SIGTRAP: case SIGILL: case SIGBUS: case SIGFPE: case SIGWINCH:
ptrace(PTRACE_GETSIGINFO, pid, 0,
(struct siginfo *)&si_ptrace);
si = &si_ptrace; break; default:
si = NULL; break;
}
} else {
sig = 0;
}
}
UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
if (sig) { switch (sig) { case SIGSEGV: if (using_seccomp || PTRACE_FULL_FAULTINFO)
(*sig_info[SIGSEGV])(SIGSEGV,
(struct siginfo *)si,
regs, NULL); else
segv(regs->faultinfo, 0, 1, NULL, NULL);
break; case SIGSYS:
handle_syscall(regs); break; case SIGTRAP + 0x80:
handle_trap(regs); break; case SIGTRAP:
relay_signal(SIGTRAP, (struct siginfo *)si, regs, NULL); break; case SIGALRM: break; case SIGIO: case SIGILL: case SIGBUS: case SIGFPE: case SIGWINCH:
block_signals_trace();
(*sig_info[sig])(sig, (struct siginfo *)si, regs, NULL);
unblock_signals_trace(); break; default:
printk(UM_KERN_ERR "%s - child stopped with signal %d\n",
__func__, sig);
fatal_sigsegv();
}
interrupt_end();
/* Avoid -ERESTARTSYS handling in host */ if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
PT_SYSCALL_NR(regs->gp) = -1;
}
}
}
/* XXX Make these percpu */ staticvoid (*cb_proc)(void *arg); staticvoid *cb_arg; static jmp_buf *cb_back;
int start_idle_thread(void *stack, jmp_buf *switch_buf)
{ int n;
set_handler(SIGWINCH);
/* * Can't use UML_SETJMP or UML_LONGJMP here because they save * and restore signals, with the possible side-effect of * trying to handle any signals which came when they were * blocked, which can't be done on this stack. * Signals must be blocked when jumping back here and restored * after returning to the jumper.
*/
n = setjmp(initial_jmpbuf); switch (n) { case INIT_JMP_NEW_THREAD:
(*switch_buf)[0].JB_IP = (unsignedlong) uml_finishsetup;
(*switch_buf)[0].JB_SP = (unsignedlong) stack +
UM_THREAD_SIZE - sizeof(void *); break; case INIT_JMP_CALLBACK:
(*cb_proc)(cb_arg);
longjmp(*cb_back, 1); break; case INIT_JMP_HALT:
kmalloc_ok = 0; return 0; case INIT_JMP_REBOOT:
kmalloc_ok = 0; return 1; default:
printk(UM_KERN_ERR "Bad sigsetjmp return in %s - %d\n",
__func__, n);
fatal_sigsegv();
}
longjmp(*switch_buf, 1);
__uml_setup("noreboot", noreboot_cmd_param, "noreboot\n" " Rather than rebooting, exit always, akin to QEMU's -no-reboot option.\n" " This is useful if you're using CONFIG_PANIC_TIMEOUT in order to catch\n" " crashes in CI\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.