cifs: fix readdir stale size bug and deferred-close reference leak
Subsystems (2)
v4 cover cifs: fix readdir stale size bug and deferred-close reference leak
This series fixes two bugs in the cifs/smb3 client: a race where readdir overwrites cached file sizes with stale server directory metadata, and a pre-existing reference leak in the deferred-close drain paths.
Patch 1 fixes a bug where stat() returns the wrong file size after a write+close or rename against Windows Server. Windows Server's directory enumeration metadata (EndOfFile) lags behind the actual file state immediately after a modification. If a concurrent readdir() runs during that lag, it overwrites the correctly cached i_size with the stale server value; a subsequent stat() within the actimeo window then returns the wrong size. Both the write+close and rename cases share the same root cause. The bug does not reproduce against Samba or with actimeo=0.
The fix adds cifsInodeInfo->time_last_write, stamped at writable close and at truncate. is_size_safe_to_change() blocks readdir from updating i_size within acregmax jiffies of that timestamp. When a size update is blocked and the server value differs from the cached one, the attribute cache is invalidated, forcing a fresh QUERY_INFO on the next stat(). time_last_write is refreshed at the actual server close in smb2_deferred_work_close() and the cifs_close_deferred_file*() drain paths to ensure the protection window is anchored to the real close time when closetimeo > 0.
Testing
-------
Reproduces against Windows Server 2022 with SMB 3.1.1 with multiple concurrent threads; does not reproduce against Samba or with actimeo=0. A reproducer exercising concurrent write+close+stat and rename+stat with concurrent readdir was run for 50,000+ iterations without hitting the bug.
A reproducer is available at https://github.com/fsorenson/cifs_cache_race_repro/
Patch 2 addresses a pre-existing reference leak in the deferred-close drain paths; when cancel_delayed_work() succeeds but the subsequent kmalloc_obj() for the processing list fails, the cancelled work's cifsFileInfo reference is silently dropped without calling _cifsFileInfo_put(), leaking the reference and the open server handle.
The fix saves the affected cfile and calls _cifsFileInfo_put() after releasing the lock.
v4: add memory barrier comments required by checkpatch; fix pre-existing
cifsFileInfo reference leak on kmalloc failure in deferred-close drain paths (patch 2)
v3: replace CIFS_INO_LOCK serialization approach with time_last_write
tracking; single patch covered both observed symptoms
v2: fix malformed patch
Frank Sorenson (2):
cifs: prevent readdir from changing file size due to stale directory metadata
cifs: fix cifsFileInfo leak on kmalloc failure in deferred close drain paths
fs/smb/client/cifsfs.c | 1 + fs/smb/client/cifsglob.h | 1 + fs/smb/client/file.c | 69 +++++++++++++++++++++++++++++++++++++++++++---- fs/smb/client/inode.c | 6 ++++ fs/smb/client/misc.c | 72 +++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 135 insertions(+), 14 deletions(-)
--
2.55.0
PATCH v4 1/2 cifs: prevent readdir from changing file size due to stale directory metadata
Windows Server's directory enumeration metadata lags behind the actual file size after a write+close or rename. A concurrent readdir() in the window between close() returning to userspace and stat() being called overwrites the correct cached i_size with the stale server value, causing stat() to return the wrong size.
Once _cifsFileInfo_put() removes the last writable handle from openFileList, is_size_safe_to_change() permits readdir to overwrite i_size. smb2_close_getattr() then stamps cifs_i->time = jiffies, making the corrupt cached value appear fresh to the next stat().
The existing check (see Fixes:) only blocked stale size updates while an active RW lease was held, not after the last writable handle closes.
Add cifsInodeInfo->time_last_write, written via smp_store_release() at writable close and on setattr/truncate. is_size_safe_to_change() checks is_inode_writable() first (acquiring open_file_lock), then rejects a readdir size update if time_last_write falls within acregmax jiffies. The spinlock release in _cifsFileInfo_put() forms a store-release barrier that pairs with the spin_lock() (load-acquire) in is_inode_writable(), ensuring the subsequent smp_load_acquire() on time_last_write observes any update from a concurrent close(). When a size update is rejected and the server value differs from the cached one, cifs_i->time is cleared to force a fresh QUERY_INFO on the next stat().
readdir is also blocked from changing i_size while writable handles are open or an RW lease is held, even on direct-IO mounts.
For deferred close (closetimeo > 0), time_last_write is refreshed at the actual server close in smb2_deferred_work_close() and in the cifs_close_deferred_file*() drain paths invoked by lease/oplock breaks and tcon teardown, anchoring the protection window to the real close time rather than the earlier userspace close.
time_last_write == 0 skips the time_before() check to avoid false positives near boot on 32-bit systems where jiffies starts close to INITIAL_JIFFIES.
Does not reproduce against Samba or with actimeo=0.
---
fs/smb/client/cifsfs.c +1 -0
@@ -440,6 +440,7 @@ cifs_alloc_inode(struct super_block *sb)return NULL ; cifs_inode->cifsAttrs =ATTR_ARCHIVE ;/* default */ cifs_inode->time =0 ; + cifs_inode->time_last_write =0 ;/* * Until the file is open and we have gotten oplock info back from the * server, can not assume caching of file data or metadata.
fs/smb/client/cifsglob.h +1 -0
@@ -1566,6 +1566,7 @@ struct cifsInodeInfo {spinlock_t writers_lock;unsigned int writers;/* Number of writers on this inode */ unsigned long time;/* jiffies of last update of inode */ +unsigned long time_last_write;/* jiffies of last writable close or truncate */ u64 uniqueid;/* server inode number */ u64 createtime;/* creation time on server */ __u8 lease_key[SMB2_LEASE_KEY_SIZE ];/* lease key for this inode */
fs/smb/client/file.c +64 -5
102 lines not on this page · raw message
fs/smb/client/inode.c +6 -0
@@ -237,6 +237,8 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,if (is_size_safe_to_change (cifs_i, fattr->cf_eof, from_readdir)) {i_size_write (inode, fattr->cf_eof); inode->i_blocks =CIFS_INO_BLOCKS (fattr->cf_bytes); + }else if (from_readdir &&i_size_read (inode) != fattr->cf_eof) { + cifs_i->time =0 ; }if (S_ISLNK (fattr->cf_mode) && fattr->cf_symlink_target) {
@@ -3277,6 +3279,8 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)if ((attrs->ia_valid &ATTR_SIZE ) && attrs->ia_size !=i_size_read (inode)) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&cifsInode->time_last_write, jiffies);truncate_setsize (inode, attrs->ia_size);netfs_resize_file (&cifsInode->netfs, attrs->ia_size, true);fscache_resize_cookie (cifs_inode_cookie (inode), attrs->ia_size);
@@ -3478,6 +3482,8 @@ cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)if ((attrs->ia_valid &ATTR_SIZE ) && attrs->ia_size !=i_size_read (inode)) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&cifsInode->time_last_write, jiffies);truncate_setsize (inode, attrs->ia_size);netfs_resize_file (&cifsInode->netfs, attrs->ia_size, true);fscache_resize_cookie (cifs_inode_cookie (inode), attrs->ia_size);
fs/smb/client/misc.c +24 -3
@@ -524,7 +524,14 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)spin_unlock (&cifs_inode->open_file_lock);list_for_each_entry_safe (tmp_list, tmp_next_list, &file_head, list) { -_cifsFileInfo_put (tmp_list->cfile, false, false); +struct cifsFileInfo *cfile = tmp_list->cfile; + +if (OPEN_FMODE (cfile->f_flags) &FMODE_WRITE ) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&CIFS_I (d_inode (cfile->dentry))->time_last_write, + jiffies); + } +_cifsFileInfo_put (cfile, false, false);list_del (&tmp_list->list);kfree (tmp_list); }
@@ -557,7 +564,14 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)spin_unlock (&tcon->open_file_lock);list_for_each_entry_safe (tmp_list, tmp_next_list, &file_head, list) { -_cifsFileInfo_put (tmp_list->cfile, true, false); +struct cifsFileInfo *cfile = tmp_list->cfile; + +if (OPEN_FMODE (cfile->f_flags) &FMODE_WRITE ) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&CIFS_I (d_inode (cfile->dentry))->time_last_write, + jiffies); + } +_cifsFileInfo_put (cfile, true, false);list_del (&tmp_list->list);kfree (tmp_list); }
@@ -626,7 +640,14 @@ void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,spin_unlock (&tcon->open_file_lock);list_for_each_entry_safe (tmp_list, tmp_next_list, &file_head, list) { -_cifsFileInfo_put (tmp_list->cfile, true, false); +struct cifsFileInfo *cfile = tmp_list->cfile; + +if (OPEN_FMODE (cfile->f_flags) &FMODE_WRITE ) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&CIFS_I (d_inode (cfile->dentry))->time_last_write, + jiffies); + } +_cifsFileInfo_put (cfile, true, false);list_del (&tmp_list->list);kfree (tmp_list); }
---
2.55.0
PATCH v4 2/2 cifs: fix cifsFileInfo leak on kmalloc failure in deferred close drain paths
In reply to Frank SorensonIn cifs_close_deferred_file(), cifs_close_all_deferred_files(), and cifs_close_deferred_file_under_dentry(), when a pending deferred close is cancelled via cancel_delayed_work(), the subsequent kmalloc_obj() to add the file to the local processing list may fail under memory pressure. The loop breaks immediately, but the cancelled work is no longer pending (it would have called _cifsFileInfo_put()), and the cfile is never added to file_head for processing. The cifsFileInfo reference and the open server handle both leak.
Fix by saving the cfile that failed allocation in a local variable, breaking as before, and calling _cifsFileInfo_put() on it after releasing the lock. Any files later in the iteration are unaffected since their deferred work is still pending and will fire normally.
---
fs/smb/client/misc.c +39 -6
@@ -497,7 +497,7 @@void cifs_close_deferred_file (struct cifsInodeInfo *cifs_inode) { -struct cifsFileInfo *cfile =NULL ; +struct cifsFileInfo *cfile =NULL , *failed_cfile =NULL ;struct file_list *tmp_list, *tmp_next_list;LIST_HEAD (file_head);
@@ -514,8 +514,10 @@ tmp_list = kmalloc_obj(struct file_list,GFP_ATOMIC ); -if (tmp_list ==NULL ) +if (tmp_list ==NULL ) { + failed_cfile = cfile;break ; + } tmp_list->cfile = cfile;list_add_tail (&tmp_list->list, &file_head); }
@@ -523,6 +525,15 @@ }spin_unlock (&cifs_inode->open_file_lock); +if (failed_cfile) { +if (OPEN_FMODE (failed_cfile->f_flags) &FMODE_WRITE ) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&CIFS_I (d_inode (failed_cfile->dentry))->time_last_write, + jiffies); + } +_cifsFileInfo_put (failed_cfile, false, false); + } +list_for_each_entry_safe (tmp_list, tmp_next_list, &file_head, list) {struct cifsFileInfo *cfile = tmp_list->cfile;
@@ -540,7 +551,7 @@void cifs_close_all_deferred_files (struct cifs_tcon *tcon) { -struct cifsFileInfo *cfile; +struct cifsFileInfo *cfile, *failed_cfile =NULL ;struct file_list *tmp_list, *tmp_next_list;LIST_HEAD (file_head);
@@ -554,8 +565,10 @@ tmp_list = kmalloc_obj(struct file_list,GFP_ATOMIC ); -if (tmp_list ==NULL ) +if (tmp_list ==NULL ) { + failed_cfile = cfile;break ; + } tmp_list->cfile = cfile;list_add_tail (&tmp_list->list, &file_head); }
@@ -563,6 +576,15 @@ }spin_unlock (&tcon->open_file_lock); +if (failed_cfile) { +if (OPEN_FMODE (failed_cfile->f_flags) &FMODE_WRITE ) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&CIFS_I (d_inode (failed_cfile->dentry))->time_last_write, + jiffies); + } +_cifsFileInfo_put (failed_cfile, true, false); + } +list_for_each_entry_safe (tmp_list, tmp_next_list, &file_head, list) {struct cifsFileInfo *cfile = tmp_list->cfile;
@@ -618,7 +640,7 @@ struct dentry *dentry) {struct file_list *tmp_list, *tmp_next_list; -struct cifsFileInfo *cfile; +struct cifsFileInfo *cfile, *failed_cfile =NULL ;LIST_HEAD (file_head);spin_lock (&tcon->open_file_lock);
@@ -631,14 +653,25 @@spin_unlock (&CIFS_I (d_inode (cfile->dentry))->deferred_lock); tmp_list = kmalloc_obj(struct file_list, GFP_ATOMIC); -if (tmp_list ==NULL ) +if (tmp_list ==NULL ) { + failed_cfile = cfile;break ; + } tmp_list->cfile = cfile;list_add_tail (&tmp_list->list, &file_head); } }spin_unlock (&tcon->open_file_lock); +if (failed_cfile) { +if (OPEN_FMODE (failed_cfile->f_flags) &FMODE_WRITE ) { +/* Pairs with smp_load_acquire() in is_size_safe_to_change(). */ +smp_store_release (&CIFS_I (d_inode (failed_cfile->dentry))->time_last_write, + jiffies); + } +_cifsFileInfo_put (failed_cfile, true, false); + } +list_for_each_entry_safe (tmp_list, tmp_next_list, &file_head, list) {struct cifsFileInfo *cfile = tmp_list->cfile;
---
2.55.0
tentatively merged into cifs-2.6.git for-next pending more reviews and testing
Quoted 67 lines · Frank Sorenson
This series fixes two bugs in the cifs/smb3 client: a race where readdir overwrites cached file sizes with stale server directory metadata, and a pre-existing reference leak in the deferred-close drain paths.
Patch 1 fixes a bug where
stat()returns the wrong file size after a write+close or rename against Windows Server. Windows Server's directory enumeration metadata (EndOfFile) lags behind the actual file state immediately after a modification. If a concurrentreaddir()runs during that lag, it overwrites the correctly cachedi_sizewith the stale server value; a subsequentstat()within the actimeo window then returns the wrong size. Both the write+close and rename cases share the same root cause. The bug does not reproduce against Samba or with actimeo=0.The fix adds
cifsInodeInfo->time_last_write, stamped at writable close and at truncate.is_size_safe_to_change()blocks readdir from updatingi_sizewithin acregmax jiffies of that timestamp. When a size update is blocked and the server value differs from the cached one, the attribute cache is invalidated, forcing a freshQUERY_INFOon the nextstat().time_last_writeis refreshed at the actual server close insmb2_deferred_work_close()and the cifs_close_deferred_file*() drain paths to ensure the protection window is anchored to the real close time when closetimeo > 0.Testing
-------
Reproduces against Windows Server 2022 with SMB 3.1.1 with multiple concurrent threads; does not reproduce against Samba or with actimeo=0. A reproducer exercising concurrent write+close+stat and rename+stat with concurrent readdir was run for 50,000+ iterations without hitting the bug.A reproducer is available at https://github.com/fsorenson/cifs_cache_race_repro/
Patch 2 addresses a pre-existing reference leak in the deferred-close drain paths; when
cancel_delayed_work()succeeds but the subsequentkmalloc_obj()for the processing list fails, the cancelled work's cifsFileInfo reference is silently dropped without calling_cifsFileInfo_put(), leaking the reference and the open server handle.The fix saves the affected cfile and calls
_cifsFileInfo_put()after releasing the lock.v4: add memory barrier comments required by checkpatch; fix pre-existing
cifsFileInfo reference leak on kmalloc failure in deferred-close drain paths (patch 2)
v3: replaceCIFS_INO_LOCKserialization approach withtime_last_write
tracking; single patch covered both observed symptoms
v2: fix malformed patchFrank Sorenson (2):
cifs: prevent readdir from changing file size due to stale directory metadata
cifs: fix cifsFileInfo leak on kmalloc failure in deferred close drain pathsfs/smb/client/cifsfs.c | 1 + fs/smb/client/cifsglob.h | 1 + fs/smb/client/file.c | 69 +++++++++++++++++++++++++++++++++++++++++++---- fs/smb/client/inode.c | 6 ++++ fs/smb/client/misc.c | 72 +++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 135 insertions(+), 14 deletions(-)--
2.55.0
Signature · 3 lines
Thanks, Steve