-
Notifications
You must be signed in to change notification settings - Fork 52
/
path_resolver.c
558 lines (466 loc) · 16.6 KB
/
path_resolver.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
#include <string.h>
#include "uv.h"
#include "uvwasi.h"
#include "uvwasi_alloc.h"
#include "uv_mapping.h"
#include "path_resolver.h"
#define UVWASI__MAX_SYMLINK_FOLLOWS 32
#ifndef _WIN32
# define IS_SLASH(c) ((c) == '/')
#else
# define IS_SLASH(c) ((c) == '/' || (c) == '\\')
#endif /* _WIN32 */
static int uvwasi__is_absolute_path(const char* path, uvwasi_size_t path_len) {
/* It's expected that only Unix style paths will be generated by WASI. */
return path != NULL && path_len > 0 && path[0] == '/';
}
static char* uvwasi__strchr_slash(const char* s) {
/* strchr() that identifies /, as well as \ on Windows. */
do {
if (IS_SLASH(*s))
return (char*) s;
} while (*s++);
return NULL;
}
static uvwasi_errno_t uvwasi__combine_paths(const uvwasi_t* uvwasi,
const char* path1,
uvwasi_size_t path1_len,
const char* path2,
uvwasi_size_t path2_len,
char** combined_path,
uvwasi_size_t* combined_len) {
/* This function joins two paths with '/'. */
uvwasi_errno_t err;
char* combined;
int combined_size;
int r;
*combined_path = NULL;
*combined_len = 0;
/* The max combined size is the path1 length + the path2 length
+ 2 for a terminating NULL and a possible path separator. */
combined_size = path1_len + path2_len + 2;
combined = uvwasi__malloc(uvwasi, combined_size);
if (combined == NULL) return UVWASI_ENOMEM;
r = snprintf(combined, combined_size, "%s/%s", path1, path2);
if (r <= 0) {
err = uvwasi__translate_uv_error(uv_translate_sys_error(errno));
goto exit;
}
err = UVWASI_ESUCCESS;
*combined_path = combined;
*combined_len = strlen(combined);
exit:
if (err != UVWASI_ESUCCESS) uvwasi__free(uvwasi, combined);
return err;
}
uvwasi_errno_t uvwasi__normalize_path(const char* path,
uvwasi_size_t path_len,
char* normalized_path,
uvwasi_size_t normalized_len) {
const char* cur;
char* ptr;
char* next;
char* last;
size_t cur_len;
int is_absolute;
if (path_len > normalized_len)
return UVWASI_ENOBUFS;
is_absolute = uvwasi__is_absolute_path(path, path_len);
normalized_path[0] = '\0';
ptr = normalized_path;
for (cur = path; cur != NULL; cur = next + 1) {
next = uvwasi__strchr_slash(cur);
cur_len = (next == NULL) ? strlen(cur) : (size_t) (next - cur);
if (cur_len == 0) {
if (ptr == normalized_path && next != NULL && is_absolute) {
*ptr = '/';
ptr++;
}
*ptr = '\0';
} else if (cur_len == 1 && cur[0] == '.') {
/* No-op. Just consume the '.' */
} else if (cur_len == 2 && cur[0] == '.' && cur[1] == '.') {
/* Identify the path segment that preceded the current one. */
last = ptr;
while (!IS_SLASH(*last) && last != normalized_path) {
last--;
}
/* If the result is currently empty, or the last prior path is also '..'
then output '..'. Otherwise, remove the last path segment. */
if (ptr == normalized_path ||
(last == ptr - 2 && last[0] == '.' && last[1] == '.') ||
(last == ptr - 3 && last[0] == '/' &&
last[1] == '.' && last[2] == '.')) {
if (ptr != normalized_path && *(ptr - 1) != '/') {
*ptr = '/';
ptr++;
}
*ptr = '.';
ptr++;
*ptr = '.';
ptr++;
} else {
/* Strip the last segment, but make sure not to strip the '/' if that
is the entire path. */
if (last == normalized_path && *last == '/')
ptr = last + 1;
else
ptr = last;
}
*ptr = '\0';
} else {
if (ptr != normalized_path && *(ptr - 1) != '/') {
*ptr = '/';
ptr++;
}
memcpy(ptr, cur, cur_len);
ptr += cur_len;
*ptr = '\0';
}
if (next == NULL)
break;
}
/* Normalized the path to the empty string. Return either '/' or '.'. */
if (ptr == normalized_path) {
if (1 == is_absolute)
*ptr = '/';
else
*ptr = '.';
ptr++;
*ptr = '\0';
}
return UVWASI_ESUCCESS;
}
static int uvwasi__is_path_sandboxed(const char* path,
uvwasi_size_t path_len,
const char* fd_path,
uvwasi_size_t fd_path_len) {
char* ptr;
int remaining_len;
if (1 == uvwasi__is_absolute_path(fd_path, fd_path_len))
return path == strstr(path, fd_path) ? 1 : 0;
/* Handle relative fds that normalized to '.' */
if (fd_path_len == 1 && fd_path[0] == '.') {
/* If the fd's path is '.', then any path does not begin with '..' is OK. */
if ((path_len == 2 && path[0] == '.' && path[1] == '.') ||
(path_len > 2 && path[0] == '.' && path[1] == '.' && path[2] == '/')) {
return 0;
}
return 1;
}
if (path != strstr(path, fd_path))
return 0;
/* Fail if the remaining path starts with '..', '../', '/..', or '/../'. */
ptr = (char*) path + fd_path_len;
remaining_len = path_len - fd_path_len;
if (remaining_len < 2)
return 1;
/* Strip a leading slash so the check is only for '..' and '../'. */
if (*ptr == '/') {
ptr++;
remaining_len--;
}
if ((remaining_len == 2 && ptr[0] == '.' && ptr[1] == '.') ||
(remaining_len > 2 && ptr[0] == '.' && ptr[1] == '.' && ptr[2] == '/')) {
return 0;
}
return 1;
}
static uvwasi_errno_t uvwasi__normalize_absolute_path(
const uvwasi_t* uvwasi,
const struct uvwasi_fd_wrap_t* fd,
const char* path,
uvwasi_size_t path_len,
char** normalized_path,
uvwasi_size_t* normalized_len
) {
/* This function resolves an absolute path to the provided file descriptor.
If the file descriptor's path is relative, then this operation will fail
with UVWASI_ENOTCAPABLE since it doesn't make sense to resolve an absolute
path to a relative prefix. If the file desciptor's path is also absolute,
then we just need to verify that the normalized path still starts with
the file descriptor's path. */
uvwasi_errno_t err;
char* abs_path;
int abs_size;
*normalized_path = NULL;
*normalized_len = 0;
abs_size = path_len + 1;
abs_path = uvwasi__malloc(uvwasi, abs_size);
if (abs_path == NULL) {
err = UVWASI_ENOMEM;
goto exit;
}
/* Normalize the input path first. */
err = uvwasi__normalize_path(path, path_len, abs_path, path_len);
if (err != UVWASI_ESUCCESS)
goto exit;
/* Once the input is normalized, ensure that it is still sandboxed. */
if (0 == uvwasi__is_path_sandboxed(abs_path,
path_len,
fd->normalized_path,
strlen(fd->normalized_path))) {
err = UVWASI_ENOTCAPABLE;
goto exit;
}
*normalized_path = abs_path;
*normalized_len = abs_size - 1;
return UVWASI_ESUCCESS;
exit:
uvwasi__free(uvwasi, abs_path);
return err;
}
static uvwasi_errno_t uvwasi__normalize_relative_path(
const uvwasi_t* uvwasi,
const struct uvwasi_fd_wrap_t* fd,
const char* path,
uvwasi_size_t path_len,
char** normalized_path,
uvwasi_size_t* normalized_len
) {
/* This function resolves a relative path to the provided file descriptor.
The relative path is concatenated to the file descriptor's path, and then
normalized. */
uvwasi_errno_t err;
char* combined;
char* normalized;
uvwasi_size_t combined_len;
uvwasi_size_t fd_path_len;
uvwasi_size_t norm_len;
*normalized_path = NULL;
*normalized_len = 0;
fd_path_len = strlen(fd->normalized_path);
err = uvwasi__combine_paths(uvwasi,
fd->normalized_path,
fd_path_len,
path,
path_len,
&combined,
&combined_len);
if (err != UVWASI_ESUCCESS) goto exit;
normalized = uvwasi__malloc(uvwasi, combined_len + 1);
if (normalized == NULL) {
err = UVWASI_ENOMEM;
goto exit;
}
/* Normalize the input path. */
err = uvwasi__normalize_path(combined,
combined_len,
normalized,
combined_len);
if (err != UVWASI_ESUCCESS)
goto exit;
norm_len = strlen(normalized);
/* Once the path is normalized, ensure that it is still sandboxed. */
if (0 == uvwasi__is_path_sandboxed(normalized,
norm_len,
fd->normalized_path,
fd_path_len)) {
err = UVWASI_ENOTCAPABLE;
goto exit;
}
err = UVWASI_ESUCCESS;
*normalized_path = normalized;
*normalized_len = norm_len;
exit:
if (err != UVWASI_ESUCCESS)
uvwasi__free(uvwasi, normalized);
uvwasi__free(uvwasi, combined);
return err;
}
static uvwasi_errno_t uvwasi__resolve_path_to_host(
const uvwasi_t* uvwasi,
const struct uvwasi_fd_wrap_t* fd,
const char* path,
uvwasi_size_t path_len,
char** resolved_path,
uvwasi_size_t* resolved_len
) {
/* Return the normalized path, but resolved to the host's real path. */
char* res_path;
char* stripped_path;
int real_path_len;
int fake_path_len;
int stripped_len;
#ifdef _WIN32
uvwasi_size_t i;
#endif /* _WIN32 */
real_path_len = strlen(fd->real_path);
fake_path_len = strlen(fd->normalized_path);
/* If the fake path is '.' just ignore it. */
if (fake_path_len == 1 && fd->normalized_path[0] == '.') {
fake_path_len = 0;
}
stripped_len = path_len - fake_path_len;
/* The resolved path's length is calculated as: the length of the fd's real
path, + 1 for a path separator, and the length of the input path (with the
fake path stripped off). */
*resolved_len = stripped_len + real_path_len + 1;
*resolved_path = uvwasi__malloc(uvwasi, *resolved_len + 1);
if (*resolved_path == NULL)
return UVWASI_ENOMEM;
res_path = *resolved_path;
stripped_path = (char*) path + fake_path_len;
memcpy(res_path, fd->real_path, real_path_len);
res_path += real_path_len;
if (stripped_len > 1 ||
(stripped_len == 1 && stripped_path[0] != '/')) {
if (stripped_path[0] != '/') {
*res_path = '/';
res_path++;
}
memcpy(res_path, stripped_path, stripped_len);
res_path += stripped_len;
}
*res_path = '\0';
#ifdef _WIN32
/* Replace / with \ on Windows. */
res_path = *resolved_path;
for (i = real_path_len; i < *resolved_len; i++) {
if (res_path[i] == '/')
res_path[i] = '\\';
}
#endif /* _WIN32 */
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi__resolve_path(const uvwasi_t* uvwasi,
const struct uvwasi_fd_wrap_t* fd,
const char* path,
uvwasi_size_t path_len,
char** resolved_path,
uvwasi_lookupflags_t flags) {
uv_fs_t req;
uvwasi_errno_t err;
const char* input;
char* host_path;
char* normalized_path;
char* link_target;
char* normalized_parent;
char* resolved_link_target;
uvwasi_size_t input_len;
uvwasi_size_t host_path_len;
uvwasi_size_t normalized_len;
uvwasi_size_t link_target_len;
uvwasi_size_t normalized_parent_len;
uvwasi_size_t resolved_link_target_len;
int follow_count;
int r;
input = path;
input_len = path_len;
link_target = NULL;
follow_count = 0;
host_path = NULL;
normalized_parent = NULL;
resolved_link_target = NULL;
start:
normalized_path = NULL;
err = UVWASI_ESUCCESS;
if (1 == uvwasi__is_absolute_path(input, input_len)) {
err = uvwasi__normalize_absolute_path(uvwasi,
fd,
input,
input_len,
&normalized_path,
&normalized_len);
} else {
err = uvwasi__normalize_relative_path(uvwasi,
fd,
input,
input_len,
&normalized_path,
&normalized_len);
}
if (err != UVWASI_ESUCCESS)
goto exit;
uvwasi__free(uvwasi, host_path);
err = uvwasi__resolve_path_to_host(uvwasi,
fd,
normalized_path,
normalized_len,
&host_path,
&host_path_len);
if (err != UVWASI_ESUCCESS)
goto exit;
if ((flags & UVWASI_LOOKUP_SYMLINK_FOLLOW) == UVWASI_LOOKUP_SYMLINK_FOLLOW) {
r = uv_fs_readlink(NULL, &req, host_path, NULL);
if (r != 0) {
#ifdef _WIN32
/* uv_fs_readlink() returns UV__UNKNOWN on Windows. Try to get a better
error using uv_fs_stat(). */
if (r == UV__UNKNOWN) {
uv_fs_req_cleanup(&req);
r = uv_fs_stat(NULL, &req, host_path, NULL);
if (r == 0) {
if (uvwasi__stat_to_filetype(&req.statbuf) !=
UVWASI_FILETYPE_SYMBOLIC_LINK) {
r = UV_EINVAL;
}
}
/* Fall through. */
}
#endif /* _WIN32 */
/* Don't report UV_EINVAL or UV_ENOENT. They mean that either the file
does not exist, or it is not a symlink. Both are OK. */
if (r != UV_EINVAL && r != UV_ENOENT)
err = uvwasi__translate_uv_error(r);
uv_fs_req_cleanup(&req);
goto exit;
}
/* Clean up memory and follow the link, unless it's time to return ELOOP. */
follow_count++;
if (follow_count >= UVWASI__MAX_SYMLINK_FOLLOWS) {
uv_fs_req_cleanup(&req);
err = UVWASI_ELOOP;
goto exit;
}
link_target_len = strlen(req.ptr);
uvwasi__free(uvwasi, link_target);
link_target = uvwasi__malloc(uvwasi, link_target_len + 1);
if (link_target == NULL) {
uv_fs_req_cleanup(&req);
err = UVWASI_ENOMEM;
goto exit;
}
memcpy(link_target, req.ptr, link_target_len + 1);
uv_fs_req_cleanup(&req);
if (1 == uvwasi__is_absolute_path(link_target, link_target_len)) {
input = link_target;
input_len = link_target_len;
} else {
uvwasi__free(uvwasi, normalized_parent);
uvwasi__free(uvwasi, resolved_link_target);
err = uvwasi__combine_paths(uvwasi,
normalized_path,
normalized_len,
"..",
2,
&normalized_parent,
&normalized_parent_len);
if (err != UVWASI_ESUCCESS) goto exit;
err = uvwasi__combine_paths(uvwasi,
normalized_parent,
normalized_parent_len,
link_target,
link_target_len,
&resolved_link_target,
&resolved_link_target_len);
if (err != UVWASI_ESUCCESS) goto exit;
input = resolved_link_target;
input_len = resolved_link_target_len;
}
uvwasi__free(uvwasi, normalized_path);
goto start;
}
exit:
if (err == UVWASI_ESUCCESS) {
*resolved_path = host_path;
} else {
*resolved_path = NULL;
uvwasi__free(uvwasi, host_path);
}
uvwasi__free(uvwasi, link_target);
uvwasi__free(uvwasi, normalized_path);
uvwasi__free(uvwasi, normalized_parent);
uvwasi__free(uvwasi, resolved_link_target);
return err;
}