Skip to contentSkip to conversation

erofs: add error handling for erofs_fill_super()

PATCH In review Chengguang Xu 2 participants 4 messages Nov 19, 2019 – Nov 20, 2019 EROFS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs: add error handling for erofs_fill_super()

There are some potential resource leaks in error case of erofs_fill_super(), so add proper error handling for it.

---
 fs/erofs/super.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
fs/erofs/super.c +23 -8
index 0e369494f2f2..06e721bd1c8c 100644
@@ -369,7 +369,7 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 	sb->s_fs_info = sbi;
 	err = erofs_read_superblock(sb);
 	if (err)
-		return err;
+		goto free;
 
 	sb->s_flags |= SB_RDONLY | SB_NOATIME;
 	sb->s_maxbytes = MAX_LFS_FILESIZE;
@@ -385,7 +385,7 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 
 	err = erofs_parse_options(sb, data);
 	if (err)
-		return err;
+		goto free;
 
 	if (test_opt(sbi, POSIX_ACL))
 		sb->s_flags |= SB_POSIXACL;
@@ -398,29 +398,44 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 
 	/* get the root inode */
 	inode = erofs_iget(sb, ROOT_NID(sbi), true);
-	if (IS_ERR(inode))
-		return PTR_ERR(inode);
+	if (IS_ERR(inode)) {
+		err = PTR_ERR(inode);
+		goto free;
+	}
 
 	if (!S_ISDIR(inode->i_mode)) {
 		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
 			  ROOT_NID(sbi), inode->i_mode);
 		iput(inode);
-		return -EINVAL;
+		err = -EINVAL;
+		goto free;
 	}
 
 	sb->s_root = d_make_root(inode);
-	if (!sb->s_root)
-		return -ENOMEM;
+	if (!sb->s_root) {
+		err = -ENOMEM;
+		goto free;
+	}
 
 	erofs_shrinker_register(sb);
 	/* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
 	err = erofs_init_managed_cache(sb);
 	if (err)
-		return err;
+		goto free_root_inode;
 
 	erofs_info(sb, "mounted with opts: %s, root inode @ nid %llu.",
 		   (char *)data, ROOT_NID(sbi));
+
 	return 0;
+
+free_root_inode:
+	dput(sb->s_root);
+	sb->s_root = NULL;
+	erofs_shrinker_unregister(sb);
+free:
+	kfree(sbi);
+	sb->s_fs_info = NULL;
+	return err;
 }
 
 static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
Signature · 1 line
2.20.1
#1 lore raw

Hi Chengguang,

Quoted 14 lines · Chengguang Xu

There are some potential resource leaks in error case of erofs_fill_super(), so add proper error handling for it.

---
 fs/erofs/super.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
fs/erofs/super.c +0 -0
index 0e369494f2f2..06e721bd1c8c 100644

fs/erofs/super.c static int erofs_fill_super(struct super_block *sb, void *data, int silent) · line 372

Quoted 5 lines
fs/erofs/super.c +1 -1
@@ -369,7 +369,7 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 	sb->s_fs_info = sbi;
 	err = erofs_read_superblock(sb);
 	if (err)
-		return err;
+		goto free;

Could you give some hints what is the potential leak exactly? Actually, it was modified on purpose recently, see the following threads:
https://lore.kernel.org/r/[email protected]
and
https://lore.kernel.org/r/[email protected]

Thanks,
Gao Xiang

#2 lore raw

---- 在 星期二, 2019-11-19 20:51:13 Gao Xiang <[email protected]> 撰写 ----

Quoted 12 lines

Hi Chengguang,

Quoted 9 lines · Chengguang Xu

There are some potential resource leaks in error case of erofs_fill_super(), so add proper error handling for it.

---
 fs/erofs/super.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
Quoted 16 lines

fs/erofs/super.c static int erofs_fill_super(struct super_block *sb, void *data, int silent) · line 372

Quoted 10 lines
fs/erofs/super.c +1 -1
index 0e369494f2f2..06e721bd1c8c 100644
@@ -369,7 +369,7 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
     sb->s_fs_info = sbi;
     err = erofs_read_superblock(sb);
     if (err)
-        return err;
+        goto free;

Could you give some hints what is the potential leak exactly? Actually, it was modified on purpose recently, see the following threads:
https://lore.kernel.org/r/[email protected]
and
https://lore.kernel.org/r/[email protected]

Sorry, it seems I misread some part of code, please just drop the patch.

Thanks

#3 lore raw
In reply to Gao Xiang

---- 在 星期二, 2019-11-19 20:51:13 Gao Xiang <[email protected]> 撰写 ----

Quoted 12 lines

Hi Chengguang,

Quoted 9 lines · Chengguang Xu

There are some potential resource leaks in error case of erofs_fill_super(), so add proper error handling for it.

---
 fs/erofs/super.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
Quoted 16 lines

fs/erofs/super.c static int erofs_fill_super(struct super_block *sb, void *data, int silent) · line 372

Quoted 10 lines
fs/erofs/super.c +1 -1
index 0e369494f2f2..06e721bd1c8c 100644
@@ -369,7 +369,7 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
     sb->s_fs_info = sbi;
     err = erofs_read_superblock(sb);
     if (err)
-        return err;
+        goto free;

Could you give some hints what is the potential leak exactly? Actually, it was modified on purpose recently, see the following threads:
https://lore.kernel.org/r/[email protected]
and
https://lore.kernel.org/r/[email protected]

Sorry, it seems I misread some part of code, please just drop the patch.

Thanks