ksmbd: fix use-after-free in oplock break notification
Subsystems (2)
PATCH 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.
Select and pin the connection under ci->m_lock before the allocations, 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.
--- fs/smb/server/oplock.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-)
fs/smb/server/oplock.c +33 -3
@@ -924,6 +924,32 @@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. +*/ +static struct ksmbd_conn *smb2_oplock_break_conn_get (struct oplock_info *opinfo) +{ +struct ksmbd_inode *ci; +struct ksmbd_conn *conn; + +if (!opinfo->o_fp) +return NULL ; + ci = opinfo->o_fp->f_ci; + +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
@@ -938,17 +964,20 @@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 +986,8 @@ 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);
Namjae Jeon
maintainerQuoted 18 lines · Abdifatah Suruur
smb2_oplock_break_noti()readsopinfo->connwithout any lock and dereferences it after two allocations which may sleep. When the durable handle owning the oplock is disconnected,session_fd_check()clearsopinfo->connand drops its conn reference underci->m_lock, and the lastksmbd_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 plainatomic_inc, and the queued break work later dereferences the stale conn viaksmbd_conn_write(), a use-after-free reachable by any authenticated client holding a durable batch oplock.Select and pin the connection under
ci->m_lockbefore the allocations, the same locksession_fd_check()uses to clearopinfo->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.
Quoted 3 lines
Fixes b003086d7696 ("ksmbd: fix NULL-deref of opinfo->conn in oplock/lease break notifiers")Signed-off-by Abdifatah Suruur <[email protected]>
I can not apply this patch...
Applying: ksmbd: fix use-after-free in oplock break notification
error: patch fragment without header at line 7: @@ -924,6 +924,32 @@ Patch failed at 0001 ksmbd: fix use-after-free in oplock break notification
Quoted 14 lines
--- fs/smb/server/oplock.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-)fs/smb/server/oplock.c +4
-0@@ -924,6 +924,32 @@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
fs/smb/server/oplock.c line 940
Quoted 10 lines
fs/smb/server/oplock.c +10
-0+* disconnected, and the last ksmbd_conn_put() frees the connection. +*/ +static struct ksmbd_conn *smb2_oplock_break_conn_get (struct oplock_info *opinfo) +{ +struct ksmbd_inode *ci; +struct ksmbd_conn *conn; + +if (!opinfo->o_fp) +return NULL ; + ci = opinfo->o_fp->f_ci;
opinfo reference does not pin opinfo->o_fp or o_fp->f_ci. opinfo can still be valid after o_fp has been freed by concurrent close, so dereferencing opinfo->o_fp here may cause a use-after-free. How is o_fp protected here?