net: fix a stack out-of-bound access
Subsystems (4)
cover net: fix a stack out-of-bound access
This patchset fixes the following kernel bug reported by Andrey:
BUG: KASAN: stack-out-of-bounds in bond_enslave+0xe0a/0x4ef0 at addr ffff8800666b7792 Write of size 16 by task a.out/3894 page:ffffea000199adc0 count:0 mapcount:0 mapping: (null) index:0x0 flags: 0x100000000000000() raw: 0100000000000000 0000000000000000 0000000000000000 00000000ffffffff raw: 0000000000000000 ffffea000199ade0 0000000000000000 0000000000000000 page dumped because: kasan: bad access detected CPU: 1 PID: 3894 Comm: a.out Not tainted 4.11.0-rc7+ #251 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:16 dump_stack+0x292/0x398 lib/dump_stack.c:52 kasan_report_error mm/kasan/report.c:212 kasan_report+0x4d8/0x510 mm/kasan/report.c:347 check_memory_region_inline mm/kasan/kasan.c:326 check_memory_region+0x139/0x190 mm/kasan/kasan.c:333 memcpy+0x37/0x50 mm/kasan/kasan.c:369 bond_enslave+0xe0a/0x4ef0 drivers/net/bonding/bond_main.c:1491 bond_do_ioctl+0xb5d/0xec0 drivers/net/bonding/bond_main.c:3449 dev_ifsioc+0x53f/0x9f0 net/core/dev_ioctl.c:338 dev_ioctl+0x249/0x1160 net/core/dev_ioctl.c:532 sock_do_ioctl+0x94/0xb0 net/socket.c:913 sock_ioctl+0x28f/0x440 net/socket.c:1004 vfs_ioctl fs/ioctl.c:45 do_vfs_ioctl+0x1bf/0x1780 fs/ioctl.c:685 SYSC_ioctl fs/ioctl.c:700 SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691 entry_SYSCALL_64_fastpath+0x1f/0xc2 arch/x86/entry/entry_64.S:204 RIP: 0033:0x7fc764b31b79 RSP: 002b:00007ffc1d73aa58 EFLAGS: 00000206 ORIG_RAX: 0000000000000010 RAX: ffffffffffffffda RBX: 00007ffc1d73abb0 RCX: 00007fc764b31b79 RDX: 00000000209eafd8 RSI: 0000008000008990 RDI: 0000000000000003 RBP: 00000000004004e0 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000206 R12: 0000000000000000 R13: 00007ffc1d73abb0 R14: 0000000000000000 R15: 0000000000000000
Please see each patch for details.
Cong Wang (3):
net: check mac address length for dev_set_mac_address()
bonding: use a larger struct for mac address
team: use a larger struct for mac address
drivers/net/bonding/bond_alb.c | 8 ++++---- drivers/net/bonding/bond_main.c | 17 +++++++++-------- drivers/net/bonding/bonding_priv.h | 6 ++++++ drivers/net/team/team.c | 9 ++++++--- include/linux/netdevice.h | 2 +- net/core/dev.c | 10 +++++++--- net/core/dev_ioctl.c | 2 ++ 7 files changed, 35 insertions(+), 19 deletions(-)
Signature · 1 line
2.5.5
PATCH 1/3 net: check mac address length for dev_set_mac_address()
dev_set_mac_address() accepts a struct sockaddr pointer as input but we have various types of mac addresse whose lengths are up to MAX_ADDR_LEN, this is confusing.
Make it void like ->ndo_set_mac_address() and let callers check its length before calling it. It is too late to fix dev_ifsioc() due to API compatiblity, so just reject those larger than sizeof(struct sockaddr).
This is also a preparation for the following patches.
--- include/linux/netdevice.h | 2 +- net/core/dev.c | 10 +++++++--- net/core/dev_ioctl.c | 2 ++ 3 files changed, 10 insertions(+), 4 deletions(-)
include/linux/netdevice.h +1 -1
@@ -3271,7 +3271,7 @@ int dev_set_alias(struct net_device *, const char *, size_t);int dev_change_net_namespace (struct net_device *,struct net *,const char *);int dev_set_mtu (struct net_device *,int );void dev_set_group (struct net_device *,int ); -int dev_set_mac_address (struct net_device *,struct sockaddr *); +int dev_set_mac_address (struct net_device *,void *);int dev_change_carrier (struct net_device *,bool new_carrier);int dev_get_phys_port_id (struct net_device *dev,struct netdev_phys_item_id *ppid);
net/core/dev.c +7 -3
@@ -6610,13 +6610,17 @@ EXPORT_SYMBOL(dev_set_group);/** * dev_set_mac_address - Change Media Access Control Address * @dev: device -* @sa: new address +* @addr: new address, whose type could be either struct sockaddr or +* any other compatible type whose length is up to MAX_ADDR_LEN depending +* on the dev->addr_len. Callers should check if its length is smaller than +* dev->addr_len!! * * Change the hardware (MAC) address of the device */ -int dev_set_mac_address (struct net_device *dev,struct sockaddr *sa) +int dev_set_mac_address (struct net_device *dev,void *addr) {const struct net_device_ops *ops = dev->netdev_ops; +struct sockaddr *sa = addr;int err;if (!ops->ndo_set_mac_address)
@@ -6625,7 +6629,7 @@ int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)return -EINVAL ;if (!netif_device_present (dev))return -ENODEV ; - err = ops->ndo_set_mac_address (dev, sa); + err = ops->ndo_set_mac_address (dev, addr);if (err)return err; dev->addr_assign_type =NET_ADDR_SET ;
net/core/dev_ioctl.c +2 -0
@@ -261,6 +261,8 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)return dev_set_mtu (dev, ifr->ifr_mtu);case SIOCSIFHWADDR : +if (dev->addr_len >sizeof (struct sockaddr )) +return -EINVAL ;return dev_set_mac_address (dev, &ifr->ifr_hwaddr);case SIOCSIFHWBROADCAST :
Signature · 1 line
2.5.5
PATCH 2/3 bonding: use a larger struct for mac address
In reply to Cong WangIPv6 tunnels use sizeof(struct in6_addr) as dev->addr_len, but in many places especially bonding, we use struct sockaddr to copy and set mac addr, this could lead to stack out-of-bounds access.
Fix it by using a larger address storage.
--- drivers/net/bonding/bond_alb.c | 8 ++++---- drivers/net/bonding/bond_main.c | 17 +++++++++-------- drivers/net/bonding/bonding_priv.h | 6 ++++++ 3 files changed, 19 insertions(+), 12 deletions(-)
drivers/net/bonding/bond_alb.c +4 -4
@@ -39,7 +39,7 @@#include <asm/byteorder.h> #include <net/bonding.h> #include <net/bond_alb.h> - +#include "bonding_priv.h" static const u8 mac_bcast[ETH_ALEN + 2] __long_aligned = {
@@ -1234,7 +1234,7 @@ static int alb_set_mac_address(struct bonding *bond, void *addr) {struct slave *slave, *rollback_slave;struct list_head *iter; -struct sockaddr sa; +struct bond_mac_addr sa;char tmp_addr[ETH_ALEN ];int res;
@@ -1257,8 +1257,8 @@ static int alb_set_mac_address(struct bonding *bond, void *addr)return 0 ; unwind: -memcpy (sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len); - sa.sa_family = bond->dev->type; +memcpy (sa.addr, bond->dev->dev_addr, bond->dev->addr_len); + sa.type = bond->dev->type;/* unwind from head to the slave that failed */ bond_for_each_slave (bond, rollback_slave, iter) {
drivers/net/bonding/bond_main.c +9 -8
@@ -1330,7 +1330,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)struct bonding *bond =netdev_priv (bond_dev);const struct net_device_ops *slave_ops = slave_dev->netdev_ops;struct slave *new_slave =NULL , *prev_slave; -struct sockaddr addr; +struct bond_mac_addr addr;int link_reporting;int res =0 , i;
@@ -1488,8 +1488,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)/* Set slave to master's mac address. The application already * set the master's mac address to that of the first slave */ -memcpy (addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len); - addr.sa_family = slave_dev->type; +memcpy (addr.addr, bond_dev->dev_addr, bond_dev->addr_len); + addr.type = slave_dev->type; res =dev_set_mac_address (slave_dev, &addr);if (res) {netdev_dbg (bond_dev,"Error %d calling set_mac_address\n" , res);
@@ -1773,8 +1773,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)* MAC if this slave's MAC is in use by the bond, or at * least print a warning. */ -ether_addr_copy (addr.sa_data, new_slave->perm_hwaddr); - addr.sa_family = slave_dev->type; +ether_addr_copy (addr.addr, new_slave->perm_hwaddr); + addr.type = slave_dev->type;dev_set_mac_address (slave_dev, &addr); }
@@ -3619,7 +3619,8 @@ static int bond_set_mac_address(struct net_device *bond_dev, void *addr) {struct bonding *bond =netdev_priv (bond_dev);struct slave *slave, *rollback_slave; -struct sockaddr *sa = addr, tmp_sa; +struct sockaddr *sa = addr; +struct bond_mac_addr tmp_sa;struct list_head *iter;int res =0 ;
@@ -3659,8 +3660,8 @@ static int bond_set_mac_address(struct net_device *bond_dev, void *addr)return 0 ; unwind: -memcpy (tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len); - tmp_sa.sa_family = bond_dev->type; +memcpy (tmp_sa.addr, bond_dev->dev_addr, bond_dev->addr_len); + tmp_sa.type = bond_dev->type;/* unwind from head to the slave that failed */ bond_for_each_slave (bond, rollback_slave, iter) {
drivers/net/bonding/bonding_priv.h +6 -0
@@ -22,4 +22,10 @@#define bond_version DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n" +/* Must be compatible with struct sockaddr */ +struct bond_mac_addr { +unsigned short type; +unsigned char addr[MAX_ADDR_LEN ]; +}; +#endif
Signature · 1 line
2.5.5
PATCH 3/3 team: use a larger struct for mac address
In reply to Cong WangIPv6 tunnels use sizeof(struct in6_addr) as dev->addr_len, but in many places especially bonding, we use struct sockaddr to copy and set mac addr, this could lead to stack out-of-bounds access.
Fix it by using a larger address storage.
--- drivers/net/team/team.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/net/team/team.c +6 -3
@@ -60,10 +60,13 @@ static struct team_port *team_port_get_rtnl(const struct net_device *dev)static int __set_port_dev_addr (struct net_device *port_dev,const unsigned char *dev_addr) { -struct sockaddr addr; +struct { +unsigned short type; +unsigned char addr[MAX_ADDR_LEN ]; + } addr; -memcpy (addr.sa_data, dev_addr, port_dev->addr_len); - addr.sa_family = port_dev->type; +memcpy (addr.addr, dev_addr, port_dev->addr_len); + addr.type = port_dev->type;return dev_set_mac_address (port_dev, &addr); }
Signature · 1 line
2.5.5
Jiri Pirko
maintainerRe: [Patch net 3/3] team: use a larger struct for mac address
Quoted 19 lines · Wed, Apr 26, 2017 at 07:03:23AM CEST, [email protected] wrote:
IPv6 tunnels use
sizeof(struct in6_addr)asdev->addr_len, but in many places especially bonding, we use struct sockaddr to copy and set mac addr, this could lead to stack out-of-bounds access.Fix it by using a larger address storage.
Reported-by Andrey Konovalov <[email protected]>Cc Jiri Pirko <[email protected]>Signed-off-by Cong Wang <[email protected]>---
drivers/net/team/team.c| 9 ++++++---1 file changed, 6 insertions(+), 3 deletions(-)drivers/net/team/team.c +0
-0
drivers/net/team/team.c static struct team_port *team_port_get_rtnl(const struct net_device *dev) · line 66
Quoted 8 lines
drivers/net/team/team.c +4
-1@@ -60,10 +60,13 @@ static struct team_port *team_port_get_rtnl(const struct net_device *dev)static int __set_port_dev_addr (struct net_device *port_dev,const unsigned char *dev_addr) { -struct sockaddr addr; +struct { +unsigned short type; +unsigned char addr[MAX_ADDR_LEN ]; + } addr;
Wouldn't it make sense to define this struct somewhere in the core headers?
Quoted 11 lines
drivers/net/team/team.c +2
-2-memcpy (addr.sa_data, dev_addr, port_dev->addr_len); - addr.sa_family = port_dev->type; +memcpy (addr.addr, dev_addr, port_dev->addr_len); + addr.type = port_dev->type;return dev_set_mac_address (port_dev, &addr); }Signature · 1 line
2.5.5
Re: [Patch net 3/3] team: use a larger struct for mac address
Quoted 15 lines · Jiri Pirko
Quoted 14 lines · Wed, Apr 26, 2017 at 07:03:23AM CEST, [email protected] wrote:
IPv6 tunnels use
sizeof(struct in6_addr)asdev->addr_len, but in many places especially bonding, we use struct sockaddr to copy and set mac addr, this could lead to stack out-of-bounds access.Fix it by using a larger address storage.
Reported-by Andrey Konovalov <[email protected]>Cc Jiri Pirko <[email protected]>Signed-off-by Cong Wang <[email protected]>---
drivers/net/team/team.c| 9 ++++++---1 file changed, 6 insertions(+), 3 deletions(-)
Quoted 16 lines
drivers/net/team/team.c static struct team_port *team_port_get_rtnl(const struct net_device *dev) · line 66
Quoted 13 lines
drivers/net/team/team.c +4
-1@@ -60,10 +60,13 @@ static struct team_port *team_port_get_rtnl(const struct net_device *dev)static int __set_port_dev_addr (struct net_device *port_dev,const unsigned char *dev_addr) { -struct sockaddr addr; +struct { +unsigned short type; +unsigned char addr[MAX_ADDR_LEN ]; + } addr;Wouldn't it make sense to define this struct somewhere in the core headers?
We already have struct sockaddr_storage that could be used throughout this set as well. We just converted a few pieces of the bonding driver over to using it for better support of ipoib bonds, via commit faeeb317a5615076dff1ff44b51e862e6064dbd0. Might be better to just use that in both bonding and team, rather than having different per-driver structs, or Yet Another Address Storage implementation.
Signature · 2 lines
Jarod Wilson [email protected]
Re: [Patch net 3/3] team: use a larger struct for mac address
Quoted 7 lines · Jarod Wilson
We already have struct
sockaddr_storagethat could be used throughout this set as well. We just converted a few pieces of the bonding driver over to using it for better support of ipoib bonds, via commitfaeeb317a5615076dff1ff44b51e862e6064dbd0. Might be better to just use that in both bonding and team, rather than having different per-driver structs, or Yet Another Address Storage implementation.
Technically, struct sockaddr_storage is not enough either, given the max is MAX_ADDR_LEN. This is why I gave up on sockaddr_storage.
Re: [Patch net 3/3] team: use a larger struct for mac address
Quoted 11 lines · Cong Wang
Quoted 7 lines · Jarod Wilson
We already have struct
sockaddr_storagethat could be used throughout this set as well. We just converted a few pieces of the bonding driver over to using it for better support of ipoib bonds, via commitfaeeb317a5615076dff1ff44b51e862e6064dbd0. Might be better to just use that in both bonding and team, rather than having different per-driver structs, or Yet Another Address Storage implementation.Technically, struct
sockaddr_storageis not enough either, given the max isMAX_ADDR_LEN. This is why I gave up onsockaddr_storage.
Wait, what? Am I missing something? MAX_ADDR_LEN is 32, and sockaddr_storage is a #define for __kernel_sockaddr_storage, which has it's __data member defined as being of size 128 - sizeof(unsigned short).
Signature · 2 lines
Jarod Wilson [email protected]
Re: [Patch net 3/3] team: use a larger struct for mac address
Quoted 16 lines · Jarod Wilson
Quoted 15 lines · Cong Wang
Quoted 11 lines · Jarod Wilson
We already have struct
sockaddr_storagethat could be used throughout this
set as well. We just converted a few pieces of the bonding driver over to using it for better support of ipoib bonds, via commitfaeeb317a5615076dff1ff44b51e862e6064dbd0. Might be better to just use that
in both bonding and team, rather than having different per-driver structs,
or Yet Another Address Storage implementation.
Quoted 7 lines
Quoted 2 lines
Technically, struct
sockaddr_storageis not enough either, given the max isMAX_ADDR_LEN. This is why I gave up onsockaddr_storage.Wait, what? Am I missing something?
MAX_ADDR_LENis 32, andsockaddr_storageis a #define for__kernel_sockaddr_storage, which has it's__datamember defined as being of size 128 - sizeof(unsigned short).
My bad, I thought it is same with sizeof(in6addr) without looking into it. The question is, why do we waste 126 - 32 = 94 bytes on stack to just use struct sockaddr_storage?
I totally understand we want a unified struct, but we already redefine it in multiple places in tree...
Re: [Patch net 3/3] team: use a larger struct for mac address
Quoted 21 lines · Cong Wang
Quoted 20 lines · Jarod Wilson
Quoted 17 lines · Cong Wang
Quoted 11 lines · Jarod Wilson
We already have struct
sockaddr_storagethat could be used throughout this
set as well. We just converted a few pieces of the bonding driver over to using it for better support of ipoib bonds, via commitfaeeb317a5615076dff1ff44b51e862e6064dbd0. Might be better to just use that
in both bonding and team, rather than having different per-driver structs,
or Yet Another Address Storage implementation.Technically, struct
sockaddr_storageis not enough either, given the max isMAX_ADDR_LEN. This is why I gave up onsockaddr_storage.
Quoted 7 lines
Quoted 3 lines
Wait, what? Am I missing something?
MAX_ADDR_LENis 32, andsockaddr_storageis a #define for__kernel_sockaddr_storage, which has it's__datamember defined as being of size 128 - sizeof(unsigned short).My bad, I thought it is same with sizeof(in6addr) without looking into it. The question is, why do we waste 126 - 32 = 94 bytes on stack to just use struct
sockaddr_storage?
That's a fair point.
Quoted 2 lines
I totally understand we want a unified struct, but we already redefine it in multiple places in tree...
Something unified and centralized with a data storage of MAX_ADDR_LEN does seem reasonable to get both consistency and minimized waste, and I could certainly do a follow-up patch for the bonding driver to switch the bits now using sockaddr_storage over to whatever new struct gets added.
Signature · 2 lines
Jarod Wilson [email protected]
Re: [Patch net 3/3] team: use a larger struct for mac address
In reply to Jiri PirkoQuoted 15 lines · Jiri Pirko
Quoted 14 lines · Wed, Apr 26, 2017 at 07:03:23AM CEST, [email protected] wrote:
IPv6 tunnels use
sizeof(struct in6_addr)asdev->addr_len, but in many places especially bonding, we use struct sockaddr to copy and set mac addr, this could lead to stack out-of-bounds access.Fix it by using a larger address storage.
Reported-by Andrey Konovalov <[email protected]>Cc Jiri Pirko <[email protected]>Signed-off-by Cong Wang <[email protected]>---
drivers/net/team/team.c| 9 ++++++---1 file changed, 6 insertions(+), 3 deletions(-)
Quoted 16 lines
drivers/net/team/team.c static struct team_port *team_port_get_rtnl(const struct net_device *dev) · line 66
Quoted 13 lines
drivers/net/team/team.c +4
-1@@ -60,10 +60,13 @@ static struct team_port *team_port_get_rtnl(const struct net_device *dev)static int __set_port_dev_addr (struct net_device *port_dev,const unsigned char *dev_addr) { -struct sockaddr addr; +struct { +unsigned short type; +unsigned char addr[MAX_ADDR_LEN ]; + } addr;Wouldn't it make sense to define this struct somewhere in the core headers?
I _did_ use a struct mac_addr until I found there are multiple places in the tree already defining it... We are in a similar situation to the union of struct in_addr and struct in6_addr, unfortunately.
We can always clean up these for net-next.