Skip to contentSkip to conversation

Fix BUG: KCSAN: data-race in xas_find_marked / xas_init_marks

PATCH In review Daniel Yang 2 participants 2 messages Oct 28, 2024 – Oct 29, 2024 VFAT/FAT/msdos filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix BUG: KCSAN: data-race in xas_find_marked / xas_init_marks

The cause of the bug is a call to truncate_setsize() while fat_file_fsync() is attempting to read. Although fat_setattr() acquires &MSDOS_I(inode)->truncate_lock before calling truncate, fat_file_fsync() doesn't acquire the read lock for truncate_lock. The function __generic_file_fsync() called in fat_file_fsync() only acquires lock to the inode itself, not the &MSDOS_I(inode) it is a member of. This leads to the data race where fat_file_fsync is reading during truncation.

To fix: &MSDOS_I(inode)->truncate_lock is acquired for reading before calling __generic_file_fsync().

---
 fs/fat/file.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fat/file.c +2 -0
index e887e9ab7..5578b771a 100644
@@ -188,7 +188,9 @@ int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
 	struct inode *inode = filp->f_mapping->host;
 	int err;
 
+	down_read(&MSDOS_I(inode)->truncate_lock);
 	err = __generic_file_fsync(filp, start, end, datasync);
+	up_read(&MSDOS_I(inode)->truncate_lock);
 	if (err)
 		return err;
 
Signature · 1 line
2.39.2
maintainer
#1 lore raw
Quoted 10 lines · Daniel Yang

The cause of the bug is a call to truncate_setsize() while fat_file_fsync() is attempting to read. Although fat_setattr() acquires &MSDOS_I(inode)->truncate_lock before calling truncate, fat_file_fsync() doesn't acquire the read lock for truncate_lock. The function __generic_file_fsync() called in fat_file_fsync() only acquires lock to the inode itself, not the &MSDOS_I(inode) it is a member of. This leads to the data race where fat_file_fsync is reading during truncation.

To fix: &MSDOS_I(inode)->truncate_lock is acquired for reading before calling __generic_file_fsync().

If writepages really races with truncate, why normal writeback path doesn't need truncate_lock? And this introduces the ABBA locking bug.

Thanks.

Quoted 20 lines
---
 fs/fat/file.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fat/file.c +2 -0
index e887e9ab7..5578b771a 100644
@@ -188,7 +188,9 @@ int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
 	struct inode *inode = filp->f_mapping->host;
 	int err;
 
+	down_read(&MSDOS_I(inode)->truncate_lock);
 	err = __generic_file_fsync(filp, start, end, datasync);
+	up_read(&MSDOS_I(inode)->truncate_lock);
 	if (err)
 		return err;
Signature · 1 line
OGAWA Hirofumi <[email protected]>