Skip to content

Commit 0000a77

Browse files
namjaejeongregkh
authored andcommitted
ksmbd: fix use-after-free in __ksmbd_close_fd() via durable scavenger
commit 235e323 upstream. When a durable file handle survives session disconnect (TCP close without SMB2_LOGOFF), session_fd_check() sets fp->conn = NULL to preserve the handle for later reconnection. However, it did not clean up the byte-range locks on fp->lock_list. Later, when the durable scavenger thread times out and calls __ksmbd_close_fd(NULL, fp), the lock cleanup loop did: spin_lock(&fp->conn->llist_lock); This caused a slab use-after-free because fp->conn was NULL and the original connection object had already been freed by ksmbd_tcp_disconnect(). The root cause is asymmetric cleanup: lock entries (smb_lock->clist) were left dangling on the freed conn->lock_list while fp->conn was nulled out. To fix this issue properly, we need to handle the lifetime of smb_lock->clist across three paths: - Safely skip clist deletion when list is empty and fp->conn is NULL. - Remove the lock from the old connection's lock_list in session_fd_check() - Re-add the lock to the new connection's lock_list in ksmbd_reopen_durable_fd(). Fixes: c8efcc7 ("ksmbd: add support for durable handles v1/v2") Co-developed-by: munan Huang <munanevil@gmail.com> Signed-off-by: munan Huang <munanevil@gmail.com> Reviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com> [ Minor context conflict resolved. ] Signed-off-by: Alva Lan <alvalan9@foxmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b32f4cd commit 0000a77

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

fs/smb/server/vfs_cache.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,11 @@ static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp)
356356
* there are not accesses to fp->lock_list.
357357
*/
358358
list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
359-
spin_lock(&fp->conn->llist_lock);
360-
list_del(&smb_lock->clist);
361-
spin_unlock(&fp->conn->llist_lock);
359+
if (!list_empty(&smb_lock->clist) && fp->conn) {
360+
spin_lock(&fp->conn->llist_lock);
361+
list_del(&smb_lock->clist);
362+
spin_unlock(&fp->conn->llist_lock);
363+
}
362364

363365
list_del(&smb_lock->flist);
364366
locks_free_lock(smb_lock->fl);
@@ -755,6 +757,7 @@ static bool session_fd_check(struct ksmbd_tree_connect *tcon,
755757
struct ksmbd_inode *ci;
756758
struct oplock_info *op;
757759
struct ksmbd_conn *conn;
760+
struct ksmbd_lock *smb_lock, *tmp_lock;
758761

759762
if (!is_reconnectable(fp))
760763
return false;
@@ -771,6 +774,12 @@ static bool session_fd_check(struct ksmbd_tree_connect *tcon,
771774
}
772775
up_write(&ci->m_lock);
773776

777+
list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) {
778+
spin_lock(&fp->conn->llist_lock);
779+
list_del_init(&smb_lock->clist);
780+
spin_unlock(&fp->conn->llist_lock);
781+
}
782+
774783
fp->conn = NULL;
775784
fp->tcon = NULL;
776785
fp->volatile_id = KSMBD_NO_FID;
@@ -844,6 +853,9 @@ int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
844853
{
845854
struct ksmbd_inode *ci;
846855
struct oplock_info *op;
856+
struct ksmbd_conn *conn = work->conn;
857+
struct ksmbd_lock *smb_lock;
858+
unsigned int old_f_state;
847859

848860
if (!fp->is_durable || fp->conn || fp->tcon) {
849861
pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon);
@@ -855,9 +867,23 @@ int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
855867
return -EBADF;
856868
}
857869

858-
fp->conn = work->conn;
870+
old_f_state = fp->f_state;
871+
fp->f_state = FP_NEW;
872+
__open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
873+
if (!has_file_id(fp->volatile_id)) {
874+
fp->f_state = old_f_state;
875+
return -EBADF;
876+
}
877+
878+
fp->conn = conn;
859879
fp->tcon = work->tcon;
860880

881+
list_for_each_entry(smb_lock, &fp->lock_list, flist) {
882+
spin_lock(&conn->llist_lock);
883+
list_add_tail(&smb_lock->clist, &conn->lock_list);
884+
spin_unlock(&conn->llist_lock);
885+
}
886+
861887
ci = fp->f_ci;
862888
down_write(&ci->m_lock);
863889
list_for_each_entry_rcu(op, &ci->m_op_list, op_entry) {
@@ -868,12 +894,6 @@ int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp)
868894
}
869895
up_write(&ci->m_lock);
870896

871-
__open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID);
872-
if (!has_file_id(fp->volatile_id)) {
873-
fp->conn = NULL;
874-
fp->tcon = NULL;
875-
return -EBADF;
876-
}
877897
return 0;
878898
}
879899

0 commit comments

Comments
 (0)