Skip to content

Commit 3d47d62

Browse files
leitaogregkh
authored andcommitted
netpoll: pass buffer size to egress_dev() to avoid MAC truncation
commit 76b93a8 upstream. egress_dev() formats np->dev_mac via snprintf() but receives buf as a bare char *, so it cannot derive the buffer size from the pointer. The size argument was hardcoded to MAC_ADDR_STR_LEN (3 * ETH_ALEN - 1 = 17), which is silly wrong in two ways: 1) misleading kernel log output on the MAC-selected target path (np->dev_name[0] == '\0'); for example "aa:bb:cc:dd:ee:ff doesn't exist, aborting" was logged as "aa:bb:cc:dd:ee:f doesn't exist, aborting". 2) the second argument of snprintf is the size of the buffer, not the size of what you want to write. Add a bufsz parameter to egress_dev() and pass sizeof(buf) from each caller, matching the standard snprintf() idiom and removing the hardcoded size from the helper. Every caller already declares "char buf[MAC_ADDR_STR_LEN + 1]" so the formatted MAC continues to fit. Tested by booting with netconsole=6665@/aa:bb:cc:dd:ee:ff,6666@10.0.0.1/00:11:22:33:44:55 on a kernel without a matching device. Pre-fix dmesg shows "aa:bb:cc:dd:ee:f doesn't exist, aborting"; post-fix shows the full "aa:bb:cc:dd:ee:ff doesn't exist, aborting". Fixes: f8a10be ("netconsole: allow selection of egress interface via MAC address") Cc: stable@vger.kernel.org Signed-off-by: Breno Leitao <leitao@debian.org> Link: https://patch.msgid.link/20260501-netpoll_snprintf_fix-v1-1-84b0566e6597@debian.org Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1bca036 commit 3d47d62

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

net/core/netpoll.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,14 +608,16 @@ EXPORT_SYMBOL_GPL(__netpoll_setup);
608608
/*
609609
* Returns a pointer to a string representation of the identifier used
610610
* to select the egress interface for the given netpoll instance. buf
611-
* must be a buffer of length at least MAC_ADDR_STR_LEN + 1.
611+
* is used to format np->dev_mac when np->dev_name is empty; bufsz must
612+
* be at least MAC_ADDR_STR_LEN + 1 to fit the formatted MAC address
613+
* and its NUL terminator.
612614
*/
613-
static char *egress_dev(struct netpoll *np, char *buf)
615+
static char *egress_dev(struct netpoll *np, char *buf, size_t bufsz)
614616
{
615617
if (np->dev_name[0])
616618
return np->dev_name;
617619

618-
snprintf(buf, MAC_ADDR_STR_LEN, "%pM", np->dev_mac);
620+
snprintf(buf, bufsz, "%pM", np->dev_mac);
619621
return buf;
620622
}
621623

@@ -645,7 +647,7 @@ static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
645647

646648
if (!IS_ENABLED(CONFIG_IPV6)) {
647649
np_err(np, "IPv6 is not supported %s, aborting\n",
648-
egress_dev(np, buf));
650+
egress_dev(np, buf, sizeof(buf)));
649651
return -EINVAL;
650652
}
651653

@@ -667,7 +669,7 @@ static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
667669
}
668670
if (err) {
669671
np_err(np, "no IPv6 address for %s, aborting\n",
670-
egress_dev(np, buf));
672+
egress_dev(np, buf, sizeof(buf)));
671673
return err;
672674
}
673675

@@ -687,14 +689,14 @@ static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev)
687689
in_dev = __in_dev_get_rtnl(ndev);
688690
if (!in_dev) {
689691
np_err(np, "no IP address for %s, aborting\n",
690-
egress_dev(np, buf));
692+
egress_dev(np, buf, sizeof(buf)));
691693
return -EDESTADDRREQ;
692694
}
693695

694696
ifa = rtnl_dereference(in_dev->ifa_list);
695697
if (!ifa) {
696698
np_err(np, "no IP address for %s, aborting\n",
697-
egress_dev(np, buf));
699+
egress_dev(np, buf, sizeof(buf)));
698700
return -EDESTADDRREQ;
699701
}
700702

@@ -719,22 +721,23 @@ int netpoll_setup(struct netpoll *np)
719721
ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
720722

721723
if (!ndev) {
722-
np_err(np, "%s doesn't exist, aborting\n", egress_dev(np, buf));
724+
np_err(np, "%s doesn't exist, aborting\n",
725+
egress_dev(np, buf, sizeof(buf)));
723726
err = -ENODEV;
724727
goto unlock;
725728
}
726729
netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
727730

728731
if (netdev_master_upper_dev_get(ndev)) {
729732
np_err(np, "%s is a slave device, aborting\n",
730-
egress_dev(np, buf));
733+
egress_dev(np, buf, sizeof(buf)));
731734
err = -EBUSY;
732735
goto put;
733736
}
734737

735738
if (!netif_running(ndev)) {
736739
np_info(np, "device %s not up yet, forcing it\n",
737-
egress_dev(np, buf));
740+
egress_dev(np, buf, sizeof(buf)));
738741

739742
err = dev_open(ndev, NULL);
740743
if (err) {

0 commit comments

Comments
 (0)