Skip to content

Commit c8d4f01

Browse files
authored
Error if creating symlink to absolute path (#272)
This commit adds an absolute path check to `path_symlink`. This behavior is specified by [WASI][1]. [1]: https://github.com/WebAssembly/wasi-filesystem/blob/main/path-resolution.md#symlinks fixes #271 Signed-off-by: Yage Hu <[email protected]>
1 parent 9811374 commit c8d4f01

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/uvwasi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,12 @@ uvwasi_errno_t uvwasi_path_symlink(uvwasi_t* uvwasi,
24012401
memcpy(truncated_old_path, old_path, old_path_len);
24022402
truncated_old_path[old_path_len] = '\0';
24032403

2404+
if (old_path_len > 0 && old_path[0] == '/') {
2405+
uv_mutex_unlock(&wrap->mutex);
2406+
uvwasi__free(uvwasi, truncated_old_path);
2407+
return UVWASI_EPERM;
2408+
}
2409+
24042410
err = uvwasi__resolve_path(uvwasi,
24052411
wrap,
24062412
new_path,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include "uvwasi.h"
6+
#include "uv.h"
7+
#include "test-common.h"
8+
9+
#define TEST_TMP_DIR "./out/tmp"
10+
#define LINK_CONTENT "/absolute"
11+
#define TEST_FILE "./out/tmp/test-path-symlink-absolute-path.link"
12+
13+
int main(void) {
14+
uvwasi_t uvwasi;
15+
uvwasi_options_t init_options;
16+
uvwasi_errno_t err;
17+
uv_fs_t req;
18+
int r;
19+
20+
setup_test_environment();
21+
22+
r = uv_fs_mkdir(NULL, &req, TEST_TMP_DIR, 0777, NULL);
23+
uv_fs_req_cleanup(&req);
24+
assert(r == 0 || r == UV_EEXIST);
25+
26+
uvwasi_options_init(&init_options);
27+
init_options.preopenc = 1;
28+
init_options.preopens = calloc(1, sizeof(uvwasi_preopen_t));
29+
init_options.preopens[0].mapped_path = "/var";
30+
init_options.preopens[0].real_path = TEST_TMP_DIR;
31+
32+
err = uvwasi_init(&uvwasi, &init_options);
33+
assert(err == 0);
34+
35+
err = uvwasi_path_symlink(&uvwasi,
36+
LINK_CONTENT,
37+
strlen(LINK_CONTENT) + 1,
38+
3,
39+
TEST_FILE,
40+
strlen(TEST_FILE) + 1);
41+
assert(err == UVWASI_EPERM);
42+
43+
uvwasi_destroy(&uvwasi);
44+
free(init_options.preopens);
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)