summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2024-05-25 12:38:53 -0400
committerKent Overstreet <kent.overstreet@linux.dev>2024-06-29 16:55:51 -0400
commit8e4dc66768a38acbadc3d84fb070d1220d0fbebd (patch)
tree595581a24058e996dfb39e3dce729545896113fa
parentfc21785d6383b87a48601eab8c1df26a5572ca43 (diff)
bcachefs: Fix sb-downgrade validation
Superblock downgrade entries are only two byte aligned, but section sizes are 8 byte aligned, which means we have to be careful about overrun checks; an entry that crosses the end of the section is allowed (and ignored) as long as it has zero errors. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> (cherry picked from commit 9242a34b760648b722f4958749ad83ef7d0f7525)
-rw-r--r--fs/bcachefs/sb-downgrade.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/fs/bcachefs/sb-downgrade.c b/fs/bcachefs/sb-downgrade.c
index 13fb3292aba4..b01d4969fe02 100644
--- a/fs/bcachefs/sb-downgrade.c
+++ b/fs/bcachefs/sb-downgrade.c
@@ -146,6 +146,14 @@ static int bch2_sb_downgrade_validate(struct bch_sb *sb, struct bch_sb_field *f,
for (const struct bch_sb_field_downgrade_entry *i = e->entries;
(void *) i < vstruct_end(&e->field);
i = downgrade_entry_next_c(i)) {
+ /*
+ * Careful: sb_field_downgrade_entry is only 2 byte aligned, but
+ * section sizes are 8 byte aligned - an empty entry spanning
+ * the end of the section is allowed (and ignored):
+ */
+ if ((void *) &i->errors[0] > vstruct_end(&e->field))
+ break;
+
if (BCH_VERSION_MAJOR(le16_to_cpu(i->version)) !=
BCH_VERSION_MAJOR(le16_to_cpu(sb->version))) {
prt_printf(err, "downgrade entry with mismatched major version (%u != %u)",