packet: validate msg_namelen in send directly
Subsystems (3)
Willem de Bruijn
maintainerPATCH 1/1 packet: validate msg_namelen in send directly
From: Willem de Bruijn <[email protected]>
Packet sockets in datagram mode take a destination address. Verify its length before passing to dev_hard_header.
Prior to 2.6.14-rc3, the send code ignored sll_halen. This is established behavior. Directly compare msg_namelen to dev->addr_len.
--- net/packet/af_packet.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
net/packet/af_packet.c +12 -6
@@ -2624,10 +2624,13 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) sll_addr))) goto out; proto = saddr->sll_protocol; - addr = saddr->sll_halen ? saddr->sll_addr :NULL ; dev =dev_get_by_index (sock_net (&po->sk), saddr->sll_ifindex); -if (addr && dev && saddr->sll_halen < dev->addr_len) - goto out_put; +if (po->sk.sk_socket->type ==SOCK_DGRAM ) { + addr = saddr->sll_addr; +if (dev && msg->msg_namelen < dev->addr_len + + offsetof(struct sockaddr_ll , sll_addr)) + goto out_put; + } } err = -ENXIO ;
@@ -2824,10 +2827,13 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll , sll_addr))) goto out; proto = saddr->sll_protocol; - addr = saddr->sll_halen ? saddr->sll_addr :NULL ; dev =dev_get_by_index (sock_net (sk), saddr->sll_ifindex); -if (addr && dev && saddr->sll_halen < dev->addr_len) - goto out_unlock; +if (sock->type ==SOCK_DGRAM ) { + addr = saddr->sll_addr; +if (dev && msg->msg_namelen < dev->addr_len + + offsetof(struct sockaddr_ll , sll_addr)) + goto out_unlock; + } } err = -ENXIO ;
Signature · 1 line
2.21.0.593.g511ec345e18-goog
From: Willem de Bruijn
Quoted 22 lines
Sent: 26 April 2019 20:28
Packet sockets in datagram mode take a destination address. Verify its length before passing todev_hard_header.Prior to 2.6.14-rc3, the send code ignored
sll_halen. This is established behavior. Directly comparemsg_namelentodev->addr_len.Fixes 6b8d95f1795c4 ("packet: validate address length if non-zero")Suggested-by David Laight <[email protected]>Signed-off-by Willem de Bruijn <[email protected]>--- net/packet/af_packet.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)net/packet/af_packet.c +0
-0@@ -2624,10 +2624,13 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) sll_addr))) goto out; proto = saddr->sll_protocol;
net/packet/af_packet.c static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) · line 2633
Quoted 10 lines
net/packet/af_packet.c +6
-3- addr = saddr->sll_halen ? saddr->sll_addr :NULL ; dev =dev_get_by_index (sock_net (&po->sk), saddr->sll_ifindex); -if (addr && dev && saddr->sll_halen < dev->addr_len) - goto out_put; +if (po->sk.sk_socket->type ==SOCK_DGRAM ) { + addr = saddr->sll_addr; +if (dev && msg->msg_namelen < dev->addr_len + + offsetof(struct sockaddr_ll , sll_addr)) + goto out_put; + }
IIRC you need to initialise 'addr - NULL' at the top of the functions. I'm surprised the compiler doesn't complain.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Willem de Bruijn
maintainerQuoted 29 lines · David Laight
From: Willem de Bruijn
Quoted 27 lines
Sent: 26 April 2019 20:28
Packet sockets in datagram mode take a destination address. Verify its length before passing todev_hard_header.Prior to 2.6.14-rc3, the send code ignored
sll_halen. This is established behavior. Directly comparemsg_namelentodev->addr_len.Fixes 6b8d95f1795c4 ("packet: validate address length if non-zero")Suggested-by David Laight <[email protected]>Signed-off-by Willem de Bruijn <[email protected]>--- net/packet/af_packet.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)net/packet/af_packet.c +1
-3@@ -2624,10 +2624,13 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) sll_addr))) goto out; proto = saddr->sll_protocol; - addr = saddr->sll_halen ? saddr->sll_addr :NULL ; dev =dev_get_by_index (sock_net (&po->sk), saddr->sll_ifindex); -if (addr && dev && saddr->sll_halen < dev->addr_len) - goto out_put; +if (po->sk.sk_socket->type ==SOCK_DGRAM ) {
Quoted 8 lines
net/packet/af_packet.c static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) · line 2633
Quoted 5 lines
net/packet/af_packet.c +5
-0+ addr = saddr->sll_addr; +if (dev && msg->msg_namelen < dev->addr_len + + offsetof(struct sockaddr_ll , sll_addr)) + goto out_put; + }IIRC you need to initialise 'addr - NULL' at the top of the functions. I'm surprised the compiler doesn't complain.
It did complain when I moved it below the if (dev && ..) branch. But inside a branch with exactly the same condition as the one where used, the compiler did figure it out. Admittedly that is fragile. Then it might be simplest to restore the unconditional assignment
proto = saddr->sll_protocol;
+ addr = saddr->sll_addr;
dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);From: Willem de Bruijn
Quoted 40 lines
Sent: 29 April 2019 13:53
Quoted 29 lines · David Laight
From: Willem de Bruijn
Quoted 27 lines
Sent: 26 April 2019 20:28
Packet sockets in datagram mode take a destination address. Verify its length before passing todev_hard_header.Prior to 2.6.14-rc3, the send code ignored
sll_halen. This is established behavior. Directly comparemsg_namelentodev->addr_len.Fixes 6b8d95f1795c4 ("packet: validate address length if non-zero")Suggested-by David Laight <[email protected]>Signed-off-by Willem de Bruijn <[email protected]>--- net/packet/af_packet.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)net/packet/af_packet.c +1
-3@@ -2624,10 +2624,13 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) sll_addr))) goto out; proto = saddr->sll_protocol; - addr = saddr->sll_halen ? saddr->sll_addr :NULL ; dev =dev_get_by_index (sock_net (&po->sk), saddr->sll_ifindex); -if (addr && dev && saddr->sll_halen < dev->addr_len) - goto out_put; +if (po->sk.sk_socket->type ==SOCK_DGRAM ) {Quoted 8 lines
net/packet/af_packet.c static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) · line 2633
Quoted 5 lines
net/packet/af_packet.c +5
-0+ addr = saddr->sll_addr; +if (dev && msg->msg_namelen < dev->addr_len + + offsetof(struct sockaddr_ll , sll_addr)) + goto out_put; + }IIRC you need to initialise 'addr - NULL' at the top of the functions. I'm surprised the compiler doesn't complain.
Quoted 3 lines
It did complain when I moved it below the if (dev && ..) branch. But inside a branch with exactly the same condition as the one where used, the compiler did figure it out. Admittedly that is fragile.
Even a function call should be enough since the called code is allowed to modify po->sk.sk_socket->type via a global pointer.
Quoted 5 lines
Then it might be simplest to restore the unconditional assignment
proto = saddr->sll_protocol; + addr = saddr->sll_addr; dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
There is an 'addr = NULL' in the 'address absent' branch. Moving that higher up makes it even more clear that the address is only set in one place.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Willem de Bruijn
maintainerQuoted 51 lines · David Laight
From: Willem de Bruijn
Quoted 40 lines
Sent: 29 April 2019 13:53
Quoted 29 lines · David Laight
From: Willem de Bruijn
Quoted 27 lines
Sent: 26 April 2019 20:28
Packet sockets in datagram mode take a destination address. Verify its length before passing todev_hard_header.Prior to 2.6.14-rc3, the send code ignored
sll_halen. This is established behavior. Directly comparemsg_namelentodev->addr_len.Fixes 6b8d95f1795c4 ("packet: validate address length if non-zero")Suggested-by David Laight <[email protected]>Signed-off-by Willem de Bruijn <[email protected]>--- net/packet/af_packet.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)net/packet/af_packet.c +1
-3@@ -2624,10 +2624,13 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) sll_addr))) goto out; proto = saddr->sll_protocol; - addr = saddr->sll_halen ? saddr->sll_addr :NULL ; dev =dev_get_by_index (sock_net (&po->sk), saddr->sll_ifindex); -if (addr && dev && saddr->sll_halen < dev->addr_len) - goto out_put; +if (po->sk.sk_socket->type ==SOCK_DGRAM ) {Quoted 8 lines
net/packet/af_packet.c static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) · line 2633
Quoted 5 lines
net/packet/af_packet.c +5
-0+ addr = saddr->sll_addr; +if (dev && msg->msg_namelen < dev->addr_len + + offsetof(struct sockaddr_ll , sll_addr)) + goto out_put; + }IIRC you need to initialise 'addr - NULL' at the top of the functions. I'm surprised the compiler doesn't complain.
Quoted 3 lines
It did complain when I moved it below the if (dev && ..) branch. But inside a branch with exactly the same condition as the one where used, the compiler did figure it out. Admittedly that is fragile.
Even a function call should be enough since the called code is allowed to modify
po->sk.sk_socket->typevia a global pointer.Quoted 2 lines
Then it might be simplest to restore the unconditional assignment
Quoted 7 lines
Quoted 3 lines
proto = saddr->sll_protocol; + addr = saddr->sll_addr; dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);There is an 'addr = NULL' in the 'address absent' branch. Moving that higher up makes it even more clear that the address is only set in one place.
I can do that. Only, touching code in multiple locations can make backporting to stable branches more difficult.