Skip to contentSkip to conversation

cifs: fix incorrect use of list iterator after the loop

PATCH In review Xiaomeng Tong 3 participants 4 messages Mar 20, 2022 – Mar 23, 2022

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

PATCH 1/1 cifs: fix incorrect use of list iterator after the loop

The bug is here:

if (!tcon) {
	resched = true;
	list_del_init(&ses->rlist);
	cifs_put_smb_ses(ses);

Because the list_for_each_entry() never exits early (without any break/goto/return inside the loop), the iterator 'ses' after the loop will always be an pointer to a invalid struct containing the HEAD (&pserver->smb_ses_list). As a result, the uses of 'ses' above will lead to a invalid memory access.

The original intention should have been to walk each entry 'ses' in '&tmp_ses_list', delete '&ses->rlist' and put 'ses'. So fix it with a list_for_each_entry_safe().

Fixes 3663c9045f51a ("cifs: check reconnects for channels of active tcons too")
---
 fs/cifs/smb2pdu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/cifs/smb2pdu.c +4 -2
index 7e7909b1ae11..f82d6fcb5c64 100644
@@ -3858,8 +3858,10 @@ void smb2_reconnect_server(struct work_struct *work)
 	tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
 	if (!tcon) {
 		resched = true;
-		list_del_init(&ses->rlist);
-		cifs_put_smb_ses(ses);
+		list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
+			list_del_init(&ses->rlist);
+			cifs_put_smb_ses(ses);
+		}
 		goto done;
 	}
 

base-commit: 14702b3b2438e2f2d07ae93b5d695c166e5c83d1

Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 41 lines · Xiaomeng Tong

The bug is here:

if (!tcon) {
        resched = true;
        list_del_init(&ses->rlist);
        cifs_put_smb_ses(ses);

Because the list_for_each_entry() never exits early (without any break/goto/return inside the loop), the iterator 'ses' after the loop will always be an pointer to a invalid struct containing the HEAD (&pserver->smb_ses_list). As a result, the uses of 'ses' above will lead to a invalid memory access.

The original intention should have been to walk each entry 'ses' in '&tmp_ses_list', delete '&ses->rlist' and put 'ses'. So fix it with a list_for_each_entry_safe().

Fixes 3663c9045f51a ("cifs: check reconnects for channels of active tcons too")
---
 fs/cifs/smb2pdu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/cifs/smb2pdu.c +4 -2
index 7e7909b1ae11..f82d6fcb5c64 100644
@@ -3858,8 +3858,10 @@ void smb2_reconnect_server(struct work_struct *work)
        tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
        if (!tcon) {
                resched = true;
-               list_del_init(&ses->rlist);
-               cifs_put_smb_ses(ses);
+               list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
+                       list_del_init(&ses->rlist);
+                       cifs_put_smb_ses(ses);
+               }
                goto done;
        }
 
Quoted 4 lines

base-commit: 14702b3b2438e2f2d07ae93b5d695c166e5c83d1 --
2.17.1

Hi Xiaomeng,
Good catch.

Steve, This one needs to be marked for CC stable 5.17+

Signature · 2 lines
Regards,
Shyam
#2 lore raw
cc stable too?
Quoted 58 lines · Shyam Prasad N via samba-technical
Quoted 41 lines · Xiaomeng Tong

The bug is here:

if (!tcon) {
        resched = true;
        list_del_init(&ses->rlist);
        cifs_put_smb_ses(ses);

Because the list_for_each_entry() never exits early (without any break/goto/return inside the loop), the iterator 'ses' after the loop will always be an pointer to a invalid struct containing the HEAD (&pserver->smb_ses_list). As a result, the uses of 'ses' above will lead to a invalid memory access.

The original intention should have been to walk each entry 'ses' in '&tmp_ses_list', delete '&ses->rlist' and put 'ses'. So fix it with a list_for_each_entry_safe().

Fixes 3663c9045f51a ("cifs: check reconnects for channels of active tcons too")
---
 fs/cifs/smb2pdu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/cifs/smb2pdu.c +4 -2
index 7e7909b1ae11..f82d6fcb5c64 100644
@@ -3858,8 +3858,10 @@ void smb2_reconnect_server(struct work_struct *work)
        tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
        if (!tcon) {
                resched = true;
-               list_del_init(&ses->rlist);
-               cifs_put_smb_ses(ses);
+               list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
+                       list_del_init(&ses->rlist);
+                       cifs_put_smb_ses(ses);
+               }
                goto done;
        }
 
Quoted 4 lines

base-commit: 14702b3b2438e2f2d07ae93b5d695c166e5c83d1 --
2.17.1

Hi Xiaomeng,
Good catch.

Steve, This one needs to be marked for CC stable 5.17+

--
Regards,
Shyam

Signature · 3 lines
Thanks,

Steve
#3 lore raw
In reply to Shyam Prasad N

tentatively merged into cifs-2.6.git for-next pending additional testing

Also added cc:stable 5.17

Quoted 58 lines · Shyam Prasad N via samba-technical
Quoted 41 lines · Xiaomeng Tong

The bug is here:

if (!tcon) {
        resched = true;
        list_del_init(&ses->rlist);
        cifs_put_smb_ses(ses);

Because the list_for_each_entry() never exits early (without any break/goto/return inside the loop), the iterator 'ses' after the loop will always be an pointer to a invalid struct containing the HEAD (&pserver->smb_ses_list). As a result, the uses of 'ses' above will lead to a invalid memory access.

The original intention should have been to walk each entry 'ses' in '&tmp_ses_list', delete '&ses->rlist' and put 'ses'. So fix it with a list_for_each_entry_safe().

Fixes 3663c9045f51a ("cifs: check reconnects for channels of active tcons too")
---
 fs/cifs/smb2pdu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/cifs/smb2pdu.c +4 -2
index 7e7909b1ae11..f82d6fcb5c64 100644
@@ -3858,8 +3858,10 @@ void smb2_reconnect_server(struct work_struct *work)
        tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
        if (!tcon) {
                resched = true;
-               list_del_init(&ses->rlist);
-               cifs_put_smb_ses(ses);
+               list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
+                       list_del_init(&ses->rlist);
+                       cifs_put_smb_ses(ses);
+               }
                goto done;
        }
 
Quoted 4 lines

base-commit: 14702b3b2438e2f2d07ae93b5d695c166e5c83d1 --
2.17.1

Hi Xiaomeng,
Good catch.

Steve, This one needs to be marked for CC stable 5.17+

--
Regards,
Shyam

Signature · 3 lines
Thanks,

Steve