Branch data Line data Source code
1 : : #ifndef __CR_RESTORER_H__
2 : : #define __CR_RESTORER_H__
3 : :
4 : : #include <signal.h>
5 : : #include <limits.h>
6 : : #include <sys/resource.h>
7 : :
8 : : #include "compiler.h"
9 : : #include "asm/types.h"
10 : : #include "asm/fpu.h"
11 : : #include "image.h"
12 : : #include "lock.h"
13 : : #include "util.h"
14 : : #include "asm/restorer.h"
15 : : #include "rst_info.h"
16 : :
17 : : #include "posix-timer.h"
18 : : #include "shmem.h"
19 : : #include "sigframe.h"
20 : : #include "vdso.h"
21 : :
22 : : #include <time.h>
23 : :
24 : : #include "protobuf/mm.pb-c.h"
25 : : #include "protobuf/vma.pb-c.h"
26 : : #include "protobuf/creds.pb-c.h"
27 : : #include "protobuf/core.pb-c.h"
28 : :
29 : : struct task_restore_core_args;
30 : : struct thread_restore_args;
31 : :
32 : : typedef long (*task_restore_fcall_t) (struct task_restore_core_args *args);
33 : : typedef long (*thread_restore_fcall_t) (struct thread_restore_args *args);
34 : :
35 : : #define RESTORE_CMD__NONE 0
36 : : #define RESTORE_CMD__GET_SELF_LEN 1
37 : : #define RESTORE_CMD__RESTORE_CORE 2
38 : : #define RESTORE_CMD__RESTORE_THREAD 3
39 : :
40 : : /*
41 : : * These *must* be power of two values.
42 : : */
43 : : #define RESTORE_ARGS_SIZE (512)
44 : : #define RESTORE_STACK_REDZONE (128)
45 : : #define RESTORE_STACK_SIZE (KILO(32))
46 : :
47 : : struct restore_mem_zone {
48 : : u8 redzone[RESTORE_STACK_REDZONE];
49 : : u8 stack[RESTORE_STACK_SIZE];
50 : : u8 rt_sigframe[RESTORE_STACK_SIGFRAME];
51 : : } __aligned(sizeof(long));
52 : :
53 : : struct rst_sched_param {
54 : : int policy;
55 : : int nice;
56 : : int prio;
57 : : };
58 : :
59 : : struct restore_posix_timer {
60 : : struct str_posix_timer spt;
61 : : struct itimerspec val;
62 : : int overrun;
63 : : };
64 : :
65 : : struct task_restore_core_args;
66 : :
67 : : /*
68 : : * We should be able to construct fpu sigframe in sigreturn_prep_fpu_frame,
69 : : * so the mem_zone.rt_sigframe should be 64-bytes aligned. To make things
70 : : * simpler, force both _args alignment be 64 bytes.
71 : : */
72 : :
73 : : struct thread_restore_args {
74 : : struct restore_mem_zone mem_zone;
75 : :
76 : : int pid;
77 : : UserRegsEntry gpregs;
78 : : u64 clear_tid_addr;
79 : :
80 : : bool has_futex;
81 : : u64 futex_rla;
82 : : u32 futex_rla_len;
83 : :
84 : : struct rst_sched_param sp;
85 : :
86 : : struct task_restore_args *ta;
87 : :
88 : : tls_t tls;
89 : :
90 : : siginfo_t *siginfo;
91 : : unsigned int siginfo_nr;
92 : : } __aligned(64);
93 : :
94 : : struct task_restore_args {
95 : : struct thread_restore_args *t; /* thread group leader */
96 : :
97 : : int fd_exe_link; /* opened self->exe file */
98 : : int logfd;
99 : : unsigned int loglevel;
100 : :
101 : : /* threads restoration */
102 : : int nr_threads; /* number of threads */
103 : : int nr_zombies;
104 : : thread_restore_fcall_t clone_restore_fn; /* helper address for clone() call */
105 : : struct thread_restore_args *thread_args; /* array of thread arguments */
106 : : struct shmem_info *shmems;
107 : : unsigned int nr_shmems;
108 : : struct task_entries *task_entries;
109 : : void *rst_mem;
110 : : unsigned long rst_mem_size;
111 : : VmaEntry *tgt_vmas;
112 : : siginfo_t *siginfo;
113 : : unsigned int siginfo_nr;
114 : : unsigned int nr_vmas;
115 : : unsigned long premmapped_addr;
116 : : unsigned long premmapped_len;
117 : : rt_sigaction_t sigchld_act;
118 : :
119 : : void *bootstrap_start;
120 : : unsigned long bootstrap_len;
121 : : unsigned long vdso_rt_size;
122 : :
123 : : struct itimerval itimers[3];
124 : :
125 : : int timer_n;
126 : : struct restore_posix_timer *posix_timers;
127 : :
128 : : CredsEntry creds;
129 : : u32 cap_inh[CR_CAP_SIZE];
130 : : u32 cap_prm[CR_CAP_SIZE];
131 : : u32 cap_eff[CR_CAP_SIZE];
132 : : u32 cap_bnd[CR_CAP_SIZE];
133 : : u32 cap_last_cap;
134 : :
135 : : MmEntry mm;
136 : : auxv_t mm_saved_auxv[AT_VECTOR_SIZE];
137 : : u32 mm_saved_auxv_size;
138 : : char comm[TASK_COMM_LEN];
139 : :
140 : : int nr_rlim;
141 : : struct rlimit *rlims;
142 : :
143 : : struct rst_tcp_sock *tcp_socks;
144 : : int tcp_socks_nr;
145 : :
146 : : int fd_last_pid; /* sys.ns_last_pid for threads rst */
147 : :
148 : : struct vdso_symtable vdso_sym_rt; /* runtime vdso symbols */
149 : : unsigned long vdso_rt_parked_at; /* safe place to keep vdso */
150 : : } __aligned(64);
151 : :
152 : : #define RESTORE_ALIGN_STACK(start, size) \
153 : : (ALIGN((start) + (size) - sizeof(long), sizeof(long)))
154 : :
155 : : static inline unsigned long restorer_stack(struct thread_restore_args *a)
156 : : {
157 : 347 : return RESTORE_ALIGN_STACK((long)a->mem_zone.stack, RESTORE_STACK_SIZE);
158 : : }
159 : :
160 : : #define TASK_ENTRIES_SIZE 4096
161 : :
162 : : enum {
163 : : CR_STATE_FAIL = -1,
164 : : CR_STATE_RESTORE_NS = 0, /* is used for executing "setup-namespace" scripts */
165 : : CR_STATE_FORKING,
166 : : CR_STATE_RESTORE,
167 : : CR_STATE_RESTORE_SIGCHLD,
168 : : /*
169 : : * For security reason processes can be resumed only when all
170 : : * credentials are restored. Otherwise someone can attach to a
171 : : * process, which are not restored credentials yet and execute
172 : : * some code.
173 : : */
174 : : CR_STATE_RESTORE_CREDS,
175 : : CR_STATE_COMPLETE
176 : : };
177 : :
178 : : #define restore_finish_stage(__stage) ({ \
179 : : futex_dec_and_wake(&task_entries->nr_in_progress); \
180 : : futex_wait_while(&task_entries->start, __stage); \
181 : : (s32) futex_get(&task_entries->start); \
182 : : })
183 : :
184 : :
185 : : /* the restorer_blob_offset__ prefix is added by gen_offsets.sh */
186 : : #define restorer_sym(rblob, name) ((void *)(rblob) + restorer_blob_offset__##name)
187 : :
188 : : #define vma_priv(vma) ((vma_entry_is(vma, VMA_AREA_REGULAR)) && \
189 : : (vma_entry_is(vma, VMA_ANON_PRIVATE) || \
190 : : vma_entry_is(vma, VMA_FILE_PRIVATE)))
191 : :
192 : : #endif /* __CR_RESTORER_H__ */
|