diff mbox series

net: bonding: Use strscpy() instead of manually-truncated strncpy()

Message ID 20210602181133.3326856-1-keescook@chromium.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: bonding: Use strscpy() instead of manually-truncated strncpy() | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 5 this patch: 5
netdev/header_inline success Link

Commit Message

Kees Cook June 2, 2021, 6:11 p.m. UTC
Silence this warning by just using strscpy() directly:

drivers/net/bonding/bond_main.c:4877:3: warning: 'strncpy' specified bound 16 equals destination size [-Wstringop-truncation]
    4877 |   strncpy(params->primary, primary, IFNAMSIZ);
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reported-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/lkml/202102150705.fdR6obB0-lkp@intel.com
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/net/bonding/bond_main.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Shoaib Rao June 2, 2021, 7:46 p.m. UTC | #1
Would it make sense to also replace the other strncpy in the same file.

Shoaib

On 6/2/21 11:11 AM, Kees Cook wrote:
> Silence this warning by just using strscpy() directly:
>
> drivers/net/bonding/bond_main.c:4877:3: warning: 'strncpy' specified bound 16 equals destination size [-Wstringop-truncation]
>      4877 |   strncpy(params->primary, primary, IFNAMSIZ);
>           |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Reported-by: kernel test robot <lkp@intel.com>
> Link: https://lore.kernel.org/lkml/202102150705.fdR6obB0-lkp@intel.com
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>   drivers/net/bonding/bond_main.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
> index c5a646d06102..ecfc48f2d0d0 100644
> --- a/drivers/net/bonding/bond_main.c
> +++ b/drivers/net/bonding/bond_main.c
> @@ -5329,10 +5329,8 @@ static int bond_check_params(struct bond_params *params)
>   			(struct reciprocal_value) { 0 };
>   	}
>   
> -	if (primary) {
> -		strncpy(params->primary, primary, IFNAMSIZ);
> -		params->primary[IFNAMSIZ - 1] = 0;
> -	}
> +	if (primary)
> +		strscpy(params->primary, primary, sizeof(params->primary));
>   
>   	memcpy(params->arp_targets, arp_target, sizeof(arp_target));
>
Kees Cook June 2, 2021, 8:02 p.m. UTC | #2
On Wed, Jun 02, 2021 at 12:46:46PM -0700, Rao Shoaib wrote:
> Would it make sense to also replace the other strncpy in the same file.

                strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);

I couldn't tell if this was a non-string, if it needed padding, etc. The
one I fixed below appears to be null-terminated? (Though now that I look
at it, perhaps it should be using strscpy_pad().)

And there are a bunch of other manual truncations in the kernel on
ifr_name:

$ git grep ifr_name | grep 'IFNAMSIZ.*=.*0'
drivers/net/tun.c:              ifr.ifr_name[IFNAMSIZ-1] = '\0';
net/core/dev_ioctl.c:   ifr->ifr_name[IFNAMSIZ-1] = 0;
net/core/dev_ioctl.c:   ifr->ifr_name[IFNAMSIZ-1] = 0;
net/decnet/dn_dev.c:    ifr->ifr_name[IFNAMSIZ-1] = 0;
net/ieee802154/socket.c:        ifr.ifr_name[IFNAMSIZ-1] = 0;
net/ipv4/devinet.c:     ifr->ifr_name[IFNAMSIZ - 1] = 0;
net/wireless/wext-core.c:       iwr.ifr_name[IFNAMSIZ-1] = 0;
tools/lib/bpf/xsk.c:    ifr.ifr_name[IFNAMSIZ - 1] = '\0';

And given the copy_to_user() that might happen, I think this should
absolutely be strscpy_pad(). I will send a v2...

-Kees

> 
> Shoaib
> 
> On 6/2/21 11:11 AM, Kees Cook wrote:
> > Silence this warning by just using strscpy() directly:
> > 
> > drivers/net/bonding/bond_main.c:4877:3: warning: 'strncpy' specified bound 16 equals destination size [-Wstringop-truncation]
> >      4877 |   strncpy(params->primary, primary, IFNAMSIZ);
> >           |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> > Reported-by: kernel test robot <lkp@intel.com>
> > Link: https://lore.kernel.org/lkml/202102150705.fdR6obB0-lkp@intel.com
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> >   drivers/net/bonding/bond_main.c | 6 ++----
> >   1 file changed, 2 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
> > index c5a646d06102..ecfc48f2d0d0 100644
> > --- a/drivers/net/bonding/bond_main.c
> > +++ b/drivers/net/bonding/bond_main.c
> > @@ -5329,10 +5329,8 @@ static int bond_check_params(struct bond_params *params)
> >   			(struct reciprocal_value) { 0 };
> >   	}
> > -	if (primary) {
> > -		strncpy(params->primary, primary, IFNAMSIZ);
> > -		params->primary[IFNAMSIZ - 1] = 0;
> > -	}
> > +	if (primary)
> > +		strscpy(params->primary, primary, sizeof(params->primary));
> >   	memcpy(params->arp_targets, arp_target, sizeof(arp_target));
diff mbox series

Patch

diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index c5a646d06102..ecfc48f2d0d0 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -5329,10 +5329,8 @@  static int bond_check_params(struct bond_params *params)
 			(struct reciprocal_value) { 0 };
 	}
 
-	if (primary) {
-		strncpy(params->primary, primary, IFNAMSIZ);
-		params->primary[IFNAMSIZ - 1] = 0;
-	}
+	if (primary)
+		strscpy(params->primary, primary, sizeof(params->primary));
 
 	memcpy(params->arp_targets, arp_target, sizeof(arp_target));