Skip to contentSkip to conversation

boot: fit: authenticate the dm-verity roothash

PATCH Superseded Daniel Golle 3 participants 11 messages Jul 12 – Jul 14

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover boot: fit: authenticate the dm-verity roothash

A signed FIT configuration can delegate the integrity of a (potentially large) root filesystem image to the kernel's dm-verity instead of having U-Boot hash the whole payload at boot: the FIT carries a "dm-verity" subnode with the roothash, salt and block parameters, U-Boot passes the roothash to Linux through the dm-mod.create bootargs, and dm-verity then validates the filesystem block by block against it.

For that to be safe the roothash has to be trusted, and in a signed configuration the only thing that establishes trust is the configuration signature. The roothash was not covered by it. fit_config_add_hash() collected the image node, its hash subnodes and its cipher subnode into the signed region, but not the dm-verity subnode, so the roothash, the sole integrity anchor for the filesystem, was left unsigned.

The result is a verified-boot bypass for the root filesystem: an attacker who can rewrite the boot medium can replace the filesystem, recompute a matching dm-verity tree, write the new roothash into the unsigned dm-verity subnode, and the configuration signature still verifies. dm-verity then faithfully validates the malicious filesystem against the attacker's roothash.

This series closes the gap.

Daniel Golle (3):
boot: fit: factor out node-path collection in fit_config_add_hash()
boot: fit: cover the dm-verity roothash with the config signature
test: fit: verify dm-verity roothash is covered by the config
signature

 boot/image-fit-sig.c                  | 119 ++++++++++------
 include/image.h                       |  25 ++++
 test/boot/fit_verity.c                | 194 ++++++++++++++++++++++++++
 test/py/tests/test_fit_verity_sign.py | 162 +++++++++++++++++++++
 tools/image-host.c                    |  22 +++
 5 files changed, 483 insertions(+), 39 deletions(-)
 create mode 100644 test/py/tests/test_fit_verity_sign.py

base-commit: 6741b0dfb41dc82a284ab1cff4c58af6ef2f3f9c

Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/3 boot: fit: factor out node-path collection in fit_config_add_hash()

fit_config_add_hash() repeats the same sequence to append a node's path to the hashed-node list three times: for the image node, for each hash subnode and for the cipher subnode. Extract it into a helper, fit_config_add_node(), with no functional change.

---
 boot/image-fit-sig.c | 73 ++++++++++++++++++++++++++------------------
 1 file changed, 44 insertions(+), 29 deletions(-)
boot/image-fit-sig.c +44 -29
index 9b5ab754561..513ee9cc16a 100644
@@ -230,6 +230,37 @@ int fit_image_verify_required_sigs(const void *fit, int image_noffset,
 	return 0;
 }
 
+/**
+ * fit_config_add_node() - Append one node's path to the hashed-node list
+ *
+ * @fit:		FIT blob
+ * @noffset:		Offset of the node whose path should be added
+ * @node_inc:		Array of path pointers to fill
+ * @count:		Pointer to current count (updated on return)
+ * @max_nodes:		Maximum entries in @node_inc
+ * @buf:		Buffer for packed path strings
+ * @buf_used:		Pointer to bytes used in @buf (updated on return)
+ * @buf_len:		Total size of @buf
+ * Return: 0 on success, -ve on error
+ */
+static int fit_config_add_node(const void *fit, int noffset, char **node_inc,
+			       int *count, int max_nodes, char *buf,
+			       int *buf_used, int buf_len)
+{
+	int ret, len;
+
+	if (*count >= max_nodes)
+		return -ENOSPC;
+	ret = fdt_get_path(fit, noffset, buf + *buf_used, buf_len - *buf_used);
+	if (ret < 0)
+		return -ENOENT;
+	len = strlen(buf + *buf_used) + 1;
+	node_inc[(*count)++] = buf + *buf_used;
+	*buf_used += len;
+
+	return 0;
+}
+
 /**
  * fit_config_add_hash() - Add hash nodes for one image to the node list
  *
@@ -250,18 +281,12 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
 			       char **node_inc, int *count, int max_nodes,
 			       char *buf, int *buf_used, int buf_len)
 {
-	int noffset, hash_count, ret, len;
+	int noffset, hash_count, ret;
 
-	if (*count >= max_nodes)
-		return -ENOSPC;
-
-	ret = fdt_get_path(fit, image_noffset, buf + *buf_used,
-			   buf_len - *buf_used);
-	if (ret < 0)
-		return -ENOENT;
-	len = strlen(buf + *buf_used) + 1;
-	node_inc[(*count)++] = buf + *buf_used;
-	*buf_used += len;
+	ret = fit_config_add_node(fit, image_noffset, node_inc, count,
+				  max_nodes, buf, buf_used, buf_len);
+	if (ret)
+		return ret;
 
 	/* Add all this image's hash subnodes */
 	hash_count = 0;
@@ -273,15 +298,10 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
 		if (strncmp(name, FIT_HASH_NODENAME,
 			    strlen(FIT_HASH_NODENAME)))
 			continue;
-		if (*count >= max_nodes)
-			return -ENOSPC;
-		ret = fdt_get_path(fit, noffset, buf + *buf_used,
-				   buf_len - *buf_used);
-		if (ret < 0)
-			return -ENOENT;
-		len = strlen(buf + *buf_used) + 1;
-		node_inc[(*count)++] = buf + *buf_used;
-		*buf_used += len;
+		ret = fit_config_add_node(fit, noffset, node_inc, count,
+					  max_nodes, buf, buf_used, buf_len);
+		if (ret)
+			return ret;
 		hash_count++;
 	}
 
@@ -296,15 +316,10 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
 	if (noffset != -FDT_ERR_NOTFOUND) {
 		if (noffset < 0)
 			return -EIO;
-		if (*count >= max_nodes)
-			return -ENOSPC;
-		ret = fdt_get_path(fit, noffset, buf + *buf_used,
-				   buf_len - *buf_used);
-		if (ret < 0)
-			return -ENOENT;
-		len = strlen(buf + *buf_used) + 1;
-		node_inc[(*count)++] = buf + *buf_used;
-		*buf_used += len;
+		ret = fit_config_add_node(fit, noffset, node_inc, count,
+					  max_nodes, buf, buf_used, buf_len);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
Signature · 1 line
2.55.0
#4 lore raw

PATCH 2/3 boot: fit: cover the dm-verity roothash with the config signature

In reply to Daniel Golle

A dm-verity protected filesystem image is not hashed by U-Boot when it is loaded; its integrity is delegated to the kernel, which validates the filesystem on the fly against the roothash taken from the FIT dm-verity subnode. The roothash is therefore the sole integrity anchor for the filesystem, yet fit_config_add_hash() only added the image node, its hash subnodes and its cipher subnode to the signed region, leaving the dm-verity subnode (roothash, salt and block parameters) unsigned.

An attacker able to rewrite the boot medium could then replace both the filesystem and the roothash, recompute a matching dm-verity tree and keep the configuration signature valid, defeating verified boot for the root filesystem.

Add the dm-verity subnode to the list of nodes covered by the configuration signature, both when signing (tools/image-host.c) and when verifying (boot/image-fit-sig.c), so the roothash and salt are authenticated together with the rest of the configuration.

---
 boot/image-fit-sig.c | 23 +++++++++++++++++++----
 tools/image-host.c   | 22 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 4 deletions(-)
boot/image-fit-sig.c +19 -4
index 513ee9cc16a..c2f09885492 100644
@@ -264,8 +264,8 @@ static int fit_config_add_node(const void *fit, int noffset, char **node_inc,
 /**
  * fit_config_add_hash() - Add hash nodes for one image to the node list
  *
- * Adds the image path, all its hash-* subnode paths, and its cipher
- * subnode path (if present) to the packed buffer.
+ * Adds the image path, all its hash-* subnode paths, and its cipher and
+ * dm-verity subnode paths (each if present) to the packed buffer.
  *
  * @fit:		FIT blob
  * @image_noffset:	Image node offset (e.g. /images/kernel-1)
@@ -322,6 +322,21 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
 			return ret;
 	}
 
+	/*
+	 * Add this image's dm-verity node if present. Its roothash is the
+	 * only integrity anchor for a dm-verity filesystem image, so it must
+	 * be covered by the configuration signature.
+	 */
+	noffset = fdt_subnode_offset(fit, image_noffset, FIT_VERITY_NODENAME);
+	if (noffset != -FDT_ERR_NOTFOUND) {
+		if (noffset < 0)
+			return -EIO;
+		ret = fit_config_add_node(fit, noffset, node_inc, count,
+					  max_nodes, buf, buf_used, buf_len);
+		if (ret)
+			return ret;
+	}
+
 	return 0;
 }
 
@@ -329,8 +344,8 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
  * fit_config_get_hash_list() - Build the list of nodes to hash
  *
  * Works through every image referenced by the configuration and collects the
- * node paths: root + config + all referenced images with their hash and
- * cipher subnodes.
+ * node paths: root + config + all referenced images with their hash,
+ * cipher and dm-verity subnodes.
  *
  * Properties known not to be image references (description, compatible,
  * default, load-only) are skipped, so any new image type is covered by default.
tools/image-host.c +22 -0
index 8f1e7be4066..235f7643d08 100644
@@ -1256,6 +1256,28 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
 			goto err_mem;
 	}
 
+	/*
+	 * Add this image's dm-verity node if present. Its roothash is the
+	 * only integrity anchor for a dm-verity filesystem image, so it must
+	 * be covered by the configuration signature.
+	 */
+	noffset = fdt_subnode_offset(fit, image_noffset,
+				     FIT_VERITY_NODENAME);
+	if (noffset != -FDT_ERR_NOTFOUND) {
+		if (noffset < 0) {
+			fprintf(stderr,
+				"Failed to get dm-verity node in configuration '%s/%s' image '%s': %s\n",
+				conf_name, sig_name, iname,
+				fdt_strerror(noffset));
+			return -EIO;
+		}
+		ret = fdt_get_path(fit, noffset, path, sizeof(path));
+		if (ret < 0)
+			goto err_path;
+		if (strlist_add(node_inc, path))
+			goto err_mem;
+	}
+
 	return 0;
 
 err_mem:
Signature · 1 line
2.55.0
#7 lore raw

PATCH 3/3 test: fit: verify dm-verity roothash is covered by the config signature

In reply to Daniel Golle

A dm-verity protected filesystem image is not hashed by U-Boot; its integrity is delegated to the kernel, which trusts the roothash taken from the FIT dm-verity subnode. For that chain of trust to hold, the roothash (and salt) must be part of the region covered by the configuration signature, otherwise an attacker can replace both the filesystem and the roothash while keeping the signature valid.

Add two independent checks of this property:

- test/py/tests/test_fit_verity_sign.py signs a configuration that references a filesystem image carrying a dm-verity subnode, then confirms that tampering the roothash or the salt is rejected by fit_check_sign. A control that tampers a byte known to be signed proves the check can fail.

- test/boot/fit_verity.c gains a runtime unit test that builds the exact node list the configuration signature is computed over, turns it into hashed regions and checks both that the roothash bytes fall inside a signed region and that tampering them changes the hash. It needs no private key, so it also runs on real devices and uses the same hash path a device would.

To let the unit test build the signed-region node list, rename the config node-list helper to fit_config_get_signed_nodes() and expose it only when unit tests are built (VISIBLE_IF_UT), keeping it static in normal builds.

---
 boot/image-fit-sig.c                  |  25 +++-
 include/image.h                       |  25 ++++
 test/boot/fit_verity.c                | 194 ++++++++++++++++++++++++++
 test/py/tests/test_fit_verity_sign.py | 162 +++++++++++++++++++++
 4 files changed, 399 insertions(+), 7 deletions(-)
 create mode 100644 test/py/tests/test_fit_verity_sign.py
boot/image-fit-sig.c +18 -7
index c2f09885492..1181fefb55f 100644
@@ -340,8 +340,19 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
 	return 0;
 }
 
+/*
+ * fit_config_get_signed_nodes() is internal, but the fit_verity unit test
+ * needs to build the same signed-region node list. Make it visible when unit
+ * tests are built and keep it static otherwise.
+ */
+#if CONFIG_IS_ENABLED(UNIT_TEST)
+#define VISIBLE_IF_UT
+#else
+#define VISIBLE_IF_UT static
+#endif
+
 /**
- * fit_config_get_hash_list() - Build the list of nodes to hash
+ * fit_config_get_signed_nodes() - Build the list of nodes to hash
  *
  * Works through every image referenced by the configuration and collects the
  * node paths: root + config + all referenced images with their hash,
@@ -358,9 +369,9 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
  * @buf_len:	Size of @buf
  * Return: number of entries in @node_inc, or -ve on error
  */
-static int fit_config_get_hash_list(const void *fit, int conf_noffset,
-				    char **node_inc, int max_nodes,
-				    char *buf, int buf_len)
+VISIBLE_IF_UT int fit_config_get_signed_nodes(const void *fit, int conf_noffset,
+					      char **node_inc, int max_nodes,
+					      char *buf, int buf_len)
 {
 	const char *conf_name;
 	int image_count;
@@ -500,9 +511,9 @@ static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset,
 	}
 
 	/* Build the node list from the config, ignoring hashed-nodes */
-	count = fit_config_get_hash_list(fit, conf_noffset,
-					 node_inc, IMAGE_MAX_HASHED_NODES,
-					 hash_buf, sizeof(hash_buf));
+	count = fit_config_get_signed_nodes(fit, conf_noffset,
+					    node_inc, IMAGE_MAX_HASHED_NODES,
+					    hash_buf, sizeof(hash_buf));
 	if (count < 0) {
 		*err_msgp = "Failed to build hash node list";
 		return -1;
include/image.h +25 -0
index 9c8a746d576..35d6b217596 100644
@@ -1884,6 +1884,31 @@ struct image_region *fit_region_make_list(const void *fit,
 		struct fdt_region *fdt_regions, int count,
 		struct image_region *region);
 
+/**
+ * fit_config_get_signed_nodes() - Build the list of nodes covered by a config
+ *				   signature
+ *
+ * Collects the paths of the nodes that the configuration signature is
+ * computed over: the root node, the configuration node, and for each image
+ * referenced by the configuration its node, its hash subnodes and its cipher
+ * and dm-verity subnodes. The result is the same node list used when creating
+ * and verifying the signature, and is suitable for passing to
+ * fdt_find_regions().
+ *
+ * @fit:	FIT blob
+ * @conf_noffset: Configuration node offset
+ * @node_inc:	Array to fill with pointers to packed path strings
+ * @max_nodes:	Number of entries in @node_inc
+ * @buf:	Buffer for the packed null-terminated path strings
+ * @buf_len:	Size of @buf
+ * Return: number of entries written to @node_inc, or -ve on error
+ */
+#if CONFIG_IS_ENABLED(UNIT_TEST)
+int fit_config_get_signed_nodes(const void *fit, int conf_noffset,
+				char **node_inc, int max_nodes,
+				char *buf, int buf_len);
+#endif
+
 static inline int fit_image_check_target_arch(const void *fdt, int node)
 {
 #ifndef USE_HOSTCC
test/boot/fit_verity.c +194 -0

203 lines not on this page · raw message

test/py/tests/test_fit_verity_sign.py new file +162 -0

162 lines not on this page · raw message

Signature · 1 line
2.55.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] boot: fit: factor out node-path collection in fit_config_add_hash()

In reply to Daniel Golle
Quoted 6 lines · Daniel Golle

fit_config_add_hash() repeats the same sequence to append a node's path to the hashed-node list three times: for the image node, for each hash subnode and for the cipher subnode. Extract it into a helper, fit_config_add_node(), with no functional change.

Signature · 1 line
Tom
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] boot: fit: cover the dm-verity roothash with the config signature

In reply to Daniel Golle
Quoted 13 lines · Daniel Golle

A dm-verity protected filesystem image is not hashed by U-Boot when it is loaded; its integrity is delegated to the kernel, which validates the filesystem on the fly against the roothash taken from the FIT dm-verity subnode. The roothash is therefore the sole integrity anchor for the filesystem, yet fit_config_add_hash() only added the image node, its hash subnodes and its cipher subnode to the signed region, leaving the dm-verity subnode (roothash, salt and block parameters) unsigned.

An attacker able to rewrite the boot medium could then replace both the filesystem and the roothash, recompute a matching dm-verity tree and keep the configuration signature valid, defeating verified boot for the root filesystem.

Quoted 6 lines

Add the dm-verity subnode to the list of nodes covered by the configuration signature, both when signing (tools/image-host.c) and when verifying (boot/image-fit-sig.c), so the roothash and salt are authenticated together with the rest of the configuration.

Signature · 1 line
Tom
#8 lore raw

Re: [PATCH 3/3] test: fit: verify dm-verity roothash is covered by the config signature

In reply to Daniel Golle
Quoted 44 lines · Daniel Golle

A dm-verity protected filesystem image is not hashed by U-Boot; its integrity is delegated to the kernel, which trusts the roothash taken from the FIT dm-verity subnode. For that chain of trust to hold, the roothash (and salt) must be part of the region covered by the configuration signature, otherwise an attacker can replace both the filesystem and the roothash while keeping the signature valid.

Add two independent checks of this property:

- test/py/tests/test_fit_verity_sign.py signs a configuration that references a filesystem image carrying a dm-verity subnode, then confirms that tampering the roothash or the salt is rejected by fit_check_sign. A control that tampers a byte known to be signed proves the check can fail.

- test/boot/fit_verity.c gains a runtime unit test that builds the exact node list the configuration signature is computed over, turns it into hashed regions and checks both that the roothash bytes fall inside a signed region and that tampering them changes the hash. It needs no private key, so it also runs on real devices and uses the same hash path a device would.

To let the unit test build the signed-region node list, rename the config node-list helper to fit_config_get_signed_nodes() and expose it only when unit tests are built (VISIBLE_IF_UT), keeping it static in normal builds.

---
 boot/image-fit-sig.c                  |  25 +++-
 include/image.h                       |  25 ++++
 test/boot/fit_verity.c                | 194 ++++++++++++++++++++++++++
 test/py/tests/test_fit_verity_sign.py | 162 +++++++++++++++++++++
 4 files changed, 399 insertions(+), 7 deletions(-)
 create mode 100644 test/py/tests/test_fit_verity_sign.py
boot/image-fit-sig.c +0 -0
index c2f09885492..1181fefb55f 100644
@@ -340,8 +340,19 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
 	return 0;
 }
 

boot/image-fit-sig.c static int fit_config_add_hash(const void *fit, int image_noffset, · line 352

Quoted 10 lines
boot/image-fit-sig.c +10 -0
+/*
+ * fit_config_get_signed_nodes() is internal, but the fit_verity unit test
+ * needs to build the same signed-region node list. Make it visible when unit
+ * tests are built and keep it static otherwise.
+ */
+#if CONFIG_IS_ENABLED(UNIT_TEST)
+#define VISIBLE_IF_UT
+#else
+#define VISIBLE_IF_UT static
+#endif

I don't like this kind of "game", and I think it's why we generally just don't mark functions as static that could be, if not for UNIT_TEST. Finding a more general way to handle this would be good and an unrelated clean-up (after verifying that for example, marking a ton of stuff as now static results in a more compact binary or provides some obvious gain).

My other comment would be that we should be doing like doc/develop/pytest//test_fit.rst for example and making sure we have good and usable generated documentation (so using pydoc comments, not "#" style). And FYI that usually means building and viewing the docs locally, not just making sure CI passes, it can be tricky I've found to get examples annotated correctly to render usefully.

Signature · 1 line
Tom
Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] boot: fit: factor out node-path collection in fit_config_add_hash()

In reply to Daniel Golle
Quoted 11 lines · Daniel Golle

boot: fit: factor out node-path collection in fit_config_add_hash()

fit_config_add_hash() repeats the same sequence to append a node's path to the hashed-node list three times: for the image node, for each hash subnode and for the cipher subnode. Extract it into a helper, fit_config_add_node(), with no functional change.

boot/image-fit-sig.c | 73 +++++++++++++++++++++++++++++++---------------------

 1 file changed, 44 insertions(+), 29 deletions(-)
#6 lore raw

Re: [PATCH 2/3] boot: fit: cover the dm-verity roothash with the config signature

In reply to Daniel Golle

Hi Daniel,

Quoted 23 lines · Daniel Golle

boot: fit: cover the dm-verity roothash with the config signature

A dm-verity protected filesystem image is not hashed by U-Boot when it is loaded; its integrity is delegated to the kernel, which validates the filesystem on the fly against the roothash taken from the FIT dm-verity subnode. The roothash is therefore the sole integrity anchor for the filesystem, yet fit_config_add_hash() only added the image node, its hash subnodes and its cipher subnode to the signed region, leaving the dm-verity subnode (roothash, salt and block parameters) unsigned.

An attacker able to rewrite the boot medium could then replace both the filesystem and the roothash, recompute a matching dm-verity tree and keep the configuration signature valid, defeating verified boot for the root filesystem.

Add the dm-verity subnode to the list of nodes covered by the configuration signature, both when signing (tools/image-host.c) and when verifying (boot/image-fit-sig.c), so the roothash and salt are authenticated together with the rest of the configuration.

[...]

boot/image-fit-sig.c | 23 +++++++++++++++++++----

Quoted 8 lines
 tools/image-host.c   | 22 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 4 deletions(-)

A dm-verity protected filesystem image is not hashed by U-Boot when it is loaded; its integrity is delegated to the kernel, which validates the filesystem on the fly against the roothash taken from the FIT dm-verity subnode. The roothash is therefore the sole integrity anchor for the filesystem, yet fit_config_add_hash() only added the image node, its

Please use present tense when describing the code before this patch ('only adds ... leaving'), since the code is still in that state when the commit is applied.

boot/image-fit-sig.c static int fit_config_add_hash(const void *fit, int image_noffset, · line 345

Quoted 6 lines
boot/image-fit-sig.c +2 -2
@@ -329,8 +344,8 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
- * node paths: root + config + all referenced images with their hash and
- * cipher subnodes.
+ * node paths: root + config + all referenced images with their hash,
+ * cipher and dm-verity subnodes.

You update the comments here, but doc/usage/fit/signature.rst (in the 'Details' section) still says the rebuilt node list "always includes the root node, the configuration node, each referenced image node and its hash/cipher subnodes". Please can you update that to mention the dm-verity subnode as well? It would also be worth a sentence in doc/usage/fit/dm-verity.rst noting that the roothash and salt are covered by the configuration signature, since that is the security property people will rely on.

Quoted 8 lines
tools/image-host.c +6 -0
@@ -1256,6 +1256,28 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
+     noffset = fdt_subnode_offset(fit, image_noffset,
+                                  FIT_VERITY_NODENAME);
+     if (noffset != -FDT_ERR_NOTFOUND) {
+             if (noffset < 0) {
+                     fprintf(stderr,
+                             "Failed to get dm-verity node in configuration '%s/%s' image '%s': %s\n",

tools/image-host.c static int fit_config_add_hash(const void *fit, int image_noffset, · line 1271

Quoted 10 lines
tools/image-host.c +10 -0
+                             conf_name, sig_name, iname,
+                             fdt_strerror(noffset));
+                     return -EIO;
+             }
+             ret = fdt_get_path(fit, noffset, path, sizeof(path));
+             if (ret < 0)
+                     goto err_path;
+             if (strlist_add(node_inc, path))
+                     goto err_mem;
+     }

This follows the existing cipher pattern, but with this addition the host-side fit_config_add_hash() has three copies of the same get-path-and-add sequence - exactly the duplication patch 1 removes on the boot side. Please can you add a similar factoring patch for tools/image-host.c, or fold it into patch 1? The two implementations must produce the same node list for verification to succeed, so keeping them easy to compare seems better.

Regards,
Simon

#9 lore raw

Re: [PATCH 3/3] test: fit: verify dm-verity roothash is covered by the config signature

In reply to Tom Rini

Hi Daniel,

Quoted 22 lines · Daniel Golle

test: fit: verify dm-verity roothash is covered by the config signature

A dm-verity protected filesystem image is not hashed by U-Boot; its integrity is delegated to the kernel, which trusts the roothash taken from the FIT dm-verity subnode. For that chain of trust to hold, the roothash (and salt) must be part of the region covered by the configuration signature, otherwise an attacker can replace both the filesystem and the roothash while keeping the signature valid.

Add two independent checks of this property:

- test/py/tests/test_fit_verity_sign.py signs a configuration that references a filesystem image carrying a dm-verity subnode, then confirms that tampering the roothash or the salt is rejected by fit_check_sign. A control that tampers a byte known to be signed proves the check can fail.

- test/boot/fit_verity.c gains a runtime unit test that builds the exact node list the configuration signature is computed over, turns it into hashed regions and checks both that the roothash
[...]

Quoted 5 lines

boot/image-fit-sig.c | 25 +++--

 include/image.h                       |  25 +++++
 test/boot/fit_verity.c                | 194 ++++++++++++++++++++++++++++++++++
 test/py/tests/test_fit_verity_sign.py | 162 ++++++++++++++++++++++++++++
 4 files changed, 399 insertions(+), 7 deletions(-)

Somehow I didn't get this email, but I'll try to reply here.

boot/image-fit-sig.c static int fit_config_add_hash(const void *fit, int image_noffset, · line 344

Quoted 7 lines
boot/image-fit-sig.c +5 -0
@@ -340,8 +340,19 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
+#if CONFIG_IS_ENABLED(UNIT_TEST)
+#define VISIBLE_IF_UT
+#else
+#define VISIBLE_IF_UT static
+#endif

Tom commented on this, but I'll just add this. Linux has VISIBLE_IF_KUNIT but in U-Boot so far we just stop the static when the function is needed elsewhere. LTO can be used to avoid the code-size increase, for platforms that want it.

test/boot/fit_verity.c static int fit_verity_test_bad_blocksize(struct unit_test_state *uts) · line 312

Quoted 6 lines
test/boot/fit_verity.c +4 -0
@@ -304,3 +309,192 @@ static int fit_verity_test_bad_blocksize(struct unit_test_state *uts)
+ count = fdt_find_regions(buf, node_inc, count, NULL, 0, fdt_regions,
+ ARRAY_SIZE(fdt_regions) - 1, region_path,
+ sizeof(region_path), 0);
+ ut_assert(count > 0);

fdt_find_regions() can return a count larger than the max passed in, meaning the region array was exhausted (see the 'count >= max_regions - 1' check in fit_config_check_sig()). If that ever happens here, fit_region_make_list() reads past the filled entries. Please can you assert that count is within the array size, matching the real verification path?

test/boot/fit_verity.c static int fit_verity_test_bad_blocksize(struct unit_test_state *uts) · line 310

Quoted 4 lines
test/boot/fit_verity.c +2 -0
@@ -304,3 +309,192 @@ static int fit_verity_test_bad_blocksize(struct unit_test_state *uts)
+ /* Tampering the roothash must change the signed hash. */
+ ((u8 *)digest)[0] ^= 0xff;

You already have digest_off, so you could write to buf[digest_off] instead of casting away the const from fdt_getprop(). Both work since buf is writable, but avoiding the cast is cleaner.

test/boot/fit_verity.c static int fit_verity_test_bad_blocksize(struct unit_test_state *uts) · line 310

Quoted 4 lines
test/boot/fit_verity.c +2 -0
@@ -304,3 +309,192 @@ static int fit_verity_test_bad_blocksize(struct unit_test_state *uts)
+ digest = fdt_getprop(buf, verity_node, FIT_VERITY_DIGEST_PROP,
+      &digest_len);

The python test also tampers the salt, but the unit test only checks the digest. Since both properties sit in the same dm-verity node the coverage result is the same, so I don't feel strongly, but a one-line comment noting that the digest check stands in for the whole node would make the asymmetry clearer. What do you think?

Regards,
Simon

#10 lore raw

Re: [PATCH 3/3] test: fit: verify dm-verity roothash is covered by the config signature

Quoted 33 lines · Simon Glass

Hi Daniel,

Quoted 22 lines · Daniel Golle

test: fit: verify dm-verity roothash is covered by the config signature

A dm-verity protected filesystem image is not hashed by U-Boot; its integrity is delegated to the kernel, which trusts the roothash taken from the FIT dm-verity subnode. For that chain of trust to hold, the roothash (and salt) must be part of the region covered by the configuration signature, otherwise an attacker can replace both the filesystem and the roothash while keeping the signature valid.

Add two independent checks of this property:

- test/py/tests/test_fit_verity_sign.py signs a configuration that references a filesystem image carrying a dm-verity subnode, then confirms that tampering the roothash or the salt is rejected by fit_check_sign. A control that tampers a byte known to be signed proves the check can fail.

- test/boot/fit_verity.c gains a runtime unit test that builds the exact node list the configuration signature is computed over, turns it into hashed regions and checks both that the roothash
[...]

Quoted 5 lines

boot/image-fit-sig.c | 25 +++--

 include/image.h                       |  25 +++++
 test/boot/fit_verity.c                | 194 ++++++++++++++++++++++++++++++++++
 test/py/tests/test_fit_verity_sign.py | 162 ++++++++++++++++++++++++++++
 4 files changed, 399 insertions(+), 7 deletions(-)

Somehow I didn't get this email, but I'll try to reply here.

Quoted 12 lines

boot/image-fit-sig.c static int fit_config_add_hash(const void *fit, int image_noffset, · line 344

Quoted 7 lines
boot/image-fit-sig.c +5 -0
@@ -340,8 +340,19 @@ static int fit_config_add_hash(const void *fit, int image_noffset,
+#if CONFIG_IS_ENABLED(UNIT_TEST)
+#define VISIBLE_IF_UT
+#else
+#define VISIBLE_IF_UT static
+#endif

Tom commented on this, but I'll just add this. Linux has VISIBLE_IF_KUNIT but in U-Boot so far we just stop the static when the function is needed elsewhere. LTO can be used to avoid the code-size increase, for platforms that want it.

As part of why this should be its own discussion, a quick look at VISIBLE_IF_KUNIT in the kernel leads me to think it's not quite the same set of needs, or if so there's a lot more architectural enforcement around what is/isn't exposed just for testing, which we lack today. Talking about LTO isn't relevant here.

Signature · 1 line
Tom