tpm2: use ELEMENTSOF() instead of sizeof() for TPML_PCR_SELECTION pcrSelections field

The count field indicates the number of elements in the pcrSelections field,
and the size of each elements is greater than 1 byte, so using sizeof() is
incorrect when verifying the count field is valid; instead ELEMENTSOF() should
be used.

Caught by coverity check: https://github.com/systemd/systemd/pull/26331#pullrequestreview-1556629586
This commit is contained in:
Dan Streetman
2023-08-04 16:12:05 -04:00
committed by Yu Watanabe
parent 42f13f10d5
commit 9afd4dde22

View File

@@ -1325,7 +1325,7 @@ size_t tpm2_tpms_pcr_selection_weight(const TPMS_PCR_SELECTION *s) {
/* Remove the (0-based) index entry from 'l', shift all following entries, and update the count. */
static void tpm2_tpml_pcr_selection_remove_index(TPML_PCR_SELECTION *l, uint32_t index) {
assert(l);
assert(l->count <= sizeof(l->pcrSelections));
assert(l->count <= ELEMENTSOF(l->pcrSelections));
assert(index < l->count);
size_t s = l->count - (index + 1);
@@ -1341,6 +1341,7 @@ static TPMS_PCR_SELECTION *tpm2_tpml_pcr_selection_get_tpms_pcr_selection(
TPMI_ALG_HASH hash_alg) {
assert(l);
assert(l->count <= ELEMENTSOF(l->pcrSelections));
TPMS_PCR_SELECTION *selection = NULL;
FOREACH_TPMS_PCR_SELECTION_IN_TPML_PCR_SELECTION(s, l)
@@ -1421,13 +1422,13 @@ void tpm2_tpml_pcr_selection_add_tpms_pcr_selection(TPML_PCR_SELECTION *l, const
}
/* It's already broken if the count is higher than the array has size for. */
assert(!(l->count > sizeof(l->pcrSelections)));
assert(l->count <= ELEMENTSOF(l->pcrSelections));
/* If full, the cleanup should result in at least one available entry. */
if (l->count == sizeof(l->pcrSelections))
if (l->count == ELEMENTSOF(l->pcrSelections))
tpm2_tpml_pcr_selection_cleanup(l);
assert(l->count < sizeof(l->pcrSelections));
assert(l->count < ELEMENTSOF(l->pcrSelections));
l->pcrSelections[l->count++] = *s;
}
@@ -1508,7 +1509,7 @@ char *tpm2_tpml_pcr_selection_to_string(const TPML_PCR_SELECTION *l) {
size_t tpm2_tpml_pcr_selection_weight(const TPML_PCR_SELECTION *l) {
assert(l);
assert(l->count <= sizeof(l->pcrSelections));
assert(l->count <= ELEMENTSOF(l->pcrSelections));
size_t weight = 0;
FOREACH_TPMS_PCR_SELECTION_IN_TPML_PCR_SELECTION(s, l) {