diff mbox series

[net-next,1/6] net: prefer strscpy over strcpy

Message ID 20240827113527.4019856-2-lihongbo22@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series replace deprecated strcpy with strscpy | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
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: 16 this patch: 16
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 4 of 4 maintainers
netdev/build_clang success Errors and warnings before: 16 this patch: 16
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 No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 21 this patch: 21
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 79 this patch: 79
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-08-27--18-00 (tests: 714)

Commit Message

Hongbo Li Aug. 27, 2024, 11:35 a.m. UTC
The deprecated helper strcpy() performs no bounds checking on the
destination buffer. This could result in linear overflows beyond
the end of the buffer, leading to all kinds of misbehaviors.
The safe replacement is strscpy() [1].

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]

Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
---
 net/core/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Dan Carpenter Aug. 27, 2024, 12:30 p.m. UTC | #1
On Tue, Aug 27, 2024 at 07:35:22PM +0800, Hongbo Li wrote:
> The deprecated helper strcpy() performs no bounds checking on the
> destination buffer. This could result in linear overflows beyond
> the end of the buffer, leading to all kinds of misbehaviors.
> The safe replacement is strscpy() [1].
> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]
> 
> Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
> ---
>  net/core/dev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 0d0b983a6c21..f5e0a0d801fd 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -11121,7 +11121,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
>  	if (!dev->ethtool)
>  		goto free_all;
>  
> -	strcpy(dev->name, name);
> +	strscpy(dev->name, name, sizeof(dev->name));

You can just do:

	strscpy(dev->name, name);

I prefer this format because it ensures that dev->name is an array and not a
pointer.  Also shorter.

regards,
dan carpenter
Hongbo Li Aug. 28, 2024, 7:43 a.m. UTC | #2
On 2024/8/27 20:30, Dan Carpenter wrote:
> On Tue, Aug 27, 2024 at 07:35:22PM +0800, Hongbo Li wrote:
>> The deprecated helper strcpy() performs no bounds checking on the
>> destination buffer. This could result in linear overflows beyond
>> the end of the buffer, leading to all kinds of misbehaviors.
>> The safe replacement is strscpy() [1].
>>
>> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]
>>
>> Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
>> ---
>>   net/core/dev.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/net/core/dev.c b/net/core/dev.c
>> index 0d0b983a6c21..f5e0a0d801fd 100644
>> --- a/net/core/dev.c
>> +++ b/net/core/dev.c
>> @@ -11121,7 +11121,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
>>   	if (!dev->ethtool)
>>   		goto free_all;
>>   
>> -	strcpy(dev->name, name);
>> +	strscpy(dev->name, name, sizeof(dev->name));
> 
> You can just do:
> 
> 	strscpy(dev->name, name);
> 
> I prefer this format because it ensures that dev->name is an array and not a
> pointer.  Also shorter.
ok, I'll remove the len.(Most of these are an array, not a pointer)

Thanks,
Hongbo

> 
> regards,
> dan carpenter
>
Dan Carpenter Aug. 28, 2024, 8:54 a.m. UTC | #3
On Wed, Aug 28, 2024 at 03:43:30PM +0800, Hongbo Li wrote:
> 
> 
> On 2024/8/27 20:30, Dan Carpenter wrote:
> > On Tue, Aug 27, 2024 at 07:35:22PM +0800, Hongbo Li wrote:
> > > The deprecated helper strcpy() performs no bounds checking on the
> > > destination buffer. This could result in linear overflows beyond
> > > the end of the buffer, leading to all kinds of misbehaviors.
> > > The safe replacement is strscpy() [1].
> > > 
> > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]
> > > 
> > > Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
> > > ---
> > >   net/core/dev.c | 2 +-
> > >   1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/net/core/dev.c b/net/core/dev.c
> > > index 0d0b983a6c21..f5e0a0d801fd 100644
> > > --- a/net/core/dev.c
> > > +++ b/net/core/dev.c
> > > @@ -11121,7 +11121,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
> > >   	if (!dev->ethtool)
> > >   		goto free_all;
> > > -	strcpy(dev->name, name);
> > > +	strscpy(dev->name, name, sizeof(dev->name));
> > 
> > You can just do:
> > 
> > 	strscpy(dev->name, name);
> > 
> > I prefer this format because it ensures that dev->name is an array and not a
> > pointer.  Also shorter.
> ok, I'll remove the len.(Most of these are an array, not a pointer)

s/Most/all/.

If it were a pointer that would have been a bug and someone would have
complained already.  :P

regards,
dan carpenter
diff mbox series

Patch

diff --git a/net/core/dev.c b/net/core/dev.c
index 0d0b983a6c21..f5e0a0d801fd 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -11121,7 +11121,7 @@  struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
 	if (!dev->ethtool)
 		goto free_all;
 
-	strcpy(dev->name, name);
+	strscpy(dev->name, name, sizeof(dev->name));
 	dev->name_assign_type = name_assign_type;
 	dev->group = INIT_NETDEV_GROUP;
 	if (!dev->ethtool_ops)