forked from emilytouchingcomputers/CTFium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
309 lines (258 loc) · 7.73 KB
/
util.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
#define _GNU_SOURCE
#include "util.h"
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <err.h>
#include <stdio.h>
#include <elf.h>
#include <stdlib.h>
#include <linux/limits.h>
#define MIN(a, b) (a < b ? a : b)
ssize_t check(ssize_t ret, const char * const msg) {
if (ret == -1) {
err(1, "%s", msg);
}
return ret;
}
void die(const char *msg) {
fprintf(stderr, "%s\n", msg);
_exit(1);
}
static int is_link(int dir_fd, const char *path) {
struct stat sb;
check(fstatat(dir_fd, path, &sb, AT_SYMLINK_NOFOLLOW), "fstatat");
return (sb.st_mode & S_IFMT) == S_IFLNK;
}
int create_under_root(const char *root, const char *abs_path) {
char path[PATH_MAX];
char dir[PATH_MAX] = "/";
int cur_dir = check(open(root, O_PATH | O_CLOEXEC), "open");
strncpy(path, abs_path+1, sizeof(path)-1); // skip the leading slash
while (1) {
char *slash = strchr(path, '/');
if (!strcmp(path, "..")) {
// don't support .. at this point
die(".. in path");
}
if (!path[0]) {
die("empty path");
}
if (!slash) {
int fd = check(openat(cur_dir, path, O_WRONLY | O_CREAT | O_CLOEXEC | O_EXCL | O_NOFOLLOW, 0700), "openat");
check(close(cur_dir), "close");
return fd;
}
*slash = 0;
if (!is_link(cur_dir, path)) {
int next_dir = check(openat(cur_dir, path, O_PATH | O_CLOEXEC | O_NOFOLLOW), "openat");
check(close(cur_dir), "close");
if (strcmp(dir, "/") != 0) {
strncat(dir, "/", sizeof(dir)-strlen(dir)-1);
}
strncat(dir, path, sizeof(dir)-strlen(dir)-1);
cur_dir = next_dir;
memmove(path, slash+1, strlen(slash+1)+1);
continue;
}
// handle links
char linkname[PATH_MAX] = {0};
ssize_t link_len = check(readlinkat(cur_dir, path, linkname, sizeof(linkname)-1), "readlinkat");
if (linkname[0] == '/') {
check(close(cur_dir), "close");
if (linkname[link_len-1] != '/') {
strncat(linkname, "/", sizeof(linkname)-strlen(linkname)-1);
}
strncat(linkname, slash+1, sizeof(linkname)-strlen(linkname)-1);
return create_under_root(root, linkname);
}
char new_path[PATH_MAX];
strncpy(new_path, dir, sizeof(new_path)-1);
// relative link
while (1) {
char *link_slash = strchr(linkname, '/');
if (link_slash) {
*link_slash = 0;
}
if (strcmp(linkname, "..") == 0) {
if (strrchr(new_path, '/') != new_path) {
*strrchr(new_path, '/') = 0;
} else {
new_path[1] = 0;
}
} else if (strcmp(linkname, ".") == 0) {
// do nothing
} else if (!linkname[0]) {
// do nothing
} else {
if (strcmp(new_path, "/") != 0) {
strncat(new_path, "/", sizeof(new_path)-strlen(new_path)-1);
}
strncat(new_path, linkname, sizeof(new_path)-strlen(new_path)-1);
}
if (!link_slash) {
break;
}
memmove(linkname, link_slash+1, strlen(link_slash+1)+1);
}
// resolved the directory link, append the rest of the path and start over
if (strcmp(new_path, "/") != 0) {
strncat(new_path, "/", sizeof(new_path)-strlen(new_path)-1);
}
strncat(new_path, slash+1, sizeof(new_path)-strlen(new_path)-1);
check(close(cur_dir), "close");
return create_under_root(root, new_path);
}
}
void copy_fd(int in, int out) {
ssize_t read_cnt;
char buf[4096];
while ((read_cnt = check(read(in, buf, sizeof(buf)), "read(copy_fd)")) > 0) {
if (write(out, buf, read_cnt) != read_cnt) {
err(1, "write(copy_fd)");
}
}
}
void readn(int fd, void *buf, size_t len) {
ssize_t read_cnt;
while ((read_cnt = check(read(fd, buf, len), "readn")) > 0) {
buf += read_cnt;
len -= read_cnt;
}
}
void writen(int fd, const void *buf, size_t len) {
ssize_t write_cnt;
while ((write_cnt = check(write(fd, buf, len), "writen")) > 0) {
buf += write_cnt;
len -= write_cnt;
}
}
void *check_malloc(size_t size) {
void *ret = malloc(size);
if (!ret) {
err(1, "malloc");
}
return ret;
}
char *read_str(int fd) {
unsigned long long sz = read_ull(fd);
if (!sz) {
die("read_str sz == 0");
}
char *buf = (char*) check_malloc(sz);
readn(fd, buf, sz);
buf[sz-1] = 0;
return buf;
}
unsigned long long read_ull(int fd) {
unsigned long long ret;
readn(fd, &ret, sizeof(ret));
return ret;
}
void send_ull(int fd, unsigned long long l) {
writen(fd, &l, sizeof(l));
}
void send_str(int chan, const char *s) {
size_t len = strlen(s)+1;
send_ull(chan, len);
writen(chan, s, len);
}
void send_fd(int chan, int fd) {
char buf[1] = {0};
struct iovec data = {.iov_base = buf, .iov_len = 1};
struct msghdr msg = {0};
msg.msg_iov = &data;
msg.msg_iovlen = 1;
char ctl_buf[CMSG_SPACE(sizeof(int))];
msg.msg_control = ctl_buf;
msg.msg_controllen = sizeof(ctl_buf);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
*(int*)CMSG_DATA(cmsg) = fd;
msg.msg_controllen = cmsg->cmsg_len;
ssize_t send_len = check(sendmsg(chan, &msg, 0), "sendmsg(fd)");
if (send_len != 1) {
err(1, "sendmsg(fd len)");
}
check(close(fd), "close(send fd)");
}
int recv_fd(int chan) {
char buf[1] = {0};
struct iovec data = {.iov_base = buf, .iov_len = 1};
struct msghdr msg = {0};
msg.msg_iov = &data;
msg.msg_iovlen = 1;
char ctl_buf[CMSG_SPACE(sizeof(int))];
msg.msg_control = ctl_buf;
msg.msg_controllen = sizeof(ctl_buf);
ssize_t recv_len = check(recvmsg(chan, &msg, 0), "recvmsg(fd)");
for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
int fd = *(int *) CMSG_DATA(cmsg);
make_cloexec(fd);
return fd;
}
}
die("no fd received");
}
void make_cloexec(int fd) {
int flags = check(fcntl(fd, F_GETFD), "fcntl(F_GETFD)");
check(fcntl(fd, F_SETFD, flags | FD_CLOEXEC), "fcntl(F_SETFD)");
}
void copy_fd_len(int in, int out, size_t len) {
ssize_t read_cnt;
char buf[4096];
while ((read_cnt = check(read(in, buf, MIN(sizeof(buf), len)), "read(copy_fd_len)")) > 0) {
len -= read_cnt;
if (write(out, buf, read_cnt) != read_cnt) {
err(1, "write(copy_fd_len)");
}
}
}
void send_pid(int chan) {
char buf[1] = {0};
struct iovec data = {.iov_base = buf, .iov_len = 1};
struct msghdr msg = {0};
msg.msg_iov = &data;
msg.msg_iovlen = 1;
struct ucred creds = {0};
creds.pid = getpid();
creds.uid = getuid();
creds.gid = getgid();
char ctl_buf[CMSG_SPACE(sizeof(creds))];
msg.msg_control = ctl_buf;
msg.msg_controllen = sizeof(ctl_buf);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_CREDENTIALS;
cmsg->cmsg_len = CMSG_LEN(sizeof(creds));
*(struct ucred *)CMSG_DATA(cmsg) = creds;
msg.msg_controllen = cmsg->cmsg_len;
ssize_t send_len = check(sendmsg(chan, &msg, 0), "sendmsg(creds)");
if (send_len != 1) {
err(1, "sendmsg(creds len)");
}
}
int recv_pid(int chan) {
char buf[1] = {0};
struct iovec data = {.iov_base = buf, .iov_len = 1};
struct msghdr msg = {0};
msg.msg_iov = &data;
msg.msg_iovlen = 1;
char ctl_buf[CMSG_SPACE(sizeof(int))];
msg.msg_control = ctl_buf;
msg.msg_controllen = sizeof(ctl_buf);
ssize_t recv_len = check(recvmsg(chan, &msg, 0), "recvmsg(fd)");
for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) {
struct ucred *creds = (struct ucred *) CMSG_DATA(cmsg);
return creds->pid;
}
}
die("no credentials received");
}