libpayload/libcbfs: Fix file hash check

Fix the buffer pointer passed to cbfs_file_hash_mismatch().

Add a test case with LZ4 compression, which would catch the bug we are
fixing.

Change-Id: I36605e2dbc0423fa6743087512f2042b37c49d35
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/65149
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Paul Menzel <paulepanter@mailbox.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Yu-Ping Wu 2022-06-15 15:03:04 +08:00 committed by Julius Werner
parent e3fded3241
commit c1d7d89d48
2 changed files with 31 additions and 7 deletions

View File

@ -120,7 +120,7 @@ static size_t cbfs_load_and_decompress(size_t offset, size_t in_size, void *buff
goto out; goto out;
} }
if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification)) if (cbfs_file_hash_mismatch(load, in_size, mdata, skip_verification))
goto out; goto out;
switch (compression) { switch (compression) {

View File

@ -43,14 +43,18 @@ vb2_error_t vb2_hash_verify(const void *buf, uint32_t size, const struct vb2_has
unsigned long ulzman(const unsigned char *src, unsigned long srcn, unsigned char *dst, unsigned long ulzman(const unsigned char *src, unsigned long srcn, unsigned char *dst,
unsigned long dstn) unsigned long dstn)
{ {
fail_msg("Unexpected call to %s", __func__); size_t copy_size = MIN(srcn, dstn);
return 0; function_called();
memcpy(dst, src, copy_size);
return copy_size;
} }
size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn) size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn)
{ {
fail_msg("Unexpected call to %s", __func__); size_t copy_size = MIN(srcn, dstn);
return 0; function_called();
memcpy(dst, src, copy_size);
return copy_size;
} }
enum cb_err cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char *name, enum cb_err cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char *name,
@ -167,7 +171,7 @@ static void test_cbfs_map_no_hash(void **state)
} }
} }
static void test_cbfs_map_valid_hash(void **state) static void test_cbfs_map_valid_hash_impl(void **state, bool lz4_compressed)
{ {
void *mapping = NULL; void *mapping = NULL;
size_t size = 0; size_t size = 0;
@ -181,8 +185,17 @@ static void test_cbfs_map_valid_hash(void **state)
expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
(const union cbfs_mdata *)&file_valid_hash, (const union cbfs_mdata *)&file_valid_hash,
be32toh(file_valid_hash.header.offset)); be32toh(file_valid_hash.header.offset));
will_return(cbfs_find_attr, NULL);
if (lz4_compressed) {
struct cbfs_file_attr_compression cattr = {
.compression = htobe32(CBFS_COMPRESS_LZ4),
.decompressed_size = htobe32(TEST_DATA_1_SIZE),
};
will_return(cbfs_find_attr, &cattr);
expect_function_call(ulz4fn);
} else {
will_return(cbfs_find_attr, NULL);
}
if (CONFIG(LP_CBFS_VERIFICATION)) { if (CONFIG(LP_CBFS_VERIFICATION)) {
will_return(cbfs_file_hash, &hash); will_return(cbfs_file_hash, &hash);
@ -203,6 +216,16 @@ static void test_cbfs_map_valid_hash(void **state)
} }
} }
static void test_cbfs_map_valid_hash(void **state)
{
test_cbfs_map_valid_hash_impl(state, false);
}
static void test_cbfs_map_valid_hash_with_lz4(void **state)
{
test_cbfs_map_valid_hash_impl(state, true);
}
static void test_cbfs_map_invalid_hash(void **state) static void test_cbfs_map_invalid_hash(void **state)
{ {
void *mapping = NULL; void *mapping = NULL;
@ -240,6 +263,7 @@ int main(void)
const struct CMUnitTest tests[] = { const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(test_cbfs_map_no_hash, setup_test_cbfs), cmocka_unit_test_setup(test_cbfs_map_no_hash, setup_test_cbfs),
cmocka_unit_test_setup(test_cbfs_map_valid_hash, setup_test_cbfs), cmocka_unit_test_setup(test_cbfs_map_valid_hash, setup_test_cbfs),
cmocka_unit_test_setup(test_cbfs_map_valid_hash_with_lz4, setup_test_cbfs),
cmocka_unit_test_setup(test_cbfs_map_invalid_hash, setup_test_cbfs), cmocka_unit_test_setup(test_cbfs_map_invalid_hash, setup_test_cbfs),
}; };