mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/3] dm: verity: don't let the superblock override the root hash
@ 2026-11-26 16:10 Ahmad Fatoum
  2026-11-26 16:10 ` [PATCH master 2/3] dm: verity: don't read past the superblock salt Ahmad Fatoum
  2026-11-26 16:10 ` [PATCH master 3/3] dm: verity: support superblocks without a salt Ahmad Fatoum
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2026-11-26 16:10 UTC (permalink / raw)
  To: barebox; +Cc: Tobias Waldekranz, Ahmad Fatoum

dm_verity_config_from_sb() pastes the hash algorithm name from the
superblock into the table unchecked, although the superblock is no more
trusted than the data it protects.

A name containing a space adds an argument to the table. If the
superblock has no salt as well, the empty salt column removes one again:
the second word of the name becomes the root digest and the root hash
given by the user the salt. "crc32 <digest>" fits into the 32-byte
field, so veritysetup open accepts modified data against a hash tree
forged by the attacker. The name needn't be NUL-terminated either.

Let's only accept a NUL-terminated name without whitespace.

Fixes: f2cc4a0a5f3c ("dm: verity: Add helper to parse superblock information")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/block/dm/dm-verity.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/block/dm/dm-verity.c b/drivers/block/dm/dm-verity.c
index d7b212fba60a..63616ce86f0e 100644
--- a/drivers/block/dm/dm-verity.c
+++ b/drivers/block/dm/dm-verity.c
@@ -15,6 +15,7 @@
 #include <digest.h>
 #include <disks.h>
 #include <fcntl.h>
+#include <string.h>
 #include <xfuncs.h>
 #include <unistd.h>
 
@@ -501,6 +502,11 @@ char *dm_verity_config_from_sb(const char *data_dev, const char *hash_dev,
 	if (le32_to_cpu(sb.version) != 1)
 		return ERR_PTR(-ENOTSUPP);
 
+	/* The name is pasted into the table, so it must be a single word */
+	if (!memchr(sb.algorithm, '\0', sizeof(sb.algorithm)) ||
+	    strpbrk(sb.algorithm, " \t\n"))
+		return ERR_PTR(-EINVAL);
+
 	sects = le32_to_cpu(sb.data_block_size) >> SECTOR_SHIFT;
 	if (!sects)
 		return ERR_PTR(-ERANGE);
-- 
2.47.3




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

* [PATCH master 2/3] dm: verity: don't read past the superblock salt
  2026-11-26 16:10 [PATCH master 1/3] dm: verity: don't let the superblock override the root hash Ahmad Fatoum
@ 2026-11-26 16:10 ` Ahmad Fatoum
  2026-11-26 16:10 ` [PATCH master 3/3] dm: verity: support superblocks without a salt Ahmad Fatoum
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2026-11-26 16:10 UTC (permalink / raw)
  To: barebox; +Cc: Tobias Waldekranz, Ahmad Fatoum

dm_verity_config_from_sb() formats salt_size bytes of the 256-byte salt
with %*phN, but salt_size is a 16-bit field from the superblock and
barebox doesn't cap %*phN at 64 bytes like Linux. veritysetup thus reads
up to 64K of stack into the table, which veritysetup dump prints.

Let's reject superblocks whose salt doesn't fit.

Fixes: f2cc4a0a5f3c ("dm: verity: Add helper to parse superblock information")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/block/dm/dm-verity.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/block/dm/dm-verity.c b/drivers/block/dm/dm-verity.c
index 63616ce86f0e..c4f88852b5f2 100644
--- a/drivers/block/dm/dm-verity.c
+++ b/drivers/block/dm/dm-verity.c
@@ -483,6 +483,7 @@ char *dm_verity_config_from_sb(const char *data_dev, const char *hash_dev,
 			       const char *root_hash)
 {
 	struct dm_verity_sb sb;
+	unsigned int salt_size;
 	blkcnt_t sects;
 	ssize_t n;
 	int fd;
@@ -507,6 +508,10 @@ char *dm_verity_config_from_sb(const char *data_dev, const char *hash_dev,
 	    strpbrk(sb.algorithm, " \t\n"))
 		return ERR_PTR(-EINVAL);
 
+	salt_size = le16_to_cpu(sb.salt_size);
+	if (salt_size > sizeof(sb.salt))
+		return ERR_PTR(-EINVAL);
+
 	sects = le32_to_cpu(sb.data_block_size) >> SECTOR_SHIFT;
 	if (!sects)
 		return ERR_PTR(-ERANGE);
@@ -518,6 +523,6 @@ char *dm_verity_config_from_sb(const char *data_dev, const char *hash_dev,
 			 le32_to_cpu(sb.data_block_size),
 			 le32_to_cpu(sb.hash_block_size),
 			 le64_to_cpu(sb.data_blocks), sb.algorithm, root_hash,
-			 le16_to_cpu(sb.salt_size), sb.salt);
+			 salt_size, sb.salt);
 }
 EXPORT_SYMBOL(dm_verity_config_from_sb);
-- 
2.47.3




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

* [PATCH master 3/3] dm: verity: support superblocks without a salt
  2026-11-26 16:10 [PATCH master 1/3] dm: verity: don't let the superblock override the root hash Ahmad Fatoum
  2026-11-26 16:10 ` [PATCH master 2/3] dm: verity: don't read past the superblock salt Ahmad Fatoum
@ 2026-11-26 16:10 ` Ahmad Fatoum
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2026-11-26 16:10 UTC (permalink / raw)
  To: barebox; +Cc: Tobias Waldekranz, Ahmad Fatoum

For a superblock without a salt, dm_verity_config_from_sb() leaves the
salt column empty. The table parser skips empty columns, so the target
gets 9 instead of 10 arguments and hash devices formatted with --salt=-
can't be opened.

Let's write "-" instead, which the verity target takes as no salt, like
in Linux.

Fixes: f2cc4a0a5f3c ("dm: verity: Add helper to parse superblock information")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/block/dm/dm-verity.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/block/dm/dm-verity.c b/drivers/block/dm/dm-verity.c
index c4f88852b5f2..ca32dc326730 100644
--- a/drivers/block/dm/dm-verity.c
+++ b/drivers/block/dm/dm-verity.c
@@ -483,6 +483,7 @@ char *dm_verity_config_from_sb(const char *data_dev, const char *hash_dev,
 			       const char *root_hash)
 {
 	struct dm_verity_sb sb;
+	char salt[2 * sizeof(sb.salt) + 1] = "-";
 	unsigned int salt_size;
 	blkcnt_t sects;
 	ssize_t n;
@@ -512,17 +513,21 @@ char *dm_verity_config_from_sb(const char *data_dev, const char *hash_dev,
 	if (salt_size > sizeof(sb.salt))
 		return ERR_PTR(-EINVAL);
 
+	/* An empty column would shift the arguments, Linux uses "-" too */
+	if (salt_size)
+		*bin2hex(salt, sb.salt, salt_size) = '\0';
+
 	sects = le32_to_cpu(sb.data_block_size) >> SECTOR_SHIFT;
 	if (!sects)
 		return ERR_PTR(-ERANGE);
 
 	sects *= le64_to_cpu(sb.data_blocks);
 
-	return xasprintf("0 %llu verity %u %s %s %u %u %llu 1 %s %s %*phN",
+	return xasprintf("0 %llu verity %u %s %s %u %u %llu 1 %s %s %s",
 			 sects, le32_to_cpu(sb.hash_type), data_dev, hash_dev,
 			 le32_to_cpu(sb.data_block_size),
 			 le32_to_cpu(sb.hash_block_size),
 			 le64_to_cpu(sb.data_blocks), sb.algorithm, root_hash,
-			 salt_size, sb.salt);
+			 salt);
 }
 EXPORT_SYMBOL(dm_verity_config_from_sb);
-- 
2.47.3




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

end of thread, other threads:[~2026-11-26 16:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 16:10 [PATCH master 1/3] dm: verity: don't let the superblock override the root hash Ahmad Fatoum
2026-11-26 16:10 ` [PATCH master 2/3] dm: verity: don't read past the superblock salt Ahmad Fatoum
2026-11-26 16:10 ` [PATCH master 3/3] dm: verity: support superblocks without a salt Ahmad Fatoum

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