Skip to content

Commit 649a8f5

Browse files
g2flyerDmitrii Kuvaiskii
authored and
Dmitrii Kuvaiskii
committed
[LibOS] Add missing locks around dentry->inode accesses
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]>
1 parent 1fcf5f3 commit 649a8f5

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

libos/include/libos_process.h

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct libos_process {
6262
LISTP_TYPE(libos_child_process) zombies;
6363

6464
struct libos_lock children_lock;
65+
66+
/* If g_dcache_lock is also required, acquire g_dcache_lock first and then fs_lock */
6567
struct libos_lock fs_lock;
6668

6769
/* Complete command line for the process, as reported by /proc/[pid]/cmdline; currently filled

libos/src/sys/libos_getcwd.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,33 @@ long libos_syscall_fchdir(int fd) {
8282
if (!hdl)
8383
return -EBADF;
8484

85+
int ret;
86+
lock(&g_dcache_lock);
87+
8588
struct libos_dentry* dent = hdl->dentry;
8689

8790
if (!dent) {
8891
log_debug("FD=%d has no path in the filesystem", fd);
89-
return -ENOTDIR;
92+
ret = -ENOTDIR;
93+
goto out;
9094
}
9195
if (!dent->inode || dent->inode->type != S_IFDIR) {
9296
char* path = NULL;
9397
dentry_abs_path(dent, &path, /*size=*/NULL);
9498
log_debug("%s is not a directory", path);
9599
free(path);
96-
put_handle(hdl);
97-
return -ENOTDIR;
100+
ret = -ENOTDIR;
101+
goto out;
98102
}
99103

100104
lock(&g_process.fs_lock);
101105
get_dentry(dent);
102106
put_dentry(g_process.cwd);
103107
g_process.cwd = dent;
104108
unlock(&g_process.fs_lock);
109+
ret = 0;
110+
out:
105111
put_handle(hdl);
106-
return 0;
112+
unlock(&g_dcache_lock);
113+
return ret;
107114
}

libos/src/sys/libos_open.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,12 @@ static ssize_t do_getdents(int fd, uint8_t* buf, size_t buf_size, bool is_getden
373373
goto out_no_unlock;
374374
}
375375

376+
lock(&g_dcache_lock);
376377
if (!hdl->dentry->inode) {
377378
ret = -ENOENT;
378-
goto out_no_unlock;
379+
goto out_unlock_only_dcache_lock;
379380
}
380381

381-
lock(&g_dcache_lock);
382382
maybe_lock_pos_handle(hdl);
383383
lock(&hdl->lock);
384384

@@ -467,6 +467,7 @@ static ssize_t do_getdents(int fd, uint8_t* buf, size_t buf_size, bool is_getden
467467
out:
468468
unlock(&hdl->lock);
469469
maybe_unlock_pos_handle(hdl);
470+
out_unlock_only_dcache_lock:
470471
unlock(&g_dcache_lock);
471472
out_no_unlock:
472473
put_handle(hdl);

0 commit comments

Comments
 (0)