Browse Source

Measured Boot Driver: Fix MISRA-C 2012 defects

This patch fixes MISRA C-2012 Pointers and Arrays
Rule 18.4 defects reported by Coverity scan:
"misra_c_2012_rule_18_4_violation: Using arithmetic on pointer "

Change-Id: I06753b28467c473e346b9871c1657284fc43a3f3
Signed-off-by: Alexei Fedorov <Alexei.Fedorov@arm.com>
pull/1979/head
Alexei Fedorov 4 years ago
parent
commit
74ae4eef1c
  1. 26
      drivers/measured_boot/event_log.c
  2. 25
      drivers/measured_boot/event_print.c

26
drivers/measured_boot/event_log.c

@ -147,13 +147,14 @@ static int add_event2(const uint8_t *hash, const image_data_t *image_ptr)
((tpml_digest_values *)ptr)->count = HASH_ALG_COUNT;
/* TCG_PCR_EVENT2.Digests[] */
ptr = (uint8_t *)ptr + offsetof(tpml_digest_values, digests);
ptr = (uint8_t *)((uintptr_t)ptr +
offsetof(tpml_digest_values, digests));
/* TCG_PCR_EVENT2.Digests[].AlgorithmId */
((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID;
/* TCG_PCR_EVENT2.Digests[].Digest[] */
ptr = (uint8_t *)ptr + offsetof(tpmt_ha, digest);
ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest));
/* Check for space in Event Log buffer */
if (((uintptr_t)ptr + TCG_DIGEST_SIZE) > EVENT_LOG_END) {
@ -170,7 +171,7 @@ static int add_event2(const uint8_t *hash, const image_data_t *image_ptr)
}
/* TCG_PCR_EVENT2.EventSize */
ptr = (uint8_t *)ptr + TCG_DIGEST_SIZE;
ptr = (uint8_t *)((uintptr_t)ptr + TCG_DIGEST_SIZE);
((event2_data_t *)ptr)->event_size = name_len;
/* Copy event data to TCG_PCR_EVENT2.Event */
@ -178,7 +179,8 @@ static int add_event2(const uint8_t *hash, const image_data_t *image_ptr)
(const void *)image_ptr->name, name_len);
/* End of event data */
log_ptr = (uint8_t *)ptr + offsetof(event2_data_t, event) + name_len;
log_ptr = (uint8_t *)((uintptr_t)ptr +
offsetof(event2_data_t, event) + name_len);
return 0;
}
@ -205,19 +207,20 @@ void event_log_init(void)
*/
(void)memcpy(ptr, (const void *)&id_event_header,
sizeof(id_event_header));
ptr = (uint8_t *)ptr + sizeof(id_event_header);
ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_header));
/* TCG_EfiSpecIdEventAlgorithmSize structure */
((id_event_algorithm_size_t *)ptr)->algorithm_id = TPM_ALG_ID;
((id_event_algorithm_size_t *)ptr)->digest_size = TCG_DIGEST_SIZE;
ptr = (uint8_t *)ptr + sizeof(id_event_algorithm_size_t);
ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_algorithm_size_t));
/*
* TCG_EfiSpecIDEventStruct.vendorInfoSize
* No vendor data
*/
((id_event_struct_data_t *)ptr)->vendor_info_size = 0;
ptr = (uint8_t *)ptr + offsetof(id_event_struct_data_t, vendor_info);
ptr = (uint8_t *)((uintptr_t)ptr +
offsetof(id_event_struct_data_t, vendor_info));
if ((uintptr_t)ptr != ((uintptr_t)event_log + ID_EVENT_SIZE)) {
panic();
}
@ -234,19 +237,20 @@ void event_log_init(void)
/* Copy Startup Locality Event Header */
(void)memcpy(ptr, (const void *)&locality_event_header,
sizeof(locality_event_header));
ptr = (uint8_t *)ptr + sizeof(locality_event_header);
ptr = (uint8_t *)((uintptr_t)ptr + sizeof(locality_event_header));
/* TCG_PCR_EVENT2.Digests[].AlgorithmId */
((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID;
/* TCG_PCR_EVENT2.Digests[].Digest[] */
(void)memset(&((tpmt_ha *)ptr)->digest, 0, TPM_ALG_ID);
ptr = (uint8_t *)ptr + offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE;
ptr = (uint8_t *)((uintptr_t)ptr +
offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE);
/* TCG_PCR_EVENT2.EventSize */
((event2_data_t *)ptr)->event_size =
(uint32_t)sizeof(startup_locality_event_t);
ptr = (uint8_t *)ptr + offsetof(event2_data_t, event);
ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event));
/* TCG_EfiStartupLocalityEvent.Signature */
(void)memcpy(ptr, (const void *)locality_signature,
@ -257,7 +261,7 @@ void event_log_init(void)
* the platform's boot firmware
*/
((startup_locality_event_t *)ptr)->startup_locality = 0U;
ptr = (uint8_t *)ptr + sizeof(startup_locality_event_t);
ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t));
if ((uintptr_t)ptr != ((uintptr_t)start_ptr + LOC_EVENT_SIZE)) {
panic();
}

25
drivers/measured_boot/event_print.c

@ -28,7 +28,7 @@ static void id_event_print(uint8_t **log_addr, size_t *log_size)
uint32_t event_size, number_of_algorithms;
size_t digest_len;
#if ENABLE_ASSERTIONS
const uint8_t *end_ptr = *log_addr + *log_size;
const uint8_t *end_ptr = (uint8_t *)((uintptr_t)*log_addr + *log_size);
bool valid = true;
#endif
@ -90,7 +90,7 @@ static void id_event_print(uint8_t **log_addr, size_t *log_size)
/* Size of DigestSizes[] */
digest_len = number_of_algorithms * sizeof(id_event_algorithm_size_t);
assert(((uint8_t *)alg_ptr + digest_len) <= end_ptr);
assert(((uintptr_t)alg_ptr + digest_len) <= (uintptr_t)end_ptr);
LOG_EVENT(" DigestSizes :\n");
for (i = 0U; i < number_of_algorithms; ++i) {
@ -118,14 +118,14 @@ static void id_event_print(uint8_t **log_addr, size_t *log_size)
}
/* Address of VendorInfoSize */
info_size_ptr = (uint8_t *)alg_ptr + digest_len;
assert(info_size_ptr <= end_ptr);
info_size_ptr = (uint8_t *)((uintptr_t)alg_ptr + digest_len);
assert((uintptr_t)info_size_ptr <= (uintptr_t)end_ptr);
info_size = *info_size_ptr++;
LOG_EVENT(" VendorInfoSize : %u\n", info_size);
/* Check VendorInfo end address */
assert((info_size_ptr + info_size) <= end_ptr);
assert(((uintptr_t)info_size_ptr + info_size) <= (uintptr_t)end_ptr);
/* Check EventSize */
assert(event_size == (sizeof(id_event_struct_t) +
@ -154,7 +154,7 @@ static void event2_print(uint8_t **log_addr, size_t *log_size)
size_t sha_size, digests_size = 0U;
void *ptr = *log_addr;
#if ENABLE_ASSERTIONS
const uint8_t *end_ptr = *log_addr + *log_size;
const uint8_t *end_ptr = (uint8_t *)((uintptr_t)*log_addr + *log_size);
#endif
assert(*log_size >= sizeof(event2_header_t));
@ -174,7 +174,8 @@ static void event2_print(uint8_t **log_addr, size_t *log_size)
for (unsigned int i = 0U; i < count; ++i) {
/* Check AlgorithmId address */
assert(((uint8_t *)ptr + offsetof(tpmt_ha, digest)) <= end_ptr);
assert(((uintptr_t)ptr +
offsetof(tpmt_ha, digest)) <= (uintptr_t)end_ptr);
LOG_EVENT(" #%u AlgorithmId : SHA", i);
switch (((tpmt_ha *)ptr)->algorithm_id) {
@ -198,8 +199,8 @@ static void event2_print(uint8_t **log_addr, size_t *log_size)
}
/* End of Digest[] */
ptr = (uint8_t *)ptr + offsetof(tpmt_ha, digest);
assert(((uint8_t *)ptr + sha_size) <= end_ptr);
ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest));
assert(((uintptr_t)ptr + sha_size) <= (uintptr_t)end_ptr);
/* Total size of all digests */
digests_size += sha_size;
@ -217,16 +218,16 @@ static void event2_print(uint8_t **log_addr, size_t *log_size)
}
/* TCG_PCR_EVENT2.EventSize */
assert(((uint8_t *)ptr + offsetof(event2_data_t, event)) <= end_ptr);
assert(((uintptr_t)ptr + offsetof(event2_data_t, event)) <= (uintptr_t)end_ptr);
event_size = ((event2_data_t *)ptr)->event_size;
LOG_EVENT(" EventSize : %u\n", event_size);
/* Address of TCG_PCR_EVENT2.Event[EventSize] */
ptr = (uint8_t *)ptr + offsetof(event2_data_t, event);
ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event));
/* End of TCG_PCR_EVENT2.Event[EventSize] */
assert(((uint8_t *)ptr + event_size) <= end_ptr);
assert(((uintptr_t)ptr + event_size) <= (uintptr_t)end_ptr);
if ((event_size == sizeof(startup_locality_event_t)) &&
(strcmp((const char *)ptr, TCG_STARTUP_LOCALITY_SIGNATURE) == 0)) {

Loading…
Cancel
Save