Skip to content

Commit ff7d80a

Browse files
winnscodeSteve French
authored andcommitted
cifs: fix session state transition to avoid use-after-free issue
We switch session state to SES_EXITING without cifs_tcp_ses_lock now, it may lead to potential use-after-free issue. Consider the following execution processes: Thread 1: __cifs_put_smb_ses() spin_lock(&cifs_tcp_ses_lock) if (--ses->ses_count > 0) spin_unlock(&cifs_tcp_ses_lock) return spin_unlock(&cifs_tcp_ses_lock) ---> **GAP** spin_lock(&ses->ses_lock) if (ses->ses_status == SES_GOOD) ses->ses_status = SES_EXITING spin_unlock(&ses->ses_lock) Thread 2: cifs_find_smb_ses() spin_lock(&cifs_tcp_ses_lock) list_for_each_entry(ses, ...) spin_lock(&ses->ses_lock) if (ses->ses_status == SES_EXITING) spin_unlock(&ses->ses_lock) continue ... spin_unlock(&ses->ses_lock) if (ret) cifs_smb_ses_inc_refcount(ret) spin_unlock(&cifs_tcp_ses_lock) If thread 1 is preempted in the gap and thread 2 start executing, thread 2 will get the session, and soon thread 1 will switch the session state to SES_EXITING and start releasing it, even though thread 1 had increased the session's refcount and still uses it. So switch session state under cifs_tcp_ses_lock to eliminate this gap. Signed-off-by: Winston Wen <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent a507db1 commit ff7d80a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

fs/smb/client/connect.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,15 +1967,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
19671967
spin_unlock(&cifs_tcp_ses_lock);
19681968
return;
19691969
}
1970+
spin_lock(&ses->ses_lock);
1971+
if (ses->ses_status == SES_GOOD)
1972+
ses->ses_status = SES_EXITING;
1973+
spin_unlock(&ses->ses_lock);
19701974
spin_unlock(&cifs_tcp_ses_lock);
19711975

19721976
/* ses_count can never go negative */
19731977
WARN_ON(ses->ses_count < 0);
19741978

19751979
spin_lock(&ses->ses_lock);
1976-
if (ses->ses_status == SES_GOOD)
1977-
ses->ses_status = SES_EXITING;
1978-
19791980
if (ses->ses_status == SES_EXITING && server->ops->logoff) {
19801981
spin_unlock(&ses->ses_lock);
19811982
cifs_free_ipc(ses);

0 commit comments

Comments
 (0)