summaryrefslogtreecommitdiffstats
path: root/fs/internal.h
diff options
context:
space:
mode:
authorAmir Goldstein <amir73il@gmail.com>2022-08-16 16:53:17 +0200
committerAl Viro <viro@zeniv.linux.org.uk>2022-08-16 16:59:54 +0200
commitd6da19c9cace63290ccfccb1fc35151ffefc0bec (patch)
tree00e66911e1e16675007d324b91572ab73adac346 /fs/internal.h
parentLinux 6.0-rc1 (diff)
downloadlinux-d6da19c9cace63290ccfccb1fc35151ffefc0bec.tar.xz
linux-d6da19c9cace63290ccfccb1fc35151ffefc0bec.zip
locks: fix TOCTOU race when granting write lease
Thread A trying to acquire a write lease checks the value of i_readcount and i_writecount in check_conflicting_open() to verify that its own fd is the only fd referencing the file. Thread B trying to open the file for read will call break_lease() in do_dentry_open() before incrementing i_readcount, which leaves a small window where thread A can acquire the write lease and then thread B completes the open of the file for read without breaking the write lease that was acquired by thread A. Fix this race by incrementing i_readcount before checking for existing leases, same as the case with i_writecount. Use a helper put_file_access() to decrement i_readcount or i_writecount in do_dentry_open() and __fput(). Fixes: 387e3746d01c ("locks: eliminate false positive conflicts for write lease") Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs/internal.h')
-rw-r--r--fs/internal.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/fs/internal.h b/fs/internal.h
index 87e96b9024ce..31861e6c3eff 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -101,6 +101,16 @@ extern void chroot_fs_refs(const struct path *, const struct path *);
extern struct file *alloc_empty_file(int, const struct cred *);
extern struct file *alloc_empty_file_noaccount(int, const struct cred *);
+static inline void put_file_access(struct file *file)
+{
+ if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
+ i_readcount_dec(file->f_inode);
+ } else if (file->f_mode & FMODE_WRITER) {
+ put_write_access(file->f_inode);
+ __mnt_drop_write(file->f_path.mnt);
+ }
+}
+
/*
* super.c
*/