File: spy.c

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (294 lines) | stat: -rw-r--r-- 6,159 bytes parent folder | download
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/mman.h>

#include <common/page.h>

#include <compel/log.h>
#include <compel/infect-rpc.h>
#include <errno.h>

#include "parasite.h"

#define PARASITE_CMD_INC PARASITE_USER_CMDS
#define PARASITE_CMD_DEC PARASITE_USER_CMDS + 1

#define err_and_ret(msg)              \
	do {                          \
		fprintf(stderr, msg); \
		return -1;            \
	} while (0)

void *saved_data = NULL;

#define SAVED_DATA_MAX page_size()

void cleanup_saved_data(void)
{
	free(saved_data);
}

static void print_vmsg(unsigned int lvl, const char *fmt, va_list parms)
{
	printf("\tLC%u: ", lvl);
	vprintf(fmt, parms);
}

static void *get_parasite_rstack_start(struct parasite_ctl *ctl)
{
	void *rstack, *r_thread_stack, *rstack_start;

	compel_get_stack(ctl, &rstack, &r_thread_stack);

	rstack_start = rstack;
	if (r_thread_stack != NULL && r_thread_stack < rstack_start)
		rstack_start = r_thread_stack;

	return rstack_start;
}

static void *read_proc_mem(int pid, void *offset, size_t len)
{
	char victim_mem_path[6 + 11 + 4 + 1];
	int written;
	int fd;
	void *data;
	ssize_t mem_read;

	written = snprintf(victim_mem_path, sizeof(victim_mem_path), "/proc/%d/mem", pid);
	if (written < 0 || written >= sizeof(victim_mem_path)) {
		fprintf(stderr, "Failed to create path string to victim's /proc/%d/mem file\n", pid);
		return NULL;
	}

	fd = open(victim_mem_path, O_RDONLY);
	if (fd < 0) {
		perror("Failed to open victim's /proc/$pid/mem file");
		return NULL;
	}

	data = malloc(len);
	if (data == NULL) {
		perror("Can't allocate memory to read victim's /proc/$pid/mem file");
		return NULL;
	}

	mem_read = pread(fd, data, len, (off_t)offset);
	if (mem_read == -1) {
		perror("Failed to read victim's /proc/$pid/mem file");
		goto freebuf;
	}

	return data;

freebuf:
	free(data);
	return NULL;
}

static int check_saved_data(struct parasite_ctl *ctl, int pid, void *stack, void *saved_data, size_t saved_data_size)
{
	if (saved_data != NULL) {
		void *current_data;

		current_data = read_proc_mem(pid, stack, saved_data_size);
		if (current_data == NULL)
			return -1;

		if (memcmp(saved_data, current_data, saved_data_size) != 0)
			return 1;
	}

	return 0;
}

static int do_infection(int pid)
{
	int state;
	struct parasite_ctl *ctl;
	struct infect_ctx *ictx;
	int *arg;
	void *stack;
	size_t saved_data_size = PARASITE_STACK_REDZONE;
	int saved_data_check;

	compel_log_init(print_vmsg, COMPEL_LOG_DEBUG);

	printf("Stopping task\n");
	state = compel_stop_task(pid);
	if (state < 0)
		err_and_ret("Can't stop task\n");

	printf("Preparing parasite ctl\n");
	ctl = compel_prepare(pid);
	if (!ctl)
		err_and_ret("Can't prepare for infection\n");

	printf("Configuring contexts\n");

	/*
	 * First -- the infection context. Most of the stuff
	 * is already filled by compel_prepare(), just set the
	 * log descriptor for parasite side, library cannot
	 * live w/o it.
	 */
	ictx = compel_infect_ctx(ctl);
	ictx->log_fd = STDERR_FILENO;

	parasite_setup_c_header(ctl);

	printf("Infecting\n");
	if (compel_infect_no_daemon(ctl, 1, sizeof(int)))
		err_and_ret("Can't infect victim\n");

	if (atexit(cleanup_saved_data))
		err_and_ret("Can't register cleanup function with atexit\n");

	stack = get_parasite_rstack_start(ctl);

	if (compel_start_daemon(ctl))
		err_and_ret("Can't start daemon in victim\n");

	/*
	 * Now get the area with arguments and run two
	 * commands one by one.
	 */
	arg = compel_parasite_args(ctl, int);

	printf("Running cmd 1\n");
	*arg = 137;
	if (compel_rpc_call_sync(PARASITE_CMD_INC, ctl))
		err_and_ret("Can't run parasite command 1\n");

	printf("Running cmd 2\n");
	*arg = 404;
	if (compel_rpc_call_sync(PARASITE_CMD_DEC, ctl))
		err_and_ret("Can't run parasite command 2\n");

	saved_data_check = check_saved_data(ctl, pid, stack, saved_data, saved_data_size);
	if (saved_data_check == -1)
		err_and_ret("Could not check saved data\n");
	if (saved_data_check != 0)
		err_and_ret("Saved data unexpectedly modified\n");

	/*
	 * Done. Cure and resume the task.
	 */
	printf("Curing\n");
	if (compel_cure(ctl))
		err_and_ret("Can't cure victim\n");

	if (compel_resume_task(pid, state, state))
		err_and_ret("Can't unseize task\n");

	printf("Done\n");

	return 0;
}

static inline int chk(int fd, int val)
{
	int v = 0;

	if (read(fd, &v, sizeof(v)) != sizeof(v))
		return 1;

	printf("%d, want %d\n", v, val);
	return v != val;
}

int main(int argc, char **argv)
{
	int p_in[2], p_out[2], p_err[2], pid, i, err = 0;

	/*
	 * Prepare IO-s and fork the victim binary
	 */
	if (pipe(p_in) || pipe(p_out) || pipe(p_err)) {
		perror("Can't make pipe");
		return -1;
	}

	pid = vfork();
	if (pid == 0) {
		close(p_in[1]);
		dup2(p_in[0], 0);
		close(p_in[0]);
		close(p_out[0]);
		dup2(p_out[1], 1);
		close(p_out[1]);
		close(p_err[0]);
		dup2(p_err[1], 2);
		close(p_err[1]);
		execl("./victim", "victim", NULL);
		exit(1);
	}

	close(p_in[0]);
	close(p_out[1]);
	close(p_err[1]);

	/*
	 * Tell the little guy some numbers
	 */
	i = 1;
	if (write(p_in[1], &i, sizeof(i)) != sizeof(i))
		return 1;
	i = 42;
	if (write(p_in[1], &i, sizeof(i)) != sizeof(i))
		return 1;

	printf("Checking the victim alive\n");
	err = chk(p_out[0], 1);
	if (err)
		return 1;
	err = chk(p_out[0], 42);
	if (err)
		return 1;

	/*
	 * Now do the infection with parasite.c
	 */

	printf("Infecting the victim\n");
	if (do_infection(pid))
		return 1;

	/*
	 * Tell the victim some more stuff to check it's alive
	 */
	i = 1234;
	if (write(p_in[1], &i, sizeof(i)) != sizeof(i))
		return 1;
	i = 4096;
	if (write(p_in[1], &i, sizeof(i)) != sizeof(i))
		return 1;

	/*
	 * Stop the victim and check the infection went well
	 */
	printf("Closing victim stdin\n");
	close(p_in[1]);
	printf("Waiting for victim to die\n");
	wait(NULL);

	printf("Checking the result\n");

	/* These two came from parasite */
	err = chk(p_out[0], 138);
	err |= chk(p_out[0], 403);

	/* These two came from post-infect */
	err |= chk(p_out[0], 1234);
	err |= chk(p_out[0], 4096);

	if (!err)
		printf("All OK\n");
	else
		printf("Something went WRONG\n");

	return 0;
}