Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -171,15 +171,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 @@ -192,6 +194,12 @@ FS.staticInit();
break;
}

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

current = FS.lookupNode(current, parts[i]);
current_path = PATH.join2(current_path, parts[i]);

Expand All @@ -207,7 +215,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 @@ -1033,7 +1044,6 @@ FS.staticInit();
if (typeof path == 'object') {
node = path;
} else {
path = PATH.normalize(path);
try {
var lookup = FS.lookupPath(path, {
follow: !(flags & {{{ cDefs.O_NOFOLLOW }}})
Expand Down
6 changes: 4 additions & 2 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ var SyscallsLibrary = {
}
return dir;
}
return PATH.join2(dir, path);
if (!dir.endsWith("/")) {
dir += "/"
}
return dir + path;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different to the old code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

join2 calls normalize which will incorrectly replace /a/b/.. with /a.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just return dir + '/' + path ? Or return '${dir}/${path}'?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll probably work fine, I put it in and will let the CI say.

},

doStat(func, path, buf) {
Expand Down Expand Up @@ -826,7 +829,6 @@ var SyscallsLibrary = {
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_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 @@ -5925,6 +5925,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_symlink_resolution.c', 'success', emcc_args=args)

def test_sigalrm(self):
self.do_runf('test_sigalrm.c', 'Received alarm!')
self.set_setting('EXIT_RUNTIME')
Expand Down
Loading