Skip to contentSkip to conversation

dm: verity target

PATCH Superseded Mandeep Singh Baines 3 participants 8 messages Nov 10, 2011 – Nov 22, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm: verity target

The verity target provides transparent integrity checking of block devices using a cryptographic digest.

dm-verity is meant to be setup as part of a verified boot path. This may be anything ranging from a boot using tboot or trustedgrub to just booting from a known-good device (like a USB drive or CD).

dm-verity is part of ChromeOS's verified boot path. It is used to verify the integrity of the root filesystem on boot. The root filesystem is mounted on a dm-verity partition which transparently verifies each block with a bootloader verified hash passed into the kernel at boot.

---
 Documentation/device-mapper/dm-bht.txt    |   59 ++
 Documentation/device-mapper/dm-verity.txt |   76 +++
 drivers/md/Kconfig                        |   30 +
 drivers/md/Makefile                       |    2 +
 drivers/md/dm-bht.c                       |  542 +++++++++++++++
 drivers/md/dm-verity.c                    | 1043 +++++++++++++++++++++++++++++
 drivers/md/dm-verity.h                    |   45 ++
 include/linux/dm-bht.h                    |  166 +++++
 8 files changed, 1963 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/device-mapper/dm-bht.txt
 create mode 100644 Documentation/device-mapper/dm-verity.txt
 create mode 100644 drivers/md/dm-bht.c
 create mode 100644 drivers/md/dm-verity.c
 create mode 100644 drivers/md/dm-verity.h
 create mode 100644 include/linux/dm-bht.h
Documentation/device-mapper/dm-bht.txt new file +59 -0
new file mode 100644
index 0000000..21d929f
@@ -0,0 +1,59 @@
+dm-bht
+======
+
+dm-bht provides a block hash tree implementation.  The use of dm-bht allows
+for integrity checking of a given block device without reading the entire
+set of blocks into memory before use.
+
+In particular, dm-bht supplies an interface for creating and verifying a tree
+of cryptographic digests with any algorithm supported by the kernel crypto API.
+
+The `verity' target is the motivating example.
+
+
+Theory of operation
+===================
+
+dm-bht is logically comprised of multiple nodes organized in a tree-like
+structure.  Each node in the tree is a cryptographic hash.  If it is a leaf
+node, the hash is of some block data on disk.  If it is an intermediary node,
+then the hash is of a number of child nodes.
+
+dm-bht has a given depth starting at 1 (ignoring the root node).  Each level in
+the tree is concretely made up of dm_bht_entry structs.  Each entry in the tree
+is a collection of neighboring nodes that fit in one page-sized block.  The
+number is determined based on PAGE_SIZE and the size of the selected
+cryptographic digest algorithm.  The hashes are linearly ordered in this entry
+and any unaligned trailing space is ignored but included when calculating the
+parent node.
+
+The tree looks something like:
+
+alg= sha256, num_blocks = 32767
+                                 [   root    ]
+                                /    . . .    \
+                     [entry_0]                 [entry_1]
+                    /  . . .  \                 . . .   \
+         [entry_0_0]   . . .  [entry_0_127]    . . . .  [entry_1_127]
+           / ... \             /   . . .  \             /           \
+     blk_0 ... blk_127  blk_16256   blk_16383      blk_32640 . . . blk_32767
+
+root is treated independently from the depth and the blocks are expected to
+be hashed and supplied to the dm-bht.  hash blocks that make up the entry
+contents are expected to be read from disk.
+
+dm-bht does not handle I/O directly but instead expects the consumer to
+supply callbacks.  The read callback will always receive a page-align value
+to pass to the block device layer to read in a hash value.
+
+Usage
+=====
+
+The API provides mechanisms for reading and verifying a tree. When reading, all
+required data for the hash tree should be populated for a block before
+attempting a verify.  This can be done by calling dm_bht_populate().  When all
+data is ready, a call to dm_bht_verify_block() with the expected hash value will
+perform both the direct block hash check and the hashes of the parent and
+neighboring nodes where needed to ensure validity up to the root hash.  Note,
+dm_bht_set_root_hexdigest() should be called before any verification attempts
+occur.
Documentation/device-mapper/dm-verity.txt new file +76 -0

76 lines not on this page · raw message

drivers/md/Kconfig +30 -0
index faa4741..3cdf95c 100644
@@ -370,4 +370,34 @@ config DM_FLAKEY
        ---help---
          A target that intermittently fails I/O for debugging purposes.
 
+config DM_BHT
+        tristate "Block hash tree support"
+        select CRYPTO
+        select CRYPTO_HASH
+        ---help---
+          Include support for device-mapper devices to use a block hash
+          tree for managing data integrity checks in a scalable way.
+
+          Targets that use this functionality should include it
+          automatically.
+
+          If unsure, say N.
+
+config DM_VERITY
+        tristate "Verity target support"
+        depends on BLK_DEV_DM
+        select DM_BHT
+        select CRYPTO
+        select CRYPTO_HASH
+        ---help---
+          This device-mapper target allows you to create a device that
+          transparently integrity checks the data on it. You'll need to
+          activate the digests you're going to use in the cryptoapi
+          configuration.
+
+          To compile this code as a module, choose M here: the module will
+          be called dm-verity.
+
+          If unsure, say N.
+
 endif # MD
drivers/md/Makefile +2 -0
index 046860c..c069953 100644
@@ -39,6 +39,8 @@ obj-$(CONFIG_DM_SNAPSHOT)	+= dm-snapshot.o
 obj-$(CONFIG_DM_PERSISTENT_DATA)	+= persistent-data/
 obj-$(CONFIG_DM_MIRROR)		+= dm-mirror.o dm-log.o dm-region-hash.o
 obj-$(CONFIG_DM_LOG_USERSPACE)	+= dm-log-userspace.o
+obj-$(CONFIG_DM_BHT)            += dm-bht.o
+obj-$(CONFIG_DM_VERITY)         += dm-verity.o
 obj-$(CONFIG_DM_ZERO)		+= dm-zero.o
 obj-$(CONFIG_DM_RAID)	+= dm-raid.o
 obj-$(CONFIG_DM_THIN_PROVISIONING)	+= dm-thin-pool.o
drivers/md/dm-bht.c new file +542 -0

542 lines not on this page · raw message

drivers/md/dm-verity.c new file +1043 -0

1,043 lines not on this page · raw message

drivers/md/dm-verity.h new file +45 -0

45 lines not on this page · raw message

include/linux/dm-bht.h new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
1.7.3.1
#1 lore raw
Quoted 7 lines · Mandeep Singh Baines

+ * TODO(wad): All hash storage memory is pre-allocated and freed once an
+ * entire branch has been verified.
+ */

include/linux/dm-bht.h new file +3 -0
+struct dm_bht {
+	/* Configured values */
+	int depth;  /* Depth of the tree including the root */

include/linux/dm-bht.h line 95

Quoted 10 lines
include/linux/dm-bht.h new file +10 -0
+	unsigned int block_count;  /* Number of blocks hashed */
+	unsigned int block_size;  /* Size of a hash block */
+	char hash_alg[CRYPTO_MAX_ALG_NAME];
+	unsigned char salt[DM_BHT_SALT_SIZE];
+
+	/* Computed values */
+	unsigned int node_count;  /* Data size (in hashes) for each entry */
+	unsigned int node_count_shift;  /* first bit set - 1 */
+	/* There is one per CPU so that verified can be simultaneous. */
+	struct hash_desc hash_desc[NR_CPUS];  /* Container for the hash alg */

Please don't add a new user for the old hash interface. If the hashes can be done asynchronous you can use ahash, if not use shash. Both interfaces are reentrant, that's probaply what you want to have here. You don't need to have this in a per cpu manner.

#2 lore raw
Quoted 15 lines · Steffen Klassert
Quoted 14 lines · Mandeep Singh Baines

+ * TODO(wad): All hash storage memory is pre-allocated and freed once an
+ * entire branch has been verified.
+ */

include/linux/dm-bht.h new file +10 -0
+struct dm_bht {
+     /* Configured values */
+     int depth;  /* Depth of the tree including the root */
+     unsigned int block_count;  /* Number of blocks hashed */
+     unsigned int block_size;  /* Size of a hash block */
+     char hash_alg[CRYPTO_MAX_ALG_NAME];
+     unsigned char salt[DM_BHT_SALT_SIZE];
+
+     /* Computed values */
+     unsigned int node_count;  /* Data size (in hashes) for each entry */
Quoted 8 lines
Quoted 3 lines

+     unsigned int node_count_shift;  /* first bit set - 1 */
+     /* There is one per CPU so that verified can be simultaneous. */
+     struct hash_desc hash_desc[NR_CPUS];  /* Container for the hash alg */

Please don't add a new user for the old hash interface. If the hashes can be done asynchronous you can use ahash, if not use shash. Both interfaces are reentrant, that's probaply what you want to have here. You don't need to have this in a per cpu manner.

I'll check out the two interfaces. I didn't realize hash_desc was deprecated specifically in favor of one of the others. I'm interested in seeing if it is possible to not keep a per-cpu desc though with the other apis. We do this now to avoid contention across multiple kernel threads on crypto operations sharing the hash_desc (e.g., by wrapping it in a mutex).

Thanks!

#3 lore raw

PATCH v2 1/1 dm: verity target

In reply to Steffen Klassert

Changes in v2:
* Use shash API instead of older hash API (thanks Steffen).

-- >8 -- (snip)

The verity target provides transparent integrity checking of block devices using a cryptographic digest.

dm-verity is meant to be setup as part of a verified boot path. This may be anything ranging from a boot using tboot or trustedgrub to just booting from a known-good device (like a USB drive or CD).

dm-verity is part of ChromeOS's verified boot path. It is used to verify the integrity of the root filesystem on boot. The root filesystem is mounted on a dm-verity partition which transparently verifies each block with a bootloader verified hash passed into the kernel at boot.

---
 Documentation/device-mapper/dm-bht.txt    |   59 ++
 Documentation/device-mapper/dm-verity.txt |   76 +++
 drivers/md/Kconfig                        |   30 +
 drivers/md/Makefile                       |    2 +
 drivers/md/dm-bht.c                       |  559 +++++++++++++++
 drivers/md/dm-verity.c                    | 1043 +++++++++++++++++++++++++++++
 drivers/md/dm-verity.h                    |   45 ++
 include/linux/dm-bht.h                    |  166 +++++
 8 files changed, 1980 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/device-mapper/dm-bht.txt
 create mode 100644 Documentation/device-mapper/dm-verity.txt
 create mode 100644 drivers/md/dm-bht.c
 create mode 100644 drivers/md/dm-verity.c
 create mode 100644 drivers/md/dm-verity.h
 create mode 100644 include/linux/dm-bht.h
Documentation/device-mapper/dm-bht.txt new file +59 -0
new file mode 100644
index 0000000..21d929f
@@ -0,0 +1,59 @@
+dm-bht
+======
+
+dm-bht provides a block hash tree implementation.  The use of dm-bht allows
+for integrity checking of a given block device without reading the entire
+set of blocks into memory before use.
+
+In particular, dm-bht supplies an interface for creating and verifying a tree
+of cryptographic digests with any algorithm supported by the kernel crypto API.
+
+The `verity' target is the motivating example.
+
+
+Theory of operation
+===================
+
+dm-bht is logically comprised of multiple nodes organized in a tree-like
+structure.  Each node in the tree is a cryptographic hash.  If it is a leaf
+node, the hash is of some block data on disk.  If it is an intermediary node,
+then the hash is of a number of child nodes.
+
+dm-bht has a given depth starting at 1 (ignoring the root node).  Each level in
+the tree is concretely made up of dm_bht_entry structs.  Each entry in the tree
+is a collection of neighboring nodes that fit in one page-sized block.  The
+number is determined based on PAGE_SIZE and the size of the selected
+cryptographic digest algorithm.  The hashes are linearly ordered in this entry
+and any unaligned trailing space is ignored but included when calculating the
+parent node.
+
+The tree looks something like:
+
+alg= sha256, num_blocks = 32767
+                                 [   root    ]
+                                /    . . .    \
+                     [entry_0]                 [entry_1]
+                    /  . . .  \                 . . .   \
+         [entry_0_0]   . . .  [entry_0_127]    . . . .  [entry_1_127]
+           / ... \             /   . . .  \             /           \
+     blk_0 ... blk_127  blk_16256   blk_16383      blk_32640 . . . blk_32767
+
+root is treated independently from the depth and the blocks are expected to
+be hashed and supplied to the dm-bht.  hash blocks that make up the entry
+contents are expected to be read from disk.
+
+dm-bht does not handle I/O directly but instead expects the consumer to
+supply callbacks.  The read callback will always receive a page-align value
+to pass to the block device layer to read in a hash value.
+
+Usage
+=====
+
+The API provides mechanisms for reading and verifying a tree. When reading, all
+required data for the hash tree should be populated for a block before
+attempting a verify.  This can be done by calling dm_bht_populate().  When all
+data is ready, a call to dm_bht_verify_block() with the expected hash value will
+perform both the direct block hash check and the hashes of the parent and
+neighboring nodes where needed to ensure validity up to the root hash.  Note,
+dm_bht_set_root_hexdigest() should be called before any verification attempts
+occur.
Documentation/device-mapper/dm-verity.txt new file +76 -0

76 lines not on this page · raw message

drivers/md/Kconfig +30 -0
index faa4741..3cdf95c 100644
@@ -370,4 +370,34 @@ config DM_FLAKEY
        ---help---
          A target that intermittently fails I/O for debugging purposes.
 
+config DM_BHT
+        tristate "Block hash tree support"
+        select CRYPTO
+        select CRYPTO_HASH
+        ---help---
+          Include support for device-mapper devices to use a block hash
+          tree for managing data integrity checks in a scalable way.
+
+          Targets that use this functionality should include it
+          automatically.
+
+          If unsure, say N.
+
+config DM_VERITY
+        tristate "Verity target support"
+        depends on BLK_DEV_DM
+        select DM_BHT
+        select CRYPTO
+        select CRYPTO_HASH
+        ---help---
+          This device-mapper target allows you to create a device that
+          transparently integrity checks the data on it. You'll need to
+          activate the digests you're going to use in the cryptoapi
+          configuration.
+
+          To compile this code as a module, choose M here: the module will
+          be called dm-verity.
+
+          If unsure, say N.
+
 endif # MD
drivers/md/Makefile +2 -0
index 046860c..c069953 100644
@@ -39,6 +39,8 @@ obj-$(CONFIG_DM_SNAPSHOT)	+= dm-snapshot.o
 obj-$(CONFIG_DM_PERSISTENT_DATA)	+= persistent-data/
 obj-$(CONFIG_DM_MIRROR)		+= dm-mirror.o dm-log.o dm-region-hash.o
 obj-$(CONFIG_DM_LOG_USERSPACE)	+= dm-log-userspace.o
+obj-$(CONFIG_DM_BHT)            += dm-bht.o
+obj-$(CONFIG_DM_VERITY)         += dm-verity.o
 obj-$(CONFIG_DM_ZERO)		+= dm-zero.o
 obj-$(CONFIG_DM_RAID)	+= dm-raid.o
 obj-$(CONFIG_DM_THIN_PROVISIONING)	+= dm-thin-pool.o
drivers/md/dm-bht.c new file +559 -0

559 lines not on this page · raw message

drivers/md/dm-verity.c new file +1043 -0

1,043 lines not on this page · raw message

drivers/md/dm-verity.h new file +45 -0

45 lines not on this page · raw message

include/linux/dm-bht.h new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
1.7.3.1
#4 lore raw

drivers/md/dm-bht.c line 94

Quoted 10 lines · Mandeep Singh Baines
drivers/md/dm-bht.c new file +9 -0
+/**
+ * dm_bht_compute_hash: hashes a page of data
+ */
+static int dm_bht_compute_hash(struct dm_bht *bht, struct page *pg,
+			       unsigned int offset, u8 *digest)
+{
+	struct shash_desc *hash_desc = bht->hash_desc[smp_processor_id()];
+	void *data;
+	int err;

You don't need to have one shash_desc per cpu. As the shash interface is synchronous, you could either allocate a new shash_desc with every call to dm_bht_compute_hash, or just place the shash_desc with the context to the stack of this function. The easiest thing would be to add the shash transformation direct to dm_bht and then something like

        struct {
                struct shash_desc desc;        
                char ctx[crypto_shash_descsize(bht->tfm)];                                                
        } hash_desc;

	hash_desc.desc.tfm = bmt->tfm;

The intermediate and final digest values are stored to hash_desc.ctx. This is local, hence it's reentrant.

Quoted 26 lines
drivers/md/dm-bht.c new file +26 -0
+
+	/* Note, this is synchronous. */
+	if (crypto_shash_init(hash_desc)) {
+		DMCRIT("failed to reinitialize crypto hash (proc:%d)",
+			smp_processor_id());
+		return -EINVAL;
+	}
+	data = kmap_atomic(pg);
+	err = crypto_shash_update(hash_desc, data + offset, PAGE_SIZE);
+	kunmap_atomic(data);
+	if (err) {
+		DMCRIT("crypto_hash_update failed");
+		return -EINVAL;
+	}
+	if (crypto_shash_update(hash_desc, bht->salt, sizeof(bht->salt))) {
+		DMCRIT("crypto_hash_update failed");
+		return -EINVAL;
+	}
+	if (crypto_shash_final(hash_desc, digest)) {
+		DMCRIT("crypto_hash_final failed");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+

[snip]

Quoted 6 lines

+ * TODO(wad): All hash storage memory is pre-allocated and freed once an
+ * entire branch has been verified.
+ */

include/linux/dm-bht.h new file +3 -0
+struct dm_bht {
+	/* Configured values */
+	int depth;  /* Depth of the tree including the root */

include/linux/dm-bht.h line 95

Quoted 10 lines
include/linux/dm-bht.h new file +10 -0
+	unsigned int block_count;  /* Number of blocks hashed */
+	unsigned int block_size;  /* Size of a hash block */
+	char hash_alg[CRYPTO_MAX_ALG_NAME];
+	unsigned char salt[DM_BHT_SALT_SIZE];
+
+	/* Computed values */
+	unsigned int node_count;  /* Data size (in hashes) for each entry */
+	unsigned int node_count_shift;  /* first bit set - 1 */
+	/* There is one per CPU so that verified can be simultaneous. */
+	struct shash_desc *hash_desc[NR_CPUS];  /* Container for the hash alg */

As mentioned above, you don't need to have one shash_desc per cpu. Replace this with a 'struct crypto_shash *tfm;' and add this transformation to the local hash_desc.

#5 lore raw
Quoted 25 lines · Steffen Klassert

drivers/md/dm-bht.c line 94

Quoted 10 lines · Mandeep Singh Baines
drivers/md/dm-bht.c new file +9 -0
+/**
+ * dm_bht_compute_hash: hashes a page of data
+ */
+static int dm_bht_compute_hash(struct dm_bht *bht, struct page *pg,
+			       unsigned int offset, u8 *digest)
+{
+	struct shash_desc *hash_desc = bht->hash_desc[smp_processor_id()];
+	void *data;
+	int err;

You don't need to have one shash_desc per cpu. As the shash interface is synchronous, you could either allocate a new shash_desc with every call to dm_bht_compute_hash, or just place the shash_desc with the context to the stack of this function. The easiest thing would be to add the shash transformation direct to dm_bht and then something like

        struct {
                struct shash_desc desc;        
                char ctx[crypto_shash_descsize(bht->tfm)];                                                
        } hash_desc;

	hash_desc.desc.tfm = bmt->tfm;
Quoted 3 lines

The intermediate and final digest values are stored to hash_desc.ctx. This is local, hence it's reentrant.

Hi Steffen,

We use a hash per CPU to avoid allocating and freeing a context per computation. We could allocate the context on the stack as you suggest. However, we were worried about overflowing the stack. The context can be quite large (close to 200 bytes for sha512).

We use a hash per CPU because there can be at most one hash in progress on each CPU at any one time.

Regards,
Mandeep

Quoted 50 lines
Quoted 26 lines
drivers/md/dm-bht.c new file +26 -0
+
+	/* Note, this is synchronous. */
+	if (crypto_shash_init(hash_desc)) {
+		DMCRIT("failed to reinitialize crypto hash (proc:%d)",
+			smp_processor_id());
+		return -EINVAL;
+	}
+	data = kmap_atomic(pg);
+	err = crypto_shash_update(hash_desc, data + offset, PAGE_SIZE);
+	kunmap_atomic(data);
+	if (err) {
+		DMCRIT("crypto_hash_update failed");
+		return -EINVAL;
+	}
+	if (crypto_shash_update(hash_desc, bht->salt, sizeof(bht->salt))) {
+		DMCRIT("crypto_hash_update failed");
+		return -EINVAL;
+	}
+	if (crypto_shash_final(hash_desc, digest)) {
+		DMCRIT("crypto_hash_final failed");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+

[snip]

Quoted 6 lines

+ * TODO(wad): All hash storage memory is pre-allocated and freed once an
+ * entire branch has been verified.
+ */

include/linux/dm-bht.h new file +3 -0
+struct dm_bht {
+	/* Configured values */
+	int depth;  /* Depth of the tree including the root */

include/linux/dm-bht.h line 95

Quoted 10 lines
include/linux/dm-bht.h new file +10 -0
+	unsigned int block_count;  /* Number of blocks hashed */
+	unsigned int block_size;  /* Size of a hash block */
+	char hash_alg[CRYPTO_MAX_ALG_NAME];
+	unsigned char salt[DM_BHT_SALT_SIZE];
+
+	/* Computed values */
+	unsigned int node_count;  /* Data size (in hashes) for each entry */
+	unsigned int node_count_shift;  /* first bit set - 1 */
+	/* There is one per CPU so that verified can be simultaneous. */
+	struct shash_desc *hash_desc[NR_CPUS];  /* Container for the hash alg */

As mentioned above, you don't need to have one shash_desc per cpu. Replace this with a 'struct crypto_shash *tfm;' and add this transformation to the local hash_desc.

#6 lore raw

Hi.

Quoted 7 lines · Mandeep Singh Baines

Hi Steffen,

We use a hash per CPU to avoid allocating and freeing a context per computation. We could allocate the context on the stack as you suggest. However, we were worried about overflowing the stack. The context can be quite large (close to 200 bytes for sha512).

Indeed, but preallocating on percpu basis can be also quite large on bigsmp machines.

Quoted 4 lines

We use a hash per CPU because there can be at most one hash in progress on each CPU at any one time.

Ok, this is the case because you use a workqueue and this processes at most one work item on each cpu the time.

On another note, you might want to use ahash here. Most crypto hardware accelerators need to use asynchronous ciphers. These can't be used if you use the synchronous interface.

And at the latest when you switch to ahash, you need more than one preallocated ahash request on each cpu, as there can be many ahash requests submitted on each cpu :)

#7 lore raw

Hi Steffen,

Sorry for the delayed repsonse.

Quoted 6 lines · Steffen Klassert

Hi.

Quoted 3 lines · Mandeep Singh Baines

Hi Steffen,

Quoted 8 lines
Quoted 4 lines

We use a hash per CPU to avoid allocating and freeing a context per computation. We could allocate the context on the stack as you suggest. However, we were worried about overflowing the stack. The context can be quite large (close to 200 bytes for sha512).

Indeed, but preallocating on percpu basis can be also quite large on bigsmp machines.

Yes, you'll consume NUM_CPUS*sizeof(context) but I'm not sure that its a lot of memory relative to the total amount of memory you'd see on a big SMP/NUMA system.

Quoted 5 lines
Quoted 4 lines

We use a hash per CPU because there can be at most one hash in progress on each CPU at any one time.

Quoted 7 lines

Ok, this is the case because you use a workqueue and this processes at most one work item on each cpu the time.

On another note, you might want to use ahash here. Most crypto hardware accelerators need to use asynchronous ciphers. These can't be used if you use the synchronous interface.

We definitely want to look at ahash but I'd like to get the baseline functionality in and then iterate. Do you think it would be cool if we deferred looking into ahash until later?

Quoted 4 lines

And at the latest when you switch to ahash, you need more than one preallocated ahash request on each cpu, as there can be many ahash requests submitted on each cpu :)

Thanks much for all the feedback.

Regards,
Mandeep