rte_zmalloc() in cpm_insert_pool_id() can return NULL when memory is
exhausted. The return value was not checked before being dereferenced,
resulting in a crash.
Add a NULL check and return -ENOMEM on failure. Also propagate the
error in tfc_cpm_set_cmm_inst() which silently discarded the return
value of cpm_insert_pool_id(), and restore the pool state on failure.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes: 80317ff6adfd ("net/bnxt/tf_core: support Thor2")
Cc: [email protected]
Signed-off-by: Denis Sergeev <[email protected]>
---
drivers/net/bnxt/tf_core/v3/tfc_cpm.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/drivers/net/bnxt/tf_core/v3/tfc_cpm.c
b/drivers/net/bnxt/tf_core/v3/tfc_cpm.c
index f58ec48db7..28e2a3d8dd 100644
--- a/drivers/net/bnxt/tf_core/v3/tfc_cpm.c
+++ b/drivers/net/bnxt/tf_core/v3/tfc_cpm.c
@@ -92,6 +92,10 @@ static int cpm_insert_pool_id(struct tfc_cpm *cpm, uint16_t
pool_id)
/* Alloc new entry */
new_pool_use = rte_zmalloc("tf", sizeof(struct cpm_pool_use), 0);
+ if (new_pool_use == NULL) {
+ PMD_DRV_LOG_LINE(ERR, "Failed to allocate pool_use entry");
+ return -ENOMEM;
+ }
new_pool_use->pool_id = pool_id;
new_pool_use->prev = NULL;
new_pool_use->next = NULL;
@@ -287,6 +291,7 @@ int tfc_cpm_get_pool_size(struct tfc_cpm *cpm, uint32_t
*pool_sz_in_records)
int tfc_cpm_set_cmm_inst(struct tfc_cpm *cpm, uint16_t pool_id, struct tfc_cmm
*cmm)
{
struct cpm_pool_entry *pool;
+ int rc;
if (cpm == NULL) {
PMD_DRV_LOG_LINE(ERR, "CPM is NULL");
@@ -313,7 +318,12 @@ int tfc_cpm_set_cmm_inst(struct tfc_cpm *cpm, uint16_t
pool_id, struct tfc_cmm *
pool->valid = false;
} else {
pool->valid = true;
- cpm_insert_pool_id(cpm, pool_id);
+ rc = cpm_insert_pool_id(cpm, pool_id);
+ if (rc) {
+ pool->cmm = NULL;
+ pool->valid = false;
+ return rc;
+ }
}
return 0;
--
2.50.1