2023-12-05 09:59:11

by Kunwu Chan

[permalink] [raw]
Subject: [PATCH v4 iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read

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 <[email protected]>
Suggested-by: Simon Horman <[email protected]>
Suggested-by: Alexander Lobakin <[email protected]>
---
v2
- Update the size calculation with IFNAMSIZ and sizeof(i40e_dbg_command_buf)
v3
- Use kasprintf to improve readability and maintainability
v4
- Fix memory leak in error path
---
.../net/ethernet/intel/i40e/i40e_debugfs.c | 20 ++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
index 88240571721a..78a7200211b2 100644
--- a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
+++ b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
@@ -72,29 +72,31 @@ 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;
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) + 1;
+ if (count < len)
+ bytes_not_copied = -ENOSPC;
+ else if (copy_to_user(buffer, buf, len))
+ bytes_not_copied = -EFAULT;
+ else
+ bytes_not_copied = 0;

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

if (bytes_not_copied)
- return -EFAULT;
+ return bytes_not_copied;

*ppos = len;
return len;
--
2.34.1


2023-12-06 12:42:09

by Alexander Lobakin

[permalink] [raw]
Subject: Re: [PATCH v4 iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read

From: Kunwu Chan <[email protected]>
Date: Tue, 5 Dec 2023 17:58:44 +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 <[email protected]>
> Suggested-by: Simon Horman <[email protected]>
> Suggested-by: Alexander Lobakin <[email protected]>

Your Signed-off-by must be the last tag in the block.
Perhaps the maintainer could fix it when taking, so that you wouldn't
need to send a new version only due to that.

> ---
> v2
> - Update the size calculation with IFNAMSIZ and sizeof(i40e_dbg_command_buf)
> v3
> - Use kasprintf to improve readability and maintainability
> v4
> - Fix memory leak in error path
> ---
> .../net/ethernet/intel/i40e/i40e_debugfs.c | 20 ++++++++++---------
> 1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
> index 88240571721a..78a7200211b2 100644
> --- a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
> +++ b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
> @@ -72,29 +72,31 @@ 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;
> 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) + 1;
> + if (count < len)
> + bytes_not_copied = -ENOSPC;
> + else if (copy_to_user(buffer, buf, len))
> + bytes_not_copied = -EFAULT;
> + else
> + bytes_not_copied = 0;
>
> - bytes_not_copied = copy_to_user(buffer, buf, len);
> kfree(buf);
>
> if (bytes_not_copied)
> - return -EFAULT;
> + return bytes_not_copied;
>
> *ppos = len;
> return len;

For the code:

Reviewed-by: Alexander Lobakin <[email protected]>

Thanks,
Olek

2023-12-07 23:26:17

by Tony Nguyen

[permalink] [raw]
Subject: Re: [PATCH v4 iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read



On 12/6/2023 4:40 AM, Alexander Lobakin wrote:
> From: Kunwu Chan <[email protected]>
> Date: Tue, 5 Dec 2023 17:58:44 +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 <[email protected]>
>> Suggested-by: Simon Horman <[email protected]>
>> Suggested-by: Alexander Lobakin <[email protected]>
>
> Your Signed-off-by must be the last tag in the block.
> Perhaps the maintainer could fix it when taking, so that you wouldn't
> need to send a new version only due to that.

You missed Intel Wired LAN ([email protected]) on this,
though the other versions did have it. Could you fix this up and be sure
to include Intel Wired LAN?

Thanks,
Tony

>> ---
>> v2
>> - Update the size calculation with IFNAMSIZ and sizeof(i40e_dbg_command_buf)
>> v3
>> - Use kasprintf to improve readability and maintainability
>> v4
>> - Fix memory leak in error path

2023-12-08 02:06:03

by Kunwu Chan

[permalink] [raw]
Subject: Re: [PATCH v4 iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read

Thanks for the reminder. It was my negligence.

I'll resend the v5 patch:
1. Keep 'Signed-off-by' be the last tag in the block
2. Cc to '[email protected]'
3. Cc to my personal email '[email protected]'

Thanks again,
Kunwu

On 2023/12/8 07:25, Tony Nguyen wrote:
>
>
> On 12/6/2023 4:40 AM, Alexander Lobakin wrote:
>> From: Kunwu Chan <[email protected]>
>> Date: Tue, 5 Dec 2023 17:58:44 +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 <[email protected]>
>>> Suggested-by: Simon Horman <[email protected]>
>>> Suggested-by: Alexander Lobakin <[email protected]>
>>
>> Your Signed-off-by must be the last tag in the block.
>> Perhaps the maintainer could fix it when taking, so that you wouldn't
>> need to send a new version only due to that.
>
> You missed Intel Wired LAN ([email protected]) on this,
> though the other versions did have it. Could you fix this up and be sure
> to include Intel Wired LAN?
>
> Thanks,
> Tony
>
>>> ---
>>> v2
>>>     - Update the size calculation with IFNAMSIZ and
>>> sizeof(i40e_dbg_command_buf)
>>> v3
>>>     - Use kasprintf to improve readability and maintainability
>>> v4
>>>     - Fix memory leak in error path