Skip to content

Commit

Permalink
[LibOS] Add missing locks around dentry->inode accesses
Browse files Browse the repository at this point in the history
Missing locks are added in `do_getdents()` and `libos_syscall_fchdir()`.
Also, this commit adds a missing call to `put_handle()` in error
handling of `libos_syscall_fchdir()`.

Signed-off-by: g2flyer <[email protected]>
  • Loading branch information
g2flyer authored and Dmitrii Kuvaiskii committed Sep 3, 2024
1 parent 1fcf5f3 commit 649a8f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions libos/include/libos_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct libos_process {
LISTP_TYPE(libos_child_process) zombies;

struct libos_lock children_lock;

/* If g_dcache_lock is also required, acquire g_dcache_lock first and then fs_lock */
struct libos_lock fs_lock;

/* Complete command line for the process, as reported by /proc/[pid]/cmdline; currently filled
Expand Down
15 changes: 11 additions & 4 deletions libos/src/sys/libos_getcwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,33 @@ long libos_syscall_fchdir(int fd) {
if (!hdl)
return -EBADF;

int ret;
lock(&g_dcache_lock);

struct libos_dentry* dent = hdl->dentry;

if (!dent) {
log_debug("FD=%d has no path in the filesystem", fd);
return -ENOTDIR;
ret = -ENOTDIR;
goto out;
}
if (!dent->inode || dent->inode->type != S_IFDIR) {
char* path = NULL;
dentry_abs_path(dent, &path, /*size=*/NULL);
log_debug("%s is not a directory", path);
free(path);
put_handle(hdl);
return -ENOTDIR;
ret = -ENOTDIR;
goto out;
}

lock(&g_process.fs_lock);
get_dentry(dent);
put_dentry(g_process.cwd);
g_process.cwd = dent;
unlock(&g_process.fs_lock);
ret = 0;
out:
put_handle(hdl);
return 0;
unlock(&g_dcache_lock);
return ret;
}
5 changes: 3 additions & 2 deletions libos/src/sys/libos_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ static ssize_t do_getdents(int fd, uint8_t* buf, size_t buf_size, bool is_getden
goto out_no_unlock;
}

lock(&g_dcache_lock);
if (!hdl->dentry->inode) {
ret = -ENOENT;
goto out_no_unlock;
goto out_unlock_only_dcache_lock;
}

lock(&g_dcache_lock);
maybe_lock_pos_handle(hdl);
lock(&hdl->lock);

Expand Down Expand Up @@ -467,6 +467,7 @@ static ssize_t do_getdents(int fd, uint8_t* buf, size_t buf_size, bool is_getden
out:
unlock(&hdl->lock);
maybe_unlock_pos_handle(hdl);
out_unlock_only_dcache_lock:
unlock(&g_dcache_lock);
out_no_unlock:
put_handle(hdl);
Expand Down

0 comments on commit 649a8f5

Please sign in to comment.