From 4ee2db2a221f6404f9fe9470da7c384a25cceea3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= <thomas@t-8ch.de>
Date: Fri, 12 Jan 2024 08:50:14 +0100
Subject: [PATCH] libblkid: (drbd) validate zero padding
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This should reduce false-positives.

See #2701.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
Reference:https://github.com/util-linux/util-linux/commit/4ee2db2a221f6404f9fe9470da7c384a25cceea3
Conflict:Context adapt
---
 libblkid/src/superblocks/drbd.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/libblkid/src/superblocks/drbd.c b/libblkid/src/superblocks/drbd.c
index f360186..1723229 100644
--- a/libblkid/src/superblocks/drbd.c
+++ b/libblkid/src/superblocks/drbd.c
@@ -70,9 +70,8 @@ struct md_on_disk_08 {
 	uint32_t bm_bytes_per_bit;
 	uint32_t reserved_u32[4];
 
-	/* Unnecessary for libblkid **
-	 * char reserved[8 * 512 - (8*(UI_SIZE+3)+4*11)];
-	 */
+	unsigned char padding_start[0];
+	unsigned char padding_end[0] __attribute__((aligned(4096)));
 };
 
 /*
@@ -118,11 +117,19 @@ struct meta_data_on_disk_9 {
 	struct peer_dev_md_on_disk_9 peers[DRBD_PEERS_MAX];
 	uint64_t history_uuids[HISTORY_UUIDS];
 
-	/* Unnecessary for libblkid **
-	 * char padding[0] __attribute__((aligned(4096)));
-	 */
+	unsigned char padding_start[0];
+	unsigned char padding_end[0] __attribute__((aligned(4096)));
 } __attribute__((packed));
 
+static int is_zero_padded(const unsigned char *padding_start,
+			  const unsigned char *padding_end)
+{
+	for (; padding_start < padding_end; padding_start++) {
+		if (*padding_start != 0)
+			return 0;
+	}
+	return 1;
+}
 
 static int probe_drbd_84(blkid_probe pr)
 {
@@ -146,6 +153,10 @@ static int probe_drbd_84(blkid_probe pr)
 			be32_to_cpu(md->magic) != DRBD_MD_MAGIC_84_UNCLEAN)
 		return 1;
 
+	if (!is_zero_padded(member_ptr(md, padding_start),
+			    member_ptr(md, padding_end)))
+		return 1;
+
 	/*
 	 * DRBD does not have "real" uuids; the following resembles DRBD's
 	 * notion of uuids (64 bit, see struct above)
@@ -190,6 +201,10 @@ static int probe_drbd_90(blkid_probe pr)
 	if (be32_to_cpu(md->magic) != DRBD_MD_MAGIC_09)
 		return 1;
 
+	if (!is_zero_padded(member_ptr(md, padding_start),
+			    member_ptr(md, padding_end)))
+		return 1;
+
 	/*
 	 * DRBD does not have "real" uuids; the following resembles DRBD's
 	 * notion of uuids (64 bit, see struct above)
-- 
2.33.0

