Skip to contentSkip to conversation

cifs: verify that tcon is valid before dereference in cifs_kill_sb

PATCH In review Ronnie Sahlberg 3 participants 3 messages Apr 13, 2022 – Apr 14, 2022

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

PATCH 1/1 cifs: verify that tcon is valid before dereference in cifs_kill_sb

On umount, cifs_sb->tlink_tree might contain entries that do not represent a valid tcon.
Check the tcon for error before we dereference it.

---
 fs/cifs/cifsfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/cifs/cifsfs.c +3 -3
index aba0783a8f09..25719b70564a 100644
@@ -266,10 +266,11 @@ static void cifs_kill_sb(struct super_block *sb)
 	 * before we kill the sb.
 	 */
 	if (cifs_sb->root) {
-		node = rb_first(root);
-		while (node != NULL) {
+		for(node = rb_first(root); node; node = rb_next(node)) {
 			tlink = rb_entry(node, struct tcon_link, tl_rbnode);
 			tcon = tlink_tcon(tlink);
+			if (IS_ERR(tcon))
+				continue;
 			cfid = &tcon->crfid;
 			mutex_lock(&cfid->fid_mutex);
 			if (cfid->dentry) {
@@ -277,7 +278,6 @@ static void cifs_kill_sb(struct super_block *sb)
 				cfid->dentry = NULL;
 			}
 			mutex_unlock(&cfid->fid_mutex);
-			node = rb_next(node);
 		}
 
 		/* finally release root dentry */
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines · Ronnie Sahlberg

On umount, cifs_sb->tlink_tree might contain entries that do not represent a valid tcon.
Check the tcon for error before we dereference it.

---
 fs/cifs/cifsfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/cifs/cifsfs.c +3 -4
index aba0783a8f09..25719b70564a 100644
@@ -266,10 +266,11 @@ static void cifs_kill_sb(struct super_block *sb)
         * before we kill the sb.
         */
        if (cifs_sb->root) {
-               node = rb_first(root);
-               while (node != NULL) {
+               for(node = rb_first(root); node; node = rb_next(node)) {
                        tlink = rb_entry(node, struct tcon_link, tl_rbnode);
                        tcon = tlink_tcon(tlink);
+                       if (IS_ERR(tcon))
+                               continue;
                        cfid = &tcon->crfid;
                        mutex_lock(&cfid->fid_mutex);
                        if (cfid->dentry) {
@@ -277,7 +278,6 @@ static void cifs_kill_sb(struct super_block *sb)
                                cfid->dentry = NULL;
                        }
                        mutex_unlock(&cfid->fid_mutex);
-                       node = rb_next(node);
                }
 
                /* finally release root dentry */
--
Quoted 2 lines

2.30.2

The changes look good to me.

Does this cover all the mount failure scenarios, Ronnie?

Signature · 2 lines
Regards,
Shyam
#2 lore raw
Quoted 43 lines · Shyam Prasad N
Quoted 37 lines · Ronnie Sahlberg

On umount, cifs_sb->tlink_tree might contain entries that do not represent a valid tcon.
Check the tcon for error before we dereference it.

---
 fs/cifs/cifsfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/cifs/cifsfs.c +3 -4
index aba0783a8f09..25719b70564a 100644
@@ -266,10 +266,11 @@ static void cifs_kill_sb(struct super_block *sb)
         * before we kill the sb.
         */
        if (cifs_sb->root) {
-               node = rb_first(root);
-               while (node != NULL) {
+               for(node = rb_first(root); node; node = rb_next(node)) {
                        tlink = rb_entry(node, struct tcon_link, tl_rbnode);
                        tcon = tlink_tcon(tlink);
+                       if (IS_ERR(tcon))
+                               continue;
                        cfid = &tcon->crfid;
                        mutex_lock(&cfid->fid_mutex);
                        if (cfid->dentry) {
@@ -277,7 +278,6 @@ static void cifs_kill_sb(struct super_block *sb)
                                cfid->dentry = NULL;
                        }
                        mutex_unlock(&cfid->fid_mutex);
-                       node = rb_next(node);
                }
 
                /* finally release root dentry */
--
Quoted 2 lines

2.30.2

The changes look good to me.

Quoted 3 lines

Does this cover all the mount failure scenarios, Ronnie?

It should. It should also fix similar issues at umount if an idle tcon has been reaped, or say a tcon failed to initialize during multiuser. For example, mount multiuser, then try to access the share with a different user without credentials. That second tcon will fail and then when we umount we would oops.

Quoted 4 lines

--
Regards,
Shyam