-
Notifications
You must be signed in to change notification settings - Fork 620
/
Copy pathrestorer.c
1479 lines (1219 loc) · 37.1 KB
/
restorer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <linux/securebits.h>
#include <linux/capability.h>
#include <linux/aio_abi.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <unistd.h>
#include <sched.h>
#include <sys/resource.h>
#include <signal.h>
#include "compiler.h"
#include "asm/string.h"
#include "asm/types.h"
#include "syscall.h"
#include "config.h"
#include "prctl.h"
#include "log.h"
#include "util.h"
#include "image.h"
#include "sk-inet.h"
#include "vma.h"
#include "uffd.h"
#include "crtools.h"
#include "lock.h"
#include "restorer.h"
#include "aio.h"
#include "seccomp.h"
#include "images/creds.pb-c.h"
#include "images/mm.pb-c.h"
#include "asm/restorer.h"
#ifndef PR_SET_PDEATHSIG
#define PR_SET_PDEATHSIG 1
#endif
#define sys_prctl_safe(opcode, val1, val2, val3) \
({ \
long __ret = sys_prctl(opcode, val1, val2, val3, 0); \
if (__ret) \
pr_err("prctl failed @%d with %ld\n", __LINE__, __ret);\
__ret; \
})
static struct task_entries *task_entries;
static futex_t thread_inprogress;
static pid_t *helpers;
static int n_helpers;
static pid_t *zombies;
static int n_zombies;
extern void cr_restore_rt (void) asm ("__cr_restore_rt")
__attribute__ ((visibility ("hidden")));
static void sigchld_handler(int signal, siginfo_t *siginfo, void *data)
{
char *r;
int i;
/* We can ignore helpers that die, we expect them to after
* CR_STATE_RESTORE is finished. */
for (i = 0; i < n_helpers; i++)
if (siginfo->si_pid == helpers[i])
return;
for (i = 0; i < n_zombies; i++)
if (siginfo->si_pid == zombies[i])
return;
if (siginfo->si_code & CLD_EXITED)
r = " exited, status=";
else if (siginfo->si_code & CLD_KILLED)
r = " killed by signal ";
else
r = "disappeared with ";
pr_info("Task %d %s %d\n", siginfo->si_pid, r, siginfo->si_status);
futex_abort_and_wake(&task_entries->nr_in_progress);
/* sa_restorer may be unmaped, so we can't go back to userspace*/
sys_kill(sys_getpid(), SIGSTOP);
sys_exit_group(1);
}
static int lsm_set_label(char *label, int procfd)
{
int ret = -1, len, lsmfd;
char path[LOG_SIMPLE_CHUNK];
if (!label)
return 0;
pr_info("restoring lsm profile %s\n", label);
simple_sprintf(path, "self/task/%ld/attr/current", sys_gettid());
lsmfd = sys_openat(procfd, path, O_WRONLY, 0);
if (lsmfd < 0) {
pr_err("failed openat %d\n", lsmfd);
return -1;
}
for (len = 0; label[len]; len++)
;
ret = sys_write(lsmfd, label, len);
sys_close(lsmfd);
if (ret < 0) {
pr_err("can't write lsm profile %d\n", ret);
return -1;
}
return 0;
}
static int restore_creds(struct thread_creds_args *args, int procfd)
{
CredsEntry *ce = &args->creds;
int b, i, ret;
struct cap_header hdr;
struct cap_data data[_LINUX_CAPABILITY_U32S_3];
/*
* We're still root here and thus can do it without failures.
*/
/*
* Setup supplementary group IDs early.
*/
if (args->groups) {
ret = sys_setgroups(ce->n_groups, args->groups);
if (ret) {
pr_err("Can't setup supplementary group IDs: %d\n", ret);
return -1;
}
}
/*
* First -- set the SECURE_NO_SETUID_FIXUP bit not to
* lose caps bits when changing xids.
*/
ret = sys_prctl(PR_SET_SECUREBITS, 1 << SECURE_NO_SETUID_FIXUP, 0, 0, 0);
if (ret) {
pr_err("Unable to set SECURE_NO_SETUID_FIXUP: %d\n", ret);
return -1;
}
/*
* Second -- restore xids. Since we still have the CAP_SETUID
* capability nothing should fail. But call the setfsXid last
* to override the setresXid settings.
*/
ret = sys_setresuid(ce->uid, ce->euid, ce->suid);
if (ret) {
pr_err("Unable to set real, effective and saved user ID: %d\n", ret);
return -1;
}
sys_setfsuid(ce->fsuid);
if (sys_setfsuid(-1) != ce->fsuid) {
pr_err("Unable to set fsuid\n");
return -1;
}
ret = sys_setresgid(ce->gid, ce->egid, ce->sgid);
if (ret) {
pr_err("Unable to set real, effective and saved group ID: %d\n", ret);
return -1;
}
sys_setfsgid(ce->fsgid);
if (sys_setfsgid(-1) != ce->fsgid) {
pr_err("Unable to set fsgid\n");
return -1;
}
/*
* Third -- restore securebits. We don't need them in any
* special state any longer.
*/
ret = sys_prctl(PR_SET_SECUREBITS, ce->secbits, 0, 0, 0);
if (ret) {
pr_err("Unable to set PR_SET_SECUREBITS: %d\n", ret);
return -1;
}
/*
* Fourth -- trim bset. This can only be done while
* having the CAP_SETPCAP capablity.
*/
for (b = 0; b < CR_CAP_SIZE; b++) {
for (i = 0; i < 32; i++) {
if (b * 32 + i > args->cap_last_cap)
break;
if (args->cap_bnd[b] & (1 << i))
/* already set */
continue;
ret = sys_prctl(PR_CAPBSET_DROP, i + b * 32, 0, 0, 0);
if (ret) {
pr_err("Unable to drop capability %d: %d\n",
i + b * 32, ret);
return -1;
}
}
}
/*
* Fifth -- restore caps. Nothing but cap bits are changed
* at this stage, so just do it.
*/
hdr.version = _LINUX_CAPABILITY_VERSION_3;
hdr.pid = 0;
BUILD_BUG_ON(_LINUX_CAPABILITY_U32S_3 != CR_CAP_SIZE);
for (i = 0; i < CR_CAP_SIZE; i++) {
data[i].eff = args->cap_eff[i];
data[i].prm = args->cap_prm[i];
data[i].inh = args->cap_inh[i];
}
ret = sys_capset(&hdr, data);
if (ret) {
pr_err("Unable to restore capabilities: %d\n", ret);
return -1;
}
if (lsm_set_label(args->lsm_profile, procfd) < 0)
return -1;
return 0;
}
/*
* This should be done after creds restore, as
* some creds changes might drop the value back
* to zero.
*/
static inline int restore_pdeath_sig(struct thread_restore_args *ta)
{
if (ta->pdeath_sig)
return sys_prctl(PR_SET_PDEATHSIG, ta->pdeath_sig, 0, 0, 0);
else
return 0;
}
static int restore_dumpable_flag(MmEntry *mme)
{
int current_dumpable;
int ret;
if (!mme->has_dumpable) {
pr_warn("Dumpable flag not present in criu dump.\n");
return 0;
}
if (mme->dumpable == 0 || mme->dumpable == 1) {
ret = sys_prctl(PR_SET_DUMPABLE, mme->dumpable, 0, 0, 0);
if (ret) {
pr_err("Unable to set PR_SET_DUMPABLE: %d\n", ret);
return -1;
}
return 0;
}
/*
* If dumpable flag is present but it is not 0 or 1, then we can not
* use prctl to set it back. Try to see if it is already correct
* (which is likely if sysctl fs.suid_dumpable is the same when dump
* and restore are run), in which case there is nothing to do.
* Otherwise, set dumpable to 0 which should be a secure fallback.
*/
current_dumpable = sys_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
if (mme->dumpable != current_dumpable) {
pr_warn("Dumpable flag [%d] does not match current [%d]. "
"Will fallback to setting it to 0 to disable it.\n",
mme->dumpable, current_dumpable);
ret = sys_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
if (ret) {
pr_err("Unable to set PR_SET_DUMPABLE: %d\n", ret);
return -1;
}
}
return 0;
}
static void restore_sched_info(struct rst_sched_param *p)
{
struct sched_param parm;
pr_info("Restoring scheduler params %d.%d.%d\n",
p->policy, p->nice, p->prio);
sys_setpriority(PRIO_PROCESS, 0, p->nice);
parm.sched_priority = p->prio;
sys_sched_setscheduler(0, p->policy, &parm);
}
static void restore_rlims(struct task_restore_args *ta)
{
int r;
for (r = 0; r < ta->rlims_n; r++) {
struct krlimit krlim;
krlim.rlim_cur = ta->rlims[r].rlim_cur;
krlim.rlim_max = ta->rlims[r].rlim_max;
sys_setrlimit(r, &krlim);
}
}
static int restore_signals(siginfo_t *ptr, int nr, bool group)
{
int ret, i;
for (i = 0; i < nr; i++) {
siginfo_t *info = ptr + i;
pr_info("Restore signal %d group %d\n", info->si_signo, group);
if (group)
ret = sys_rt_sigqueueinfo(sys_getpid(), info->si_signo, info);
else
ret = sys_rt_tgsigqueueinfo(sys_getpid(),
sys_gettid(), info->si_signo, info);
if (ret) {
pr_err("Unable to send siginfo %d %x with code %d\n",
info->si_signo, info->si_code, ret);
return -1;;
}
}
return 0;
}
static int restore_seccomp(struct task_restore_args *args)
{
int ret;
switch (args->seccomp_mode) {
case SECCOMP_MODE_DISABLED:
return 0;
case SECCOMP_MODE_STRICT:
ret = sys_prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, 0, 0, 0);
if (ret < 0) {
pr_err("prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT) returned %d\n", ret);
goto die;
}
return 0;
case SECCOMP_MODE_FILTER: {
int i;
void *filter_data;
filter_data = &args->seccomp_filters[args->seccomp_filters_n];
for (i = 0; i < args->seccomp_filters_n; i++) {
struct sock_fprog *fprog = &args->seccomp_filters[i];
fprog->filter = filter_data;
/* We always TSYNC here, since we require that the
* creds for all threads be the same; this means we
* don't have to restore_seccomp() in threads, and that
* future TSYNC behavior will be correct.
*/
ret = sys_seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, (char *) fprog);
if (ret < 0) {
pr_err("sys_seccomp() returned %d\n", ret);
goto die;
}
filter_data += fprog->len * sizeof(struct sock_filter);
}
return 0;
}
default:
goto die;
}
return 0;
die:
return -1;
}
static int restore_thread_common(struct thread_restore_args *args)
{
sys_set_tid_address((int *)decode_pointer(args->clear_tid_addr));
if (args->has_futex && args->futex_rla_len) {
int ret;
ret = sys_set_robust_list(decode_pointer(args->futex_rla),
args->futex_rla_len);
if (ret) {
pr_err("Failed to recover futex robust list: %d\n", ret);
return -1;
}
}
restore_sched_info(&args->sp);
if (restore_nonsigframe_gpregs(&args->gpregs))
return -1;
restore_tls(&args->tls);
return 0;
}
static void noinline rst_sigreturn(unsigned long new_sp)
{
ARCH_RT_SIGRETURN(new_sp);
}
/*
* Threads restoration via sigreturn. Note it's locked
* routine and calls for unlock at the end.
*/
long __export_restore_thread(struct thread_restore_args *args)
{
struct rt_sigframe *rt_sigframe;
k_rtsigset_t to_block;
unsigned long new_sp;
int my_pid = sys_gettid();
int ret;
if (my_pid != args->pid) {
pr_err("Thread pid mismatch %d/%d\n", my_pid, args->pid);
goto core_restore_end;
}
/* All signals must be handled by thread leader */
ksigfillset(&to_block);
ret = sys_sigprocmask(SIG_SETMASK, &to_block, NULL, sizeof(k_rtsigset_t));
if (ret) {
pr_err("Unable to block signals %d\n", ret);
goto core_restore_end;
}
rt_sigframe = (void *)args->mem_zone.rt_sigframe;
if (restore_thread_common(args))
goto core_restore_end;
ret = restore_creds(args->creds_args, args->ta->proc_fd);
if (ret)
goto core_restore_end;
ret = restore_dumpable_flag(&args->ta->mm);
if (ret)
goto core_restore_end;
pr_info("%ld: Restored\n", sys_gettid());
restore_finish_stage(CR_STATE_RESTORE);
if (restore_signals(args->siginfo, args->siginfo_n, false))
goto core_restore_end;
restore_finish_stage(CR_STATE_RESTORE_SIGCHLD);
restore_pdeath_sig(args);
if (args->ta->seccomp_mode != SECCOMP_MODE_DISABLED)
pr_info("Restoring seccomp mode %d for %ld\n", args->ta->seccomp_mode, sys_getpid());
restore_finish_stage(CR_STATE_RESTORE_CREDS);
futex_dec_and_wake(&thread_inprogress);
new_sp = (long)rt_sigframe + SIGFRAME_OFFSET;
rst_sigreturn(new_sp);
core_restore_end:
pr_err("Restorer abnormal termination for %ld\n", sys_getpid());
futex_abort_and_wake(&task_entries->nr_in_progress);
sys_exit_group(1);
return -1;
}
static long restore_self_exe_late(struct task_restore_args *args)
{
int fd = args->fd_exe_link, ret;
pr_info("Restoring EXE link\n");
ret = sys_prctl_safe(PR_SET_MM, PR_SET_MM_EXE_FILE, fd, 0);
if (ret)
pr_err("Can't restore EXE link (%d)\n", ret);
sys_close(fd);
return ret;
}
static unsigned long restore_mapping(const VmaEntry *vma_entry)
{
int prot = vma_entry->prot;
int flags = vma_entry->flags | MAP_FIXED;
unsigned long addr;
if (vma_entry_is(vma_entry, VMA_AREA_SYSVIPC))
return sys_shmat(vma_entry->fd, decode_pointer(vma_entry->start),
(vma_entry->prot & PROT_WRITE) ? 0 : SHM_RDONLY);
/*
* Restore or shared mappings are tricky, since
* we open anonymous mapping via map_files/
* MAP_ANONYMOUS should be eliminated so fd would
* be taken into account by a kernel.
*/
if (vma_entry_is(vma_entry, VMA_ANON_SHARED) && (vma_entry->fd != -1UL))
flags &= ~MAP_ANONYMOUS;
/* A mapping of file with MAP_SHARED is up to date */
if (vma_entry->fd == -1 || !(vma_entry->flags & MAP_SHARED))
prot |= PROT_WRITE;
pr_debug("\tmmap(%"PRIx64" -> %"PRIx64", %x %x %d)\n",
vma_entry->start, vma_entry->end,
prot, flags, (int)vma_entry->fd);
/*
* Should map memory here. Note we map them as
* writable since we're going to restore page
* contents.
*/
addr = sys_mmap(decode_pointer(vma_entry->start),
vma_entry_len(vma_entry),
prot, flags,
vma_entry->fd,
vma_entry->pgoff);
if (vma_entry->fd != -1)
sys_close(vma_entry->fd);
return addr;
}
/*
* This restores aio ring header, content, head and in-kernel position
* of tail. To set tail, we write to /dev/null and use the fact this
* operation is synchronious for the device. Also, we unmap temporary
* anonymous area, used to store content of ring buffer during restore
* and mapped in map_private_vma().
*/
static int restore_aio_ring(struct rst_aio_ring *raio)
{
struct aio_ring *ring = (void *)raio->addr;
int i, maxr, count, fd, ret;
unsigned head = ring->head;
unsigned tail = ring->tail;
struct iocb *iocb, **iocbp;
unsigned long ctx = 0;
unsigned size;
char buf[1];
ret = sys_io_setup(raio->nr_req, &ctx);
if (ret < 0) {
pr_err("Ring setup failed with %d\n", ret);
return -1;
}
if (tail == 0 && head == 0)
goto populate;
fd = sys_open("/dev/null", O_WRONLY, 0);
if (fd < 0) {
pr_err("Can't open /dev/null for aio\n");
return -1;
}
/*
* If tail < head, we have to do full turn and then submit
* tail more request, i.e. ring->nr + tail.
* If we do not do full turn, in-kernel completed_events
* will initialize wrong.
*
* Maximum number reqs to submit at once are ring->nr-1,
* so we won't allocate more.
*/
if (tail < head)
count = ring->nr + tail;
else
count = tail;
maxr = min_t(unsigned, count, ring->nr-1);
/*
* Since we only interested in moving the tail, the requests
* may be any. We submit count identical requests.
*/
size = sizeof(struct iocb) + maxr * sizeof(struct iocb *);
iocb = (void *)sys_mmap(NULL, size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
iocbp = (void *)iocb + sizeof(struct iocb);
if (iocb == MAP_FAILED) {
pr_err("Can't mmap aio tmp buffer\n");
return -1;
}
iocb->aio_fildes = fd;
iocb->aio_buf = (unsigned long)buf;
iocb->aio_nbytes = 1;
iocb->aio_lio_opcode = IOCB_CMD_PWRITE; /* Write is nop, read populates buf */
for (i = 0; i < maxr; i++)
iocbp[i] = iocb;
i = 0;
do {
ret = sys_io_submit(ctx, count - i, iocbp);
if (ret < 0) {
pr_err("Can't submit aio iocbs: ret=%d\n", ret);
return -1;
}
i += ret;
/*
* We may submit less than requested, because of too big
* count OR behaviour of get_reqs_available(), which
* takes available requests only if their number is
* aliquot to kioctx::req_batch. Free part of buffer
* for next iteration.
*
* Direct set of head is equal to sys_io_getevents() call,
* and faster. See kernel for the details.
*/
((struct aio_ring *)ctx)->head = i < head ? i : head;
} while (i < count);
sys_munmap(iocb, size);
sys_close(fd);
populate:
i = offsetof(struct aio_ring, io_events);
builtin_memcpy((void *)ctx + i, (void *)ring + i, raio->len - i);
/*
* If we failed to get the proper nr_req right and
* created smaller or larger ring, then this remap
* will (should) fail, since AIO rings has immutable
* size.
*
* This is not great, but anyway better than putting
* a ring of wrong size into correct place.
*
* Also, this unmaps temporary anonymous area on raio->addr.
*/
ctx = sys_mremap(ctx, raio->len, raio->len,
MREMAP_FIXED | MREMAP_MAYMOVE,
raio->addr);
if (ctx != raio->addr) {
pr_err("Ring remap failed with %ld\n", ctx);
return -1;
}
return 0;
}
static void rst_tcp_repair_off(struct rst_tcp_sock *rts)
{
int aux, ret;
aux = rts->reuseaddr;
pr_debug("pie: Turning repair off for %d (reuse %d)\n", rts->sk, aux);
tcp_repair_off(rts->sk);
ret = sys_setsockopt(rts->sk, SOL_SOCKET, SO_REUSEADDR, &aux, sizeof(aux));
if (ret < 0)
pr_err("Failed to restore of SO_REUSEADDR on socket (%d)\n", ret);
}
static void rst_tcp_socks_all(struct task_restore_args *ta)
{
int i;
for (i = 0; i < ta->tcp_socks_n; i++)
rst_tcp_repair_off(&ta->tcp_socks[i]);
}
static int enable_uffd(int uffd, unsigned long addr, unsigned long len)
{
/*
* If uffd == -1, this means that userfaultfd is not enabled
* or it is not available.
*/
if (uffd == -1)
return 0;
#ifdef CONFIG_HAS_UFFD
int rc;
struct uffdio_register uffdio_register;
unsigned long expected_ioctls;
uffdio_register.range.start = addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
pr_info("lazy-pages: uffdio_register.range.start 0x%lx\n", (unsigned long) uffdio_register.range.start);
pr_info("lazy-pages: uffdio_register.len 0x%llx\n", uffdio_register.range.len);
rc = sys_ioctl(uffd, UFFDIO_REGISTER, (unsigned long) &uffdio_register);
pr_info("lazy-pages: ioctl UFFDIO_REGISTER rc %d\n", rc);
pr_info("lazy-pages: uffdio_register.range.start 0x%lx\n", (unsigned long) uffdio_register.range.start);
pr_info("lazy-pages: uffdio_register.len 0x%llx\n", uffdio_register.range.len);
if (rc != 0)
return -1;
expected_ioctls = (1 << _UFFDIO_WAKE) | (1 << _UFFDIO_COPY) | (1 << _UFFDIO_ZEROPAGE);
if ((uffdio_register.ioctls & expected_ioctls) != expected_ioctls) {
pr_err("lazy-pages: unexpected missing uffd ioctl for anon memory\n");
}
#endif
return 0;
}
static int vma_remap(VmaEntry *vma_entry, int uffd)
{
unsigned long src = vma_premmaped_start(vma_entry);
unsigned long dst = vma_entry->start;
unsigned long len = vma_entry_len(vma_entry);
unsigned long guard = 0, tmp;
pr_info("Remap %lx->%lx len %lx\n", src, dst, len);
if (src - dst < len)
guard = dst;
else if (dst - src < len)
guard = dst + len - PAGE_SIZE;
if (src == dst)
return 0;
if (guard != 0) {
/*
* mremap() returns an error if a target and source vma-s are
* overlapped. In this case the source vma are remapped in
* a temporary place and then remapped to the target address.
* Here is one hack to find non-ovelapped temporary place.
*
* 1. initial placement. We need to move src -> tgt.
* | |+++++src+++++|
* |-----tgt-----| |
*
* 2. map a guard page at the non-ovelapped border of a target vma.
* | |+++++src+++++|
* |G|----tgt----| |
*
* 3. remap src to any other place.
* G prevents src from being remaped on tgt again
* | |-------------| -> |+++++src+++++|
* |G|---tgt-----| |
*
* 4. remap src to tgt, no overlapping any longer
* |+++++src+++++| <---- |-------------|
* |G|---tgt-----| |
*/
unsigned long addr;
/* Map guard page (step 2) */
tmp = sys_mmap((void *) guard, PAGE_SIZE, PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (tmp != guard) {
pr_err("Unable to map a guard page %lx (%lx)\n", guard, tmp);
return -1;
}
/* Move src to non-overlapping place (step 3) */
addr = sys_mmap(NULL, len, PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (addr == (unsigned long) MAP_FAILED) {
pr_err("Unable to reserve memory (%lx)\n", addr);
return -1;
}
tmp = sys_mremap(src, len, len,
MREMAP_MAYMOVE | MREMAP_FIXED, addr);
if (tmp != addr) {
pr_err("Unable to remap %lx -> %lx (%lx)\n", src, addr, tmp);
return -1;
}
src = addr;
}
tmp = sys_mremap(src, len, len, MREMAP_MAYMOVE | MREMAP_FIXED, dst);
if (tmp != dst) {
pr_err("Unable to remap %lx -> %lx\n", src, dst);
return -1;
}
/*
* If running in userfaultfd/lazy-pages mode pages with
* MAP_ANONYMOUS and MAP_PRIVATE are remapped but without the
* real content.
* The function enable_uffd() marks the page(s) as userfaultfd
* pages, so that the processes will hang until the memory is
* injected via userfaultfd.
*/
if (vma_entry_can_be_lazy(vma_entry))
if (enable_uffd(uffd, dst, len) != 0)
return -1;
return 0;
}
static int timerfd_arm(struct task_restore_args *args)
{
int i;
for (i = 0; i < args->timerfd_n; i++) {
struct restore_timerfd *t = &args->timerfd[i];
int ret;
pr_debug("timerfd: arm for fd %d (%d)\n", t->fd, i);
if (t->settime_flags & TFD_TIMER_ABSTIME) {
struct timespec ts = { };
/*
* We might need to adjust value because the checkpoint
* and restore procedure takes some time itself. Note
* we don't adjust nanoseconds, since the result may
* overflow the limit NSEC_PER_SEC FIXME
*/
if (sys_clock_gettime(t->clockid, &ts)) {
pr_err("Can't get current time\n");
return -1;
}
t->val.it_value.tv_sec += (time_t)ts.tv_sec;
pr_debug("Ajust id %#x it_value(%llu, %llu) -> it_value(%llu, %llu)\n",
t->id, (unsigned long long)ts.tv_sec,
(unsigned long long)ts.tv_nsec,
(unsigned long long)t->val.it_value.tv_sec,
(unsigned long long)t->val.it_value.tv_nsec);
}
ret = sys_timerfd_settime(t->fd, t->settime_flags, &t->val, NULL);
if (t->ticks)
ret |= sys_ioctl(t->fd, TFD_IOC_SET_TICKS, (unsigned long)&t->ticks);
if (ret) {
pr_err("Can't restore ticks/time for timerfd - %d\n", i);
return ret;
}
}
return 0;
}
static int create_posix_timers(struct task_restore_args *args)
{
int ret, i;
kernel_timer_t next_id;
struct sigevent sev;
for (i = 0; i < args->posix_timers_n; i++) {
sev.sigev_notify = args->posix_timers[i].spt.it_sigev_notify;
sev.sigev_signo = args->posix_timers[i].spt.si_signo;
sev.sigev_value.sival_ptr = args->posix_timers[i].spt.sival_ptr;
while (1) {
ret = sys_timer_create(args->posix_timers[i].spt.clock_id, &sev, &next_id);
if (ret < 0) {
pr_err("Can't create posix timer - %d\n", i);
return ret;
}
if (next_id == args->posix_timers[i].spt.it_id)
break;
ret = sys_timer_delete(next_id);
if (ret < 0) {
pr_err("Can't remove temporaty posix timer 0x%x\n", next_id);
return ret;
}
if ((long)next_id > args->posix_timers[i].spt.it_id) {
pr_err("Can't create timers, kernel don't give them consequently\n");
return -1;
}
}
}
return 0;
}
static void restore_posix_timers(struct task_restore_args *args)
{
int i;
struct restore_posix_timer *rt;
for (i = 0; i < args->posix_timers_n; i++) {
rt = &args->posix_timers[i];
sys_timer_settime((kernel_timer_t)rt->spt.it_id, 0, &rt->val, NULL);
}
}
static void *bootstrap_start;
static unsigned int bootstrap_len;
/*
* sys_munmap must not return here. The controll process must
* trap us on the exit from sys_munmap.
*/
#ifdef CONFIG_VDSO
static unsigned long vdso_rt_size;
#else
#define vdso_rt_size (0)
#endif
void __export_unmap(void)
{
sys_munmap(bootstrap_start, bootstrap_len - vdso_rt_size);
}
/*
* This function unmaps all VMAs, which don't belong to
* the restored process or the restorer.
*
* The restorer memory is two regions -- area with restorer, its stack
* and arguments and the one with private vmas of the tasks we restore
* (a.k.a. premmaped area):
*
* 0 task_size
* +----+====+----+====+---+
*
* Thus to unmap old memory we have to do 3 unmaps:
* [ 0 -- 1st area start ]
* [ 1st end -- 2nd start ]
* [ 2nd start -- task_size ]
*/
static int unmap_old_vmas(void *premmapped_addr, unsigned long premmapped_len,
void *bootstrap_start, unsigned long bootstrap_len,
unsigned long task_size)
{
unsigned long s1, s2;
void *p1, *p2;
int ret;
if (premmapped_addr < bootstrap_start) {
p1 = premmapped_addr;
s1 = premmapped_len;
p2 = bootstrap_start;
s2 = bootstrap_len;
} else {
p2 = premmapped_addr;
s2 = premmapped_len;
p1 = bootstrap_start;
s1 = bootstrap_len;
}
ret = sys_munmap(NULL, p1 - NULL);
if (ret) {
pr_err("Unable to unmap (%p-%p): %d\n", NULL, p1, ret);
return -1;
}
ret = sys_munmap(p1 + s1, p2 - (p1 + s1));
if (ret) {
pr_err("Unable to unmap (%p-%p): %d\n", p1 + s1, p2, ret);
return -1;
}
ret = sys_munmap(p2 + s2, task_size - (unsigned long)(p2 + s2));
if (ret) {
pr_err("Unable to unmap (%p-%p): %d\n",
p2 + s2, (void *)task_size, ret);
return -1;
}
return 0;
}
static int wait_helpers(struct task_restore_args *task_args)
{
int i;
for (i = 0; i < task_args->helpers_n; i++) {
int status;
pid_t pid = task_args->helpers[i];
/* Check that a helper completed. */
if (sys_wait4(pid, &status, 0, NULL) == -1) {
/* It has been waited in sigchld_handler */