Skip to contentSkip to conversation

cifs: fix session state checks to avoid use-after-free issues

PATCH Applied Winston Wen 3 participants 25 messages Jun 26, 2023 – Jul 3, 2023 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover cifs: fix session state checks to avoid use-after-free issues

The patchset has 3 patches that fix some problems related to session state check/transition to avoid use-after-free issues.

Winston Wen (3):
cifs: fix session state transition to avoid use-after-free issue
cifs: fix session state check in reconnect to avoid use-after-free issue
cifs: fix session state check in smb2_find_smb_ses

 fs/smb/client/connect.c       | 7 ++++---
 fs/smb/client/smb2pdu.c       | 6 ++++++
 fs/smb/client/smb2transport.c | 7 +++++++
 3 files changed, 17 insertions(+), 3 deletions(-)
#1 lore raw

PATCH 1/3 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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/smb/client/connect.c +4 -3
index 9d16626e7a66..165ecb222c19 100644
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
 		spin_unlock(&cifs_tcp_ses_lock);
 		return;
 	}
+	spin_lock(&ses->ses_lock);
+	if (ses->ses_status == SES_GOOD)
+		ses->ses_status = SES_EXITING;
+	spin_unlock(&ses->ses_lock);
 	spin_unlock(&cifs_tcp_ses_lock);
 
 	/* ses_count can never go negative */
 	WARN_ON(ses->ses_count < 0);
 
 	spin_lock(&ses->ses_lock);
-	if (ses->ses_status == SES_GOOD)
-		ses->ses_status = SES_EXITING;
-
 	if (ses->ses_status == SES_EXITING && server->ops->logoff) {
 		spin_unlock(&ses->ses_lock);
 		cifs_free_ipc(ses);
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 70 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/smb/client/connect.c +4 -4
index 9d16626e7a66..165ecb222c19 100644
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING && server->ops->logoff) {
                spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Signature · 2 lines
Regards,
Shyam
#3 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Added Cc: stable and Shyam's RB and merged into cifs-2.6.git for-next

Quoted 82 lines · Shyam Prasad N
Quoted 70 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/smb/client/connect.c +4 -4
index 9d16626e7a66..165ecb222c19 100644
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING && server->ops->logoff) {
                spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

--
Regards,
Shyam

Signature · 3 lines
Thanks,

Steve
#4 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 89 lines · Steve French

Added Cc: stable and Shyam's RB and merged into cifs-2.6.git for-next

Quoted 79 lines · Shyam Prasad N
Quoted 70 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/smb/client/connect.c +4 -4
index 9d16626e7a66..165ecb222c19 100644
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING && server->ops->logoff) {
                spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

fs/smb/client/connect.c +2 -4
index 9d16626e7a66..78874eb2537d 100644
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Please let me know if you see a problem with this.

Signature · 2 lines
Regards,
Shyam
#5 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 136 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 95 lines · Steve French

Added Cc: stable and Shyam's RB and merged into cifs-2.6.git for-next

Quoted 83 lines · Shyam Prasad N
Quoted 73 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/smb/client/connect.c +4 -4
index 9d16626e7a66..165ecb222c19 100644
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses
 *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
 server->ops->logoff) { spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

fs/smb/client/connect.c +2 -4
index 9d16626e7a66..78874eb2537d 100644
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */

Perhaps we need some refactoring here.

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Signature · 2 lines
Thanks,
Winston
#6 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 153 lines · Winston Wen
Quoted 136 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 95 lines · Steve French

Added Cc: stable and Shyam's RB and merged into cifs-2.6.git for-next

Quoted 83 lines · Shyam Prasad N
Quoted 73 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/smb/client/connect.c +4 -4
index 9d16626e7a66..165ecb222c19 100644
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses
 *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
 server->ops->logoff) { spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

fs/smb/client/connect.c +2 -4
index 9d16626e7a66..78874eb2537d 100644
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
                cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

Quoted 8 lines

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Quoted 8 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

The other two patches are not strictly necessary with this change, but don't hurt.

--
Regards,
Shyam

From a4c43bd8e586af6d33cabbfe90fe9a9d4f31d464 Mon Sep 17 00:00:00 2001
From: Shyam Prasad N <[email protected]>
Date: Mon, 26 Jun 2023 08:19:08 +0000
Subject: [PATCH] cifs: remove smb session from list rest of the cleanup

There is a window between refcount reaching 0 and actually removing the ses off the list in cifs_put_smb_ses. During this window, some other users may bump up the refcount and start using it.

This change fixes it by eliminating the window, by removing the ses off the list first, then dropping the lock.

---
 fs/smb/client/connect.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/smb/client/connect.c +2 -4
index 9d16626e7a66..78874eb2537d 100644
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
 		spin_unlock(&cifs_tcp_ses_lock);
 		return;
 	}
-	spin_unlock(&cifs_tcp_ses_lock);
 
 	/* ses_count can never go negative */
 	WARN_ON(ses->ses_count < 0);
+	list_del_init(&ses->smb_ses_list);
+	spin_unlock(&cifs_tcp_ses_lock);
 
 	spin_lock(&ses->ses_lock);
 	if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
 		cifs_free_ipc(ses);
 	}
 
-	spin_lock(&cifs_tcp_ses_lock);
-	list_del_init(&ses->smb_ses_list);
-	spin_unlock(&cifs_tcp_ses_lock);
 
 	chan_count = ses->chan_count;
 
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 162 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 160 lines · Winston Wen
Quoted 136 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 95 lines · Steve French

Added Cc: stable and Shyam's RB and merged into cifs-2.6.git for-next

Quoted 83 lines · Shyam Prasad N
Quoted 73 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..165ecb222c19 100644 --- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c

(quoted diff) +4 -4
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
 server->ops->logoff) { spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

fs/smb/client/connect.c +2 -4
index 9d16626e7a66..78874eb2537d 100644
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct cifs_ses
 *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct cifs_ses
 *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 3 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 6 lines
Quoted 6 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 4 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

Signature · 2 lines
Thanks,
Winston
#8 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 190 lines · Winston Wen
Quoted 162 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 160 lines · Winston Wen
Quoted 136 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 95 lines · Steve French

Added Cc: stable and Shyam's RB and merged into cifs-2.6.git for-next

Quoted 83 lines · Shyam Prasad N
Quoted 73 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..165ecb222c19 100644 --- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c

(quoted diff) +4 -4
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
 server->ops->logoff) { spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

fs/smb/client/connect.c +2 -4
index 9d16626e7a66..78874eb2537d 100644
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct cifs_ses
 *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct cifs_ses
 *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 3 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 6 lines
Quoted 6 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

I don't really have a strong opinion about this. Even if they stay, I'm okay. But curious to know what you mean by the cleaning work on other paths here. Do you still think there's more cleanup needed around this?

Quoted 12 lines

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

--
Thanks,
Winston

Signature · 2 lines
Regards,
Shyam
#9 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 196 lines · On Tue, 27 Jun 2023 17:43:25 +0530 Shyam Prasad N <[email protected]>…
Quoted 194 lines · Winston Wen
Quoted 162 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 160 lines · Winston Wen
Quoted 136 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 95 lines · Steve French

Added Cc: stable and Shyam's RB and merged into cifs-2.6.git for-next

Quoted 83 lines · Shyam Prasad N
Quoted 73 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..165ecb222c19 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c

(quoted diff) +4 -4
@@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
 server->ops->logoff) { spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
--
Quoted 2 lines

2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

fs/smb/client/connect.c +2 -4
index 9d16626e7a66..78874eb2537d 100644
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct cifs_ses
 *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 3 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 6 lines
Quoted 6 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 4 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Quoted 8 lines
Quoted 3 lines

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

I don't really have a strong opinion about this. Even if they stay, I'm okay. But curious to know what you mean by the cleaning work on other paths here. Do you still think there's more cleanup needed around this?

IIRC there are other paths that scan the list and do the check, like cifs_find_smb_ses(). So I think if they become unnecessary now after this fix patch, maybe we can also remove them at the same time to avoid make others confused.

But I also don't have a strong opinion about this. I think we have the following options and all are okay to me. Which one do you prefer?

- keep/add the check
- remove all checks
- remove all checks and add a WARNING

(I think we shouldn't find a exiting session in the list now.)

Quoted 16 lines
Quoted 13 lines

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

--
Thanks,
Winston

Signature · 2 lines
Thanks,
Winston

A message that is not in the archive (sent off-list or not yet received): 20230628084333.7c9b8469@winn-pc

#11 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 214 lines · On Wed, 28 Jun 2023 08:43:33 +0800 Winston Wen <[email protected]> wrote:
Quoted 197 lines · On Tue, 27 Jun 2023 17:43:25 +0530 Shyam Prasad N <[email protected]>…
Quoted 195 lines · Winston Wen
Quoted 162 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 160 lines · Winston Wen
Quoted 136 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 95 lines · Steve French

Added Cc: stable and Shyam's RB and merged into
cifs-2.6.git for-next

Quoted 83 lines · Shyam Prasad N
Quoted 72 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..165ecb222c19 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)

spin_unlock(&cifs_tcp_ses_lock); return;
        }
(quoted diff) +4 -3
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
server->ops->logoff) { spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
Quoted 3 lines

--
2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..78874eb2537d 100644 --- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c

(quoted diff) +2 -4
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 4 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 6 lines
Quoted 6 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 4 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Quoted 8 lines
Quoted 3 lines

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

I don't really have a strong opinion about this. Even if they stay, I'm okay. But curious to know what you mean by the cleaning work on other paths here. Do you still think there's more cleanup needed around this?

IIRC there are other paths that scan the list and do the check, like cifs_find_smb_ses(). So I think if they become unnecessary now after this fix patch, maybe we can also remove them at the same time to avoid make others confused.

Quoted 28 lines

But I also don't have a strong opinion about this. I think we have the following options and all are okay to me. Which one do you prefer?

- keep/add the check
- remove all checks
- remove all checks and add a WARNING

(I think we shouldn't find a exiting session in the list now.)

Quoted 16 lines
Quoted 13 lines

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

--
Thanks,
Winston

Attaching the patch (remove all checks and add a warning)

Signature · 2 lines
Thanks,
Winston

From 047495d974dc896313a8c5a258ee654e0bdfefa2 Mon Sep 17 00:00:00 2001
From: Winston Wen <[email protected]>
Date: Wed, 28 Jun 2023 09:03:39 +0800
Subject: [PATCH] cifs: give a warning if existing session is found in server's
list

With Shyam's fix [1], we remove smb session from the list before setting its state to SES_EXITING, so we should never find a exiting session in the list now.

Remove the check and add a warning.

[1] https://lore.kernel.org/linux-cifs/CANT5p=oETR0vg29rGohLXoeqw0Lrrt8GsLbhjV6snLth7od=Nw@mail.gmail.com/

---
 fs/smb/client/connect.c       | 6 ++----
 fs/smb/client/smb2pdu.c       | 1 +
 fs/smb/client/smb2transport.c | 2 ++
 3 files changed, 5 insertions(+), 4 deletions(-)
fs/smb/client/connect.c +2 -4
index 78874eb2537d..3d35dc742615 100644
@@ -1920,11 +1920,9 @@ cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
 
 	spin_lock(&cifs_tcp_ses_lock);
 	list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
+		WARN_ON(ses->ses_status == SES_EXITING);
+
 		spin_lock(&ses->ses_lock);
-		if (ses->ses_status == SES_EXITING) {
-			spin_unlock(&ses->ses_lock);
-			continue;
-		}
 		spin_lock(&ses->chan_lock);
 		if (match_session(ses, ctx)) {
 			spin_unlock(&ses->chan_lock);
fs/smb/client/smb2pdu.c +1 -0
index 17fe212ab895..4d4504a8e63e 100644
@@ -3797,6 +3797,7 @@ void smb2_reconnect_server(struct work_struct *work)
 
 	spin_lock(&cifs_tcp_ses_lock);
 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
+		WARN_ON(ses->ses_status == SES_EXITING);
 
 		tcon_selected = false;
 
fs/smb/client/smb2transport.c +2 -0
index 790acf65a092..d0f9373620c7 100644
@@ -151,6 +151,8 @@ smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
 	pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
 
 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
+		WARN_ON(ses->ses_status == SES_EXITING);
+
 		if (ses->Suid != ses_id)
 			continue;
 		++ses->ses_count;
Signature · 1 line
2.40.1
#12 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

this didn't apply (merge conflict with Shyam's updated patch) - can you update it based on for-next and resend

Also let me know if you see other patches missing.

Quoted 252 lines · Winston Wen
Quoted 214 lines · On Wed, 28 Jun 2023 08:43:33 +0800 Winston Wen <[email protected]> wrote:
Quoted 197 lines · On Tue, 27 Jun 2023 17:43:25 +0530 Shyam Prasad N <[email protected]>…
Quoted 195 lines · Winston Wen
Quoted 162 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 160 lines · Winston Wen
Quoted 136 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 95 lines · Steve French

Added Cc: stable and Shyam's RB and merged into
cifs-2.6.git for-next

Quoted 83 lines · Shyam Prasad N
Quoted 72 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..165ecb222c19 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)

spin_unlock(&cifs_tcp_ses_lock); return;
        }
(quoted diff) +4 -3
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
server->ops->logoff) { spin_unlock(&ses->ses_lock);
                cifs_free_ipc(ses);
Quoted 3 lines

--
2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..78874eb2537d 100644 --- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c

(quoted diff) +2 -4
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 4 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 6 lines
Quoted 6 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 4 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Quoted 8 lines
Quoted 3 lines

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

I don't really have a strong opinion about this. Even if they stay, I'm okay. But curious to know what you mean by the cleaning work on other paths here. Do you still think there's more cleanup needed around this?

IIRC there are other paths that scan the list and do the check, like cifs_find_smb_ses(). So I think if they become unnecessary now after this fix patch, maybe we can also remove them at the same time to avoid make others confused.

Quoted 28 lines

But I also don't have a strong opinion about this. I think we have the following options and all are okay to me. Which one do you prefer?

- keep/add the check
- remove all checks
- remove all checks and add a WARNING

(I think we shouldn't find a exiting session in the list now.)

Quoted 16 lines
Quoted 13 lines

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

--
Thanks,
Winston

Attaching the patch (remove all checks and add a warning)

--
Thanks,
Winston

Signature · 3 lines
Thanks,

Steve
#13 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Hi Steve,

Quoted 4 lines · On Wed, 28 Jun 2023 12:16:09 -0500 Steve French <[email protected]> wrote:

this didn't apply (merge conflict with Shyam's updated patch) - can you update it based on for-next and resend

Also let me know if you see other patches missing.

With Shyam's updated patch, It is expected that no existing session would be found in the list, so the check of session state is no longer strictly necessary now, but don't hurt.

So we have 2 choices in scanning the list:
- do the check. (patch 2/3, merged)
- remove the check and add a warning. (this new patch)

If you find the latter to be better, you have the option to replace the original two patches with this new one.

Alternatively, you can simply disregard the new patch and take no action, as the patch 2/3 has already been merged into the for-next branch. (This may be a better choice for now, but I don't have a strong opinion on this, both are okay to me.)

Really sorry for my poor English and the lack of clarity in my explanation.

Quoted 261 lines
Quoted 255 lines · Winston Wen
Quoted 220 lines · On Wed, 28 Jun 2023 08:43:33 +0800 Winston Wen <[email protected]> wrote:
Quoted 199 lines · On Tue, 27 Jun 2023 17:43:25 +0530 Shyam Prasad N <[email protected]>…
Quoted 197 lines · Winston Wen
Quoted 164 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 162 lines · Winston Wen
Quoted 137 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 96 lines · Steve French

Added Cc: stable and Shyam's RB and merged into
cifs-2.6.git for-next

Quoted 84 lines · Shyam Prasad N
Quoted 71 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..165ecb222c19 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)

spin_unlock(&cifs_tcp_ses_lock); return;
        }
(quoted diff) +4 -3
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
Quoted 5 lines

server->ops->logoff) { spin_unlock(&ses->ses_lock); cifs_free_ipc(ses); --
2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..78874eb2537d 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c

(quoted diff) +2 -4
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 4 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 6 lines
Quoted 6 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 4 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Quoted 8 lines
Quoted 3 lines

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

I don't really have a strong opinion about this. Even if they stay, I'm okay. But curious to know what you mean by the cleaning work on other paths here. Do you still think there's more cleanup needed around this?

IIRC there are other paths that scan the list and do the check, like cifs_find_smb_ses(). So I think if they become unnecessary now after this fix patch, maybe we can also remove them at the same time to avoid make others confused.

But I also don't have a strong opinion about this. I think we have the following options and all are okay to me. Which one do you prefer?

Quoted 25 lines

- keep/add the check
- remove all checks
- remove all checks and add a WARNING

(I think we shouldn't find a exiting session in the list now.)

Quoted 16 lines
Quoted 13 lines

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

--
Thanks,
Winston

Attaching the patch (remove all checks and add a warning)

--
Thanks,
Winston

Signature · 2 lines
Thanks,
Winston
#14 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 294 lines · Winston Wen

Hi Steve,

Quoted 4 lines · On Wed, 28 Jun 2023 12:16:09 -0500 Steve French <[email protected]> wrote:

this didn't apply (merge conflict with Shyam's updated patch) - can you update it based on for-next and resend

Also let me know if you see other patches missing.

With Shyam's updated patch, It is expected that no existing session would be found in the list, so the check of session state is no longer strictly necessary now, but don't hurt.

So we have 2 choices in scanning the list:
- do the check. (patch 2/3, merged)
- remove the check and add a warning. (this new patch)

If you find the latter to be better, you have the option to replace the original two patches with this new one.

Alternatively, you can simply disregard the new patch and take no action, as the patch 2/3 has already been merged into the for-next branch. (This may be a better choice for now, but I don't have a strong opinion on this, both are okay to me.)

Really sorry for my poor English and the lack of clarity in my explanation.

Quoted 253 lines
Quoted 250 lines · Winston Wen
Quoted 245 lines · On Wed, 28 Jun 2023 08:43:33 +0800 Winston Wen <[email protected]> wrote:
Quoted 199 lines · On Tue, 27 Jun 2023 17:43:25 +0530 Shyam Prasad N <[email protected]>…
Quoted 197 lines · Winston Wen
Quoted 164 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 162 lines · Winston Wen
Quoted 137 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 96 lines · Steve French

Added Cc: stable and Shyam's RB and merged into
cifs-2.6.git for-next

Quoted 84 lines · Shyam Prasad N
Quoted 71 lines · Winston Wen

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.

---
 fs/smb/client/connect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..165ecb222c19 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1963,15 +1963,16 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)

spin_unlock(&cifs_tcp_ses_lock); return;
        }
(quoted diff) +4 -3
+       spin_lock(&ses->ses_lock);
+       if (ses->ses_status == SES_GOOD)
+               ses->ses_status = SES_EXITING;
+       spin_unlock(&ses->ses_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
 
        spin_lock(&ses->ses_lock);
-       if (ses->ses_status == SES_GOOD)
-               ses->ses_status = SES_EXITING;
-
        if (ses->ses_status == SES_EXITING &&
Quoted 5 lines

server->ops->logoff) { spin_unlock(&ses->ses_lock); cifs_free_ipc(ses); --
2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..78874eb2537d 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c

(quoted diff) +2 -4
@@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
                return;
        }
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0.
With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 4 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 6 lines
Quoted 6 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 4 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Quoted 8 lines
Quoted 3 lines

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

I don't really have a strong opinion about this. Even if they stay, I'm okay. But curious to know what you mean by the cleaning work on other paths here. Do you still think there's more cleanup needed around this?

IIRC there are other paths that scan the list and do the check, like cifs_find_smb_ses(). So I think if they become unnecessary now after this fix patch, maybe we can also remove them at the same time to avoid make others confused.

But I also don't have a strong opinion about this. I think we have the following options and all are okay to me. Which one do you prefer?

- keep/add the check
- remove all checks
- remove all checks and add a WARNING

(I think we shouldn't find a exiting session in the list now.)

Quoted 16 lines
Quoted 13 lines

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

--
Thanks,
Winston

Quoted 8 lines
Quoted 5 lines

Attaching the patch (remove all checks and add a warning)

--
Thanks,
Winston

Quoted 3 lines

--
Thanks,
Winston

Hi Winston/Steve,

It turns out that my patch above has a problem. For logoff to work, we need the session in the smb_ses_list. So I think Winston's patch series here make sense.

@Winston Wen We may need similar checks for SES_EXITING in other places where we iterate smb_ses_list.
Can you see if all the places are covered and send additional patches if needed? If you do not get to it in a few days, I can do it myself.

Thanks for these patches. Keep them coming. :)

Signature · 2 lines
Regards,
Shyam
#15 lore raw

Re: [PATCH 1/3] cifs: fix session state transition to avoid use-after-free issue

Quoted 311 lines · On Thu, 29 Jun 2023 21:50:18 +0530 Shyam Prasad N <[email protected]>…
Quoted 299 lines · Winston Wen

Hi Steve,

Quoted 4 lines · On Wed, 28 Jun 2023 12:16:09 -0500 Steve French <[email protected]> wrote:

this didn't apply (merge conflict with Shyam's updated patch) - can you update it based on for-next and resend

Also let me know if you see other patches missing.

With Shyam's updated patch, It is expected that no existing session would be found in the list, so the check of session state is no longer strictly necessary now, but don't hurt.

So we have 2 choices in scanning the list:
- do the check. (patch 2/3, merged)
- remove the check and add a warning. (this new patch)

If you find the latter to be better, you have the option to replace the original two patches with this new one.

Alternatively, you can simply disregard the new patch and take no action, as the patch 2/3 has already been merged into the for-next branch. (This may be a better choice for now, but I don't have a strong opinion on this, both are okay to me.)

Really sorry for my poor English and the lack of clarity in my explanation.

Quoted 258 lines
Quoted 255 lines · Winston Wen
Quoted 250 lines · On Wed, 28 Jun 2023 08:43:33 +0800 Winston Wen <[email protected]> wrote:
Quoted 204 lines · On Tue, 27 Jun 2023 17:43:25 +0530 Shyam Prasad N <[email protected]>…
Quoted 202 lines · Winston Wen
Quoted 168 lines · On Tue, 27 Jun 2023 12:24:04 +0530 Shyam Prasad N <[email protected]>…
Quoted 166 lines · Winston Wen
Quoted 140 lines · On Mon, 26 Jun 2023 12:24:35 +0530 Shyam Prasad N <[email protected]>…
Quoted 98 lines · Steve French

Added Cc: stable and Shyam's RB and merged into
cifs-2.6.git for-next

Quoted 86 lines · Shyam Prasad N
Quoted 78 lines · Winston Wen

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]> ---
fs/smb/client/connect.c | 7 ++++---
1 file changed, 4 insertions(+), 3
deletions(-)

diff --git a/fs/smb/client/connect.c
b/fs/smb/client/connect.c index
9d16626e7a66..165ecb222c19 100644 ---
a/fs/smb/client/connect.c +++
b/fs/smb/client/connect.c @@ -1963,15
+1963,16 @@ void __cifs_put_smb_ses(struct
cifs_ses *ses)
spin_unlock(&cifs_tcp_ses_lock); return; }
+ spin_lock(&ses->ses_lock);
+ if (ses->ses_status == SES_GOOD)
+ ses->ses_status = SES_EXITING;
+ spin_unlock(&ses->ses_lock);
spin_unlock(&cifs_tcp_ses_lock);

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

spin_lock(&ses->ses_lock);
- if (ses->ses_status == SES_GOOD)
- ses->ses_status = SES_EXITING;
-
if (ses->ses_status == SES_EXITING &&
server->ops->logoff) {
spin_unlock(&ses->ses_lock);
cifs_free_ipc(ses); --
2.40.1

Good catch.
Looks good to me.
@Steve French Please CC stable for this one.

Quoted 3 lines

--
Regards,
Shyam

Quoted 4 lines

--
Thanks,

Steve

@Winston Wen I think the following change should be sufficient to fix this issue:

(quoted diff) +0 -0

b/fs/smb/client/connect.c index 9d16626e7a66..78874eb2537d 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1963,10 +1963,11 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)

spin_unlock(&cifs_tcp_ses_lock); return;
        }
(quoted diff) +2 -4
-       spin_unlock(&cifs_tcp_ses_lock);
 
        /* ses_count can never go negative */
        WARN_ON(ses->ses_count < 0);
+       list_del_init(&ses->smb_ses_list);
+       spin_unlock(&cifs_tcp_ses_lock);
 
        spin_lock(&ses->ses_lock);
        if (ses->ses_status == SES_GOOD)
@@ -1986,9 +1987,6 @@ void __cifs_put_smb_ses(struct
 cifs_ses *ses) cifs_free_ipc(ses);
        }
 
-       spin_lock(&cifs_tcp_ses_lock);
-       list_del_init(&ses->smb_ses_list);
-       spin_unlock(&cifs_tcp_ses_lock);
 
        chan_count = ses->chan_count;
 

The bug was that the ses was kept in the smb ses list, even after the ref count had reached 0. With the above change, that should be fixed, and no one should be able to get to the ses from that point.

Quoted 2 lines

Please let me know if you see a problem with this.

Hi Shyam,

Thanks for the comments! And sorry for my late reply...

It make sense to me that maybe we should remove the session from the list once its refcount is reduced to 0 to avoid any futher access. In fact, I did try to do this from the beginning. But I was not sure if we need to access the session from the list in the free process, such as the following:

smb2_check_receive()
smb2_verify_signature()

    server->ops->calc_signature()
      smb2_calc_signature()
        smb2_find_smb_ses()
          /* scan the list and find the session */
Quoted 4 lines
Quoted 1 line

Perhaps we need some refactoring here.

Yes. The above ses finding is expected to fail during a reconnect.

Agreed.

Quoted 7 lines
Quoted 7 lines

So I gave up on this approach and did a small fix to make it work, but maybe I missed something elsewhere...

Quoted 6 lines
Quoted 3 lines

--
Thanks,
Winston

Attaching the above change as a patch.
It replaces this particular patch in the series.

I think this is a better way to fix the problem, the session really should not stay in the list and be found after it has been marked EXITING.

Quoted 4 lines

The other two patches are not strictly necessary with this change, but don't hurt.

Quoted 8 lines
Quoted 3 lines

Yes. Feel free to drop them if they are not necessary. And if that's the case, perhaps we should do some cleaning work on other paths to ensure consistency.

I don't really have a strong opinion about this. Even if they stay, I'm okay. But curious to know what you mean by the cleaning work on other paths here. Do you still think there's more cleanup needed around this?

IIRC there are other paths that scan the list and do the check, like cifs_find_smb_ses(). So I think if they become unnecessary now after this fix patch, maybe we can also remove them at the same time to avoid make others confused.

But I also don't have a strong opinion about this. I think we have the following options and all are okay to me. Which one do you prefer?

- keep/add the check
- remove all checks
- remove all checks and add a WARNING

(I think we shouldn't find a exiting session in the list now.)

Quoted 16 lines
Quoted 13 lines

Thanks for your review and comments!

Quoted 4 lines

--
Regards,
Shyam

--
Thanks,
Winston

Quoted 8 lines
Quoted 5 lines

Attaching the patch (remove all checks and add a warning)

--
Thanks,
Winston

Quoted 3 lines

--
Thanks,
Winston

Hi Winston/Steve,

It turns out that my patch above has a problem. For logoff to work, we need the session in the smb_ses_list. So I think Winston's patch series here make sense.

Quoted 7 lines

@Winston Wen We may need similar checks for SES_EXITING in other places where we iterate smb_ses_list.
Can you see if all the places are covered and send additional patches if needed? If you do not get to it in a few days, I can do it myself.

Thanks for these patches. Keep them coming. :)

Sorry for my late reply.

I checked all the places where we iterate smb_ses_list and didn't see any real issues. But there is a dangerous pattern here which is very easy to misuse, and I don't know how to fix it...

I think the rule when we iterate smb_ses_list is: we can't use a sesion which is exiting or may be exiting out of cifs_tcp_ses_lock.

In some case, like smb2_reconnect_server() and cifs_find_smb_ses(), we can simply do the check and filter out exiting sessions.

But in some other casees, like cifs_debug_files_proc_show(), smb2_check_message() and smb2_is_valid_oplock_break(), we may indeed need to handle all sessions, including those which are exiting.

And we must be very careful to don't use the session we got after we release cifs_tcp_ses_lock.

For example, the following change in smb2_check_message() will lead to potential use-after-free problem:

smb2_check_message()

{
	...
	struct cifs_ses *ses = NULL;
	struct cifs_ses *iter;

	/* decrypt frame now that it is completely read in */
	spin_lock(&cifs_tcp_ses_lock);
	list_for_each_entry(iter, &pserver->smb_ses_list, smb_ses_list)
	{ 
		if (iter->Suid == le64_to_cpu(thdr->SessionId)) {
			ses = iter;
			break;
		}
	}
	spin_unlock(&cifs_tcp_ses_lock);

	if (!ses) {
		cifs_dbg(VFS, "no decryption - session id not found\n");
		return 1;
	}

	/* New code: */ 
	cifs_dbg(FYI, "server inflight=%d\n",
			ses->server->in_flight);

	...
}

Because once we realse cifs_tcp_ses_lock, we can no longer use the session.

I'm not sure if this needs to be fixed and/or how to fix it, what do you think about it?

Signature · 2 lines
Thanks,
Winston
#16 lore raw

PATCH 2/3 cifs: fix session state check in reconnect to avoid use-after-free issue

In reply to Winston Wen

Don't collect exiting session in smb2_reconnect_server(), because it will be released soon.

Note that the exiting session will stay in server->smb_ses_list until it complete the cifs_free_ipc() and logoff() and then delete itself from the list.

---
 fs/smb/client/smb2pdu.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/smb/client/smb2pdu.c +6 -0
index 17fe212ab895..e04766fe6f80 100644
@@ -3797,6 +3797,12 @@ void smb2_reconnect_server(struct work_struct *work)
 
 	spin_lock(&cifs_tcp_ses_lock);
 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
+		spin_lock(&ses->ses_lock);
+		if (ses->ses_status == SES_EXITING) {
+			spin_unlock(&ses->ses_lock);
+			continue;
+		}
+		spin_unlock(&ses->ses_lock);
 
 		tcon_selected = false;
 
Signature · 1 line
2.40.1
Reviewed-by on v1 2/3
#17 lore raw

Re: [PATCH 2/3] cifs: fix session state check in reconnect to avoid use-after-free issue

Quoted 31 lines · Winston Wen

Don't collect exiting session in smb2_reconnect_server(), because it will be released soon.

Note that the exiting session will stay in server->smb_ses_list until it complete the cifs_free_ipc() and logoff() and then delete itself from the list.

---
 fs/smb/client/smb2pdu.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/smb/client/smb2pdu.c +6 -1
index 17fe212ab895..e04766fe6f80 100644
@@ -3797,6 +3797,12 @@ void smb2_reconnect_server(struct work_struct *work)
 
        spin_lock(&cifs_tcp_ses_lock);
        list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
+               spin_unlock(&ses->ses_lock);
 
                tcon_selected = false;
 
--
Quoted 2 lines

2.40.1

Hi Winston,

We already have this check in smb2_reconnect, which gets called from smb2_reconnect_server.
But one additional check here will not hurt.

Signature · 2 lines
Regards,
Shyam
#18 lore raw

Re: [PATCH 2/3] cifs: fix session state check in reconnect to avoid use-after-free issue

Quoted 40 lines · On Mon, 26 Jun 2023 10:57:32 +0530 Shyam Prasad N <[email protected]>…
Quoted 32 lines · Winston Wen

Don't collect exiting session in smb2_reconnect_server(), because it will be released soon.

Note that the exiting session will stay in server->smb_ses_list until it complete the cifs_free_ipc() and logoff() and then delete itself from the list.

---
 fs/smb/client/smb2pdu.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/smb/client/smb2pdu.c +6 -0
index 17fe212ab895..e04766fe6f80 100644
@@ -3797,6 +3797,12 @@ void smb2_reconnect_server(struct
 work_struct *work)
 
        spin_lock(&cifs_tcp_ses_lock);
        list_for_each_entry(ses, &pserver->smb_ses_list,
 smb_ses_list) {
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
+               spin_unlock(&ses->ses_lock);
 
                tcon_selected = false;
Quoted 3 lines

--
2.40.1

Hi Winston,

Quoted 6 lines

We already have this check in smb2_reconnect, which gets called from smb2_reconnect_server.
But one additional check here will not hurt.

Hi Shyam,

Thanks for the review! And sorry for my mistake that when I replied a minute ago I forgot to cc others...

I think the check in smb2_reconnect is not enough for this situation, but maybe I missed something...

Consider the following process:

smb2_reconnect_server():

    spin_lock(&cifs_tcp_ses_lock)
    list_for_each_entry(ses, ...)
        ...
        if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect)
            cifs_smb_ses_inc_refcount(ses)
    spin_unlock(&cifs_tcp_ses_lock)

    /* -> session may have been released before smb2_reconnect */
    list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist)
        smb2_reconnect()
        list_del_init(&tcon->rlist)
        if (tcon->ipc)
            cifs_put_smb_ses(tcon->ses)
        else
            cifs_put_tcon(tcon)

When we do smb2_reconnect(), the session may have been released, and all the access to its field in smb2_reconnect(), such as ses->status or ses->ses_lock, is illegal. And when we call the cifs_put_smb_ses() on it again, it will crash...

#19 lore raw

Re: [PATCH 2/3] cifs: fix session state check in reconnect to avoid use-after-free issue

Quoted 78 lines · Winston Wen
Quoted 40 lines · On Mon, 26 Jun 2023 10:57:32 +0530 Shyam Prasad N <[email protected]>…
Quoted 32 lines · Winston Wen

Don't collect exiting session in smb2_reconnect_server(), because it will be released soon.

Note that the exiting session will stay in server->smb_ses_list until it complete the cifs_free_ipc() and logoff() and then delete itself from the list.

---
 fs/smb/client/smb2pdu.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/smb/client/smb2pdu.c +6 -0
index 17fe212ab895..e04766fe6f80 100644
@@ -3797,6 +3797,12 @@ void smb2_reconnect_server(struct
 work_struct *work)
 
        spin_lock(&cifs_tcp_ses_lock);
        list_for_each_entry(ses, &pserver->smb_ses_list,
 smb_ses_list) {
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
+               spin_unlock(&ses->ses_lock);
 
                tcon_selected = false;
Quoted 3 lines

--
2.40.1

Hi Winston,

Quoted 6 lines

We already have this check in smb2_reconnect, which gets called from smb2_reconnect_server.
But one additional check here will not hurt.

Hi Shyam,

Thanks for the review! And sorry for my mistake that when I replied a minute ago I forgot to cc others...

I think the check in smb2_reconnect is not enough for this situation, but maybe I missed something...

Consider the following process:

smb2_reconnect_server():

    spin_lock(&cifs_tcp_ses_lock)
    list_for_each_entry(ses, ...)
        ...
        if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect)
            cifs_smb_ses_inc_refcount(ses)
    spin_unlock(&cifs_tcp_ses_lock)

    /* -> session may have been released before smb2_reconnect */
    list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist)
        smb2_reconnect()
        list_del_init(&tcon->rlist)
        if (tcon->ipc)
            cifs_put_smb_ses(tcon->ses)
        else
            cifs_put_tcon(tcon)
Quoted 4 lines

When we do smb2_reconnect(), the session may have been released, and all the access to its field in smb2_reconnect(), such as ses->status or ses->ses_lock, is illegal. And when we call the cifs_put_smb_ses() on it again, it will crash...

I see what you mean.

I think __cifs_put_smb_ses is at fault here.
Once the ses_count reaches 0, it should do all the following before it drops cifs_tcp_ses_lock:
1. Mark as SES_EXITING
2. Remove the session from it's list.

That way, smb2_reconnect_server should not even be able to find the session in the list.

Signature · 2 lines
Regards,
Shyam
#20 lore raw

Re: [PATCH 2/3] cifs: fix session state check in reconnect to avoid use-after-free issue

Quoted 87 lines · On Mon, 26 Jun 2023 11:57:16 +0530 Shyam Prasad N <[email protected]>…
Quoted 78 lines · Winston Wen
Quoted 40 lines · On Mon, 26 Jun 2023 10:57:32 +0530 Shyam Prasad N <[email protected]>…
Quoted 32 lines · Winston Wen

Don't collect exiting session in smb2_reconnect_server(), because it will be released soon.

Note that the exiting session will stay in server->smb_ses_list until it complete the cifs_free_ipc() and logoff() and then delete itself from the list.

---
 fs/smb/client/smb2pdu.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/smb/client/smb2pdu.c +6 -0
index 17fe212ab895..e04766fe6f80 100644
@@ -3797,6 +3797,12 @@ void smb2_reconnect_server(struct
 work_struct *work)
 
        spin_lock(&cifs_tcp_ses_lock);
        list_for_each_entry(ses, &pserver->smb_ses_list,
 smb_ses_list) {
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
+               spin_unlock(&ses->ses_lock);
 
                tcon_selected = false;
Quoted 3 lines

--
2.40.1

Hi Winston,

Quoted 6 lines

We already have this check in smb2_reconnect, which gets called from smb2_reconnect_server.
But one additional check here will not hurt.

Hi Shyam,

Thanks for the review! And sorry for my mistake that when I replied a minute ago I forgot to cc others...

I think the check in smb2_reconnect is not enough for this situation, but maybe I missed something...

Consider the following process:

smb2_reconnect_server():

    spin_lock(&cifs_tcp_ses_lock)
    list_for_each_entry(ses, ...)
        ...
        if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect)
            cifs_smb_ses_inc_refcount(ses)
    spin_unlock(&cifs_tcp_ses_lock)

    /* -> session may have been released before smb2_reconnect */
    list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist)
        smb2_reconnect()
        list_del_init(&tcon->rlist)
        if (tcon->ipc)
            cifs_put_smb_ses(tcon->ses)
        else
            cifs_put_tcon(tcon)
Quoted 4 lines

When we do smb2_reconnect(), the session may have been released, and all the access to its field in smb2_reconnect(), such as ses->status or ses->ses_lock, is illegal. And when we call the cifs_put_smb_ses() on it again, it will crash...

I see what you mean.

Quoted 9 lines

I think __cifs_put_smb_ses is at fault here.
Once the ses_count reaches 0, it should do all the following before it drops cifs_tcp_ses_lock:
1. Mark as SES_EXITING
2. Remove the session from it's list.

That way, smb2_reconnect_server should not even be able to find the session in the list.

Hi Shyam,

Please see the reply in patch 1.

Signature · 2 lines
Thanks,
Winston
#21 lore raw

Re: [PATCH 2/3] cifs: fix session state check in reconnect to avoid use-after-free issue

In reply to Shyam Prasad N

added RB and merged into cifs-2.6.git for-next

Quoted 47 lines · Shyam Prasad N
Quoted 31 lines · Winston Wen

Don't collect exiting session in smb2_reconnect_server(), because it will be released soon.

Note that the exiting session will stay in server->smb_ses_list until it complete the cifs_free_ipc() and logoff() and then delete itself from the list.

---
 fs/smb/client/smb2pdu.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/smb/client/smb2pdu.c +6 -1
index 17fe212ab895..e04766fe6f80 100644
@@ -3797,6 +3797,12 @@ void smb2_reconnect_server(struct work_struct *work)
 
        spin_lock(&cifs_tcp_ses_lock);
        list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
+               spin_unlock(&ses->ses_lock);
 
                tcon_selected = false;
 
--
Quoted 2 lines

2.40.1

Hi Winston,

We already have this check in smb2_reconnect, which gets called from smb2_reconnect_server.
But one additional check here will not hurt.

--
Regards,
Shyam

Signature · 3 lines
Thanks,

Steve
#22 lore raw

PATCH 3/3 cifs: fix session state check in smb2_find_smb_ses

In reply to Winston Wen

Chech the session state and skip it if it's exiting.

---
 fs/smb/client/smb2transport.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/smb/client/smb2transport.c +7 -0
index 790acf65a092..22954a9c7a6c 100644
@@ -153,7 +153,14 @@ smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
 		if (ses->Suid != ses_id)
 			continue;
+
+		spin_lock(&ses->ses_lock);
+		if (ses->ses_status == SES_EXITING) {
+			spin_unlock(&ses->ses_lock);
+			continue;
+		}
 		++ses->ses_count;
+		spin_unlock(&ses->ses_lock);
 		return ses;
 	}
 
Signature · 1 line
2.40.1
#23 lore raw

Re: [PATCH 3/3] cifs: fix session state check in smb2_find_smb_ses

Quoted 28 lines · Winston Wen

Chech the session state and skip it if it's exiting.

---
 fs/smb/client/smb2transport.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/smb/client/smb2transport.c +7 -1
index 790acf65a092..22954a9c7a6c 100644
@@ -153,7 +153,14 @@ smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
        list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
                if (ses->Suid != ses_id)
                        continue;
+
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
                ++ses->ses_count;
+               spin_unlock(&ses->ses_lock);
                return ses;
        }
 
--
Quoted 2 lines

2.40.1

Thanks for the change.
Looks good to me.
CC stable for this one too.

Signature · 2 lines
Regards,
Shyam
#24 lore raw

Re: [PATCH 3/3] cifs: fix session state check in smb2_find_smb_ses

added RB and merged into cifs-2.6.git for-next

Quoted 40 lines · Shyam Prasad N
Quoted 28 lines · Winston Wen

Chech the session state and skip it if it's exiting.

---
 fs/smb/client/smb2transport.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/smb/client/smb2transport.c +7 -1
index 790acf65a092..22954a9c7a6c 100644
@@ -153,7 +153,14 @@ smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
        list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
                if (ses->Suid != ses_id)
                        continue;
+
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
                ++ses->ses_count;
+               spin_unlock(&ses->ses_lock);
                return ses;
        }
 
--
Quoted 2 lines

2.40.1

Thanks for the change.
Looks good to me.
CC stable for this one too.

--
Regards,
Shyam

Signature · 3 lines
Thanks,

Steve
Applied
#25 lore raw

Re: [PATCH 3/3] cifs: fix session state check in smb2_find_smb_ses

In reply to Shyam Prasad N

merged into cifs-2.6.git for-next, added cc: stable and RB

Quoted 40 lines · Shyam Prasad N
Quoted 28 lines · Winston Wen

Chech the session state and skip it if it's exiting.

---
 fs/smb/client/smb2transport.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/smb/client/smb2transport.c +7 -1
index 790acf65a092..22954a9c7a6c 100644
@@ -153,7 +153,14 @@ smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
        list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
                if (ses->Suid != ses_id)
                        continue;
+
+               spin_lock(&ses->ses_lock);
+               if (ses->ses_status == SES_EXITING) {
+                       spin_unlock(&ses->ses_lock);
+                       continue;
+               }
                ++ses->ses_count;
+               spin_unlock(&ses->ses_lock);
                return ses;
        }
 
--
Quoted 2 lines

2.40.1

Thanks for the change.
Looks good to me.
CC stable for this one too.

--
Regards,
Shyam

Signature · 3 lines
Thanks,

Steve