Durable handle deadlines are stored as 32-bit millisecond counts and compared with ordinary integer comparisons. If a timeout crosses the counter wrap, the handle appears expired immediately. Reconnect then fails with STATUS_OBJECT_NAME_NOT_FOUND, and the scavenger can close the handle before its timeout expires. With HZ of 100, 250 or 1000, the millisecond count first wraps five minutes after boot because of the initial jiffies value, and then every 49.7 days. Store the deadline in jiffies and use wrap-safe time comparisons. Use durable_timeout to identify timed handles, since zero is a valid deadline. The reconnect lookup now checks the handle under global_ft.lock. session_fd_check() clears the connection and sets the deadline under that lock, so the lookup sees both changes or neither. Fixes: 520da3c488c5 ("ksmbd: avoid reclaiming expired durable opens by the client") Cc: stable@vger.kernel.org Signed-off-by: Ian Bridges --- Changes in v2 Sashiko found a race in the reconnect lookup. The lookup now checks the handle under a lock. The same unlocked read could also see the expired deadline of an earlier disconnect. That part existed before v1. v1: https://lore.kernel.org/linux-cifs/20261203061636.3016138-1-icb@fastmail.org/ Sashiko's review: https://sashiko.dev/#/patchset/20261203061636.3016138-1-icb%40fastmail.org Tested on ksmbd-for-next 030c481c019b, on x86_64 with KASAN, in freshly booted guests. On the base, a durable handle whose timeout ran past the first wrap after boot could not be reconnected. With the patch, the same reconnect succeeded, and a reconnect after the wrap succeeded too. A test-only delay held the disconnect after it cleared the connection and before it stored the deadline. A reconnect sent during that delay failed with v1. It succeeded on the base and with v2. This bug also affects smbtorture. Its durable handle tests ask for the maximum timeout of five minutes, so on the base they fail when they run within five minutes of boot. This patch was developed with LLM assistance. fs/smb/server/vfs_cache.c | 37 +++++++++++++++++++++++++------------ fs/smb/server/vfs_cache.h | 2 +- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/fs/smb/server/vfs_cache.c b/fs/smb/server/vfs_cache.c index 7db0a52dd4ae..5ae64a319c34 100644 --- a/fs/smb/server/vfs_cache.c +++ b/fs/smb/server/vfs_cache.c @@ -1229,17 +1229,30 @@ struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id) struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id) { - struct ksmbd_file *fp; + struct ksmbd_file *fp, *stale = NULL; + + if (!has_file_id(id)) + return NULL; - fp = __ksmbd_lookup_fd(&global_ft, id); + /* + * session_fd_check() clears fp->conn and sets the deadline under + * global_ft.lock, so check both under the same lock. + */ + read_lock(&global_ft.lock); + fp = idr_find(global_ft.idr, id); + if (fp) + fp = ksmbd_fp_get(fp); if (fp && (fp->durable_reconnect_disabled || fp->conn || - (fp->durable_scavenger_timeout && - (fp->durable_scavenger_timeout < - jiffies_to_msecs(jiffies))))) { - ksmbd_put_durable_fd(fp); + (fp->durable_timeout && + time_after(jiffies, fp->durable_scavenger_timeout)))) { + stale = fp; fp = NULL; } + read_unlock(&global_ft.lock); + + if (stale) + ksmbd_put_durable_fd(stale); return fp; } @@ -1408,7 +1421,7 @@ int ksmbd_invalidate_durable_fd(unsigned long long id) } fp->durable_timeout = 1; - fp->durable_scavenger_timeout = jiffies_to_msecs(jiffies); + fp->durable_scavenger_timeout = jiffies; ksmbd_put_durable_fd(fp); if (waitqueue_active(&dh_wq)) wake_up(&dh_wq); @@ -1916,8 +1929,8 @@ static int ksmbd_durable_scavenger(void *dummy) continue; found_fp_timeout = true; - if (fp->durable_scavenger_timeout <= - jiffies_to_msecs(jiffies)) { + if (time_after_eq(jiffies, + fp->durable_scavenger_timeout)) { __ksmbd_remove_durable_fd(fp); /* * Take a transient reference so fp @@ -1934,8 +1947,8 @@ static int ksmbd_durable_scavenger(void *dummy) } durable_timeout = - fp->durable_scavenger_timeout - - jiffies_to_msecs(jiffies); + jiffies_to_msecs(fp->durable_scavenger_timeout - + jiffies); if (min_timeout > durable_timeout) min_timeout = durable_timeout; @@ -2111,7 +2124,7 @@ static bool session_fd_check(struct ksmbd_tree_connect *tcon, if (fp->durable_timeout) fp->durable_scavenger_timeout = - jiffies_to_msecs(jiffies) + fp->durable_timeout; + jiffies + msecs_to_jiffies(fp->durable_timeout); atomic_dec(&conn->stats.open_files_count); write_unlock(&global_ft.lock); up_write(&ci->m_lock); diff --git a/fs/smb/server/vfs_cache.h b/fs/smb/server/vfs_cache.h index bc1955b87855..26e10093b2ae 100644 --- a/fs/smb/server/vfs_cache.h +++ b/fs/smb/server/vfs_cache.h @@ -152,7 +152,7 @@ struct ksmbd_file { bool stream_del_pending; unsigned int durable_timeout; - unsigned int durable_scavenger_timeout; + unsigned long durable_scavenger_timeout; /* CREATE action returned when this durable handle was established. */ __le32 create_action; base-commit: 030c481c019bec1fcb3fd66287ad80dfbdc1b62f -- 2.47.3