Skip to contentSkip to conversation

fs/ntfs3: Refactoring of super.c

PATCH Superseded Konstantin Komarov 2 participants 7 messages Sep 28, 2021 – Sep 29, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Konstantin Komarov

author maintainer
#0 lore raw

v2 cover fs/ntfs3: Refactoring of super.c

Fix memory leak in ntfs_discard.
Reject mount so we won't corrupt fs.
Refactor ntfs_init_from_boot function.

v2:
Fixed wrong patch 1/3.
Merged two changes to the same piece of code.

Konstantin Komarov (3):
fs/ntfs3: Fix memory leak if fill_super failed fs/ntfs3: Reject mount if boot's cluster size < media sector size fs/ntfs3: Refactoring of ntfs_init_from_boot

 fs/ntfs3/ntfs_fs.h |  2 --
 fs/ntfs3/super.c   | 36 +++++++++++++++++++++++++-----------
 2 files changed, 25 insertions(+), 13 deletions(-)
Signature · 1 line
2.33.0
author maintainer
#1 lore raw

PATCH v2 1/3 fs/ntfs3: Fix memory leak if fill_super failed

Restore fc->s_fs_info to free memory allocated in ntfs_init_fs_context.

---
 fs/ntfs3/super.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ntfs3/super.c +4 -0
index 800897777eb0..aff90f70e7bf 100644
@@ -1242,6 +1242,10 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	return 0;
 out:
 	iput(inode);
+
+	/* Restore fc->s_fs_info to free memory allocated in ntfs_init_fs_context. */
+	fc->s_fs_info = sbi;
+
 	return err;
 }
 
Signature · 1 line
2.33.0
#2 lore raw

Re: [PATCH v2 1/3] fs/ntfs3: Fix memory leak if fill_super failed

Quoted 12 lines · Konstantin Komarov

Restore fc->s_fs_info to free memory allocated in ntfs_init_fs_context.

---
 fs/ntfs3/super.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ntfs3/super.c +0 -0
index 800897777eb0..aff90f70e7bf 100644

fs/ntfs3/super.c static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 1248

Quoted 7 lines
fs/ntfs3/super.c +4 -0
@@ -1242,6 +1242,10 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	return 0;
 out:
 	iput(inode);
+
+	/* Restore fc->s_fs_info to free memory allocated in ntfs_init_fs_context. */
+	fc->s_fs_info = sbi;
+

Nack. fc->s_fs_info is already pointing to sbi. We null this just before we exit so it is impossible to be anything else in failure case.

	fc->fs_private = NULL;
	fc->s_fs_info = NULL;

	return 0;
out:
	iput(inode);
Quoted 7 lines
fs/ntfs3/super.c +0 -0
 	return err;
 }
 
Signature · 1 line
2.33.0
author maintainer
#3 lore raw

Re: [PATCH v2 1/3] fs/ntfs3: Fix memory leak if fill_super failed

Quoted 8 lines · Kari Argillander
Quoted 7 lines · Konstantin Komarov

Restore fc->s_fs_info to free memory allocated in ntfs_init_fs_context.

---
 fs/ntfs3/super.c | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 16 lines

fs/ntfs3/super.c static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 1248

Quoted 12 lines
fs/ntfs3/super.c +4 -0
index 800897777eb0..aff90f70e7bf 100644
@@ -1242,6 +1242,10 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	return 0;
 out:
 	iput(inode);
+
+	/* Restore fc->s_fs_info to free memory allocated in ntfs_init_fs_context. */
+	fc->s_fs_info = sbi;
+

Nack. fc->s_fs_info is already pointing to sbi. We null this just before we exit so it is impossible to be anything else in failure case.

We have seen memory leak once, but looking at the code of function I can't point where it was caused. Will try to reproduce again. For now will commit only
"Reject mount if boot's cluster size < media sector size" and "Refactoring of ntfs_init_from_boot".

Quoted 14 lines
	fc->fs_private = NULL;
	fc->s_fs_info = NULL;

	return 0;
out:
	iput(inode);
Quoted 7 lines
fs/ntfs3/super.c +0 -0
 	return err;
 }
 
Signature · 1 line
2.33.0
author maintainer
#4 lore raw

PATCH v2 2/3 fs/ntfs3: Reject mount if boot's cluster size < media sector size

In reply to Konstantin Komarov

If we continue to work in this case, then we can corrupt fs.

---
 fs/ntfs3/super.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/ntfs3/super.c +12 -1
index aff90f70e7bf..890c5d9d6d60 100644
@@ -763,9 +763,20 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
 	sbi->mft.lbo = mlcn << sbi->cluster_bits;
 	sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
 
-	if (sbi->cluster_size < sbi->sector_size)
+	/* Compare boot's cluster and sector. */
+	if (sbi->cluster_size < boot_sector_size)
 		goto out;
 
+	/* Compare boot's cluster and media sector. */
+	if (sbi->cluster_size < sector_size) {
+		/* No way to use ntfs_get_block in this case. */
+		ntfs_err(
+			sb,
+			"Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)",
+			sbi->cluster_size, sector_size);
+		goto out;
+	}
+
 	sbi->cluster_mask = sbi->cluster_size - 1;
 	sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
 	sbi->record_size = record_size = boot->record_size < 0
Signature · 1 line
2.33.0
Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] fs/ntfs3: Reject mount if boot's cluster size < media sector size

Quoted 2 lines · Konstantin Komarov

If we continue to work in this case, then we can corrupt fs.

Remember to add fixes tag.

Quoted 10 lines
---
 fs/ntfs3/super.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/ntfs3/super.c +0 -0
index aff90f70e7bf..890c5d9d6d60 100644

fs/ntfs3/super.c static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size, · line 767

Quoted 6 lines
fs/ntfs3/super.c +2 -1
@@ -763,9 +763,20 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
 	sbi->mft.lbo = mlcn << sbi->cluster_bits;
 	sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits;
 
-	if (sbi->cluster_size < sbi->sector_size)
+	/* Compare boot's cluster and sector. */
+	if (sbi->cluster_size < boot_sector_size)

This should actually be in patch 3/3 as this has nothing to do with bug fix and bug fix is most important thing to be seperated and as you have do be front in the series. After that

Quoted 19 lines
fs/ntfs3/super.c +10 -0
 		goto out;
 
+	/* Compare boot's cluster and media sector. */
+	if (sbi->cluster_size < sector_size) {
+		/* No way to use ntfs_get_block in this case. */
+		ntfs_err(
+			sb,
+			"Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u)",
+			sbi->cluster_size, sector_size);
+		goto out;
+	}
+
 	sbi->cluster_mask = sbi->cluster_size - 1;
 	sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
 	sbi->record_size = record_size = boot->record_size < 0
Signature · 1 line
2.33.0
author maintainer
#6 lore raw

PATCH v2 3/3 fs/ntfs3: Refactoring of ntfs_init_from_boot

In reply to Konstantin Komarov

Remove ntfs_sb_info members sector_size and sector_bits. Print details why mount failed.

---
 fs/ntfs3/ntfs_fs.h |  2 --
 fs/ntfs3/super.c   | 19 +++++++++----------
 2 files changed, 9 insertions(+), 12 deletions(-)
fs/ntfs3/ntfs_fs.h +0 -2
index 6731b5d9e2d8..38b7c1a9dc52 100644
@@ -211,10 +211,8 @@ struct ntfs_sb_info {
 	u32 blocks_per_cluster; // cluster_size / sb->s_blocksize
 
 	u32 record_size;
-	u32 sector_size;
 	u32 index_size;
 
-	u8 sector_bits;
 	u8 cluster_bits;
 	u8 record_bits;
 
fs/ntfs3/super.c +9 -10
index 890c5d9d6d60..1c70871a3758 100644
@@ -682,7 +682,7 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
 	struct ntfs_sb_info *sbi = sb->s_fs_info;
 	int err;
 	u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
-	u64 sectors, clusters, fs_size, mlcn, mlcn2;
+	u64 sectors, clusters, mlcn, mlcn2;
 	struct NTFS_BOOT *boot;
 	struct buffer_head *bh;
 	struct MFT_REC *rec;
@@ -740,20 +740,20 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
 		goto out;
 	}
 
-	sbi->sector_size = boot_sector_size;
-	sbi->sector_bits = blksize_bits(boot_sector_size);
-	fs_size = (sectors + 1) << sbi->sector_bits;
+	sbi->volume.size = sectors * boot_sector_size;
 
-	gb = format_size_gb(fs_size, &mb);
+	gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
 
 	/*
 	 * - Volume formatted and mounted with the same sector size.
 	 * - Volume formatted 4K and mounted as 512.
 	 * - Volume formatted 512 and mounted as 4K.
 	 */
-	if (sbi->sector_size != sector_size) {
-		ntfs_warn(sb,
-			  "Different NTFS' sector size and media sector size");
+	if (boot_sector_size != sector_size) {
+		ntfs_warn(
+			sb,
+			"Different NTFS' sector size (%u) and media sector size (%u)",
+			boot_sector_size, sector_size);
 		dev_size += sector_size - 1;
 	}
 
@@ -800,10 +800,9 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
 				  : (u32)boot->index_size << sbi->cluster_bits;
 
 	sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
-	sbi->volume.size = sectors << sbi->sector_bits;
 
 	/* Warning if RAW volume. */
-	if (dev_size < fs_size) {
+	if (dev_size < sbi->volume.size + boot_sector_size) {
 		u32 mb0, gb0;
 
 		gb0 = format_size_gb(dev_size, &mb0);
Signature · 1 line
2.33.0