Skip to content

Commit

Permalink
net: bonding: Use strscpy_pad() instead of manually-truncated strncpy()
Browse files Browse the repository at this point in the history
Silence this warning by using strscpy_pad() 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);
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Additionally replace other strncpy() uses, as it is considered deprecated:
https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings

Reported-by: kernel test robot <[email protected]>
Link: https://lore.kernel.org/lkml/[email protected]
Acked-by: Jay Vosburgh <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
kees authored and davem330 committed Jun 3, 2021
1 parent 9c153d3 commit 4390207
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
8 changes: 3 additions & 5 deletions drivers/net/bonding/bond_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ static int bond_check_dev_link(struct bonding *bond,
*/

/* Yes, the mii is overlaid on the ifreq.ifr_ifru */
strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
strscpy_pad(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
mii = if_mii(&ifr);
if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
mii->reg_num = MII_BMSR;
Expand Down Expand Up @@ -5330,10 +5330,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_pad(params->primary, primary, sizeof(params->primary));

memcpy(params->arp_targets, arp_target, sizeof(arp_target));

Expand Down
3 changes: 1 addition & 2 deletions drivers/net/bonding/bond_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,7 @@ static int bond_option_primary_set(struct bonding *bond,
RCU_INIT_POINTER(bond->primary_slave, NULL);
bond_select_active_slave(bond);
}
strncpy(bond->params.primary, primary, IFNAMSIZ);
bond->params.primary[IFNAMSIZ - 1] = 0;
strscpy_pad(bond->params.primary, primary, IFNAMSIZ);

netdev_dbg(bond->dev, "Recording %s as primary, but it has not been enslaved yet\n",
primary);
Expand Down

0 comments on commit 4390207

Please sign in to comment.