From 6f3fd6358f605c181f958ded9ef92803cec172fd Mon Sep 17 00:00:00 2001 From: Jakub Czapiga Date: Thu, 22 Jul 2021 08:52:46 +0200 Subject: tests: Add lib/cbfs-verification-test test case This commit adds test case for lib/cbfs verification mechanisms. Signed-off-by: Jakub Czapiga Change-Id: I1d8cbb1c2d0a9db3236de065428b70a9c2a66330 Reviewed-on: https://review.coreboot.org/c/coreboot/+/56601 Reviewed-by: Julius Werner Reviewed-by: Paul Fagerburg Tested-by: build bot (Jenkins) --- tests/include/tests/lib/cbfs_util.h | 102 +++++++++++++++++++ tests/lib/Makefile.inc | 32 ++++++ tests/lib/cbfs-verification-test.c | 196 ++++++++++++++++++++++++++++++++++++ tests/mock/cbfs_file_mock.c | 46 +++++++++ tests/stubs/die.c | 16 +++ 5 files changed, 392 insertions(+) create mode 100644 tests/include/tests/lib/cbfs_util.h create mode 100644 tests/lib/cbfs-verification-test.c create mode 100644 tests/mock/cbfs_file_mock.c create mode 100644 tests/stubs/die.c (limited to 'tests') diff --git a/tests/include/tests/lib/cbfs_util.h b/tests/include/tests/lib/cbfs_util.h new file mode 100644 index 000000000000..ebf2fb8b7e6d --- /dev/null +++ b/tests/include/tests/lib/cbfs_util.h @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef TESTS_LIB_CBFS_UTIL_H +#define TESTS_LIB_CBFS_UTIL_H + +#include +#include +#include + +#define BE32(be32) EMPTY_WRAP(\ + ((be32) >> 24) & 0xff, ((be32) >> 16) & 0xff, \ + ((be32) >> 8) & 0xff, ((be32) >> 0) & 0xff) + +#define BE64(be64) EMPTY_WRAP( \ + BE32(((be64) >> 32) & 0xFFFFFFFF), \ + BE32(((be64) >> 0) & 0xFFFFFFFF)) + +#define FILENAME_SIZE 16 + +struct cbfs_test_file { + struct cbfs_file header; + u8 filename[FILENAME_SIZE]; + u8 attrs_and_data[200]; +}; + +#define TEST_CBFS_CACHE_SIZE (2 * MiB) +#define TEST_MCACHE_SIZE (2 * MiB) + +#define HEADER_INITIALIZER(ftype, attr_len, file_len) { \ + .magic = CBFS_FILE_MAGIC, \ + .len = cpu_to_be32(file_len), \ + .type = cpu_to_be32(ftype), \ + .attributes_offset = \ + cpu_to_be32(attr_len ? sizeof(struct cbfs_file) + FILENAME_SIZE : 0), \ + .offset = cpu_to_be32(sizeof(struct cbfs_file) + FILENAME_SIZE + attr_len), \ +} + +#define HASH_ATTR_SIZE (offsetof(struct cbfs_file_attr_hash, hash.raw) + VB2_SHA256_DIGEST_SIZE) + +/* This macro basically does nothing but suppresses linter messages */ +#define EMPTY_WRAP(...) __VA_ARGS__ + +#define TEST_DATA_1_FILENAME "test/data/1" +#define TEST_DATA_1_SIZE sizeof((u8[]){TEST_DATA_1}) +#define TEST_DATA_1 EMPTY_WRAP( \ + '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', \ + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', \ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', \ + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', \ + '[', '\\', ']', '^', '_', '`', \ + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', \ + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') + +#define TEST_DATA_2_FILENAME "test/data/2" +#define TEST_DATA_2_SIZE sizeof((u8[]){TEST_DATA_2}) +#define TEST_DATA_2 EMPTY_WRAP( \ + 0x9d, 0xa9, 0x91, 0xac, 0x5d, 0xb2, 0x70, 0x76, 0x37, 0x94, 0x94, 0xa8, 0x8b, 0x78, \ + 0xb9, 0xaa, 0x1a, 0x8e, 0x9a, 0x16, 0xbe, 0xdc, 0x29, 0x42, 0x46, 0x58, 0xd4, 0x37, \ + 0x94, 0xca, 0x05, 0xdb, 0x54, 0xfa, 0xd8, 0x6e, 0x54, 0xd8, 0x30, 0x46, 0x5d, 0x62, \ + 0xc2, 0xce, 0xd8, 0x74, 0x60, 0xaf, 0x83, 0x8f, 0xfa, 0x97, 0xdd, 0x6e, 0xcb, 0x60, \ + 0xfa, 0xed, 0x8b, 0x55, 0x9e, 0xc1, 0xc2, 0x18, 0x4f, 0xe2, 0x28, 0x7e, 0xd7, 0x2f, \ + 0xa2, 0x86, 0xfb, 0x4d, 0x3e, 0x00, 0x5a, 0xf7, 0xc2, 0xad, 0x0e, 0xa7, 0xa2, 0xf7, \ + 0x38, 0x66, 0xe6, 0x5c, 0x76, 0x98, 0x89, 0x63, 0xeb, 0xc5, 0xf5, 0xb7, 0xa7, 0x58, \ + 0xe0, 0xf0, 0x2e, 0x2f, 0xb0, 0x95, 0xb7, 0x43, 0x28, 0x19, 0x2d, 0xef, 0x1a, 0xb3, \ + 0x42, 0x31, 0x55, 0x0f, 0xbc, 0xcd, 0x01, 0xe5, 0x39, 0x18, 0x88, 0x83, 0xb2, 0xc5, \ + 0x4b, 0x3b, 0x38, 0xe7) + +#define TEST_DATA_INT_1_FILENAME "test-int-1" +#define TEST_DATA_INT_1_SIZE 8 +#define TEST_DATA_INT_1 0xFEDCBA9876543210ULL + +#define TEST_DATA_INT_2_FILENAME "test-int-2" +#define TEST_DATA_INT_2_SIZE 8 +#define TEST_DATA_INT_2 0x10FE32DC54A97698ULL + +#define TEST_DATA_INT_3_FILENAME "test-int-3" +#define TEST_DATA_INT_3_SIZE 8 +#define TEST_DATA_INT_3 0xFA57F003B0036667ULL + +#define TEST_SHA256 EMPTY_WRAP( \ + 0xef, 0xc7, 0xb1, 0x0a, 0xbf, 0x54, 0x2f, 0xaa, 0x12, 0xa6, 0xeb, 0xf, 0xff, 0xf4, \ + 0x19, 0xc1, 0x63, 0xf4, 0x60, 0x50, 0xc5, 0xb0, 0xbe, 0x37, 0x32, 0x11, 0x19, 0x63, \ + 0x61, 0xe0, 0x53, 0xe0) + +#define INVALID_SHA256 EMPTY_WRAP( \ + 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'n', 'o', 't', ' ', 'a', ' ', \ + 'v', 'a', 'l', 'i', 'd', ' ', 'S', 'H', 'A', '2', '5', '6', '!', '!', \ + '!', '!', '!', '!') + +extern const u8 test_data_1[TEST_DATA_1_SIZE]; +extern const u8 test_data_2[TEST_DATA_2_SIZE]; +extern const u8 test_data_int_1[TEST_DATA_INT_1_SIZE]; +extern const u8 test_data_int_2[TEST_DATA_INT_2_SIZE]; +extern const u8 test_data_int_3[TEST_DATA_INT_3_SIZE]; + +extern const u8 good_hash[VB2_SHA256_DIGEST_SIZE]; +extern const u8 bad_hash[VB2_SHA256_DIGEST_SIZE]; + +extern const struct cbfs_test_file file_no_hash; +extern const struct cbfs_test_file file_valid_hash; +extern const struct cbfs_test_file file_broken_hash; + +#endif /* TESTS_LIB_CBFS_UTIL_H */ diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 45cef1b4ca84..44679eb08aa3 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -32,6 +32,10 @@ tests-y += spd_cache-ddr3-test tests-y += spd_cache-ddr4-test tests-y += cbmem_stage_cache-test tests-y += libgcc-test +tests-y += cbfs-verification-no-sha512-test +tests-y += cbfs-verification-has-sha512-test +tests-y += cbfs-no-verification-no-sha512-test +tests-y += cbfs-no-verification-has-sha512-test string-test-srcs += tests/lib/string-test.c string-test-srcs += src/lib/string.c @@ -176,3 +180,31 @@ cbmem_stage_cache-test-srcs += src/lib/imd.c cbmem_stage_cache-test-config += CONFIG_CBMEM_STAGE_CACHE=1 libgcc-test-srcs += tests/lib/libgcc-test.c + +# CBFS varification tests are compiled with CONFIG_CBFS_VERIFICATION +# and VB2_SUPPORT_SHA512 set and unset. Code should work with and without +# verification and with hash structure of different sizes. +cbfs-verification-no-sha512-test-stage := bootblock +cbfs-verification-no-sha512-test-srcs := tests/lib/cbfs-verification-test.c \ + tests/stubs/console.c \ + tests/stubs/die.c \ + tests/mock/cbfs_file_mock.c \ + src/lib/cbfs.c \ + src/commonlib/bsd/cbfs_private.c \ + src/commonlib/mem_pool.c \ + src/commonlib/region.c +cbfs-verification-no-sha512-test-mocks += cbfs_get_boot_device cbfs_lookup +cbfs-verification-no-sha512-test-config += CONFIG_COLLECT_TIMESTAMPS=0 \ + CONFIG_CBFS_VERIFICATION=1 \ + CONFIG_NO_CBFS_MCACHE=1 \ + VB2_SUPPORT_SHA512=0 + +$(call copy-test,cbfs-verification-no-sha512-test,cbfs-verification-has-sha512-test) +cbfs-verification-has-sha512-test-config += VB2_SUPPORT_SHA512=1 + +$(call copy-test,cbfs-verification-no-sha512-test,cbfs-no-verification-no-sha512-test) +cbfs-no-verification-no-sha512-test-config += CONFIG_CBFS_VERIFICATION=0 + +$(call copy-test,cbfs-verification-no-sha512-test,cbfs-no-verification-has-sha512-test) +cbfs-no-verification-has-sha512-test-config += CONFIG_CBFS_VERIFICATION=0 \ + VB2_SUPPORT_SHA512=1 diff --git a/tests/lib/cbfs-verification-test.c b/tests/lib/cbfs-verification-test.c new file mode 100644 index 000000000000..3f3573961f32 --- /dev/null +++ b/tests/lib/cbfs-verification-test.c @@ -0,0 +1,196 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include +#include +#include + + +/* Mocks */ + +static struct cbfs_boot_device cbd; + +const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro) +{ + check_expected(force_ro); + return &cbd; +} + +size_t vb2_digest_size(enum vb2_hash_algorithm hash_alg) +{ + if (hash_alg != VB2_HASH_SHA256) { + fail_msg("Unsupported hash algorithm: %d\n", hash_alg); + return 0; + } + + return VB2_SHA256_DIGEST_SIZE; +} + +vb2_error_t vb2_hash_verify(const void *buf, uint32_t size, const struct vb2_hash *hash) +{ + check_expected_ptr(buf); + check_expected(size); + assert_int_equal(hash->algo, VB2_HASH_SHA256); + + if (!memcmp(hash->sha256, good_hash, sizeof(good_hash))) + return VB2_SUCCESS; + + if (!memcmp(hash->sha256, bad_hash, sizeof(bad_hash))) + return VB2_ERROR_SHA_MISMATCH; + + fail_msg("%s called with bad hash", __func__); + return VB2_ERROR_SHA_MISMATCH; +} + +size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn) +{ + fail_msg("Unexpected call to %s", __func__); + return 0; +} + +size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn) +{ + fail_msg("Unexpected call to %s", __func__); + return 0; +} + +vb2_error_t vb2_digest_init(struct vb2_digest_context *dc, enum vb2_hash_algorithm hash_alg) +{ + if (hash_alg != VB2_HASH_SHA256) { + fail_msg("Unsupported hash algorithm: %d\n", hash_alg); + return VB2_ERROR_SHA_INIT_ALGORITHM; + } + + return VB2_SUCCESS; +} + +vb2_error_t vb2_digest_extend(struct vb2_digest_context *dc, const uint8_t *buf, uint32_t size) +{ + check_expected(buf); + check_expected(size); + return VB2_SUCCESS; +} + +vb2_error_t vb2_digest_finalize(struct vb2_digest_context *dc, uint8_t *digest, uint32_t size) +{ + memcpy(digest, mock_ptr_type(void *), size); + return VB2_SUCCESS; +} + +/* Original function alias created by test framework. Used for call wrapping in mock below. */ +cb_err_t __real_cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out, + size_t *data_offset_out, struct vb2_hash *metadata_hash); + +cb_err_t cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out, + size_t *data_offset_out, struct vb2_hash *metadata_hash) +{ + const cb_err_t err = + __real_cbfs_lookup(dev, name, mdata_out, data_offset_out, metadata_hash); + assert_int_equal(mock_type(cb_err_t), err); + return err; +} + +/* Tests */ + +static int setup_test_cbfs(void **state) +{ + memset(&cbd, 0, sizeof(cbd)); + return 0; +} + +static void test_cbfs_map_no_hash(void **state) +{ + void *mapping = NULL; + assert_int_equal(0, rdev_chain_mem(&cbd.rdev, &file_no_hash, sizeof(file_no_hash))); + + if (CONFIG(CBFS_VERIFICATION)) { + /* File with no hash. No hash causes hash mismatch by default, + so mapping will not be completed successfully. */ + expect_value(cbfs_get_boot_device, force_ro, false); + will_return(cbfs_lookup, CB_SUCCESS); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_null(mapping); + } else { + expect_value(cbfs_get_boot_device, force_ro, false); + will_return(cbfs_lookup, CB_SUCCESS); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_ptr_equal(mapping, file_no_hash.attrs_and_data); + } +} + +static void test_cbfs_map_valid_hash(void **state) +{ + void *mapping = NULL; + assert_int_equal(0, + rdev_chain_mem(&cbd.rdev, &file_valid_hash, sizeof(file_valid_hash))); + + if (CONFIG(CBFS_VERIFICATION)) { + expect_value(cbfs_get_boot_device, force_ro, false); + expect_value(vb2_hash_verify, buf, + &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]); + expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE); + will_return(cbfs_lookup, CB_SUCCESS); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_ptr_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]); + } else { + expect_value(cbfs_get_boot_device, force_ro, false); + will_return(cbfs_lookup, CB_SUCCESS); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_ptr_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]); + } +} + +static void test_cbfs_map_invalid_hash(void **state) +{ + void *mapping = NULL; + assert_int_equal( + 0, rdev_chain_mem(&cbd.rdev, &file_broken_hash, sizeof(file_broken_hash))); + + if (CONFIG(CBFS_VERIFICATION)) { + expect_value(cbfs_get_boot_device, force_ro, false); + expect_value(vb2_hash_verify, buf, + &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE]); + expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE); + will_return(cbfs_lookup, CB_SUCCESS); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_null(mapping); + } else { + expect_value(cbfs_get_boot_device, force_ro, false); + will_return(cbfs_lookup, CB_SUCCESS); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_ptr_equal(mapping, &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE]); + } +} + +void test_init_boot_device_verify(void **state) +{ + struct vb2_hash hash = {.algo = VB2_HASH_SHA256}; + const uint8_t hash_value[VB2_SHA256_DIGEST_SIZE] = {0}; + memset(&cbd, 0, sizeof(cbd)); + assert_int_equal(0, + rdev_chain_mem(&cbd.rdev, &file_valid_hash, sizeof(file_valid_hash))); + + if (CONFIG(CBFS_VERIFICATION)) { + expect_memory(vb2_digest_extend, buf, &file_valid_hash, + be32_to_cpu(file_valid_hash.header.offset)); + expect_value(vb2_digest_extend, size, + be32_to_cpu(file_valid_hash.header.offset)); + will_return(vb2_digest_finalize, hash_value); + } + + assert_int_equal(CB_SUCCESS, cbfs_init_boot_device(&cbd, &hash)); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + 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_invalid_hash, setup_test_cbfs), + + cmocka_unit_test(test_init_boot_device_verify), + }; + + return cb_run_group_tests(tests, NULL, NULL); +} diff --git a/tests/mock/cbfs_file_mock.c b/tests/mock/cbfs_file_mock.c new file mode 100644 index 000000000000..a4feb618f75e --- /dev/null +++ b/tests/mock/cbfs_file_mock.c @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include + +TEST_REGION(cbfs_cache, TEST_CBFS_CACHE_SIZE); + +const u8 test_data_1[TEST_DATA_1_SIZE] = { TEST_DATA_1 }; +const u8 test_data_2[TEST_DATA_2_SIZE] = { TEST_DATA_2 }; +const u8 test_data_int_1[TEST_DATA_INT_1_SIZE] = { BE64(TEST_DATA_INT_1) }; +const u8 test_data_int_2[TEST_DATA_INT_2_SIZE] = { BE64(TEST_DATA_INT_2) }; +const u8 test_data_int_3[TEST_DATA_INT_3_SIZE] = { BE64(TEST_DATA_INT_3) }; + +const u8 good_hash[VB2_SHA256_DIGEST_SIZE] = { TEST_SHA256 }; +const u8 bad_hash[VB2_SHA256_DIGEST_SIZE] = { INVALID_SHA256 }; + +const struct cbfs_test_file file_no_hash = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_1_SIZE), + .filename = TEST_DATA_1_FILENAME, + .attrs_and_data = { + TEST_DATA_1, + }, +}; + +const struct cbfs_test_file file_valid_hash = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, HASH_ATTR_SIZE, TEST_DATA_1_SIZE), + .filename = TEST_DATA_1_FILENAME, + .attrs_and_data = { + BE32(CBFS_FILE_ATTR_TAG_HASH), + BE32(HASH_ATTR_SIZE), + BE32(VB2_HASH_SHA256), + TEST_SHA256, + TEST_DATA_1, + }, +}; + +const struct cbfs_test_file file_broken_hash = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, HASH_ATTR_SIZE, TEST_DATA_1_SIZE), + .filename = TEST_DATA_1_FILENAME, + .attrs_and_data = { + BE32(CBFS_FILE_ATTR_TAG_HASH), + BE32(HASH_ATTR_SIZE), + BE32(VB2_HASH_SHA256), + INVALID_SHA256, + TEST_DATA_1, + }, +}; diff --git a/tests/stubs/die.c b/tests/stubs/die.c new file mode 100644 index 000000000000..84bb401ab03e --- /dev/null +++ b/tests/stubs/die.c @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include + +void die(const char *msg, ...) +{ + /* die() can be called in middle a function, so we should not allow for it to return */ + static char msg_buf[256]; + va_list v; + va_start(v, msg); + vsnprintf(msg_buf, ARRAY_SIZE(msg_buf), msg, v); + va_end(v); + fail_msg("%s", msg_buf); +} -- cgit v1.2.3