From: Daeho Jeong <daeho43@gmail.com>
To: linux-kernel@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com
Cc: Daeho Jeong <daehojeong@google.com>
Subject: [f2fs-dev] [PATCH v3] fsck.f2fs: sanitize invalid segment type during block update
Date: Tue, 4 Aug 2026 10:18:03 -0700 [thread overview]
Message-ID: <20260804171804.28860-1-daeho43@gmail.com> (raw)
From: Daeho Jeong <daehojeong@google.com>
If an invalid segment type (type < 0 || type >= NO_CHECK_TYPE) is read
from SIT during block update, update_block() now validates the type,
reports corruption via ASSERT_MSG(), fixes se->type directly, and falls
back to a valid active curseg type based on its SSA summary block type
when fix_on is enabled. find_next_free_block() also validates want_type to
prevent out-of-bounds accesses.
Signed-off-by: Daeho Jeong <daehojeong@google.com>
---
v3: do not update se->orig_type.
v2: used ASSERT_MSG() instead of ERR_MSG().
used the correct type info for fixed_type.
fixed se->type on top of fixing the local type only.
---
fsck/mount.c | 5 +++++
fsck/segment.c | 20 ++++++++++++++++++--
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/fsck/mount.c b/fsck/mount.c
index 6f640a0..1804403 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -3059,6 +3059,11 @@ int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left,
u64 end_blkaddr = (get_sb(segment_count_main) <<
get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
+ if (want_type < 0 || want_type >= NO_CHECK_TYPE) {
+ ASSERT_MSG("Invalid segment type %d for block allocation", want_type);
+ return -EINVAL;
+ }
+
if (c.zoned_model == F2FS_ZONED_HM && !new_sec) {
struct curseg_info *curseg = CURSEG_I(sbi, want_type);
unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
diff --git a/fsck/segment.c b/fsck/segment.c
index 96de22a..9f3fa0c 100644
--- a/fsck/segment.c
+++ b/fsck/segment.c
@@ -770,15 +770,32 @@ int update_block(struct f2fs_sb_info *sbi, void *buf, u32 *blkaddr,
struct seg_entry *se;
struct f2fs_summary sum;
u64 new_blkaddr, old_blkaddr = *blkaddr, offset;
- int ret, type;
+ int ret, type, sum_type;
if (c.zoned_model != F2FS_ZONED_HM)
return dev_write_block(buf, old_blkaddr, WRITE_LIFE_NONE);
+ sum_type = get_sum_entry(sbi, old_blkaddr, &sum);
+
/* update sit bitmap & valid_blocks && se->type for old block*/
se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
offset = OFFSET_IN_SEG(sbi, old_blkaddr);
type = se->type;
+ if (type < 0 || type >= NO_CHECK_TYPE) {
+ int fixed_type = (sum_type == SEG_TYPE_NODE ||
+ sum_type == SEG_TYPE_CUR_NODE) ?
+ CURSEG_HOT_NODE : CURSEG_HOT_DATA;
+
+ ASSERT_MSG("Invalid SIT segment type %d at blkaddr 0x%"PRIx64,
+ type, old_blkaddr);
+
+ if (!c.fix_on)
+ return -EINVAL;
+
+ FIX_MSG("Correct invalid segment type %d -> %d for blkaddr 0x%"PRIx64"\n",
+ type, fixed_type, old_blkaddr);
+ se->type = type = fixed_type;
+ }
se->valid_blocks--;
f2fs_clear_bit(offset, (char *)se->cur_valid_map);
if (need_fsync_data_record(sbi))
@@ -811,7 +828,6 @@ int update_block(struct f2fs_sb_info *sbi, void *buf, u32 *blkaddr,
f2fs_set_sit_bitmap(sbi, new_blkaddr);
/* update SSA */
- get_sum_entry(sbi, old_blkaddr, &sum);
update_sum_entry(sbi, new_blkaddr, &sum);
if (IS_DATASEG(type)) {
--
2.55.0.571.g244d577d93-goog
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
WARNING: multiple messages have this Message-ID (diff)
From: Daeho Jeong <daeho43@gmail.com>
To: linux-kernel@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com
Cc: Daeho Jeong <daehojeong@google.com>
Subject: [PATCH v3] fsck.f2fs: sanitize invalid segment type during block update
Date: Tue, 4 Aug 2026 10:18:03 -0700 [thread overview]
Message-ID: <20260804171804.28860-1-daeho43@gmail.com> (raw)
From: Daeho Jeong <daehojeong@google.com>
If an invalid segment type (type < 0 || type >= NO_CHECK_TYPE) is read
from SIT during block update, update_block() now validates the type,
reports corruption via ASSERT_MSG(), fixes se->type directly, and falls
back to a valid active curseg type based on its SSA summary block type
when fix_on is enabled. find_next_free_block() also validates want_type to
prevent out-of-bounds accesses.
Signed-off-by: Daeho Jeong <daehojeong@google.com>
---
v3: do not update se->orig_type.
v2: used ASSERT_MSG() instead of ERR_MSG().
used the correct type info for fixed_type.
fixed se->type on top of fixing the local type only.
---
fsck/mount.c | 5 +++++
fsck/segment.c | 20 ++++++++++++++++++--
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/fsck/mount.c b/fsck/mount.c
index 6f640a0..1804403 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -3059,6 +3059,11 @@ int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left,
u64 end_blkaddr = (get_sb(segment_count_main) <<
get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
+ if (want_type < 0 || want_type >= NO_CHECK_TYPE) {
+ ASSERT_MSG("Invalid segment type %d for block allocation", want_type);
+ return -EINVAL;
+ }
+
if (c.zoned_model == F2FS_ZONED_HM && !new_sec) {
struct curseg_info *curseg = CURSEG_I(sbi, want_type);
unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
diff --git a/fsck/segment.c b/fsck/segment.c
index 96de22a..9f3fa0c 100644
--- a/fsck/segment.c
+++ b/fsck/segment.c
@@ -770,15 +770,32 @@ int update_block(struct f2fs_sb_info *sbi, void *buf, u32 *blkaddr,
struct seg_entry *se;
struct f2fs_summary sum;
u64 new_blkaddr, old_blkaddr = *blkaddr, offset;
- int ret, type;
+ int ret, type, sum_type;
if (c.zoned_model != F2FS_ZONED_HM)
return dev_write_block(buf, old_blkaddr, WRITE_LIFE_NONE);
+ sum_type = get_sum_entry(sbi, old_blkaddr, &sum);
+
/* update sit bitmap & valid_blocks && se->type for old block*/
se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
offset = OFFSET_IN_SEG(sbi, old_blkaddr);
type = se->type;
+ if (type < 0 || type >= NO_CHECK_TYPE) {
+ int fixed_type = (sum_type == SEG_TYPE_NODE ||
+ sum_type == SEG_TYPE_CUR_NODE) ?
+ CURSEG_HOT_NODE : CURSEG_HOT_DATA;
+
+ ASSERT_MSG("Invalid SIT segment type %d at blkaddr 0x%"PRIx64,
+ type, old_blkaddr);
+
+ if (!c.fix_on)
+ return -EINVAL;
+
+ FIX_MSG("Correct invalid segment type %d -> %d for blkaddr 0x%"PRIx64"\n",
+ type, fixed_type, old_blkaddr);
+ se->type = type = fixed_type;
+ }
se->valid_blocks--;
f2fs_clear_bit(offset, (char *)se->cur_valid_map);
if (need_fsync_data_record(sbi))
@@ -811,7 +828,6 @@ int update_block(struct f2fs_sb_info *sbi, void *buf, u32 *blkaddr,
f2fs_set_sit_bitmap(sbi, new_blkaddr);
/* update SSA */
- get_sum_entry(sbi, old_blkaddr, &sum);
update_sum_entry(sbi, new_blkaddr, &sum);
if (IS_DATASEG(type)) {
--
2.55.0.571.g244d577d93-goog
next reply other threads:[~2026-08-04 17:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-04 17:18 Daeho Jeong [this message]
2026-08-04 17:18 ` [PATCH v3] fsck.f2fs: sanitize invalid segment type during block update Daeho Jeong
2026-08-05 1:21 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-08-05 1:21 ` Chao Yu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260804171804.28860-1-daeho43@gmail.com \
--to=daeho43@gmail.com \
--cc=daehojeong@google.com \
--cc=kernel-team@android.com \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.