Skip to content

Commit d6539ee

Browse files
leitaogregkh
authored andcommitted
netpoll: pass buffer size to egress_dev() to avoid MAC truncation
[ Upstream commit 76b93a8 ] 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: Sasha Levin <sashal@kernel.org>
1 parent 3eaf609 commit d6539ee

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
@@ -695,14 +695,16 @@ EXPORT_SYMBOL_GPL(__netpoll_setup);
695695
/*
696696
* Returns a pointer to a string representation of the identifier used
697697
* to select the egress interface for the given netpoll instance. buf
698-
* must be a buffer of length at least MAC_ADDR_STR_LEN + 1.
698+
* is used to format np->dev_mac when np->dev_name is empty; bufsz must
699+
* be at least MAC_ADDR_STR_LEN + 1 to fit the formatted MAC address
700+
* and its NUL terminator.
699701
*/
700-
static char *egress_dev(struct netpoll *np, char *buf)
702+
static char *egress_dev(struct netpoll *np, char *buf, size_t bufsz)
701703
{
702704
if (np->dev_name[0])
703705
return np->dev_name;
704706

705-
snprintf(buf, MAC_ADDR_STR_LEN, "%pM", np->dev_mac);
707+
snprintf(buf, bufsz, "%pM", np->dev_mac);
706708
return buf;
707709
}
708710

@@ -732,7 +734,7 @@ static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
732734

733735
if (!IS_ENABLED(CONFIG_IPV6)) {
734736
np_err(np, "IPv6 is not supported %s, aborting\n",
735-
egress_dev(np, buf));
737+
egress_dev(np, buf, sizeof(buf)));
736738
return -EINVAL;
737739
}
738740

@@ -754,7 +756,7 @@ static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
754756
}
755757
if (err) {
756758
np_err(np, "no IPv6 address for %s, aborting\n",
757-
egress_dev(np, buf));
759+
egress_dev(np, buf, sizeof(buf)));
758760
return err;
759761
}
760762

@@ -774,14 +776,14 @@ static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev)
774776
in_dev = __in_dev_get_rtnl(ndev);
775777
if (!in_dev) {
776778
np_err(np, "no IP address for %s, aborting\n",
777-
egress_dev(np, buf));
779+
egress_dev(np, buf, sizeof(buf)));
778780
return -EDESTADDRREQ;
779781
}
780782

781783
ifa = rtnl_dereference(in_dev->ifa_list);
782784
if (!ifa) {
783785
np_err(np, "no IP address for %s, aborting\n",
784-
egress_dev(np, buf));
786+
egress_dev(np, buf, sizeof(buf)));
785787
return -EDESTADDRREQ;
786788
}
787789

@@ -823,22 +825,23 @@ int netpoll_setup(struct netpoll *np)
823825
ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
824826

825827
if (!ndev) {
826-
np_err(np, "%s doesn't exist, aborting\n", egress_dev(np, buf));
828+
np_err(np, "%s doesn't exist, aborting\n",
829+
egress_dev(np, buf, sizeof(buf)));
827830
err = -ENODEV;
828831
goto unlock;
829832
}
830833
netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
831834

832835
if (netdev_master_upper_dev_get(ndev)) {
833836
np_err(np, "%s is a slave device, aborting\n",
834-
egress_dev(np, buf));
837+
egress_dev(np, buf, sizeof(buf)));
835838
err = -EBUSY;
836839
goto put;
837840
}
838841

839842
if (!netif_running(ndev)) {
840843
np_info(np, "device %s not up yet, forcing it\n",
841-
egress_dev(np, buf));
844+
egress_dev(np, buf, sizeof(buf)));
842845

843846
err = dev_open(ndev, NULL);
844847
if (err) {

0 commit comments

Comments
 (0)