f2fs: avoid out-of-range memory access
PATCH v2 1/1 f2fs: avoid out-of-range memory access
blk_off might over 512 due to fs corrupt. Use ENTRIES_IN_SUM to protect invalid memory access.
v2:
- fix typo
--- fs/f2fs/segment.c | 2 ++ 1 file changed, 2 insertions(+)
fs/f2fs/segment.c +2 -0
@@ -3403,6 +3403,8 @@ static int read_compacted_summaries(struct f2fs_sb_info *sbi)for (j =0 ; j < blk_off; j++) {struct f2fs_summary *s; +if (j >=ENTRIES_IN_SUM ) +return -EFAULT ; s = (struct f2fs_summary *)(kaddr + offset); seg_i->sum_blk->entries[j] = *s; offset +=SUMMARY_SIZE ;
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
Hi Ocean,
If filesystem is corrupted, it should fail mount due to below check in f2fs_sanity_check_ckpt(), so we are safe in read_compacted_summaries() to access entries[0,blk_off], right?
for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
return 1;Thanks,
Quoted 24 lines · Ocean Chen
blk_offmight over 512 due to fs corrupt. UseENTRIES_IN_SUMto protect invalid memory access.v2:
- fix typoSigned-off-by Ocean Chen <[email protected]>--- fs/f2fs/segment.c | 2 ++ 1 file changed, 2 insertions(+)fs/f2fs/segment.c +2
-0@@ -3403,6 +3403,8 @@ static int read_compacted_summaries(struct f2fs_sb_info *sbi)for (j =0 ; j < blk_off; j++) {struct f2fs_summary *s; +if (j >=ENTRIES_IN_SUM ) +return -EFAULT ; s = (struct f2fs_summary *)(kaddr + offset); seg_i->sum_blk->entries[j] = *s; offset +=SUMMARY_SIZE ;
Hi Yu Chao,
The cur_data_segno only was checked in mount process. In terms of security concern, it's better to check value before using it. I know the risk is low. IMHO, it can be safer.
BTW, I found we can only check blk_off before for loop instead of checking 'j' in each iteratoin.
Quoted 38 lines · Chao Yu
Hi Ocean,
If filesystem is corrupted, it should fail mount due to below check in
f2fs_sanity_check_ckpt(), so we are safe inread_compacted_summaries()to access entries[0,blk_off], right?for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs || le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) return 1;Thanks,
Quoted 24 lines · Ocean Chen
blk_offmight over 512 due to fs corrupt. UseENTRIES_IN_SUMto protect invalid memory access.v2:
- fix typoSigned-off-by Ocean Chen <[email protected]>--- fs/f2fs/segment.c | 2 ++ 1 file changed, 2 insertions(+)fs/f2fs/segment.c +2
-0@@ -3403,6 +3403,8 @@ static int read_compacted_summaries(struct f2fs_sb_info *sbi)for (j =0 ; j < blk_off; j++) {struct f2fs_summary *s; +if (j >=ENTRIES_IN_SUM ) +return -EFAULT ; s = (struct f2fs_summary *)(kaddr + offset); seg_i->sum_blk->entries[j] = *s; offset +=SUMMARY_SIZE ;
Hi Ocean,
Quoted 4 lines · Ocean Chen
Hi Yu Chao,
The
cur_data_segnoonly was checked in mount process. In terms of security concern, it's better to check value before using it. I know the
Could you explain more about security concern.. Do you get any report from user or tools that complaining f2fs issue/codes?
I'm not against sanity check for basic core data of filesystem in run-time, but, in order to troubleshoot root cause of this issue we can trigger panic directly to dump more info under F2FS_CHECK_FS macro.
So, maybe we can change as below?
blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
+if (blk_off > ENTRIES_IN_SUM) {
+ f2fs_bug_on(1);
+ f2fs_put_page(page, 1);
+ return -EFAULT;
+}
Thanks,
Quoted 45 lines
risk is low. IMHO, it can be safer.
BTW, I found we can only checkblk_offbefore for loop instead of checking 'j' in each iteratoin.Quoted 30 lines · Chao Yu
Hi Ocean,
If filesystem is corrupted, it should fail mount due to below check in
f2fs_sanity_check_ckpt(), so we are safe inread_compacted_summaries()to access entries[0,blk_off], right?for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs || le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) return 1;Thanks,
Quoted 16 lines · Ocean Chen
blk_offmight over 512 due to fs corrupt. UseENTRIES_IN_SUMto protect invalid memory access.v2:
- fix typoSigned-off-by Ocean Chen <[email protected]>--- fs/f2fs/segment.c | 2 ++ 1 file changed, 2 insertions(+)fs/f2fs/segment.c +0
-0@@ -3403,6 +3403,8 @@ static int read_compacted_summaries(struct f2fs_sb_info *sbi)Quoted 8 lines
fs/f2fs/segment.c static int read_compacted_summaries(struct f2fs_sb_info *sbi) · line 3410
Quoted 8 lines
fs/f2fs/segment.c +2
-0for (j =0 ; j < blk_off; j++) {struct f2fs_summary *s; +if (j >=ENTRIES_IN_SUM ) +return -EFAULT ; s = (struct f2fs_summary *)(kaddr + offset); seg_i->sum_blk->entries[j] = *s; offset +=SUMMARY_SIZE ;.
Hi YuChao,
Yes, we got externel researcher reports this security vulnerability.
And dump info is better when blk_off is invalid. I'll prepare the next patch for it.
Quoted 71 lines · Chao Yu
Hi Ocean,
Quoted 4 lines · Ocean Chen
Hi Yu Chao,
The
cur_data_segnoonly was checked in mount process. In terms of security concern, it's better to check value before using it. I know theCould you explain more about security concern.. Do you get any report from user or tools that complaining f2fs issue/codes?
I'm not against sanity check for basic core data of filesystem in run-time, but, in order to troubleshoot root cause of this issue we can trigger panic directly to dump more info under
F2FS_CHECK_FSmacro.So, maybe we can change as below?
blk_off=le16_to_cpu(ckpt->cur_data_blkoff[i]);(quoted diff) +5
-0+if (blk_off > ENTRIES_IN_SUM) { + f2fs_bug_on(1); + f2fs_put_page(page, 1); + return -EFAULT; +}Thanks,
Quoted 45 lines
risk is low. IMHO, it can be safer.
BTW, I found we can only checkblk_offbefore for loop instead of checking 'j' in each iteratoin.Quoted 30 lines · Chao Yu
Hi Ocean,
If filesystem is corrupted, it should fail mount due to below check in
f2fs_sanity_check_ckpt(), so we are safe inread_compacted_summaries()to access entries[0,blk_off], right?for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs || le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) return 1;Thanks,
Quoted 16 lines · Ocean Chen
blk_offmight over 512 due to fs corrupt. UseENTRIES_IN_SUMto protect invalid memory access.v2:
- fix typoSigned-off-by Ocean Chen <[email protected]>--- fs/f2fs/segment.c | 2 ++ 1 file changed, 2 insertions(+)fs/f2fs/segment.c +0
-0@@ -3403,6 +3403,8 @@ static int read_compacted_summaries(struct f2fs_sb_info *sbi)Quoted 8 lines
fs/f2fs/segment.c static int read_compacted_summaries(struct f2fs_sb_info *sbi) · line 3410
Quoted 8 lines
fs/f2fs/segment.c +2
-0for (j =0 ; j < blk_off; j++) {struct f2fs_summary *s; +if (j >=ENTRIES_IN_SUM ) +return -EFAULT ; s = (struct f2fs_summary *)(kaddr + offset); seg_i->sum_blk->entries[j] = *s; offset +=SUMMARY_SIZE ;.