Skip to contentSkip to conversation

f2fs: avoid out-of-range memory access

PATCH Superseded Ocean Chen 2 participants 5 messages Jul 2, 2019 – Jul 8, 2019 F2FS file system

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ocean Chen

author
#0 lore raw

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
index 8dee063c833f..a5e8af0bd62e 100644
@@ -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
#1 lore raw

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_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
index 8dee063c833f..a5e8af0bd62e 100644
@@ -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;
author
#2 lore raw

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 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_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
index 8dee063c833f..a5e8af0bd62e 100644
@@ -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;
#3 lore raw

Hi Ocean,

Quoted 4 lines · Ocean Chen

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

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 check blk_off before 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 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 16 lines · Ocean Chen

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 +0 -0
index 8dee063c833f..a5e8af0bd62e 100644
@@ -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 -0
 		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;

.

author
#4 lore raw

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_segno only 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]);

(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 check blk_off before 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 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 16 lines · Ocean Chen

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 +0 -0
index 8dee063c833f..a5e8af0bd62e 100644
@@ -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 -0
 		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;

.