diff mbox series

[v2] lib/rte_rib6: fix stack buffer overflow

Message ID 20210616181833.356159-1-ohilyard@iol.unh.edu (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers show
Series [v2] lib/rte_rib6: fix stack buffer overflow | expand

Checks

Context Check Description
ci/iol-mellanox-Functional fail Functional Testing issues
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-testing fail Testing issues
ci/iol-abi-testing success Testing PASS
ci/github-robot success github build: passed
ci/intel-Testing success Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-intel-Functional success Functional Testing PASS
ci/checkpatch success coding style OK

Commit Message

Owen Hilyard June 16, 2021, 6:18 p.m. UTC
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.

Fixes: f7e861e21c ("rib: support IPv6")

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

Comments

Medvedkin, Vladimir June 18, 2021, 4:22 p.m. UTC | #1
Hi Owen,

Just a few nits inlined below

On 16/06/2021 21:18, ohilyard@iol.unh.edu wrote:
> 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.
> 
> Fixes: f7e861e21c ("rib: support IPv6")
> 
> Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
> ---
>   lib/rib/rte_rib6.c | 27 +++++++++++++++++++--------
>   1 file changed, 19 insertions(+), 8 deletions(-)
> 
> diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c
> index f6c55ee45..a4daf12ca 100644
> --- a/lib/rib/rte_rib6.c
> +++ b/lib/rib/rte_rib6.c
> @@ -79,20 +79,31 @@ 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;
> +	uint8_t index, msk;
> +
> +	/* depth & 127 clamps depth to values that will not

Please be consistent with the coding style. Check 1.3.1 in 
https://doc.dpdk.org/guides/contributing/coding_style.html

> +	 * 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;

(depth & INT8_MAX) / UINT8_MAX ?

> +
> +	/*
> +	 * 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 *
>   get_nxt_node(struct rte_rib6_node *node,
>   	const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
>   {
> +	if (node->depth == 128)
> +		return NULL;

please use RIB6_MAXDEPTH instead of 128.
Also I'd put a blank line before the final return.

>   	return (get_dir(ip, node->depth)) ? node->right : node->left;
>   }
>   
> 

while this is a bug fix, please add Cc: stable@dpdk.org on v3 to 
backport this patch.

Apart from that LGTM.
Medvedkin, Vladimir June 18, 2021, 4:27 p.m. UTC | #2
On 18/06/2021 19:22, Medvedkin, Vladimir wrote:
> Hi Owen,
> 
> Just a few nits inlined below
> 
> On 16/06/2021 21:18, ohilyard@iol.unh.edu wrote:
>> 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.
>>
>> Fixes: f7e861e21c ("rib: support IPv6")
>>
>> Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
>> ---
>>   lib/rib/rte_rib6.c | 27 +++++++++++++++++++--------
>>   1 file changed, 19 insertions(+), 8 deletions(-)
>>
>> diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c
>> index f6c55ee45..a4daf12ca 100644
>> --- a/lib/rib/rte_rib6.c
>> +++ b/lib/rib/rte_rib6.c
>> @@ -79,20 +79,31 @@ 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;
>> +    uint8_t index, msk;
>> +
>> +    /* depth & 127 clamps depth to values that will not
> 
> Please be consistent with the coding style. Check 1.3.1 in 
> https://doc.dpdk.org/guides/contributing/coding_style.html
> 
>> +     * 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;
> 
> (depth & INT8_MAX) / UINT8_MAX ?

Ugh...
sed 's/UINT8_MAX/CHAR_BIT'

> 
>> +
>> +    /*
>> +     * 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 *
>>   get_nxt_node(struct rte_rib6_node *node,
>>       const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
>>   {
>> +    if (node->depth == 128)
>> +        return NULL;
> 
> please use RIB6_MAXDEPTH instead of 128.
> Also I'd put a blank line before the final return.
> 
>>       return (get_dir(ip, node->depth)) ? node->right : node->left;
>>   }
>>
> 
> while this is a bug fix, please add Cc: stable@dpdk.org on v3 to 
> backport this patch.
> 
> Apart from that LGTM.
>
diff mbox series

Patch

diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c
index f6c55ee45..a4daf12ca 100644
--- a/lib/rib/rte_rib6.c
+++ b/lib/rib/rte_rib6.c
@@ -79,20 +79,31 @@  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;
+	uint8_t 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 *
 get_nxt_node(struct rte_rib6_node *node,
 	const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
 {
+	if (node->depth == 128)
+		return NULL;
 	return (get_dir(ip, node->depth)) ? node->right : node->left;
 }