diff mbox series

[v3,iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read

Message ID 20231204014455.2444734-1-chentao@kylinos.cn (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [v3,iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1115 this patch: 1115
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 1142 this patch: 1142
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1142 this patch: 1142
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 30 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Kunwu Dec. 4, 2023, 1:44 a.m. UTC
The size of "i40e_dbg_command_buf" is 256, the size of "name"
depends on "IFNAMSIZ", plus a null character and format size,
the total size is more than 256.

Improve readability and maintainability by replacing a hardcoded string
allocation and formatting by the use of the kasprintf() helper.

Fixes: 02e9c290814c ("i40e: debugfs interface")
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Suggested-by: Simon Horman <horms@kernel.org>
Suggested-by: Alexander Lobakin <aleksander.lobakin@intel.com>
---
v2
   - Update the size calculation with IFNAMSIZ and sizeof(i40e_dbg_command_buf)
v3
   - Use kasprintf to improve readability and maintainability
---
 drivers/net/ethernet/intel/i40e/i40e_debugfs.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

Comments

Alexander Lobakin Dec. 4, 2023, 12:51 p.m. UTC | #1
From: Kunwu Chan <chentao@kylinos.cn>
Date: Mon, 4 Dec 2023 09:44:55 +0800

> The size of "i40e_dbg_command_buf" is 256, the size of "name"
> depends on "IFNAMSIZ", plus a null character and format size,
> the total size is more than 256.
> 
> Improve readability and maintainability by replacing a hardcoded string
> allocation and formatting by the use of the kasprintf() helper.
> 
> Fixes: 02e9c290814c ("i40e: debugfs interface")
> Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
> Suggested-by: Simon Horman <horms@kernel.org>
> Suggested-by: Alexander Lobakin <aleksander.lobakin@intel.com>
> ---
> v2
>    - Update the size calculation with IFNAMSIZ and sizeof(i40e_dbg_command_buf)
> v3
>    - Use kasprintf to improve readability and maintainability
> ---
>  drivers/net/ethernet/intel/i40e/i40e_debugfs.c | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
> index 88240571721a..a176de89de9c 100644
> --- a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
> +++ b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
> @@ -72,23 +72,22 @@ static ssize_t i40e_dbg_command_read(struct file *filp, char __user *buffer,
>  {
>  	struct i40e_pf *pf = filp->private_data;
>  	int bytes_not_copied;
> -	int buf_size = 256;
> -	char *buf;
> +	char *buf = NULL;

This is unneeded.

>  	int len;
>  
>  	/* don't allow partial reads */
>  	if (*ppos != 0)
>  		return 0;
> -	if (count < buf_size)
> -		return -ENOSPC;
>  
> -	buf = kzalloc(buf_size, GFP_KERNEL);
> +	buf = kasprintf(GFP_KERNEL, "%s: %s\n",
> +			pf->vsi[pf->lan_vsi]->netdev->name,
> +			i40e_dbg_command_buf);
>  	if (!buf)
>  		return -ENOSPC;
>  
> -	len = snprintf(buf, buf_size, "%s: %s\n",
> -		       pf->vsi[pf->lan_vsi]->netdev->name,
> -		       i40e_dbg_command_buf);
> +	len = strlen(buf);

strlen() doesn't include the terminating '\0', but you need to copy it
as well. Hence `strlen(buf) + 1`.

> +	if (count < len)
> +		return -ENOSPC;

Here you have memory leak in case the condition is true.

>  
>  	bytes_not_copied = copy_to_user(buffer, buf, len);
>  	kfree(buf);

Taking the above two into account, I'd suggest doing something like:

	if (!buf)
		return -ENOSPC;

	len = strlen(buf) + 1;

	if (count < len)
		bytes_not_copied = -ENOSPC;
	elseif (copy_to_user(buffer, buf, len))
		bytes_not_copied = -EFAULT;
	else
		bytes_not_copied = 0;

	kfree(buf);

	if (bytes_not_copied)
		return bytes_not_copied;

	*ppos = len;

	return len;
}

Thanks,
Olek
Kunwu Dec. 5, 2023, 3:05 a.m. UTC | #2
Hi Alexander,
Thanks for your reply.

It's my bad, I'll follow your suggestion in v4 patch:
1. keep 'buf' as it defined before.
2. resolve memory leak as your suggestion.
3. make 'bytes_not_copied' as a return value for error path.

Thanks again,
Kunwu
On 2023/12/4 20:51, Alexander Lobakin wrote:
> This is unneeded.
diff mbox series

Patch

diff --git a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
index 88240571721a..a176de89de9c 100644
--- a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
+++ b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
@@ -72,23 +72,22 @@  static ssize_t i40e_dbg_command_read(struct file *filp, char __user *buffer,
 {
 	struct i40e_pf *pf = filp->private_data;
 	int bytes_not_copied;
-	int buf_size = 256;
-	char *buf;
+	char *buf = NULL;
 	int len;
 
 	/* don't allow partial reads */
 	if (*ppos != 0)
 		return 0;
-	if (count < buf_size)
-		return -ENOSPC;
 
-	buf = kzalloc(buf_size, GFP_KERNEL);
+	buf = kasprintf(GFP_KERNEL, "%s: %s\n",
+			pf->vsi[pf->lan_vsi]->netdev->name,
+			i40e_dbg_command_buf);
 	if (!buf)
 		return -ENOSPC;
 
-	len = snprintf(buf, buf_size, "%s: %s\n",
-		       pf->vsi[pf->lan_vsi]->netdev->name,
-		       i40e_dbg_command_buf);
+	len = strlen(buf);
+	if (count < len)
+		return -ENOSPC;
 
 	bytes_not_copied = copy_to_user(buffer, buf, len);
 	kfree(buf);