DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH] lib/rte_rib6: fix stack buffer overflow
@ 2021-06-16 16:07 ohilyard
  2021-06-16 16:56 ` Stephen Hemminger
  2021-06-16 18:18 ` [dpdk-dev] [PATCH v2] " ohilyard
  0 siblings, 2 replies; 12+ messages in thread
From: ohilyard @ 2021-06-16 16:07 UTC (permalink / raw)
  To: vladimir.medvedkin; +Cc: dev, david.marchand, Owen Hilyard

From: Owen Hilyard <ohilyard@iol.unh.edu>

ASAN found a stack buffer overflow in lib/rib/rte_rib6.c:get_dir.
The fix for the stack buffer overflow was to make sure depth
was always < 128, since when depth = 128 it caused the index
into the ip address to be 16, which read off the end of the array.

While trying to solve the buffer overflow, I noticed that a few
changes could be made to remove the for loop entirely.

Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
---
 lib/rib/rte_rib6.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c
index f6c55ee45..2de50449d 100644
--- a/lib/rib/rte_rib6.c
+++ b/lib/rib/rte_rib6.c
@@ -79,14 +79,20 @@ is_covered(const uint8_t ip1[RTE_RIB6_IPV6_ADDR_SIZE],
 static inline int
 get_dir(const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
 {
-	int i = 0;
-	uint8_t p_depth, msk;
-
-	for (p_depth = depth; p_depth >= 8; p_depth -= 8)
-		i++;
-
-	msk = 1 << (7 - p_depth);
-	return (ip[i] & msk) != 0;
+	int index, msk;
+	/* depth & 127 clamps depth to values that will not
+	 * read off the end of ip.
+	 * depth is the number of bits deep into ip to traverse, and
+	 * is incremented in blocks of 8 (1 byte). This means the last
+	 * 3 bits are irrelevant to what the index of ip should be.
+	 */
+	index = (depth & 127) >> 3;
+	/*
+	 * msk is the bitmask used to extract the bit used to decide the
+	 * direction of the next step of the binary search.
+	 */
+	msk = 1 << (7 - (depth & 7));
+	return (ip[index] & msk) != 0;
 }
 
 static inline struct rte_rib6_node *
-- 
2.30.2


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-06-24 13:23 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-16 16:07 [dpdk-dev] [PATCH] lib/rte_rib6: fix stack buffer overflow ohilyard
2021-06-16 16:56 ` Stephen Hemminger
2021-06-16 17:27   ` Medvedkin, Vladimir
2021-06-16 18:18 ` [dpdk-dev] [PATCH v2] " ohilyard
2021-06-18 16:22   ` Medvedkin, Vladimir
2021-06-18 16:27     ` Medvedkin, Vladimir
2021-06-21 13:28   ` [dpdk-dev] [PATCH v3] " ohilyard
2021-06-22  7:10     ` David Marchand
2021-06-22 10:51       ` Medvedkin, Vladimir
2021-06-23 15:17     ` [dpdk-dev] [PATCH v4] rib: fix max depth IPv6 lookup ohilyard
2021-06-24 13:23       ` David Marchand
2021-06-24  9:01     ` [dpdk-dev] [PATCH v3] lib/rte_rib6: fix stack buffer overflow Medvedkin, Vladimir

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).