From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare()
Date: Wed, 11 Nov 2026 23:47:10 -0500 [thread overview]
Message-ID: <20261112044712.1577296-22-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>
ext4_getfsmap_dev_compare() returns the difference of two u32 encoded
device numbers as an int. new_encode_dev() places bits 8-19 of the
minor number in bits 20-31, so any device with a minor number of
0x80000 or higher encodes to a value with bit 31 set, and the
difference of such a value and a smaller one does not fit in an int.
The comparison then has the wrong sign.
With an external journal on a low-numbered device and the filesystem
on such a high-minor device, sort() orders the handlers as
[data device, log device]. ext4_getfsmap() relies on the handlers
being sorted in ascending device order: a query covering only the
log device hits "fmh_keys[1].fmr_device < handlers[i].gfd_dev" on
the data device first and breaks out of the loop, so no mappings are
returned for the journal device, and a full query reports the devices
in descending order, which breaks pagination with the last returned
record as the next low key.
Use cmp_int() so that the comparison cannot overflow.
Add a KUnit test with a high-minor data device and an external
journal device.
Fixes: 0c9ec4beecac ("ext4: support GETFSMAP ioctls")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
fs/ext4/fsmap-test.c | 56 ++++++++++++++++++++++++++++++++++++++++++++
fs/ext4/fsmap.c | 2 +-
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/fsmap-test.c b/fs/ext4/fsmap-test.c
index 63a1061b413a..ef7681cf9964 100644
--- a/fs/ext4/fsmap-test.c
+++ b/fs/ext4/fsmap-test.c
@@ -765,6 +765,61 @@ static void test_getfsmap_external_logdev_and_multi_device(struct kunit *test)
KUNIT_EXPECT_EQ(test, head.fmh_entries, 0U);
}
+/*
+ * A data device whose encoded dev_t has bit 31 set (minor >= 0x80000) must
+ * still sort after a lower-numbered external journal device, so that devices
+ * are reported in ascending order and log-device-only queries work.
+ */
+static void test_getfsmap_high_encoded_datadev_order(struct kunit *test)
+{
+ struct fsmap_test_ctx *ctx;
+ struct ext4_fsmap_head head;
+ struct fsmap_rec_sink *sink;
+ u32 datadev, logdev = new_encode_dev(FSMAP_TEST_LOGDEV);
+ int err;
+
+ sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, sink);
+ ctx = fsmap_create_ctx(test, 12, 0, 1, 128);
+ fsmap_set_group_tables(ctx, 0, 2, 3, 4);
+ fsmap_mark_clusters_used(ctx, 0, 0, 12);
+
+ ctx->data_bdev.bdev.bd_dev = MKDEV(259, 0x80000);
+ datadev = new_encode_dev(ctx->data_bdev.bdev.bd_dev);
+ KUNIT_ASSERT_GE(test, datadev, 0x80000000U);
+ KUNIT_ASSERT_LT(test, logdev, datadev);
+
+ ctx->mock_journal.j_blk_offset = 16;
+ ctx->mock_journal.j_total_len = 64;
+ ctx->sbi.s_journal = &ctx->mock_journal;
+ ctx->sbi.s_journal_bdev_file = &ctx->log_bdev_file;
+
+ /* Multi-device query reports logdev (2 recs) before datadev (6) */
+ fsmap_init_full_range_head(&head, FSMAP_TEST_MAX_RECS);
+ err = ext4_getfsmap(ctx->sb, &head, fsmap_collect_cb, sink);
+ KUNIT_ASSERT_EQ(test, err, 0);
+ KUNIT_ASSERT_EQ(test, head.fmh_entries, 8U);
+ fsmap_expect_rec(test, &sink->recs[0], logdev, 0, 16,
+ EXT4_FMR_OWN_UNKNOWN);
+ fsmap_expect_rec(test, &sink->recs[1], logdev, 16, 64,
+ EXT4_FMR_OWN_LOG);
+ KUNIT_EXPECT_EQ(test, (u32)sink->recs[2].fmr_device, datadev);
+ KUNIT_EXPECT_EQ(test, (u32)sink->recs[7].fmr_device, datadev);
+
+ /* Targeted query against only logdev */
+ memset(sink, 0, sizeof(*sink));
+ fsmap_init_full_range_head(&head, FSMAP_TEST_MAX_RECS);
+ head.fmh_keys[0].fmr_device = logdev;
+ head.fmh_keys[1].fmr_device = logdev;
+ err = ext4_getfsmap(ctx->sb, &head, fsmap_collect_cb, sink);
+ KUNIT_ASSERT_EQ(test, err, 0);
+ KUNIT_ASSERT_EQ(test, head.fmh_entries, 2U);
+ fsmap_expect_rec(test, &sink->recs[0], logdev, 0, 16,
+ EXT4_FMR_OWN_UNKNOWN);
+ fsmap_expect_rec(test, &sink->recs[1], logdev, 16, 64,
+ EXT4_FMR_OWN_LOG);
+}
+
static struct kunit_case fsmap_test_cases[] = {
KUNIT_CASE(test_fsmap_unit_conversions),
KUNIT_CASE(test_getfsmap_invalid_flags_devices_and_keys),
@@ -773,6 +828,7 @@ static struct kunit_case fsmap_test_cases[] = {
KUNIT_CASE(test_getfsmap_1k_bofs_bigalloc_and_meta_bg),
KUNIT_CASE(test_getfsmap_count_only_pagination_and_abort),
KUNIT_CASE(test_getfsmap_external_logdev_and_multi_device),
+ KUNIT_CASE(test_getfsmap_high_encoded_datadev_order),
{}
};
diff --git a/fs/ext4/fsmap.c b/fs/ext4/fsmap.c
index f41c2298d196..dd465b27f879 100644
--- a/fs/ext4/fsmap.c
+++ b/fs/ext4/fsmap.c
@@ -69,7 +69,7 @@ static int ext4_getfsmap_dev_compare(const void *p1, const void *p2)
const struct ext4_getfsmap_dev *d1 = p1;
const struct ext4_getfsmap_dev *d2 = p2;
- return d1->gfd_dev - d2->gfd_dev;
+ return cmp_int(d1->gfd_dev, d2->gfd_dev);
}
/* Compare a record against our starting point */
--
2.56.0.542.g3ef89b7020-goog
next prev parent reply other threads:[~2026-11-12 4:47 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12 4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12 4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12 4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12 4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12 4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12 4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12 4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12 4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12 4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12 4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12 4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12 4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12 4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12 4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12 4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12 4:47 ` Theodore Ts'o [this message]
2026-11-12 4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12 4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o
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=20261112044712.1577296-22-tytso@mit.edu \
--to=tytso@mit.edu \
--cc=linux-ext4@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox