ksmbd: fix use-after-free in oplock break notification
Subsystems (2)
PATCH v2 1/1 ksmbd: fix use-after-free in oplock break notification
smb2_oplock_break_noti() reads opinfo->conn without any lock and dereferences it after two allocations which may sleep. When the durable handle owning the oplock is disconnected, session_fd_check() clears opinfo->conn and drops its conn reference under ci->m_lock, and the last ksmbd_conn_put() frees the connection. A break triggered by another connection that races with the teardown can then resurrect the freed connection: ksmbd_conn_get() is a plain atomic_inc, and the queued break work later dereferences the stale conn via ksmbd_conn_write(), a use-after-free reachable by any authenticated client holding a durable batch oplock.
Give the oplock_info its own reference on the inode (o_ci) so the notification path can take ci->m_lock without dereferencing opinfo->o_fp, which is not pinned by the oplock_info reference and may be freed by a concurrent close. Select and pin the connection under ci->m_lock, the same lock session_fd_check() uses to clear opinfo->conn, so a concurrent teardown either loses the race to the clear or keeps the connection alive until the notification work releases it. Transfer the reference to the work item and release it on allocation failures.
---
v2:
- The oplock_info now holds a reference on the inode (o_ci), taken when the oplock is granted and released when the oplock_info is freed, so smb2_oplock_break_conn_get() no longer dereferences opinfo->o_fp, which a concurrent close may free (review from Namjae Jeon).
--- fs/smb/server/oplock.c | 38 +++++++++++++++++++++++++++++++++++--- fs/smb/server/oplock.h | 1 + fs/smb/server/vfs_cache.h | 6 ++++++ 3 files changed, 42 insertions(+), 3 deletions(-)
fs/smb/server/oplock.c +35 -3
@@ -9,6 +9,7 @@#include "glob.h" #include "oplock.h" +#include "vfs_cache.h" #include "smb_common.h" #include "../common/smb2status.h"
@@ -236,6 +237,8 @@ static void __free_opinfo(struct oplock_info *opinfo) {if (opinfo->is_lease)free_lease (opinfo); +if (opinfo->o_ci) +ksmbd_inode_put (opinfo->o_ci);ksmbd_conn_put (opinfo->conn);kfree (opinfo); }
@@ -924,6 +927,30 @@ static void __smb2_oplock_break_noti(struct work_struct *wk)ksmbd_conn_put (conn); } +/* +* Select and pin the connection used for an oplock break before doing any +* allocations which may sleep. opinfo->conn is cleared under ci->m_lock by +* session_fd_check() when the durable handle owning the oplock is +* disconnected, and the last ksmbd_conn_put() frees the connection. The +* oplock_info holds its own reference on the inode (o_ci, taken when the +* oplock was granted), so the lock is always reachable here without +* dereferencing opinfo->o_fp, which a concurrent close may free. +*/ +static struct ksmbd_conn *smb2_oplock_break_conn_get (struct oplock_info *opinfo) +{ +struct ksmbd_conn *conn; + +down_read (&opinfo->o_ci->m_lock); + conn =READ_ONCE (opinfo->conn); +if (conn && !ksmbd_conn_releasing (conn)) + conn =ksmbd_conn_get (conn); +else + conn =NULL ; +up_read (&opinfo->o_ci->m_lock); + +return conn; +} +/** * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock * break command from server to client
@@ -938,17 +965,20 @@ static int smb2_oplock_break_noti(struct oplock_info *opinfo)int ret =0 ;struct ksmbd_work *work; - conn =READ_ONCE (opinfo->conn); + conn =smb2_oplock_break_conn_get (opinfo);if (!conn)return ksmbd_invalidate_durable_fd (opinfo->fid); work =ksmbd_alloc_work_struct (); -if (!work) +if (!work) { +ksmbd_conn_put (conn);return -ENOMEM ; + } br_info = kmalloc_obj(struct oplock_break_info, KSMBD_DEFAULT_GFP);if (!br_info) {ksmbd_free_work_struct (work); +ksmbd_conn_put (conn);return -ENOMEM ; }
@@ -957,7 +987,8 @@ static int smb2_oplock_break_noti(struct oplock_info *opinfo) br_info->open_trunc = opinfo->open_trunc; work->request_buf = (char *)br_info; - work->conn =ksmbd_conn_get (conn); +/* Transfer the reference acquired by smb2_oplock_break_conn_get(). */ + work->conn = conn; work->sess = opinfo->sess;ksmbd_conn_r_count_inc (conn);
@@ -1724,6 +1755,7 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,* lease_table first. */ opinfo->o_fp = fp; + opinfo->o_ci =ksmbd_inode_ref (fp->f_ci);if (new_lease) { new_lb =alloc_lease_table (opinfo);if (!new_lb) {
fs/smb/server/oplock.h +1 -0
@@ -64,6 +64,7 @@ struct oplock_info {struct ksmbd_session *sess;struct ksmbd_work *work;struct ksmbd_file *o_fp; +struct ksmbd_inode *o_ci;/* inode ref held while the oplock lives */ int level;int op_state;spinlock_t state_lock;
fs/smb/server/vfs_cache.h +6 -0
@@ -206,6 +206,12 @@ struct ksmbd_file *ksmbd_file_get(struct ksmbd_file *fp);void ksmbd_fd_put (struct ksmbd_work *work,struct ksmbd_file *fp);struct ksmbd_inode *ksmbd_inode_lookup_lock (struct dentry *d);void ksmbd_inode_put (struct ksmbd_inode *ci); + +static inline struct ksmbd_inode *ksmbd_inode_ref (struct ksmbd_inode *ci) +{ +atomic_inc (&ci->m_count); +return ci; +}bool ksmbd_close_disconnected_durable_delete_on_close (struct dentry *dentry);struct ksmbd_file *ksmbd_lookup_global_fd (unsigned long long id);struct ksmbd_file *ksmbd_lookup_durable_fd (unsigned long long id);
Signature · 1 line
2.53.0
Namjae Jeon
maintainerfs/smb/server/vfs_cache.h struct ksmbd_file *ksmbd_file_get(struct ksmbd_file *fp); · line 212
Quoted 4 lines
fs/smb/server/vfs_cache.h +4
-0+ +static inline struct ksmbd_inode *ksmbd_inode_ref (struct ksmbd_inode *ci) +{ +atomic_inc (&ci->m_count);
Using m_count to pin o_ci breaks delete-on-close. The last file close will no longer drop m_count to zero, so __ksmbd_inode_close() can skip unlinking a delete-pending file.
Quoted 8 lines
fs/smb/server/vfs_cache.h +2
-1+return ci; +}bool ksmbd_close_disconnected_durable_delete_on_close (struct dentry *dentry);struct ksmbd_file *ksmbd_lookup_global_fd (unsigned long long id);struct ksmbd_file *ksmbd_lookup_durable_fd (unsigned long long id); --2.53.0
Hi Namjae,
Quoted 2 lines · Namjae Jeon
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static inline struct ksmbd_inode *ksmbd_inode_ref(struct ksmbd_inode
*ci)
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+{ + atomic_inc(&ci->m_count);Using
m_countto pino_cibreaks delete-on-close. The last file close will no longer dropm_countto zero, so__ksmbd_inode_close()can skip unlinking a delete-pending file.
You're right, thank you. Holding the extra m_count reference until the oplock_info is RCU-freed delays the drop to zero that __ksmbd_inode_close() keys the delete-on-close unlink on, so a delete-pending file opened with an oplock would never be unlinked.
v3 drops the inode reference entirely. The notification path does not need one: every caller of oplock_break() already holds a live ksmbd_file (or an explicit ksmbd_inode_lookup_lock() reference in the parent lease break paths) on the inode that owns the break target's oplock list, so ci is pinned for the duration of the call. The caller's inode is threaded through oplock_break() into smb2_oplock_break_noti(), which takes ci->m_lock to select and pin the connection without ever dereferencing opinfo->o_fp.
opinfo->conn is updated under ci->m_lock by both session_fd_check() (clear on disconnect) and ksmbd_reopen_durable_fd() (reassign reconnect), so holding the read lock also prevents the oplock's connection reference from being dropped while the notification it - the same shape as the lease fix in commit 3a98de41b0a4d.
Please see the v3 patch attached.
Regards,
Abdifatah Suruur
Quoted 5 lines · Namjae Jeon
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static inline struct ksmbd_inode *ksmbd_inode_ref(struct ksmbd_inode*ci)
Review
Quoted 2 lines
(quoted diff) +2
-0+{ + atomic_inc(&ci->m_count);
Quoted 14 lines
Using
m_countto pino_cibreaks delete-on-close. The last file close will no longer dropm_countto zero, so__ksmbd_inode_close()can skip unlinking a delete-pending file.Review
Quoted 3 lines
(quoted diff) +2
-0+ return ci; +} bool ksmbd_close_disconnected_durable_delete_on_close(struct dentry*dentry);
Quoted 5 lines
struct
ksmbd_file*ksmbd_lookup_global_fd(unsigned long long id); structksmbd_file*ksmbd_lookup_durable_fd(unsigned long long id);
--
2.53.0
From 94e0e653c5aa0020e0dafb4c7cd8d8ccee88badb Mon Sep 17 00:00:00 2001
From: Abdifatah Suruur <[email protected]>
Date: Sat, 29 Aug 2026 18:40:22 +0300
Subject: [PATCH v3] ksmbd: fix use-after-free in oplock break notification
smb2_oplock_break_noti() reads opinfo->conn without any lock and dereferences it after two allocations which may sleep. When the durable handle owning the oplock is disconnected, session_fd_check() clears opinfo->conn and drops its conn reference under ci->m_lock, and the last ksmbd_conn_put() frees the connection. A break triggered by another connection that races with the teardown can then resurrect the freed connection: ksmbd_conn_get() is a plain atomic_inc, and the queued break work later dereferences the stale conn via ksmbd_conn_write(), a use-after-free reachable by any authenticated client holding a durable batch oplock.
Thread the caller's inode into the notification path instead of taking a new reference on it. Every caller of oplock_break() already holds a live ksmbd_file (or an explicit ksmbd_inode_lookup_lock() reference, in the parent lease break paths) on the inode that owns the break target's oplock list, so ci cannot be freed during the call, and its lock can be taken without dereferencing opinfo->o_fp, which a concurrent close may free. Select and pin the connection under ci->m_lock, the same lock session_fd_check() and ksmbd_reopen_durable_fd() use to update opinfo->conn, so a concurrent detach either loses the race to the clear or keeps the connection alive until the notification work releases it. Transfer the reference to the work item and release it on allocation failures.
---
v3:
- Drop the inode reference held by the oplock_info (v2's o_ci):
holding m_count past the last close broke delete-on-close, since __ksmbd_inode_close() only promotes S_DEL_ON_CLS and unlinks when the close drops m_count to zero (review from Namjae Jeon).
- Pass the caller's inode, pinned for the duration of the call, into the break and notification paths instead.
--- fs/smb/server/oplock.c | 73 +++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 16 deletions(-)
fs/smb/server/oplock.c +57 -16
@@ -924,31 +924,69 @@ static void __smb2_oplock_break_noti(struct work_struct *wk)ksmbd_conn_put (conn); } +/* +* Select and pin the connection used for an oplock break before doing any +* allocations which may sleep. The caller of oplock_break() holds a live +* reference on ci (a file being opened, a file being operated on, or an +* explicit ksmbd_inode_lookup_lock() reference in the parent lease break +* paths), so the inode cannot be freed during the call and its lock is +* reachable without dereferencing opinfo->o_fp, which is not pinned by +* the oplock reference and may be freed by a concurrent close. +* +* opinfo->conn is cleared under ci->m_lock by session_fd_check() when the +* durable handle owning the oplock is disconnected, reassigned by +* ksmbd_reopen_durable_fd() under the same lock, and the last +* ksmbd_conn_put() of the old connection frees it. Holding the read lock +* excludes both writers, so the connection cannot be freed while it is +* selected. +*/ +static struct ksmbd_conn *smb2_oplock_break_conn_get (struct oplock_info *opinfo, +struct ksmbd_inode *ci) +{ +struct ksmbd_conn *conn; + +down_read (&ci->m_lock); + conn =READ_ONCE (opinfo->conn); +if (conn && !ksmbd_conn_releasing (conn)) + conn =ksmbd_conn_get (conn); +else + conn =NULL ; +up_read (&ci->m_lock); + +return conn; +} +/** * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock * break command from server to client * @opinfo: oplock info object +* @ci: inode owning the break target's oplock list, pinned by +* the caller * * Return: 0 on success, otherwise error */ -static int smb2_oplock_break_noti (struct oplock_info *opinfo) +static int smb2_oplock_break_noti (struct oplock_info *opinfo, +struct ksmbd_inode *ci) {struct ksmbd_conn *conn;struct oplock_break_info *br_info;int ret =0 ;struct ksmbd_work *work; - conn =READ_ONCE (opinfo->conn); + conn =smb2_oplock_break_conn_get (opinfo, ci);if (!conn)return ksmbd_invalidate_durable_fd (opinfo->fid); work =ksmbd_alloc_work_struct (); -if (!work) +if (!work) { +ksmbd_conn_put (conn);return -ENOMEM ; + } br_info = kmalloc_obj(struct oplock_break_info, KSMBD_DEFAULT_GFP);if (!br_info) {ksmbd_free_work_struct (work); +ksmbd_conn_put (conn);return -ENOMEM ; }
@@ -957,7 +995,8 @@ static int smb2_oplock_break_noti(struct oplock_info *opinfo) br_info->open_trunc = opinfo->open_trunc; work->request_buf = (char *)br_info; - work->conn =ksmbd_conn_get (conn); +/* Transfer the reference acquired by smb2_oplock_break_conn_get(). */ + work->conn = conn; work->sess = opinfo->sess;ksmbd_conn_r_count_inc (conn);
@@ -1154,9 +1193,9 @@ static void wait_lease_breaking(struct oplock_info *opinfo) } } -static int oplock_break (struct oplock_info *brk_opinfo,int req_op_level, -struct ksmbd_work *in_work,bool share_break, -bool sync_lease_break) +static int oplock_break (struct oplock_info *brk_opinfo,struct ksmbd_inode *ci, +int req_op_level,struct ksmbd_work *in_work, +bool share_break,bool sync_lease_break) {int err =0 ;bool sent_interim = false;
@@ -1298,7 +1337,7 @@ static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level, } } - err =smb2_oplock_break_noti (brk_opinfo); + err =smb2_oplock_break_noti (brk_opinfo, ci);ksmbd_debug (OPLOCK ,"oplock granted = %d\n" , brk_opinfo->level);if (brk_opinfo->op_state ==OPLOCK_CLOSING )
@@ -1326,13 +1365,14 @@ static int oplock_break_add(struct list_head *head, struct oplock_info *opinfo)return 0 ; } -static void oplock_break_drain_none (struct list_head *head) +static void oplock_break_drain_none (struct list_head *head, +struct ksmbd_inode *ci) {struct oplock_break_entry *ent, *tmp;list_for_each_entry_safe (ent, tmp, head, list) { -oplock_break (ent->opinfo,SMB2_OPLOCK_LEVEL_NONE ,NULL , false, - false); +oplock_break (ent->opinfo, ci,SMB2_OPLOCK_LEVEL_NONE ,NULL , + false, false);list_del (&ent->list);opinfo_put (ent->opinfo);kfree (ent);
@@ -1481,7 +1521,7 @@ void smb_send_parent_lease_break_noti(struct ksmbd_file *fp, }up_read (&p_ci->m_lock); -oplock_break_drain_none (&brk_list); +oplock_break_drain_none (&brk_list, p_ci);ksmbd_inode_put (p_ci); }
@@ -1525,7 +1565,7 @@ void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp) }up_read (&p_ci->m_lock); -oplock_break_drain_none (&brk_list); +oplock_break_drain_none (&brk_list, p_ci);ksmbd_inode_put (p_ci); }
@@ -1665,7 +1705,7 @@ int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, prev_durable_detached = prev_op_snapshot.durable_detached; prev_fid = prev_op_snapshot.fid; - err =oplock_break (prev_opinfo, break_level, work, + err =oplock_break (prev_opinfo, ci, break_level, work, share_ret <0 && prev_opinfo->is_lease, false);if (prev_durable_detached || (prev_durable_open && err == -ENOENT ))ksmbd_invalidate_durable_fd (prev_fid);
@@ -1771,7 +1811,8 @@ static bool smb_break_all_write_oplock(struct ksmbd_work *work, } brk_opinfo->open_trunc = is_trunc; -oplock_break (brk_opinfo,SMB2_OPLOCK_LEVEL_II , work, false, false); +oplock_break (brk_opinfo, fp->f_ci,SMB2_OPLOCK_LEVEL_II , work, false, + false); sent_break = true;opinfo_put (brk_opinfo);
@@ -1863,7 +1904,7 @@ static void __smb_break_all_levII_oplock(struct ksmbd_work *work, brk_op->op_state =OPLOCK_STATE_NONE ;spin_unlock (&brk_op->state_lock); }else { - oplock_break(brk_op, + oplock_break(brk_op, ci, brk_op->is_lease && !is_trunc ? SMB2_OPLOCK_LEVEL_II : SMB2_OPLOCK_LEVEL_NONE, send_interim && !sent_interim ? work : NULL,
Signature · 1 line
2.53.0
Quoted 26 lines · Suruur
Hi Namjae,
Quoted 7 lines · Namjae Jeon
Review
Quoted 4 lines
(quoted diff) +4
-0+ +static inline struct ksmbd_inode *ksmbd_inode_ref(struct ksmbd_inode *ci) +{ + atomic_inc(&ci->m_count);Using
m_countto pino_cibreaks delete-on-close. The last file close will no longer dropm_countto zero, so__ksmbd_inode_close()can skip unlinking a delete-pending file.You're right, thank you. Holding the extra
m_countreference until theoplock_infois RCU-freed delays the drop to zero that__ksmbd_inode_close()keys the delete-on-close unlink on, so a delete-pending file opened with an oplock would never be unlinked.v3 drops the inode reference entirely. The notification path does not need one: every caller of
oplock_break()already holds a liveksmbd_file(or an explicitksmbd_inode_lookup_lock()reference in the parent lease break paths) on the inode that owns the break target's oplock list, so ci is pinned for the duration of the call. The caller's inode is threaded throughoplock_break()intosmb2_oplock_break_noti(), which takesci->m_lockto select and pin the connection without ever dereferencingopinfo->o_fp.
Quoted 7 lines
opinfo->connis updated underci->m_lockby bothsession_fd_check()(clear on disconnect) andksmbd_reopen_durable_fd()(reassign reconnect), so holding the read lock also prevents the oplock's connection reference from being dropped while the notification it - the same shape as the lease fix in commit3a98de41b0a4d.Please see the v3 patch attached.
Applied it to #ksmbd-for-next.
Thanks!