Skip to content

Commit

Permalink
Merge pull request #76 from fischerling/fix-resolve-infinite-loop
Browse files Browse the repository at this point in the history
Fix `resolve_path` infinite loop
  • Loading branch information
Galfurian committed Apr 5, 2024
2 parents 7c92356 + 2a566df commit 9d6764c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
5 changes: 4 additions & 1 deletion mentos/src/fs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ int resolve_path(const char *path, char *buffer, size_t buflen, int flags)
if (symlinks > SYMLOOP_MAX) { return -ELOOP; }

char link[PATH_MAX];
ssize_t nbytes = sys_readlink(abspath, link, sizeof(link));
vfs_file_t* link_file = vfs_open_abspath(abspath, O_RDONLY, 0);
if (link_file == NULL) { return -errno; }
ssize_t nbytes = vfs_readlink(link_file, link, sizeof(link));
vfs_close(link_file);
if (nbytes == -1) { return -errno; }

// Null-terminate link
Expand Down
31 changes: 14 additions & 17 deletions mentos/src/fs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,21 @@ vfs_file_t *vfs_open(const char *path, int flags, mode_t mode)
{
pr_debug("vfs_open(path: %s, flags: %d, mode: %d)\n", path, flags, mode);
assert(path && "Provided null path.");
if (path[0] != '/') {
// Allocate a variable for the path.
char absolute_path[PATH_MAX];
// Resolve all symbolic links in the path before opening the file.
int resolve_flags = FOLLOW_LINKS;
// Allow the last component to be non existing when attempting to create it.
if (bitmask_check(flags, O_CREAT)) {
resolve_flags |= CREAT_LAST_COMPONENT;
}
int ret = resolve_path(path, absolute_path, sizeof(absolute_path), resolve_flags);
if (ret < 0) {
pr_err("vfs_open(%s): Cannot resolve path!\n", path);
errno = -ret;
return NULL;
}
return vfs_open_abspath(absolute_path, flags, mode);
// Allocate a variable for the path.
char absolute_path[PATH_MAX];
// Resolve all symbolic links in the path before opening the file.
int resolve_flags = FOLLOW_LINKS;
// Allow the last component to be non existing when attempting to create it.
if (bitmask_check(flags, O_CREAT)) {
resolve_flags |= CREAT_LAST_COMPONENT;
}
int ret = resolve_path(path, absolute_path, sizeof(absolute_path), resolve_flags);
if (ret < 0) {
pr_err("vfs_open(%s): Cannot resolve path!\n", path);
errno = -ret;
return NULL;
}
return vfs_open_abspath(path, flags, mode);
return vfs_open_abspath(absolute_path, flags, mode);
}

int vfs_close(vfs_file_t *file)
Expand Down

0 comments on commit 9d6764c

Please sign in to comment.