Skip to contentSkip to conversation

Add option to write Merkle tree to a file

PATCH In review Eric Biggers 2 participants 11 messages Jun 3, 2021 – Jun 9, 2021

Subsystems (2)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

cover Add option to write Merkle tree to a file

Make 'fsverity digest' and 'fsverity sign' support writing the Merkle tree and fs-verity descriptor to files, using new options '--out-merkle-tree=FILE' and '--out-descriptor=FILE'.

Normally these new options aren't useful, but they can be needed in cases where the fs-verity metadata needs to be consumed by something other than one of the native Linux kernel implementations of fs-verity.

This is different from 'fsverity dump_metadata' in that 'fsverity dump_metadata' only works on a file with fs-verity enabled, whereas these new options are for the userspace file digest computation.

Supporting this required adding some optional callbacks to libfsverity_compute_digest().

Eric Biggers (4):
lib/compute_digest: add callbacks for getting the verity metadata programs/test_compute_digest: test the metadata callbacks programs/utils: add full_pwrite() and preallocate_file() programs/fsverity: add --out-merkle-tree and --out-descriptor options

 include/libfsverity.h          |  46 +++++++++++-
 lib/compute_digest.c           | 130 +++++++++++++++++++++++++++-----
 programs/cmd_digest.c          |   7 +-
 programs/cmd_sign.c            |  17 +++--
 programs/fsverity.c            |  88 +++++++++++++++++++++-
 programs/fsverity.h            |   4 +-
 programs/test_compute_digest.c | 133 +++++++++++++++++++++++++++++++++
 programs/utils.c               |  59 +++++++++++++++
 programs/utils.h               |   3 +
 9 files changed, 458 insertions(+), 29 deletions(-)

base-commit: cf8fa5e5a7ac5b3b2dbfcc87e5dbd5f984c2d83a

Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/4 lib/compute_digest: add callbacks for getting the verity metadata

From: Eric Biggers <[email protected]>

Allow callers of libfsverity_compute_digest() to provide callback functions which get passed the Merkle tree and fs-verity descriptor after they are calculated.

This will allow adding options to 'fsverity digest' and 'fsverity sign' which cause this metadata to be dumped to files. Normally this isn't useful, but this can be needed in cases where the fs-verity metadata needs to be consumed by something other than one of the native Linux kernel implementations of fs-verity.

---
 include/libfsverity.h |  46 ++++++++++++++-
 lib/compute_digest.c  | 130 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 156 insertions(+), 20 deletions(-)
include/libfsverity.h +44 -2
index 6c42e5e..c2c6c18 100644
@@ -61,8 +61,18 @@ struct libfsverity_merkle_tree_params {
 	/** @reserved1: must be 0 */
 	uint64_t reserved1[8];
 
+	/**
+	 * @metadata_callbacks: if non-NULL, this gives a set of callback
+	 * functions to which libfsverity_compute_digest() will pass the Merkle
+	 * tree blocks and fs-verity descriptor after they are computed.
+	 * Normally this isn't useful, but this can be needed in rare cases
+	 * where the metadata needs to be consumed by something other than one
+	 * of the native Linux kernel implementations of fs-verity.
+	 */
+	const struct libfsverity_metadata_callbacks *metadata_callbacks;
+
 	/** @reserved2: must be 0 */
-	uintptr_t reserved2[8];
+	uintptr_t reserved2[7];
 };
 
 struct libfsverity_digest {
@@ -78,6 +88,37 @@ struct libfsverity_signature_params {
 	uintptr_t reserved2[8];		/* must be 0 */
 };
 
+struct libfsverity_metadata_callbacks {
+
+	/** @ctx: context passed to the below callbacks (opaque to library) */
+	void *ctx;
+
+	/**
+	 * @merkle_tree_size: if non-NULL, called with the total size of the
+	 * Merkle tree in bytes, prior to any call to @merkle_tree_block.  Must
+	 * return 0 on success, or a negative errno value on failure.
+	 */
+	int (*merkle_tree_size)(void *ctx, uint64_t size);
+
+	/**
+	 * @merkle_tree_block: if non-NULL, called with each block of the
+	 * Merkle tree after it is computed.  The offset is the offset in bytes
+	 * to the block within the Merkle tree, using the Merkle tree layout
+	 * used by FS_IOC_READ_VERITY_METADATA.  The offsets won't necessarily
+	 * be in increasing order.  Must return 0 on success, or a negative
+	 * errno value on failure.
+	 */
+	int (*merkle_tree_block)(void *ctx, const void *block, size_t size,
+				 uint64_t offset);
+
+	/**
+	 * @descriptor: if non-NULL, called with the fs-verity descriptor after
+	 * it is computed.  Must return 0 on success, or a negative errno value
+	 * on failure.
+	 */
+	int (*descriptor)(void *ctx, const void *descriptor, size_t size);
+};
+
 /*
  * libfsverity_read_fn_t - callback that incrementally provides a file's data
  * @fd: the user-provided "file descriptor" (opaque to library)
@@ -101,7 +142,8 @@ typedef int (*libfsverity_read_fn_t)(void *fd, void *buf, size_t count);
  *
  * Returns:
  * * 0 for success, -EINVAL for invalid input arguments, -ENOMEM if libfsverity
- *   failed to allocate memory, or an error returned by @read_fn.
+ *   failed to allocate memory, or an error returned by @read_fn or by one of
+ *   the @params->metadata_callbacks.
  * * digest_ret returns a pointer to the digest on success. The digest object
  *   is allocated by libfsverity and must be freed by the caller using free().
  */
lib/compute_digest.c +112 -18

192 lines not on this page · raw message

Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH 2/4 programs/test_compute_digest: test the metadata callbacks

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Test that the libfsverity_metadata_callbacks support seems to be working correctly.

---
 programs/test_compute_digest.c | 133 +++++++++++++++++++++++++++++++++
 1 file changed, 133 insertions(+)
programs/test_compute_digest.c +133 -0
index e7f2645..67266fa 100644
@@ -13,6 +13,7 @@
 
 #include <ctype.h>
 #include <inttypes.h>
+#include <openssl/sha.h>
 
 struct mem_file {
 	u8 *data;
@@ -37,6 +38,13 @@ static int error_read_fn(void *fd __attribute__((unused)),
 	return -EIO;
 }
 
+static int zeroes_read_fn(void *fd __attribute__((unused)),
+			  void *buf, size_t count)
+{
+	memset(buf, 0, count);
+	return 0;
+}
+
 static const struct test_case {
 	u32 hash_algorithm;
 	u32 block_size;
@@ -249,6 +257,130 @@ static void test_invalid_params(void)
 	ASSERT(d == NULL);
 }
 
+static struct {
+	u64 merkle_tree_size;
+	u64 merkle_tree_block;
+	u64 descriptor;
+} metadata_callback_counts;
+
+static int handle_merkle_tree_size(void *ctx, u64 size)
+{
+	metadata_callback_counts.merkle_tree_size++;
+
+	/* Test that the ctx argument is passed through correctly. */
+	ASSERT(ctx == (void *)1);
+
+	/* Test that the expected Merkle tree size is reported. */
+	ASSERT(size == 5 * 1024);
+	return 0;
+}
+
+static int handle_merkle_tree_block(void *ctx, const void *block, size_t size,
+				    u64 offset)
+{
+	u8 digest[SHA256_DIGEST_LENGTH];
+	u64 count = metadata_callback_counts.merkle_tree_block++;
+	const char *expected_digest;
+
+	/* Test that ->merkle_tree_size() was called first. */
+	ASSERT(metadata_callback_counts.merkle_tree_size == 1);
+
+	/* Test that the ctx argument is passed through correctly. */
+	ASSERT(ctx == (void *)1);
+
+	/*
+	 * Test that this Merkle tree block has the expected size, offset, and
+	 * contents.  The 4 blocks at "level 0" should be reported first, in
+	 * order; then the 1 block at "level 1" should be reported last (but the
+	 * level 1 block should have the smallest offset).
+	 */
+	ASSERT(size == 1024);
+	SHA256(block, size, digest);
+	if (count == 4) {
+		/* 1 block at level 1 */
+		ASSERT(offset == 0);
+		expected_digest = "\x68\xc5\x38\xe1\x19\x58\xd6\x5d"
+				  "\x68\xb6\xfe\x8e\x9f\xb8\xcc\xab"
+				  "\xec\xfd\x92\x8b\x01\xd0\x63\x44"
+				  "\xe2\x23\xed\x41\xdd\xc4\x54\x4a";
+	} else {
+		/* 4 blocks at level 0 */
+		ASSERT(offset == 1024 + (count * 1024));
+		if (count < 3) {
+			expected_digest = "\xf7\x89\xba\xab\x53\x85\x9f\xaf"
+					  "\x36\xd6\xd7\x5d\x10\x42\x06\x42"
+					  "\x94\x20\x2d\x6e\x13\xe7\x71\x6f"
+					  "\x39\x4f\xba\x43\x4c\xcc\x49\x86";
+		} else {
+			expected_digest = "\x00\xfe\xd0\x3c\x5d\x6e\xab\x21"
+					  "\x31\x43\xf3\xd9\x6a\x5c\xa3\x1c"
+					  "\x2b\x89\xf5\x68\x4e\x6c\x8e\x07"
+					  "\x87\x3e\x5e\x97\x65\x17\xb4\x8f";
+		}
+	}
+	ASSERT(!memcmp(digest, expected_digest, SHA256_DIGEST_LENGTH));
+	return 0;
+}
+
+static const u8 expected_file_digest[SHA256_DIGEST_LENGTH] =
+	"\x09\xcb\xba\xee\xd2\xa0\x4c\x2d\xa2\x42\xc1\x0e\x15\x68\xd9\x6f"
+	"\x35\x8a\x16\xaa\x1e\xbe\x8c\xf0\x28\x61\x20\xc1\x3c\x93\x66\xd1";
+
+static int handle_descriptor(void *ctx, const void *descriptor, size_t size)
+{
+	u8 digest[SHA256_DIGEST_LENGTH];
+
+	metadata_callback_counts.descriptor++;
+	/* Test that the ctx argument is passed through correctly. */
+	ASSERT(ctx == (void *)1);
+
+	/* Test that the fs-verity descriptor is reported correctly. */
+	ASSERT(size == 256);
+	SHA256(descriptor, size, digest);
+	ASSERT(!memcmp(digest, expected_file_digest, SHA256_DIGEST_LENGTH));
+	return 0;
+}
+
+static const struct libfsverity_metadata_callbacks metadata_callbacks = {
+	.ctx = (void *)1, /* arbitrary value for testing purposes */
+	.merkle_tree_size = handle_merkle_tree_size,
+	.merkle_tree_block = handle_merkle_tree_block,
+	.descriptor = handle_descriptor,
+};
+
+/* Test that the libfsverity_metadata_callbacks work correctly. */
+static void test_metadata_callbacks(void)
+{
+	/*
+	 * For a useful test, we want a file whose Merkle tree will have at
+	 * least 2 levels (this one will have exactly 2).  The contents of the
+	 * file aren't too important.
+	 */
+	struct libfsverity_merkle_tree_params params = {
+		.version = 1,
+		.hash_algorithm = FS_VERITY_HASH_ALG_SHA256,
+		.block_size = 1024,
+		.file_size = 100000,
+		.metadata_callbacks = &metadata_callbacks,
+	};
+	struct libfsverity_digest *d;
+
+	ASSERT(libfsverity_compute_digest(NULL, zeroes_read_fn,
+					  &params, &d) == 0);
+
+	/* Test that the callbacks were called the correct number of times. */
+	ASSERT(metadata_callback_counts.merkle_tree_size == 1);
+	ASSERT(metadata_callback_counts.merkle_tree_block == 5);
+	ASSERT(metadata_callback_counts.descriptor == 1);
+
+	/* Test that the computed file digest is as expected. */
+	ASSERT(d->digest_algorithm == FS_VERITY_HASH_ALG_SHA256);
+	ASSERT(d->digest_size == SHA256_DIGEST_LENGTH);
+	ASSERT(!memcmp(d->digest, expected_file_digest, SHA256_DIGEST_LENGTH));
+
+	free(d);
+}
+
 int main(int argc, char *argv[])
 {
 	const bool update = (argc == 2 && !strcmp(argv[1], "--update"));
@@ -305,6 +437,7 @@ int main(int argc, char *argv[])
 	}
 
 	test_invalid_params();
+	test_metadata_callbacks();
 	printf("test_compute_digest passed\n");
 	return 0;
 }
Signature · 1 line
2.31.1
author maintainer
#3 lore raw

PATCH 3/4 programs/utils: add full_pwrite() and preallocate_file()

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

These helper functions will be used by the implementation of the --out-merkle-tree option for 'fsverity digest' and 'fsverity sign'.

---
 programs/utils.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 programs/utils.h |  3 +++
 2 files changed, 62 insertions(+)
programs/utils.c +59 -0
index ce19b57..116eb95 100644
@@ -13,10 +13,14 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <inttypes.h>
 #include <limits.h>
 #include <stdarg.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#ifdef _WIN32
+#  include <windows.h>
+#endif
 
 /* ========== Memory allocation ========== */
 
@@ -126,6 +130,26 @@ bool get_file_size(struct filedes *file, u64 *size_ret)
 	return true;
 }
 
+bool preallocate_file(struct filedes *file, u64 size)
+{
+	int res;
+
+	if (size == 0)
+		return true;
+#ifdef _WIN32
+	/* Not exactly the same as posix_fallocate(), but good enough... */
+	res = _chsize_s(file->fd, size);
+#else
+	res = posix_fallocate(file->fd, 0, size);
+#endif
+	if (res != 0) {
+		error_msg_errno("preallocating %" PRIu64 "-byte file '%s'",
+				size, file->name);
+		return false;
+	}
+	return true;
+}
+
 bool full_read(struct filedes *file, void *buf, size_t count)
 {
 	while (count) {
@@ -160,6 +184,41 @@ bool full_write(struct filedes *file, const void *buf, size_t count)
 	return true;
 }
 
+static int raw_pwrite(int fd, const void *buf, int count, u64 offset)
+{
+#ifdef _WIN32
+	HANDLE h = (HANDLE)_get_osfhandle(fd);
+	OVERLAPPED pos = { .Offset = offset, .OffsetHigh = offset >> 32 };
+	DWORD written = 0;
+
+	/* Not exactly the same as pwrite(), but good enough... */
+	if (!WriteFile(h, buf, count, &written, &pos)) {
+		errno = EIO;
+		return -1;
+	}
+	return written;
+#else
+	return pwrite(fd, buf, count, offset);
+#endif
+}
+
+bool full_pwrite(struct filedes *file, const void *buf, size_t count,
+		 u64 offset)
+{
+	while (count) {
+		int n = raw_pwrite(file->fd, buf, min(count, INT_MAX), offset);
+
+		if (n < 0) {
+			error_msg_errno("writing to '%s'", file->name);
+			return false;
+		}
+		buf += n;
+		count -= n;
+		offset += n;
+	}
+	return true;
+}
+
 bool filedes_close(struct filedes *file)
 {
 	int res;
programs/utils.h +3 -0
index ab5005f..9a5c97a 100644
@@ -40,8 +40,11 @@ struct filedes {
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode);
 bool get_file_size(struct filedes *file, u64 *size_ret);
+bool preallocate_file(struct filedes *file, u64 size);
 bool full_read(struct filedes *file, void *buf, size_t count);
 bool full_write(struct filedes *file, const void *buf, size_t count);
+bool full_pwrite(struct filedes *file, const void *buf, size_t count,
+		 u64 offset);
 bool filedes_close(struct filedes *file);
 int read_callback(void *file, void *buf, size_t count);
 
Signature · 1 line
2.31.1
#4 lore raw

Re: [fsverity-utils PATCH 3/4] programs/utils: add full_pwrite() and preallocate_file()

Quoted 81 lines · Eric Biggers

From: Eric Biggers <[email protected]>

These helper functions will be used by the implementation of the --out-merkle-tree option for 'fsverity digest' and 'fsverity sign'.

---
 programs/utils.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 programs/utils.h |  3 +++
 2 files changed, 62 insertions(+)
programs/utils.c +43 -0
index ce19b57..116eb95 100644
@@ -13,10 +13,14 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <inttypes.h>
 #include <limits.h>
 #include <stdarg.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#ifdef _WIN32
+#  include <windows.h>
+#endif
 
 /* ========== Memory allocation ========== */
 
@@ -126,6 +130,26 @@ bool get_file_size(struct filedes *file, u64 *size_ret)
        return true;
 }
 
+bool preallocate_file(struct filedes *file, u64 size)
+{
+       int res;
+
+       if (size == 0)
+               return true;
+#ifdef _WIN32
+       /* Not exactly the same as posix_fallocate(), but good enough... */
+       res = _chsize_s(file->fd, size);
+#else
+       res = posix_fallocate(file->fd, 0, size);
+#endif
+       if (res != 0) {
+               error_msg_errno("preallocating %" PRIu64 "-byte file '%s'",
+                               size, file->name);
+               return false;
+       }
+       return true;
+}
+
 bool full_read(struct filedes *file, void *buf, size_t count)
 {
        while (count) {
@@ -160,6 +184,41 @@ bool full_write(struct filedes *file, const void *buf, size_t count)
        return true;
 }
 
+static int raw_pwrite(int fd, const void *buf, int count, u64 offset)
+{
+#ifdef _WIN32
+       HANDLE h = (HANDLE)_get_osfhandle(fd);
+       OVERLAPPED pos = { .Offset = offset, .OffsetHigh = offset >> 32 };
+       DWORD written = 0;
+
+       /* Not exactly the same as pwrite(), but good enough... */
+       if (!WriteFile(h, buf, count, &written, &pos)) {
+               errno = EIO;
+               return -1;
+       }
+       return written;
+#else
+       return pwrite(fd, buf, count, offset);
+#endif
+}
+
+bool full_pwrite(struct filedes *file, const void *buf, size_t count,

programs/utils.c bool full_write(struct filedes *file, const void *buf, size_t count) · line 215

Quoted 10 lines
programs/utils.c +10 -0
+                u64 offset)
+{
+       while (count) {
+               int n = raw_pwrite(file->fd, buf, min(count, INT_MAX), offset);
+
+               if (n < 0) {
+                       error_msg_errno("writing to '%s'", file->name);
+                       return false;
+               }
+               buf += n;

I think this pointer arithmetic is not portable? Consider changing the type of buf to "const char*".

Quoted 28 lines
programs/utils.c +6 -0
+               count -= n;
+               offset += n;
+       }
+       return true;
+}
+
 bool filedes_close(struct filedes *file)
 {
        int res;
programs/utils.h +3 -1
index ab5005f..9a5c97a 100644
@@ -40,8 +40,11 @@ struct filedes {
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode);
 bool get_file_size(struct filedes *file, u64 *size_ret);
+bool preallocate_file(struct filedes *file, u64 size);
 bool full_read(struct filedes *file, void *buf, size_t count);
 bool full_write(struct filedes *file, const void *buf, size_t count);
+bool full_pwrite(struct filedes *file, const void *buf, size_t count,
+                u64 offset);
 bool filedes_close(struct filedes *file);
 int read_callback(void *file, void *buf, size_t count);
 
--

2.31.1

author maintainer
#5 lore raw

Re: [fsverity-utils PATCH 3/4] programs/utils: add full_pwrite() and preallocate_file()

Quoted 6 lines · Victor Hsieh
Quoted 6 lines
programs/utils.c +6 -0
+
+bool full_pwrite(struct filedes *file, const void *buf, size_t count,
+                u64 offset)
+{
+       while (count) {
+               int n = raw_pwrite(file->fd, buf, min(count, INT_MAX), offset);
Quoted 9 lines

programs/utils.c bool full_write(struct filedes *file, const void *buf, size_t count) · line 215

Quoted 6 lines
programs/utils.c +6 -0
+
+               if (n < 0) {
+                       error_msg_errno("writing to '%s'", file->name);
+                       return false;
+               }
+               buf += n;

I think this pointer arithmetic is not portable? Consider changing the type of buf to "const char*".

fsverity-utils is already using void pointer arithmetic elsewhere, for example in full_read() and full_write().

I am allowing the use of some gcc/clang extensions which are widely used, including in the Linux kernel (which fsverity-utils is generally trying to follow the coding style of), and are annoying to do without. Void pointer arithmetic is one of these.

If we really needed to support someone compiling fsverity-utils with e.g. Visual Studio, we could add -Wpedantic to the compiler flags and get rid of all the gcc/clang extensions. But I don't see a reason to do that now.

- Eric

Reviewed-by
#6 lore raw

Re: [fsverity-utils PATCH 3/4] programs/utils: add full_pwrite() and preallocate_file()

Quoted 21 lines · Eric Biggers
Quoted 6 lines · Victor Hsieh
Quoted 6 lines
programs/utils.c +6 -0
+
+bool full_pwrite(struct filedes *file, const void *buf, size_t count,
+                u64 offset)
+{
+       while (count) {
+               int n = raw_pwrite(file->fd, buf, min(count, INT_MAX), offset);
Quoted 9 lines

programs/utils.c bool full_write(struct filedes *file, const void *buf, size_t count) · line 215

Quoted 6 lines
programs/utils.c +6 -0
+
+               if (n < 0) {
+                       error_msg_errno("writing to '%s'", file->name);
+                       return false;
+               }
+               buf += n;

I think this pointer arithmetic is not portable? Consider changing the type of buf to "const char*".

fsverity-utils is already using void pointer arithmetic elsewhere, for example in full_read() and full_write().

Quoted 8 lines

I am allowing the use of some gcc/clang extensions which are widely used, including in the Linux kernel (which fsverity-utils is generally trying to follow the coding style of), and are annoying to do without. Void pointer arithmetic is one of these.

If we really needed to support someone compiling fsverity-utils with e.g. Visual Studio, we could add -Wpedantic to the compiler flags and get rid of all the gcc/clang extensions. But I don't see a reason to do that now.

Yeah, that's what I was thinking since the code has #ifdef _WIN32. I'd think the
"host" side program should be more portable than the kernel itself. But if this is
already used elsewhere, no objection to keeping assuming so.

Quoted 2 lines

- Eric

author maintainer
#7 lore raw

Re: [fsverity-utils PATCH 3/4] programs/utils: add full_pwrite() and preallocate_file()

Quoted 31 lines · Victor Hsieh
Quoted 21 lines · Eric Biggers
Quoted 6 lines · Victor Hsieh
Quoted 6 lines
programs/utils.c +6 -0
+
+bool full_pwrite(struct filedes *file, const void *buf, size_t count,
+                u64 offset)
+{
+       while (count) {
+               int n = raw_pwrite(file->fd, buf, min(count, INT_MAX), offset);
Quoted 9 lines

programs/utils.c bool full_write(struct filedes *file, const void *buf, size_t count) · line 215

Quoted 6 lines
programs/utils.c +6 -0
+
+               if (n < 0) {
+                       error_msg_errno("writing to '%s'", file->name);
+                       return false;
+               }
+               buf += n;

I think this pointer arithmetic is not portable? Consider changing the type of buf to "const char*".

fsverity-utils is already using void pointer arithmetic elsewhere, for example in full_read() and full_write().

Quoted 8 lines

I am allowing the use of some gcc/clang extensions which are widely used, including in the Linux kernel (which fsverity-utils is generally trying to follow the coding style of), and are annoying to do without. Void pointer arithmetic is one of these.

If we really needed to support someone compiling fsverity-utils with e.g. Visual Studio, we could add -Wpedantic to the compiler flags and get rid of all the gcc/clang extensions. But I don't see a reason to do that now.

Quoted 8 lines

Yeah, that's what I was thinking since the code has #ifdef _WIN32. I'd think the
"host" side program should be more portable than the kernel itself. But if this is
already used elsewhere, no objection to keeping assuming so.

Windows builds are supported with Mingw-w64, not with Visual Studio. So that isn't an issue.

- Eric

author maintainer
#8 lore raw

PATCH 4/4 programs/fsverity: add --out-merkle-tree and --out-descriptor options

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Make 'fsverity digest' and 'fsverity sign' support writing the Merkle tree and fs-verity descriptor to files, using new options '--out-merkle-tree=FILE' and '--out-descriptor=FILE'.

Normally these new options aren't useful, but they can be needed in cases where the fs-verity metadata needs to be consumed by something other than one of the native Linux kernel implementations of fs-verity.

This is different from 'fsverity dump_metadata' in that 'fsverity dump_metadata' only works on a file with fs-verity enabled, whereas these new options are for the userspace file digest computation.

---
 programs/cmd_digest.c |  7 +++-
 programs/cmd_sign.c   | 17 ++++++---
 programs/fsverity.c   | 88 ++++++++++++++++++++++++++++++++++++++++++-
 programs/fsverity.h   |  4 +-
 4 files changed, 107 insertions(+), 9 deletions(-)
programs/cmd_digest.c +6 -1
index 1a3c769..fd9f4de 100644
@@ -18,6 +18,8 @@ static const struct option longopts[] = {
 	{"hash-alg",		required_argument, NULL, OPT_HASH_ALG},
 	{"block-size",		required_argument, NULL, OPT_BLOCK_SIZE},
 	{"salt",		required_argument, NULL, OPT_SALT},
+	{"out-merkle-tree",     required_argument, NULL, OPT_OUT_MERKLE_TREE},
+	{"out-descriptor",      required_argument, NULL, OPT_OUT_DESCRIPTOR},
 	{"compact",		no_argument,	   NULL, OPT_COMPACT},
 	{"for-builtin-sig",	no_argument,	   NULL, OPT_FOR_BUILTIN_SIG},
 	{NULL, 0, NULL, 0}
@@ -40,6 +42,8 @@ int fsverity_cmd_digest(const struct fsverity_command *cmd,
 		case OPT_HASH_ALG:
 		case OPT_BLOCK_SIZE:
 		case OPT_SALT:
+		case OPT_OUT_MERKLE_TREE:
+		case OPT_OUT_DESCRIPTOR:
 			if (!parse_tree_param(c, optarg, &tree_params))
 				goto out_usage;
 			break;
@@ -114,7 +118,8 @@ int fsverity_cmd_digest(const struct fsverity_command *cmd,
 	}
 	status = 0;
 out:
-	destroy_tree_params(&tree_params);
+	if (!destroy_tree_params(&tree_params) && status == 0)
+		status = 1;
 	return status;
 
 out_err:
programs/cmd_sign.c +11 -6
index 47ba6a2..81a4ddc 100644
@@ -27,11 +27,13 @@ static bool write_signature(const char *filename, const u8 *sig, u32 sig_size)
 }
 
 static const struct option longopts[] = {
-	{"hash-alg",	required_argument, NULL, OPT_HASH_ALG},
-	{"block-size",	required_argument, NULL, OPT_BLOCK_SIZE},
-	{"salt",	required_argument, NULL, OPT_SALT},
-	{"key",		required_argument, NULL, OPT_KEY},
-	{"cert",	required_argument, NULL, OPT_CERT},
+	{"hash-alg",	    required_argument, NULL, OPT_HASH_ALG},
+	{"block-size",	    required_argument, NULL, OPT_BLOCK_SIZE},
+	{"salt",	    required_argument, NULL, OPT_SALT},
+	{"out-merkle-tree", required_argument, NULL, OPT_OUT_MERKLE_TREE},
+	{"out-descriptor",  required_argument, NULL, OPT_OUT_DESCRIPTOR},
+	{"key",		    required_argument, NULL, OPT_KEY},
+	{"cert",	    required_argument, NULL, OPT_CERT},
 	{NULL, 0, NULL, 0}
 };
 
@@ -54,6 +56,8 @@ int fsverity_cmd_sign(const struct fsverity_command *cmd,
 		case OPT_HASH_ALG:
 		case OPT_BLOCK_SIZE:
 		case OPT_SALT:
+		case OPT_OUT_MERKLE_TREE:
+		case OPT_OUT_DESCRIPTOR:
 			if (!parse_tree_param(c, optarg, &tree_params))
 				goto out_usage;
 			break;
@@ -117,7 +121,8 @@ int fsverity_cmd_sign(const struct fsverity_command *cmd,
 	status = 0;
 out:
 	filedes_close(&file);
-	destroy_tree_params(&tree_params);
+	if (!destroy_tree_params(&tree_params) && status == 0)
+		status = 1;
 	free(digest);
 	free(sig);
 	return status;
programs/fsverity.c +87 -1

126 lines not on this page · raw message

programs/fsverity.h +3 -1
index 9785013..fe24087 100644
@@ -29,6 +29,8 @@ enum {
 	OPT_KEY,
 	OPT_LENGTH,
 	OPT_OFFSET,
+	OPT_OUT_DESCRIPTOR,
+	OPT_OUT_MERKLE_TREE,
 	OPT_SALT,
 	OPT_SIGNATURE,
 };
@@ -59,6 +61,6 @@ int fsverity_cmd_sign(const struct fsverity_command *cmd,
 void usage(const struct fsverity_command *cmd, FILE *fp);
 bool parse_tree_param(int opt_char, const char *arg,
 		      struct libfsverity_merkle_tree_params *params);
-void destroy_tree_params(struct libfsverity_merkle_tree_params *params);
+bool destroy_tree_params(struct libfsverity_merkle_tree_params *params);
 
 #endif /* PROGRAMS_FSVERITY_H */
Signature · 1 line
2.31.1
Reviewed-by on the v1 series
#9 lore raw
In reply to Eric Biggers

Thanks Eric!

Quoted 38 lines · Eric Biggers

Make 'fsverity digest' and 'fsverity sign' support writing the Merkle tree and fs-verity descriptor to files, using new options '--out-merkle-tree=FILE' and '--out-descriptor=FILE'.

Normally these new options aren't useful, but they can be needed in cases where the fs-verity metadata needs to be consumed by something other than one of the native Linux kernel implementations of fs-verity.

This is different from 'fsverity dump_metadata' in that 'fsverity dump_metadata' only works on a file with fs-verity enabled, whereas these new options are for the userspace file digest computation.

Supporting this required adding some optional callbacks to libfsverity_compute_digest().

Eric Biggers (4):
lib/compute_digest: add callbacks for getting the verity metadata programs/test_compute_digest: test the metadata callbacks programs/utils: add full_pwrite() and preallocate_file() programs/fsverity: add --out-merkle-tree and --out-descriptor options

 include/libfsverity.h          |  46 +++++++++++-
 lib/compute_digest.c           | 130 +++++++++++++++++++++++++++-----
 programs/cmd_digest.c          |   7 +-
 programs/cmd_sign.c            |  17 +++--
 programs/fsverity.c            |  88 +++++++++++++++++++++-
 programs/fsverity.h            |   4 +-
 programs/test_compute_digest.c | 133 +++++++++++++++++++++++++++++++++
 programs/utils.c               |  59 +++++++++++++++
 programs/utils.h               |   3 +
 9 files changed, 458 insertions(+), 29 deletions(-)

base-commit: cf8fa5e5a7ac5b3b2dbfcc87e5dbd5f984c2d83a --
2.31.1

author maintainer
#10 lore raw
In reply to Eric Biggers
Quoted 33 lines · Eric Biggers

Make 'fsverity digest' and 'fsverity sign' support writing the Merkle tree and fs-verity descriptor to files, using new options '--out-merkle-tree=FILE' and '--out-descriptor=FILE'.

Normally these new options aren't useful, but they can be needed in cases where the fs-verity metadata needs to be consumed by something other than one of the native Linux kernel implementations of fs-verity.

This is different from 'fsverity dump_metadata' in that 'fsverity dump_metadata' only works on a file with fs-verity enabled, whereas these new options are for the userspace file digest computation.

Supporting this required adding some optional callbacks to libfsverity_compute_digest().

Eric Biggers (4):
lib/compute_digest: add callbacks for getting the verity metadata programs/test_compute_digest: test the metadata callbacks programs/utils: add full_pwrite() and preallocate_file() programs/fsverity: add --out-merkle-tree and --out-descriptor options

 include/libfsverity.h          |  46 +++++++++++-
 lib/compute_digest.c           | 130 +++++++++++++++++++++++++++-----
 programs/cmd_digest.c          |   7 +-
 programs/cmd_sign.c            |  17 +++--
 programs/fsverity.c            |  88 +++++++++++++++++++++-
 programs/fsverity.h            |   4 +-
 programs/test_compute_digest.c | 133 +++++++++++++++++++++++++++++++++
 programs/utils.c               |  59 +++++++++++++++
 programs/utils.h               |   3 +
 9 files changed, 458 insertions(+), 29 deletions(-)
Quoted 1 line

base-commit: cf8fa5e5a7ac5b3b2dbfcc87e5dbd5f984c2d83a

All applied.

- Eric