systemd/0621-tpm2-use-GREEDY_REALLO...

46 lines
1.9 KiB
Diff

From 06e759d897bfe850edf9539de028c490c65adccf Mon Sep 17 00:00:00 2001
From: Dan Streetman <ddstreet@ieee.org>
Date: Fri, 8 Sep 2023 14:22:11 -0400
Subject: [PATCH] tpm2: use GREEDY_REALLOC_APPEND() in
tpm2_get_capability_handles(), cap max value
Simplify the function with GREEDY_REALLOC_APPEND(). Also limit the size_t-sized
max value to UINT32_MAX since that's the maximum of the range this searches,
and the max parameter for tpm2_get_capability() is uint32_t.
(cherry picked from commit 7014006906113acf35d4927ef7f287ddaa935fca)
Related: RHEL-16182
---
src/shared/tpm2-util.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c
index 5a5f4db45e..853761d50a 100644
--- a/src/shared/tpm2-util.c
+++ b/src/shared/tpm2-util.c
@@ -395,6 +395,8 @@ static int tpm2_get_capability_handles(
assert(ret_handles);
assert(ret_n_handles);
+ max = MIN(max, UINT32_MAX);
+
while (max > 0) {
TPMU_CAPABILITIES capability;
r = tpm2_get_capability(c, TPM2_CAP_HANDLES, current, (uint32_t) max, &capability);
@@ -410,13 +412,10 @@ static int tpm2_get_capability_handles(
if (n_handles > SIZE_MAX - handle_list.count)
return log_oom_debug();
- if (!GREEDY_REALLOC(handles, n_handles + handle_list.count))
+ if (!GREEDY_REALLOC_APPEND(handles, n_handles, handle_list.handle, handle_list.count))
return log_oom_debug();
- memcpy_safe(&handles[n_handles], handle_list.handle, sizeof(handles[0]) * handle_list.count);
-
max -= handle_list.count;
- n_handles += handle_list.count;
/* Update current to the handle index after the last handle in the list. */
current = handles[n_handles - 1] + 1;