Skip to content

Commit a66faad

Browse files
committed
lcfs-fsverity: fix root_hash for zero-length files
lcfs_fsverity_context_get_digest() unconditionally zero-padded and hashed a full Merkle tree block to produce root_hash, even for zero-length files. Per the fs-verity on-disk format, an empty file has zero Merkle tree blocks, so root_hash must be all-zero bytes instead. This meant composefs-info measure-file (and any direct caller of lcfs_fsverity_context_get_digest with empty input) reported a digest the kernel, fsverity-utils, and composefs-rs's independent reimplementation can never produce, even though composefs images themselves are unaffected since they never reference empty backing objects. Assisted-by: https://github.com/cgwalters/cgwalters#llms Closes: composefs#442 Signed-off-by: Colin Walters <walters@verbum.org>
1 parent a7ea6cd commit a66faad

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎libcomposefs/lcfs-fsverity.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,22 @@ void lcfs_fsverity_context_get_digest(FsVerityContext *ctx,
432432
{
433433
struct fsverity_descriptor descriptor;
434434

435-
lcfs_fsverity_context_flush_level(ctx, 0);
436-
437435
memset(&descriptor, 0, sizeof(descriptor));
438436
descriptor.version = 1;
439437
descriptor.hash_algorithm = 1;
440438
descriptor.log_blocksize = 12;
441439
descriptor.salt_size = 0;
442440
descriptor.data_size_le = htole64(ctx->file_size);
443441

444-
do_sha256(ctx, ctx->buffer[ctx->max_level], FSVERITY_BLOCK_SIZE,
445-
descriptor.root_hash);
442+
/* An empty file has zero Merkle tree blocks, so per the fs-verity
443+
* on-disk format root_hash stays all-zero (as set by the memset
444+
* above). There is nothing to flush or hash in that case. */
445+
if (ctx->file_size > 0) {
446+
lcfs_fsverity_context_flush_level(ctx, 0);
447+
448+
do_sha256(ctx, ctx->buffer[ctx->max_level], FSVERITY_BLOCK_SIZE,
449+
descriptor.root_hash);
450+
}
446451

447452
do_sha256(ctx, (uint8_t *)&descriptor, sizeof(descriptor), digest);
448453
}

‎tests/test-lcfs.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,29 @@ static void test_no_verity(void)
244244
close(tmpfd);
245245
}
246246

247+
// Regression test for https://github.com/composefs/composefs/issues/442.
248+
//
249+
// A zero-length file has no Merkle tree blocks, so the root_hash field
250+
// inside the fsverity_descriptor must be all-zero. The final digest
251+
// below is *not* that root_hash: it's SHA256(descriptor), i.e. what
252+
// lcfs_compute_fsverity_from_data() / `composefs-info measure-file`
253+
// actually returns, and what the kernel reports via `fsverity measure`
254+
// for a real empty file with fs-verity enabled.
255+
static void test_fsverity_empty_file(void)
256+
{
257+
static const uint8_t expected[LCFS_DIGEST_SIZE] = {
258+
0x3d, 0x24, 0x8c, 0xa5, 0x42, 0xa2, 0x4f, 0xc6,
259+
0x2d, 0x1c, 0x43, 0xb9, 0x16, 0xea, 0xe5, 0x01,
260+
0x68, 0x78, 0xe2, 0x53, 0x3c, 0x88, 0x23, 0x84,
261+
0x80, 0xb2, 0x61, 0x28, 0xa1, 0xf1, 0xaf, 0x95,
262+
};
263+
uint8_t digest[LCFS_DIGEST_SIZE];
264+
265+
int r = lcfs_compute_fsverity_from_data(digest, NULL, 0);
266+
assert(r == 0);
267+
assert(memcmp(digest, expected, LCFS_DIGEST_SIZE) == 0);
268+
}
269+
247270
int main(int argc, char **argv)
248271
{
249272
(void)argc;
@@ -255,4 +278,5 @@ int main(int argc, char **argv)
255278
test_xattr_addremove();
256279
test_xattr_doubleadd();
257280
test_hardlinked_whiteout_load();
281+
test_fsverity_empty_file();
258282
}

0 commit comments

Comments
 (0)