Branch data Line data Source code
1 : : #include <unistd.h>
2 : : #include <inttypes.h>
3 : :
4 : : #include <sys/stat.h>
5 : : #include <sys/wait.h>
6 : : #include <sys/mman.h>
7 : :
8 : : #include "protobuf.h"
9 : : #include "protobuf/sa.pb-c.h"
10 : : #include "protobuf/timer.pb-c.h"
11 : : #include "protobuf/creds.pb-c.h"
12 : : #include "protobuf/core.pb-c.h"
13 : : #include "protobuf/pagemap.pb-c.h"
14 : :
15 : : #include "fdset.h"
16 : : #include "syscall.h"
17 : : #include "ptrace.h"
18 : : #include "asm/processor-flags.h"
19 : : #include "parasite-syscall.h"
20 : : #include "parasite-blob.h"
21 : : #include "parasite.h"
22 : : #include "crtools.h"
23 : : #include "namespaces.h"
24 : : #include "kerndat.h"
25 : : #include "pstree.h"
26 : : #include "posix-timer.h"
27 : : #include "net.h"
28 : : #include "mem.h"
29 : : #include "vma.h"
30 : : #include "proc_parse.h"
31 : :
32 : : #include <string.h>
33 : : #include <stdlib.h>
34 : : #include <elf.h>
35 : :
36 : : #include "asm/parasite-syscall.h"
37 : : #include "asm/dump.h"
38 : : #include "asm/restorer.h"
39 : :
40 : : #define parasite_size (round_up(sizeof(parasite_blob), PAGE_SIZE))
41 : :
42 : : static int can_run_syscall(unsigned long ip, unsigned long start, unsigned long end)
43 : : {
44 [ - + ]: 3664 : return ip >= start && ip < (end - code_syscall_size);
45 : : }
46 : :
47 : : static int syscall_fits_vma_area(struct vma_area *vma_area)
48 : : {
49 : 3664 : return can_run_syscall((unsigned long)vma_area->e->start,
50 : : (unsigned long)vma_area->e->start,
51 : : (unsigned long)vma_area->e->end);
52 : : }
53 : :
54 : 7328 : static struct vma_area *get_vma_by_ip(struct list_head *vma_area_list, unsigned long ip)
55 : : {
56 : : struct vma_area *vma_area;
57 : :
58 [ + - ]: 3664 : list_for_each_entry(vma_area, vma_area_list, list) {
59 [ - + ]: 3664 : if (vma_area->e->start >= TASK_SIZE)
60 : 0 : continue;
61 [ - + ]: 3664 : if (!(vma_area->e->prot & PROT_EXEC))
62 : 0 : continue;
63 [ - + ]: 3664 : if (syscall_fits_vma_area(vma_area))
64 : : return vma_area;
65 : : }
66 : :
67 : : return NULL;
68 : : }
69 : :
70 : : static inline int ptrace_get_regs(int pid, user_regs_struct_t *regs)
71 : : {
72 : : struct iovec iov;
73 : :
74 : 83073 : iov.iov_base = regs;
75 : 83073 : iov.iov_len = sizeof(user_regs_struct_t);
76 : 83073 : return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov);
77 : : }
78 : :
79 : : static inline int ptrace_set_regs(int pid, user_regs_struct_t *regs)
80 : : {
81 : : struct iovec iov;
82 : :
83 : 21098 : iov.iov_base = regs;
84 : 21098 : iov.iov_len = sizeof(user_regs_struct_t);
85 : 21098 : return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov);
86 : : }
87 : :
88 : 5053 : static int get_thread_ctx(int pid, struct thread_ctx *ctx)
89 : : {
90 [ - + ]: 5053 : if (ptrace(PTRACE_GETSIGMASK, pid, sizeof(k_rtsigset_t), &ctx->sigmask)) {
91 : 0 : pr_perror("can't get signal blocking mask for %d", pid);
92 : 0 : return -1;
93 : : }
94 : :
95 [ - + ]: 5053 : if (ptrace_get_regs(pid, &ctx->regs)) {
96 : 0 : pr_perror("Can't obtain registers (pid: %d)", pid);
97 : 0 : return -1;
98 : : }
99 : :
100 : : return 0;
101 : : }
102 : :
103 : 8717 : static int restore_thread_ctx(int pid, struct thread_ctx *ctx)
104 : : {
105 : : int ret = 0;
106 : :
107 [ - + ]: 8717 : if (ptrace_set_regs(pid, &ctx->regs)) {
108 : 0 : pr_perror("Can't restore registers (pid: %d)", pid);
109 : : ret = -1;
110 : : }
111 [ - + ]: 8717 : if (ptrace(PTRACE_SETSIGMASK, pid, sizeof(k_rtsigset_t), &ctx->sigmask)) {
112 : 0 : pr_perror("Can't block signals");
113 : : ret = -1;
114 : : }
115 : :
116 : 8717 : return ret;
117 : : }
118 : :
119 : 24762 : static int parasite_run(pid_t pid, int cmd, unsigned long ip, void *stack,
120 : : user_regs_struct_t *regs, struct thread_ctx *octx)
121 : : {
122 : : k_rtsigset_t block;
123 : :
124 : : ksigfillset(&block);
125 [ - + ]: 12381 : if (ptrace(PTRACE_SETSIGMASK, pid, sizeof(k_rtsigset_t), &block)) {
126 : 0 : pr_perror("Can't block signals for %d", pid);
127 : 0 : goto err_sig;
128 : : }
129 : :
130 : 12381 : parasite_setup_regs(ip, stack, regs);
131 [ - + ]: 12381 : if (ptrace_set_regs(pid, regs)) {
132 : 0 : pr_perror("Can't set registers for %d", pid);
133 : 0 : goto err_regs;
134 : : }
135 : :
136 [ - + ]: 12381 : if (ptrace(cmd, pid, NULL, NULL)) {
137 : 0 : pr_perror("Can't run parasite at %d", pid);
138 : : goto err_cont;
139 : : }
140 : :
141 : : return 0;
142 : :
143 : : err_cont:
144 [ # # ]: 0 : if (ptrace_set_regs(pid, &octx->regs))
145 : 0 : pr_perror("Can't restore regs for %d", pid);
146 : : err_regs:
147 [ # # ]: 0 : if (ptrace(PTRACE_SETSIGMASK, pid, sizeof(k_rtsigset_t), &octx->sigmask))
148 : 0 : pr_perror("Can't restore sigmask for %d", pid);
149 : : err_sig:
150 : : return -1;
151 : : }
152 : :
153 : : /* we run at @regs->ip */
154 : 8268 : static int parasite_trap(struct parasite_ctl *ctl, pid_t pid,
155 : : user_regs_struct_t *regs,
156 : : struct thread_ctx *octx)
157 : : {
158 : : siginfo_t siginfo;
159 : : int status;
160 : : int ret = -1;
161 : :
162 : : /*
163 : : * Most ideas are taken from Tejun Heo's parasite thread
164 : : * https://code.google.com/p/ptrace-parasite/
165 : : */
166 : :
167 [ - + ]: 4134 : if (wait4(pid, &status, __WALL, NULL) != pid) {
168 : 0 : pr_perror("Waited pid mismatch (pid: %d)", pid);
169 : : goto err;
170 : : }
171 : :
172 [ - + ]: 4134 : if (!WIFSTOPPED(status)) {
173 : 0 : pr_err("Task is still running (pid: %d)\n", pid);
174 : : goto err;
175 : : }
176 : :
177 [ - + ]: 4134 : if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo)) {
178 : 0 : pr_perror("Can't get siginfo (pid: %d)", pid);
179 : : goto err;
180 : : }
181 : :
182 [ - + ]: 4134 : if (ptrace_get_regs(pid, regs)) {
183 : 0 : pr_perror("Can't obtain registers (pid: %d)", pid);
184 : : goto err;
185 : : }
186 : :
187 [ + - ][ - + ]: 4134 : if (WSTOPSIG(status) != SIGTRAP || siginfo.si_code != ARCH_SI_TRAP) {
188 : 0 : pr_debug("** delivering signal %d si_code=%d\n",
189 : : siginfo.si_signo, siginfo.si_code);
190 : :
191 : 0 : pr_err("Unexpected %d task interruption, aborting\n", pid);
192 : : goto err;
193 : : }
194 : :
195 : : /*
196 : : * We've reached this point if int3 is triggered inside our
197 : : * parasite code. So we're done.
198 : : */
199 : : ret = 0;
200 : : err:
201 [ - + ]: 4134 : if (restore_thread_ctx(pid, octx))
202 : : ret = -1;
203 : :
204 : 4134 : return ret;
205 : : }
206 : :
207 : 3664 : int __parasite_execute_syscall(struct parasite_ctl *ctl, user_regs_struct_t *regs)
208 : : {
209 : 3664 : pid_t pid = ctl->pid.real;
210 : : int err;
211 : :
212 : : /*
213 : : * Inject syscall instruction and remember original code,
214 : : * we will need it to restore original program content.
215 : : */
216 : 3664 : memcpy(ctl->code_orig, code_syscall, sizeof(ctl->code_orig));
217 [ - + ]: 3664 : if (ptrace_swap_area(pid, (void *)ctl->syscall_ip,
218 : : (void *)ctl->code_orig, sizeof(ctl->code_orig))) {
219 : 0 : pr_err("Can't inject syscall blob (pid: %d)\n", pid);
220 : 0 : return -1;
221 : : }
222 : :
223 : 3664 : err = parasite_run(pid, PTRACE_CONT, ctl->syscall_ip, 0, regs, &ctl->orig);
224 [ + - ]: 3664 : if (!err)
225 : 3664 : err = parasite_trap(ctl, pid, regs, &ctl->orig);
226 : :
227 [ - + ]: 3664 : if (ptrace_poke_area(pid, (void *)ctl->code_orig,
228 : 3664 : (void *)ctl->syscall_ip, sizeof(ctl->code_orig))) {
229 : 0 : pr_err("Can't restore syscall blob (pid: %d)\n", ctl->pid.real);
230 : : err = -1;
231 : : }
232 : :
233 : 3664 : return err;
234 : : }
235 : :
236 : 5490 : void *parasite_args_s(struct parasite_ctl *ctl, int args_size)
237 : : {
238 [ - + ]: 5490 : BUG_ON(args_size > ctl->args_size);
239 : 5490 : return ctl->addr_args;
240 : : }
241 : :
242 : 940 : static int parasite_execute_trap_by_pid(unsigned int cmd,
243 : : struct parasite_ctl *ctl, pid_t pid,
244 : : void *stack,
245 : : struct thread_ctx *octx)
246 : : {
247 : 470 : user_regs_struct_t regs = octx->regs;
248 : : int ret;
249 : :
250 : 470 : *ctl->addr_cmd = cmd;
251 : :
252 : 470 : ret = parasite_run(pid, PTRACE_CONT, ctl->parasite_ip, stack, ®s, octx);
253 [ + - ]: 470 : if (ret == 0)
254 : 470 : ret = parasite_trap(ctl, pid, ®s, octx);
255 [ + - ]: 470 : if (ret == 0)
256 : 470 : ret = (int)REG_RES(regs);
257 : :
258 [ - + ]: 470 : if (ret)
259 : 0 : pr_err("Parasite exited with %d\n", ret);
260 : :
261 : 470 : return ret;
262 : : }
263 : :
264 : 44084 : static int __parasite_send_cmd(int sockfd, struct ctl_msg *m)
265 : : {
266 : : int ret;
267 : :
268 : 44084 : ret = send(sockfd, m, sizeof(*m), 0);
269 [ - + ]: 44084 : if (ret == -1) {
270 : 0 : pr_perror("Failed to send command %d to daemon\n", m->cmd);
271 : 0 : return -1;
272 [ - + ]: 44084 : } else if (ret != sizeof(*m)) {
273 : 0 : pr_err("Message to daemon is trimmed (%d/%d)\n",
274 : : (int)sizeof(*m), ret);
275 : 0 : return -1;
276 : : }
277 : :
278 : 44084 : pr_debug("Sent msg to daemon %d %d %d\n", m->cmd, m->ack, m->err);
279 : 44084 : return 0;
280 : : }
281 : :
282 : 44084 : static int parasite_wait_ack(int sockfd, unsigned int cmd, struct ctl_msg *m)
283 : : {
284 : : int ret;
285 : :
286 : 44084 : pr_debug("Wait for ack %d on daemon socket\n", cmd);
287 : :
288 : : while (1) {
289 : : memzero(m, sizeof(*m));
290 : :
291 : 44084 : ret = recv(sockfd, m, sizeof(*m), MSG_WAITALL);
292 [ - + ]: 44084 : if (ret == -1) {
293 : 0 : pr_perror("Failed to read ack");
294 : 0 : return -1;
295 [ - + ]: 44084 : } else if (ret != sizeof(*m)) {
296 : 0 : pr_err("Message reply from daemon is trimmed (%d/%d)\n",
297 : : (int)sizeof(*m), ret);
298 : 0 : return -1;
299 : : }
300 : 44084 : pr_debug("Fetched ack: %d %d %d\n",
301 : : m->cmd, m->ack, m->err);
302 : :
303 [ + - ][ - + ]: 44084 : if (m->cmd != cmd || m->ack != cmd) {
304 : 0 : pr_err("Communication error, this is not "
305 : : "the ack we expected\n");
306 : 0 : return -1;
307 : : }
308 : : return 0;
309 : : }
310 : :
311 : : return -1;
312 : : }
313 : :
314 : 40420 : int __parasite_wait_daemon_ack(unsigned int cmd,
315 : : struct parasite_ctl *ctl)
316 : : {
317 : : struct ctl_msg m;
318 : :
319 [ + - ]: 40420 : if (parasite_wait_ack(ctl->tsock, cmd, &m))
320 : : return -1;
321 : :
322 [ - + ]: 40420 : if (m.err != 0) {
323 : 0 : pr_err("Command %d for daemon failed with %d\n",
324 : : cmd, m.err);
325 : 0 : return -1;
326 : : }
327 : :
328 : : return 0;
329 : : }
330 : :
331 : 4896 : int __parasite_execute_daemon(unsigned int cmd, struct parasite_ctl *ctl)
332 : : {
333 : : struct ctl_msg m;
334 : :
335 : 44084 : m = ctl_msg_cmd(cmd);
336 : 44084 : return __parasite_send_cmd(ctl->tsock, &m);
337 : : }
338 : :
339 : 34407 : int parasite_execute_daemon(unsigned int cmd, struct parasite_ctl *ctl)
340 : : {
341 : : int ret;
342 : :
343 : : ret = __parasite_execute_daemon(cmd, ctl);
344 [ + - ]: 34407 : if (!ret)
345 : 34407 : ret = __parasite_wait_daemon_ack(cmd, ctl);
346 : :
347 : 34407 : return ret;
348 : : }
349 : :
350 : 3664 : static int gen_parasite_saddr(struct sockaddr_un *saddr, int key)
351 : : {
352 : : int sun_len;
353 : :
354 : 3664 : saddr->sun_family = AF_UNIX;
355 : 3664 : snprintf(saddr->sun_path, UNIX_PATH_MAX,
356 : : "X/crtools-pr-%d", key);
357 : :
358 : 3664 : sun_len = SUN_LEN(saddr);
359 : 3664 : *saddr->sun_path = '\0';
360 : :
361 : 3664 : return sun_len;
362 : : }
363 : :
364 : 8560 : int parasite_send_fd(struct parasite_ctl *ctl, int fd)
365 : : {
366 [ - + ]: 8560 : if (send_fd(ctl->tsock, NULL, 0, fd) < 0) {
367 : 0 : pr_perror("Can't send file descriptor");
368 : 0 : return -1;
369 : : }
370 : : return 0;
371 : : }
372 : :
373 : : /*
374 : : * We need to detect parasite crashes not to hang on socket operations.
375 : : * Since CRIU holds parasite with ptrace, it will receive SIGCHLD if the
376 : : * latter would crash.
377 : : *
378 : : * This puts a restriction on how to execute a sub-process on dump stage.
379 : : * One should use the cr_system helper, that blocks sigcild and waits
380 : : * for the spawned program to finish.
381 : : */
382 : 518 : static void sigchld_handler(int signal, siginfo_t *siginfo, void *data)
383 : : {
384 : : int pid, status;
385 : :
386 : 518 : pr_err("si_code=%d si_pid=%d si_status=%d\n",
387 : : siginfo->si_code, siginfo->si_pid, siginfo->si_status);
388 : :
389 : 518 : pid = waitpid(-1, &status, WNOHANG);
390 [ - + ]: 518 : if (pid <= 0)
391 : 518 : return;
392 : :
393 [ # # ]: 0 : if (WIFEXITED(status))
394 : 0 : pr_err("%d exited with %d unexpectedly\n", pid, WEXITSTATUS(status));
395 [ # # ]: 0 : else if (WIFSIGNALED(status))
396 : 0 : pr_err("%d was killed by %d unexpectedly\n", pid, WTERMSIG(status));
397 [ # # ]: 0 : else if (WIFSTOPPED(status))
398 : 0 : pr_err("%d was stopped by %d unexpectedly\n", pid, WSTOPSIG(status));
399 : :
400 : 0 : exit(1);
401 : : }
402 : :
403 : 3664 : static int setup_child_handler()
404 : : {
405 : 3664 : struct sigaction sa = {
406 : : .sa_sigaction = sigchld_handler,
407 : : .sa_flags = SA_SIGINFO | SA_RESTART,
408 : : };
409 : :
410 : 3664 : sigemptyset(&sa.sa_mask);
411 : 3664 : sigaddset(&sa.sa_mask, SIGCHLD);
412 [ - + ]: 3664 : if (sigaction(SIGCHLD, &sa, NULL)) {
413 : 0 : pr_perror("Unable to setup SIGCHLD handler");
414 : 0 : return -1;
415 : : }
416 : :
417 : : return 0;
418 : : }
419 : :
420 : 3664 : static int restore_child_handler()
421 : : {
422 : 3664 : struct sigaction sa = {
423 : : .sa_handler = SIG_DFL,
424 : : .sa_flags = SA_SIGINFO | SA_RESTART,
425 : : };
426 : :
427 : 3664 : sigemptyset(&sa.sa_mask);
428 : 3664 : sigaddset(&sa.sa_mask, SIGCHLD);
429 [ - + ]: 3664 : if (sigaction(SIGCHLD, &sa, NULL)) {
430 : 0 : pr_perror("Unable to setup SIGCHLD handler");
431 : 0 : return -1;
432 : : }
433 : :
434 : : return 0;
435 : : }
436 : :
437 : : static int ssock = -1;
438 : :
439 : 7328 : static int prepare_tsock(struct parasite_ctl *ctl, pid_t pid,
440 : : struct parasite_init_args *args)
441 : : {
442 : 3664 : pr_info("Putting tsock into pid %d\n", pid);
443 : 3664 : args->h_addr_len = gen_parasite_saddr(&args->h_addr, getpid());
444 : :
445 [ + + ]: 3664 : if (ssock == -1) {
446 : 1792 : int rst = -1;
447 : :
448 : 1792 : pr_info("Switching to %d's net for tsock creation\n", pid);
449 [ + - ]: 1792 : if (switch_ns(pid, &net_ns_desc, &rst))
450 : 0 : return -1;
451 : :
452 : 1792 : ssock = socket(PF_UNIX, SOCK_SEQPACKET, 0);
453 [ - + ]: 1792 : if (ssock < 0)
454 : 0 : pr_perror("Can't create socket");
455 : :
456 [ + - ][ + - ]: 1792 : if (rst >= 0 && restore_ns(rst, &net_ns_desc) < 0)
457 : : return -1;
458 [ + - ]: 1792 : if (ssock < 0)
459 : : return -1;
460 : :
461 [ - + ]: 1792 : if (bind(ssock, (struct sockaddr *)&args->h_addr, args->h_addr_len) < 0) {
462 : 0 : pr_perror("Can't bind socket");
463 : 0 : goto err;
464 : : }
465 : :
466 [ - + ]: 1792 : if (listen(ssock, 1)) {
467 : 1792 : pr_perror("Can't listen on transport socket");
468 : : goto err;
469 : : }
470 : : }
471 : :
472 : : return 0;
473 : : err:
474 : 0 : close_safe(&ssock);
475 : : return -1;
476 : : }
477 : :
478 : 3664 : static int accept_tsock()
479 : : {
480 : : int sock;
481 : :
482 : 3664 : sock = accept(ssock, NULL, 0);
483 [ - + ]: 3664 : if (sock < 0) {
484 : 0 : pr_perror("Can't accept connection to the transport socket");
485 : 0 : close_safe(&ssock);
486 : : }
487 : :
488 : 3664 : return sock;
489 : : }
490 : :
491 : 3664 : static int parasite_init_daemon(struct parasite_ctl *ctl)
492 : : {
493 : : struct parasite_init_args *args;
494 : 3664 : pid_t pid = ctl->pid.real;
495 : : user_regs_struct_t regs;
496 : 3664 : struct ctl_msg m = { };
497 : :
498 : 3664 : *ctl->addr_cmd = PARASITE_CMD_INIT_DAEMON;
499 : :
500 : 3664 : args = parasite_args(ctl, struct parasite_init_args);
501 : :
502 : 3664 : args->sigframe = ctl->rsigframe;
503 : 3664 : args->log_level = log_get_loglevel();
504 : :
505 [ + - ]: 3664 : if (prepare_tsock(ctl, pid, args))
506 : : goto err;;
507 : :
508 : : /* after this we can catch parasite errors in chld handler */
509 [ + - ]: 3664 : if (setup_child_handler())
510 : : goto err;
511 : :
512 : 3664 : regs = ctl->orig.regs;
513 [ + - ]: 3664 : if (parasite_run(pid, PTRACE_CONT, ctl->parasite_ip, ctl->rstack, ®s, &ctl->orig))
514 : : goto err;
515 : :
516 : 3664 : ctl->tsock = accept_tsock();
517 [ + - ]: 3664 : if (ctl->tsock < 0)
518 : : goto err;
519 : :
520 [ + - ]: 3664 : if (parasite_send_fd(ctl, log_get_fd()))
521 : : goto err;
522 : :
523 : 3664 : pr_info("Wait for parasite being daemonized...\n");
524 : :
525 [ - + ]: 3664 : if (parasite_wait_ack(ctl->tsock, PARASITE_CMD_INIT_DAEMON, &m)) {
526 : 0 : pr_err("Can't switch parasite %d to daemon mode %d\n",
527 : : pid, m.err);
528 : 0 : goto err;
529 : : }
530 : :
531 : 3664 : ctl->daemonized = true;
532 : 3664 : pr_info("Parasite %d has been switched to daemon mode\n", pid);
533 : 3664 : return 0;
534 : : err:
535 : : return -1;
536 : : }
537 : :
538 : 470 : int parasite_dump_thread_seized(struct parasite_ctl *ctl, int id,
539 : : struct pid *tid, CoreEntry *core)
540 : : {
541 : : struct parasite_dump_thread *args;
542 : 470 : pid_t pid = tid->real;
543 : 470 : ThreadCoreEntry *tc = core->thread_core;
544 : : int ret;
545 : : struct thread_ctx octx;
546 : :
547 [ - + ]: 470 : BUG_ON(id == 0); /* Leader is dumped in dump_task_core_all */
548 : :
549 : 470 : args = parasite_args(ctl, struct parasite_dump_thread);
550 : :
551 : 470 : ret = get_thread_ctx(pid, &octx);
552 [ + - ]: 470 : if (ret)
553 : : return -1;
554 : :
555 : 470 : tc->has_blk_sigset = true;
556 : 470 : memcpy(&tc->blk_sigset, &octx.sigmask, sizeof(k_rtsigset_t));
557 : :
558 : 470 : ret = parasite_execute_trap_by_pid(PARASITE_CMD_DUMP_THREAD, ctl,
559 : : pid, ctl->r_thread_stack, &octx);
560 [ - + ]: 470 : if (ret) {
561 : 0 : pr_err("Can't init thread in parasite %d\n", pid);
562 : 0 : return -1;
563 : : }
564 : :
565 : 470 : ret = get_task_regs(pid, octx.regs, core);
566 [ - + ]: 470 : if (ret) {
567 : 0 : pr_err("Can't obtain regs for thread %d\n", pid);
568 : 0 : return -1;
569 : : }
570 : :
571 : 470 : tid->virt = args->tid;
572 : 470 : return dump_thread_core(pid, core, args);
573 : : }
574 : :
575 : 919 : int parasite_dump_sigacts_seized(struct parasite_ctl *ctl, struct cr_fdset *cr_fdset)
576 : : {
577 : : struct parasite_dump_sa_args *args;
578 : : int ret, sig, fd;
579 : 919 : SaEntry se = SA_ENTRY__INIT;
580 : :
581 : 919 : args = parasite_args(ctl, struct parasite_dump_sa_args);
582 : :
583 : 919 : ret = parasite_execute_daemon(PARASITE_CMD_DUMP_SIGACTS, ctl);
584 [ + - ]: 919 : if (ret < 0)
585 : : return ret;
586 : :
587 : 919 : fd = fdset_fd(cr_fdset, CR_FD_SIGACT);
588 : :
589 [ + + ]: 59735 : for (sig = 1; sig <= SIGMAX; sig++) {
590 : 58816 : int i = sig - 1;
591 : :
592 [ + + ]: 58816 : if (sig == SIGSTOP || sig == SIGKILL)
593 : 1838 : continue;
594 : :
595 : 113956 : ASSIGN_TYPED(se.sigaction, encode_pointer(args->sas[i].rt_sa_handler));
596 : 56978 : ASSIGN_TYPED(se.flags, args->sas[i].rt_sa_flags);
597 : 113956 : ASSIGN_TYPED(se.restorer, encode_pointer(args->sas[i].rt_sa_restorer));
598 : 56978 : ASSIGN_TYPED(se.mask, args->sas[i].rt_sa_mask.sig[0]);
599 : :
600 [ + - ]: 56978 : if (pb_write_one(fd, &se, PB_SIGACT) < 0)
601 : : return -1;
602 : : }
603 : :
604 : : return 0;
605 : : }
606 : :
607 : : static void encode_itimer(struct itimerval *v, ItimerEntry *ie)
608 : : {
609 : 2757 : ie->isec = v->it_interval.tv_sec;
610 : 2757 : ie->iusec = v->it_interval.tv_usec;
611 : 2757 : ie->vsec = v->it_value.tv_sec;
612 : 2757 : ie->vusec = v->it_value.tv_usec;
613 : : }
614 : :
615 : 919 : int parasite_dump_itimers_seized(struct parasite_ctl *ctl, struct pstree_item *item)
616 : : {
617 : 919 : CoreEntry *core = item->core[0];
618 : : struct parasite_dump_itimers_args *args;
619 : : int ret;
620 : :
621 : 919 : args = parasite_args(ctl, struct parasite_dump_itimers_args);
622 : :
623 : 919 : ret = parasite_execute_daemon(PARASITE_CMD_DUMP_ITIMERS, ctl);
624 [ + - ]: 919 : if (ret < 0)
625 : : return ret;
626 : :
627 : 919 : encode_itimer(&args->real, core->tc->timers->real);
628 : 919 : encode_itimer(&args->virt, core->tc->timers->virt);
629 : 919 : encode_itimer(&args->prof, core->tc->timers->prof);
630 : :
631 : 919 : return 0;
632 : : }
633 : :
634 : : static void encode_posix_timer(struct posix_timer *v, struct proc_posix_timer *vp, PosixTimerEntry *pte)
635 : : {
636 : 8 : pte->it_id = vp->spt.it_id;
637 : 8 : pte->clock_id = vp->spt.clock_id;
638 : 8 : pte->si_signo = vp->spt.si_signo;
639 : 8 : pte->it_sigev_notify = vp->spt.it_sigev_notify;
640 : 16 : pte->sival_ptr = encode_pointer(vp->spt.sival_ptr);
641 : :
642 : 8 : pte->overrun = v->overrun;
643 : :
644 : 8 : pte->isec = v->val.it_interval.tv_sec;
645 : 8 : pte->insec = v->val.it_interval.tv_nsec;
646 : 8 : pte->vsec = v->val.it_value.tv_sec;
647 : 8 : pte->vnsec = v->val.it_value.tv_nsec;
648 : : }
649 : :
650 : 1838 : static int core_alloc_posix_timers(TaskTimersEntry *tte, int n,
651 : : PosixTimerEntry **pte)
652 : : {
653 : : int sz;
654 : :
655 : : /*
656 : : * Will be free()-ed in core_entry_free()
657 : : */
658 : :
659 : 919 : sz = n * (sizeof(PosixTimerEntry *) + sizeof(PosixTimerEntry));
660 [ - + ]: 919 : tte->posix = xmalloc(sz);
661 [ + - ]: 919 : if (!tte->posix)
662 : : return -1;
663 : :
664 : 919 : tte->n_posix = n;
665 : 919 : *pte = (PosixTimerEntry *)(tte->posix + n);
666 : : return 0;
667 : : }
668 : :
669 : 919 : int parasite_dump_posix_timers_seized(struct proc_posix_timers_stat *proc_args,
670 : : struct parasite_ctl *ctl, struct pstree_item *item)
671 : : {
672 : 919 : CoreEntry *core = item->core[0];
673 : 919 : TaskTimersEntry *tte = core->tc->timers;
674 : : PosixTimerEntry *pte;
675 : : struct parasite_dump_posix_timers_args * args;
676 : : struct proc_posix_timer *temp;
677 : : int i;
678 : : int ret = 0;
679 : :
680 [ + - ]: 919 : if (core_alloc_posix_timers(tte, proc_args->timer_n, &pte))
681 : : return -1;
682 : :
683 : 919 : args = parasite_args_s(ctl, posix_timers_dump_size(proc_args->timer_n));
684 : 919 : args->timer_n = proc_args->timer_n;
685 : :
686 : : i = 0;
687 [ + + ]: 927 : list_for_each_entry(temp, &proc_args->timers, list) {
688 : 8 : args->timer[i].it_id = temp->spt.it_id;
689 : 8 : i++;
690 : : }
691 : :
692 : 919 : ret = parasite_execute_daemon(PARASITE_CMD_DUMP_POSIX_TIMERS, ctl);
693 [ + - ]: 919 : if (ret < 0)
694 : : goto end_posix;
695 : :
696 : : i = 0;
697 [ + + ]: 927 : list_for_each_entry(temp, &proc_args->timers, list) {
698 : 8 : posix_timer_entry__init(&pte[i]);
699 : 8 : encode_posix_timer(&args->timer[i], temp, &pte[i]);
700 : 8 : tte->posix[i] = &pte[i];
701 : 8 : i++;
702 : : }
703 : :
704 : : end_posix:
705 : 919 : free_posix_timers(proc_args);
706 : 919 : return ret;
707 : : }
708 : :
709 : 3664 : int parasite_dump_misc_seized(struct parasite_ctl *ctl, struct parasite_dump_misc *misc)
710 : : {
711 : : struct parasite_dump_misc *ma;
712 : :
713 : 3664 : ma = parasite_args(ctl, struct parasite_dump_misc);
714 [ + - ]: 3664 : if (parasite_execute_daemon(PARASITE_CMD_DUMP_MISC, ctl) < 0)
715 : : return -1;
716 : :
717 : 3664 : *misc = *ma;
718 : 3664 : return 0;
719 : : }
720 : :
721 : 38 : struct parasite_tty_args *parasite_dump_tty(struct parasite_ctl *ctl, int fd)
722 : : {
723 : : struct parasite_tty_args *p;
724 : :
725 : 38 : p = parasite_args(ctl, struct parasite_tty_args);
726 : 38 : p->fd = fd;
727 : :
728 [ + - ]: 38 : if (parasite_execute_daemon(PARASITE_CMD_DUMP_TTY, ctl) < 0)
729 : : return NULL;
730 : :
731 : 38 : return p;
732 : : }
733 : :
734 : 919 : int parasite_dump_creds(struct parasite_ctl *ctl, CredsEntry *ce)
735 : : {
736 : : struct parasite_dump_creds *pc;
737 : :
738 : 919 : pc = parasite_args(ctl, struct parasite_dump_creds);
739 [ + - ]: 919 : if (parasite_execute_daemon(PARASITE_CMD_DUMP_CREDS, ctl) < 0)
740 : : return -1;
741 : :
742 : 919 : ce->secbits = pc->secbits;
743 : 919 : ce->n_groups = pc->ngroups;
744 : :
745 : : /*
746 : : * Achtung! We leak the parasite args pointer to the caller.
747 : : * It's not safe in general, but in our case is OK, since the
748 : : * latter doesn't go to parasite before using the data in it.
749 : : */
750 : :
751 : : BUILD_BUG_ON(sizeof(ce->groups[0]) != sizeof(pc->groups[0]));
752 : 919 : ce->groups = pc->groups;
753 : 919 : return 0;
754 : : }
755 : :
756 : 907 : int parasite_drain_fds_seized(struct parasite_ctl *ctl,
757 : 907 : struct parasite_drain_fd *dfds, int *lfds, struct fd_opts *opts)
758 : : {
759 : : int ret = -1, size;
760 : : struct parasite_drain_fd *args;
761 : :
762 : : size = drain_fds_size(dfds);
763 : 907 : args = parasite_args_s(ctl, size);
764 : 907 : memcpy(args, dfds, size);
765 : :
766 : : ret = __parasite_execute_daemon(PARASITE_CMD_DRAIN_FDS, ctl);
767 [ - + ]: 907 : if (ret) {
768 : 0 : pr_err("Parasite failed to drain descriptors\n");
769 : 0 : goto err;
770 : : }
771 : :
772 : 907 : ret = recv_fds(ctl->tsock, lfds, dfds->nr_fds, opts);
773 [ - + ]: 907 : if (ret)
774 : 0 : pr_err("Can't retrieve FDs from socket\n");
775 : :
776 : 907 : ret |= __parasite_wait_daemon_ack(PARASITE_CMD_DRAIN_FDS, ctl);
777 : : err:
778 : 907 : return ret;
779 : : }
780 : :
781 : 210 : int parasite_get_proc_fd_seized(struct parasite_ctl *ctl)
782 : : {
783 : : int ret = -1, fd;
784 : :
785 : : ret = __parasite_execute_daemon(PARASITE_CMD_GET_PROC_FD, ctl);
786 [ - + ]: 210 : if (ret) {
787 : 0 : pr_err("Parasite failed to get proc fd\n");
788 : 0 : return ret;
789 : : }
790 : :
791 : 210 : fd = recv_fd(ctl->tsock);
792 [ - + ]: 210 : if (fd < 0)
793 : 0 : pr_err("Can't retrieve FD from socket\n");
794 [ - + ]: 210 : if (__parasite_wait_daemon_ack(PARASITE_CMD_GET_PROC_FD, ctl)) {
795 : 0 : close_safe(&fd);
796 : 0 : return -1;
797 : : }
798 : :
799 : 210 : return fd;
800 : : }
801 : :
802 : :
803 : : static bool task_in_parasite(struct parasite_ctl *ctl, user_regs_struct_t *regs)
804 : : {
805 : 3664 : void *addr = (void *) REG_IP(*regs);
806 [ + - ][ - + ]: 3664 : return addr >= ctl->remote_map &&
807 : 3664 : addr < ctl->remote_map + ctl->map_length;
808 : : }
809 : :
810 : 3664 : static int parasite_fini_seized(struct parasite_ctl *ctl)
811 : : {
812 : 3664 : pid_t pid = ctl->pid.real;
813 : : user_regs_struct_t regs;
814 : : int status, ret = 0;
815 : :
816 : : /* stop getting chld from parasite -- we're about to step-by-step it */
817 [ + - ]: 3664 : if (restore_child_handler())
818 : : return -1;
819 : :
820 [ + - ]: 3664 : if (!ctl->daemonized)
821 : : return 0;
822 : :
823 : : /* Start to trace syscalls for each thread */
824 [ - + ]: 3664 : if (ptrace(PTRACE_INTERRUPT, pid, NULL, NULL)) {
825 : 0 : pr_perror("Unable to interrupt the process");
826 : 0 : return -1;
827 : : }
828 : :
829 : 3664 : pr_debug("Waiting for %d to trap\n", pid);
830 [ - + ]: 3664 : if (wait4(pid, &status, __WALL, NULL) != pid) {
831 : 0 : pr_perror("Waited pid mismatch (pid: %d)", pid);
832 : 0 : return -1;
833 : : }
834 : :
835 : 3664 : pr_debug("Daemon %d exited trapping\n", pid);
836 [ - + ]: 3664 : if (!WIFSTOPPED(status)) {
837 : 0 : pr_err("Task is still running (pid: %d)\n", pid);
838 : 0 : return -1;
839 : : }
840 : :
841 : : ret = ptrace_get_regs(pid, ®s);
842 [ - + ]: 3664 : if (ret) {
843 : 0 : pr_perror("Unable to get registers");
844 : 0 : return -1;
845 : : }
846 : :
847 [ - + ]: 3664 : if (!task_in_parasite(ctl, ®s)) {
848 : 0 : pr_err("The task is not in parasite code\n");
849 : 0 : return -1;
850 : : }
851 : :
852 : 3664 : ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
853 [ - + ]: 3664 : if (ret) {
854 : 0 : pr_perror("ptrace");
855 : 0 : return -1;
856 : : }
857 : :
858 : : ret = __parasite_execute_daemon(PARASITE_CMD_FINI, ctl);
859 : 3664 : close_safe(&ctl->tsock);
860 [ + - ]: 3664 : if (ret)
861 : : return -1;
862 : :
863 [ + - ]: 3664 : if (parasite_stop_on_syscall(1, __NR_rt_sigreturn))
864 : : return -1;
865 : :
866 : : /*
867 : : * All signals are unblocked now. The kernel notifies about leaving
868 : : * syscall before starting to deliver signals. All parasite code are
869 : : * executed with blocked signals, so we can sefly unmap a parasite blob.
870 : : */
871 : :
872 : 3664 : return 0;
873 : : }
874 : :
875 : : /*
876 : : * Trap tasks on the exit from the specified syscall
877 : : *
878 : : * tasks - number of processes, which should be trapped
879 : : * sys_nr - the required syscall number
880 : : */
881 : 8695 : int parasite_stop_on_syscall(int tasks, const int sys_nr)
882 : : {
883 : : user_regs_struct_t regs;
884 : : int status, ret;
885 : : pid_t pid;
886 : :
887 : : /* Stop all threads on the enter point in sys_rt_sigreturn */
888 [ + + ]: 78917 : while (tasks) {
889 : 70222 : pid = wait4(-1, &status, __WALL, NULL);
890 [ - + ]: 70222 : if (pid == -1) {
891 : 0 : pr_perror("wait4 failed");
892 : 0 : return -1;
893 : : }
894 : :
895 [ + - ][ - + ]: 70222 : if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGTRAP) {
896 : 0 : pr_err("Task is in unexpected state: %x\n", status);
897 : 0 : return -1;
898 : : }
899 : :
900 : 70222 : pr_debug("%d was trapped\n", pid);
901 [ - + ]: 70222 : if (!WIFSTOPPED(status)) {
902 : 0 : pr_err("%d\n", status);
903 : 0 : return -1;
904 : : }
905 : : ret = ptrace_get_regs(pid, ®s);
906 [ - + ]: 70222 : if (ret) {
907 : 0 : pr_perror("ptrace");
908 : 0 : return -1;
909 : : }
910 : :
911 : 70222 : pr_debug("%d is going to execute the syscall %lx\n", pid, REG_SYSCALL_NR(regs));
912 [ + + ]: 70222 : if (REG_SYSCALL_NR(regs) == sys_nr) {
913 : : /*
914 : : * The process is going to execute the required syscall,
915 : : * the next stop will be on the exit from this syscall
916 : : */
917 : 9636 : ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
918 [ - + ]: 9636 : if (ret) {
919 : 0 : pr_perror("ptrace");
920 : 0 : return -1;
921 : : }
922 : :
923 : 9636 : pid = wait4(pid, &status, __WALL, NULL);
924 [ - + ]: 9636 : if (pid == -1) {
925 : 0 : pr_perror("wait4 failed");
926 : 0 : return -1;
927 : : }
928 : :
929 [ + - ][ - + ]: 9636 : if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGTRAP) {
930 : 0 : pr_err("Task is in unexpected state: %x\n", status);
931 : 0 : return -1;
932 : : }
933 : :
934 : 9636 : pr_debug("%d was stopped\n", pid);
935 : 9636 : tasks--;
936 : 9636 : continue;
937 : : }
938 : :
939 : 60586 : ret = ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
940 [ - + ]: 60586 : if (ret) {
941 : 0 : pr_perror("ptrace");
942 : 70222 : return -1;
943 : : }
944 : : }
945 : :
946 : : return 0;
947 : : }
948 : :
949 : 3664 : int parasite_cure_remote(struct parasite_ctl *ctl)
950 : : {
951 : : int ret = 0;
952 : :
953 [ + - ]: 3664 : if (ctl->parasite_ip)
954 [ + - ]: 3664 : if (parasite_fini_seized(ctl))
955 : : return -1;
956 : :
957 : 3664 : close_safe(&ctl->tsock);
958 : :
959 [ + - ]: 3664 : if (ctl->remote_map) {
960 : : struct parasite_unmap_args *args;
961 : :
962 : 3664 : *ctl->addr_cmd = PARASITE_CMD_UNMAP;
963 : :
964 : 3664 : args = parasite_args(ctl, struct parasite_unmap_args);
965 : 3664 : args->parasite_start = ctl->remote_map;
966 : 3664 : args->parasite_len = ctl->map_length;
967 [ - + ]: 3664 : if (parasite_unmap(ctl, ctl->parasite_ip))
968 : : ret = -1;
969 : : }
970 : :
971 : 3664 : return ret;
972 : : }
973 : :
974 : 3664 : int parasite_cure_local(struct parasite_ctl *ctl)
975 : : {
976 : : int ret = 0;
977 : :
978 [ + - ]: 3664 : if (ctl->local_map) {
979 [ - + ]: 3664 : if (munmap(ctl->local_map, ctl->map_length)) {
980 : 0 : pr_err("munmap failed (pid: %d)\n", ctl->pid.real);
981 : : ret = -1;
982 : : }
983 : : }
984 : :
985 : 3664 : free(ctl);
986 : 3664 : return ret;
987 : : }
988 : :
989 : 919 : int parasite_cure_seized(struct parasite_ctl *ctl)
990 : : {
991 : : int ret;
992 : :
993 : 919 : ret = parasite_cure_remote(ctl);
994 [ + - ]: 919 : if (!ret)
995 : 919 : ret = parasite_cure_local(ctl);
996 : :
997 : 919 : return ret;
998 : : }
999 : :
1000 : : /*
1001 : : * parasite_unmap() is used for unmapping parasite and restorer blobs.
1002 : : * A blob can contain code for unmapping itself, so the porcess is
1003 : : * trapped on the exit from the munmap syscall.
1004 : : */
1005 : 4583 : int parasite_unmap(struct parasite_ctl *ctl, unsigned long addr)
1006 : : {
1007 : 4583 : user_regs_struct_t regs = ctl->orig.regs;
1008 : 4583 : pid_t pid = ctl->pid.real;
1009 : : int ret = -1;
1010 : :
1011 : 4583 : ret = parasite_run(pid, PTRACE_SYSCALL, addr, NULL, ®s, &ctl->orig);
1012 [ + - ]: 4583 : if (ret)
1013 : : goto err;
1014 : :
1015 : 4583 : ret = parasite_stop_on_syscall(1, __NR_munmap);
1016 : :
1017 [ - + ]: 4583 : if (restore_thread_ctx(pid, &ctl->orig))
1018 : : ret = -1;
1019 : : err:
1020 : 4583 : return ret;
1021 : : }
1022 : :
1023 : : /* If vma_area_list is NULL, a place for injecting syscall will not be set. */
1024 : 4583 : struct parasite_ctl *parasite_prep_ctl(pid_t pid, struct vm_area_list *vma_area_list)
1025 : : {
1026 : : struct parasite_ctl *ctl = NULL;
1027 : : struct vma_area *vma_area;
1028 : :
1029 [ + - ]: 4583 : if (!arch_can_dump_task(pid))
1030 : : goto err;
1031 : :
1032 : : /*
1033 : : * Control block early setup.
1034 : : */
1035 [ - + ]: 4583 : ctl = xzalloc(sizeof(*ctl));
1036 [ - + ]: 4583 : if (!ctl) {
1037 : 0 : pr_err("Parasite control block allocation failed (pid: %d)\n", pid);
1038 : 0 : goto err;
1039 : : }
1040 : :
1041 : 4583 : ctl->tsock = -1;
1042 : :
1043 [ + - ]: 4583 : if (get_thread_ctx(pid, &ctl->orig))
1044 : : goto err;
1045 : :
1046 : 4583 : ctl->pid.real = pid;
1047 : 4583 : ctl->pid.virt = 0;
1048 : :
1049 [ + + ]: 4583 : if (vma_area_list == NULL)
1050 : : return ctl;
1051 : :
1052 : : /* Search a place for injecting syscall */
1053 : 3664 : vma_area = get_vma_by_ip(&vma_area_list->h, REG_IP(ctl->orig.regs));
1054 [ - + ]: 3664 : if (!vma_area) {
1055 : 0 : pr_err("No suitable VMA found to run parasite "
1056 : : "bootstrap code (pid: %d)\n", pid);
1057 : 0 : goto err;
1058 : : }
1059 : :
1060 : 3664 : ctl->syscall_ip = vma_area->e->start;
1061 : :
1062 : 3664 : return ctl;
1063 : :
1064 : : err:
1065 [ # # ]: 0 : xfree(ctl);
1066 : : return NULL;
1067 : : }
1068 : :
1069 : 3664 : int parasite_map_exchange(struct parasite_ctl *ctl, unsigned long size)
1070 : : {
1071 : : int fd;
1072 : :
1073 : 3664 : ctl->remote_map = mmap_seized(ctl, NULL, size,
1074 : : PROT_READ | PROT_WRITE | PROT_EXEC,
1075 : : MAP_ANONYMOUS | MAP_SHARED, -1, 0);
1076 [ - + ]: 3664 : if (!ctl->remote_map) {
1077 : 0 : pr_err("Can't allocate memory for parasite blob (pid: %d)\n", ctl->pid.real);
1078 : 0 : return -1;
1079 : : }
1080 : :
1081 : 3664 : ctl->map_length = round_up(size, PAGE_SIZE);
1082 : :
1083 [ - + ]: 3664 : fd = open_proc_rw(ctl->pid.real, "map_files/%p-%p",
1084 : : ctl->remote_map, ctl->remote_map + ctl->map_length);
1085 [ + - ]: 3664 : if (fd < 0)
1086 : : return -1;
1087 : :
1088 : 3664 : ctl->local_map = mmap(NULL, size, PROT_READ | PROT_WRITE,
1089 : : MAP_SHARED | MAP_FILE, fd, 0);
1090 : 3664 : close(fd);
1091 : :
1092 [ - + ]: 3664 : if (ctl->local_map == MAP_FAILED) {
1093 : 0 : ctl->local_map = NULL;
1094 : 0 : pr_perror("Can't map remote parasite map");
1095 : 0 : return -1;
1096 : : }
1097 : :
1098 : : return 0;
1099 : : }
1100 : :
1101 : 3664 : static unsigned long parasite_args_size(struct vm_area_list *vmas, struct parasite_drain_fd *dfds, int timer_n)
1102 : : {
1103 : : unsigned long size = PARASITE_ARG_SIZE_MIN;
1104 : :
1105 [ + + ]: 3664 : if (dfds)
1106 : 919 : size = max(size, (unsigned long)drain_fds_size(dfds));
1107 [ + + ]: 3664 : if (timer_n)
1108 : 4 : size = max(size, (unsigned long)posix_timers_dump_size(timer_n));
1109 : 3664 : size = max(size, (unsigned long)dump_pages_args_size(vmas));
1110 : :
1111 : 3664 : return round_up(size, PAGE_SIZE);
1112 : : }
1113 : :
1114 : 7328 : static int parasite_start_daemon(struct parasite_ctl *ctl, struct pstree_item *item)
1115 : : {
1116 : 3664 : pid_t pid = ctl->pid.real;
1117 : :
1118 : : /*
1119 : : * Get task registers before going daemon, since the
1120 : : * get_task_regs needs to call ptrace on _stopped_ task,
1121 : : * while in daemon it is not such.
1122 : : */
1123 : :
1124 [ - + ]: 3664 : if (get_task_regs(pid, ctl->orig.regs, item->core[0])) {
1125 : 0 : pr_err("Can't obtain regs for thread %d\n", pid);
1126 : : return -1;
1127 : : }
1128 : :
1129 [ + - ]: 3664 : if (construct_sigframe(ctl->sigframe, ctl->rsigframe, item->core[0]))
1130 : : return -1;
1131 : :
1132 [ + - ]: 3664 : if (parasite_init_daemon(ctl))
1133 : : return -1;;
1134 : :
1135 : : return 0;
1136 : : }
1137 : :
1138 : 3664 : struct parasite_ctl *parasite_infect_seized(pid_t pid, struct pstree_item *item,
1139 : : struct vm_area_list *vma_area_list, struct parasite_drain_fd *dfds,
1140 : : int timer_n)
1141 : : {
1142 : : int ret;
1143 : : struct parasite_ctl *ctl;
1144 : : unsigned long p, map_exchange_size;
1145 : :
1146 [ - + ]: 3664 : BUG_ON(item->threads[0].real != pid);
1147 : :
1148 [ + - ]: 3664 : if (pstree_alloc_cores(item))
1149 : : return NULL;
1150 : :
1151 : 3664 : ctl = parasite_prep_ctl(pid, vma_area_list);
1152 [ + - ]: 3664 : if (!ctl)
1153 : : return NULL;
1154 : :
1155 : : /*
1156 : : * Inject a parasite engine. Ie allocate memory inside alien
1157 : : * space and copy engine code there. Then re-map the engine
1158 : : * locally, so we will get an easy way to access engine memory
1159 : : * without using ptrace at all.
1160 : : */
1161 : :
1162 : 3664 : ctl->args_size = parasite_args_size(vma_area_list, dfds, timer_n);
1163 : : map_exchange_size = parasite_size + ctl->args_size;
1164 : 3664 : map_exchange_size += RESTORE_STACK_SIGFRAME + PARASITE_STACK_SIZE;
1165 [ + + ]: 3664 : if (item->nr_threads > 1)
1166 : 112 : map_exchange_size += PARASITE_STACK_SIZE;
1167 : :
1168 : 3664 : memcpy(&item->core[0]->tc->blk_sigset, &ctl->orig.sigmask, sizeof(k_rtsigset_t));
1169 : :
1170 : 3664 : ret = parasite_map_exchange(ctl, map_exchange_size);
1171 [ + - ]: 3664 : if (ret)
1172 : : goto err_restore;
1173 : :
1174 : 3664 : pr_info("Putting parasite blob into %p->%p\n", ctl->local_map, ctl->remote_map);
1175 : 3664 : memcpy(ctl->local_map, parasite_blob, sizeof(parasite_blob));
1176 : :
1177 : : /* Setup the rest of a control block */
1178 : 3664 : ctl->parasite_ip = (unsigned long)parasite_sym(ctl->remote_map, __export_parasite_head_start);
1179 : 3664 : ctl->addr_cmd = parasite_sym(ctl->local_map, __export_parasite_cmd);
1180 : 3664 : ctl->addr_args = parasite_sym(ctl->local_map, __export_parasite_args);
1181 : :
1182 : 3664 : p = parasite_size + ctl->args_size;
1183 : :
1184 : 3664 : ctl->rsigframe = ctl->remote_map + p;
1185 : 3664 : ctl->sigframe = ctl->local_map + p;
1186 : :
1187 : : p += RESTORE_STACK_SIGFRAME;
1188 : 3664 : p += PARASITE_STACK_SIZE;
1189 : 3664 : ctl->rstack = ctl->remote_map + p;
1190 : :
1191 [ + + ]: 3664 : if (item->nr_threads > 1) {
1192 : 112 : p += PARASITE_STACK_SIZE;
1193 : 112 : ctl->r_thread_stack = ctl->remote_map + p;
1194 : : }
1195 : :
1196 [ - + ]: 3664 : if (parasite_start_daemon(ctl, item))
1197 : : goto err_restore;
1198 : :
1199 : : return ctl;
1200 : :
1201 : : err_restore:
1202 : 0 : parasite_cure_seized(ctl);
1203 : 0 : return NULL;
1204 : : }
|