Skip to content

Commit 2efd4fc

Browse files
wdebruijdavem330
authored andcommitted
ip: in cmsg IP(V6)_ORIGDSTADDR call pskb_may_pull
Syzbot reported a read beyond the end of the skb head when returning IPV6_ORIGDSTADDR: BUG: KMSAN: kernel-infoleak in put_cmsg+0x5ef/0x860 net/core/scm.c:242 CPU: 0 PID: 4501 Comm: syz-executor128 Not tainted 4.17.0+ #9 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x185/0x1d0 lib/dump_stack.c:113 kmsan_report+0x188/0x2a0 mm/kmsan/kmsan.c:1125 kmsan_internal_check_memory+0x138/0x1f0 mm/kmsan/kmsan.c:1219 kmsan_copy_to_user+0x7a/0x160 mm/kmsan/kmsan.c:1261 copy_to_user include/linux/uaccess.h:184 [inline] put_cmsg+0x5ef/0x860 net/core/scm.c:242 ip6_datagram_recv_specific_ctl+0x1cf3/0x1eb0 net/ipv6/datagram.c:719 ip6_datagram_recv_ctl+0x41c/0x450 net/ipv6/datagram.c:733 rawv6_recvmsg+0x10fb/0x1460 net/ipv6/raw.c:521 [..] This logic and its ipv4 counterpart read the destination port from the packet at skb_transport_offset(skb) + 4. With MSG_MORE and a local SOCK_RAW sender, syzbot was able to cook a packet that stores headers exactly up to skb_transport_offset(skb) in the head and the remainder in a frag. Call pskb_may_pull before accessing the pointer to ensure that it lies in skb head. Link: http://lkml.kernel.org/r/CAF=yD-LEJwZj5a1-bAAj2Oy_hKmGygV6rsJ_WOrAYnv-fnayiQ@mail.gmail.com Reported-by: [email protected] Signed-off-by: Willem de Bruijn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ae2dcb2 commit 2efd4fc

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

net/ipv4/ip_sockglue.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,18 @@ static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
150150
{
151151
struct sockaddr_in sin;
152152
const struct iphdr *iph = ip_hdr(skb);
153-
__be16 *ports = (__be16 *)skb_transport_header(skb);
153+
__be16 *ports;
154+
int end;
154155

155-
if (skb_transport_offset(skb) + 4 > (int)skb->len)
156+
end = skb_transport_offset(skb) + 4;
157+
if (end > 0 && !pskb_may_pull(skb, end))
156158
return;
157159

158160
/* All current transport protocols have the port numbers in the
159161
* first four bytes of the transport header and this function is
160162
* written with this assumption in mind.
161163
*/
164+
ports = (__be16 *)skb_transport_header(skb);
162165

163166
sin.sin_family = AF_INET;
164167
sin.sin_addr.s_addr = iph->daddr;

net/ipv6/datagram.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,13 +700,16 @@ void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
700700
}
701701
if (np->rxopt.bits.rxorigdstaddr) {
702702
struct sockaddr_in6 sin6;
703-
__be16 *ports = (__be16 *) skb_transport_header(skb);
703+
__be16 *ports;
704+
int end;
704705

705-
if (skb_transport_offset(skb) + 4 <= (int)skb->len) {
706+
end = skb_transport_offset(skb) + 4;
707+
if (end <= 0 || pskb_may_pull(skb, end)) {
706708
/* All current transport protocols have the port numbers in the
707709
* first four bytes of the transport header and this function is
708710
* written with this assumption in mind.
709711
*/
712+
ports = (__be16 *)skb_transport_header(skb);
710713

711714
sin6.sin6_family = AF_INET6;
712715
sin6.sin6_addr = ipv6_hdr(skb)->daddr;

0 commit comments

Comments
 (0)