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"
|
|
|
|
#include "protobuf/itimer.pb-c.h"
|
|
|
|
#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"
|
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"
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
#define parasite_size (round_up(sizeof(parasite_blob), sizeof(long)))
|
|
|
|
|
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-01-09 17:26:46 +04:00
|
|
|
int __parasite_execute(struct parasite_ctl *ctl, pid_t pid, user_regs_struct_t *regs)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
siginfo_t siginfo;
|
|
|
|
int status;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
again:
|
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);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err;
|
|
|
|
}
|
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) {
|
2011-09-23 12:00:45 +04:00
|
|
|
retry_signal:
|
2012-02-15 18:00:50 +04:00
|
|
|
pr_debug("** delivering signal %d si_code=%d\n",
|
|
|
|
siginfo.si_signo, siginfo.si_code);
|
|
|
|
|
2012-03-01 19:01:05 +04:00
|
|
|
if (ctl->signals_blocked) {
|
|
|
|
pr_err("Unexpected %d task interruption, aborting\n", pid);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-02-13 23:06:18 +04:00
|
|
|
/* FIXME: jerr(siginfo.si_code > 0, err_restore); */
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
/*
|
|
|
|
* This requires some explanation. If a signal from original
|
|
|
|
* program delivered while we're trying to execute our
|
|
|
|
* injected blob -- we need to setup original registers back
|
|
|
|
* so the kernel would make sigframe for us and update the
|
|
|
|
* former registers.
|
|
|
|
*
|
|
|
|
* Then we should swap registers back to our modified copy
|
|
|
|
* and retry.
|
|
|
|
*/
|
|
|
|
|
2013-05-24 16:20:09 +04:00
|
|
|
if (ptrace(PTRACE_SETREGS, pid, NULL, &ctl->threads[0].regs_orig)) {
|
2013-02-26 17:08:47 +04:00
|
|
|
pr_perror("Can't set registers (pid: %d)", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptrace(PTRACE_INTERRUPT, pid, NULL, NULL)) {
|
2013-02-26 17:08:47 +04:00
|
|
|
pr_perror("Can't interrupt (pid: %d)", pid);
|
2012-02-15 18:00:50 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptrace(PTRACE_CONT, pid, NULL, (void *)(unsigned long)siginfo.si_signo)) {
|
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-04-25 21:34:05 +04:00
|
|
|
if (SI_EVENT(siginfo.si_code) != PTRACE_EVENT_STOP)
|
2011-09-23 12:00:45 +04:00
|
|
|
goto retry_signal;
|
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
/*
|
|
|
|
* Signal is delivered, so we should update
|
|
|
|
* original registers.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
user_regs_struct_t r;
|
|
|
|
if (ptrace(PTRACE_GETREGS, pid, NULL, &r)) {
|
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;
|
|
|
|
}
|
2013-05-24 16:20:09 +04:00
|
|
|
ctl->threads[0].regs_orig = r;
|
2012-02-15 18:00:50 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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;
|
2011-09-23 12:00:45 +04:00
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
#define parasite_args(ctl, type) ({ \
|
2013-03-01 20:11:28 +04:00
|
|
|
BUILD_BUG_ON(sizeof(type) > PARASITE_ARG_SIZE_MIN);\
|
2012-10-30 20:58:03 +03:00
|
|
|
ctl->addr_args; \
|
|
|
|
})
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
static int parasite_execute_by_id(unsigned int cmd, struct parasite_ctl *ctl, int id)
|
2012-02-12 00:32:32 +04:00
|
|
|
{
|
2013-05-24 16:20:08 +04:00
|
|
|
struct parasite_thread_ctl *thread = &ctl->threads[id];
|
2013-05-24 16:20:09 +04:00
|
|
|
user_regs_struct_t regs = thread->regs_orig;
|
2013-05-24 16:20:08 +04:00
|
|
|
pid_t pid = thread->tid;
|
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
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
parasite_setup_regs(ctl->parasite_ip, ®s);
|
2012-02-12 00:32:32 +04:00
|
|
|
|
2012-02-21 09:25:16 +03:00
|
|
|
ret = __parasite_execute(ctl, pid, ®s);
|
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
|
|
|
|
2013-04-08 17:56:48 +04:00
|
|
|
if (ctl->pid.real != pid)
|
2013-05-24 16:20:09 +04:00
|
|
|
if (ptrace(PTRACE_SETREGS, pid, NULL, &thread->regs_orig)) {
|
2013-04-08 17:52:32 +04:00
|
|
|
pr_perror("Can't restore registers (pid: %d)", pid);
|
2012-02-21 09:25:16 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-12 00:32:32 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-08 17:33:56 +04:00
|
|
|
int parasite_execute(unsigned int cmd, struct parasite_ctl *ctl)
|
2012-02-21 09:25:16 +03:00
|
|
|
{
|
2013-05-24 16:20:08 +04:00
|
|
|
return parasite_execute_by_id(cmd, ctl, 0);
|
2012-02-21 09:25:16 +03: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
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
ret = parasite_execute(PARASITE_CMD_CFG_LOG, ctl);
|
2012-02-01 16:23:50 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-12 17:42:48 +04:00
|
|
|
static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
|
2012-07-14 15:36:00 +04:00
|
|
|
{
|
2012-10-11 15:59:43 +04:00
|
|
|
struct parasite_init_args *args;
|
2012-08-01 10:17:14 +04:00
|
|
|
static int sock = -1;
|
2012-07-14 15:36:00 +04:00
|
|
|
|
2012-10-30 20:58:03 +03:00
|
|
|
args = parasite_args(ctl, struct parasite_init_args);
|
2012-10-11 15:59:43 +04:00
|
|
|
|
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-10-11 15:59:43 +04:00
|
|
|
args->p_addr_len = gen_parasite_saddr(&args->p_addr, pid);
|
2012-11-12 17:42:48 +04:00
|
|
|
args->nr_threads = nr_threads;
|
2013-05-24 16:20:08 +04:00
|
|
|
args->id = 0;
|
2012-07-14 15:36:00 +04:00
|
|
|
|
2012-08-01 10:17:14 +04:00
|
|
|
if (sock == -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;
|
|
|
|
}
|
|
|
|
|
2012-08-01 10:17:14 +04:00
|
|
|
sock = socket(PF_UNIX, SOCK_DGRAM, 0);
|
2013-04-11 19:57:46 +04:00
|
|
|
if (sock < 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;
|
|
|
|
if (sock < 0)
|
2012-08-01 10:17:14 +04:00
|
|
|
return -1;
|
2012-07-14 15:36:00 +04:00
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
if (bind(sock, (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
|
|
|
|
2012-08-01 10:17:14 +04:00
|
|
|
} else {
|
|
|
|
struct sockaddr addr = { .sa_family = AF_UNSPEC, };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When the peer of a dgram socket dies the original socket
|
|
|
|
* remains in connected state, thus denying any connections
|
|
|
|
* from "other" sources. Unconnect the socket by hands thus
|
|
|
|
* allowing for parasite to connect back.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (connect(sock, &addr, sizeof(addr)) < 0) {
|
|
|
|
pr_perror("Can't unconnect");
|
|
|
|
goto err;
|
|
|
|
}
|
2012-07-14 15:36:00 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
if (parasite_execute(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;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
if (connect(sock, (struct sockaddr *)&args->p_addr, args->p_addr_len) < 0) {
|
2012-08-01 09:48:02 +04:00
|
|
|
pr_perror("Can't connect a transport socket");
|
2012-07-14 15:36:00 +04:00
|
|
|
goto err;
|
2012-08-01 09:48:02 +04:00
|
|
|
}
|
2012-07-14 15:36:00 +04:00
|
|
|
|
|
|
|
ctl->tsock = sock;
|
|
|
|
return 0;
|
|
|
|
err:
|
2013-04-11 19:57:46 +04:00
|
|
|
close_safe(&sock);
|
2012-07-14 15:36:00 +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;
|
2012-02-21 09:25:17 +03:00
|
|
|
int ret;
|
|
|
|
|
2012-11-12 17:42:53 +04:00
|
|
|
args = parasite_args(ctl, struct parasite_dump_thread);
|
2013-05-24 16:20:08 +04:00
|
|
|
args->id = id;
|
2012-10-11 15:59:43 +04:00
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
ret = parasite_execute_by_id(PARASITE_CMD_DUMP_THREAD, ctl, id);
|
2012-02-21 09:25:17 +03:00
|
|
|
|
2013-01-09 18:32:56 +03:00
|
|
|
memcpy(&core->thread_core->blk_sigset, &args->blocked, sizeof(args->blocked));
|
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
|
|
|
|
|
|
|
ret = parasite_execute(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
|
|
|
|
|
|
|
ret = parasite_execute(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
|
|
|
}
|
|
|
|
|
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);
|
2012-10-11 15:59:43 +04:00
|
|
|
if (parasite_execute(PARASITE_CMD_DUMP_MISC, ctl) < 0)
|
|
|
|
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;
|
|
|
|
|
2012-10-15 23:42:49 +04:00
|
|
|
if (parasite_execute(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);
|
2012-10-11 15:59:43 +04:00
|
|
|
if (parasite_execute(PARASITE_CMD_DUMP_CREDS, ctl) < 0)
|
|
|
|
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
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
ret = parasite_execute(PARASITE_CMD_DRAIN_FDS, ctl);
|
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);
|
2012-03-28 17:36:00 +04:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Can't retrieve FDs from socket\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:42:18 +04:00
|
|
|
/*
|
|
|
|
* Find out proxy vdso vma and drop it from the list. Also
|
|
|
|
* fix vdso status on vmas if wrong status found.
|
|
|
|
*/
|
|
|
|
int parasite_fixup_vdso(struct parasite_ctl *ctl, pid_t pid,
|
|
|
|
struct vm_area_list *vma_area_list)
|
|
|
|
{
|
|
|
|
unsigned long proxy_addr = VDSO_BAD_ADDR;
|
|
|
|
struct parasite_vdso_vma_entry *args;
|
|
|
|
struct vma_area *marked = NULL;
|
|
|
|
struct vma_area *vma;
|
|
|
|
int fd, ret = -1;
|
|
|
|
off_t off;
|
|
|
|
u64 pfn;
|
|
|
|
|
|
|
|
args = parasite_args(ctl, struct parasite_vdso_vma_entry);
|
|
|
|
fd = open_proc(pid, "pagemap");
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
list_for_each_entry(vma, &vma_area_list->h, list) {
|
|
|
|
if (!vma_area_is(vma, VMA_AREA_REGULAR))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((vma->vma.prot & VDSO_PROT) != VDSO_PROT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I need to poke every potentially marked vma,
|
|
|
|
* otherwise if task never called for vdso functions
|
|
|
|
* page frame number won't be reported.
|
|
|
|
*/
|
|
|
|
args->start = vma->vma.start;
|
|
|
|
args->len = vma_area_len(vma);
|
|
|
|
|
|
|
|
if (parasite_execute(PARASITE_CMD_CHECK_VDSO_MARK, ctl)) {
|
|
|
|
pr_err("vdso: Parasite failed to poke for mark\n");
|
|
|
|
ret = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:40:49 +04:00
|
|
|
/*
|
|
|
|
* Defer handling marked vdso.
|
|
|
|
*/
|
|
|
|
if (unlikely(args->is_marked)) {
|
|
|
|
BUG_ON(args->proxy_addr == VDSO_BAD_ADDR);
|
|
|
|
BUG_ON(marked);
|
|
|
|
marked = vma;
|
|
|
|
proxy_addr = args->proxy_addr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:42:18 +04:00
|
|
|
off = (vma->vma.start / PAGE_SIZE) * sizeof(u64);
|
|
|
|
if (lseek(fd, off, SEEK_SET) != off) {
|
|
|
|
pr_perror("Failed to seek address %lx\n", vma->vma.start);
|
|
|
|
ret = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read(fd, &pfn, sizeof(pfn));
|
|
|
|
if (ret < 0 || ret != sizeof(pfn)) {
|
|
|
|
pr_perror("Can't read pme for pid %d", pid);
|
|
|
|
ret = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfn = PME_PFRAME(pfn);
|
|
|
|
BUG_ON(!pfn);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set proper VMA statuses.
|
|
|
|
*/
|
|
|
|
if (pfn == vdso_pfn) {
|
|
|
|
if (!vma_area_is(vma, VMA_AREA_VDSO)) {
|
|
|
|
pr_debug("vdso: Restore status by pfn at %lx\n",
|
|
|
|
(long)vma->vma.start);
|
|
|
|
vma->vma.status |= VMA_AREA_VDSO;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (vma_area_is(vma, VMA_AREA_VDSO)) {
|
|
|
|
pr_debug("vdso: Drop mishinted status at %lx\n",
|
|
|
|
(long)vma->vma.start);
|
|
|
|
vma->vma.status &= ~VMA_AREA_VDSO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is marked vdso, it means such vdso is autogenerated
|
|
|
|
* and must be dropped from vma list.
|
|
|
|
*/
|
|
|
|
if (marked) {
|
|
|
|
pr_debug("vdso: Found marked at %lx (proxy at %lx)\n",
|
|
|
|
(long)marked->vma.start, (long)proxy_addr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't forget to restore the proxy vdso status, since
|
|
|
|
* it's being not recognized by the kernel as vdso.
|
|
|
|
*/
|
|
|
|
list_for_each_entry(vma, &vma_area_list->h, list) {
|
|
|
|
if (vma->vma.start == proxy_addr) {
|
|
|
|
vma->vma.status |= VMA_AREA_REGULAR | VMA_AREA_VDSO;
|
|
|
|
pr_debug("vdso: Restore proxy status at %lx\n",
|
|
|
|
(long)vma->vma.start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_debug("vdso: Droppping marked vdso at %lx\n",
|
|
|
|
(long)vma->vma.start);
|
|
|
|
list_del(&marked->list);
|
|
|
|
xfree(marked);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
err:
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-07 19:16:33 +04:00
|
|
|
int parasite_get_proc_fd_seized(struct parasite_ctl *ctl)
|
|
|
|
{
|
|
|
|
int ret = -1, fd;
|
|
|
|
|
2012-10-11 15:59:43 +04:00
|
|
|
ret = parasite_execute(PARASITE_CMD_GET_PROC_FD, ctl);
|
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);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_err("Can't retrieve FD from socket\n");
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-11-12 17:42:51 +04:00
|
|
|
int parasite_init_threads_seized(struct parasite_ctl *ctl, struct pstree_item *item)
|
|
|
|
{
|
2013-05-24 16:20:08 +04:00
|
|
|
struct parasite_init_args *args;
|
2012-11-12 17:42:51 +04:00
|
|
|
int ret = 0, i;
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
args = parasite_args(ctl, struct parasite_init_args);
|
|
|
|
|
|
|
|
for (i = 1; i < item->nr_threads; i++) {
|
|
|
|
pid_t tid = item->threads[i].real;
|
2013-05-24 16:20:09 +04:00
|
|
|
user_regs_struct_t *regs_orig = &ctl->threads[i].regs_orig;
|
2013-05-24 16:20:08 +04:00
|
|
|
|
|
|
|
ctl->threads[i].tid = tid;
|
|
|
|
ctl->nr_threads++;
|
|
|
|
|
|
|
|
args->id = i;
|
2012-11-12 17:42:51 +04:00
|
|
|
|
2013-05-24 16:20:09 +04:00
|
|
|
ret = ptrace(PTRACE_GETREGS, tid, NULL, regs_orig);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("Can't obtain registers (pid: %d)", tid);
|
|
|
|
break;
|
|
|
|
}
|
2013-05-24 16:20:08 +04:00
|
|
|
|
|
|
|
ret = parasite_execute_by_id(PARASITE_CMD_INIT_THREAD, ctl, i);
|
2012-11-12 17:42:51 +04:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Can't init thread in parasite %d\n",
|
|
|
|
item->threads[i].real);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
int parasite_fini_threads_seized(struct parasite_ctl *ctl)
|
2012-11-12 17:42:51 +04:00
|
|
|
{
|
2013-05-24 16:20:08 +04:00
|
|
|
struct parasite_init_args *args;
|
2012-11-12 17:42:51 +04:00
|
|
|
int ret = 0, i;
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
args = parasite_args(ctl, struct parasite_init_args);
|
|
|
|
|
|
|
|
for (i = 1; i < ctl->nr_threads; i++) {
|
|
|
|
pid_t tid = ctl->threads[i].tid;
|
2012-11-12 17:42:51 +04:00
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
args->id = i;
|
|
|
|
ret = parasite_execute_by_id(PARASITE_CMD_FINI_THREAD, ctl, i);
|
2012-11-12 17:42:51 +04:00
|
|
|
/*
|
|
|
|
* Note the thread's fini() can be called even when not
|
|
|
|
* all threads were init()'ed, say we're rolling back from
|
|
|
|
* error happened while we were init()'ing some thread, thus
|
|
|
|
* -ENOENT will be returned but we should continie for the
|
|
|
|
* rest of threads set.
|
|
|
|
*
|
|
|
|
* Strictly speaking we always init() threads in sequence thus
|
|
|
|
* we could simply break the loop once first -ENOENT returned
|
|
|
|
* but I prefer to be on a safe side even if some future changes
|
|
|
|
* would change the code logic.
|
|
|
|
*/
|
|
|
|
if (ret && ret != -ENOENT) {
|
2013-05-24 16:20:08 +04:00
|
|
|
pr_err("Can't fini thread in parasite %d\n", tid);
|
2012-11-12 17:42:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
static int parasite_fini_seized(struct parasite_ctl *ctl)
|
|
|
|
{
|
|
|
|
struct parasite_init_args *args;
|
|
|
|
|
|
|
|
args = parasite_args(ctl, struct parasite_init_args);
|
|
|
|
args->id = 0;
|
|
|
|
|
|
|
|
return parasite_execute(PARASITE_CMD_FINI, ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-08-01 10:17:14 +04:00
|
|
|
ctl->tsock = -1;
|
2012-07-14 15:36:00 +04:00
|
|
|
|
2012-02-15 18:00:50 +04:00
|
|
|
if (ctl->parasite_ip) {
|
2012-03-01 19:01:05 +04:00
|
|
|
ctl->signals_blocked = 0;
|
2013-05-24 16:20:08 +04:00
|
|
|
parasite_fini_threads_seized(ctl);
|
|
|
|
parasite_fini_seized(ctl);
|
2012-02-01 16:23:50 +03:00
|
|
|
}
|
2011-09-23 12:00:45 +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-04-08 17:56:48 +04:00
|
|
|
if (ptrace_poke_area(ctl->pid.real, (void *)ctl->code_orig,
|
2012-02-15 18:00:50 +04:00
|
|
|
(void *)ctl->syscall_ip, sizeof(ctl->code_orig))) {
|
2013-04-08 17:56:48 +04:00
|
|
|
pr_err("Can't restore syscall blob (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-24 16:20:09 +04:00
|
|
|
if (ptrace(PTRACE_SETREGS, ctl->pid.real, NULL, &ctl->threads[0].regs_orig)) {
|
2013-04-08 17:56:48 +04:00
|
|
|
pr_err("Can't restore registers (pid: %d)\n", ctl->pid.real);
|
2012-02-08 14:11:54 +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-24 16:20:08 +04:00
|
|
|
struct parasite_ctl *parasite_prep_ctl(pid_t pid, struct vm_area_list *vma_area_list, unsigned int nr_threads)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
struct parasite_ctl *ctl = NULL;
|
|
|
|
struct vma_area *vma_area;
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
BUG_ON(nr_threads == 0);
|
|
|
|
|
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-24 16:20:08 +04:00
|
|
|
ctl = xzalloc(sizeof(*ctl) + nr_threads * sizeof(ctl->threads[0]));
|
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-05-24 16:20:08 +04:00
|
|
|
ctl->nr_threads = 1;
|
|
|
|
ctl->threads[0].tid = pid;
|
2012-07-14 15:36:00 +04:00
|
|
|
|
2013-05-24 16:20:09 +04:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, NULL, &ctl->threads[0].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-24 16:20:09 +04:00
|
|
|
vma_area = get_vma_by_ip(&vma_area_list->h, REG_IP(ctl->threads[0].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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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));
|
2013-04-08 17:56:48 +04:00
|
|
|
if (ptrace_swap_area(pid, (void *)ctl->syscall_ip,
|
2012-02-15 18:00:50 +04:00
|
|
|
(void *)ctl->code_orig, sizeof(ctl->code_orig))) {
|
|
|
|
pr_err("Can't inject syscall blob (pid: %d)\n", pid);
|
|
|
|
goto err;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
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-03-01 20:12:46 +04:00
|
|
|
static unsigned long parasite_args_size(struct vm_area_list *vmas, struct parasite_drain_fd *dfds)
|
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-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
|
|
|
|
|
|
|
return 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-03-01 20:11:51 +04:00
|
|
|
struct vm_area_list *vma_area_list, struct parasite_drain_fd *dfds)
|
2012-12-17 22:53:23 +03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct parasite_ctl *ctl;
|
|
|
|
|
2013-05-24 16:20:08 +04:00
|
|
|
BUG_ON(item->threads[0].real != pid);
|
|
|
|
|
|
|
|
ctl = parasite_prep_ctl(pid, vma_area_list, item->nr_threads);
|
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-03-01 20:12:46 +04:00
|
|
|
ctl->args_size = parasite_args_size(vma_area_list, dfds);
|
2013-03-01 20:11:28 +04:00
|
|
|
ret = parasite_map_exchange(ctl, parasite_size + ctl->args_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
|
|
|
|
2012-11-12 17:42:48 +04:00
|
|
|
ret = parasite_init(ctl, pid, item->nr_threads);
|
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
|
|
|
}
|
|
|
|
|
2012-03-01 19:01:05 +04:00
|
|
|
ctl->signals_blocked = 1;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-12 17:42:55 +04:00
|
|
|
ret = parasite_init_threads_seized(ctl, item);
|
|
|
|
if (ret)
|
|
|
|
goto err_restore;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|