[v5,2/3] net/ice: refactor raw pattern parsing function

Message ID 20240722135046.285868-2-vladimir.medvedkin@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Bruce Richardson
Headers
Series [v5,1/3] net/ice: fix possible memory leak |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Medvedkin, Vladimir July 22, 2024, 1:50 p.m. UTC
Replace strlen with more secure strnlen in ice_hash_parse_raw_pattern.

Signed-off-by: Vladimir Medvedkin <vladimir.medvedkin@intel.com>

tmp
---
 drivers/net/ice/ice_hash.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
  

Comments

Bruce Richardson July 22, 2024, 3:10 p.m. UTC | #1
On Mon, Jul 22, 2024 at 01:50:45PM +0000, Vladimir Medvedkin wrote:
> Replace strlen with more secure strnlen in ice_hash_parse_raw_pattern.
> 
> Signed-off-by: Vladimir Medvedkin <vladimir.medvedkin@intel.com>
> 
I believe there are quite a number of other small things in this function
that could do with improvement e.g. the processing of the hex strings has
no error checks for reporting invalid (i.e. non-hex) characters.

However, this patch does improve things a bit by enhancing the length
checks, so

Acked-by: Bruce Richardson <bruce.richardson@intel.com>

> ---
>  drivers/net/ice/ice_hash.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/ice/ice_hash.c b/drivers/net/ice/ice_hash.c
> index 6b3095e2c5..aa76718313 100644
> --- a/drivers/net/ice/ice_hash.c
> +++ b/drivers/net/ice/ice_hash.c
> @@ -658,10 +658,13 @@ ice_hash_parse_raw_pattern(struct ice_adapter *ad,
>  	raw_spec = item->spec;
>  	raw_mask = item->mask;
>  
> -	spec_len = strlen((char *)(uintptr_t)raw_spec->pattern);
> -	if (strlen((char *)(uintptr_t)raw_mask->pattern) !=
> -		spec_len)
> -		return -rte_errno;
> +	spec_len = strnlen((char *)(uintptr_t)raw_spec->pattern,
> +		raw_spec->length + 1);
> +	if (spec_len != raw_spec->length)
> +		return -EINVAL;
> +	if (strnlen((char *)(uintptr_t)raw_mask->pattern, raw_spec->length + 1) !=
> +			spec_len)
> +		return -EINVAL;
>  
>  	pkt_len = spec_len / 2;
>  
> -- 
> 2.34.1
>
  

Patch

diff --git a/drivers/net/ice/ice_hash.c b/drivers/net/ice/ice_hash.c
index 6b3095e2c5..aa76718313 100644
--- a/drivers/net/ice/ice_hash.c
+++ b/drivers/net/ice/ice_hash.c
@@ -658,10 +658,13 @@  ice_hash_parse_raw_pattern(struct ice_adapter *ad,
 	raw_spec = item->spec;
 	raw_mask = item->mask;
 
-	spec_len = strlen((char *)(uintptr_t)raw_spec->pattern);
-	if (strlen((char *)(uintptr_t)raw_mask->pattern) !=
-		spec_len)
-		return -rte_errno;
+	spec_len = strnlen((char *)(uintptr_t)raw_spec->pattern,
+		raw_spec->length + 1);
+	if (spec_len != raw_spec->length)
+		return -EINVAL;
+	if (strnlen((char *)(uintptr_t)raw_mask->pattern, raw_spec->length + 1) !=
+			spec_len)
+		return -EINVAL;
 
 	pkt_len = spec_len / 2;