usb: gadget: f_mass_storage: take filesem in nofua store to fix backing-file UAF
Subsystems (2)
PATCH 1/1 usb: gadget: f_mass_storage: take filesem in nofua store to fix backing-file UAF
fsg_store_nofua() syncs the LUN's backing file when switching from async to sync mode:
if (!nofua && curlun->nofua) fsg_lun_fsync_sub(curlun);
fsg_lun_fsync_sub() dereferences curlun->filp (vfs_fsync(filp)). Unlike every other store that touches the backing file -- fsg_store_ro() and fsg_store_cdrom() (down_read), fsg_store_file() (down_write) -- fsg_store_nofua() holds no filesem while doing so.
A concurrent close of the backing file runs under down_write(filesem) and does fput(curlun->filp) in fsg_lun_close() (fsg_store_file() with an empty path, the SCSI START_STOP eject in do_start_stop(), or forced_eject), so it can free the file while fsg_store_nofua() is dereferencing it:
BUG: KASAN: slab-use-after-free in vfs_fsync+0x20/0xe0 vfs_fsync+0x20/0xe0 fsg_lun_fsync_sub+0xa0/0xd0 fsg_store_nofua+0x138/0x160 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 Allocated by task 3459: fsg_store_file+0x150/0x1f0 configfs_write_iter+0x1a7/0x250 Freed by task 3459: __fput+0x456/0x6c0 __fput_deferred+0x10d/0x330 fsg_store_file+0x9e/0x1f0 configfs_write_iter+0x1a7/0x250
Take filesem for read around the fsync in fsg_store_nofua(), as the sibling stores do, so the backing file cannot be closed underneath it.
Reproduced on a mass_storage gadget over dummy_hcd by writing lun.N/nofua (1 then 0) from one task while another task rewrites and clears lun.N/file.
--- drivers/usb/gadget/function/f_mass_storage.c | 6 ++++-- drivers/usb/gadget/function/storage_common.c | 5 ++++- drivers/usb/gadget/function/storage_common.h | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-)
drivers/usb/gadget/function/f_mass_storage.c +6 -2
@@ -2663,8 +2663,9 @@ const char *buf, size_t count) {struct fsg_lun *curlun =fsg_lun_from_dev (dev); +struct rw_semaphore *filesem =dev_get_drvdata (dev); -return fsg_store_nofua (curlun, buf, count); +return fsg_store_nofua (curlun, filesem, buf, count); } static ssize_t file_store(struct device *dev, struct device_attribute *attr,
@@ -3239,7 +3240,10 @@static ssize_t fsg_lun_opts_nofua_store (struct config_item *item,const char *page,size_t len) { -return fsg_store_nofua (to_fsg_lun_opts (item)->lun, page, len); +struct fsg_lun_opts *opts =to_fsg_lun_opts (item); +struct fsg_opts *fsg_opts =to_fsg_opts (opts->group.cg_item.ci_parent); + +return fsg_store_nofua (opts->lun, &fsg_opts->common->filesem, page, len); }CONFIGFS_ATTR (fsg_lun_opts_, nofua);
drivers/usb/gadget/function/storage_common.c +4 -1
@@ -415,7 +415,8 @@ }EXPORT_SYMBOL_GPL (fsg_store_ro); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count) +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count) {bool nofua;int ret;
@@ -424,11 +425,13 @@if (ret)return ret; +down_read (filesem);/* Sync data when switching from async mode to sync */ if (!nofua && curlun->nofua)fsg_lun_fsync_sub (curlun); curlun->nofua = nofua; +up_read (filesem);return count; }
drivers/usb/gadget/function/storage_common.h +2 -1
@@ -210,7 +210,8 @@ssize_t fsg_show_removable (struct fsg_lun *curlun,char *buf);ssize_t fsg_store_ro (struct fsg_lun *curlun,struct rw_semaphore *filesem,const char *buf,size_t count); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count); +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count);ssize_t fsg_store_file (struct fsg_lun *curlun,struct rw_semaphore *filesem,const char *buf,size_t count); ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 19 lines
fsg_store_nofua()syncs the LUN's backing file when switching from async to sync mode:if (!nofua && curlun->nofua) fsg_lun_fsync_sub(curlun);
fsg_lun_fsync_sub()dereferencescurlun->filp(vfs_fsync(filp)). Unlike every other store that touches the backing file --fsg_store_ro()andfsg_store_cdrom()(down_read),fsg_store_file()(down_write) --fsg_store_nofua()holds no filesem while doing so.A concurrent close of the backing file runs under
down_write(filesem)and doesfput(curlun->filp)infsg_lun_close()(fsg_store_file()with an empty path, the SCSISTART_STOPeject indo_start_stop(), orforced_eject), so it can free the file whilefsg_store_nofua()is dereferencing it:
[ ... ]
Reproduced on amass_storagegadget overdummy_hcdby writing lun.N/nofua (1 then 0) from one task while another task rewrites and clears lun.N/file.
Quoted 2 lines
Signed-off-by Yu Zhang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg Kroah-Hartman
maintainerQuoted 87 lines · Yu Zhang
fsg_store_nofua()syncs the LUN's backing file when switching from async to sync mode:if (!nofua && curlun->nofua) fsg_lun_fsync_sub(curlun);
fsg_lun_fsync_sub()dereferencescurlun->filp(vfs_fsync(filp)). Unlike every other store that touches the backing file --fsg_store_ro()andfsg_store_cdrom()(down_read),fsg_store_file()(down_write) --fsg_store_nofua()holds no filesem while doing so.A concurrent close of the backing file runs under
down_write(filesem)and doesfput(curlun->filp)infsg_lun_close()(fsg_store_file()with an empty path, the SCSISTART_STOPeject indo_start_stop(), orforced_eject), so it can free the file whilefsg_store_nofua()is dereferencing it:BUG: KASAN: slab-use-after-free in vfs_fsync+0x20/0xe0 vfs_fsync+0x20/0xe0 fsg_lun_fsync_sub+0xa0/0xd0 fsg_store_nofua+0x138/0x160 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 Allocated by task 3459: fsg_store_file+0x150/0x1f0 configfs_write_iter+0x1a7/0x250 Freed by task 3459: __fput+0x456/0x6c0 __fput_deferred+0x10d/0x330 fsg_store_file+0x9e/0x1f0 configfs_write_iter+0x1a7/0x250Take filesem for read around the fsync in
fsg_store_nofua(), as the sibling stores do, so the backing file cannot be closed underneath it.Reproduced on a
mass_storagegadget overdummy_hcdby writing lun.N/nofua (1 then 0) from one task while another task rewrites and clears lun.N/file.Signed-off-by Yu Zhang <[email protected]>--- drivers/usb/gadget/function/f_mass_storage.c | 6 ++++-- drivers/usb/gadget/function/storage_common.c | 5 ++++- drivers/usb/gadget/function/storage_common.h | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-)drivers/usb/gadget/function/f_mass_storage.c +6
-2@@ -2663,8 +2663,9 @@ const char *buf, size_t count) {struct fsg_lun *curlun =fsg_lun_from_dev (dev); +struct rw_semaphore *filesem =dev_get_drvdata (dev); -return fsg_store_nofua (curlun, buf, count); +return fsg_store_nofua (curlun, filesem, buf, count); } static ssize_t file_store(struct device *dev, struct device_attribute *attr,@@ -3239,7 +3240,10 @@static ssize_t fsg_lun_opts_nofua_store (struct config_item *item,const char *page,size_t len) { -return fsg_store_nofua (to_fsg_lun_opts (item)->lun, page, len); +struct fsg_lun_opts *opts =to_fsg_lun_opts (item); +struct fsg_opts *fsg_opts =to_fsg_opts (opts->group.cg_item.ci_parent); + +return fsg_store_nofua (opts->lun, &fsg_opts->common->filesem, page, len); }CONFIGFS_ATTR (fsg_lun_opts_, nofua);drivers/usb/gadget/function/storage_common.c +2
-1@@ -415,7 +415,8 @@ }EXPORT_SYMBOL_GPL (fsg_store_ro); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count) +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count) {bool nofua;int ret;
drivers/usb/gadget/function/storage_common.c line 428
Quoted 4 lines
drivers/usb/gadget/function/storage_common.c +1
-0@@ -424,11 +425,13 @@if (ret)return ret; +down_read (filesem);
guard()?
That's what happens when you use a LLM for kernel work, it only knows old-styles of what to do.
thanks,
greg k-h
Quoted 26 lines · Yu Zhang
fsg_store_nofua()syncs the LUN's backing file when switching from async to sync mode:if (!nofua && curlun->nofua) fsg_lun_fsync_sub(curlun);
fsg_lun_fsync_sub()dereferencescurlun->filp(vfs_fsync(filp)). Unlike every other store that touches the backing file --fsg_store_ro()andfsg_store_cdrom()(down_read),fsg_store_file()(down_write) --fsg_store_nofua()holds no filesem while doing so.drivers/usb/gadget/function/storage_common.c +2
-1@@ -415,7 +415,8 @@ }EXPORT_SYMBOL_GPL (fsg_store_ro); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count) +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count) {bool nofua;int ret;
drivers/usb/gadget/function/storage_common.c line 434
Quoted 10 lines
drivers/usb/gadget/function/storage_common.c +2
-0@@ -424,11 +425,13 @@if (ret)return ret; +down_read (filesem);/* Sync data when switching from async mode to sync */ if (!nofua && curlun->nofua)fsg_lun_fsync_sub (curlun); curlun->nofua = nofua; +up_read (filesem);
Shouldn't filesem be held only within the body of the conditional? There's no reason to take it if fsg_lun_fsync_sub() isn't going to be called.
Alan Stern
PATCH v2 1/1 usb: gadget: f_mass_storage: take filesem in nofua store to fix backing-file UAF
In reply to Yu Zhangfsg_store_nofua() syncs the LUN's backing file when switching from async to sync mode:
if (!nofua && curlun->nofua) fsg_lun_fsync_sub(curlun);
fsg_lun_fsync_sub() dereferences curlun->filp (vfs_fsync(filp)). Unlike every other store that touches the backing file -- fsg_store_ro() and fsg_store_cdrom() (down_read), fsg_store_file() (down_write) -- fsg_store_nofua() holds no filesem while doing so.
A concurrent close of the backing file runs under down_write(filesem) and does fput(curlun->filp) in fsg_lun_close() (fsg_store_file() with an empty path, the SCSI START_STOP eject in do_start_stop(), or forced_eject), so it can free the file while fsg_store_nofua() is dereferencing it:
BUG: KASAN: slab-use-after-free in vfs_fsync+0x20/0xe0 vfs_fsync+0x20/0xe0 fsg_lun_fsync_sub+0xa0/0xd0 fsg_store_nofua+0x138/0x160 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 Allocated by task 3459: fsg_store_file+0x150/0x1f0 configfs_write_iter+0x1a7/0x250 Freed by task 3459: __fput+0x456/0x6c0 __fput_deferred+0x10d/0x330 fsg_store_file+0x9e/0x1f0 configfs_write_iter+0x1a7/0x250
Hold filesem for read around the fsync in fsg_store_nofua() so the backing file cannot be closed underneath it. The lock is only needed when the fsync actually runs, so it is taken only in that branch.
Reproduced on a mass_storage gadget over dummy_hcd by writing lun.N/nofua (1 then 0) from one task while another task rewrites and clears lun.N/file.
---
v2:
- Use guard(rwsem_read) instead of a bare down_read()/up_read() pair (Greg KH).
- Take filesem only inside the branch that actually calls fsg_lun_fsync_sub(), not around the whole function / the curlun->nofua assignment, since the lock is only needed for the sync (Alan Stern).
v1: https://lore.kernel.org/all/[email protected]/
drivers/usb/gadget/function/f_mass_storage.c | 8 ++++++-- drivers/usb/gadget/function/storage_common.c | 7 +++++-- drivers/usb/gadget/function/storage_common.h | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-)
drivers/usb/gadget/function/f_mass_storage.c +6 -2
@@ -2663,8 +2663,9 @@ static ssize_t nofua_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {struct fsg_lun *curlun =fsg_lun_from_dev (dev); +struct rw_semaphore *filesem =dev_get_drvdata (dev); -return fsg_store_nofua (curlun, buf, count); +return fsg_store_nofua (curlun, filesem, buf, count); } static ssize_t file_store(struct device *dev, struct device_attribute *attr,
@@ -3239,7 +3240,10 @@ static ssize_t fsg_lun_opts_nofua_show(struct config_item *item, char *page)static ssize_t fsg_lun_opts_nofua_store (struct config_item *item,const char *page,size_t len) { -return fsg_store_nofua (to_fsg_lun_opts (item)->lun, page, len); +struct fsg_lun_opts *opts =to_fsg_lun_opts (item); +struct fsg_opts *fsg_opts =to_fsg_opts (opts->group.cg_item.ci_parent); + +return fsg_store_nofua (opts->lun, &fsg_opts->common->filesem, page, len); }CONFIGFS_ATTR (fsg_lun_opts_, nofua);
drivers/usb/gadget/function/storage_common.c +5 -2
@@ -415,7 +415,8 @@ ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem, }EXPORT_SYMBOL_GPL (fsg_store_ro); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count) +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count) {bool nofua;int ret;
@@ -425,8 +426,10 @@ ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)return ret;/* Sync data when switching from async mode to sync */ -if (!nofua && curlun->nofua) +if (!nofua && curlun->nofua) { +guard (rwsem_read)(filesem);fsg_lun_fsync_sub (curlun); + } curlun->nofua = nofua;
drivers/usb/gadget/function/storage_common.h +2 -1
@@ -210,7 +210,8 @@ ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);ssize_t fsg_show_removable (struct fsg_lun *curlun,char *buf);ssize_t fsg_store_ro (struct fsg_lun *curlun,struct rw_semaphore *filesem,const char *buf,size_t count); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count); +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count);ssize_t fsg_store_file (struct fsg_lun *curlun,struct rw_semaphore *filesem,const char *buf,size_t count); ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 19 lines
fsg_store_nofua()syncs the LUN's backing file when switching from async to sync mode:if (!nofua && curlun->nofua) fsg_lun_fsync_sub(curlun);
fsg_lun_fsync_sub()dereferencescurlun->filp(vfs_fsync(filp)). Unlike every other store that touches the backing file --fsg_store_ro()andfsg_store_cdrom()(down_read),fsg_store_file()(down_write) --fsg_store_nofua()holds no filesem while doing so.A concurrent close of the backing file runs under
down_write(filesem)and doesfput(curlun->filp)infsg_lun_close()(fsg_store_file()with an empty path, the SCSISTART_STOPeject indo_start_stop(), orforced_eject), so it can free the file whilefsg_store_nofua()is dereferencing it:
[ ... ]
Reproduced on amass_storagegadget overdummy_hcdby writing lun.N/nofua (1 then 0) from one task while another task rewrites and clears lun.N/file.
Quoted 2 lines
Signed-off-by Yu Zhang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 49 lines · Yu Zhang
fsg_store_nofua()syncs the LUN's backing file when switching from async to sync mode:if (!nofua && curlun->nofua) fsg_lun_fsync_sub(curlun);
fsg_lun_fsync_sub()dereferencescurlun->filp(vfs_fsync(filp)). Unlike every other store that touches the backing file --fsg_store_ro()andfsg_store_cdrom()(down_read),fsg_store_file()(down_write) --fsg_store_nofua()holds no filesem while doing so.A concurrent close of the backing file runs under
down_write(filesem)and doesfput(curlun->filp)infsg_lun_close()(fsg_store_file()with an empty path, the SCSISTART_STOPeject indo_start_stop(), orforced_eject), so it can free the file whilefsg_store_nofua()is dereferencing it:BUG: KASAN: slab-use-after-free in vfs_fsync+0x20/0xe0 vfs_fsync+0x20/0xe0 fsg_lun_fsync_sub+0xa0/0xd0 fsg_store_nofua+0x138/0x160 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 Allocated by task 3459: fsg_store_file+0x150/0x1f0 configfs_write_iter+0x1a7/0x250 Freed by task 3459: __fput+0x456/0x6c0 __fput_deferred+0x10d/0x330 fsg_store_file+0x9e/0x1f0 configfs_write_iter+0x1a7/0x250Hold filesem for read around the fsync in
fsg_store_nofua()so the backing file cannot be closed underneath it. The lock is only needed when the fsync actually runs, so it is taken only in that branch.Reproduced on a
mass_storagegadget overdummy_hcdby writing lun.N/nofua (1 then 0) from one task while another task rewrites and clears lun.N/file.Signed-off-by Yu Zhang <[email protected]>---
v2:
- Useguard(rwsem_read)instead of a bare down_read()/up_read() pair (Greg KH).
- Take filesem only inside the branch that actually callsfsg_lun_fsync_sub(), not around the whole function / thecurlun->nofuaassignment, since the lock is only needed for the sync (Alan Stern).
Quoted 6 lines
v1: https://lore.kernel.org/all/[email protected]/
drivers/usb/gadget/function/f_mass_storage.c | 8 ++++++-- drivers/usb/gadget/function/storage_common.c | 7 +++++-- drivers/usb/gadget/function/storage_common.h | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-)
Quoted 71 lines
drivers/usb/gadget/function/f_mass_storage.c +6
-2@@ -2663,8 +2663,9 @@ static ssize_t nofua_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {struct fsg_lun *curlun =fsg_lun_from_dev (dev); +struct rw_semaphore *filesem =dev_get_drvdata (dev); -return fsg_store_nofua (curlun, buf, count); +return fsg_store_nofua (curlun, filesem, buf, count); } static ssize_t file_store(struct device *dev, struct device_attribute *attr,@@ -3239,7 +3240,10 @@ static ssize_t fsg_lun_opts_nofua_show(struct config_item *item, char *page)static ssize_t fsg_lun_opts_nofua_store (struct config_item *item,const char *page,size_t len) { -return fsg_store_nofua (to_fsg_lun_opts (item)->lun, page, len); +struct fsg_lun_opts *opts =to_fsg_lun_opts (item); +struct fsg_opts *fsg_opts =to_fsg_opts (opts->group.cg_item.ci_parent); + +return fsg_store_nofua (opts->lun, &fsg_opts->common->filesem, page, len); }CONFIGFS_ATTR (fsg_lun_opts_, nofua);drivers/usb/gadget/function/storage_common.c +5
-2@@ -415,7 +415,8 @@ ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem, }EXPORT_SYMBOL_GPL (fsg_store_ro); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count) +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count) {bool nofua;int ret;@@ -425,8 +426,10 @@ ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)return ret;/* Sync data when switching from async mode to sync */ -if (!nofua && curlun->nofua) +if (!nofua && curlun->nofua) { +guard (rwsem_read)(filesem);fsg_lun_fsync_sub (curlun); + } curlun->nofua = nofua;drivers/usb/gadget/function/storage_common.h +2
-1@@ -210,7 +210,8 @@ ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);ssize_t fsg_show_removable (struct fsg_lun *curlun,char *buf);ssize_t fsg_store_ro (struct fsg_lun *curlun,struct rw_semaphore *filesem,const char *buf,size_t count); -ssize_t fsg_store_nofua (struct fsg_lun *curlun,const char *buf,size_t count); +ssize_t fsg_store_nofua (struct fsg_lun *curlun,struct rw_semaphore *filesem, +const char *buf,size_t count);ssize_t fsg_store_file (struct fsg_lun *curlun,struct rw_semaphore *filesem,const char *buf,size_t count); ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,Signature · 1 line
2.43.0