Skip to contentSkip to conversation

fs: f2fs: fix potential shift-out-of-bounds error in sanity_check_raw_super()

PATCH In review Anant Thazhemadam 3 participants 7 messages Dec 9, 2020 – Dec 10, 2020 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: f2fs: fix potential shift-out-of-bounds error in sanity_check_raw_super()

In sanity_check_raw_super(), if
1 << le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE, then the block size is deemed to be invalid.

syzbot triggered a shift-out-of-bounds bug by assigning a value of 59 to le32_to_cpu(raw_super->log_blocksize).
Although the value assigned itself isn't of much significance, this goes to show that even if the block size is invalid, le32_to_cpu(raw_super->log_blocksize) can be potentially evaluated to a value for which the shift exponent becomes too large for the unsigned int.

Since 1 << le32_to_cpu(raw_super->log_blocksize) must be = 4096 for a valid block size, le32_to_cpu(raw_super->log_blocksize) must equal 12. Replacing the existing check with the more direct sanity check resolves this bug.

---
 fs/f2fs/super.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/f2fs/super.c +2 -5
index 33808c397580..4bc7372af43f 100644
@@ -2775,7 +2775,6 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
 	block_t total_sections, blocks_per_seg;
 	struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
 					(bh->b_data + F2FS_SUPER_OFFSET);
-	unsigned int blocksize;
 	size_t crc_offset = 0;
 	__u32 crc = 0;
 
@@ -2802,10 +2801,8 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
 	}
 
 	/* Currently, support only 4KB block size */
-	blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
-	if (blocksize != F2FS_BLKSIZE) {
-		f2fs_info(sbi, "Invalid blocksize (%u), supports only 4KB",
-			  blocksize);
+	if (le32_to_cpu(raw_super->log_blocksize) != 12) {
+		f2fs_info(sbi, "Invalid blocksize. Only 4KB supported");
 		return -EFSCORRUPTED;
 	}
 
Signature · 8 lines
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

Hi Anant,

I've posted a patch a little earlier. :P

https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#u

Thanks,

Quoted 40 lines · Anant Thazhemadam

In sanity_check_raw_super(), if
1 << le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE, then the block size is deemed to be invalid.

syzbot triggered a shift-out-of-bounds bug by assigning a value of 59 to le32_to_cpu(raw_super->log_blocksize).
Although the value assigned itself isn't of much significance, this goes to show that even if the block size is invalid, le32_to_cpu(raw_super->log_blocksize) can be potentially evaluated to a value for which the shift exponent becomes too large for the unsigned int.

Since 1 << le32_to_cpu(raw_super->log_blocksize) must be = 4096 for a valid block size, le32_to_cpu(raw_super->log_blocksize) must equal 12. Replacing the existing check with the more direct sanity check resolves this bug.

---
  fs/f2fs/super.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
fs/f2fs/super.c +0 -1
index 33808c397580..4bc7372af43f 100644
@@ -2775,7 +2775,6 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  	block_t total_sections, blocks_per_seg;
  	struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  					(bh->b_data + F2FS_SUPER_OFFSET);
-	unsigned int blocksize;
  	size_t crc_offset = 0;
  	__u32 crc = 0;
 
@@ -2802,10 +2801,8 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  	}
 

fs/f2fs/super.c static int sanity_check_raw_super(struct f2fs_sb_info *sbi, · line 2808

Quoted 10 lines
fs/f2fs/super.c +2 -4
  	/* Currently, support only 4KB block size */
-	blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
-	if (blocksize != F2FS_BLKSIZE) {
-		f2fs_info(sbi, "Invalid blocksize (%u), supports only 4KB",
-			  blocksize);
+	if (le32_to_cpu(raw_super->log_blocksize) != 12) {
+		f2fs_info(sbi, "Invalid blocksize. Only 4KB supported");
  		return -EFSCORRUPTED;
  	}
 

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw
Quoted 10 lines · Anant Thazhemadam
Quoted 6 lines · Chao Yu

Hi Anant,

I've posted a patch a little earlier. :P

https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#u

Ah well, that's alright, especially considering that your patch looks better. Glad that bug has been fixed nonetheless. :)

Anyway, thanks a lot for taking time to fix f2fs bug. :)

I prefer to add your Signed-off-by into "f2fs: fix shift-out-of-bounds in sanity_check_raw_super()" if you don't mind.

Thanks,

Quoted 5 lines

Cheers,
Anant
.

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#4 lore raw
Quoted 9 lines · Chao Yu
Quoted 8 lines · Anant Thazhemadam
Quoted 6 lines · Chao Yu

Hi Anant,

I've posted a patch a little earlier. :P

https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#u

Quoted 9 lines
Quoted 2 lines

Ah well, that's alright, especially considering that your patch looks better. Glad that bug has been fixed nonetheless. :)

Anyway, thanks a lot for taking time to fix f2fs bug. :)

I prefer to add your Signed-off-by into "f2fs: fix shift-out-of-bounds in sanity_check_raw_super()" if you don't mind.

Sure, I'd appreciate that. Thank you! :D

Thanks,
Anant

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#5 lore raw

Jaegeuk,

Could you please help to add signed-off of Anant manually in

f2fs: fix shift-out-of-bounds in sanity_check_raw_super()

Thanks,

Quoted 21 lines · Anant Thazhemadam
Quoted 9 lines · Chao Yu
Quoted 8 lines · Anant Thazhemadam
Quoted 6 lines · Chao Yu

Hi Anant,

I've posted a patch a little earlier. :P

https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#u

Quoted 9 lines
Quoted 2 lines

Ah well, that's alright, especially considering that your patch looks better. Glad that bug has been fixed nonetheless. :)

Anyway, thanks a lot for taking time to fix f2fs bug. :)

I prefer to add your Signed-off-by into "f2fs: fix shift-out-of-bounds in sanity_check_raw_super()" if you don't mind.

Quoted 7 lines

Sure, I'd appreciate that. Thank you! :D

Thanks,
Anant

.

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

maintainer
#6 lore raw
Quoted 5 lines · Chao Yu

Jaegeuk,

Could you please help to add signed-off of Anant manually in

f2fs: fix shift-out-of-bounds in sanity_check_raw_super()

Done. Thank you guys. :)

Quoted 32 lines

Thanks,

Quoted 28 lines · Anant Thazhemadam
Quoted 9 lines · Chao Yu
Quoted 8 lines · Anant Thazhemadam
Quoted 6 lines · Chao Yu

Hi Anant,

I've posted a patch a little earlier. :P

https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#u

Quoted 9 lines
Quoted 2 lines

Ah well, that's alright, especially considering that your patch looks better. Glad that bug has been fixed nonetheless. :)

Anyway, thanks a lot for taking time to fix f2fs bug. :)

I prefer to add your Signed-off-by into "f2fs: fix shift-out-of-bounds in sanity_check_raw_super()" if you don't mind.

Sure, I'd appreciate that. Thank you! :D

Thanks,
Anant

.

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel