LCOV - code coverage report
Current view: top level - home/snorch/criu - cr-restore.c (source / functions) Hit Total Coverage
Test: coverage3.info Lines: 755 1061 71.2 %
Date: 2014-04-22 Functions: 47 51 92.2 %
Branches: 392 760 51.6 %

           Branch data     Line data    Source code
       1                 :            : #include <stdio.h>
       2                 :            : #include <stdlib.h>
       3                 :            : #include <signal.h>
       4                 :            : #include <limits.h>
       5                 :            : #include <unistd.h>
       6                 :            : #include <errno.h>
       7                 :            : #include <dirent.h>
       8                 :            : #include <string.h>
       9                 :            : 
      10                 :            : #include <fcntl.h>
      11                 :            : #include <grp.h>
      12                 :            : 
      13                 :            : #include <sys/types.h>
      14                 :            : #include <sys/stat.h>
      15                 :            : #include <sys/mman.h>
      16                 :            : #include <sys/vfs.h>
      17                 :            : #include <sys/ptrace.h>
      18                 :            : #include <sys/wait.h>
      19                 :            : #include <sys/file.h>
      20                 :            : #include <sys/shm.h>
      21                 :            : #include <sys/mount.h>
      22                 :            : #include <sys/prctl.h>
      23                 :            : 
      24                 :            : #include <sched.h>
      25                 :            : 
      26                 :            : #include <sys/sendfile.h>
      27                 :            : 
      28                 :            : #include "compiler.h"
      29                 :            : #include "asm/types.h"
      30                 :            : #include "asm/restorer.h"
      31                 :            : 
      32                 :            : #include "cr_options.h"
      33                 :            : #include "servicefd.h"
      34                 :            : #include "image.h"
      35                 :            : #include "util.h"
      36                 :            : #include "util-pie.h"
      37                 :            : #include "log.h"
      38                 :            : #include "syscall.h"
      39                 :            : #include "restorer.h"
      40                 :            : #include "sockets.h"
      41                 :            : #include "sk-packet.h"
      42                 :            : #include "lock.h"
      43                 :            : #include "files.h"
      44                 :            : #include "files-reg.h"
      45                 :            : #include "pipes.h"
      46                 :            : #include "fifo.h"
      47                 :            : #include "sk-inet.h"
      48                 :            : #include "eventfd.h"
      49                 :            : #include "eventpoll.h"
      50                 :            : #include "signalfd.h"
      51                 :            : #include "proc_parse.h"
      52                 :            : #include "restorer-blob.h"
      53                 :            : #include "crtools.h"
      54                 :            : #include "namespaces.h"
      55                 :            : #include "mem.h"
      56                 :            : #include "mount.h"
      57                 :            : #include "fsnotify.h"
      58                 :            : #include "pstree.h"
      59                 :            : #include "net.h"
      60                 :            : #include "tty.h"
      61                 :            : #include "cpu.h"
      62                 :            : #include "file-lock.h"
      63                 :            : #include "page-read.h"
      64                 :            : #include "vdso.h"
      65                 :            : #include "stats.h"
      66                 :            : #include "tun.h"
      67                 :            : #include "vma.h"
      68                 :            : #include "kerndat.h"
      69                 :            : #include "rst-malloc.h"
      70                 :            : #include "plugin.h"
      71                 :            : 
      72                 :            : #include "parasite-syscall.h"
      73                 :            : 
      74                 :            : #include "protobuf.h"
      75                 :            : #include "protobuf/sa.pb-c.h"
      76                 :            : #include "protobuf/timer.pb-c.h"
      77                 :            : #include "protobuf/vma.pb-c.h"
      78                 :            : #include "protobuf/rlimit.pb-c.h"
      79                 :            : #include "protobuf/pagemap.pb-c.h"
      80                 :            : #include "protobuf/siginfo.pb-c.h"
      81                 :            : 
      82                 :            : #include "asm/restore.h"
      83                 :            : 
      84                 :            : static struct pstree_item *current;
      85                 :            : 
      86                 :            : static int restore_task_with_children(void *);
      87                 :            : static int sigreturn_restore(pid_t pid, CoreEntry *core);
      88                 :            : static int prepare_restorer_blob(void);
      89                 :            : static int prepare_rlimits(int pid, CoreEntry *core);
      90                 :            : static int prepare_posix_timers(int pid, CoreEntry *core);
      91                 :            : static int prepare_signals(int pid);
      92                 :            : 
      93                 :        347 : static int shmem_remap(void *old_addr, void *new_addr, unsigned long size)
      94                 :            : {
      95                 :            :         void *ret;
      96                 :            : 
      97                 :        347 :         ret = mremap(old_addr, size, size,
      98                 :            :                         MREMAP_FIXED | MREMAP_MAYMOVE, new_addr);
      99         [ -  + ]:        347 :         if (new_addr != ret) {
     100                 :          0 :                 pr_perror("mremap failed");
     101                 :          0 :                 return -1;
     102                 :            :         }
     103                 :            : 
     104                 :            :         return 0;
     105                 :            : }
     106                 :            : 
     107                 :       1012 : static int crtools_prepare_shared(void)
     108                 :            : {
     109         [ +  - ]:       1012 :         if (prepare_shared_fdinfo())
     110                 :            :                 return -1;
     111                 :            : 
     112                 :            :         /* Connections are unlocked from criu */
     113         [ +  - ]:       1012 :         if (collect_inet_sockets())
     114                 :            :                 return -1;
     115                 :            : 
     116         [ +  - ]:       1012 :         if (tty_prep_fds())
     117                 :            :                 return -1;
     118                 :            : 
     119                 :       1012 :         return 0;
     120                 :            : }
     121                 :            : 
     122                 :            : /*
     123                 :            :  * Collect order information:
     124                 :            :  * - reg_file should be before remap, as the latter needs
     125                 :            :  *   to find file_desc objects
     126                 :            :  * - per-pid collects (mm and fd) should be after remap and
     127                 :            :  *   reg_file since both per-pid ones need to get fdesc-s
     128                 :            :  *   and bump counters on remaps if they exist
     129                 :            :  */
     130                 :            : 
     131                 :            : static struct collect_image_info *cinfos[] = {
     132                 :            :         &reg_file_cinfo,
     133                 :            :         &remap_cinfo,
     134                 :            :         &nsfile_cinfo,
     135                 :            :         &pipe_cinfo,
     136                 :            :         &fifo_cinfo,
     137                 :            :         &unix_sk_cinfo,
     138                 :            :         &packet_sk_cinfo,
     139                 :            :         &netlink_sk_cinfo,
     140                 :            :         &eventfd_cinfo,
     141                 :            :         &epoll_tfd_cinfo,
     142                 :            :         &epoll_cinfo,
     143                 :            :         &signalfd_cinfo,
     144                 :            :         &inotify_cinfo,
     145                 :            :         &inotify_mark_cinfo,
     146                 :            :         &fanotify_cinfo,
     147                 :            :         &fanotify_mark_cinfo,
     148                 :            :         &tty_info_cinfo,
     149                 :            :         &tty_cinfo,
     150                 :            :         &tunfile_cinfo,
     151                 :            :         &ext_file_cinfo,
     152                 :            : };
     153                 :            : 
     154                 :        354 : static int root_prepare_shared(void)
     155                 :            : {
     156                 :            :         int ret = 0, i;
     157                 :            :         struct pstree_item *pi;
     158                 :            : 
     159                 :        354 :         pr_info("Preparing info about shared resources\n");
     160                 :            : 
     161         [ +  - ]:        354 :         if (prepare_shared_tty())
     162                 :            :                 return -1;
     163                 :            : 
     164         [ +  - ]:        354 :         if (prepare_shared_reg_files())
     165                 :            :                 return -1;
     166                 :            : 
     167         [ +  + ]:       7434 :         for (i = 0; i < ARRAY_SIZE(cinfos); i++) {
     168                 :       7080 :                 ret = collect_image(cinfos[i]);
     169         [ +  - ]:       7080 :                 if (ret)
     170                 :            :                         return -1;
     171                 :            :         }
     172                 :            : 
     173         [ +  - ]:        354 :         if (collect_pipes())
     174                 :            :                 return -1;
     175         [ +  - ]:        354 :         if (collect_fifo())
     176                 :            :                 return -1;
     177         [ +  - ]:        354 :         if (collect_unix_sockets())
     178                 :            :                 return -1;
     179                 :            : 
     180         [ +  - ]:        354 :         if (tty_verify_active_pairs())
     181                 :            :                 return -1;
     182                 :            : 
     183         [ +  + ]:       1360 :         for_each_pstree_item(pi) {
     184         [ -  + ]:       1006 :                 if (pi->state == TASK_HELPER)
     185                 :          0 :                         continue;
     186                 :            : 
     187                 :       1006 :                 ret = prepare_mm_pid(pi);
     188         [ +  - ]:       1006 :                 if (ret < 0)
     189                 :            :                         break;
     190                 :            : 
     191                 :       1006 :                 ret = prepare_fd_pid(pi);
     192         [ +  - ]:       1006 :                 if (ret < 0)
     193                 :            :                         break;
     194                 :            :         }
     195                 :            : 
     196         [ +  - ]:        354 :         if (ret < 0)
     197                 :            :                 goto err;
     198                 :            : 
     199                 :        354 :         mark_pipe_master();
     200                 :            : 
     201                 :        354 :         ret = tty_setup_slavery();
     202         [ +  - ]:        354 :         if (ret)
     203                 :            :                 goto err;
     204                 :            : 
     205                 :        354 :         ret = resolve_unix_peers();
     206         [ +  - ]:        354 :         if (ret)
     207                 :            :                 goto err;
     208                 :            : 
     209                 :        354 :         ret = prepare_restorer_blob();
     210         [ +  - ]:        354 :         if (ret)
     211                 :            :                 goto err;
     212                 :            : 
     213                 :        354 :         show_saved_shmems();
     214                 :        354 :         show_saved_files();
     215                 :            : err:
     216                 :        354 :         return ret;
     217                 :            : }
     218                 :            : 
     219                 :            : /* Map a private vma, if it is not mapped by a parent yet */
     220                 :      47560 : static int map_private_vma(pid_t pid, struct vma_area *vma, void **tgt_addr,
     221                 :            :                         struct vma_area **pvma, struct list_head *pvma_list)
     222                 :            : {
     223                 :            :         int ret;
     224                 :            :         void *addr, *paddr = NULL;
     225                 :            :         unsigned long nr_pages, size;
     226                 :      23780 :         struct vma_area *p = *pvma;
     227                 :            : 
     228         [ +  + ]:      23780 :         if (vma_area_is(vma, VMA_FILE_PRIVATE)) {
     229                 :       5224 :                 ret = get_filemap_fd(vma);
     230         [ -  + ]:       5224 :                 if (ret < 0) {
     231                 :          0 :                         pr_err("Can't fixup VMA's fd\n");
     232                 :            :                         return -1;
     233                 :            :                 }
     234                 :       5224 :                 vma->e->fd = ret;
     235                 :            :         }
     236                 :            : 
     237                 :      23780 :         nr_pages = vma_entry_len(vma->e) / PAGE_SIZE;
     238         [ -  + ]:      23780 :         vma->page_bitmap = xzalloc(BITS_TO_LONGS(nr_pages) * sizeof(long));
     239         [ +  - ]:      23780 :         if (vma->page_bitmap == NULL)
     240                 :            :                 return -1;
     241                 :            : 
     242         [ +  + ]:      25987 :         list_for_each_entry_continue(p, pvma_list, list) {
     243         [ +  + ]:       8485 :                 if (p->e->start > vma->e->start)
     244                 :            :                          break;
     245                 :            : 
     246 [ +  - ][ +  + ]:       2207 :                 if (!vma_priv(p->e))
     247                 :          3 :                         continue;
     248                 :            : 
     249 [ +  + ][ -  + ]:       2204 :                  if (p->e->end != vma->e->end ||
     250                 :            :                      p->e->start != vma->e->start)
     251                 :       2056 :                         continue;
     252                 :            : 
     253                 :            :                 /* Check flags, which must be identical for both vma-s */
     254         [ +  - ]:        148 :                 if ((vma->e->flags ^ p->e->flags) & (MAP_GROWSDOWN | MAP_ANONYMOUS))
     255                 :            :                         break;
     256                 :            : 
     257 [ +  + ][ +  - ]:        148 :                 if (!(vma->e->flags & MAP_ANONYMOUS) &&
     258                 :        117 :                     vma->e->shmid != p->e->shmid)
     259                 :            :                         break;
     260                 :            : 
     261                 :        148 :                 pr_info("COW 0x%016"PRIx64"-0x%016"PRIx64" 0x%016"PRIx64" vma\n",
     262                 :            :                         vma->e->start, vma->e->end, vma->e->pgoff);
     263                 :        148 :                 paddr = decode_pointer(vma->premmaped_addr);
     264                 :            :         }
     265                 :            : 
     266                 :      23780 :         *pvma = p;
     267                 :            : 
     268                 :            :         /*
     269                 :            :          * A grow-down VMA has a guard page, which protect a VMA below it.
     270                 :            :          * So one more page is mapped here to restore content of the first page
     271                 :            :          */
     272         [ +  + ]:      23780 :         if (vma->e->flags & MAP_GROWSDOWN) {
     273                 :        487 :                 vma->e->start -= PAGE_SIZE;
     274         [ -  + ]:        487 :                 if (paddr)
     275                 :          0 :                         paddr -= PAGE_SIZE;
     276                 :            :         }
     277                 :            : 
     278                 :      23780 :         size = vma_entry_len(vma->e);
     279         [ +  - ]:      23780 :         if (paddr == NULL) {
     280                 :            :                 /*
     281                 :            :                  * The respective memory area was NOT found in the parent.
     282                 :            :                  * Map a new one.
     283                 :            :                  */
     284                 :      23780 :                 pr_info("Map 0x%016"PRIx64"-0x%016"PRIx64" 0x%016"PRIx64" vma\n",
     285                 :            :                         vma->e->start, vma->e->end, vma->e->pgoff);
     286                 :            : 
     287                 :      23780 :                 addr = mmap(*tgt_addr, size,
     288                 :      23780 :                                 vma->e->prot | PROT_WRITE,
     289                 :      23780 :                                 vma->e->flags | MAP_FIXED,
     290                 :      47560 :                                 vma->e->fd, vma->e->pgoff);
     291                 :            : 
     292         [ -  + ]:      23780 :                 if (addr == MAP_FAILED) {
     293                 :          0 :                         pr_perror("Unable to map ANON_VMA");
     294                 :            :                         return -1;
     295                 :            :                 }
     296                 :            :         } else {
     297                 :            :                 /*
     298                 :            :                  * This region was found in parent -- remap it to inherit physical
     299                 :            :                  * pages (if any) from it (and COW them later if required).
     300                 :            :                  */
     301                 :          0 :                 vma->ppage_bitmap = p->page_bitmap;
     302                 :            : 
     303                 :          0 :                 addr = mremap(paddr, size, size,
     304                 :            :                                 MREMAP_FIXED | MREMAP_MAYMOVE, *tgt_addr);
     305         [ #  # ]:          0 :                 if (addr != *tgt_addr) {
     306                 :          0 :                         pr_perror("Unable to remap a private vma");
     307                 :            :                         return -1;
     308                 :            :                 }
     309                 :            : 
     310                 :            :         }
     311                 :            : 
     312                 :      23780 :         vma->premmaped_addr = (unsigned long) addr;
     313                 :      23780 :         pr_debug("\tpremap 0x%016"PRIx64"-0x%016"PRIx64" -> %016lx\n",
     314                 :            :                 vma->e->start, vma->e->end, (unsigned long)addr);
     315                 :            : 
     316         [ +  + ]:      23780 :         if (vma->e->flags & MAP_GROWSDOWN) { /* Skip gurad page */
     317                 :        487 :                 vma->e->start += PAGE_SIZE;
     318                 :        487 :                 vma->premmaped_addr += PAGE_SIZE;
     319                 :            :         }
     320                 :            : 
     321         [ +  + ]:      23780 :         if (vma_area_is(vma, VMA_FILE_PRIVATE))
     322                 :       5224 :                 close(vma->e->fd);
     323                 :            : 
     324                 :      23780 :         *tgt_addr += size;
     325                 :            :         return 0;
     326                 :            : }
     327                 :            : 
     328                 :        471 : static int restore_priv_vma_content(pid_t pid)
     329                 :            : {
     330                 :            :         struct vma_area *vma;
     331                 :            :         int ret = 0;
     332                 :        471 :         struct list_head *vmas = &current->rst->vmas.h;
     333                 :            : 
     334                 :            :         unsigned int nr_restored = 0;
     335                 :            :         unsigned int nr_shared = 0;
     336                 :            :         unsigned int nr_droped = 0;
     337                 :            :         unsigned int nr_compared = 0;
     338                 :            :         unsigned long va;
     339                 :            :         struct page_read pr;
     340                 :            : 
     341                 :        471 :         vma = list_first_entry(vmas, struct vma_area, list);
     342                 :            : 
     343         [ -  + ]:        471 :         ret = open_page_read(pid, &pr,
     344                 :        471 :                         opts.auto_dedup ? O_RDWR : O_RSTR, false);
     345         [ +  - ]:        471 :         if (ret)
     346                 :            :                 return -1;
     347                 :            : 
     348                 :            :         /*
     349                 :            :          * Read page contents.
     350                 :            :          */
     351                 :            :         while (1) {
     352                 :            :                 unsigned long off, i, nr_pages;;
     353                 :            :                 struct iovec iov;
     354                 :            : 
     355                 :      27944 :                 ret = pr.get_pagemap(&pr, &iov);
     356         [ +  + ]:      27944 :                 if (ret <= 0)
     357                 :            :                         break;
     358                 :            : 
     359                 :      27473 :                 va = (unsigned long)iov.iov_base;
     360                 :      27473 :                 nr_pages = iov.iov_len / PAGE_SIZE;
     361                 :            : 
     362         [ +  + ]:     332816 :                 for (i = 0; i < nr_pages; i++) {
     363                 :            :                         unsigned char buf[PAGE_SIZE];
     364                 :            :                         void *p;
     365                 :            : 
     366                 :            :                         /*
     367                 :            :                          * The lookup is over *all* possible VMAs
     368                 :            :                          * read from image file.
     369                 :            :                          */
     370         [ +  + ]:     328730 :                         while (va >= vma->e->end) {
     371         [ +  - ]:      23387 :                                 if (vma->list.next == vmas)
     372                 :            :                                         goto err_addr;
     373                 :            :                                 vma = list_entry(vma->list.next, struct vma_area, list);
     374                 :            :                         }
     375                 :            : 
     376                 :            :                         /*
     377                 :            :                          * Make sure the page address is inside existing VMA
     378                 :            :                          * and the VMA it refers to still private one, since
     379                 :            :                          * there is no guarantee that the data from pagemap is
     380                 :            :                          * valid.
     381                 :            :                          */
     382         [ +  - ]:     305343 :                         if (va < vma->e->start)
     383                 :            :                                 goto err_addr;
     384 [ +  - ][ -  + ]:     305343 :                         else if (unlikely(!vma_priv(vma->e))) {
     385                 :          0 :                                 pr_err("Trying to restore page for non-private VMA\n");
     386                 :          0 :                                 goto err_addr;
     387                 :            :                         }
     388                 :            : 
     389                 :     305343 :                         off = (va - vma->e->start) / PAGE_SIZE;
     390                 :     610686 :                         p = decode_pointer((off) * PAGE_SIZE +
     391                 :     305343 :                                         vma->premmaped_addr);
     392                 :            : 
     393                 :     305343 :                         set_bit(off, vma->page_bitmap);
     394         [ -  + ]:     305343 :                         if (vma->ppage_bitmap) { /* inherited vma */
     395                 :            :                                 clear_bit(off, vma->ppage_bitmap);
     396                 :            : 
     397                 :          0 :                                 ret = pr.read_page(&pr, va, buf);
     398         [ #  # ]:          0 :                                 if (ret < 0)
     399                 :            :                                         goto err_read;
     400                 :          0 :                                 va += PAGE_SIZE;
     401                 :            : 
     402                 :          0 :                                 nr_compared++;
     403                 :            : 
     404         [ #  # ]:          0 :                                 if (memcmp(p, buf, PAGE_SIZE) == 0) {
     405                 :          0 :                                         nr_shared++; /* the page is cowed */
     406                 :          0 :                                         continue;
     407                 :            :                                 }
     408                 :            : 
     409                 :            :                                 memcpy(p, buf, PAGE_SIZE);
     410                 :            :                         } else {
     411                 :     305343 :                                 ret = pr.read_page(&pr, va, p);
     412         [ +  - ]:     305343 :                                 if (ret < 0)
     413                 :            :                                         goto err_read;
     414                 :     305343 :                                 va += PAGE_SIZE;
     415                 :            :                         }
     416                 :            : 
     417                 :     305343 :                         nr_restored++;
     418                 :            :                 }
     419                 :            : 
     420         [ +  - ]:      27473 :                 if (pr.put_pagemap)
     421                 :      27473 :                         pr.put_pagemap(&pr);
     422                 :      27944 :         }
     423                 :            : 
     424                 :            : err_read:
     425                 :        471 :         pr.close(&pr);
     426         [ +  - ]:        471 :         if (ret < 0)
     427                 :            :                 return ret;
     428                 :            : 
     429                 :            :         /* Remove pages, which were not shared with a child */
     430         [ +  + ]:      24800 :         list_for_each_entry(vma, vmas, list) {
     431                 :            :                 unsigned long size, i = 0;
     432                 :      24329 :                 void *addr = decode_pointer(vma->premmaped_addr);
     433                 :            : 
     434         [ +  - ]:      24329 :                 if (vma->ppage_bitmap == NULL)
     435                 :      24329 :                         continue;
     436                 :            : 
     437                 :          0 :                 size = vma_entry_len(vma->e) / PAGE_SIZE;
     438                 :            :                 while (1) {
     439                 :            :                         /* Find all pages, which are not shared with this child */
     440                 :          0 :                         i = find_next_bit(vma->ppage_bitmap, size, i);
     441                 :            : 
     442         [ #  # ]:          0 :                         if ( i >= size)
     443                 :            :                                 break;
     444                 :            : 
     445                 :          0 :                         ret = madvise(addr + PAGE_SIZE * i,
     446                 :            :                                                 PAGE_SIZE, MADV_DONTNEED);
     447         [ #  # ]:          0 :                         if (ret < 0) {
     448                 :          0 :                                 pr_perror("madvise failed");
     449                 :          0 :                                 return -1;
     450                 :            :                         }
     451                 :          0 :                         i++;
     452                 :          0 :                         nr_droped++;
     453                 :          0 :                 }
     454                 :            :         }
     455                 :            : 
     456                 :        471 :         cnt_add(CNT_PAGES_COMPARED, nr_compared);
     457                 :        471 :         cnt_add(CNT_PAGES_SKIPPED_COW, nr_shared);
     458                 :        471 :         cnt_add(CNT_PAGES_RESTORED, nr_restored);
     459                 :            : 
     460                 :        471 :         pr_info("nr_restored_pages: %d\n", nr_restored);
     461                 :        471 :         pr_info("nr_shared_pages:   %d\n", nr_shared);
     462                 :        471 :         pr_info("nr_droped_pages:   %d\n", nr_droped);
     463                 :            : 
     464                 :        471 :         return 0;
     465                 :            : 
     466                 :            : err_addr:
     467                 :          0 :         pr_err("Page entry address %lx outside of VMA %lx-%lx\n",
     468                 :            :                va, (long)vma->e->start, (long)vma->e->end);
     469                 :          0 :         return -1;
     470                 :            : }
     471                 :            : 
     472                 :        478 : static int prepare_mappings(int pid)
     473                 :            : {
     474                 :            :         int ret = 0;
     475                 :            :         struct vma_area *pvma, *vma;
     476                 :            :         void *addr;
     477                 :            :         struct vm_area_list *vmas;
     478                 :            :         struct list_head *parent_vmas = NULL;
     479                 :        478 :         LIST_HEAD(empty);
     480                 :            : 
     481                 :            :         void *old_premmapped_addr = NULL;
     482                 :            :         unsigned long old_premmapped_len, pstart = 0;
     483                 :            : 
     484                 :        478 :         vmas = &current->rst->vmas;
     485         [ +  + ]:        478 :         if (vmas->nr == 0) /* Zombie */
     486                 :            :                 goto out;
     487                 :            : 
     488                 :            :         /*
     489                 :            :          * Keep parent vmas at hands to check whether we can "inherit" them.
     490                 :            :          * See comments in map_private_vma.
     491                 :            :          */
     492         [ +  + ]:        471 :         if (current->parent)
     493                 :        117 :                 parent_vmas = &current->parent->rst->vmas.h;
     494                 :            :         else
     495                 :            :                 parent_vmas = &empty;
     496                 :            : 
     497                 :            :         /* Reserve a place for mapping private vma-s one by one */
     498                 :        471 :         addr = mmap(NULL, vmas->priv_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
     499         [ -  + ]:        471 :         if (addr == MAP_FAILED) {
     500                 :          0 :                 pr_perror("Unable to reserve memory (%lu bytes)", vmas->priv_size);
     501                 :          0 :                 return -1;
     502                 :            :         }
     503                 :            : 
     504                 :        471 :         old_premmapped_addr = current->rst->premmapped_addr;
     505                 :        471 :         old_premmapped_len = current->rst->premmapped_len;
     506                 :        471 :         current->rst->premmapped_addr = addr;
     507                 :        471 :         current->rst->premmapped_len = vmas->priv_size;
     508                 :            : 
     509                 :        471 :         pvma = list_entry(parent_vmas, struct vma_area, list);
     510                 :            : 
     511         [ +  + ]:      24800 :         list_for_each_entry(vma, &vmas->h, list) {
     512         [ -  + ]:      24329 :                 if (pstart > vma->e->start) {
     513                 :            :                         ret = -1;
     514                 :          0 :                         pr_err("VMA-s are not sorted in the image file\n");
     515                 :          0 :                         break;
     516                 :            :                 }
     517                 :            :                 pstart = vma->e->start;
     518                 :            : 
     519 [ +  + ][ +  + ]:      24329 :                 if (!vma_priv(vma->e))
     520                 :        549 :                         continue;
     521                 :            : 
     522                 :      23780 :                 ret = map_private_vma(pid, vma, &addr, &pvma, parent_vmas);
     523         [ +  - ]:      23780 :                 if (ret < 0)
     524                 :            :                         break;
     525                 :            :         }
     526                 :            : 
     527         [ +  - ]:        471 :         if (ret >= 0)
     528                 :        471 :                 ret = restore_priv_vma_content(pid);
     529                 :            : 
     530                 :            : out:
     531   [ -  +  #  # ]:        478 :         if (old_premmapped_addr &&
     532                 :          0 :             munmap(old_premmapped_addr, old_premmapped_len)) {
     533                 :          0 :                 pr_perror("Unable to unmap %p(%lx)",
     534                 :            :                                 old_premmapped_addr, old_premmapped_len);
     535                 :          0 :                 return -1;
     536                 :            :         }
     537                 :            : 
     538                 :            : 
     539                 :        478 :         return ret;
     540                 :            : }
     541                 :            : 
     542                 :            : /*
     543                 :            :  * A gard page must be unmapped after restoring content and
     544                 :            :  * forking children to restore COW memory.
     545                 :            :  */
     546                 :        354 : static int unmap_guard_pages()
     547                 :            : {
     548                 :            :         struct vma_area *vma;
     549                 :        354 :         struct list_head *vmas = &current->rst->vmas.h;
     550                 :            : 
     551         [ +  + ]:      18101 :         list_for_each_entry(vma, vmas, list) {
     552 [ +  + ][ +  + ]:      17747 :                 if (!vma_priv(vma->e))
     553                 :        405 :                         continue;
     554                 :            : 
     555         [ +  + ]:      17342 :                 if (vma->e->flags & MAP_GROWSDOWN) {
     556                 :        361 :                         void *addr = decode_pointer(vma->premmaped_addr);
     557                 :            : 
     558         [ -  + ]:        361 :                         if (munmap(addr - PAGE_SIZE, PAGE_SIZE)) {
     559                 :          0 :                                 pr_perror("Can't unmap guard page\n");
     560                 :          0 :                                 return -1;
     561                 :            :                         }
     562                 :            :                 }
     563                 :            :         }
     564                 :            : 
     565                 :            :         return 0;
     566                 :            : }
     567                 :            : 
     568                 :        347 : static int open_vmas(int pid)
     569                 :            : {
     570                 :            :         struct vma_area *vma;
     571                 :            :         int ret = 0;
     572                 :        347 :         struct list_head *vmas = &current->rst->vmas.h;
     573                 :            : 
     574         [ +  + ]:      18094 :         list_for_each_entry(vma, vmas, list) {
     575         [ +  + ]:      17747 :                 if (!(vma_area_is(vma, VMA_AREA_REGULAR)))
     576                 :        347 :                         continue;
     577                 :            : 
     578                 :      17400 :                 pr_info("Opening 0x%016"PRIx64"-0x%016"PRIx64" 0x%016"PRIx64" (%x) vma\n",
     579                 :            :                                 vma->e->start, vma->e->end,
     580                 :            :                                 vma->e->pgoff, vma->e->status);
     581                 :            : 
     582         [ +  + ]:      17400 :                 if (vma_area_is(vma, VMA_AREA_SYSVIPC))
     583                 :         10 :                         ret = vma->e->shmid;
     584         [ +  + ]:      17390 :                 else if (vma_area_is(vma, VMA_ANON_SHARED))
     585                 :         36 :                         ret = get_shmem_fd(pid, vma->e);
     586         [ +  + ]:      17354 :                 else if (vma_area_is(vma, VMA_FILE_SHARED))
     587                 :         12 :                         ret = get_filemap_fd(vma);
     588         [ -  + ]:      17342 :                 else if (vma_area_is(vma, VMA_AREA_SOCKET))
     589                 :          0 :                         ret = get_socket_fd(pid, vma->e);
     590                 :            :                 else
     591                 :      17342 :                         continue;
     592                 :            : 
     593         [ -  + ]:         58 :                 if (ret < 0) {
     594                 :          0 :                         pr_err("Can't fixup fd\n");
     595                 :          0 :                         break;
     596                 :            :                 }
     597                 :            : 
     598                 :         58 :                 pr_info("\t`- setting %d as mapping fd\n", ret);
     599                 :         58 :                 vma->e->fd = ret;
     600                 :            :         }
     601                 :            : 
     602         [ +  - ]:        347 :         return ret < 0 ? -1 : 0;
     603                 :            : }
     604                 :            : 
     605                 :            : static rt_sigaction_t sigchld_act;
     606                 :        347 : static int prepare_sigactions(int pid)
     607                 :            : {
     608                 :            :         rt_sigaction_t act, oact;
     609                 :            :         int fd_sigact;
     610                 :            :         SaEntry *e;
     611                 :            :         int sig;
     612                 :            :         int ret = -1;
     613                 :            : 
     614                 :        347 :         fd_sigact = open_image(CR_FD_SIGACT, O_RSTR, pid);
     615         [ +  - ]:        347 :         if (fd_sigact < 0)
     616                 :            :                 return -1;
     617                 :            : 
     618         [ +  + ]:      22555 :         for (sig = 1; sig <= SIGMAX; sig++) {
     619         [ +  + ]:      22208 :                 if (sig == SIGKILL || sig == SIGSTOP)
     620                 :        694 :                         continue;
     621                 :            : 
     622                 :      21514 :                 ret = pb_read_one_eof(fd_sigact, &e, PB_SIGACT);
     623         [ -  + ]:      21514 :                 if (ret == 0) {
     624         [ #  # ]:          0 :                         if (sig != SIGMAX_OLD + 1) { /* backward compatibility */
     625                 :          0 :                                 pr_err("Unexpected EOF %d\n", sig);
     626                 :            :                                 ret = -1;
     627                 :          0 :                                 break;
     628                 :            :                         }
     629                 :          0 :                         pr_warn("This format of sigacts-%d.img is deprecated\n", pid);
     630                 :          0 :                         break;
     631                 :            :                 }
     632         [ +  - ]:      21514 :                 if (ret < 0)
     633                 :            :                         break;
     634                 :            : 
     635                 :      43028 :                 ASSIGN_TYPED(act.rt_sa_handler, decode_pointer(e->sigaction));
     636                 :      21514 :                 ASSIGN_TYPED(act.rt_sa_flags, e->flags);
     637                 :      43028 :                 ASSIGN_TYPED(act.rt_sa_restorer, decode_pointer(e->restorer));
     638                 :      21514 :                 ASSIGN_TYPED(act.rt_sa_mask.sig[0], e->mask);
     639                 :            : 
     640                 :      21514 :                 sa_entry__free_unpacked(e, NULL);
     641                 :            : 
     642         [ +  + ]:      21514 :                 if (sig == SIGCHLD) {
     643                 :        347 :                         sigchld_act = act;
     644                 :        347 :                         continue;
     645                 :            :                 }
     646                 :            :                 /*
     647                 :            :                  * A pure syscall is used, because glibc
     648                 :            :                  * sigaction overwrites se_restorer.
     649                 :            :                  */
     650                 :      21167 :                 ret = sys_sigaction(sig, &act, &oact, sizeof(k_rtsigset_t));
     651         [ -  + ]:      21167 :                 if (ret == -1) {
     652                 :          0 :                         pr_err("%d: Can't restore sigaction: %m\n", pid);
     653                 :          0 :                         goto err;
     654                 :            :                 }
     655                 :            :         }
     656                 :            : 
     657                 :            : err:
     658                 :        347 :         close_safe(&fd_sigact);
     659                 :        347 :         return ret;
     660                 :            : }
     661                 :            : 
     662                 :        347 : static int pstree_wait_helpers()
     663                 :            : {
     664                 :            :         struct pstree_item *pi;
     665                 :            : 
     666         [ +  + ]:        471 :         list_for_each_entry(pi, &current->children, sibling) {
     667                 :            :                 int status, ret;
     668                 :            : 
     669         [ +  - ]:        124 :                 if (pi->state != TASK_HELPER)
     670                 :        124 :                         continue;
     671                 :            : 
     672                 :            :                 /* Check, that a helper completed. */
     673                 :          0 :                 ret = waitpid(pi->pid.virt, &status, 0);
     674         [ #  # ]:          0 :                 if (ret == -1) {
     675         [ #  # ]:          0 :                         if (errno == ECHILD)
     676                 :          0 :                                 continue; /* It has been waited in sigchld_handler */
     677                 :          0 :                         pr_err("waitpid(%d) failed\n", pi->pid.virt);
     678                 :          0 :                         return -1;
     679                 :            :                 }
     680 [ #  # ][ #  # ]:          0 :                 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
     681                 :          0 :                         pr_err("%d exited with non-zero code (%d,%d)\n", pi->pid.virt,
     682                 :            :                                 WEXITSTATUS(status), WTERMSIG(status));
     683                 :          0 :                         return -1;
     684                 :            :                 }
     685                 :            : 
     686                 :            :         }
     687                 :            : 
     688                 :            :         return 0;
     689                 :            : }
     690                 :            : 
     691                 :            : 
     692                 :        347 : static int restore_one_alive_task(int pid, CoreEntry *core)
     693                 :            : {
     694                 :        347 :         pr_info("Restoring resources\n");
     695                 :            : 
     696                 :        347 :         rst_mem_switch_to_private();
     697                 :            : 
     698         [ +  - ]:        347 :         if (pstree_wait_helpers())
     699                 :            :                 return -1;
     700                 :            : 
     701         [ +  - ]:        347 :         if (prepare_fds(current))
     702                 :            :                 return -1;
     703                 :            : 
     704         [ +  - ]:        347 :         if (prepare_file_locks(pid))
     705                 :            :                 return -1;
     706                 :            : 
     707         [ +  - ]:        347 :         if (prepare_sigactions(pid))
     708                 :            :                 return -1;
     709                 :            : 
     710         [ +  - ]:        347 :         if (open_vmas(pid))
     711                 :            :                 return -1;
     712                 :            : 
     713         [ +  - ]:        347 :         if (prepare_signals(pid))
     714                 :            :                 return -1;
     715                 :            : 
     716         [ +  - ]:        347 :         if (prepare_posix_timers(pid, core))
     717                 :            :                 return -1;
     718                 :            : 
     719         [ +  - ]:        347 :         if (prepare_rlimits(pid, core) < 0)
     720                 :            :                 return -1;
     721                 :            : 
     722                 :        347 :         return sigreturn_restore(pid, core);
     723                 :            : }
     724                 :            : 
     725                 :          7 : static void zombie_prepare_signals(void)
     726                 :            : {
     727                 :            :         sigset_t blockmask;
     728                 :            :         int sig;
     729                 :            :         struct sigaction act;
     730                 :            : 
     731                 :          7 :         sigfillset(&blockmask);
     732                 :          7 :         sigprocmask(SIG_UNBLOCK, &blockmask, NULL);
     733                 :            : 
     734                 :            :         memset(&act, 0, sizeof(act));
     735                 :            :         act.sa_handler = SIG_DFL;
     736                 :            : 
     737         [ +  + ]:        455 :         for (sig = 1; sig <= SIGMAX; sig++)
     738                 :        448 :                 sigaction(sig, &act, NULL);
     739                 :          7 : }
     740                 :            : 
     741                 :            : #define SIG_FATAL_MASK  (       \
     742                 :            :                 (1 << SIGHUP)     |\
     743                 :            :                 (1 << SIGINT)     |\
     744                 :            :                 (1 << SIGQUIT)    |\
     745                 :            :                 (1 << SIGILL)     |\
     746                 :            :                 (1 << SIGTRAP)    |\
     747                 :            :                 (1 << SIGABRT)    |\
     748                 :            :                 (1 << SIGIOT)     |\
     749                 :            :                 (1 << SIGBUS)     |\
     750                 :            :                 (1 << SIGFPE)     |\
     751                 :            :                 (1 << SIGKILL)    |\
     752                 :            :                 (1 << SIGUSR1)    |\
     753                 :            :                 (1 << SIGSEGV)    |\
     754                 :            :                 (1 << SIGUSR2)    |\
     755                 :            :                 (1 << SIGPIPE)    |\
     756                 :            :                 (1 << SIGALRM)    |\
     757                 :            :                 (1 << SIGTERM)    |\
     758                 :            :                 (1 << SIGXCPU)    |\
     759                 :            :                 (1 << SIGXFSZ)    |\
     760                 :            :                 (1 << SIGVTALRM)|\
     761                 :            :                 (1 << SIGPROF)    |\
     762                 :            :                 (1 << SIGPOLL)    |\
     763                 :            :                 (1 << SIGIO)      |\
     764                 :            :                 (1 << SIGSYS)     |\
     765                 :            :                 (1 << SIGUNUSED)|\
     766                 :            :                 (1 << SIGSTKFLT)|\
     767                 :            :                 (1 << SIGPWR)      \
     768                 :            :         )
     769                 :            : 
     770                 :            : static inline int sig_fatal(int sig)
     771                 :            : {
     772 [ #  # ][ #  # ]:          0 :         return (sig > 0) && (sig < SIGMAX) && (SIG_FATAL_MASK & (1UL << sig));
     773                 :            : }
     774                 :            : 
     775                 :            : struct task_entries *task_entries;
     776                 :            : 
     777                 :         14 : static int restore_one_zombie(int pid, CoreEntry *core)
     778                 :            : {
     779                 :          7 :         int exit_code = core->tc->exit_code;
     780                 :            : 
     781                 :          7 :         pr_info("Restoring zombie with %d code\n", exit_code);
     782                 :            : 
     783                 :          7 :         sys_prctl(PR_SET_NAME, (long)(void *)core->tc->comm, 0, 0, 0);
     784                 :            : 
     785         [ +  - ]:          7 :         if (task_entries != NULL) {
     786                 :          7 :                 restore_finish_stage(CR_STATE_RESTORE);
     787                 :          7 :                 zombie_prepare_signals();
     788                 :          7 :                 mutex_lock(&task_entries->zombie_lock);
     789                 :            :         }
     790                 :            : 
     791         [ -  + ]:          7 :         if (exit_code & 0x7f) {
     792                 :            :                 int signr;
     793                 :            : 
     794                 :            :                 /* prevent generating core files */
     795         [ #  # ]:          0 :                 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0))
     796                 :          0 :                         pr_perror("Can't drop the dumpable flag");
     797                 :            : 
     798                 :            :                 signr = exit_code & 0x7F;
     799         [ #  # ]:          0 :                 if (!sig_fatal(signr)) {
     800                 :          0 :                         pr_warn("Exit with non fatal signal ignored\n");
     801                 :            :                         signr = SIGABRT;
     802                 :            :                 }
     803                 :            : 
     804         [ #  # ]:          0 :                 if (kill(pid, signr) < 0)
     805                 :          0 :                         pr_perror("Can't kill myself, will just exit");
     806                 :            : 
     807                 :            :                 exit_code = 0;
     808                 :            :         }
     809                 :            : 
     810                 :          7 :         exit((exit_code >> 8) & 0x7f);
     811                 :            : 
     812                 :            :         /* never reached */
     813                 :            :         BUG_ON(1);
     814                 :            :         return -1;
     815                 :            : }
     816                 :            : 
     817                 :       2920 : static int check_core(CoreEntry *core, struct pstree_item *me)
     818                 :            : {
     819                 :            :         int ret = -1;
     820                 :            : 
     821         [ -  + ]:       1460 :         if (core->mtype != CORE_ENTRY__MARCH) {
     822                 :          0 :                 pr_err("Core march mismatch %d\n", (int)core->mtype);
     823                 :            :                 goto out;
     824                 :            :         }
     825                 :            : 
     826         [ -  + ]:       1460 :         if (!core->tc) {
     827                 :          0 :                 pr_err("Core task state data missed\n");
     828                 :            :                 goto out;
     829                 :            :         }
     830                 :            : 
     831         [ +  + ]:       1460 :         if (core->tc->task_state != TASK_DEAD) {
     832 [ +  - ][ -  + ]:       1432 :                 if (!core->ids && !me->ids) {
     833                 :          0 :                         pr_err("Core IDS data missed for non-zombie\n");
     834                 :            :                         goto out;
     835                 :            :                 }
     836                 :            : 
     837         [ -  + ]:       1432 :                 if (!CORE_THREAD_ARCH_INFO(core)) {
     838                 :          0 :                         pr_err("Core info data missed for non-zombie\n");
     839                 :            :                         goto out;
     840                 :            :                 }
     841                 :            :         }
     842                 :            : 
     843                 :            :         ret = 0;
     844                 :            : out:
     845                 :       1460 :         return ret;
     846                 :            : }
     847                 :            : 
     848                 :        354 : static int restore_one_task(int pid, CoreEntry *core)
     849                 :            : {
     850                 :            :         int ret;
     851                 :            : 
     852                 :            :         /* No more fork()-s => no more per-pid logs */
     853                 :            : 
     854      [ +  +  - ]:        354 :         switch ((int)core->tc->task_state) {
     855                 :            :         case TASK_ALIVE:
     856                 :            :         case TASK_STOPPED:
     857                 :        347 :                 ret = restore_one_alive_task(pid, core);
     858                 :          0 :                 break;
     859                 :            :         case TASK_DEAD:
     860                 :          7 :                 ret = restore_one_zombie(pid, core);
     861                 :            :                 break;
     862                 :            :         default:
     863                 :          0 :                 pr_err("Unknown state in code %d\n", (int)core->tc->task_state);
     864                 :            :                 ret = -1;
     865                 :          0 :                 break;
     866                 :            :         }
     867                 :            : 
     868                 :          0 :         core_entry__free_unpacked(core, NULL);
     869                 :          0 :         return ret;
     870                 :            : }
     871                 :            : 
     872                 :            : /* All arguments should be above stack, because it grows down */
     873                 :            : struct cr_clone_arg {
     874                 :            :         char stack[PAGE_SIZE] __attribute__((aligned (8)));
     875                 :            :         char stack_ptr[0];
     876                 :            :         struct pstree_item *item;
     877                 :            :         unsigned long clone_flags;
     878                 :            :         int fd;
     879                 :            : 
     880                 :            :         CoreEntry *core;
     881                 :            : };
     882                 :            : 
     883                 :       1460 : static inline int fork_with_pid(struct pstree_item *item)
     884                 :            : {
     885                 :            :         int ret = -1, fd;
     886                 :            :         struct cr_clone_arg ca;
     887                 :       1460 :         pid_t pid = item->pid.virt;
     888                 :            : 
     889         [ +  - ]:       1460 :         if (item->state != TASK_HELPER) {
     890                 :       1460 :                 fd = open_image(CR_FD_CORE, O_RSTR, pid);
     891         [ +  - ]:       1460 :                 if (fd < 0)
     892                 :            :                         return -1;
     893                 :            : 
     894                 :       1460 :                 ret = pb_read_one(fd, &ca.core, PB_CORE);
     895                 :       1460 :                 close(fd);
     896                 :            : 
     897         [ +  - ]:       1460 :                 if (ret < 0)
     898                 :            :                         return -1;
     899                 :            : 
     900         [ +  - ]:       1460 :                 if (check_core(ca.core, item))
     901                 :            :                         return -1;
     902                 :            : 
     903                 :       1460 :                 item->state = ca.core->tc->task_state;
     904                 :            : 
     905      [ +  -  + ]:       1460 :                 switch (item->state) {
     906                 :            :                 case TASK_ALIVE:
     907                 :            :                 case TASK_STOPPED:
     908                 :            :                         break;
     909                 :            :                 case TASK_DEAD:
     910                 :         28 :                         item->parent->rst->nr_zombies++;
     911                 :         28 :                         break;
     912                 :            :                 default:
     913                 :          0 :                         pr_err("Unknown task state %d\n", item->state);
     914                 :          0 :                         return -1;
     915                 :            :                 }
     916                 :            :         } else
     917                 :          0 :                 ca.core = NULL;
     918                 :            : 
     919                 :            :         ret = -1;
     920                 :            : 
     921                 :       1460 :         ca.item = item;
     922                 :       1460 :         ca.clone_flags = item->rst->clone_flags;
     923                 :            : 
     924                 :       1460 :         pr_info("Forking task with %d pid (flags 0x%lx)\n", pid, ca.clone_flags);
     925                 :            : 
     926         [ +  + ]:       1460 :         if (!(ca.clone_flags & CLONE_NEWPID)) {
     927                 :            :                 char buf[32];
     928                 :            : 
     929                 :       1040 :                 ca.fd = open(LAST_PID_PATH, O_RDWR);
     930         [ -  + ]:       1040 :                 if (ca.fd < 0) {
     931                 :          0 :                         pr_perror("%d: Can't open %s", pid, LAST_PID_PATH);
     932                 :          0 :                         goto err;
     933                 :            :                 }
     934                 :            : 
     935         [ -  + ]:       1040 :                 if (flock(ca.fd, LOCK_EX)) {
     936                 :          0 :                         close(ca.fd);
     937                 :          0 :                         pr_perror("%d: Can't lock %s", pid, LAST_PID_PATH);
     938                 :          0 :                         goto err;
     939                 :            :                 }
     940                 :            : 
     941                 :       1040 :                 snprintf(buf, sizeof(buf), "%d", pid - 1);
     942         [ +  - ]:       1040 :                 if (write_img_buf(ca.fd, buf, strlen(buf)))
     943                 :            :                         goto err_unlock;
     944                 :            :         } else {
     945                 :        420 :                 ca.fd = -1;
     946         [ -  + ]:        420 :                 BUG_ON(pid != INIT_PID);
     947                 :            :         }
     948                 :            : 
     949         [ +  + ]:       1460 :         if (ca.clone_flags & CLONE_NEWNET)
     950                 :            :                 /*
     951                 :            :                  * When restoring a net namespace we need to communicate
     952                 :            :                  * with the original (i.e. -- init) one. Thus, prepare for
     953                 :            :                  * that before we leave the existing namespaces.
     954                 :            :                  */
     955         [ +  - ]:        428 :                 if (netns_pre_create())
     956                 :            :                         goto err_unlock;
     957                 :            : 
     958                 :       1460 :         ret = clone(restore_task_with_children, ca.stack_ptr,
     959                 :       1460 :                         ca.clone_flags | SIGCHLD, &ca);
     960                 :            : 
     961         [ -  + ]:        772 :         if (ret < 0)
     962                 :          0 :                 pr_perror("Can't fork for %d", pid);
     963                 :            : 
     964         [ +  + ]:        772 :         if (item == root_item)
     965                 :        448 :                 item->pid.real = ret;
     966                 :            : 
     967 [ +  + ][ +  - ]:        772 :         if (opts.pidfile && root_item == item) {
     968                 :            :                 int pid;
     969                 :            : 
     970                 :            :                 pid = ret;
     971                 :            : 
     972                 :        210 :                 ret = write_pidfile(pid);
     973         [ -  + ]:        210 :                 if (ret < 0) {
     974                 :          0 :                         pr_perror("Can't write pidfile");
     975                 :          0 :                         kill(pid, SIGKILL);
     976                 :            :                 }
     977                 :            :         }
     978                 :            : 
     979                 :            : err_unlock:
     980         [ +  + ]:        772 :         if (ca.fd >= 0) {
     981         [ -  + ]:        562 :                 if (flock(ca.fd, LOCK_UN))
     982                 :          0 :                         pr_perror("%d: Can't unlock %s", pid, LAST_PID_PATH);
     983                 :            : 
     984                 :        562 :                 close(ca.fd);
     985                 :            :         }
     986                 :            : err:
     987         [ +  - ]:        772 :         if (ca.core)
     988                 :        772 :                 core_entry__free_unpacked(ca.core, NULL);
     989                 :        772 :         return ret;
     990                 :            : }
     991                 :            : 
     992                 :        436 : static void sigchld_handler(int signal, siginfo_t *siginfo, void *data)
     993                 :            : {
     994                 :            :         struct pstree_item *pi;
     995                 :        436 :         pid_t pid = siginfo->si_pid;
     996                 :            :         int status;
     997                 :            :         int exit;
     998                 :            : 
     999                 :        436 :         exit = (siginfo->si_code == CLD_EXITED);
    1000                 :        436 :         status = siginfo->si_status;
    1001                 :            : 
    1002                 :            :         /* skip scripts */
    1003 [ -  + ][ #  # ]:        436 :         if (!current && root_item->pid.real != pid) {
    1004                 :          0 :                 pid = waitpid(root_item->pid.real, &status, WNOHANG);
    1005         [ #  # ]:          0 :                 if (pid <= 0)
    1006                 :        436 :                         return;
    1007                 :          0 :                 exit = WIFEXITED(status);
    1008         [ #  # ]:          0 :                 status = exit ? WEXITSTATUS(status) : WTERMSIG(status);
    1009                 :            :         }
    1010                 :            : 
    1011 [ +  - ][ +  - ]:        436 :         if (!current || status)
    1012                 :            :                 goto err;
    1013                 :            : 
    1014         [ +  - ]:        436 :         while (pid) {
    1015                 :        436 :                 pid = waitpid(-1, &status, WNOHANG);
    1016         [ -  + ]:        436 :                 if (pid <= 0)
    1017                 :            :                         return;
    1018                 :            : 
    1019                 :          0 :                 exit = WIFEXITED(status);
    1020         [ #  # ]:          0 :                 status = exit ? WEXITSTATUS(status) : WTERMSIG(status);
    1021         [ #  # ]:          0 :                 if (status)
    1022                 :            :                         break;
    1023                 :            : 
    1024                 :            :                 /* Exited (with zero code) helpers are OK */
    1025         [ #  # ]:          0 :                 list_for_each_entry(pi, &current->children, sibling)
    1026         [ #  # ]:          0 :                         if (pi->pid.virt == siginfo->si_pid)
    1027                 :            :                                 break;
    1028                 :            : 
    1029         [ #  # ]:          0 :                 BUG_ON(&pi->sibling == &current->children);
    1030         [ #  # ]:        436 :                 if (pi->state != TASK_HELPER)
    1031                 :            :                         break;
    1032                 :            :         }
    1033                 :            : 
    1034                 :            : err:
    1035         [ #  # ]:          0 :         if (exit)
    1036                 :          0 :                 pr_err("%d exited, status=%d\n", pid, status);
    1037                 :            :         else
    1038                 :          0 :                 pr_err("%d killed by signal %d\n", pid, status);
    1039                 :            : 
    1040                 :          0 :         futex_abort_and_wake(&task_entries->nr_in_progress);
    1041                 :            : }
    1042                 :            : 
    1043                 :        478 : static void restore_sid(void)
    1044                 :            : {
    1045                 :            :         pid_t sid;
    1046                 :            : 
    1047                 :            :         /*
    1048                 :            :          * SID can only be reset to pid or inherited from parent.
    1049                 :            :          * Thus we restore it right here to let our kids inherit
    1050                 :            :          * one in case they need it.
    1051                 :            :          *
    1052                 :            :          * PGIDs are restored late when all tasks are forked and
    1053                 :            :          * we can call setpgid() on custom values.
    1054                 :            :          */
    1055                 :            : 
    1056         [ +  + ]:        478 :         if (current->pid.virt == current->sid) {
    1057                 :        366 :                 pr_info("Restoring %d to %d sid\n", current->pid.virt, current->sid);
    1058                 :        366 :                 sid = setsid();
    1059         [ -  + ]:        366 :                 if (sid != current->sid) {
    1060                 :          0 :                         pr_perror("Can't restore sid (%d)", sid);
    1061                 :          0 :                         exit(1);
    1062                 :            :                 }
    1063                 :            :         } else {
    1064                 :        112 :                 sid = getsid(getpid());
    1065         [ -  + ]:        112 :                 if (sid != current->sid) {
    1066                 :            :                         /* Skip the root task if it's not init */
    1067 [ #  # ][ #  # ]:          0 :                         if (current == root_item && root_item->pid.virt != INIT_PID)
    1068                 :        478 :                                 return;
    1069                 :          0 :                         pr_err("Requested sid %d doesn't match inherited %d\n",
    1070                 :            :                                         current->sid, sid);
    1071                 :          0 :                         exit(1);
    1072                 :            :                 }
    1073                 :            :         }
    1074                 :            : }
    1075                 :            : 
    1076                 :        354 : static void restore_pgid(void)
    1077                 :            : {
    1078                 :            :         /*
    1079                 :            :          * Unlike sessions, process groups (a.k.a. pgids) can be joined
    1080                 :            :          * by any task, provided the task with pid == pgid (group leader)
    1081                 :            :          * exists. Thus, in order to restore pgid we must make sure that
    1082                 :            :          * group leader was born and created the group, then join one.
    1083                 :            :          *
    1084                 :            :          * We do this _before_ finishing the forking stage to make sure
    1085                 :            :          * helpers are still with us.
    1086                 :            :          */
    1087                 :            : 
    1088                 :        354 :         pid_t pgid, my_pgid = current->pgid;
    1089                 :            : 
    1090                 :        354 :         pr_info("Restoring %d to %d pgid\n", current->pid.virt, my_pgid);
    1091                 :            : 
    1092                 :        354 :         pgid = getpgrp();
    1093         [ +  + ]:        354 :         if (my_pgid == pgid)
    1094                 :        354 :                 return;
    1095                 :            : 
    1096         [ -  + ]:          2 :         if (my_pgid != current->pid.virt) {
    1097                 :            :                 struct pstree_item *leader;
    1098                 :            : 
    1099                 :            :                 /*
    1100                 :            :                  * Wait for leader to become such.
    1101                 :            :                  * Missing leader means we're going to crtools
    1102                 :            :                  * group (-j option).
    1103                 :            :                  */
    1104                 :            : 
    1105                 :          0 :                 leader = current->rst->pgrp_leader;
    1106         [ #  # ]:          0 :                 if (leader) {
    1107         [ #  # ]:          0 :                         BUG_ON(my_pgid != leader->pid.virt);
    1108                 :          0 :                         futex_wait_until(&leader->rst->pgrp_set, 1);
    1109                 :            :                 }
    1110                 :            :         }
    1111                 :            : 
    1112                 :          2 :         pr_info("\twill call setpgid, mine pgid is %d\n", pgid);
    1113         [ -  + ]:          2 :         if (setpgid(0, my_pgid) != 0) {
    1114                 :          0 :                 pr_perror("Can't restore pgid (%d/%d->%d)", current->pid.virt, pgid, current->pgid);
    1115                 :          0 :                 exit(1);
    1116                 :            :         }
    1117                 :            : 
    1118         [ +  - ]:          2 :         if (my_pgid == current->pid.virt)
    1119                 :          2 :                 futex_set_and_wake(&current->rst->pgrp_set, 1);
    1120                 :            : }
    1121                 :            : 
    1122                 :        354 : static int mount_proc(void)
    1123                 :            : {
    1124                 :            :         int fd, ret;
    1125                 :        354 :         char proc_mountpoint[] = "crtools-proc.XXXXXX";
    1126                 :            : 
    1127         [ -  + ]:        354 :         if (mkdtemp(proc_mountpoint) == NULL) {
    1128                 :          0 :                 pr_perror("mkdtemp failed %s", proc_mountpoint);
    1129                 :          0 :                 return -1;
    1130                 :            :         }
    1131                 :            : 
    1132                 :        354 :         pr_info("Mount procfs in %s\n", proc_mountpoint);
    1133         [ -  + ]:        354 :         if (mount("proc", proc_mountpoint, "proc", MS_MGC_VAL, NULL)) {
    1134                 :          0 :                 pr_perror("mount failed");
    1135                 :          0 :                 rmdir(proc_mountpoint);
    1136                 :          0 :                 return -1;
    1137                 :            :         }
    1138                 :            : 
    1139                 :        354 :         ret = fd = open_detach_mount(proc_mountpoint);
    1140         [ +  - ]:        354 :         if (fd >= 0) {
    1141                 :        354 :                 ret = set_proc_fd(fd);
    1142                 :        354 :                 close(fd);
    1143                 :            :         }
    1144                 :            : 
    1145                 :        354 :         return ret;
    1146                 :            : }
    1147                 :            : 
    1148                 :            : /*
    1149                 :            :  * Tasks cannot change sid (session id) arbitrary, but can either
    1150                 :            :  * inherit one from ancestor, or create a new one with id equal to
    1151                 :            :  * their pid. Thus sid-s restore is tied with children creation.
    1152                 :            :  */
    1153                 :            : 
    1154                 :        478 : static int create_children_and_session(void)
    1155                 :            : {
    1156                 :            :         int ret;
    1157                 :            :         struct pstree_item *child;
    1158                 :            : 
    1159                 :        478 :         pr_info("Restoring children in alien sessions:\n");
    1160         [ +  + ]:       1118 :         list_for_each_entry(child, &current->children, sibling) {
    1161         [ +  - ]:        640 :                 if (!restore_before_setsid(child))
    1162                 :        640 :                         continue;
    1163                 :            : 
    1164 [ #  # ][ #  # ]:          0 :                 BUG_ON(child->born_sid != -1 && getsid(getpid()) != child->born_sid);
    1165                 :            : 
    1166                 :          0 :                 ret = fork_with_pid(child);
    1167         [ #  # ]:          0 :                 if (ret < 0)
    1168                 :            :                         return ret;
    1169                 :            :         }
    1170                 :            : 
    1171                 :        478 :         restore_sid();
    1172                 :            : 
    1173                 :        478 :         pr_info("Restoring children in our session:\n");
    1174         [ +  + ]:        802 :         list_for_each_entry(child, &current->children, sibling) {
    1175         [ -  + ]:        448 :                 if (restore_before_setsid(child))
    1176                 :          0 :                         continue;
    1177                 :            : 
    1178                 :        448 :                 ret = fork_with_pid(child);
    1179         [ +  - ]:        324 :                 if (ret < 0)
    1180                 :            :                         return ret;
    1181                 :            :         }
    1182                 :            : 
    1183                 :            :         return 0;
    1184                 :            : }
    1185                 :            : 
    1186                 :        688 : static int restore_task_with_children(void *_arg)
    1187                 :            : {
    1188                 :            :         struct cr_clone_arg *ca = _arg;
    1189                 :            :         pid_t pid;
    1190                 :            :         int ret;
    1191                 :            :         sigset_t blockmask;
    1192                 :            : 
    1193                 :        688 :         current = ca->item;
    1194                 :            : 
    1195         [ +  + ]:        688 :         if (current != root_item) {
    1196                 :            :                 char buf[PATH_MAX];
    1197                 :            :                 int fd;
    1198                 :            : 
    1199                 :            :                 /* Determine PID in CRIU's namespace */
    1200                 :        124 :                 fd = get_service_fd(CR_PROC_FD_OFF);
    1201         [ -  + ]:        124 :                 if (fd < 0)
    1202                 :          0 :                         exit(1);
    1203                 :            : 
    1204                 :        124 :                 ret = readlinkat(fd, "self", buf, sizeof(buf) - 1);
    1205         [ -  + ]:        124 :                 if (ret < 0) {
    1206                 :          0 :                         pr_perror("Unable to read the /proc/self link");
    1207                 :          0 :                         exit(1);
    1208                 :            :                 }
    1209                 :        124 :                 buf[ret] = '\0';
    1210                 :            : 
    1211                 :        248 :                 current->pid.real = atoi(buf);
    1212                 :        124 :                 pr_debug("PID: real %d virt %d\n",
    1213                 :            :                                 current->pid.real, current->pid.virt);
    1214                 :            :         }
    1215                 :            : 
    1216         [ +  + ]:        688 :         if ( !(ca->clone_flags & CLONE_FILES))
    1217                 :        680 :                 close_safe(&ca->fd);
    1218                 :            : 
    1219         [ +  - ]:        688 :         if (current->state != TASK_HELPER) {
    1220                 :        688 :                 ret = clone_service_fd(current->rst->service_fd_id);
    1221         [ -  + ]:        688 :                 if (ret)
    1222                 :          0 :                         exit(1);
    1223                 :            :         }
    1224                 :            : 
    1225                 :        688 :         pid = getpid();
    1226         [ -  + ]:        688 :         if (current->pid.virt != pid) {
    1227                 :          0 :                 pr_err("Pid %d do not match expected %d\n", pid, current->pid.virt);
    1228                 :          0 :                 exit(-1);
    1229                 :            :         }
    1230                 :            : 
    1231                 :        688 :         ret = log_init_by_pid();
    1232         [ -  + ]:        688 :         if (ret < 0)
    1233                 :          0 :                 exit(1);
    1234                 :            : 
    1235                 :            :         /* Restore root task */
    1236         [ +  + ]:        688 :         if (current->parent == NULL) {
    1237         [ -  + ]:        564 :                 if (restore_finish_stage(CR_STATE_RESTORE_NS) < 0)
    1238                 :          0 :                         exit(1);
    1239                 :            : 
    1240         [ -  + ]:        564 :                 if (prepare_namespace(current, ca->clone_flags))
    1241                 :          0 :                         exit(1);
    1242                 :            : 
    1243         [ +  - ]:        354 :                 if (!(root_ns_mask & CLONE_NEWNS))
    1244         [ -  + ]:        354 :                         if (rst_collect_local_mntns())
    1245                 :          0 :                                 exit(1);
    1246                 :            : 
    1247                 :            :                 /*
    1248                 :            :                  * We need non /proc proc mount for restoring pid and mount
    1249                 :            :                  * namespaces and do not care for the rest of the cases.
    1250                 :            :                  * Thus -- mount proc at custom location for any new namespace
    1251                 :            :                  */
    1252         [ +  - ]:        354 :                 if (mount_proc())
    1253                 :            :                         goto err;
    1254                 :            : 
    1255         [ -  + ]:        354 :                 if (close_old_fds(current))
    1256                 :          0 :                         exit(1);
    1257                 :            : 
    1258         [ -  + ]:        354 :                 if (mntns_collect_root(getpid()) < 0)
    1259                 :          0 :                         exit(1);
    1260                 :            : 
    1261         [ +  - ]:        354 :                 if (root_prepare_shared())
    1262                 :            :                         goto err;
    1263                 :            :         }
    1264                 :            : 
    1265                 :            :         /*
    1266                 :            :          * The block mask will be restored in sigreturn.
    1267                 :            :          *
    1268                 :            :          * TODO: This code should be removed, when a freezer will be added.
    1269                 :            :          */
    1270                 :        478 :         sigfillset(&blockmask);
    1271                 :        478 :         sigdelset(&blockmask, SIGCHLD);
    1272                 :        478 :         ret = sigprocmask(SIG_BLOCK, &blockmask, NULL);
    1273         [ -  + ]:        478 :         if (ret) {
    1274                 :          0 :                 pr_perror("%d: Can't block signals", current->pid.virt);
    1275                 :          0 :                 goto err;
    1276                 :            :         }
    1277                 :            : 
    1278         [ +  - ]:        478 :         if (prepare_mappings(pid))
    1279                 :            :                 goto err;
    1280                 :            : 
    1281         [ +  + ]:        478 :         if (!(ca->clone_flags & CLONE_FILES)) {
    1282                 :        470 :                 ret = close_old_fds(current);
    1283         [ -  + ]:        470 :                 if (ret)
    1284                 :          0 :                         exit(1);
    1285                 :            :         }
    1286                 :            : 
    1287         [ +  - ]:        478 :         if (create_children_and_session())
    1288                 :            :                 goto err;
    1289                 :            : 
    1290 [ +  + ][ +  - ]:        354 :         if (current->ids && current->ids->has_mnt_ns_id) {
    1291                 :            :                 struct ns_id *nsid;
    1292                 :            : 
    1293                 :        347 :                 nsid = lookup_ns_by_id(current->ids->mnt_ns_id, &mnt_ns_desc);
    1294         [ -  + ]:        347 :                 if (nsid == NULL) {
    1295                 :          0 :                         pr_err("Can't find mount namespace %d\n", current->ids->mnt_ns_id);
    1296                 :          0 :                         goto err;
    1297                 :            :                 }
    1298         [ +  - ]:        347 :                 if (restore_task_mnt_ns(nsid, current->pid.real))
    1299                 :            :                         goto err;
    1300                 :            :         }
    1301                 :            : 
    1302         [ +  - ]:        354 :         if (unmap_guard_pages())
    1303                 :            :                 goto err;
    1304                 :            : 
    1305                 :        354 :         restore_pgid();
    1306                 :            : 
    1307         [ +  - ]:        354 :         if (restore_finish_stage(CR_STATE_FORKING) < 0)
    1308                 :            :                 goto err;
    1309                 :            : 
    1310 [ +  + ][ -  + ]:        354 :         if (current->parent == NULL && fini_mnt_ns())
    1311                 :          0 :                 exit (1);
    1312                 :            : 
    1313         [ +  - ]:        354 :         if (current->state == TASK_HELPER)
    1314                 :            :                 return 0;
    1315                 :            : 
    1316                 :        354 :         return restore_one_task(current->pid.virt, ca->core);
    1317                 :            : err:
    1318         [ #  # ]:          0 :         if (current->parent == NULL)
    1319                 :          0 :                 fini_mnt_ns();
    1320                 :            : 
    1321                 :          0 :         exit(1);
    1322                 :            : }
    1323                 :            : 
    1324                 :       2804 : static inline int stage_participants(int next_stage)
    1325                 :            : {
    1326   [ +  +  +  +  :       2804 :         switch (next_stage) {
                   -  - ]
    1327                 :            :         case CR_STATE_FAIL:
    1328                 :            :                 return 0;
    1329                 :            :         case CR_STATE_RESTORE_NS:
    1330                 :       1012 :                 return 1;
    1331                 :            :         case CR_STATE_FORKING:
    1332                 :        448 :                 return task_entries->nr_tasks + task_entries->nr_helpers;
    1333                 :            :         case CR_STATE_RESTORE:
    1334                 :            :         case CR_STATE_RESTORE_SIGCHLD:
    1335                 :        896 :                 return task_entries->nr_threads;
    1336                 :            :         case CR_STATE_RESTORE_CREDS:
    1337                 :        448 :                 return task_entries->nr_threads;
    1338                 :            :         }
    1339                 :            : 
    1340                 :          0 :         BUG();
    1341                 :          0 :         return -1;
    1342                 :            : }
    1343                 :            : 
    1344                 :            : static int restore_wait_inprogress_tasks()
    1345                 :            : {
    1346                 :            :         int ret;
    1347                 :       2240 :         futex_t *np = &task_entries->nr_in_progress;
    1348                 :            : 
    1349                 :       2240 :         futex_wait_while_gt(np, 0);
    1350                 :            :         ret = (int)futex_get(np);
    1351 [ -  + ][ -  + ]:       2240 :         if (ret < 0)
    1352                 :            :                 return ret;
    1353                 :            : 
    1354                 :            :         return 0;
    1355                 :            : }
    1356                 :            : 
    1357                 :       1792 : static void __restore_switch_stage(int next_stage)
    1358                 :            : {
    1359                 :       1792 :         futex_set(&task_entries->nr_in_progress,
    1360                 :       1792 :                         stage_participants(next_stage));
    1361                 :       1792 :         futex_set_and_wake(&task_entries->start, next_stage);
    1362                 :       1792 : }
    1363                 :            : 
    1364                 :       1792 : static int restore_switch_stage(int next_stage)
    1365                 :            : {
    1366                 :       1792 :         __restore_switch_stage(next_stage);
    1367                 :       1792 :         return restore_wait_inprogress_tasks();
    1368                 :            : }
    1369                 :            : 
    1370                 :        448 : static int attach_to_tasks()
    1371                 :            : {
    1372                 :            :         struct pstree_item *item;
    1373                 :            : 
    1374         [ +  + ]:       1398 :         for_each_pstree_item(item) {
    1375                 :            :                 pid_t pid = item->pid.real;
    1376                 :            :                 int status, i;
    1377                 :            : 
    1378         [ +  + ]:        950 :                 if (item->state == TASK_DEAD)
    1379                 :         31 :                         continue;
    1380                 :            : 
    1381         [ +  + ]:        929 :                 if (item->state == TASK_HELPER)
    1382                 :         10 :                         continue;
    1383                 :            : 
    1384         [ +  - ]:        919 :                 if (parse_threads(item->pid.real, &item->threads, &item->nr_threads))
    1385                 :          0 :                         return -1;
    1386                 :            : 
    1387         [ +  + ]:       2308 :                 for (i = 0; i < item->nr_threads; i++) {
    1388                 :       1389 :                         pid = item->threads[i].real;
    1389                 :            : 
    1390         [ -  + ]:       1389 :                         if (ptrace(PTRACE_ATTACH, pid, 0, 0)) {
    1391                 :          0 :                                 pr_perror("Can't attach to %d", pid);
    1392                 :          0 :                                 return -1;
    1393                 :            :                         }
    1394                 :            : 
    1395         [ -  + ]:       1389 :                         if (wait4(pid, &status, __WALL, NULL) != pid) {
    1396                 :          0 :                                 pr_perror("waitpid() failed");
    1397                 :          0 :                                 return -1;
    1398                 :            :                         }
    1399                 :            : 
    1400         [ -  + ]:       1389 :                         if (ptrace(PTRACE_SYSCALL, pid, NULL, NULL)) {
    1401                 :          0 :                                 pr_perror("Unable to start %d", pid);
    1402                 :          0 :                                 return -1;
    1403                 :            :                         }
    1404                 :            :                 }
    1405                 :            :         }
    1406                 :            : 
    1407                 :            :         return 0;
    1408                 :            : }
    1409                 :            : 
    1410                 :        448 : static void finalize_restore(int status)
    1411                 :            : {
    1412                 :            :         struct pstree_item *item;
    1413                 :            : 
    1414         [ +  + ]:       1398 :         for_each_pstree_item(item) {
    1415                 :        950 :                 pid_t pid = item->pid.real;
    1416                 :            :                 struct parasite_ctl *ctl;
    1417                 :            :                 int i;
    1418                 :            : 
    1419         [ +  + ]:        950 :                 if (item->state == TASK_DEAD)
    1420                 :         21 :                         continue;
    1421                 :            : 
    1422         [ +  + ]:        929 :                 if (item->state == TASK_HELPER)
    1423                 :         10 :                         continue;
    1424                 :            : 
    1425         [ +  - ]:        919 :                 if (status  < 0)
    1426                 :            :                         goto detach;
    1427                 :            : 
    1428                 :            :                 /* Unmap the restorer blob */
    1429                 :        919 :                 ctl = parasite_prep_ctl(pid, NULL);
    1430         [ +  - ]:        919 :                 if (ctl == NULL)
    1431                 :            :                         goto detach;
    1432                 :            : 
    1433                 :        919 :                 parasite_unmap(ctl, (unsigned long) item->rst->munmap_restorer);
    1434                 :            : 
    1435         [ +  - ]:        919 :                 xfree(ctl);
    1436                 :            : 
    1437         [ +  + ]:        919 :                 if (item->state == TASK_STOPPED)
    1438                 :        919 :                         kill(item->pid.real, SIGSTOP);
    1439                 :            : detach:
    1440         [ +  + ]:       2308 :                 for (i = 0; i < item->nr_threads; i++) {
    1441                 :       1389 :                         pid = item->threads[i].real;
    1442         [ -  + ]:       1389 :                         if (pid < 0) {
    1443         [ #  # ]:          0 :                                 BUG_ON(status >= 0);
    1444                 :            :                                 break;
    1445                 :            :                         }
    1446                 :            : 
    1447         [ -  + ]:       1389 :                         if (ptrace(PTRACE_DETACH, pid, NULL, 0))
    1448                 :          0 :                                 pr_perror("Unable to execute %d", pid);
    1449                 :            :                 }
    1450                 :            :         }
    1451                 :        448 : }
    1452                 :            : 
    1453                 :       1012 : static int restore_root_task(struct pstree_item *init)
    1454                 :            : {
    1455                 :            :         int ret, fd;
    1456                 :            :         struct sigaction act, old_act;
    1457                 :            : 
    1458                 :            :         fd = open("/proc", O_DIRECTORY | O_RDONLY);
    1459         [ -  + ]:       1012 :         if (fd < 0) {
    1460                 :          0 :                 pr_perror("Unable to open /proc");
    1461                 :          0 :                 return -1;
    1462                 :            :         }
    1463                 :            : 
    1464                 :       1012 :         ret = install_service_fd(CR_PROC_FD_OFF, fd);
    1465                 :       1012 :         close(fd);
    1466         [ +  - ]:       1012 :         if (ret < 0)
    1467                 :            :                 return -1;
    1468                 :            : 
    1469                 :       1012 :         ret = sigaction(SIGCHLD, NULL, &act);
    1470         [ -  + ]:       1012 :         if (ret < 0) {
    1471                 :          0 :                 pr_perror("sigaction() failed");
    1472                 :          0 :                 return -1;
    1473                 :            :         }
    1474                 :            : 
    1475                 :       1012 :         act.sa_flags |= SA_NOCLDSTOP | SA_SIGINFO | SA_RESTART;
    1476                 :       1012 :         act.sa_sigaction = sigchld_handler;
    1477                 :       1012 :         sigemptyset(&act.sa_mask);
    1478                 :       1012 :         sigaddset(&act.sa_mask, SIGCHLD);
    1479                 :            : 
    1480                 :       1012 :         ret = sigaction(SIGCHLD, &act, &old_act);
    1481         [ -  + ]:       1012 :         if (ret < 0) {
    1482                 :          0 :                 pr_perror("sigaction() failed");
    1483                 :          0 :                 return -1;
    1484                 :            :         }
    1485                 :            : 
    1486                 :            :         /*
    1487                 :            :          * FIXME -- currently we assume that all the tasks live
    1488                 :            :          * in the same set of namespaces. This is done to debug
    1489                 :            :          * the ns contents dumping/restoring. Need to revisit
    1490                 :            :          * this later.
    1491                 :            :          */
    1492                 :            : 
    1493         [ +  + ]:       1012 :         if (init->pid.virt == INIT_PID) {
    1494         [ -  + ]:        420 :                 if (!(root_ns_mask & CLONE_NEWPID)) {
    1495                 :          0 :                         pr_err("This process tree can only be restored "
    1496                 :            :                                 "in a new pid namespace.\n"
    1497                 :            :                                 "criu should be re-executed with the "
    1498                 :            :                                 "\"--namespace pid\" option.\n");
    1499                 :          0 :                         return -1;
    1500                 :            :                 }
    1501         [ -  + ]:        592 :         } else  if (root_ns_mask & CLONE_NEWPID) {
    1502                 :          0 :                 pr_err("Can't restore pid namespace without the process init\n");
    1503                 :          0 :                 return -1;
    1504                 :            :         }
    1505                 :            : 
    1506                 :       1012 :         futex_set(&task_entries->nr_in_progress,
    1507                 :       1012 :                         stage_participants(CR_STATE_RESTORE_NS));
    1508                 :            : 
    1509                 :       1012 :         ret = fork_with_pid(init);
    1510         [ +  - ]:        448 :         if (ret < 0)
    1511                 :            :                 return -1;
    1512                 :            : 
    1513                 :        448 :         pr_info("Wait until namespaces are created\n");
    1514                 :            :         ret = restore_wait_inprogress_tasks();
    1515         [ +  - ]:        448 :         if (ret)
    1516                 :            :                 goto out;
    1517                 :            : 
    1518                 :        448 :         ret = run_scripts("setup-namespaces");
    1519         [ +  - ]:        448 :         if (ret)
    1520                 :            :                 goto out;
    1521                 :            : 
    1522                 :        448 :         timing_start(TIME_FORK);
    1523                 :            : 
    1524                 :        448 :         ret = restore_switch_stage(CR_STATE_FORKING);
    1525         [ +  - ]:        448 :         if (ret < 0)
    1526                 :            :                 goto out;
    1527                 :            : 
    1528                 :        448 :         timing_stop(TIME_FORK);
    1529                 :            : 
    1530                 :        448 :         ret = restore_switch_stage(CR_STATE_RESTORE);
    1531         [ +  - ]:        448 :         if (ret < 0)
    1532                 :            :                 goto out_kill;
    1533                 :            : 
    1534                 :        448 :         ret = restore_switch_stage(CR_STATE_RESTORE_SIGCHLD);
    1535         [ +  - ]:        448 :         if (ret < 0)
    1536                 :            :                 goto out_kill;
    1537                 :            : 
    1538                 :            :         /* Restore SIGCHLD here to skip SIGCHLD from a network sctip */
    1539                 :        448 :         ret = sigaction(SIGCHLD, &old_act, NULL);
    1540         [ -  + ]:        448 :         if (ret < 0) {
    1541                 :          0 :                 pr_perror("sigaction() failed");
    1542                 :          0 :                 goto out_kill;
    1543                 :            :         }
    1544                 :            : 
    1545                 :        448 :         ret = run_scripts("post-restore");
    1546         [ -  + ]:        448 :         if (ret != 0) {
    1547                 :          0 :                 pr_err("Aborting restore due to script ret code %d\n", ret);
    1548                 :          0 :                 timing_stop(TIME_RESTORE);
    1549                 :          0 :                 write_stats(RESTORE_STATS);
    1550                 :          0 :                 goto out_kill;
    1551                 :            :         }
    1552                 :            : 
    1553                 :            :         /* Unlock network before disabling repair mode on sockets */
    1554                 :        448 :         network_unlock();
    1555                 :            : 
    1556                 :            :         /*
    1557                 :            :          * -------------------------------------------------------------
    1558                 :            :          * Below this line nothing can fail, because network is unlocked
    1559                 :            :          */
    1560                 :            : 
    1561                 :        448 :         ret = restore_switch_stage(CR_STATE_RESTORE_CREDS);
    1562         [ -  + ]:        448 :         BUG_ON(ret);
    1563                 :            : 
    1564                 :        448 :         timing_stop(TIME_RESTORE);
    1565                 :            : 
    1566                 :        448 :         ret = attach_to_tasks();
    1567                 :            : 
    1568                 :        448 :         pr_info("Restore finished successfully. Resuming tasks.\n");
    1569                 :        448 :         futex_set_and_wake(&task_entries->start, CR_STATE_COMPLETE);
    1570                 :            : 
    1571         [ +  - ]:        448 :         if (ret == 0)
    1572                 :        448 :                 ret = parasite_stop_on_syscall(task_entries->nr_threads, __NR_rt_sigreturn);
    1573                 :            : 
    1574                 :            :         /*
    1575                 :            :          * finalize_restore() always detaches from processes and
    1576                 :            :          * they continue run through sigreturn.
    1577                 :            :          */
    1578                 :        448 :         finalize_restore(ret);
    1579                 :            : 
    1580                 :        448 :         write_stats(RESTORE_STATS);
    1581                 :            : 
    1582 [ -  + ][ #  # ]:        448 :         if (!opts.restore_detach && !opts.exec_cmd)
    1583                 :          0 :                 wait(NULL);
    1584                 :            : 
    1585                 :            :         return 0;
    1586                 :            : 
    1587                 :            : out_kill:
    1588                 :            :         /*
    1589                 :            :          * The processes can be killed only when all of them have been created,
    1590                 :            :          * otherwise an external proccesses can be killed.
    1591                 :            :          */
    1592         [ #  # ]:          0 :         if (root_ns_mask & CLONE_NEWPID) {
    1593                 :            :                 /* Kill init */
    1594         [ #  # ]:          0 :                 if (root_item->pid.real > 0)
    1595                 :          0 :                         kill(root_item->pid.real, SIGKILL);
    1596                 :            :         } else {
    1597                 :            :                 struct pstree_item *pi;
    1598                 :            : 
    1599         [ #  # ]:          0 :                 for_each_pstree_item(pi)
    1600         [ #  # ]:          0 :                         if (pi->pid.virt > 0)
    1601                 :          0 :                                 kill(pi->pid.virt, SIGKILL);
    1602                 :            :         }
    1603                 :            : 
    1604                 :            : out:
    1605                 :          0 :         __restore_switch_stage(CR_STATE_FAIL);
    1606                 :          0 :         pr_err("Restoring FAILED.\n");
    1607                 :          0 :         return 1;
    1608                 :            : }
    1609                 :            : 
    1610                 :       1012 : static int prepare_task_entries()
    1611                 :            : {
    1612                 :       1012 :         task_entries = mmap(NULL, TASK_ENTRIES_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
    1613         [ -  + ]:       1012 :         if (task_entries == MAP_FAILED) {
    1614                 :          0 :                 pr_perror("Can't map shmem");
    1615                 :          0 :                 return -1;
    1616                 :            :         }
    1617                 :       1012 :         task_entries->nr_threads = 0;
    1618                 :       1012 :         task_entries->nr_tasks = 0;
    1619                 :       1012 :         task_entries->nr_helpers = 0;
    1620                 :            :         futex_set(&task_entries->start, CR_STATE_RESTORE_NS);
    1621                 :            :         mutex_init(&task_entries->zombie_lock);
    1622                 :            : 
    1623                 :       1012 :         return 0;
    1624                 :            : }
    1625                 :            : 
    1626                 :       1012 : int cr_restore_tasks(void)
    1627                 :            : {
    1628                 :            :         int ret = -1;
    1629                 :            : 
    1630         [ +  - ]:       1012 :         if (cr_plugin_init())
    1631                 :            :                 return -1;
    1632                 :            : 
    1633         [ +  - ]:       1012 :         if (check_img_inventory() < 0)
    1634                 :            :                 goto err;
    1635                 :            : 
    1636         [ +  - ]:       1012 :         if (init_stats(RESTORE_STATS))
    1637                 :            :                 goto err;
    1638                 :            : 
    1639         [ +  - ]:       1012 :         if (kerndat_init_rst())
    1640                 :            :                 goto err;
    1641                 :            : 
    1642                 :       1012 :         timing_start(TIME_RESTORE);
    1643                 :            : 
    1644         [ +  - ]:       1012 :         if (cpu_init() < 0)
    1645                 :            :                 goto err;
    1646                 :            : 
    1647         [ +  - ]:       1012 :         if (vdso_init())
    1648                 :            :                 goto err;
    1649                 :            : 
    1650         [ +  - ]:       1012 :         if (prepare_task_entries() < 0)
    1651                 :            :                 goto err;
    1652                 :            : 
    1653         [ +  - ]:       1012 :         if (prepare_pstree() < 0)
    1654                 :            :                 goto err;
    1655                 :            : 
    1656         [ +  - ]:       1012 :         if (crtools_prepare_shared() < 0)
    1657                 :            :                 goto err;
    1658                 :            : 
    1659                 :       1012 :         ret = restore_root_task(root_item);
    1660                 :            : err:
    1661                 :        448 :         cr_plugin_fini();
    1662                 :        448 :         return ret;
    1663                 :            : }
    1664                 :            : 
    1665                 :        694 : static long restorer_get_vma_hint(pid_t pid, struct list_head *tgt_vma_list,
    1666                 :            :                 struct list_head *self_vma_list, long vma_len)
    1667                 :            : {
    1668                 :            :         struct vma_area *t_vma, *s_vma;
    1669                 :            :         long prev_vma_end = 0;
    1670                 :            :         struct vma_area end_vma;
    1671                 :            :         VmaEntry end_e;
    1672                 :            : 
    1673                 :        347 :         end_vma.e = &end_e;
    1674                 :        347 :         end_e.start = end_e.end = TASK_SIZE;
    1675                 :            :         prev_vma_end = PAGE_SIZE * 0x10; /* CONFIG_LSM_MMAP_MIN_ADDR=65536 */
    1676                 :            : 
    1677                 :        347 :         s_vma = list_first_entry(self_vma_list, struct vma_area, list);
    1678                 :        347 :         t_vma = list_first_entry(tgt_vma_list, struct vma_area, list);
    1679                 :            : 
    1680                 :            :         while (1) {
    1681         [ -  + ]:        347 :                 if (prev_vma_end + vma_len > s_vma->e->start) {
    1682         [ #  # ]:          0 :                         if (s_vma->list.next == self_vma_list) {
    1683                 :            :                                 s_vma = &end_vma;
    1684                 :          0 :                                 continue;
    1685                 :            :                         }
    1686         [ #  # ]:          0 :                         if (s_vma == &end_vma)
    1687                 :            :                                 break;
    1688         [ #  # ]:          0 :                         if (prev_vma_end < s_vma->e->end)
    1689                 :          0 :                                 prev_vma_end = s_vma->e->end;
    1690                 :            :                         s_vma = list_entry(s_vma->list.next, struct vma_area, list);
    1691                 :          0 :                         continue;
    1692                 :            :                 }
    1693                 :            : 
    1694         [ -  + ]:        347 :                 if (prev_vma_end + vma_len > t_vma->e->start) {
    1695         [ #  # ]:          0 :                         if (t_vma->list.next == tgt_vma_list) {
    1696                 :            :                                 t_vma = &end_vma;
    1697                 :          0 :                                 continue;
    1698                 :            :                         }
    1699         [ #  # ]:          0 :                         if (t_vma == &end_vma)
    1700                 :            :                                 break;
    1701         [ #  # ]:          0 :                         if (prev_vma_end < t_vma->e->end)
    1702                 :          0 :                                 prev_vma_end = t_vma->e->end;
    1703                 :            :                         t_vma = list_entry(t_vma->list.next, struct vma_area, list);
    1704                 :          0 :                         continue;
    1705                 :            :                 }
    1706                 :            : 
    1707                 :            :                 return prev_vma_end;
    1708                 :            :         }
    1709                 :            : 
    1710                 :            :         return -1;
    1711                 :            : }
    1712                 :            : 
    1713                 :            : static inline int timeval_valid(struct timeval *tv)
    1714                 :            : {
    1715 [ +  - ][ -  + ]:         12 :         return (tv->tv_sec >= 0) && ((unsigned long)tv->tv_usec < USEC_PER_SEC);
         [ +  - ][ -  + ]
    1716                 :            : }
    1717                 :            : 
    1718                 :       1041 : static inline int decode_itimer(char *n, ItimerEntry *ie, struct itimerval *val)
    1719                 :            : {
    1720 [ +  - ][ +  + ]:       1041 :         if (ie->isec == 0 && ie->iusec == 0) {
    1721                 :            :                 memzero_p(val);
    1722                 :       1035 :                 return 0;
    1723                 :            :         }
    1724                 :            : 
    1725                 :          6 :         val->it_interval.tv_sec = ie->isec;
    1726                 :          6 :         val->it_interval.tv_usec = ie->iusec;
    1727                 :            : 
    1728         [ -  + ]:          6 :         if (!timeval_valid(&val->it_interval)) {
    1729                 :          0 :                 pr_err("Invalid timer interval\n");
    1730                 :          0 :                 return -1;
    1731                 :            :         }
    1732                 :            : 
    1733 [ +  - ][ -  + ]:          6 :         if (ie->vsec == 0 && ie->vusec == 0) {
    1734                 :            :                 /*
    1735                 :            :                  * Remaining time was too short. Set it to
    1736                 :            :                  * interval to make the timer armed and work.
    1737                 :            :                  */
    1738                 :          0 :                 val->it_value.tv_sec = ie->isec;
    1739                 :          0 :                 val->it_value.tv_usec = ie->iusec;
    1740                 :            :         } else {
    1741                 :          6 :                 val->it_value.tv_sec = ie->vsec;
    1742                 :          6 :                 val->it_value.tv_usec = ie->vusec;
    1743                 :            :         }
    1744                 :            : 
    1745         [ -  + ]:          6 :         if (!timeval_valid(&val->it_value)) {
    1746                 :          0 :                 pr_err("Invalid timer value\n");
    1747                 :          0 :                 return -1;
    1748                 :            :         }
    1749                 :            : 
    1750                 :          6 :         pr_info("Restored %s timer to %ld.%ld -> %ld.%ld\n", n,
    1751                 :            :                         val->it_value.tv_sec, val->it_value.tv_usec,
    1752                 :            :                         val->it_interval.tv_sec, val->it_interval.tv_usec);
    1753                 :            : 
    1754                 :          6 :         return 0;
    1755                 :            : }
    1756                 :            : 
    1757                 :            : /*
    1758                 :            :  * Legacy itimers restore from CR_FD_ITIMERS
    1759                 :            :  */
    1760                 :            : 
    1761                 :          0 : static int prepare_itimers_from_fd(int pid, struct task_restore_args *args)
    1762                 :            : {
    1763                 :            :         int fd, ret = -1;
    1764                 :            :         ItimerEntry *ie;
    1765                 :            : 
    1766                 :          0 :         fd = open_image(CR_FD_ITIMERS, O_RSTR, pid);
    1767         [ #  # ]:          0 :         if (fd < 0)
    1768                 :            :                 return fd;
    1769                 :            : 
    1770                 :          0 :         ret = pb_read_one(fd, &ie, PB_ITIMER);
    1771         [ #  # ]:          0 :         if (ret < 0)
    1772                 :            :                 goto out;
    1773                 :          0 :         ret = decode_itimer("real", ie, &args->itimers[0]);
    1774                 :          0 :         itimer_entry__free_unpacked(ie, NULL);
    1775         [ #  # ]:          0 :         if (ret < 0)
    1776                 :            :                 goto out;
    1777                 :            : 
    1778                 :          0 :         ret = pb_read_one(fd, &ie, PB_ITIMER);
    1779         [ #  # ]:          0 :         if (ret < 0)
    1780                 :            :                 goto out;
    1781                 :          0 :         ret = decode_itimer("virt", ie, &args->itimers[1]);
    1782                 :          0 :         itimer_entry__free_unpacked(ie, NULL);
    1783         [ #  # ]:          0 :         if (ret < 0)
    1784                 :            :                 goto out;
    1785                 :            : 
    1786                 :          0 :         ret = pb_read_one(fd, &ie, PB_ITIMER);
    1787         [ #  # ]:          0 :         if (ret < 0)
    1788                 :            :                 goto out;
    1789                 :          0 :         ret = decode_itimer("prof", ie, &args->itimers[2]);
    1790                 :          0 :         itimer_entry__free_unpacked(ie, NULL);
    1791                 :            :         if (ret < 0)
    1792                 :            :                 goto out;
    1793                 :            : out:
    1794                 :          0 :         close_safe(&fd);
    1795                 :          0 :         return ret;
    1796                 :            : }
    1797                 :            : 
    1798                 :        694 : static int prepare_itimers(int pid, CoreEntry *core, struct task_restore_args *args)
    1799                 :            : {
    1800                 :            :         int ret = 0;
    1801                 :        347 :         TaskTimersEntry *tte = core->tc->timers;
    1802                 :            : 
    1803         [ -  + ]:        347 :         if (!tte)
    1804                 :          0 :                 return prepare_itimers_from_fd(pid, args);
    1805                 :            : 
    1806                 :        347 :         ret |= decode_itimer("real", tte->real, &args->itimers[0]);
    1807                 :        347 :         ret |= decode_itimer("virt", tte->virt, &args->itimers[1]);
    1808                 :        347 :         ret |= decode_itimer("prof", tte->prof, &args->itimers[2]);
    1809                 :            : 
    1810                 :            :         return ret;
    1811                 :            : }
    1812                 :            : 
    1813                 :            : static inline int timespec_valid(struct timespec *ts)
    1814                 :            : {
    1815 [ +  - ][ -  + ]:          8 :         return (ts->tv_sec >= 0) && ((unsigned long)ts->tv_nsec < NSEC_PER_SEC);
         [ +  - ][ -  + ]
    1816                 :            : }
    1817                 :            : 
    1818                 :          4 : static inline int decode_posix_timer(PosixTimerEntry *pte,
    1819                 :            :                 struct restore_posix_timer *pt)
    1820                 :            : {
    1821                 :          4 :         pt->val.it_interval.tv_sec = pte->isec;
    1822                 :          4 :         pt->val.it_interval.tv_nsec = pte->insec;
    1823                 :            : 
    1824         [ -  + ]:          4 :         if (!timespec_valid(&pt->val.it_interval)) {
    1825                 :          0 :                 pr_err("Invalid timer interval(posix)\n");
    1826                 :          0 :                 return -1;
    1827                 :            :         }
    1828                 :            : 
    1829 [ -  + ][ #  # ]:          4 :         if (pte->vsec == 0 && pte->vnsec == 0) {
    1830                 :            :                 // Remaining time was too short. Set it to
    1831                 :            :                 // interval to make the timer armed and work.
    1832                 :          0 :                 pt->val.it_value.tv_sec = pte->isec;
    1833                 :          0 :                 pt->val.it_value.tv_nsec = pte->insec;
    1834                 :            :         } else {
    1835                 :          4 :                 pt->val.it_value.tv_sec = pte->vsec;
    1836                 :          4 :                 pt->val.it_value.tv_nsec = pte->vnsec;
    1837                 :            :         }
    1838                 :            : 
    1839         [ -  + ]:          4 :         if (!timespec_valid(&pt->val.it_value)) {
    1840                 :          0 :                 pr_err("Invalid timer value(posix)\n");
    1841                 :          0 :                 return -1;
    1842                 :            :         }
    1843                 :            : 
    1844                 :          4 :         pt->spt.it_id = pte->it_id;
    1845                 :          4 :         pt->spt.clock_id = pte->clock_id;
    1846                 :          4 :         pt->spt.si_signo = pte->si_signo;
    1847                 :          4 :         pt->spt.it_sigev_notify = pte->it_sigev_notify;
    1848                 :          8 :         pt->spt.sival_ptr = decode_pointer(pte->sival_ptr);
    1849                 :          4 :         pt->overrun = pte->overrun;
    1850                 :            : 
    1851                 :          4 :         return 0;
    1852                 :            : }
    1853                 :            : 
    1854                 :          2 : static int cmp_posix_timer_proc_id(const void *p1, const void *p2)
    1855                 :            : {
    1856                 :          2 :         return ((struct restore_posix_timer *)p1)->spt.it_id - ((struct restore_posix_timer *)p2)->spt.it_id;
    1857                 :            : }
    1858                 :            : 
    1859                 :            : static unsigned long posix_timers_cpos;
    1860                 :            : static unsigned int posix_timers_nr;
    1861                 :            : 
    1862                 :        347 : static void sort_posix_timers(void)
    1863                 :            : {
    1864                 :            :         /*
    1865                 :            :          * This is required for restorer's create_posix_timers(),
    1866                 :            :          * it will probe them one-by-one for the desired ID, since
    1867                 :            :          * kernel doesn't provide another API for timer creation
    1868                 :            :          * with given ID.
    1869                 :            :          */
    1870                 :            : 
    1871         [ +  + ]:        347 :         if (posix_timers_nr > 0)
    1872                 :          2 :                 qsort(rst_mem_remap_ptr(posix_timers_cpos, RM_PRIVATE),
    1873                 :            :                                 posix_timers_nr,
    1874                 :            :                                 sizeof(struct restore_posix_timer),
    1875                 :            :                                 cmp_posix_timer_proc_id);
    1876                 :        347 : }
    1877                 :            : 
    1878                 :            : /*
    1879                 :            :  * Legacy posix timers restoration from CR_FD_POSIX_TIMERS
    1880                 :            :  */
    1881                 :            : 
    1882                 :          0 : static int prepare_posix_timers_from_fd(int pid)
    1883                 :            : {
    1884                 :          0 :         int fd = -1;
    1885                 :            :         int ret = -1;
    1886                 :            :         struct restore_posix_timer *t;
    1887                 :            : 
    1888                 :          0 :         fd = open_image(CR_FD_POSIX_TIMERS, O_RSTR, pid);
    1889         [ #  # ]:          0 :         if (fd < 0) {
    1890         [ #  # ]:          0 :                 if (errno == ENOENT) /* backward compatibility */
    1891                 :            :                         return 0;
    1892                 :            :                 else
    1893                 :          0 :                         return fd;
    1894                 :            :         }
    1895                 :            : 
    1896                 :            :         while (1) {
    1897                 :            :                 PosixTimerEntry *pte;
    1898                 :            : 
    1899                 :          0 :                 ret = pb_read_one_eof(fd, &pte, PB_POSIX_TIMER);
    1900         [ #  # ]:          0 :                 if (ret <= 0)
    1901                 :            :                         break;
    1902                 :            : 
    1903                 :          0 :                 t = rst_mem_alloc(sizeof(struct restore_posix_timer), RM_PRIVATE);
    1904         [ #  # ]:          0 :                 if (!t)
    1905                 :            :                         break;
    1906                 :            : 
    1907                 :          0 :                 ret = decode_posix_timer(pte, t);
    1908         [ #  # ]:          0 :                 if (ret < 0)
    1909                 :            :                         break;
    1910                 :            : 
    1911                 :          0 :                 posix_timer_entry__free_unpacked(pte, NULL);
    1912                 :          0 :                 posix_timers_nr++;
    1913                 :          0 :         }
    1914                 :            : 
    1915                 :          0 :         close_safe(&fd);
    1916         [ #  # ]:          0 :         if (!ret)
    1917                 :          0 :                 sort_posix_timers();
    1918                 :            : 
    1919                 :          0 :         return ret;
    1920                 :            : }
    1921                 :            : 
    1922                 :        694 : static int prepare_posix_timers(int pid, CoreEntry *core)
    1923                 :            : {
    1924                 :            :         int i, ret = -1;
    1925                 :        347 :         TaskTimersEntry *tte = core->tc->timers;
    1926                 :            :         struct restore_posix_timer *t;
    1927                 :            : 
    1928                 :        347 :         posix_timers_cpos = rst_mem_cpos(RM_PRIVATE);
    1929                 :            : 
    1930         [ -  + ]:        347 :         if (!tte)
    1931                 :          0 :                 return prepare_posix_timers_from_fd(pid);
    1932                 :            : 
    1933                 :        347 :         posix_timers_nr = tte->n_posix;
    1934         [ +  + ]:        351 :         for (i = 0; i < posix_timers_nr; i++) {
    1935                 :          4 :                 t = rst_mem_alloc(sizeof(struct restore_posix_timer), RM_PRIVATE);
    1936         [ +  - ]:          4 :                 if (!t)
    1937                 :            :                         goto out;
    1938                 :            : 
    1939         [ +  - ]:          4 :                 if (decode_posix_timer(tte->posix[i], t))
    1940                 :            :                         goto out;
    1941                 :            :         }
    1942                 :            : 
    1943                 :            :         ret = 0;
    1944                 :        347 :         sort_posix_timers();
    1945                 :            : out:
    1946                 :            :         return ret;
    1947                 :            : }
    1948                 :            : 
    1949                 :            : static inline int verify_cap_size(CredsEntry *ce)
    1950                 :            : {
    1951 [ +  - ][ +  - ]:        347 :         return ((ce->n_cap_inh == CR_CAP_SIZE) && (ce->n_cap_eff == CR_CAP_SIZE) &&
    1952 [ +  - ][ -  + ]:        694 :                 (ce->n_cap_prm == CR_CAP_SIZE) && (ce->n_cap_bnd == CR_CAP_SIZE));
    1953                 :            : }
    1954                 :            : 
    1955                 :        347 : static int prepare_creds(int pid, struct task_restore_args *args)
    1956                 :            : {
    1957                 :            :         int fd, ret;
    1958                 :            :         CredsEntry *ce;
    1959                 :            : 
    1960                 :        347 :         fd = open_image(CR_FD_CREDS, O_RSTR, pid);
    1961         [ +  - ]:        347 :         if (fd < 0)
    1962                 :            :                 return fd;
    1963                 :            : 
    1964                 :        347 :         ret = pb_read_one(fd, &ce, PB_CREDS);
    1965                 :        347 :         close_safe(&fd);
    1966                 :            : 
    1967         [ +  - ]:        347 :         if (ret < 0)
    1968                 :            :                 return ret;
    1969         [ -  + ]:        347 :         if (!verify_cap_size(ce)) {
    1970                 :          0 :                 pr_err("Caps size mismatch %d %d %d %d\n",
    1971                 :            :                        (int)ce->n_cap_inh, (int)ce->n_cap_eff,
    1972                 :            :                        (int)ce->n_cap_prm, (int)ce->n_cap_bnd);
    1973                 :          0 :                 return -1;
    1974                 :            :         }
    1975                 :            : 
    1976         [ +  - ]:        347 :         if (!may_restore(ce))
    1977                 :            :                 return -1;
    1978                 :            : 
    1979                 :        347 :         args->creds = *ce;
    1980                 :        347 :         args->creds.cap_inh = args->cap_inh;
    1981                 :        347 :         memcpy(args->cap_inh, ce->cap_inh, sizeof(args->cap_inh));
    1982                 :        347 :         args->creds.cap_eff = args->cap_eff;
    1983                 :        347 :         memcpy(args->cap_eff, ce->cap_eff, sizeof(args->cap_eff));
    1984                 :        347 :         args->creds.cap_prm = args->cap_prm;
    1985                 :        347 :         memcpy(args->cap_prm, ce->cap_prm, sizeof(args->cap_prm));
    1986                 :        347 :         args->creds.cap_bnd = args->cap_bnd;
    1987                 :        347 :         memcpy(args->cap_bnd, ce->cap_bnd, sizeof(args->cap_bnd));
    1988                 :            : 
    1989                 :            :         /*
    1990                 :            :          * We can set supplementary groups here. This won't affect any
    1991                 :            :          * permission checks for us (we're still root) and will not be
    1992                 :            :          * reset by subsequent creds changes in restorer.
    1993                 :            :          */
    1994                 :            : 
    1995                 :            :         BUILD_BUG_ON(sizeof(*ce->groups) != sizeof(gid_t));
    1996         [ -  + ]:        347 :         if (setgroups(ce->n_groups, ce->groups) < 0) {
    1997                 :          0 :                 pr_perror("Can't set supplementary groups");
    1998                 :          0 :                 return -1;
    1999                 :            :         }
    2000                 :            : 
    2001                 :        347 :         creds_entry__free_unpacked(ce, NULL);
    2002                 :            : 
    2003                 :        347 :         args->cap_last_cap = kern_last_cap;
    2004                 :            : 
    2005                 :            :         /* XXX -- validate creds here? */
    2006                 :            : 
    2007                 :        347 :         return 0;
    2008                 :            : }
    2009                 :            : 
    2010                 :        347 : static int prepare_mm(pid_t pid, struct task_restore_args *args)
    2011                 :            : {
    2012                 :            :         int exe_fd, i, ret = -1;
    2013                 :        347 :         MmEntry *mm = current->rst->mm;
    2014                 :            : 
    2015                 :        347 :         args->mm = *mm;
    2016                 :        347 :         args->mm.n_mm_saved_auxv = 0;
    2017                 :        347 :         args->mm.mm_saved_auxv = NULL;
    2018                 :            : 
    2019         [ -  + ]:        347 :         if (mm->n_mm_saved_auxv > AT_VECTOR_SIZE) {
    2020                 :          0 :                 pr_err("Image corrupted on pid %d\n", pid);
    2021                 :          0 :                 goto out;
    2022                 :            :         }
    2023                 :            : 
    2024                 :        347 :         args->mm_saved_auxv_size = mm->n_mm_saved_auxv*sizeof(auxv_t);
    2025         [ +  + ]:      13533 :         for (i = 0; i < mm->n_mm_saved_auxv; ++i) {
    2026                 :      13186 :                 args->mm_saved_auxv[i] = (auxv_t)mm->mm_saved_auxv[i];
    2027                 :            :         }
    2028                 :            : 
    2029                 :        347 :         exe_fd = open_reg_by_id(mm->exe_file_id);
    2030         [ +  - ]:        347 :         if (exe_fd < 0)
    2031                 :            :                 goto out;
    2032                 :            : 
    2033                 :        347 :         args->fd_exe_link = exe_fd;
    2034                 :            :         ret = 0;
    2035                 :            : out:
    2036                 :        347 :         return ret;
    2037                 :            : }
    2038                 :            : 
    2039                 :            : static void *restorer;
    2040                 :            : static unsigned long restorer_len;
    2041                 :            : 
    2042                 :        354 : static int prepare_restorer_blob(void)
    2043                 :            : {
    2044                 :            :         /*
    2045                 :            :          * We map anonymous mapping, not mremap the restorer itself later.
    2046                 :            :          * Otherwise the restorer vma would be tied to criu binary which
    2047                 :            :          * in turn will lead to set-exe-file prctl to fail with EBUSY.
    2048                 :            :          */
    2049                 :            : 
    2050                 :        354 :         restorer_len = round_up(sizeof(restorer_blob), PAGE_SIZE);
    2051                 :        354 :         restorer = mmap(NULL, restorer_len,
    2052                 :            :                         PROT_READ | PROT_WRITE | PROT_EXEC,
    2053                 :            :                         MAP_PRIVATE | MAP_ANON, 0, 0);
    2054         [ -  + ]:        354 :         if (restorer == MAP_FAILED) {
    2055                 :          0 :                 pr_perror("Can't map restorer code");
    2056                 :          0 :                 return -1;
    2057                 :            :         }
    2058                 :            : 
    2059                 :            :         memcpy(restorer, &restorer_blob, sizeof(restorer_blob));
    2060                 :        354 :         return 0;
    2061                 :            : }
    2062                 :            : 
    2063                 :        347 : static int remap_restorer_blob(void *addr)
    2064                 :            : {
    2065                 :            :         void *mem;
    2066                 :            : 
    2067                 :        347 :         mem = mremap(restorer, restorer_len, restorer_len,
    2068                 :            :                         MREMAP_FIXED | MREMAP_MAYMOVE, addr);
    2069         [ -  + ]:        347 :         if (mem != addr) {
    2070                 :          0 :                 pr_perror("Can't remap restorer blob");
    2071                 :          0 :                 return -1;
    2072                 :            :         }
    2073                 :            : 
    2074                 :            :         return 0;
    2075                 :            : }
    2076                 :            : 
    2077                 :        582 : static int validate_sched_parm(struct rst_sched_param *sp)
    2078                 :            : {
    2079         [ +  - ]:        582 :         if ((sp->nice < -20) || (sp->nice > 19))
    2080                 :            :                 return 0;
    2081                 :            : 
    2082 [ +  - ][ +  + ]:        582 :         switch (sp->policy) {
                 [ -  + ]
    2083                 :            :         case SCHED_RR:
    2084                 :            :         case SCHED_FIFO:
    2085                 :          2 :                 return ((sp->prio > 0) && (sp->prio < 100));
    2086                 :            :         case SCHED_IDLE:
    2087                 :            :         case SCHED_OTHER:
    2088                 :            :         case SCHED_BATCH:
    2089                 :        580 :                 return sp->prio == 0;
    2090                 :            :         }
    2091                 :            : 
    2092                 :            :         return 0;
    2093                 :            : }
    2094                 :            : 
    2095                 :        582 : static int prep_sched_info(struct rst_sched_param *sp, ThreadCoreEntry *tc)
    2096                 :            : {
    2097         [ -  + ]:        582 :         if (!tc->has_sched_policy) {
    2098                 :          0 :                 sp->policy = SCHED_OTHER;
    2099                 :          0 :                 sp->nice = 0;
    2100                 :          0 :                 return 0;
    2101                 :            :         }
    2102                 :            : 
    2103                 :        582 :         sp->policy = tc->sched_policy;
    2104                 :        582 :         sp->nice = tc->sched_nice;
    2105                 :        582 :         sp->prio = tc->sched_prio;
    2106                 :            : 
    2107         [ -  + ]:        582 :         if (!validate_sched_parm(sp)) {
    2108                 :          0 :                 pr_err("Inconsistent sched params received (%d.%d.%d)\n",
    2109                 :            :                                 sp->policy, sp->nice, sp->prio);
    2110                 :          0 :                 return -1;
    2111                 :            :         }
    2112                 :            : 
    2113                 :            :         return 0;
    2114                 :            : }
    2115                 :            : 
    2116                 :            : static unsigned long decode_rlim(u_int64_t ival)
    2117                 :            : {
    2118                 :            :         return ival == -1 ? RLIM_INFINITY : ival;
    2119                 :            : }
    2120                 :            : 
    2121                 :            : static unsigned long rlims_cpos;
    2122                 :            : static unsigned int rlims_nr;
    2123                 :            : 
    2124                 :            : /*
    2125                 :            :  * Legacy rlimits restore from CR_FD_RLIMIT
    2126                 :            :  */
    2127                 :            : 
    2128                 :          0 : static int prepare_rlimits_from_fd(int pid)
    2129                 :            : {
    2130                 :            :         struct rlimit *r;
    2131                 :            :         int fd, ret;
    2132                 :            : 
    2133                 :            :         /*
    2134                 :            :          * Old image -- read from the file.
    2135                 :            :          */
    2136                 :          0 :         fd = open_image(CR_FD_RLIMIT, O_RSTR | O_OPT, pid);
    2137         [ #  # ]:          0 :         if (fd < 0) {
    2138         [ #  # ]:          0 :                 if (fd == -ENOENT) {
    2139                 :          0 :                         pr_info("Skip rlimits for %d\n", pid);
    2140                 :          0 :                         return 0;
    2141                 :            :                 }
    2142                 :            : 
    2143                 :            :                 return -1;
    2144                 :            :         }
    2145                 :            : 
    2146                 :            :         while (1) {
    2147                 :            :                 RlimitEntry *re;
    2148                 :            : 
    2149                 :          0 :                 ret = pb_read_one_eof(fd, &re, PB_RLIMIT);
    2150         [ #  # ]:          0 :                 if (ret <= 0)
    2151                 :            :                         break;
    2152                 :            : 
    2153                 :          0 :                 r = rst_mem_alloc(sizeof(*r), RM_PRIVATE);
    2154         [ #  # ]:          0 :                 if (!r) {
    2155                 :          0 :                         pr_err("Can't allocate memory for resource %d\n",
    2156                 :            :                                rlims_nr);
    2157                 :          0 :                         return -1;
    2158                 :            :                 }
    2159                 :            : 
    2160                 :          0 :                 r->rlim_cur = decode_rlim(re->cur);
    2161                 :          0 :                 r->rlim_max = decode_rlim(re->max);
    2162         [ #  # ]:          0 :                 if (r->rlim_cur > r->rlim_max) {
    2163                 :          0 :                         pr_err("Can't restore cur > max for %d.%d\n",
    2164                 :            :                                         pid, rlims_nr);
    2165                 :          0 :                         r->rlim_cur = r->rlim_max;
    2166                 :            :                 }
    2167                 :            : 
    2168                 :          0 :                 rlimit_entry__free_unpacked(re, NULL);
    2169                 :            : 
    2170                 :          0 :                 rlims_nr++;
    2171                 :          0 :         }
    2172                 :            : 
    2173                 :          0 :         close(fd);
    2174                 :            : 
    2175                 :          0 :         return 0;
    2176                 :            : }
    2177                 :            : 
    2178                 :        694 : static int prepare_rlimits(int pid, CoreEntry *core)
    2179                 :            : {
    2180                 :            :         int i;
    2181                 :        347 :         TaskRlimitsEntry *rls = core->tc->rlimits;
    2182                 :            :         struct rlimit *r;
    2183                 :            : 
    2184                 :        347 :         rlims_cpos = rst_mem_cpos(RM_PRIVATE);
    2185                 :            : 
    2186         [ +  - ]:        347 :         if (!rls)
    2187                 :          0 :                 return prepare_rlimits_from_fd(pid);
    2188                 :            : 
    2189         [ +  + ]:       5899 :         for (i = 0; i < rls->n_rlimits; i++) {
    2190                 :       5552 :                 r = rst_mem_alloc(sizeof(*r), RM_PRIVATE);
    2191         [ -  + ]:       5552 :                 if (!r) {
    2192                 :          0 :                         pr_err("Can't allocate memory for resource %d\n", i);
    2193                 :            :                         return -1;
    2194                 :            :                 }
    2195                 :            : 
    2196                 :       5552 :                 r->rlim_cur = decode_rlim(rls->rlimits[i]->cur);
    2197                 :       5552 :                 r->rlim_max = decode_rlim(rls->rlimits[i]->max);
    2198                 :            : 
    2199         [ -  + ]:       5552 :                 if (r->rlim_cur > r->rlim_max) {
    2200                 :          0 :                         pr_warn("Can't restore cur > max for %d.%d\n", pid, i);
    2201                 :          0 :                         r->rlim_cur = r->rlim_max;
    2202                 :            :                 }
    2203                 :            :         }
    2204                 :            : 
    2205                 :        347 :         rlims_nr = rls->n_rlimits;
    2206                 :            :         return 0;
    2207                 :            : }
    2208                 :            : 
    2209                 :        929 : static int open_signal_image(int type, pid_t pid, unsigned int *nr)
    2210                 :            : {
    2211                 :            :         int fd, ret;
    2212                 :            : 
    2213                 :        929 :         fd = open_image(type, O_RSTR | O_OPT, pid);
    2214         [ -  + ]:        929 :         if (fd < 0) {
    2215         [ #  # ]:          0 :                 if (fd == -ENOENT) /* backward compatibility */
    2216                 :            :                         return 0;
    2217                 :            :                 else
    2218                 :          0 :                         return -1;
    2219                 :            :         }
    2220                 :            : 
    2221                 :        929 :         *nr = 0;
    2222                 :            :         while (1) {
    2223                 :            :                 SiginfoEntry *sie;
    2224                 :            :                 siginfo_t *info, *t;
    2225                 :            : 
    2226                 :       1070 :                 ret = pb_read_one_eof(fd, &sie, PB_SIGINFO);
    2227         [ +  + ]:       1070 :                 if (ret <= 0)
    2228                 :            :                         break;
    2229         [ -  + ]:        141 :                 if (sie->siginfo.len != sizeof(siginfo_t)) {
    2230                 :          0 :                         pr_err("Unknown image format");
    2231                 :            :                         ret = -1;
    2232                 :          0 :                         break;
    2233                 :            :                 }
    2234                 :        141 :                 info = (siginfo_t *) sie->siginfo.data;
    2235                 :        141 :                 t = rst_mem_alloc(sizeof(siginfo_t), RM_PRIVATE);
    2236         [ +  - ]:        141 :                 if (!t) {
    2237                 :            :                         ret = -1;
    2238                 :            :                         break;
    2239                 :            :                 }
    2240                 :            : 
    2241                 :            :                 memcpy(t, info, sizeof(*info));
    2242                 :        141 :                 (*nr)++;
    2243                 :            : 
    2244                 :        141 :                 siginfo_entry__free_unpacked(sie, NULL);
    2245                 :        141 :         }
    2246                 :            : 
    2247                 :        929 :         close(fd);
    2248                 :            : 
    2249                 :        929 :         return ret ? : 0;
    2250                 :            : }
    2251                 :            : 
    2252                 :            : static unsigned long siginfo_cpos;
    2253                 :            : static unsigned int siginfo_nr, *siginfo_priv_nr;
    2254                 :            : 
    2255                 :        347 : static int prepare_signals(int pid)
    2256                 :            : {
    2257                 :            :         int ret = -1, i;
    2258                 :            : 
    2259                 :        347 :         siginfo_cpos = rst_mem_cpos(RM_PRIVATE);
    2260         [ -  + ]:        347 :         siginfo_priv_nr = xmalloc(sizeof(int) * current->nr_threads);
    2261         [ +  - ]:        347 :         if (siginfo_priv_nr == NULL)
    2262                 :            :                 goto out;
    2263                 :            : 
    2264                 :        347 :         ret = open_signal_image(CR_FD_SIGNAL, pid, &siginfo_nr);
    2265         [ +  - ]:        347 :         if (ret < 0)
    2266                 :            :                 goto out;
    2267                 :            : 
    2268         [ +  + ]:        929 :         for (i = 0; i < current->nr_threads; i++) {
    2269                 :        582 :                 ret = open_signal_image(CR_FD_PSIGNAL,
    2270                 :       1164 :                                 current->threads[i].virt, &siginfo_priv_nr[i]);
    2271         [ +  - ]:        582 :                 if (ret < 0)
    2272                 :            :                         goto out;
    2273                 :            :         }
    2274                 :            : out:
    2275                 :        347 :         return ret;
    2276                 :            : }
    2277                 :            : 
    2278                 :            : extern void __gcov_flush(void) __attribute__((weak));
    2279                 :          0 : void __gcov_flush(void) {}
    2280                 :            : 
    2281                 :        347 : static int sigreturn_restore(pid_t pid, CoreEntry *core)
    2282                 :            : {
    2283                 :            :         void *mem = MAP_FAILED;
    2284                 :            :         void *restore_thread_exec_start;
    2285                 :            :         void *restore_task_exec_start;
    2286                 :            : 
    2287                 :            :         long new_sp, exec_mem_hint;
    2288                 :            :         long ret;
    2289                 :            : 
    2290                 :            :         long restore_bootstrap_len;
    2291                 :            : 
    2292                 :            :         struct task_restore_args *task_args;
    2293                 :            :         struct thread_restore_args *thread_args;
    2294                 :            :         long args_len;
    2295                 :            : 
    2296                 :            :         struct vma_area *vma;
    2297                 :            :         unsigned long tgt_vmas;
    2298                 :            : 
    2299                 :            :         void *tcp_socks_mem;
    2300                 :            :         unsigned long tcp_socks;
    2301                 :            : 
    2302                 :            :         unsigned long vdso_rt_vma_size = 0;
    2303                 :            :         unsigned long vdso_rt_size = 0;
    2304                 :            :         unsigned long vdso_rt_delta = 0;
    2305                 :            : 
    2306                 :            :         struct vm_area_list self_vmas;
    2307                 :        347 :         struct vm_area_list *vmas = &current->rst->vmas;
    2308                 :            :         int i;
    2309                 :            : 
    2310                 :        347 :         pr_info("Restore via sigreturn\n");
    2311                 :            : 
    2312                 :            :         /* pr_info_vma_list(&self_vma_list); */
    2313                 :            : 
    2314                 :            :         BUILD_BUG_ON(sizeof(struct task_restore_args) & 1);
    2315                 :            :         BUILD_BUG_ON(sizeof(struct thread_restore_args) & 1);
    2316                 :            :         BUILD_BUG_ON(TASK_ENTRIES_SIZE % PAGE_SIZE);
    2317                 :            : 
    2318                 :        347 :         args_len = round_up(sizeof(*task_args) + sizeof(*thread_args) * current->nr_threads, PAGE_SIZE);
    2319                 :        347 :         pr_info("%d threads require %ldK of memory\n",
    2320                 :            :                         current->nr_threads, KBYTES(args_len));
    2321                 :            : 
    2322                 :            :         /*
    2323                 :            :          * Copy VMAs to private rst memory so that it's able to
    2324                 :            :          * walk them and m(un|re)map.
    2325                 :            :          */
    2326                 :            : 
    2327                 :        347 :         tgt_vmas = rst_mem_cpos(RM_PRIVATE);
    2328         [ +  + ]:      18094 :         list_for_each_entry(vma, &vmas->h, list) {
    2329                 :            :                 VmaEntry *vme;
    2330                 :            : 
    2331                 :      17747 :                 vme = rst_mem_alloc(sizeof(*vme), RM_PRIVATE);
    2332         [ +  - ]:      17747 :                 if (!vme)
    2333                 :            :                         goto err_nv;
    2334                 :            : 
    2335                 :      17747 :                 *vme = *vma->e;
    2336                 :            : 
    2337 [ +  + ][ +  + ]:      17747 :                 if (vma_priv(vma->e))
    2338                 :      17342 :                         vma_premmaped_start(vme) = vma->premmaped_addr;
    2339                 :            :         }
    2340                 :            : 
    2341                 :            :         /*
    2342                 :            :          * Copy tcp sockets fds to rst memory -- restorer will
    2343                 :            :          * turn repair off before going sigreturn
    2344                 :            :          */
    2345                 :            : 
    2346                 :        347 :         tcp_socks = rst_mem_cpos(RM_PRIVATE);
    2347                 :        347 :         tcp_socks_mem = rst_mem_alloc(rst_tcp_socks_len(), RM_PRIVATE);
    2348         [ +  - ]:        347 :         if (!tcp_socks_mem)
    2349                 :            :                 goto err_nv;
    2350                 :            : 
    2351                 :        347 :         memcpy(tcp_socks_mem, rst_tcp_socks, rst_tcp_socks_len());
    2352                 :            : 
    2353                 :            :         /*
    2354                 :            :          * We're about to search for free VM area and inject the restorer blob
    2355                 :            :          * into it. No irrelevent mmaps/mremaps beyond this point, otherwise
    2356                 :            :          * this unwanted mapping might get overlapped by the restorer.
    2357                 :            :          */
    2358                 :            : 
    2359                 :        347 :         ret = parse_self_maps_lite(&self_vmas);
    2360         [ +  - ]:        347 :         if (ret < 0)
    2361                 :            :                 goto err;
    2362                 :            : 
    2363                 :        347 :         restore_bootstrap_len = restorer_len + args_len +
    2364                 :        347 :                                 TASK_ENTRIES_SIZE +
    2365                 :        347 :                                 rst_mem_remap_size();
    2366                 :            : 
    2367                 :            :         /*
    2368                 :            :          * Figure out how much memory runtime vdso will need.
    2369                 :            :          */
    2370                 :        347 :         vdso_rt_vma_size = vdso_vma_size(&vdso_sym_rt);
    2371         [ +  - ]:        347 :         if (vdso_rt_vma_size) {
    2372                 :        347 :                 vdso_rt_delta = ALIGN(restore_bootstrap_len, PAGE_SIZE) - restore_bootstrap_len;
    2373                 :        347 :                 vdso_rt_size = vdso_rt_vma_size + vdso_rt_delta;
    2374                 :            :         }
    2375                 :            : 
    2376                 :        347 :         restore_bootstrap_len += vdso_rt_size;
    2377                 :            : 
    2378                 :            :         /*
    2379                 :            :          * Restorer is a blob (code + args) that will get mapped in some
    2380                 :            :          * place, that should _not_ intersect with both -- current mappings
    2381                 :            :          * and mappings of the task we're restoring here. The subsequent
    2382                 :            :          * call finds the start address for the restorer.
    2383                 :            :          *
    2384                 :            :          * After the start address is found we populate it with the restorer
    2385                 :            :          * parts one by one (some are remap-ed, some are mmap-ed and copied
    2386                 :            :          * or inited from scratch).
    2387                 :            :          */
    2388                 :            : 
    2389                 :        347 :         exec_mem_hint = restorer_get_vma_hint(pid, &vmas->h, &self_vmas.h,
    2390                 :            :                                               restore_bootstrap_len);
    2391         [ -  + ]:        347 :         if (exec_mem_hint == -1) {
    2392                 :          0 :                 pr_err("No suitable area for task_restore bootstrap (%ldK)\n",
    2393                 :            :                        restore_bootstrap_len);
    2394                 :          0 :                 goto err;
    2395                 :            :         }
    2396                 :            : 
    2397                 :        347 :         pr_info("Found bootstrap VMA hint at: 0x%lx (needs ~%ldK)\n", exec_mem_hint,
    2398                 :            :                         KBYTES(restore_bootstrap_len));
    2399                 :            : 
    2400                 :        347 :         ret = remap_restorer_blob((void *)exec_mem_hint);
    2401         [ +  - ]:        347 :         if (ret < 0)
    2402                 :            :                 goto err;
    2403                 :            : 
    2404                 :            :         /*
    2405                 :            :          * Prepare a memory map for restorer. Note a thread space
    2406                 :            :          * might be completely unused so it's here just for convenience.
    2407                 :            :          */
    2408                 :        347 :         restore_thread_exec_start       = restorer_sym(exec_mem_hint, __export_restore_thread);
    2409                 :        347 :         restore_task_exec_start         = restorer_sym(exec_mem_hint, __export_restore_task);
    2410                 :        347 :         current->rst->munmap_restorer     = restorer_sym(exec_mem_hint, __export_unmap);
    2411                 :            : 
    2412                 :        347 :         exec_mem_hint += restorer_len;
    2413                 :            : 
    2414                 :            :         /* VMA we need to run task_restore code */
    2415                 :        347 :         mem = mmap((void *)exec_mem_hint, args_len,
    2416                 :            :                         PROT_READ | PROT_WRITE,
    2417                 :            :                         MAP_PRIVATE | MAP_ANON | MAP_FIXED, 0, 0);
    2418         [ -  + ]:        347 :         if (mem != (void *)exec_mem_hint) {
    2419                 :          0 :                 pr_err("Can't mmap section for restore code\n");
    2420                 :          0 :                 goto err;
    2421                 :            :         }
    2422                 :            : 
    2423                 :        347 :         exec_mem_hint -= restorer_len;
    2424                 :            : 
    2425                 :            :         memzero(mem, args_len);
    2426                 :            :         task_args       = mem;
    2427                 :        347 :         thread_args     = (struct thread_restore_args *)(task_args + 1);
    2428                 :            : 
    2429                 :            :         /*
    2430                 :            :          * Get a reference to shared memory area which is
    2431                 :            :          * used to signal if shmem restoration complete
    2432                 :            :          * from low-level restore code.
    2433                 :            :          *
    2434                 :            :          * This shmem area is mapped right after the whole area of
    2435                 :            :          * sigreturn rt code. Note we didn't allocated it before
    2436                 :            :          * but this area is taken into account for 'hint' memory
    2437                 :            :          * address.
    2438                 :            :          */
    2439                 :            : 
    2440                 :        347 :         mem += args_len;
    2441                 :        347 :         ret = shmem_remap(task_entries, mem, TASK_ENTRIES_SIZE);
    2442         [ +  - ]:        347 :         if (ret < 0)
    2443                 :            :                 goto err;
    2444                 :        347 :         mem += TASK_ENTRIES_SIZE;
    2445                 :            : 
    2446         [ +  - ]:        347 :         if (rst_mem_remap(mem))
    2447                 :            :                 goto err;
    2448                 :            : 
    2449                 :        347 :         task_args->task_entries = mem - TASK_ENTRIES_SIZE;
    2450                 :            : 
    2451                 :        347 :         task_args->rst_mem = mem;
    2452                 :        347 :         task_args->rst_mem_size = rst_mem_remap_size();
    2453                 :            : 
    2454                 :        347 :         task_args->bootstrap_start = (void *)exec_mem_hint;
    2455                 :        347 :         task_args->bootstrap_len = restore_bootstrap_len;
    2456                 :        347 :         task_args->vdso_rt_size = vdso_rt_size;
    2457                 :            : 
    2458                 :        347 :         task_args->premmapped_addr = (unsigned long) current->rst->premmapped_addr;
    2459                 :        347 :         task_args->premmapped_len = current->rst->premmapped_len;
    2460                 :            : 
    2461                 :        347 :         task_args->shmems = rst_mem_remap_ptr(rst_shmems, RM_SHREMAP);
    2462                 :        347 :         task_args->nr_shmems = nr_shmems;
    2463                 :            : 
    2464                 :        347 :         task_args->nr_vmas = vmas->nr;
    2465                 :        347 :         task_args->tgt_vmas = rst_mem_remap_ptr(tgt_vmas, RM_PRIVATE);
    2466                 :            : 
    2467                 :        347 :         task_args->timer_n = posix_timers_nr;
    2468                 :        347 :         task_args->posix_timers = rst_mem_remap_ptr(posix_timers_cpos, RM_PRIVATE);
    2469                 :            : 
    2470                 :        347 :         task_args->siginfo_nr = siginfo_nr;
    2471                 :        347 :         task_args->siginfo = rst_mem_remap_ptr(siginfo_cpos, RM_PRIVATE);
    2472                 :            : 
    2473                 :        347 :         task_args->tcp_socks_nr = rst_tcp_socks_nr;
    2474                 :        347 :         task_args->tcp_socks = rst_mem_remap_ptr(tcp_socks, RM_PRIVATE);
    2475                 :            : 
    2476                 :            :         /*
    2477                 :            :          * Arguments for task restoration.
    2478                 :            :          */
    2479                 :            : 
    2480         [ -  + ]:        347 :         BUG_ON(core->mtype != CORE_ENTRY__MARCH);
    2481                 :            : 
    2482                 :        347 :         task_args->logfd     = log_get_fd();
    2483                 :        347 :         task_args->loglevel  = log_get_loglevel();
    2484                 :        347 :         task_args->sigchld_act       = sigchld_act;
    2485                 :            : 
    2486                 :        347 :         strncpy(task_args->comm, core->tc->comm, sizeof(task_args->comm));
    2487                 :            : 
    2488                 :        347 :         task_args->nr_rlim = rlims_nr;
    2489         [ +  - ]:        347 :         if (rlims_nr)
    2490                 :        347 :                 task_args->rlims = rst_mem_remap_ptr(rlims_cpos, RM_PRIVATE);
    2491                 :            : 
    2492                 :            :         /*
    2493                 :            :          * Fill up per-thread data.
    2494                 :            :          */
    2495         [ +  + ]:        929 :         for (i = 0; i < current->nr_threads; i++) {
    2496                 :            :                 int fd_core;
    2497                 :            :                 CoreEntry *tcore;
    2498                 :            :                 struct rt_sigframe *sigframe;
    2499                 :            : 
    2500                 :        582 :                 thread_args[i].pid = current->threads[i].virt;
    2501                 :        582 :                 thread_args[i].siginfo_nr = siginfo_priv_nr[i];
    2502                 :        582 :                 thread_args[i].siginfo = rst_mem_remap_ptr(siginfo_cpos, RM_PRIVATE);
    2503                 :        582 :                 thread_args[i].siginfo += siginfo_nr;
    2504                 :        582 :                 siginfo_nr += thread_args[i].siginfo_nr;
    2505                 :            : 
    2506                 :            :                 /* skip self */
    2507         [ +  + ]:        582 :                 if (thread_args[i].pid == pid) {
    2508                 :        347 :                         task_args->t = thread_args + i;
    2509                 :        347 :                         tcore = core;
    2510                 :            :                 } else {
    2511                 :        235 :                         fd_core = open_image(CR_FD_CORE, O_RSTR, thread_args[i].pid);
    2512         [ -  + ]:        235 :                         if (fd_core < 0) {
    2513                 :          0 :                                 pr_err("Can't open core data for thread %d\n",
    2514                 :            :                                        thread_args[i].pid);
    2515                 :          0 :                                 goto err;
    2516                 :            :                         }
    2517                 :            : 
    2518                 :        235 :                         ret = pb_read_one(fd_core, &tcore, PB_CORE);
    2519                 :        235 :                         close(fd_core);
    2520                 :            :                 }
    2521                 :            : 
    2522 [ +  + ][ -  + ]:        582 :                 if ((tcore->tc || tcore->ids) && thread_args[i].pid != pid) {
                 [ -  + ]
    2523                 :          0 :                         pr_err("Thread has optional fields present %d\n",
    2524                 :            :                                thread_args[i].pid);
    2525                 :            :                         ret = -1;
    2526                 :            :                 }
    2527                 :            : 
    2528         [ -  + ]:        582 :                 if (ret < 0) {
    2529                 :          0 :                         pr_err("Can't read core data for thread %d\n",
    2530                 :            :                                thread_args[i].pid);
    2531                 :          0 :                         goto err;
    2532                 :            :                 }
    2533                 :            : 
    2534                 :        582 :                 thread_args[i].ta               = task_args;
    2535                 :        582 :                 thread_args[i].gpregs           = *CORE_THREAD_ARCH_INFO(tcore)->gpregs;
    2536                 :        582 :                 thread_args[i].clear_tid_addr   = CORE_THREAD_ARCH_INFO(tcore)->clear_tid_addr;
    2537                 :            :                 core_get_tls(tcore, &thread_args[i].tls);
    2538                 :            : 
    2539         [ +  - ]:        582 :                 if (tcore->thread_core) {
    2540                 :        582 :                         thread_args[i].has_futex        = true;
    2541                 :        582 :                         thread_args[i].futex_rla        = tcore->thread_core->futex_rla;
    2542                 :        582 :                         thread_args[i].futex_rla_len    = tcore->thread_core->futex_rla_len;
    2543                 :            : 
    2544                 :        582 :                         ret = prep_sched_info(&thread_args[i].sp, tcore->thread_core);
    2545         [ +  - ]:        582 :                         if (ret)
    2546                 :            :                                 goto err;
    2547                 :            :                 }
    2548                 :            : 
    2549                 :        582 :                 sigframe = (struct rt_sigframe *)thread_args[i].mem_zone.rt_sigframe;
    2550                 :            : 
    2551         [ +  - ]:        582 :                 if (construct_sigframe(sigframe, sigframe, tcore))
    2552                 :            :                         goto err;
    2553                 :            : 
    2554         [ +  + ]:        582 :                 if (thread_args[i].pid != pid)
    2555                 :        235 :                         core_entry__free_unpacked(tcore, NULL);
    2556                 :            : 
    2557                 :        582 :                 pr_info("Thread %4d stack %8p rt_sigframe %8p\n",
    2558                 :            :                                 i, thread_args[i].mem_zone.stack,
    2559                 :            :                                 thread_args[i].mem_zone.rt_sigframe);
    2560                 :            : 
    2561                 :            :         }
    2562                 :            : 
    2563                 :            :         /*
    2564                 :            :          * Restorer needs own copy of vdso parameters. Runtime
    2565                 :            :          * vdso must be kept non intersecting with anything else,
    2566                 :            :          * since we need it being accessible even when own
    2567                 :            :          * self-vmas are unmaped.
    2568                 :            :          */
    2569                 :        347 :         mem += rst_mem_remap_size();
    2570                 :        347 :         task_args->vdso_rt_parked_at = (unsigned long)mem + vdso_rt_delta;
    2571                 :        347 :         task_args->vdso_sym_rt = vdso_sym_rt;
    2572                 :            : 
    2573                 :        694 :         new_sp = restorer_stack(task_args->t);
    2574                 :            : 
    2575                 :            :         /* No longer need it */
    2576                 :        347 :         core_entry__free_unpacked(core, NULL);
    2577                 :            : 
    2578                 :        347 :         ret = prepare_itimers(pid, core, task_args);
    2579         [ +  - ]:        347 :         if (ret < 0)
    2580                 :            :                 goto err;
    2581                 :            : 
    2582                 :        347 :         ret = prepare_creds(pid, task_args);
    2583         [ +  - ]:        347 :         if (ret < 0)
    2584                 :            :                 goto err;
    2585                 :            : 
    2586                 :        347 :         ret = prepare_mm(pid, task_args);
    2587         [ +  - ]:        347 :         if (ret < 0)
    2588                 :            :                 goto err;
    2589                 :            : 
    2590                 :            :         /*
    2591                 :            :          * Open the last_pid syscl early, since restorer (maybe) lives
    2592                 :            :          * in chroot and has no access to "/proc/..." paths.
    2593                 :            :          */
    2594                 :        347 :         task_args->fd_last_pid = open(LAST_PID_PATH, O_RDWR);
    2595         [ -  + ]:        347 :         if (task_args->fd_last_pid < 0) {
    2596                 :          0 :                 pr_perror("Can't open sys.ns_last_pid");
    2597                 :          0 :                 goto err;
    2598                 :            :         }
    2599                 :            : 
    2600                 :            :         /*
    2601                 :            :          * Now prepare run-time data for threads restore.
    2602                 :            :          */
    2603                 :        347 :         task_args->nr_threads                = current->nr_threads;
    2604                 :        347 :         task_args->nr_zombies                = current->rst->nr_zombies;
    2605                 :        347 :         task_args->clone_restore_fn  = (void *)restore_thread_exec_start;
    2606                 :        347 :         task_args->thread_args               = thread_args;
    2607                 :            : 
    2608                 :            :         /*
    2609                 :            :          * Make root and cwd restore _that_ late not to break any
    2610                 :            :          * attempts to open files by paths above (e.g. /proc).
    2611                 :            :          */
    2612                 :            : 
    2613         [ +  - ]:        347 :         if (prepare_fs(pid))
    2614                 :            :                 goto err;
    2615                 :            : 
    2616                 :        347 :         close_image_dir();
    2617                 :        347 :         close_proc();
    2618                 :        347 :         close_service_fd(ROOT_FD_OFF);
    2619                 :            : 
    2620                 :        347 :         __gcov_flush();
    2621                 :            : 
    2622                 :          0 :         pr_info("task_args: %p\n"
    2623                 :            :                 "task_args->pid: %d\n"
    2624                 :            :                 "task_args->nr_threads: %d\n"
    2625                 :            :                 "task_args->clone_restore_fn: %p\n"
    2626                 :            :                 "task_args->thread_args: %p\n",
    2627                 :            :                 task_args, task_args->t->pid,
    2628                 :            :                 task_args->nr_threads,
    2629                 :            :                 task_args->clone_restore_fn,
    2630                 :            :                 task_args->thread_args);
    2631                 :            : 
    2632                 :            :         /*
    2633                 :            :          * An indirect call to task_restore, note it never returns
    2634                 :            :          * and restoring core is extremely destructive.
    2635                 :            :          */
    2636                 :            : 
    2637                 :          0 :         JUMP_TO_RESTORER_BLOB(new_sp, restore_task_exec_start, task_args);
    2638                 :            : 
    2639                 :            : err:
    2640                 :          0 :         free_mappings(&self_vmas);
    2641                 :            : err_nv:
    2642                 :            :         /* Just to be sure */
    2643                 :          0 :         exit(1);
    2644                 :            :         return -1;
    2645                 :            : }

Generated by: LCOV version 1.9