Skip to contentSkip to conversation

f2fs-tools: sanity check segno and blk_off when building curseg array

PATCH Superseded Jin Qian 2 participants 2 messages Apr 27, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jin Qian

author
#0 lore raw

PATCH 1/1 f2fs-tools: sanity check segno and blk_off when building curseg array

segno and blk_off were read from input image without sanity check. This could lead to buffer overflow when accessing internal arrays like SIT sentries and seg_entry cur_valid_map.

---
 fsck/mount.c | 3 +++
 1 file changed, 3 insertions(+)
fsck/mount.c +3 -0
index ffaa0ed..749c417 100644
@@ -1040,6 +1040,9 @@ static void build_curseg(struct f2fs_sb_info *sbi)
 			blk_off = get_cp(cur_node_blkoff[i - CURSEG_HOT_NODE]);
 			segno = get_cp(cur_node_segno[i - CURSEG_HOT_NODE]);
 		}
+		ASSERT(segno < TOTAL_SEGS(sbi));
+		ASSERT((blk_off >> 3) < SIT_VBLOCK_MAP_SIZE);
+
 		array[i].segno = segno;
 		array[i].zone = GET_ZONENO_FROM_SEGNO(sbi, segno);
 		array[i].next_segno = NULL_SEGNO;
Signature · 6 lines
2.13.0.rc0.306.g87b477812d-goog


------------------------------------------------------------------------------
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

Hi Jin,

Quoted 14 lines · Jin Qian

segno and blk_off were read from input image without sanity check. This could lead to buffer overflow when accessing internal arrays like SIT sentries and seg_entry cur_valid_map.

---
 fsck/mount.c | 3 +++
 1 file changed, 3 insertions(+)
fsck/mount.c +0 -0
index ffaa0ed..749c417 100644

fsck/mount.c static void build_curseg(struct f2fs_sb_info *sbi) · line 1044

Quoted 5 lines
fsck/mount.c +2 -0
@@ -1040,6 +1040,9 @@ static void build_curseg(struct f2fs_sb_info *sbi)
 			blk_off = get_cp(cur_node_blkoff[i - CURSEG_HOT_NODE]);
 			segno = get_cp(cur_node_segno[i - CURSEG_HOT_NODE]);
 		}
+		ASSERT(segno < TOTAL_SEGS(sbi));
+		ASSERT((blk_off >> 3) < SIT_VBLOCK_MAP_SIZE);
		ASSERT(blk_off < DEFAULT_BLOCKS_PER_SEGMENT);

Otherwise, blk_off can be cut by shift operation.

Thanks,

Quoted 6 lines
fsck/mount.c +1 -0
+
 		array[i].segno = segno;
 		array[i].zone = GET_ZONENO_FROM_SEGNO(sbi, segno);
 		array[i].next_segno = NULL_SEGNO;
Signature · 1 line
2.13.0.rc0.306.g87b477812d-goog

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