Skip to contentSkip to conversation

fix 'proc_info_root' leak about ntfs procfs

PATCH New Ye Bin 1 participant 4 messages Oct 11, 2024 NTFS3 filesystem

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

cover fix 'proc_info_root' leak about ntfs procfs

From: Ye Bin <[email protected]>

Ye Bin (3):
fs/ntfs3: factor out ntfs_{create/remove}_procdir() fs/ntfs3: factor out ntfs_{create/remove}_proc_root() fs/ntfs3: fix 'proc_info_root' leak when init ntfs failed

 fs/ntfs3/super.c | 89 ++++++++++++++++++++++++++++++------------------
 1 file changed, 56 insertions(+), 33 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/3 fs/ntfs3: factor out ntfs_{create/remove}_procdir()

From: Ye Bin <[email protected]>

Introduce ntfs_create_procdir() and ntfs_remove_procdir() to create/remove "/proc/fs/ntfs3/.."

---
 fs/ntfs3/super.c | 59 +++++++++++++++++++++++++++++-------------------
 1 file changed, 36 insertions(+), 23 deletions(-)
fs/ntfs3/super.c +36 -23
index 128d49512f5d..660f11c6259c 100644
@@ -550,6 +550,40 @@ static const struct proc_ops ntfs3_label_fops = {
 	.proc_write = ntfs3_label_write,
 };
 
+static void ntfs_create_procdir(struct super_block *sb)
+{
+	struct proc_dir_entry *e;
+
+	if (!proc_info_root)
+		return;
+
+	e = proc_mkdir(sb->s_id, proc_info_root);
+	if (e) {
+		struct ntfs_sb_info *sbi = sb->s_fs_info;
+
+		proc_create_data("volinfo", 0444, e,
+				 &ntfs3_volinfo_fops, sb);
+		proc_create_data("label", 0644, e,
+				 &ntfs3_label_fops, sb);
+		sbi->procdir = e;
+	}
+}
+
+static void ntfs_remove_procdir(struct super_block *sb)
+{
+	struct ntfs_sb_info *sbi = sb->s_fs_info;
+
+	if (!sbi->procdir)
+		return;
+
+	remove_proc_entry("label", sbi->procdir);
+	remove_proc_entry("volinfo", sbi->procdir);
+	remove_proc_entry(sb->s_id, proc_info_root);
+	sbi->procdir = NULL;
+}
+#else
+static void ntfs_create_procdir(struct super_block *sb) {}
+static void ntfs_remove_procdir(struct super_block *sb) {}
 #endif
 
 static struct kmem_cache *ntfs_inode_cachep;
@@ -639,15 +673,7 @@ static void ntfs_put_super(struct super_block *sb)
 {
 	struct ntfs_sb_info *sbi = sb->s_fs_info;
 
-#ifdef CONFIG_PROC_FS
-	// Remove /proc/fs/ntfs3/..
-	if (sbi->procdir) {
-		remove_proc_entry("label", sbi->procdir);
-		remove_proc_entry("volinfo", sbi->procdir);
-		remove_proc_entry(sb->s_id, proc_info_root);
-		sbi->procdir = NULL;
-	}
-#endif
+	ntfs_remove_procdir(sb);
 
 	/* Mark rw ntfs as clear, if possible. */
 	ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
@@ -1585,20 +1611,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
 		kfree(boot2);
 	}
 
-#ifdef CONFIG_PROC_FS
-	/* Create /proc/fs/ntfs3/.. */
-	if (proc_info_root) {
-		struct proc_dir_entry *e = proc_mkdir(sb->s_id, proc_info_root);
-		static_assert((S_IRUGO | S_IWUSR) == 0644);
-		if (e) {
-			proc_create_data("volinfo", S_IRUGO, e,
-					 &ntfs3_volinfo_fops, sb);
-			proc_create_data("label", S_IRUGO | S_IWUSR, e,
-					 &ntfs3_label_fops, sb);
-			sbi->procdir = e;
-		}
-	}
-#endif
+	ntfs_create_procdir(sb);
 
 	if (is_legacy_ntfs(sb))
 		sb->s_flags |= SB_RDONLY;
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH 2/3 fs/ntfs3: factor out ntfs_{create/remove}_proc_root()

In reply to Ye Bin

From: Ye Bin <[email protected]>

Introduce ntfs_create_proc_root()/ntfs_remove_proc_root() for create/remove "/proc/fs/ntfs3".

---
 fs/ntfs3/super.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
fs/ntfs3/super.c +17 -9
index 660f11c6259c..cf1fcced7304 100644
@@ -581,9 +581,24 @@ static void ntfs_remove_procdir(struct super_block *sb)
 	remove_proc_entry(sb->s_id, proc_info_root);
 	sbi->procdir = NULL;
 }
+
+static void ntfs_create_proc_root(void)
+{
+	proc_info_root = proc_mkdir("fs/ntfs3", NULL);
+}
+
+static void ntfs_remove_proc_root(void)
+{
+	if (proc_info_root) {
+		remove_proc_entry("fs/ntfs3", NULL);
+		proc_info_root = NULL;
+	}
+}
 #else
 static void ntfs_create_procdir(struct super_block *sb) {}
 static void ntfs_remove_procdir(struct super_block *sb) {}
+static void ntfs_create_proc_root(void) {}
+static void ntfs_remove_proc_root(void) {}
 #endif
 
 static struct kmem_cache *ntfs_inode_cachep;
@@ -1861,10 +1876,7 @@ static int __init init_ntfs_fs(void)
 	if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
 		pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
 
-#ifdef CONFIG_PROC_FS
-	/* Create "/proc/fs/ntfs3" */
-	proc_info_root = proc_mkdir("fs/ntfs3", NULL);
-#endif
+	ntfs_create_proc_root();
 
 	err = ntfs3_init_bitmap();
 	if (err)
@@ -1898,11 +1910,7 @@ static void __exit exit_ntfs_fs(void)
 	unregister_filesystem(&ntfs_fs_type);
 	unregister_as_ntfs_legacy();
 	ntfs3_exit_bitmap();
-
-#ifdef CONFIG_PROC_FS
-	if (proc_info_root)
-		remove_proc_entry("fs/ntfs3", NULL);
-#endif
+	ntfs_remove_proc_root();
 }
 
 MODULE_LICENSE("GPL");
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH 3/3 fs/ntfs3: fix 'proc_info_root' leak when init ntfs failed

In reply to Ye Bin

From: Ye Bin <[email protected]>

There's a issue as follows:
proc_dir_entry 'fs/ntfs3' already registered

  WARNING: CPU: 3 PID: 9788 at fs/proc/generic.c:375 proc_register+0x418/0x590
  Modules linked in: ntfs3(E+)
  Call Trace:
   <TASK>
   _proc_mkdir+0x165/0x200
   init_ntfs_fs+0x36/0xf90 [ntfs3]
   do_one_initcall+0x115/0x6c0
   do_init_module+0x253/0x760
   load_module+0x55f2/0x6c80
   init_module_from_file+0xd2/0x130
   __x64_sys_finit_module+0xbf/0x130
   do_syscall_64+0x72/0x1c0

Above issue happens as missing destroy 'proc_info_root' when error happens after create 'proc_info_root' in init_ntfs_fs(). So destroy 'proc_info_root' in error path in init_ntfs_fs().

Fixes 7832e123490a ("fs/ntfs3: Add support /proc/fs/ntfs3/<dev>/volinfo and /proc/fs/ntfs3/<dev>/label")
---
 fs/ntfs3/super.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ntfs3/super.c +3 -1
index cf1fcced7304..bc43603c22af 100644
@@ -1880,7 +1880,7 @@ static int __init init_ntfs_fs(void)
 
 	err = ntfs3_init_bitmap();
 	if (err)
-		return err;
+		goto out2;
 
 	ntfs_inode_cachep = kmem_cache_create(
 		"ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
@@ -1900,6 +1900,8 @@ static int __init init_ntfs_fs(void)
 	kmem_cache_destroy(ntfs_inode_cachep);
 out1:
 	ntfs3_exit_bitmap();
+out2:
+	ntfs_remove_proc_root();
 	return err;
 }
 
Signature · 1 line
2.31.1