mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] dm zoned: fix the validation of the superblocks
@ 2026-12-01 10:56 Yingjie Wang
  2026-12-01 10:56 ` [PATCH 1/3] dm zoned: do not update metadata state from an invalid superblock Yingjie Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 10:56 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Damien Le Moal, Hannes Reinecke, Yingjie Wang

Patch 1 fixes dmz_check_sb() changing the metadata version, UUIDs and
label before it knows that the superblock is valid.  An invalid
secondary superblock can thereby make the volume unusable.

Patch 2 adds the missing checks of the metadata layout.  Without them, a
superblock with inconsistent sizes can result in an out-of-bounds write
when the table is loaded, and in metadata being written over data.

Patch 3 fixes a crash if the label is not NUL-terminated.

Tested with KASAN on null_blk devices, including fault injection, a
32-bit kernel, and volumes formatted with all dmzadm releases.  All of
these problems were reproduced without the series and are gone with it.

Yingjie Wang (3):
  dm zoned: do not update metadata state from an invalid superblock
  dm zoned: validate the metadata layout in the superblock
  dm zoned: handle labels that are not NUL-terminated

 drivers/md/dm-zoned-metadata.c | 64 ++++++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 19 deletions(-)


base-commit: 2ee78e96ca1894cbfe00eff1e4ff24a93215337f
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] dm zoned: do not update metadata state from an invalid superblock
  2026-12-01 10:56 [PATCH 0/3] dm zoned: fix the validation of the superblocks Yingjie Wang
@ 2026-12-01 10:56 ` Yingjie Wang
  2026-12-01 10:56 ` [PATCH 2/3] dm zoned: validate the metadata layout in the superblock Yingjie Wang
  2026-12-01 10:56 ` [PATCH 3/3] dm zoned: handle labels that are not NUL-terminated Yingjie Wang
  2 siblings, 0 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 10:56 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Damien Le Moal, Hannes Reinecke,
	Yingjie Wang, stable

dmz_check_sb() stores the version, UUIDs and label of a superblock
before it has finished validating it.  If the superblock is then
rejected, zmd and the device are left with the values it contained.

If the secondary superblock is invalid, it is recovered from the primary
one using these values, and the next metadata flush writes them to the
primary superblock, too.  For example:

 - If the secondary superblock has a bad checksum and a version of 1,
   both superblocks become version 1, and the tertiary superblocks are
   not checked anymore.

 - If it has no device UUID, the device UUID is cleared in both
   superblocks.  Both are invalid then, and the table cannot be loaded
   anymore.

Conversely, if the primary superblock is rejected after its UUID and
label were stored, a valid secondary superblock is rejected, too, if
these are different, and the volume is not recovered.

Use local variables during validation and only update zmd and the
device once the superblock is known to be valid.

Fixes: bd5c40313a14 ("dm zoned: metadata version 2")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 drivers/md/dm-zoned-metadata.c | 38 ++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/drivers/md/dm-zoned-metadata.c b/drivers/md/dm-zoned-metadata.c
index 687573bbc091..a6b8372010d8 100644
--- a/drivers/md/dm-zoned-metadata.c
+++ b/drivers/md/dm-zoned-metadata.c
@@ -975,7 +975,7 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 	struct dmz_super *sb = dsb->sb;
 	struct dmz_dev *dev = dsb->dev;
 	unsigned int nr_meta_zones, nr_data_zones, i;
-	u32 crc, stored_crc;
+	u32 crc, stored_crc, version;
 	u64 gen, sb_block;
 
 	if (le32_to_cpu(sb->magic) != DMZ_MAGIC) {
@@ -984,13 +984,13 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 		return -ENXIO;
 	}
 
-	zmd->sb_version = le32_to_cpu(sb->version);
-	if (zmd->sb_version > DMZ_META_VER) {
+	version = le32_to_cpu(sb->version);
+	if (version > DMZ_META_VER) {
 		dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)",
-			    DMZ_META_VER, zmd->sb_version);
+			    DMZ_META_VER, version);
 		return -EINVAL;
 	}
-	if (zmd->sb_version < 2 && tertiary) {
+	if (version < 2 && tertiary) {
 		dmz_dev_err(dev, "Tertiary superblocks are not supported");
 		return -EINVAL;
 	}
@@ -1011,29 +1011,27 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 			    sb_block, (u64)dsb->zone->id << zmd->zone_nr_blocks_shift);
 		return -EINVAL;
 	}
-	if (zmd->sb_version > 1) {
-		uuid_t sb_uuid;
+	if (version > 1) {
+		uuid_t sb_uuid, dev_uuid;
 
 		import_uuid(&sb_uuid, sb->dmz_uuid);
 		if (uuid_is_null(&sb_uuid)) {
 			dmz_dev_err(dev, "NULL DM-Zoned uuid");
 			return -ENXIO;
-		} else if (uuid_is_null(&zmd->uuid)) {
-			uuid_copy(&zmd->uuid, &sb_uuid);
-		} else if (!uuid_equal(&zmd->uuid, &sb_uuid)) {
+		} else if (!uuid_is_null(&zmd->uuid) &&
+			   !uuid_equal(&zmd->uuid, &sb_uuid)) {
 			dmz_dev_err(dev, "mismatching DM-Zoned uuid, is %pUl expected %pUl",
 				    &sb_uuid, &zmd->uuid);
 			return -ENXIO;
 		}
-		if (!strlen(zmd->label))
-			memcpy(zmd->label, sb->dmz_label, BDEVNAME_SIZE);
-		else if (memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) {
+		if (strlen(zmd->label) &&
+		    memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) {
 			dmz_dev_err(dev, "mismatching DM-Zoned label, is %s expected %s",
 				    sb->dmz_label, zmd->label);
 			return -ENXIO;
 		}
-		import_uuid(&dev->uuid, sb->dev_uuid);
-		if (uuid_is_null(&dev->uuid)) {
+		import_uuid(&dev_uuid, sb->dev_uuid);
+		if (uuid_is_null(&dev_uuid)) {
 			dmz_dev_err(dev, "NULL device uuid");
 			return -ENXIO;
 		}
@@ -1046,7 +1044,7 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 			if (gen != 0)
 				dmz_dev_warn(dev, "Invalid generation %llu",
 					    gen);
-			return 0;
+			goto valid;
 		}
 	}
 
@@ -1103,6 +1101,14 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 	zmd->nr_meta_zones = nr_meta_zones;
 	zmd->nr_data_zones = nr_data_zones;
 
+valid:
+	zmd->sb_version = version;
+	if (version > 1) {
+		import_uuid(&zmd->uuid, sb->dmz_uuid);
+		memcpy(zmd->label, sb->dmz_label, BDEVNAME_SIZE);
+		import_uuid(&dev->uuid, sb->dev_uuid);
+	}
+
 	return 0;
 }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/3] dm zoned: validate the metadata layout in the superblock
  2026-12-01 10:56 [PATCH 0/3] dm zoned: fix the validation of the superblocks Yingjie Wang
  2026-12-01 10:56 ` [PATCH 1/3] dm zoned: do not update metadata state from an invalid superblock Yingjie Wang
@ 2026-12-01 10:56 ` Yingjie Wang
  2026-12-01 10:56 ` [PATCH 3/3] dm zoned: handle labels that are not NUL-terminated Yingjie Wang
  2 siblings, 0 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 10:56 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Damien Le Moal, Hannes Reinecke,
	Yingjie Wang, stable

dmz_check_sb() does not check all the superblock fields that describe
the metadata layout:

 - nr_map_blocks is used to allocate zmd->map_mblk[], which
   dmz_load_mapping() then fills according to nr_chunks.  If there are
   not enough mapping blocks for nr_chunks, it writes past the end of
   the array:

     BUG: KASAN: slab-out-of-bounds in dmz_ctr_metadata
     Write of size 8

 - nr_bitmap_blocks is not used to locate the bitmap of a zone.
   dmz_get_bitmap() assumes that there is a bitmap for every zone.
   This is not the case if the device has grown since it was formatted.
   The bitmaps of the new zones are not part of the metadata sets then,
   and the data in these zones can be lost after a set was recovered.

 - The mapping and bitmap blocks may exceed nr_meta_blocks, so that
   metadata accesses go past the end of the metadata set.  This can
   overwrite the data of other chunks.

 - nr_reserved_seq is checked against the number of zones that remain
   after subtracting one metadata set, but nr_data_zones is calculated
   by subtracting both sets.  The result can underflow, which defeats
   the check of nr_chunks.

Add the missing checks.  dmzadm creates one bitmap for every zone,
including metadata, read-only and offline zones, so that volumes it
formatted are not affected unless the device has grown.

Fixes: 3b1a94c88b79 ("dm zoned: drive-managed zoned block device target")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 drivers/md/dm-zoned-metadata.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/md/dm-zoned-metadata.c b/drivers/md/dm-zoned-metadata.c
index a6b8372010d8..64e0edaa0044 100644
--- a/drivers/md/dm-zoned-metadata.c
+++ b/drivers/md/dm-zoned-metadata.c
@@ -1079,7 +1079,8 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 	}
 
 	if (!le32_to_cpu(sb->nr_reserved_seq) ||
-	    le32_to_cpu(sb->nr_reserved_seq) >= (zmd->nr_useable_zones - nr_meta_zones)) {
+	    nr_meta_zones * 2ULL + le32_to_cpu(sb->nr_reserved_seq) >=
+	    zmd->nr_useable_zones) {
 		dmz_dev_err(dev, "Invalid number of reserved sequential zones");
 		return -ENXIO;
 	}
@@ -1092,6 +1093,25 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 		return -ENXIO;
 	}
 
+	if (le32_to_cpu(sb->nr_map_blocks) <
+	    DIV_ROUND_UP(le32_to_cpu(sb->nr_chunks), DMZ_MAP_ENTRIES)) {
+		dmz_dev_err(dev, "Invalid number of mapping blocks");
+		return -ENXIO;
+	}
+
+	if (le32_to_cpu(sb->nr_bitmap_blocks) <
+	    (u64)zmd->nr_zones * zmd->zone_nr_bitmap_blocks) {
+		dmz_dev_err(dev, "Invalid number of bitmap blocks");
+		return -ENXIO;
+	}
+
+	if (1ULL + le32_to_cpu(sb->nr_map_blocks) +
+	    le32_to_cpu(sb->nr_bitmap_blocks) >
+	    le32_to_cpu(sb->nr_meta_blocks)) {
+		dmz_dev_err(dev, "Invalid number of metadata blocks");
+		return -ENXIO;
+	}
+
 	/* OK */
 	zmd->nr_meta_blocks = le32_to_cpu(sb->nr_meta_blocks);
 	zmd->nr_reserved_seq = le32_to_cpu(sb->nr_reserved_seq);
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/3] dm zoned: handle labels that are not NUL-terminated
  2026-12-01 10:56 [PATCH 0/3] dm zoned: fix the validation of the superblocks Yingjie Wang
  2026-12-01 10:56 ` [PATCH 1/3] dm zoned: do not update metadata state from an invalid superblock Yingjie Wang
  2026-12-01 10:56 ` [PATCH 2/3] dm zoned: validate the metadata layout in the superblock Yingjie Wang
@ 2026-12-01 10:56 ` Yingjie Wang
  2026-12-01 11:06   ` Damien Le Moal
  2 siblings, 1 reply; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 10:56 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Damien Le Moal, Hannes Reinecke,
	Yingjie Wang, stable

If the label in the superblock uses all 32 bytes, it is not
NUL-terminated.  It is copied to zmd->label, which has the same size and
is used as a string.  With CONFIG_FORTIFY_SOURCE, the next call of
dmz_check_sb() crashes:

  strlen: detected buffer overflow: 33 byte read of buffer size 32
  kernel BUG at lib/string_helpers.c:1044!

If the label does not match, it is also printed directly from the
superblock, which can read past the end of the block:

  BUG: KASAN: use-after-free in string
  Read of size 1

Add space for the terminating NUL byte to zmd->label, and limit the
length of the label that is printed.  dmzadm does not create such
labels.

Fixes: bd5c40313a14 ("dm zoned: metadata version 2")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 drivers/md/dm-zoned-metadata.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/dm-zoned-metadata.c b/drivers/md/dm-zoned-metadata.c
index 64e0edaa0044..04ae8e2b3627 100644
--- a/drivers/md/dm-zoned-metadata.c
+++ b/drivers/md/dm-zoned-metadata.c
@@ -145,7 +145,7 @@ struct dmz_metadata {
 	unsigned int		nr_devs;
 
 	char			devname[BDEVNAME_SIZE];
-	char			label[BDEVNAME_SIZE];
+	char			label[BDEVNAME_SIZE + 1];
 	uuid_t			uuid;
 
 	sector_t		zone_bitmap_size;
@@ -1026,7 +1026,7 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 		}
 		if (strlen(zmd->label) &&
 		    memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) {
-			dmz_dev_err(dev, "mismatching DM-Zoned label, is %s expected %s",
+			dmz_dev_err(dev, "mismatching DM-Zoned label, is %.32s expected %s",
 				    sb->dmz_label, zmd->label);
 			return -ENXIO;
 		}
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 3/3] dm zoned: handle labels that are not NUL-terminated
  2026-12-01 10:56 ` [PATCH 3/3] dm zoned: handle labels that are not NUL-terminated Yingjie Wang
@ 2026-12-01 11:06   ` Damien Le Moal
  2026-12-01 12:15     ` Yingjie Wang
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Le Moal @ 2026-12-01 11:06 UTC (permalink / raw)
  To: Yingjie Wang, Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Hannes Reinecke, stable

On 12/1/26 19:56, Yingjie Wang wrote:
> If the label in the superblock uses all 32 bytes, it is not
> NUL-terminated.  It is copied to zmd->label, which has the same size and
> is used as a string.  With CONFIG_FORTIFY_SOURCE, the next call of
> dmz_check_sb() crashes:
> 
>   strlen: detected buffer overflow: 33 byte read of buffer size 32
>   kernel BUG at lib/string_helpers.c:1044!
> 
> If the label does not match, it is also printed directly from the
> superblock, which can read past the end of the block:
> 
>   BUG: KASAN: use-after-free in string
>   Read of size 1
> 
> Add space for the terminating NUL byte to zmd->label, and limit the
> length of the label that is printed.  dmzadm does not create such
> labels.
> 
> Fixes: bd5c40313a14 ("dm zoned: metadata version 2")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yingjie Wang <x-professor@outlook.com>
> ---
>  drivers/md/dm-zoned-metadata.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/md/dm-zoned-metadata.c b/drivers/md/dm-zoned-metadata.c
> index 64e0edaa0044..04ae8e2b3627 100644
> --- a/drivers/md/dm-zoned-metadata.c
> +++ b/drivers/md/dm-zoned-metadata.c
> @@ -145,7 +145,7 @@ struct dmz_metadata {
>  	unsigned int		nr_devs;
>  
>  	char			devname[BDEVNAME_SIZE];
> -	char			label[BDEVNAME_SIZE];
> +	char			label[BDEVNAME_SIZE + 1];

This alone does not guarantee that the label is NUL terminated either. Who says
that this was written with dmzadm or that a chnage in dmzadm introduces a bug?
So the strlen() call below can still crash.

So in addition to this change, we should check that the label has a terminating
NUL character and reject the device as broken otherwise.

>  	uuid_t			uuid;
>  
>  	sector_t		zone_bitmap_size;
> @@ -1026,7 +1026,7 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
>  		}
>  		if (strlen(zmd->label) &&
>  		    memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) {
> -			dmz_dev_err(dev, "mismatching DM-Zoned label, is %s expected %s",
> +			dmz_dev_err(dev, "mismatching DM-Zoned label, is %.32s expected %s",

With the above suggested additional check, this change will not be needed.

>  				    sb->dmz_label, zmd->label);
>  			return -ENXIO;
>  		}


-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 3/3] dm zoned: handle labels that are not NUL-terminated
  2026-12-01 11:06   ` Damien Le Moal
@ 2026-12-01 12:15     ` Yingjie Wang
  0 siblings, 0 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 12:15 UTC (permalink / raw)
  To: Damien Le Moal, Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Hannes Reinecke, stable

Hi Damien,

On 12/1/26 20:06, Damien Le Moal wrote:
>> -	char			label[BDEVNAME_SIZE];
>> +	char			label[BDEVNAME_SIZE + 1];
>
> This alone does not guarantee that the label is NUL terminated either. Who says
> that this was written with dmzadm or that a chnage in dmzadm introduces a bug?
> So the strlen() call below can still crash.

zmd is kzalloc'ed and only 32 bytes are copied into the label, so the
last byte stays 0 and strlen() is fine.

But you're right about dmzadm, what I wrote in the commit message is
wrong. For the default label, 2.0.0 to 2.1.3 do

	sprintf(dev->label, "dmz-%s", dev->bdev[0].name);

so if the device name has 28 or more characters, there is no NUL in the
32 bytes on disk, and loading the volume hits this crash. (If dmzadm is
built with _FORTIFY_SOURCE, it aborts instead.)

> So in addition to this change, we should check that the label has a terminating
> NUL character and reject the device as broken otherwise.

Agreed. That also rejects the volumes above, but they should be rare
and dmzadm 2.2.0 or later can relabel them. I'll add the check in v2,
fix the commit message and drop the extra byte, which isn't needed then.

Thanks,
Yingjie

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01 12:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 10:56 [PATCH 0/3] dm zoned: fix the validation of the superblocks Yingjie Wang
2026-12-01 10:56 ` [PATCH 1/3] dm zoned: do not update metadata state from an invalid superblock Yingjie Wang
2026-12-01 10:56 ` [PATCH 2/3] dm zoned: validate the metadata layout in the superblock Yingjie Wang
2026-12-01 10:56 ` [PATCH 3/3] dm zoned: handle labels that are not NUL-terminated Yingjie Wang
2026-12-01 11:06   ` Damien Le Moal
2026-12-01 12:15     ` Yingjie Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®