vfs: fix TOCTOU race condition
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
7556c3170e
commit
36d90bc0f6
2 changed files with 23 additions and 13 deletions
13
fs/vfs/vfs.c
13
fs/vfs/vfs.c
|
|
@ -187,6 +187,17 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
dirent_t *test = NULL;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
|
||||
/* best-effort first check */
|
||||
if (res == 0) {
|
||||
dirent_dec_ref_lock(test);
|
||||
return -EEXIST;
|
||||
}
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
/* re-check under lock */
|
||||
test = NULL;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
if (res == 0) {
|
||||
/* exists */
|
||||
ret = -EEXIST;
|
||||
|
|
@ -197,8 +208,6 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
goto out_err;
|
||||
}
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
/* create inode */
|
||||
fs_t *fs = parent->fs;
|
||||
inode_t *new_ino;
|
||||
|
|
|
|||
Loading…
Reference in a new issue