-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
282 lines (232 loc) · 5.32 KB
/
main.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
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <libgen.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/sendfile.h>
#include "manager/swapfd.h"
#include "include/ptrace.h"
#include "include/pie-util-fd.h"
#define SRC_FILE "src.txt"
#define SRC_FILE2 "src2.txt"
#define DST_FILE "dst.txt"
#define DST_FILE2 "dst2.txt"
#define CGROUP_DIR "/sys/fs/cgroup/freezer/user/kirill/3"
static int move_to_freezer_cgroup(pid_t child)
{
char path[PATH_MAX], buf[16];
int fd, len, ret = 0;
sprintf(path, "%s/cgroup.procs", CGROUP_DIR);
fd = open(path, O_WRONLY);
if (fd < 0) {
perror("Can't open cgroup.procs");
return -1;
}
sprintf(buf, "%d", child);
len = strlen(buf);
if (write(fd, buf, len) != len) {
perror("Can't move to cgroup");
ret = -1;
}
close(fd);
return ret;
}
static int change_cgroup_state(const char *state)
{
char path[PATH_MAX];
int fd, len, ret = 0;
sprintf(path, "%s/freezer.state", CGROUP_DIR);
fd = open(path, O_WRONLY);
if (fd < 0) {
perror("Can't open cgroup.state");
return -1;
}
len = strlen(state);
if (write(fd, state, len) != len) {
fprintf(stderr, "Can't make cgroup %s, errno=%d\n", state, errno);
ret = -1;
}
close(fd);
return ret;
}
static int copy_exe(void)
{
int exe, new_exe, ret;
exe = open("/proc/self/exe", O_RDONLY);
if (exe < 0) {
perror("Can't open exe");
return -1;
}
new_exe = open("/tmp/test_exe", O_RDWR|O_CREAT, 0777);
if (new_exe < 0) {
perror("Can't create exe");
return -1;
}
while ((ret = sendfile(new_exe, exe, NULL, 1024)) > 0)
;
if (ret < 0) {
perror("sendfile");
return -1;
}
close(exe);
return new_exe;
}
static int set_locks(int fd)
{
struct flock lock;
memset(&lock, 0, sizeof(struct flock));
lock.l_type = F_WRLCK;
lock.l_start=5;
lock.l_whence = SEEK_SET;
lock.l_len=0;
lock.l_pid = getpid();
if (fcntl(fd, F_SETLK, &lock) < 0)
return -1;
lock.l_start=0;
lock.l_whence = SEEK_SET;
lock.l_len=2;
if (fcntl(fd, F_SETLK, &lock) < 0)
return -1;
return 0;
}
static void do_something(int *pipe_fd, int fd)
{
void *addr;
if (set_locks(fd) < 0) {
perror("Can't set locks");
}
addr = mmap(0, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
perror("Can't mmap");
return;
}
printf("Setting address %lx\n", (unsigned long)addr);
addr = mmap(0, 2 * PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED) {
perror("Can't mmap");
return;
}
*(char *)addr = 0;
printf("Setting address %lx\n", (unsigned long)addr);
if (write(pipe_fd[1], &addr, sizeof(addr)) != sizeof(addr)) {
perror("Can't write");
return;
}
while (1)
sleep(10);
}
int main()
{
unsigned long setfd[3] = { FD_CLOEXEC, 0, FD_CLOEXEC };
int src[3], dst[3], ret, exe, cwd_fd;
unsigned long addr = 0x12345678;
unsigned size = sizeof(addr);
struct swapfd_exchange se;
struct parasite_ctl *ctl;
pid_t child;
int fd[2], st = TASK_ALIVE;
memset(&se, 0, sizeof(se));
if (unlink(DST_FILE) < 0)
printf("Can't unlink\n");
if (unlink(SRC_FILE) < 0)
printf("Can't unlink\n");
src[0] = open(SRC_FILE, O_RDWR|O_CREAT|O_CLOEXEC, 0777);
dst[0] = open(DST_FILE, O_RDWR|O_CREAT, 0777);
src[1] = open(SRC_FILE2, O_RDWR|O_CREAT|O_CLOEXEC, 0777);
dst[1] = open(DST_FILE2, O_RDWR|O_CREAT, 0777);
src[2] = open("/proc/self/exe", O_RDONLY);
dst[2] = exe = copy_exe();
cwd_fd = open("/tmp", O_RDONLY);
if (src[0] < 0 || dst[0] < 0 || src[1] < 0 || dst[1] < 0 || src[2] < 0 || dst[2] < 0 || cwd_fd < 0) {
perror("main: Can't open");
return 1;
}
if (write(dst[0], &addr, size) != size) {
perror("Can't write");
return 1;
}
if (write(src[0], &addr, size) != size) {
perror("Can't write");
return 1;
}
if (pipe(fd)) {
perror("Can't pipe");
return 1;
}
child = fork();
if (child < 0) {
perror("Can't fork");
return 1;
} else if (child == 0) {
close(dst[0]);
close(dst[1]);
close(dst[2]);
close(cwd_fd);
do_something(fd, src[0]);
return 0;
}
ret = 1;
if (read(fd[0], &addr, size) != size) {
perror("Can't read");
goto out_kill;
}
if (move_to_freezer_cgroup(child) < 0)
goto out_kill;
if (change_cgroup_state("FROZEN") < 0)
goto out_kill;
kill(child, SIGSTOP);
/* Entry point */
if (attach_to_task(child) != child) {
change_cgroup_state("THAWED");
goto out_kill;
}
if (change_cgroup_state("THAWED") < 0)
goto out_detach;
st = wait_task_seized(child);
if (st < 0)
goto out_detach;
se.pid = child;
se.addr = &addr;
se.addr_fd = &dst[0];
se.naddr = 1;
se.src_fd = src;
se.dst_fd = dst;
se.setfd = setfd;
se.nfd = 3;
se.exe_fd = exe;
se.cwd_fd = cwd_fd;
se.root.cwd_fd = open("/", O_PATH);
se.root.path = "tmp";
if (se.root.cwd_fd < 0)
perror("Open /");
ret = set_parasite_ctl(se.pid, &ctl);
if (ret < 0)
goto out_detach;
#if 0
/* swapfd_tracee was removed as obsolete */
if (swapfd_tracee(ctl, &se) == 0)
ret = 0;
#endif
ret = -1;
if (swap_fd(ctl, src[0], dst[0], 0, 0) != 0)
goto out_destroy;
if (swap_map(ctl, src[0], addr, addr + 2 * PAGE_SIZE,
PROT_READ|PROT_WRITE, MAP_PRIVATE, 0) != 0)
goto out_destroy;
pr_debug("Success\n");
ret = 0;
out_destroy:
destroy_parasite_ctl(se.pid, ctl);
out_detach:
detach_from_task(child, st);
/* Exit point */
out_kill:
kill(child, SIGTERM);
return ret;
}