[PATCH v7 01/18] lpm: restrict name size

Stephen Hemminger
2025-12-24T22:13:15Z

Chec that lpm name size is within limits and add checks
for truncation.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
lib/lpm/rte_lpm.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/lib/lpm/rte_lpm.c b/lib/lpm/rte_lpm.c
index 6dab86a05e..2b49d82769 100644
--- a/lib/lpm/rte_lpm.c
+++ b/lib/lpm/rte_lpm.c
@@ -5,6 +5,7 @@

#include <string.h>
#include <stdint.h>
+#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <sys/queue.h>
@@ -152,7 +153,7 @@ struct rte_lpm *
rte_lpm_create(const char *name, int socket_id,
const struct rte_lpm_config *config)
{
- char mem_name[RTE_LPM_NAMESIZE];
+ char mem_name[RTE_LPM_NAMESIZE + sizeof("LPM_")];
struct __rte_lpm *i_lpm;
struct rte_lpm *lpm = NULL;
struct rte_tailq_entry *te;
@@ -170,6 +171,11 @@ rte_lpm_create(const char *name, int socket_id,
return NULL;
}

+ if (strlen(name) >= RTE_LPM_NAMESIZE) {
+ rte_errno = ENAMETOOLONG;
+ return NULL;
+ }
+
snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);

rte_mcfg_tailq_write_lock();
@@ -327,8 +333,10 @@ rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg)
/* No other things to do. */
} else if (cfg->mode == RTE_LPM_QSBR_MODE_DQ) {
/* Init QSBR defer queue. */
- snprintf(rcu_dq_name, sizeof(rcu_dq_name),
- "LPM_RCU_%s", i_lpm->name);
+ if (snprintf(rcu_dq_name, sizeof(rcu_dq_name),
+ "LPM_RCU_%s", i_lpm->name) >= (int)sizeof(rcu_dq_name))
+ LPM_LOG(NOTICE, "LPM rcu defer queue name truncated");
+
params.name = rcu_dq_name;
params.size = cfg->dq_size;
if (params.size == 0)
@@ -343,7 +351,8 @@ rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg)
params.v = cfg->v;
i_lpm->dq = rte_rcu_qsbr_dq_create(&params);
if (i_lpm->dq == NULL) {
- LPM_LOG(ERR, "LPM defer queue creation failed");
+ LPM_LOG(ERR, "LPM defer queue creation failed: %s",
+ rte_strerror(rte_errno));
return 1;
}
} else {
--
2.51.0