diff options
-rw-r--r-- | payloads/libpayload/include/cbfs.h | 22 | ||||
-rw-r--r-- | payloads/libpayload/libcbfs/cbfs.c | 71 | ||||
-rw-r--r-- | payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c | 87 |
3 files changed, 158 insertions, 22 deletions
diff --git a/payloads/libpayload/include/cbfs.h b/payloads/libpayload/include/cbfs.h index 8adadb737e..23b96695ef 100644 --- a/payloads/libpayload/include/cbfs.h +++ b/payloads/libpayload/include/cbfs.h @@ -17,9 +17,13 @@ static inline size_t cbfs_load(const char *name, void *buf, size_t size); static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size); +static inline size_t cbfs_unverified_area_load(const char *area, const char *name, void *buf, + size_t size); static inline void *cbfs_map(const char *name, size_t *size_out); static inline void *cbfs_ro_map(const char *name, size_t *size_out); +static inline void *cbfs_unverified_area_map(const char *area, const char *name, + size_t *size_out); void cbfs_unmap(void *mapping); @@ -39,6 +43,9 @@ ssize_t _cbfs_boot_lookup(const char *name, bool force_ro, union cbfs_mdata *mda void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro); +void *_cbfs_unverified_area_load(const char *area, const char *name, void *buf, + size_t *size_inout); + /********************************************************************************************** * INLINE IMPLEMENTATIONS * **********************************************************************************************/ @@ -53,6 +60,12 @@ static inline void *cbfs_ro_map(const char *name, size_t *size_out) return _cbfs_load(name, NULL, size_out, true); } +static inline void *cbfs_unverified_area_map(const char *area, const char *name, + size_t *size_out) +{ + return _cbfs_unverified_area_load(area, name, NULL, size_out); +} + static inline size_t cbfs_load(const char *name, void *buf, size_t size) { if (_cbfs_load(name, buf, &size, false)) @@ -69,6 +82,15 @@ static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size) return 0; } +static inline size_t cbfs_unverified_area_load(const char *area, const char *name, void *buf, + size_t size) +{ + if (_cbfs_unverified_area_load(area, name, buf, &size)) + return size; + else + return 0; +} + static inline size_t cbfs_get_size(const char *name) { union cbfs_mdata mdata; diff --git a/payloads/libpayload/libcbfs/cbfs.c b/payloads/libpayload/libcbfs/cbfs.c index 530f9e292d..44173c82f6 100644 --- a/payloads/libpayload/libcbfs/cbfs.c +++ b/payloads/libpayload/libcbfs/cbfs.c @@ -4,6 +4,7 @@ #include <arch/virtual.h> #include <assert.h> #include <cbfs.h> +#include <cbfs_glue.h> #include <commonlib/bsd/cbfs_private.h> #include <commonlib/bsd/fmap_serialized.h> #include <libpayload.h> @@ -78,9 +79,9 @@ void cbfs_unmap(void *mapping) } static bool cbfs_file_hash_mismatch(const void *buffer, size_t size, - const union cbfs_mdata *mdata) + const union cbfs_mdata *mdata, bool skip_verification) { - if (!CONFIG(LP_CBFS_VERIFICATION)) + if (!CONFIG(LP_CBFS_VERIFICATION) || skip_verification) return false; const struct vb2_hash *hash = cbfs_file_hash(mdata); @@ -98,7 +99,7 @@ static bool cbfs_file_hash_mismatch(const void *buffer, size_t size, static size_t cbfs_load_and_decompress(size_t offset, size_t in_size, void *buffer, size_t buffer_size, uint32_t compression, - const union cbfs_mdata *mdata) + const union cbfs_mdata *mdata, bool skip_verification) { void *load = buffer; size_t out_size = 0; @@ -119,7 +120,7 @@ static size_t cbfs_load_and_decompress(size_t offset, size_t in_size, void *buff goto out; } - if (cbfs_file_hash_mismatch(buffer, in_size, mdata)) + if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification)) goto out; switch (compression) { @@ -146,46 +147,37 @@ out: return out_size; } -void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro) +static void *do_load(union cbfs_mdata *mdata, ssize_t offset, void *buf, size_t *size_inout, + bool skip_verification) { - ssize_t offset; - size_t out_size; - union cbfs_mdata mdata; bool malloced = false; - - DEBUG("%s(name='%s', buf=%p, force_ro=%s)\n", __func__, name, buf, - force_ro ? "true" : "false"); - - offset = _cbfs_boot_lookup(name, force_ro, &mdata); - if (offset < 0) - return NULL; - + size_t out_size; uint32_t compression = CBFS_COMPRESS_NONE; const struct cbfs_file_attr_compression *cattr = - cbfs_find_attr(&mdata, CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr)); + cbfs_find_attr(mdata, CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr)); if (cattr) { compression = be32toh(cattr->compression); out_size = be32toh(cattr->decompressed_size); } else { - out_size = be32toh(mdata.h.len); + out_size = be32toh(mdata->h.len); } if (buf) { if (!size_inout || *size_inout < out_size) { - ERROR("'%s' buffer too small", mdata.h.filename); + ERROR("'%s' buffer too small", mdata->h.filename); return NULL; } } else { buf = malloc(out_size); if (!buf) { - ERROR("'%s' allocation failure", mdata.h.filename); + ERROR("'%s' allocation failure", mdata->h.filename); return NULL; } malloced = true; } - if (cbfs_load_and_decompress(offset, be32toh(mdata.h.len), buf, out_size, compression, - &mdata) + if (cbfs_load_and_decompress(offset, be32toh(mdata->h.len), buf, out_size, compression, + mdata, skip_verification) != out_size) { if (malloced) free(buf); @@ -196,3 +188,38 @@ void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro) return buf; } + +void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro) +{ + ssize_t offset; + union cbfs_mdata mdata; + + DEBUG("%s(name='%s', buf=%p, force_ro=%s)\n", __func__, name, buf, + force_ro ? "true" : "false"); + + offset = _cbfs_boot_lookup(name, force_ro, &mdata); + if (offset < 0) + return NULL; + + return do_load(&mdata, offset, buf, size_inout, false); +} + +void *_cbfs_unverified_area_load(const char *area, const char *name, void *buf, + size_t *size_inout) +{ + struct cbfs_dev dev; + union cbfs_mdata mdata; + size_t data_offset; + + DEBUG("%s(area='%s', name='%s', buf=%p)\n", __func__, area, name, buf); + + if (fmap_locate_area(area, &dev.offset, &dev.size) != CB_SUCCESS) + return NULL; + + if (cbfs_lookup(&dev, name, &mdata, &data_offset, NULL)) { + ERROR("'%s' not found in '%s'\n", name, area); + return NULL; + } + + return do_load(&mdata, dev.offset + data_offset, buf, size_inout, true); +} diff --git a/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c b/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c index 0f7cdc7108..0f840763a8 100644 --- a/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c +++ b/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c @@ -598,6 +598,92 @@ static void test_cbfs_boot_device_read_failure(void **state) assert_null(mapping); } +/* This test uses RW CBFS only */ +static void test_cbfs_unverified_area_map(void **state) +{ + struct cbfs_test_state *s = *state; + void *mapping = NULL; + size_t size_out = 0; + const struct cbfs_test_file *cbfs_files[] = { + &test_file_int_1, &test_file_2, NULL, &test_file_int_3, + &test_file_int_2, NULL, NULL, &test_file_1, + }; + uint8_t *cbfs_buf = NULL; + size_t foffset = 0; + + cbfs_buf = s->cbfs_rw_buf; + set_fmap_locate_area_results((size_t)cbfs_buf, s->cbfs_rw_size, CB_SUCCESS); + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 0); + expect_cbfs_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_1.header.offset)); + will_return(cbfs_find_attr, NULL); + mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_INT_1_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_1_SIZE, size_out); + assert_memory_equal(test_data_int_1, mapping, TEST_DATA_INT_1_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 1); + expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_2.header.offset)); + will_return(cbfs_find_attr, &test_file_2.attrs_and_data); + expect_value(ulzman, srcn, TEST_DATA_2_SIZE); + expect_value(ulzman, dstn, TEST_DATA_2_SIZE); + mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_2_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_2_SIZE, size_out); + assert_memory_equal(test_data_2, mapping, TEST_DATA_2_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 3); + expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_3.header.offset)); + will_return(cbfs_find_attr, &test_file_int_3.attrs_and_data); + expect_value(ulz4fn, srcn, TEST_DATA_INT_3_SIZE); + expect_value(ulz4fn, dstn, TEST_DATA_INT_3_SIZE); + mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_INT_3_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_3_SIZE, size_out); + assert_memory_equal(test_data_int_3, mapping, TEST_DATA_INT_3_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 4); + expect_cbfs_lookup(TEST_DATA_INT_2_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_2.header.offset)); + will_return(cbfs_find_attr, NULL); + mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_INT_2_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_2_SIZE, size_out); + assert_memory_equal(test_data_int_2, mapping, TEST_DATA_INT_2_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 7); + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_1.header.offset)); + will_return(cbfs_find_attr, NULL); + mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_1_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_1_SIZE, size_out); + assert_memory_equal(test_data_1, mapping, TEST_DATA_1_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + expect_cbfs_lookup("invalid_file", CB_CBFS_NOT_FOUND, 0, 0); + mapping = cbfs_unverified_area_map("TEST_AREA", "invalid_file", &size_out); + assert_null(mapping); +} #define TEST_CBFS_NAME_ALIGN_RO_RW(fn, test_name, enable_unaligned, enable_init_ro, \ enable_init_rw) \ @@ -636,6 +722,7 @@ int main(void) TEST_CBFS_LOOKUP(test_cbfs_load), TEST_CBFS_LOOKUP(test_cbfs_map_with_mcache), TEST_CBFS_LOOKUP(test_cbfs_boot_device_read_failure), + TEST_CBFS_LOOKUP(test_cbfs_unverified_area_map), }; return lp_run_group_tests(tests, NULL, NULL); |