2011-09-23 12:00:45 +04:00
|
|
|
#include <unistd.h>
|
2013-01-09 18:48:00 +04:00
|
|
|
#include <inttypes.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
2012-05-29 20:11:00 +04:00
|
|
|
#include <sys/mman.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
#include "protobuf.h"
|
|
|
|
#include "protobuf/sa.pb-c.h"
|
2013-06-27 23:32:18 +04:00
|
|
|
#include "protobuf/timer.pb-c.h"
|
2012-10-11 15:59:43 +04:00
|
|
|
#include "protobuf/creds.pb-c.h"
|
2013-01-09 18:32:56 +03:00
|
|
|
#include "protobuf/core.pb-c.h"
|
2013-03-12 21:00:05 +04:00
|
|
|
#include "protobuf/pagemap.pb-c.h"
|
2012-10-11 15:59:43 +04:00
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
#include "syscall.h"
|
2011-12-19 21:57:59 +04:00
|
|
|
#include "ptrace.h"
|
2013-01-09 17:02:47 +04:00
|
|
|
#include "asm/processor-flags.h"
|
2011-09-23 12:00:45 +04:00
|
|
|
#include "parasite-syscall.h"
|
|
|
|
#include "parasite-blob.h"
|
|
|
|
#include "parasite.h"
|
2012-05-29 20:11:00 +04:00
|
|
|
#include "crtools.h"
|
2012-08-02 08:10:22 +04:00
|
|
|
#include "namespaces.h"
|
2013-05-24 01:42:18 +04:00
|
|
|
#include "kerndat.h"
|
2012-10-08 18:59:36 +04:00
|
|
|
#include "pstree.h"
|
2013-01-15 23:24:01 +04:00
|
|
|
#include "net.h"
|
2013-04-08 17:39:21 +04:00
|
|
|
#include "mem.h"
|
2013-05-24 01:42:18 +04:00
|
|
|
#include "vdso.h"
|
2013-05-24 16:20:21 +04:00
|
|
|
#include "restorer.h"
|
2013-06-27 23:32:22 +04:00
|
|
|
#include "proc_parse.h"
|
2012-05-29 20:11:00 +04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2013-01-09 17:26:31 +04:00
|
|
|
#include "asm/parasite-syscall.h"
|
2013-01-09 18:32:56 +03:00
|
|
|
#include "asm/dump.h"
|
2013-05-24 16:20:21 +04:00
|
|
|
#include "asm/restorer.h"
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2013-05-24 16:20:21 +04:00
|
|
|
#define parasite_size (round_up(sizeof(parasite_blob), PAGE_SIZE))
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
static int can_run_syscall(unsigned long ip, unsigned long start, unsigned long end)
|
|
|
|
{
|
|
|
|
return ip >= start && ip < (end - code_syscall_size);
|
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
static int syscall_fits_vma_area(struct vma_area *vma_area)
|
|
|
|
{
|
|
|
|
return can_run_syscall((unsigned long)vma_area->vma.start,
|
|
|
|
(unsigned long)vma_area->vma.start,
|
|
|
|
(unsigned long)vma_area->vma.end);
|
|
|
|
}
|
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
static struct vma_area *get_vma_by_ip(struct list_head *vma_area_list, unsigned long ip)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2012-02-15 18:00:50 +04:00
|
|
|
struct vma_area *vma_area;
|
|
|
|
|
|
|
|
list_for_each_entry(vma_area, vma_area_list, list) {
|
2013-01-18 11:08:36 +04:00
|
|
|
if (vma_area->vma.start >= TASK_SIZE)
|
2012-02-15 18:00:50 +04:00
|
|
|
continue;
|
|
|
|
if (!(vma_area->vma.prot & PROT_EXEC))
|
|
|
|
continue;
|
|
|
|
if (syscall_fits_vma_area(vma_area))
|
|
|
|
return vma_area;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
/* we run at @regs->ip */
|
2013-07-11 13:46:53 +04:00
|
|
|
static int __parasite_execute_trap(struct parasite_ctl *ctl, pid_t pid,
|
2013-05-24 16:20:13 +04:00
|
|
|
user_regs_struct_t *regs,
|
2013-05-24 16:20:15 +04:00
|
|
|
user_regs_struct_t *regs_orig,
|
2013-07-11 13:46:51 +04:00
|
|
|
k_rtsigset_t *sigmask)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2013-07-11 13:46:51 +04:00
|
|
|
k_rtsigset_t blockall;
|
2011-09-23 12:00:45 +04:00
|
|
|
siginfo_t siginfo;
|
|
|
|
int status;
|
|
|
|
int ret = -1;
|
|
|
|
|
2013-07-11 13:46:51 +04:00
|
|
|
ksigfillset(&blockall);
|
|
|
|
if (ptrace(PTRACE_SETSIGMASK, pid, sizeof(k_rtsigset_t), &blockall)) {
|
|
|
|
pr_perror("Can't block signals");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
if (ptrace(PTRACE_SETREGS, pid, NULL, regs)) {
|
2013-02-26 17:08:47 +04:00
|
|
|
pr_perror("Can't set registers (pid: %d)", pid);
|
2013-07-11 13:46:52 +04:00
|
|
|
goto err_sigmask;
|
2012-02-15 18:00:50 +04:00
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Most ideas are taken from Tejun Heo's parasite thread
|
|
|
|
* https://code.google.com/p/ptrace-parasite/
|
|
|
|
*/
|
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
if (ptrace(PTRACE_CONT, pid, NULL, NULL)) {
|
2013-02-26 17:08:47 +04:00
|
|
|
pr_perror("Can't continue (pid: %d)", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait4(pid, &status, __WALL, NULL) != pid) {
|
2013-02-26 17:08:47 +04:00
|
|
|
pr_perror("Waited pid mismatch (pid: %d)", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFSTOPPED(status)) {
|
|
|
|
pr_err("Task is still running (pid: %d)\n", pid);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo)) {
|
2013-02-26 17:08:47 +04:00
|
|
|
pr_perror("Can't get siginfo (pid: %d)", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, NULL, regs)) {
|
2013-02-26 17:08:47 +04:00
|
|
|
pr_perror("Can't obtain registers (pid: %d)", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2013-01-14 11:25:45 +04:00
|
|
|
if (WSTOPSIG(status) != SIGTRAP || siginfo.si_code != ARCH_SI_TRAP) {
|
2012-02-15 18:00:50 +04:00
|
|
|
pr_debug("** delivering signal %d si_code=%d\n",
|
|
|
|
siginfo.si_signo, siginfo.si_code);
|
|
|
|
|
2013-07-11 13:46:51 +04:00
|
|
|
pr_err("Unexpected %d task interruption, aborting\n", pid);
|
|
|
|
goto err;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-04-12 13:00:06 -07:00
|
|
|
* We've reached this point if int3 is triggered inside our
|
2012-10-29 13:40:45 +04:00
|
|
|
* parasite code. So we're done.
|
2011-09-23 12:00:45 +04:00
|
|
|
*/
|
2012-02-15 18:00:50 +04:00
|
|
|
ret = 0;
|
2013-07-11 13:46:52 +04:00
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
err:
|
2013-07-11 13:46:52 +04:00
|
|
|
if (ptrace(PTRACE_SETREGS, pid, NULL, regs_orig)) {
|
|
|
|
pr_perror("Can't restore registers (pid: %d)", pid);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
err_sigmask:
|
2013-07-11 13:46:51 +04:00
|
|
|
if (ptrace(PTRACE_SETSIGMASK, pid, sizeof(k_rtsigset_t), &sigmask)) {
|
|
|
|
pr_perror("Can't block signals");
|
|
|
|
ret = -1;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:46:53 +04:00
|
|
|
int __parasite_execute_syscall(struct parasite_ctl *ctl, user_regs_struct_t *regs)
|
|
|
|
{
|
|
|
|
pid_t pid = ctl->pid.real;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inject syscall instruction and remember original code,
|
|
|
|
* we will need it to restore original program content.
|
|
|
|
*/
|
|
|
|
memcpy(ctl->code_orig, code_syscall, sizeof(ctl->code_orig));
|
|
|
|
if (ptrace_swap_area(pid, (void *)ctl->syscall_ip,
|
|
|
|
(void *)ctl->code_orig, sizeof(ctl->code_orig))) {
|
|
|
|
pr_err("Can't inject syscall blob (pid: %d)\n", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
parasite_setup_regs(ctl->syscall_ip, 0, regs);
|
|
|
|
err = __parasite_execute_trap(ctl, pid, regs, &ctl->regs_orig,
|
|
|
|
&ctl->sig_blocked);
|
|
|
|
|
|
|
|
if (ptrace_poke_area(pid, (void *)ctl->code_orig,
|
|
|
|
(void *)ctl->syscall_ip, sizeof(ctl->code_orig))) {
|
|
|
|
pr_err("Can't restore syscall blob (pid: %d)\n", ctl->pid.real);
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-04-08 17:33:56 +04:00
|
|
|
void *parasite_args_s(struct parasite_ctl *ctl, int args_size)
|
2012-10-11 15:59:43 +04:00
|
|
|
{
|
2013-03-01 20:11:28 +04:00
|
|
|
BUG_ON(args_size > ctl->args_size);
|
2012-10-11 15:59:43 +04:00
|
|
|
return ctl->addr_args;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:49 +04:00
|
|
|
static int parasite_execute_trap_by_pid(unsigned int cmd,
|
|
|
|
struct parasite_ctl *ctl, pid_t pid,
|
|
|
|
user_regs_struct_t *regs_orig,
|
2013-07-11 13:46:51 +04:00
|
|
|
void *stack,
|
|
|
|
k_rtsigset_t *sigmask)
|
2012-02-12 00:32:32 +04:00
|
|
|
{
|
2013-05-27 16:38:49 +04:00
|
|
|
user_regs_struct_t regs = *regs_orig;
|
2012-02-12 00:32:32 +04:00
|
|
|
int ret;
|
|
|
|
|
2012-10-11 17:59:10 +04:00
|
|
|
*ctl->addr_cmd = cmd;
|
2012-02-12 00:32:32 +04:00
|
|
|
|
2013-05-27 16:38:49 +04:00
|
|
|
parasite_setup_regs(ctl->parasite_ip, stack, ®s);
|
2012-02-12 00:32:32 +04:00
|
|
|
|
2013-07-11 13:46:51 +04:00
|
|
|
ret = __parasite_execute_trap(ctl, pid, ®s, regs_orig, sigmask);
|
2012-06-26 20:00:00 +04:00
|
|
|
if (ret == 0)
|
2013-01-14 11:25:46 +04:00
|
|
|
ret = (int)REG_RES(regs);
|
2012-02-12 00:32:32 +04:00
|
|
|
|
2012-02-16 21:54:49 +04:00
|
|
|
if (ret)
|
2012-06-26 20:01:00 +04:00
|
|
|
pr_err("Parasite exited with %d\n", ret);
|
2012-02-16 21:54:49 +04:00
|
|
|
|
2012-02-12 00:32:32 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
static int parasite_execute_trap(unsigned int cmd, struct parasite_ctl *ctl)
|
2012-02-21 09:25:16 +03:00
|
|
|
{
|
2013-07-11 13:46:51 +04:00
|
|
|
return parasite_execute_trap_by_pid(cmd, ctl, ctl->pid.real,
|
|
|
|
&ctl->regs_orig, ctl->rstack,
|
|
|
|
&ctl->sig_blocked);
|
2012-02-21 09:25:16 +03:00
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
static int __parasite_send_cmd(int sockfd, struct ctl_msg *m)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = send(sockfd, m, sizeof(*m), 0);
|
|
|
|
if (ret == -1) {
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_perror("Failed to send command %d to daemon\n", m->cmd);
|
2013-05-24 16:20:12 +04:00
|
|
|
return -1;
|
|
|
|
} else if (ret != sizeof(*m)) {
|
|
|
|
pr_err("Message to daemon is trimmed (%d/%d)\n",
|
|
|
|
(int)sizeof(*m), ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_debug("Sent msg to daemon %d %d %d\n", m->cmd, m->ack, m->err);
|
2013-05-24 16:20:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
static int parasite_wait_ack(int sockfd, unsigned int cmd, struct ctl_msg *m)
|
2013-05-24 16:20:12 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_debug("Wait for ack %d on daemon socket\n", cmd);
|
2013-05-24 16:20:12 +04:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
memzero(m, sizeof(*m));
|
|
|
|
|
|
|
|
ret = recv(sockfd, m, sizeof(*m), MSG_WAITALL);
|
|
|
|
if (ret == -1) {
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_perror("Failed to read ack");
|
2013-05-24 16:20:12 +04:00
|
|
|
return -1;
|
|
|
|
} else if (ret != sizeof(*m)) {
|
|
|
|
pr_err("Message reply from daemon is trimmed (%d/%d)\n",
|
|
|
|
(int)sizeof(*m), ret);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_debug("Fetched ack: %d %d %d\n",
|
|
|
|
m->cmd, m->ack, m->err);
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
if (m->cmd != cmd || m->ack != cmd) {
|
2013-05-24 16:20:12 +04:00
|
|
|
pr_err("Communication error, this is not "
|
|
|
|
"the ack we expected\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __parasite_execute_daemon_wait_ack(unsigned int cmd,
|
2013-05-27 16:38:51 +04:00
|
|
|
struct parasite_ctl *ctl)
|
2013-05-24 16:20:12 +04:00
|
|
|
{
|
|
|
|
struct ctl_msg m;
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
if (parasite_wait_ack(ctl->tsock, cmd, &m))
|
2013-05-24 16:20:12 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m.err != 0) {
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_err("Command %d for daemon failed with %d\n",
|
|
|
|
cmd, m.err);
|
2013-05-24 16:20:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
int __parasite_execute_daemon(unsigned int cmd,
|
|
|
|
struct parasite_ctl *ctl, bool wait_ack)
|
2013-05-24 16:20:12 +04:00
|
|
|
{
|
|
|
|
struct ctl_msg m;
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
m = ctl_msg_cmd(cmd);
|
2013-05-24 16:20:12 +04:00
|
|
|
if (__parasite_send_cmd(ctl->tsock, &m))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (wait_ack)
|
2013-05-27 16:38:51 +04:00
|
|
|
return __parasite_execute_daemon_wait_ack(cmd, ctl);
|
2013-05-24 16:20:12 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
int parasite_execute_daemon(unsigned int cmd,
|
|
|
|
struct parasite_ctl *ctl)
|
2013-05-24 16:20:12 +04:00
|
|
|
{
|
2013-05-27 16:38:51 +04:00
|
|
|
return __parasite_execute_daemon(cmd, ctl, true);
|
2013-05-24 16:20:12 +04:00
|
|
|
}
|
|
|
|
|
2012-12-17 22:52:06 +03:00
|
|
|
static int munmap_seized(struct parasite_ctl *ctl, void *addr, size_t length)
|
|
|
|
{
|
|
|
|
unsigned long x;
|
2011-11-29 13:33:59 +03:00
|
|
|
|
2012-12-17 22:52:06 +03:00
|
|
|
return syscall_seized(ctl, __NR_munmap, &x,
|
|
|
|
(unsigned long)addr, length, 0, 0, 0, 0);
|
2011-11-29 13:33:59 +03:00
|
|
|
}
|
|
|
|
|
2012-03-21 11:47:00 +04:00
|
|
|
static int gen_parasite_saddr(struct sockaddr_un *saddr, int key)
|
2012-02-01 16:23:50 +03:00
|
|
|
{
|
|
|
|
int sun_len;
|
|
|
|
|
|
|
|
saddr->sun_family = AF_UNIX;
|
|
|
|
snprintf(saddr->sun_path, UNIX_PATH_MAX,
|
2012-03-21 11:47:00 +04:00
|
|
|
"X/crtools-pr-%d", key);
|
2012-02-01 16:23:50 +03:00
|
|
|
|
|
|
|
sun_len = SUN_LEN(saddr);
|
|
|
|
*saddr->sun_path = '\0';
|
|
|
|
|
|
|
|
return sun_len;
|
|
|
|
}
|
|
|
|
|
2013-04-08 17:33:56 +04:00
|
|
|
int parasite_send_fd(struct parasite_ctl *ctl, int fd)
|
2012-02-01 16:23:50 +03:00
|
|
|
{
|
2012-07-14 15:36:00 +04:00
|
|
|
if (send_fd(ctl->tsock, NULL, 0, fd) < 0) {
|
2012-02-01 16:23:50 +03:00
|
|
|
pr_perror("Can't send file descriptor");
|
2012-07-14 15:36:00 +04:00
|
|
|
return -1;
|
2012-02-01 16:23:50 +03:00
|
|
|
}
|
2012-07-14 15:36:00 +04:00
|
|
|
return 0;
|
2012-02-01 16:23:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parasite_set_logfd(struct parasite_ctl *ctl, pid_t pid)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-10-11 15:59:43 +04:00
|
|
|
struct parasite_log_args *a;
|
2012-02-01 16:23:50 +03:00
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
ret = parasite_send_fd(ctl, log_get_fd());
|
2012-02-01 16:23:50 +03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
a = parasite_args(ctl, struct parasite_log_args);
|
2012-10-11 15:59:43 +04:00
|
|
|
a->log_level = log_get_loglevel();
|
2012-10-08 19:56:12 +04:00
|
|
|
|
2013-05-24 16:20:05 +04:00
|
|
|
ret = parasite_execute_trap(PARASITE_CMD_CFG_LOG, ctl);
|
2012-02-01 16:23:50 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:46:50 +04:00
|
|
|
static int ssock = -1;
|
2012-10-11 15:59:43 +04:00
|
|
|
|
2013-07-11 13:46:50 +04:00
|
|
|
static int prepare_tsock(struct parasite_ctl *ctl, pid_t pid,
|
|
|
|
struct parasite_init_args *args)
|
|
|
|
{
|
2012-08-01 10:17:14 +04:00
|
|
|
pr_info("Putting tsock into pid %d\n", pid);
|
2012-12-20 16:07:44 +04:00
|
|
|
args->h_addr_len = gen_parasite_saddr(&args->h_addr, getpid());
|
2012-07-14 15:36:00 +04:00
|
|
|
|
2013-05-30 17:59:17 +04:00
|
|
|
if (ssock == -1) {
|
2012-08-02 08:10:22 +04:00
|
|
|
int rst = -1;
|
|
|
|
|
2013-01-17 18:14:55 +04:00
|
|
|
if (current_ns_mask & CLONE_NEWNET) {
|
2012-08-02 08:10:22 +04:00
|
|
|
pr_info("Switching to %d's net for tsock creation\n", pid);
|
|
|
|
|
2013-01-15 23:24:01 +04:00
|
|
|
if (switch_ns(pid, &net_ns_desc, &rst))
|
2012-08-02 08:10:22 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-30 17:59:17 +04:00
|
|
|
ssock = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (ssock < 0)
|
2012-08-01 10:17:14 +04:00
|
|
|
pr_perror("Can't create socket");
|
2013-04-11 19:57:46 +04:00
|
|
|
|
|
|
|
if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
|
|
|
|
return -1;
|
2013-05-30 17:59:17 +04:00
|
|
|
if (ssock < 0)
|
2012-08-01 10:17:14 +04:00
|
|
|
return -1;
|
2012-07-14 15:36:00 +04:00
|
|
|
|
2013-05-30 17:59:17 +04:00
|
|
|
if (bind(ssock, (struct sockaddr *)&args->h_addr, args->h_addr_len) < 0) {
|
2012-08-01 10:17:14 +04:00
|
|
|
pr_perror("Can't bind socket");
|
|
|
|
goto err;
|
|
|
|
}
|
2012-08-02 08:10:22 +04:00
|
|
|
|
2013-05-30 17:59:17 +04:00
|
|
|
if (listen(ssock, 1)) {
|
|
|
|
pr_perror("Can't listen on transport socket");
|
2012-08-01 10:17:14 +04:00
|
|
|
goto err;
|
|
|
|
}
|
2012-07-14 15:36:00 +04:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:46:50 +04:00
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
close_safe(&ssock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int accept_tsock()
|
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = accept(ssock, NULL, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
pr_perror("Can't accept connection to the transport socket");
|
|
|
|
close_safe(&ssock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parasite_init(struct parasite_ctl *ctl, pid_t pid, struct pstree_item *item)
|
|
|
|
{
|
|
|
|
struct parasite_init_args *args;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
args = parasite_args(ctl, struct parasite_init_args);
|
|
|
|
|
|
|
|
args->sigframe = ctl->rsigframe;
|
|
|
|
|
|
|
|
if (prepare_tsock(ctl, pid, args))
|
|
|
|
goto err;
|
|
|
|
|
2013-05-24 16:20:05 +04:00
|
|
|
if (parasite_execute_trap(PARASITE_CMD_INIT, ctl) < 0) {
|
2012-08-01 09:48:02 +04:00
|
|
|
pr_err("Can't init parasite\n");
|
2012-07-14 15:36:00 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:46:50 +04:00
|
|
|
sock = accept_tsock();
|
|
|
|
if (sock < 0)
|
2012-07-14 15:36:00 +04:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
ctl->tsock = sock;
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
static int parasite_daemonize(struct parasite_ctl *ctl)
|
2013-05-24 16:20:12 +04:00
|
|
|
{
|
2013-05-27 16:38:52 +04:00
|
|
|
pid_t pid = ctl->pid.real;
|
2013-05-24 16:20:12 +04:00
|
|
|
user_regs_struct_t regs;
|
|
|
|
struct ctl_msg m = { };
|
2013-07-11 13:46:51 +04:00
|
|
|
k_rtsigset_t blockall;
|
|
|
|
|
|
|
|
ksigfillset(&blockall);
|
2013-05-24 16:20:12 +04:00
|
|
|
|
|
|
|
*ctl->addr_cmd = PARASITE_CMD_DAEMONIZE;
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
regs = ctl->regs_orig;
|
|
|
|
parasite_setup_regs(ctl->parasite_ip, ctl->rstack, ®s);
|
2013-05-24 16:20:12 +04:00
|
|
|
|
|
|
|
if (ptrace(PTRACE_SETREGS, pid, NULL, ®s)) {
|
|
|
|
pr_perror("Can't set registers (pid: %d)", pid);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:46:51 +04:00
|
|
|
if (ptrace(PTRACE_SETSIGMASK, pid, sizeof(k_rtsigset_t), &blockall)) {
|
|
|
|
pr_perror("Can't block signals");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
if (ptrace(PTRACE_CONT, pid, NULL, NULL)) {
|
|
|
|
pr_perror("Can't continue (pid: %d)\n", pid);
|
2013-05-27 16:38:52 +04:00
|
|
|
ptrace(PTRACE_SETREGS, pid, NULL, ctl->regs_orig);
|
2013-05-24 16:20:12 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("Wait for parasite being daemonized...\n");
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
if (parasite_wait_ack(ctl->tsock, PARASITE_CMD_DAEMONIZE, &m)) {
|
2013-05-24 16:20:12 +04:00
|
|
|
pr_err("Can't switch parasite %d to daemon mode %d\n",
|
|
|
|
pid, m.err);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
ctl->daemonized = true;
|
2013-05-24 16:20:12 +04:00
|
|
|
pr_info("Parasite %d has been switched to daemon mode\n", pid);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2013-07-11 13:46:51 +04:00
|
|
|
if (ptrace(PTRACE_SETSIGMASK, pid, sizeof(k_rtsigset_t), ctl->sig_blocked))
|
|
|
|
pr_perror("Can't block signals");
|
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
int parasite_dump_thread_seized(struct parasite_ctl *ctl, int id,
|
|
|
|
struct pid *tid, CoreEntry *core)
|
2012-02-21 09:25:17 +03:00
|
|
|
{
|
2012-11-12 17:42:53 +04:00
|
|
|
struct parasite_dump_thread *args;
|
2013-07-10 17:42:03 +04:00
|
|
|
pid_t pid = tid->real;
|
|
|
|
user_regs_struct_t regs_orig;
|
2012-02-21 09:25:17 +03:00
|
|
|
int ret;
|
|
|
|
|
2013-07-10 17:42:03 +04:00
|
|
|
BUG_ON(id == 0); /* Leader is dumped in dump_task_core_all */
|
2012-10-11 15:59:43 +04:00
|
|
|
|
2013-07-10 17:42:03 +04:00
|
|
|
args = parasite_args(ctl, struct parasite_dump_thread);
|
2013-05-27 16:38:50 +04:00
|
|
|
|
2013-07-11 13:46:51 +04:00
|
|
|
ret = ptrace(PTRACE_GETSIGMASK, pid, sizeof(k_rtsigset_t),
|
|
|
|
&core->thread_core->blk_sigset);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("ptrace can't get signal blocking mask for %d", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
core->thread_core->has_blk_sigset = true;
|
|
|
|
|
2013-07-10 17:42:03 +04:00
|
|
|
ret = ptrace(PTRACE_GETREGS, pid, NULL, ®s_orig);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("Can't obtain registers (pid: %d)", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-27 16:38:50 +04:00
|
|
|
|
2013-07-10 17:42:03 +04:00
|
|
|
ret = parasite_execute_trap_by_pid(PARASITE_CMD_INIT_THREAD, ctl,
|
|
|
|
pid, ®s_orig,
|
2013-07-11 13:46:51 +04:00
|
|
|
ctl->r_thread_stack,
|
|
|
|
(k_rtsigset_t *) &core->thread_core->blk_sigset);
|
2013-07-10 17:42:03 +04:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Can't init thread in parasite %d\n", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-27 16:38:50 +04:00
|
|
|
|
2013-07-10 17:42:03 +04:00
|
|
|
ret = get_task_regs(pid, regs_orig, core);
|
|
|
|
if (ret)
|
|
|
|
pr_err("Can't obtain regs for thread %d\n", pid);
|
2013-05-27 16:38:50 +04:00
|
|
|
|
2013-07-10 17:42:03 +04:00
|
|
|
if (parasite_execute_trap_by_pid(PARASITE_CMD_FINI_THREAD, ctl,
|
|
|
|
pid, ®s_orig,
|
2013-07-11 13:46:51 +04:00
|
|
|
ctl->r_thread_stack,
|
|
|
|
(k_rtsigset_t *) &core->thread_core->blk_sigset)) {
|
2013-07-10 17:42:03 +04:00
|
|
|
pr_err("Can't init thread in parasite %d\n", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
return -1;
|
2013-05-27 16:38:50 +04:00
|
|
|
|
2013-07-10 17:42:03 +04:00
|
|
|
BUG_ON(!core->thread_core->sas);
|
|
|
|
copy_sas(core->thread_core->sas, &args->sas);
|
2012-02-21 09:25:17 +03:00
|
|
|
|
2013-01-18 11:08:38 +04:00
|
|
|
CORE_THREAD_ARCH_INFO(core)->clear_tid_addr = encode_pointer(args->tid_addr);
|
2013-01-09 18:27:54 +03:00
|
|
|
tid->virt = args->tid;
|
2013-01-09 18:32:56 +03:00
|
|
|
core_put_tls(core, args->tls);
|
2012-02-21 09:25:17 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-24 16:45:19 +04:00
|
|
|
int parasite_dump_sigacts_seized(struct parasite_ctl *ctl, struct cr_fdset *cr_fdset)
|
|
|
|
{
|
2012-10-11 15:59:43 +04:00
|
|
|
struct parasite_dump_sa_args *args;
|
2012-12-04 19:26:54 +04:00
|
|
|
int ret, sig, fd;
|
2012-07-18 16:25:06 +04:00
|
|
|
SaEntry se = SA_ENTRY__INIT;
|
2012-07-15 10:18:35 +04:00
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
args = parasite_args(ctl, struct parasite_dump_sa_args);
|
2012-10-11 15:59:43 +04:00
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
ret = parasite_execute_daemon(PARASITE_CMD_DUMP_SIGACTS, ctl);
|
2012-07-15 10:18:35 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
fd = fdset_fd(cr_fdset, CR_FD_SIGACT);
|
|
|
|
|
2012-12-04 19:26:54 +04:00
|
|
|
for (sig = 1; sig <= SIGMAX; sig++) {
|
|
|
|
int i = sig - 1;
|
|
|
|
|
|
|
|
if (sig == SIGSTOP || sig == SIGKILL)
|
2012-07-15 10:18:35 +04:00
|
|
|
continue;
|
|
|
|
|
2013-01-18 11:08:38 +04:00
|
|
|
ASSIGN_TYPED(se.sigaction, encode_pointer(args->sas[i].rt_sa_handler));
|
2012-10-11 15:59:43 +04:00
|
|
|
ASSIGN_TYPED(se.flags, args->sas[i].rt_sa_flags);
|
2013-01-18 11:08:38 +04:00
|
|
|
ASSIGN_TYPED(se.restorer, encode_pointer(args->sas[i].rt_sa_restorer));
|
2012-10-11 15:59:43 +04:00
|
|
|
ASSIGN_TYPED(se.mask, args->sas[i].rt_sa_mask.sig[0]);
|
2012-07-15 10:18:35 +04:00
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
if (pb_write_one(fd, &se, PB_SIGACT) < 0)
|
2012-07-15 10:18:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-01-24 16:45:19 +04:00
|
|
|
}
|
|
|
|
|
2012-07-18 07:23:05 +04:00
|
|
|
static int dump_one_timer(struct itimerval *v, int fd)
|
|
|
|
{
|
2012-07-18 16:27:01 +04:00
|
|
|
ItimerEntry ie = ITIMER_ENTRY__INIT;
|
2012-07-18 07:23:05 +04:00
|
|
|
|
|
|
|
ie.isec = v->it_interval.tv_sec;
|
|
|
|
ie.iusec = v->it_interval.tv_usec;
|
|
|
|
ie.vsec = v->it_value.tv_sec;
|
2013-05-06 23:12:00 +04:00
|
|
|
ie.vusec = v->it_value.tv_usec;
|
2012-07-18 07:23:05 +04:00
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
return pb_write_one(fd, &ie, PB_ITIMERS);
|
2012-07-18 07:23:05 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 16:45:19 +04:00
|
|
|
int parasite_dump_itimers_seized(struct parasite_ctl *ctl, struct cr_fdset *cr_fdset)
|
|
|
|
{
|
2012-10-11 15:59:43 +04:00
|
|
|
struct parasite_dump_itimers_args *args;
|
2012-07-18 07:23:05 +04:00
|
|
|
int ret, fd;
|
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
args = parasite_args(ctl, struct parasite_dump_itimers_args);
|
2012-10-11 15:59:43 +04:00
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
ret = parasite_execute_daemon(PARASITE_CMD_DUMP_ITIMERS, ctl);
|
2012-07-18 07:23:05 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
fd = fdset_fd(cr_fdset, CR_FD_ITIMERS);
|
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
ret = dump_one_timer(&args->real, fd);
|
2012-07-18 07:23:05 +04:00
|
|
|
if (!ret)
|
2012-10-11 15:59:43 +04:00
|
|
|
ret = dump_one_timer(&args->virt, fd);
|
2012-07-18 07:23:05 +04:00
|
|
|
if (!ret)
|
2012-10-11 15:59:43 +04:00
|
|
|
ret = dump_one_timer(&args->prof, fd);
|
2012-07-18 07:23:05 +04:00
|
|
|
|
|
|
|
return ret;
|
2012-01-24 16:45:19 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 23:32:22 +04:00
|
|
|
static int dump_one_posix_timer(struct posix_timer *v, struct proc_posix_timer *vp, int fd)
|
|
|
|
{
|
|
|
|
PosixTimerEntry pte = POSIX_TIMER_ENTRY__INIT;
|
|
|
|
|
|
|
|
pte.it_id = vp->spt.it_id;
|
|
|
|
pte.clock_id = vp->spt.clock_id;
|
|
|
|
pte.si_signo = vp->spt.si_signo;
|
|
|
|
pte.it_sigev_notify = vp->spt.it_sigev_notify;
|
|
|
|
pte.sival_ptr = encode_pointer(vp->spt.sival_ptr);
|
|
|
|
|
|
|
|
pte.overrun = v->overrun;
|
|
|
|
|
|
|
|
pte.isec = v->val.it_interval.tv_sec;
|
|
|
|
pte.insec = v->val.it_interval.tv_nsec;
|
|
|
|
pte.vsec = v->val.it_value.tv_sec;
|
|
|
|
pte.vnsec = v->val.it_value.tv_nsec;
|
|
|
|
|
|
|
|
return pb_write_one(fd, &pte, PB_POSIX_TIMERS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int parasite_dump_posix_timers_seized(struct proc_posix_timers_stat *proc_args, struct parasite_ctl *ctl, struct cr_fdset *cr_fdset)
|
|
|
|
{
|
|
|
|
struct parasite_dump_posix_timers_args * args;
|
|
|
|
struct proc_posix_timer *temp;
|
|
|
|
int i, fd;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
args = parasite_args_s(ctl, posix_timers_dump_size(proc_args->timer_n));
|
|
|
|
args->timer_n = proc_args->timer_n;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
list_for_each_entry(temp, &proc_args->timers, list) {
|
|
|
|
args->timer[i].it_id = temp->spt.it_id;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = parasite_execute_daemon(PARASITE_CMD_DUMP_POSIX_TIMERS, ctl);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end_posix;
|
|
|
|
|
|
|
|
fd = fdset_fd(cr_fdset, CR_FD_POSIX_TIMERS);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
list_for_each_entry(temp, &proc_args->timers, list) {
|
|
|
|
ret = dump_one_posix_timer(&args->timer[i], temp, fd);
|
|
|
|
i++;
|
|
|
|
if (ret)
|
|
|
|
goto end_posix;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_posix:
|
|
|
|
while (!list_empty(&proc_args->timers)) {
|
|
|
|
temp = list_first_entry(&proc_args->timers, struct proc_posix_timer, list);
|
|
|
|
list_del(&temp->list);
|
|
|
|
xfree(temp);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-27 21:35:59 +04:00
|
|
|
int parasite_dump_misc_seized(struct parasite_ctl *ctl, struct parasite_dump_misc *misc)
|
|
|
|
{
|
2012-10-11 15:59:43 +04:00
|
|
|
struct parasite_dump_misc *ma;
|
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
ma = parasite_args(ctl, struct parasite_dump_misc);
|
2013-05-24 16:20:12 +04:00
|
|
|
if (parasite_execute_daemon(PARASITE_CMD_DUMP_MISC, ctl) < 0)
|
2012-10-11 15:59:43 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
*misc = *ma;
|
|
|
|
return 0;
|
2012-01-27 21:35:59 +04:00
|
|
|
}
|
|
|
|
|
2012-10-15 23:55:34 +04:00
|
|
|
struct parasite_tty_args *parasite_dump_tty(struct parasite_ctl *ctl, int fd)
|
2012-10-15 23:42:49 +04:00
|
|
|
{
|
2012-10-15 23:55:34 +04:00
|
|
|
struct parasite_tty_args *p;
|
2012-10-15 23:42:49 +04:00
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
p = parasite_args(ctl, struct parasite_tty_args);
|
2012-10-15 23:55:34 +04:00
|
|
|
p->fd = fd;
|
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
if (parasite_execute_daemon(PARASITE_CMD_DUMP_TTY, ctl) < 0)
|
2012-10-15 23:55:34 +04:00
|
|
|
return NULL;
|
2012-10-15 23:42:49 +04:00
|
|
|
|
2012-10-15 23:55:34 +04:00
|
|
|
return p;
|
2012-10-15 23:42:49 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
int parasite_dump_creds(struct parasite_ctl *ctl, CredsEntry *ce)
|
|
|
|
{
|
|
|
|
struct parasite_dump_creds *pc;
|
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
pc = parasite_args(ctl, struct parasite_dump_creds);
|
2013-05-24 16:20:12 +04:00
|
|
|
if (parasite_execute_daemon(PARASITE_CMD_DUMP_CREDS, ctl) < 0)
|
2012-10-11 15:59:43 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
ce->secbits = pc->secbits;
|
2012-10-11 16:52:52 +04:00
|
|
|
ce->n_groups = pc->ngroups;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Achtung! We leak the parasite args pointer to the caller.
|
|
|
|
* It's not safe in general, but in our case is OK, since the
|
|
|
|
* latter doesn't go to parasite before using the data in it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BUILD_BUG_ON(sizeof(ce->groups[0]) != sizeof(pc->groups[0]));
|
|
|
|
ce->groups = pc->groups;
|
2012-10-11 15:59:43 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-21 19:59:07 +04:00
|
|
|
int parasite_drain_fds_seized(struct parasite_ctl *ctl,
|
2012-09-05 16:41:14 +04:00
|
|
|
struct parasite_drain_fd *dfds, int *lfds, struct fd_opts *opts)
|
2012-03-28 17:36:00 +04:00
|
|
|
{
|
2012-10-11 15:59:43 +04:00
|
|
|
int ret = -1, size;
|
|
|
|
struct parasite_drain_fd *args;
|
|
|
|
|
|
|
|
size = drain_fds_size(dfds);
|
2012-10-30 20:58:03 +03:00
|
|
|
args = parasite_args_s(ctl, size);
|
2012-10-11 15:59:43 +04:00
|
|
|
memcpy(args, dfds, size);
|
2012-03-28 17:36:00 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
ret = __parasite_execute_daemon(PARASITE_CMD_DRAIN_FDS, ctl, false);
|
2012-03-28 17:36:00 +04:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Parasite failed to drain descriptors\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-09-05 16:41:14 +04:00
|
|
|
ret = recv_fds(ctl->tsock, lfds, dfds->nr_fds, opts);
|
2013-05-24 16:20:12 +04:00
|
|
|
if (ret)
|
2012-03-28 17:36:00 +04:00
|
|
|
pr_err("Can't retrieve FDs from socket\n");
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
ret |= __parasite_execute_daemon_wait_ack(PARASITE_CMD_DRAIN_FDS, ctl);
|
2012-03-28 17:36:00 +04:00
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-07 19:16:33 +04:00
|
|
|
int parasite_get_proc_fd_seized(struct parasite_ctl *ctl)
|
|
|
|
{
|
|
|
|
int ret = -1, fd;
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
ret = __parasite_execute_daemon(PARASITE_CMD_GET_PROC_FD, ctl, false);
|
2012-09-07 19:16:33 +04:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Parasite failed to get proc fd\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = recv_fd(ctl->tsock);
|
2013-05-24 16:20:12 +04:00
|
|
|
if (fd < 0)
|
2012-09-07 19:16:33 +04:00
|
|
|
pr_err("Can't retrieve FD from socket\n");
|
2013-05-27 16:38:51 +04:00
|
|
|
if (__parasite_execute_daemon_wait_ack(PARASITE_CMD_GET_PROC_FD, ctl)) {
|
2013-05-24 16:20:12 +04:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
2012-09-07 19:16:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:04:58 +04:00
|
|
|
|
|
|
|
static bool task_in_parasite(struct parasite_ctl *ctl, user_regs_struct_t *regs)
|
|
|
|
{
|
|
|
|
void *addr = (void *) REG_IP(*regs);
|
|
|
|
return addr >= ctl->remote_map &&
|
|
|
|
addr < ctl->remote_map + ctl->map_length;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:23 +04:00
|
|
|
static int parasite_fini_seized(struct parasite_ctl *ctl)
|
2012-11-12 17:42:51 +04:00
|
|
|
{
|
2013-05-27 16:38:51 +04:00
|
|
|
pid_t pid = ctl->pid.real;
|
2013-06-14 15:04:58 +04:00
|
|
|
user_regs_struct_t regs;
|
|
|
|
int status, ret = 0;
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
if (!ctl->daemonized)
|
|
|
|
return 0;
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
/* Start to trace syscalls for each thread */
|
2013-06-14 15:04:56 +04:00
|
|
|
if (ptrace(PTRACE_INTERRUPT, pid, NULL, NULL)) {
|
|
|
|
pr_perror("Unable to interrupt the process");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_debug("Waiting for %d to trap\n", pid);
|
|
|
|
if (wait4(pid, &status, __WALL, NULL) != pid) {
|
|
|
|
pr_perror("Waited pid mismatch (pid: %d)", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_debug("Daemon %d exited trapping\n", pid);
|
|
|
|
if (!WIFSTOPPED(status)) {
|
|
|
|
pr_err("Task is still running (pid: %d)\n", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-24 16:20:23 +04:00
|
|
|
|
2013-06-14 15:04:58 +04:00
|
|
|
ret = ptrace(PTRACE_GETREGS, pid, NULL, ®s);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("Unable to get registers");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!task_in_parasite(ctl, ®s)) {
|
|
|
|
pr_err("The task is not in parasite code\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("ptrace");
|
|
|
|
return -1;
|
2012-11-12 17:42:51 +04:00
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
ret = __parasite_execute_daemon(PARASITE_CMD_FINI, ctl, false);
|
2013-06-14 15:04:57 +04:00
|
|
|
close_safe(&ctl->tsock);
|
2013-05-24 16:20:23 +04:00
|
|
|
if (ret)
|
|
|
|
return -1;
|
2012-11-12 17:42:51 +04:00
|
|
|
|
2013-05-24 16:20:23 +04:00
|
|
|
/* Stop all threads on the enter point in sys_rt_sigreturn */
|
|
|
|
while (1) {
|
2013-06-14 15:04:59 +04:00
|
|
|
if (wait4(pid, &status, __WALL, NULL) < 0) {
|
2013-05-24 16:20:23 +04:00
|
|
|
pr_perror("wait4 failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2013-06-22 13:14:42 +04:00
|
|
|
if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGTRAP) {
|
|
|
|
pr_err("Task is in unexpected state: %x\n", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:23 +04:00
|
|
|
pr_debug("%d was trapped\n", pid);
|
|
|
|
if (!WIFSTOPPED(status)) {
|
|
|
|
pr_err("%d\n", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = ptrace(PTRACE_GETREGS, pid, NULL, ®s);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("ptrace");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-31 09:20:47 +04:00
|
|
|
pr_debug("%d is going to execute the syscall %lx\n", pid, REG_SYSCALL_NR(regs));
|
|
|
|
if (REG_SYSCALL_NR(regs) == __NR_rt_sigreturn) {
|
2013-05-24 16:20:23 +04:00
|
|
|
pr_debug("%d was stopped\n", pid);
|
2013-05-27 16:38:51 +04:00
|
|
|
break;
|
2013-05-24 16:20:23 +04:00
|
|
|
}
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2013-05-24 16:20:23 +04:00
|
|
|
ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("ptrace");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-24 16:20:12 +04:00
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("ptrace");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-24 16:20:23 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
if (wait4(pid, &status, __WALL, NULL) != pid) {
|
|
|
|
pr_perror("wait4 failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-24 16:20:23 +04:00
|
|
|
|
2013-05-27 16:38:51 +04:00
|
|
|
pr_debug("Trap %d\n", pid);
|
|
|
|
if (!WIFSTOPPED(status)) {
|
|
|
|
pr_err("%d\n", status);
|
|
|
|
return -1;
|
2013-05-24 16:20:12 +04:00
|
|
|
}
|
2013-05-24 16:20:08 +04:00
|
|
|
|
2013-05-24 16:20:12 +04:00
|
|
|
return ret;
|
2013-05-24 16:20:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int parasite_cure_remote(struct parasite_ctl *ctl)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2012-02-15 18:00:50 +04:00
|
|
|
int ret = 0;
|
2012-02-01 16:23:50 +03:00
|
|
|
|
2013-05-24 16:20:23 +04:00
|
|
|
if (ctl->parasite_ip)
|
|
|
|
if (parasite_fini_seized(ctl))
|
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2013-06-14 15:04:57 +04:00
|
|
|
close_safe(&ctl->tsock);
|
2013-05-24 16:20:12 +04:00
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
if (ctl->remote_map) {
|
|
|
|
if (munmap_seized(ctl, (void *)ctl->remote_map, ctl->map_length)) {
|
2013-04-08 17:56:48 +04:00
|
|
|
pr_err("munmap_seized failed (pid: %d)\n", ctl->pid.real);
|
2012-02-15 18:00:50 +04:00
|
|
|
ret = -1;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2013-05-14 11:48:51 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parasite_cure_local(struct parasite_ctl *ctl)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (ctl->local_map) {
|
|
|
|
if (munmap(ctl->local_map, ctl->map_length)) {
|
|
|
|
pr_err("munmap failed (pid: %d)\n", ctl->pid.real);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 13:32:43 +04:00
|
|
|
free(ctl);
|
2011-09-23 12:00:45 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
int parasite_cure_seized(struct parasite_ctl *ctl)
|
2013-05-14 11:48:51 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
ret = parasite_cure_remote(ctl);
|
2013-05-14 11:48:51 +04:00
|
|
|
if (!ret)
|
|
|
|
ret = parasite_cure_local(ctl);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
struct parasite_ctl *parasite_prep_ctl(pid_t pid, struct vm_area_list *vma_area_list)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
struct parasite_ctl *ctl = NULL;
|
|
|
|
struct vma_area *vma_area;
|
|
|
|
|
2013-03-27 15:43:27 +04:00
|
|
|
if (!arch_can_dump_task(pid))
|
2013-02-14 20:26:29 +04:00
|
|
|
goto err;
|
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
/*
|
|
|
|
* Control block early setup.
|
|
|
|
*/
|
2013-05-27 16:38:52 +04:00
|
|
|
ctl = xzalloc(sizeof(*ctl));
|
2011-09-23 12:00:45 +04:00
|
|
|
if (!ctl) {
|
2011-09-30 14:37:12 +04:00
|
|
|
pr_err("Parasite control block allocation failed (pid: %d)\n", pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-07-14 15:36:00 +04:00
|
|
|
ctl->tsock = -1;
|
|
|
|
|
2013-07-11 13:46:51 +04:00
|
|
|
if (ptrace(PTRACE_GETSIGMASK, pid, sizeof(k_rtsigset_t), &ctl->sig_blocked)) {
|
|
|
|
pr_perror("ptrace doesn't support PTRACE_GETSIGMASK\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, NULL, &ctl->regs_orig)) {
|
2012-02-15 18:00:50 +04:00
|
|
|
pr_err("Can't obtain registers (pid: %d)\n", pid);
|
|
|
|
goto err;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
vma_area = get_vma_by_ip(&vma_area_list->h, REG_IP(ctl->regs_orig));
|
2011-09-23 12:00:45 +04:00
|
|
|
if (!vma_area) {
|
2011-09-30 14:37:12 +04:00
|
|
|
pr_err("No suitable VMA found to run parasite "
|
2012-02-15 18:00:50 +04:00
|
|
|
"bootstrap code (pid: %d)\n", pid);
|
2012-02-13 23:17:51 +04:00
|
|
|
goto err;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2013-04-08 17:56:48 +04:00
|
|
|
ctl->pid.real = pid;
|
|
|
|
ctl->pid.virt = 0;
|
2012-02-15 18:00:50 +04:00
|
|
|
ctl->syscall_ip = vma_area->vma.start;
|
|
|
|
|
2012-12-17 22:53:23 +03:00
|
|
|
return ctl;
|
|
|
|
|
|
|
|
err:
|
|
|
|
xfree(ctl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parasite_map_exchange(struct parasite_ctl *ctl, unsigned long size)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2012-12-18 20:48:56 +03:00
|
|
|
ctl->remote_map = mmap_seized(ctl, NULL, size,
|
2012-02-13 23:17:51 +04:00
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
|
|
|
|
if (!ctl->remote_map) {
|
2013-04-08 17:56:48 +04:00
|
|
|
pr_err("Can't allocate memory for parasite blob (pid: %d)\n", ctl->pid.real);
|
2012-12-17 22:53:23 +03:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-12-18 20:48:56 +03:00
|
|
|
ctl->map_length = round_up(size, PAGE_SIZE);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2013-04-08 17:56:48 +04:00
|
|
|
fd = open_proc_rw(ctl->pid.real, "map_files/%p-%p",
|
2012-02-15 18:00:50 +04:00
|
|
|
ctl->remote_map, ctl->remote_map + ctl->map_length);
|
2012-02-17 01:39:36 +04:00
|
|
|
if (fd < 0)
|
2012-12-17 22:53:23 +03:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-12-18 20:48:56 +03:00
|
|
|
ctl->local_map = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
2012-02-13 23:17:51 +04:00
|
|
|
MAP_SHARED | MAP_FILE, fd, 0);
|
2012-02-12 14:50:34 +04:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ctl->local_map == MAP_FAILED) {
|
2012-02-15 18:00:50 +04:00
|
|
|
ctl->local_map = NULL;
|
2012-02-12 14:50:34 +04:00
|
|
|
pr_perror("Can't map remote parasite map");
|
2012-12-17 22:53:23 +03:00
|
|
|
return -1;
|
2012-02-12 14:50:34 +04:00
|
|
|
}
|
|
|
|
|
2012-12-17 22:53:23 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-27 23:32:22 +04:00
|
|
|
static unsigned long parasite_args_size(struct vm_area_list *vmas, struct parasite_drain_fd *dfds, int timer_n)
|
2013-03-01 20:11:28 +04:00
|
|
|
{
|
2013-03-01 20:11:38 +04:00
|
|
|
unsigned long size = PARASITE_ARG_SIZE_MIN;
|
|
|
|
|
2013-05-14 12:00:40 +04:00
|
|
|
if (dfds)
|
|
|
|
size = max(size, (unsigned long)drain_fds_size(dfds));
|
2013-06-27 23:32:22 +04:00
|
|
|
if (timer_n)
|
|
|
|
size = max(size, (unsigned long)posix_timers_dump_size(timer_n));
|
2013-05-14 11:02:18 +04:00
|
|
|
size = max(size, (unsigned long)dump_pages_args_size(vmas));
|
2013-03-01 20:11:38 +04:00
|
|
|
|
2013-05-24 16:20:21 +04:00
|
|
|
return round_up(size, PAGE_SIZE);
|
2013-03-01 20:11:28 +04:00
|
|
|
}
|
|
|
|
|
2013-03-01 20:11:38 +04:00
|
|
|
struct parasite_ctl *parasite_infect_seized(pid_t pid, struct pstree_item *item,
|
2013-06-27 23:32:22 +04:00
|
|
|
struct vm_area_list *vma_area_list, struct parasite_drain_fd *dfds,
|
|
|
|
int timer_n)
|
2012-12-17 22:53:23 +03:00
|
|
|
{
|
2013-05-27 16:38:52 +04:00
|
|
|
int ret;
|
2012-12-17 22:53:23 +03:00
|
|
|
struct parasite_ctl *ctl;
|
2013-05-27 16:38:52 +04:00
|
|
|
unsigned long p, map_exchange_size;
|
2012-12-17 22:53:23 +03:00
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
BUG_ON(item->threads[0].real != pid);
|
|
|
|
|
2013-05-24 16:20:14 +04:00
|
|
|
if (pstree_alloc_cores(item))
|
|
|
|
return NULL;
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
ctl = parasite_prep_ctl(pid, vma_area_list);
|
2012-12-17 22:53:23 +03:00
|
|
|
if (!ctl)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inject a parasite engine. Ie allocate memory inside alien
|
|
|
|
* space and copy engine code there. Then re-map the engine
|
|
|
|
* locally, so we will get an easy way to access engine memory
|
|
|
|
* without using ptrace at all.
|
|
|
|
*/
|
|
|
|
|
2013-06-27 23:32:22 +04:00
|
|
|
ctl->args_size = parasite_args_size(vma_area_list, dfds, timer_n);
|
2013-05-27 16:38:52 +04:00
|
|
|
map_exchange_size = parasite_size + ctl->args_size;
|
|
|
|
map_exchange_size += RESTORE_STACK_SIGFRAME + PARASITE_STACK_SIZE;
|
|
|
|
if (item->nr_threads > 1)
|
|
|
|
map_exchange_size += PARASITE_STACK_SIZE;
|
2013-07-11 13:46:51 +04:00
|
|
|
|
|
|
|
memcpy(&item->core[0]->tc->blk_sigset, &ctl->sig_blocked, sizeof(k_rtsigset_t));
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
ret = parasite_map_exchange(ctl, map_exchange_size);
|
2012-12-17 22:53:23 +03:00
|
|
|
if (ret)
|
|
|
|
goto err_restore;
|
|
|
|
|
2012-02-12 14:50:34 +04:00
|
|
|
pr_info("Putting parasite blob into %p->%p\n", ctl->local_map, ctl->remote_map);
|
2012-02-13 21:49:18 +04:00
|
|
|
memcpy(ctl->local_map, parasite_blob, sizeof(parasite_blob));
|
2012-02-12 14:50:34 +04:00
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
/* Setup the rest of a control block */
|
2012-11-13 20:51:32 +03:00
|
|
|
ctl->parasite_ip = (unsigned long)parasite_sym(ctl->remote_map, __export_parasite_head_start);
|
|
|
|
ctl->addr_cmd = parasite_sym(ctl->local_map, __export_parasite_cmd);
|
|
|
|
ctl->addr_args = parasite_sym(ctl->local_map, __export_parasite_args);
|
2012-02-12 14:51:38 +04:00
|
|
|
|
2013-05-24 16:20:10 +04:00
|
|
|
p = parasite_size + ctl->args_size;
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
ctl->rsigframe = ctl->remote_map + p;
|
|
|
|
ctl->sigframe = ctl->local_map + p;
|
|
|
|
|
|
|
|
p += RESTORE_STACK_SIGFRAME;
|
|
|
|
|
|
|
|
ctl->rstack = ctl->remote_map + p;
|
|
|
|
p += PARASITE_STACK_SIZE;
|
2013-05-24 16:20:21 +04:00
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
if (item->nr_threads > 1) {
|
|
|
|
ctl->r_thread_stack = ctl->remote_map + p;
|
|
|
|
p += PARASITE_STACK_SIZE;
|
2013-05-24 16:20:10 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 15:36:39 +04:00
|
|
|
ret = parasite_init(ctl, pid, item);
|
2012-02-01 16:23:50 +03:00
|
|
|
if (ret) {
|
|
|
|
pr_err("%d: Can't create a transport socket\n", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err_restore;
|
2012-02-01 16:23:50 +03:00
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
ret = get_task_regs(pid, ctl->regs_orig, item->core[0]);
|
2013-05-24 16:20:14 +04:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Can't obtain regs for thread %d\n", pid);
|
|
|
|
goto err_restore;
|
|
|
|
}
|
|
|
|
|
2012-02-01 16:23:50 +03:00
|
|
|
ret = parasite_set_logfd(ctl, pid);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("%d: Can't set a logging descriptor\n", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err_restore;
|
2012-02-01 16:23:50 +03:00
|
|
|
}
|
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
if (construct_sigframe(ctl->sigframe, ctl->rsigframe, item->core[0]))
|
|
|
|
goto err_restore;
|
2013-05-24 16:20:21 +04:00
|
|
|
|
2013-05-27 16:38:52 +04:00
|
|
|
if (parasite_daemonize(ctl))
|
|
|
|
goto err_restore;
|
2013-05-24 16:20:15 +04:00
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
return ctl;
|
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
err_restore:
|
2013-05-24 16:20:08 +04:00
|
|
|
parasite_cure_seized(ctl);
|
2012-04-06 17:58:00 +04:00
|
|
|
return NULL;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|