Resizing the eventfd list before the epoll event list can leave the handle with
a smaller eventfd array and the old interrupt count if the second allocation
fails. Subsequent access can then read past the end of the array.
Allocate both replacement arrays before changing the handle. Preserve the
existing entries and free the original arrays only after both allocations
succeed.
Fixes: 8cb5d08db940 ("interrupts: extend event list")
Cc: [email protected]
Signed-off-by: Anatoly Burakov <[email protected]>
---
lib/eal/common/eal_common_interrupts.c | 40 +++++++++++++++-----------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/lib/eal/common/eal_common_interrupts.c
b/lib/eal/common/eal_common_interrupts.c
index f3eee636df3..3b46e90c4c3 100644
--- a/lib/eal/common/eal_common_interrupts.c
+++ b/lib/eal/common/eal_common_interrupts.c
@@ -107,6 +107,7 @@ RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_event_list_update)
int rte_intr_event_list_update(struct rte_intr_handle *intr_handle, int size)
{
struct rte_epoll_event *tmp_elist;
+ size_t copy_count;
bool uses_rte_memory;
int *tmp_efds;
@@ -121,34 +122,41 @@ int rte_intr_event_list_update(struct rte_intr_handle
*intr_handle, int size)
uses_rte_memory =
RTE_INTR_INSTANCE_USES_RTE_MEMORY(intr_handle->alloc_flags);
if (uses_rte_memory) {
- tmp_efds = rte_realloc(intr_handle->efds, size * sizeof(int),
- 0);
+ tmp_efds = rte_calloc(NULL, size, sizeof(*tmp_efds), 0);
+ tmp_elist = rte_calloc(NULL, size, sizeof(*tmp_elist), 0);
} else {
- tmp_efds = realloc(intr_handle->efds, size * sizeof(int));
+ tmp_efds = calloc(size, sizeof(*tmp_efds));
+ tmp_elist = calloc(size, sizeof(*tmp_elist));
}
- if (tmp_efds == NULL) {
- EAL_LOG(ERR, "Failed to realloc the efds list");
+ if (tmp_efds == NULL || tmp_elist == NULL) {
+ EAL_LOG(ERR, "Failed to allocate interrupt event lists");
+ if (uses_rte_memory) {
+ rte_free(tmp_efds);
+ rte_free(tmp_elist);
+ } else {
+ free(tmp_efds);
+ free(tmp_elist);
+ }
rte_errno = ENOMEM;
goto fail;
}
- intr_handle->efds = tmp_efds;
+
+ copy_count = RTE_MIN((size_t)size, (size_t)intr_handle->nb_intr);
+ memcpy(tmp_efds, intr_handle->efds, copy_count * sizeof(*tmp_efds));
+ memcpy(tmp_elist, intr_handle->elist, copy_count * sizeof(*tmp_elist));
for (int i = intr_handle->nb_intr; i < size; i++)
- intr_handle->efds[i] = -1;
+ tmp_efds[i] = -1;
if (uses_rte_memory) {
- tmp_elist = rte_realloc(intr_handle->elist,
- size * sizeof(struct rte_epoll_event), 0);
+ rte_free(intr_handle->efds);
+ rte_free(intr_handle->elist);
} else {
- tmp_elist = realloc(intr_handle->elist,
- size * sizeof(struct rte_epoll_event));
- }
- if (tmp_elist == NULL) {
- EAL_LOG(ERR, "Failed to realloc the event list");
- rte_errno = ENOMEM;
- goto fail;
+ free(intr_handle->efds);
+ free(intr_handle->elist);
}
intr_handle->elist = tmp_elist;
+ intr_handle->efds = tmp_efds;
intr_handle->nb_intr = size;
return 0;
--
2.52.0