Skip to contentSkip to conversation

RDMA/nldev: prevent underflow in nldev_stat_set_counter_dynamic_doit()

PATCH Applied Dan Carpenter 3 participants 3 messages Mar 16, 2022 – Mar 18, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RDMA/nldev: prevent underflow in nldev_stat_set_counter_dynamic_doit()

This code checks "index" for an upper bound but it does not check for negatives. Change the type to unsigned to prevent underflows.

Fixes 3c3c1f141639 ("RDMA/nldev: Allow optional-counter status configuration through RDMA netlink")

---
Could we not use a nldev_policy[] to tighten the bounds checking even more?

 drivers/infiniband/core/nldev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/infiniband/core/nldev.c +2 -1
index f5aacaf7fb8e..ca24ce34da76 100644
@@ -1951,9 +1951,10 @@ static int nldev_stat_set_counter_dynamic_doit(struct nlattr *tb[],
 					       u32 port)
 {
 	struct rdma_hw_stats *stats;
-	int rem, i, index, ret = 0;
 	struct nlattr *entry_attr;
 	unsigned long *target;
+	int rem, i, ret = 0;
+	u32 index;
 
 	stats = ib_get_hw_stats_port(device, port);
 	if (!stats)
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Dan Carpenter

This code checks "index" for an upper bound but it does not check for negatives. Change the type to unsigned to prevent underflows.

Fixes 3c3c1f141639 ("RDMA/nldev: Allow optional-counter status configuration through RDMA netlink")

---
Could we not use a nldev_policy[] to tighten the bounds checking even more?

We are doing it, when calling to nlmsg_parse() at the beginning of nldev_stat_set_doit(). The entry_attr, which used as input to index, is tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX].

However it is not enough and we still need your change, because input can be large enough to be casted to negative value.

Thanks,

maintainer Applied
#2 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

This code checks "index" for an upper bound but it does not check for negatives. Change the type to unsigned to prevent underflows.

Fixes 3c3c1f141639 ("RDMA/nldev: Allow optional-counter status configuration through RDMA netlink")
Quoted 6 lines

---
Could we not use a nldev_policy[] to tighten the bounds checking even more?

 drivers/infiniband/core/nldev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Applied to for-next, thanks

Jason