Skip to contentSkip to conversation

f2fs: fix potential corruption in area before F2FS_SUPER_OFFSET

PATCH Superseded Sheng Yong 2 participants 7 messages Jan 29, 2018 F2FS file system

lore
Share & tools
lore

Sheng Yong

author
#0 lore raw

PATCH 1/1 f2fs: fix potential corruption in area before F2FS_SUPER_OFFSET

sb_getblk does not guarantee the buffer head is uptodate. If bh is not uptodate, the data (may be used as boot code) in area before F2FS_SUPER_OFFSET may get corrupted when super block is committed.

---
 fs/f2fs/super.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/super.c +2 -2
index 8173ae688814..7def48cb2b22 100644
@@ -2334,7 +2334,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 	}
 
 	/* write back-up superblock first */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
 	if (!bh)
 		return -EIO;
 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
@@ -2345,7 +2345,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 		return err;
 
 	/* write current valid superblock */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block);
 	if (!bh)
 		return -EIO;
 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Signature · 6 lines
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
#1 lore raw
Quoted 2 lines · Sheng Yong

sb_getblk does not guarantee the buffer head is uptodate. If bh is not uptodate, the data (may be used as boot code) in area before

Why boot code can be stored into the position f2fs superblock locates?

And even we have updated the buffer, we will still commit superblock's datas into that buffer, boot code can be messed up anyway, right?

Thanks,

Quoted 22 lines

F2FS_SUPER_OFFSET may get corrupted when super block is committed.

---
 fs/f2fs/super.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/super.c +1 -1
index 8173ae688814..7def48cb2b22 100644
@@ -2334,7 +2334,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 	}
 
 	/* write back-up superblock first */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
 	if (!bh)
 		return -EIO;
 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));

fs/f2fs/super.c int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) · line 2351

Quoted 8 lines
fs/f2fs/super.c +1 -1
@@ -2345,7 +2345,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 		return err;
 
 	/* write current valid superblock */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block);
 	if (!bh)
 		return -EIO;
 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

author
#2 lore raw

Hi, Chao

Quoted 5 lines · Chao Yu
Quoted 2 lines · Sheng Yong

sb_getblk does not guarantee the buffer head is uptodate. If bh is not uptodate, the data (may be used as boot code) in area before

Why boot code can be stored into the position f2fs superblock locates?

According to Ext4_Disk_Layout [1], the first 1024 bytes is used to install boot sectors (may be used by some legacy bootloader, I'm not quite sure :(

Quoted 3 lines

And even we have updated the buffer, we will still commit superblock's datas into that buffer, boot code can be messed up anyway, right?

The first 1024 bytes are all set to 0 by mkfs, while __f2fs_commit_super only updates bh->b_data from F2FS_SUPER_OFFSET, but not the whole 4KB area. If the buffer head is not read from super block (4KB), there is a chance it may get messed up.

Thanks,
Sheng

Quoted 1 line

[1] https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout

Quoted 25 lines

Thanks,

Quoted 23 lines

F2FS_SUPER_OFFSET may get corrupted when super block is committed.

---
  fs/f2fs/super.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/super.c +1 -1
index 8173ae688814..7def48cb2b22 100644
@@ -2334,7 +2334,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  	}
 
  	/* write back-up superblock first */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
  	if (!bh)
  		return -EIO;
  	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
@@ -2345,7 +2345,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  		return err;
 
Quoted 11 lines

fs/f2fs/super.c int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) · line 2351

Quoted 7 lines
fs/f2fs/super.c +1 -1
  	/* write current valid superblock */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block);
  	if (!bh)
  		return -EIO;
  	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));

.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

Reviewed-by
#3 lore raw

Hi Sheng Yong,

Quoted 10 lines · Sheng Yong

Hi, Chao

Quoted 5 lines · Chao Yu
Quoted 2 lines · Sheng Yong

sb_getblk does not guarantee the buffer head is uptodate. If bh is not uptodate, the data (may be used as boot code) in area before

Why boot code can be stored into the position f2fs superblock locates?

According to Ext4_Disk_Layout [1], the first 1024 bytes is used to install boot sectors (may be used by some legacy bootloader, I'm not quite sure :(

OK, I can understand your concern now, anyway, no matter the area is used by someone else or not, let's keep data in that area as it is as possible as we can.

Thanks,

Quoted 51 lines
Quoted 3 lines

And even we have updated the buffer, we will still commit superblock's datas into that buffer, boot code can be messed up anyway, right?

The first 1024 bytes are all set to 0 by mkfs, while __f2fs_commit_super only updates bh->b_data from F2FS_SUPER_OFFSET, but not the whole 4KB area. If the buffer head is not read from super block (4KB), there is a chance it may get messed up.

Thanks,
Sheng

Quoted 1 line

[1] https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout

Quoted 25 lines

Thanks,

Quoted 23 lines

F2FS_SUPER_OFFSET may get corrupted when super block is committed.

---
  fs/f2fs/super.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/super.c +1 -1
index 8173ae688814..7def48cb2b22 100644
@@ -2334,7 +2334,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  	}
 
  	/* write back-up superblock first */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
  	if (!bh)
  		return -EIO;
  	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
@@ -2345,7 +2345,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  		return err;
 
Quoted 11 lines

fs/f2fs/super.c int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) · line 2351

Quoted 7 lines
fs/f2fs/super.c +1 -1
  	/* write current valid superblock */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block);
  	if (!bh)
  		return -EIO;
  	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));

.

Quoted 2 lines

.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

author
#4 lore raw
Quoted 10 lines · Chao Yu

Hi Sheng Yong,

Quoted 7 lines · Sheng Yong

Hi, Chao

Quoted 4 lines · Chao Yu
Quoted 2 lines · Sheng Yong

sb_getblk does not guarantee the buffer head is uptodate. If bh is not uptodate, the data (may be used as boot code) in area before

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Why boot code can be stored into the position f2fs superblock locates?

According to Ext4_Disk_Layout [1], the first 1024 bytes is used to install boot sectors (may be used by some legacy bootloader, I'm not quite sure :(

OK, I can understand your concern now, anyway, no matter the area is used by someone else or not, let's keep data in that area as it is as possible as we can.

I checked the change again, I think we should also remove the extra set_buffer_uptodate(bh); in __f2fs_commit_super like the following,

fs/f2fs/super.c +0 -1
index 8173ae688814..07d72d651c45 100644
@@ -1894,7 +1894,6 @@ static int __f2fs_commit_super(struct buffer_head *bh,
 	lock_buffer(bh);
 	if (super)
 		memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
-	set_buffer_uptodate(bh);
 	set_buffer_dirty(bh);
 	unlock_buffer(bh);
 

I will resend it, thanks.

Quoted 56 lines

Thanks,

Quoted 51 lines
Quoted 3 lines

And even we have updated the buffer, we will still commit superblock's datas into that buffer, boot code can be messed up anyway, right?

The first 1024 bytes are all set to 0 by mkfs, while __f2fs_commit_super only updates bh->b_data from F2FS_SUPER_OFFSET, but not the whole 4KB area. If the buffer head is not read from super block (4KB), there is a chance it may get messed up.

Thanks,
Sheng

Quoted 1 line

[1] https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout

Quoted 36 lines

Thanks,

Quoted 22 lines

F2FS_SUPER_OFFSET may get corrupted when super block is committed.

---
   fs/f2fs/super.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/super.c +1 -1
index 8173ae688814..7def48cb2b22 100644
@@ -2334,7 +2334,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
   	}
 
   	/* write back-up superblock first */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
   	if (!bh)
   		return -EIO;
   	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));

fs/f2fs/super.c int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) · line 2351

Quoted 8 lines
fs/f2fs/super.c +1 -1
@@ -2345,7 +2345,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
   		return err;
 
   	/* write current valid superblock */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block);
   	if (!bh)
   		return -EIO;
   	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));

.

Quoted 6 lines
Quoted 2 lines

.

.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

author
#5 lore raw

PATCH v2 1/1 f2fs: fix potential corruption in area before F2FS_SUPER_OFFSET

In reply to Sheng Yong

sb_getblk does not guarantee the buffer head is uptodate. If bh is not uptodate, the data (may be used as boot code) in area before F2FS_SUPER_OFFSET may get corrupted when super block is committed.

---
 fs/f2fs/super.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/f2fs/super.c +2 -3
index 8173ae688814..3db1f02ed8c2 100644
@@ -1894,7 +1894,6 @@ static int __f2fs_commit_super(struct buffer_head *bh,
 	lock_buffer(bh);
 	if (super)
 		memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
-	set_buffer_uptodate(bh);
 	set_buffer_dirty(bh);
 	unlock_buffer(bh);
 
@@ -2334,7 +2333,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 	}
 
 	/* write back-up superblock first */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
 	if (!bh)
 		return -EIO;
 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
@@ -2345,7 +2344,7 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 		return err;
 
 	/* write current valid superblock */
-	bh = sb_getblk(sbi->sb, sbi->valid_super_block);
+	bh = sb_bread(sbi->sb, sbi->valid_super_block);
 	if (!bh)
 		return -EIO;
 	err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Signature · 6 lines
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Reviewed-by on v2 1/1
#6 lore raw
Quoted 5 lines · Sheng Yong

sb_getblk does not guarantee the buffer head is uptodate. If bh is not uptodate, the data (may be used as boot code) in area before F2FS_SUPER_OFFSET may get corrupted when super block is committed.

Thanks,

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot