Skip to content

Fix path resolution for symlinks #23072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,17 @@ FS.staticInit();
// paths
//
lookupPath(path, opts = {}) {
path = PATH_FS.resolve(path);

if (!path) return { path: '', node: null };
opts.follow_mount ??= true

if (!PATH.isAbs(path)) {
path = FS.cwd() + '/' + path;
}

// limit max consecutive symlinks to 40 (SYMLOOP_MAX).
linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) {
// split the absolute path
var parts = path.split('/').filter((p) => !!p);
var parts = path.split('/').filter((p) => !!p && (p !== '.'));

// start at the root
var current = FS.root;
Expand All @@ -193,6 +195,12 @@ FS.staticInit();
break;
}

if (parts[i] === '..') {
current_path = PATH.dirname(current_path);
current = current.parent;
continue;
}

current_path = PATH.join2(current_path, parts[i]);
try {
current = FS.lookupNode(current, parts[i]);
Expand All @@ -218,7 +226,10 @@ FS.staticInit();
throw new FS.ErrnoError({{{ cDefs.ENOSYS }}});
}
var link = current.node_ops.readlink(current);
path = PATH_FS.resolve(PATH.dirname(current_path), link, ...parts.slice(i + 1));
if (!PATH.isAbs(link)) {
link = PATH.dirname(current_path) + '/' + link;
}
path = link + '/' + parts.slice(i + 1).join('/');
continue linkloop;
}
}
Expand Down Expand Up @@ -1045,7 +1056,6 @@ FS.staticInit();
if (typeof path == 'object') {
node = path;
} else {
path = PATH.normalize(path);
// noent_okay makes it so that if the final component of the path
// doesn't exist, lookupPath returns `node: undefined`. `path` will be
// updated to point to the target of all symlinks.
Expand Down
6 changes: 1 addition & 5 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var SyscallsLibrary = {
}
return dir;
}
return PATH.join2(dir, path);
return dir + '/' + path;
},

doStat(func, path, buf) {
Expand Down Expand Up @@ -833,10 +833,6 @@ var SyscallsLibrary = {
__syscall_mkdirat: (dirfd, path, mode) => {
path = SYSCALLS.getStr(path);
path = SYSCALLS.calculateAt(dirfd, path);
// remove a trailing slash, if one - /a/b/ has basename of '', but
// we want to create b in the context of this function
path = PATH.normalize(path);
if (path[path.length-1] === '/') path = path.substr(0, path.length-1);
FS.mkdir(path, mode, 0);
return 0;
},
Expand Down
57 changes: 57 additions & 0 deletions test/fs/test_fs_symlink_resolution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <unistd.h>
#include <fcntl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#if defined(__EMSCRIPTEN__)
#include "emscripten.h"
#endif

void makedir(const char *dir) {
int rtn = mkdir(dir, 0777);
assert(rtn == 0);
}

void changedir(const char *dir) {
int rtn = chdir(dir);
assert(rtn == 0);
}

static void create_file(const char *path) {
printf("creating: %s\n", path);
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0777);
assert(fd >= 0);

close(fd);
}

void setup() {
#if defined(__EMSCRIPTEN__) && defined(NODEFS)
makedir("working");
EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working'));
changedir("working");
#endif
makedir("a");
makedir("b");
makedir("b/c");
symlink("../b/c", "a/link");
}


int main() {
setup();
create_file("a/link/../x.txt");
struct stat statBuf;
assert(stat("a/link/../x.txt", &statBuf) == 0);
assert(stat("b/x.txt", &statBuf) == 0);
makedir("a/link/../d");
assert(stat("a/link/../d", &statBuf) == 0);
assert(stat("b/d", &statBuf) == 0);

assert(truncate("a/link/../x.txt", 0) == 0);
assert(chmod("a/link/../x.txt", 0777) == 0);
printf("success\n");
}
14 changes: 14 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5874,6 +5874,20 @@ def test_fs_64bit(self):
self.set_setting('FORCE_FILESYSTEM')
self.do_runf('fs/test_64bit.c', 'success')

@requires_node
@parameterized({
'': ([],),
'nodefs': (['-DNODEFS', '-lnodefs.js'],),
'noderawfs': (['-sNODERAWFS'],),
})
def test_fs_symlink_resolution(self, args):
nodefs = '-DNODEFS' in args or '-sNODERAWFS' in args
if self.get_setting('WASMFS'):
if nodefs:
self.skipTest('NODEFS in WasmFS')
self.set_setting('FORCE_FILESYSTEM')
self.do_runf('fs/test_fs_symlink_resolution.c', 'success', emcc_args=args)

@parameterized({
'': ([],),
'nodefs': (['-DNODEFS', '-lnodefs.js'],),
Expand Down
4 changes: 0 additions & 4 deletions test/wasmfs/wasmfs_mkdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ int main() {
// Try to make the root directory.
errno = 0;
mkdir("/", 0777);
#ifdef WASMFS
assert(errno == EEXIST);
#else
assert(errno == EINVAL);
#endif

// Try to make a directory that exists already.
errno = 0;
Expand Down
Loading