cifs: prevent readdir from changing file size due to stale directory metadata
Subsystems (2)
PATCH v3 1/1 cifs: prevent readdir from changing file size due to stale directory metadata
Windows Server's directory enumeration metadata lags behind the actual file state immediately after a local modification. If a concurrent readdir() runs while this lag exists, it overwrites the correct locally- cached i_size with the stale value from the server's directory listing. A subsequent stat() within the actimeo window returns the wrong size.
This manifests in two ways:
- After write+close: stat() returns 0 for a file just written
- After rename: stat() on the renamed file returns 0
Both are the same root cause. The bug does not reproduce against Samba (no directory metadata lag) or with actimeo=0 (bypasses the attribute cache).
The existing is_size_safe_to_change() check only blocked stale readdir size updates while an active RW lease was held. It does not protect the window after the last writable handle closes, which is when the race occurs.
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() that returns the authoritative size from the server's open-file table rather than the stale directory enumeration.
time_last_write is refreshed at the actual server close in smb2_deferred_work_close() and in the cifs_close_deferred_file*() drain paths (triggered by lease/oplock breaks and tcon teardown) to ensure the protection window is anchored to the real close time when closetimeo > 0.
Memory ordering: time_last_write uses smp_store_release() at all write sites. For the close path, the spinlock release in _cifsFileInfo_put() forms the store-release that pairs with is_inode_writable()'s spin_lock() (load-acquire), guaranteeing that the subsequent smp_load_acquire() on time_last_write observes any concurrent close. The setattr path uses smp_store_release() directly, relying on acquire-release semantics; store propagation (nanoseconds) is negligible relative to acregmax (seconds).
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/
v3: replace CIFS_INO_LOCK serialization approach with time_last_write
tracking; single patch now covers both observed symptoms
v2: fix malformed patch
Frank Sorenson (1):
cifs: prevent readdir from changing file size due to stale directory metadata
fs/smb/client/cifsfs.c | 1 + fs/smb/client/cifsglob.h | 1 + fs/smb/client/file.c | 64 ++++++++++++++++++++++++++++++++++++++++++++---- fs/smb/client/inode.c | 4 +++ fs/smb/client/misc.c | 21 +++++++++++++--- 5 files changed, 83 insertions(+), 8 deletions(-)
--
2.55.0
PATCH 1/1 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 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 + fs/smb/client/cifsglob.h | 1 + fs/smb/client/file.c | 64 ++++++++++++++++++++++++++++++++++++---- fs/smb/client/inode.c | 4 +++ fs/smb/client/misc.c | 21 +++++++++++-- 5 files changed, 83 insertions(+), 8 deletions(-)
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 +59 -5
97 lines not on this page · raw message
fs/smb/client/inode.c +4 -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,7 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)if ((attrs->ia_valid &ATTR_SIZE ) && attrs->ia_size !=i_size_read (inode)) { +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 +3481,7 @@ cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)if ((attrs->ia_valid &ATTR_SIZE ) && attrs->ia_size !=i_size_read (inode)) { +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 +18 -3
@@ -524,7 +524,12 @@ 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 ) +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 +562,12 @@ 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 ) +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 +636,12 @@ 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 ) +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); }
Signature · 1 line
2.55.0