aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWillem de Bruijn <willemb@google.com>2017-04-12 19:24:35 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2017-05-03 11:36:36 -0400
commit8c04e2acd5371387f5e42e3bab412c291d108391 (patch)
tree7b1160bf38b1127633b85bd1fa72ea9fa1996f31
parentc86872a43400c1e5df2acbd9bb23e3941ef1c98d (diff)
net-timestamp: avoid use-after-free in ip_recv_error
[ Upstream commit 1862d6208db0aeca9c8ace44915b08d5ab2cd667 ] Syzkaller reported a use-after-free in ip_recv_error at line info->ipi_ifindex = skb->dev->ifindex; This function is called on dequeue from the error queue, at which point the device pointer may no longer be valid. Save ifindex on enqueue in __skb_complete_tx_timestamp, when the pointer is valid or NULL. Store it in temporary storage skb->cb. It is safe to reference skb->dev here, as called from device drivers or dev_queue_xmit. The exception is when called from tcp_ack_tstamp; in that case it is NULL and ifindex is set to 0 (invalid). Do not return a pktinfo cmsg if ifindex is 0. This maintains the current behavior of not returning a cmsg if skb->dev was NULL. On dequeue, the ipv4 path will cast from sock_exterr_skb to in_pktinfo. Both have ifindex as their first element, so no explicit conversion is needed. This is by design, introduced in commit 0b922b7a829c ("net: original ingress device index in PKTINFO"). For ipv6 ip6_datagram_support_cmsg converts to in6_pktinfo. Fixes: 829ae9d61165 ("net-timestamp: allow reading recv cmsg on errqueue with origin tstamp") Reported-by: Andrey Konovalov <andreyknvl@google.com> Signed-off-by: Willem de Bruijn <willemb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--net/core/skbuff.c1
-rw-r--r--net/ipv4/ip_sockglue.c9
-rw-r--r--net/ipv6/datagram.c10
3 files changed, 6 insertions, 14 deletions
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index f0f462c0573d..5ec34eae3f0a 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -3779,6 +3779,7 @@ static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3779 serr->ee.ee_errno = ENOMSG; 3779 serr->ee.ee_errno = ENOMSG;
3780 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3780 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3781 serr->ee.ee_info = tstype; 3781 serr->ee.ee_info = tstype;
3782 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
3782 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 3783 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3783 serr->ee.ee_data = skb_shinfo(skb)->tskey; 3784 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3784 if (sk->sk_protocol == IPPROTO_TCP && 3785 if (sk->sk_protocol == IPPROTO_TCP &&
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index 9826695ddfc6..4d37bdcbc2d5 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -474,16 +474,15 @@ static bool ipv4_datagram_support_cmsg(const struct sock *sk,
474 return false; 474 return false;
475 475
476 /* Support IP_PKTINFO on tstamp packets if requested, to correlate 476 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
477 * timestamp with egress dev. Not possible for packets without dev 477 * timestamp with egress dev. Not possible for packets without iif
478 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY). 478 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
479 */ 479 */
480 if ((!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) || 480 info = PKTINFO_SKB_CB(skb);
481 (!skb->dev)) 481 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
482 !info->ipi_ifindex)
482 return false; 483 return false;
483 484
484 info = PKTINFO_SKB_CB(skb);
485 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr; 485 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
486 info->ipi_ifindex = skb->dev->ifindex;
487 return true; 486 return true;
488} 487}
489 488
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index 8616d17cf08f..442ec1f39ed1 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -400,9 +400,6 @@ static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
400 * At one point, excluding local errors was a quick test to identify icmp/icmp6 400 * At one point, excluding local errors was a quick test to identify icmp/icmp6
401 * errors. This is no longer true, but the test remained, so the v6 stack, 401 * errors. This is no longer true, but the test remained, so the v6 stack,
402 * unlike v4, also honors cmsg requests on all wifi and timestamp errors. 402 * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
403 *
404 * Timestamp code paths do not initialize the fields expected by cmsg:
405 * the PKTINFO fields in skb->cb[]. Fill those in here.
406 */ 403 */
407static bool ip6_datagram_support_cmsg(struct sk_buff *skb, 404static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
408 struct sock_exterr_skb *serr) 405 struct sock_exterr_skb *serr)
@@ -414,14 +411,9 @@ static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
414 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL) 411 if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
415 return false; 412 return false;
416 413
417 if (!skb->dev) 414 if (!IP6CB(skb)->iif)
418 return false; 415 return false;
419 416
420 if (skb->protocol == htons(ETH_P_IPV6))
421 IP6CB(skb)->iif = skb->dev->ifindex;
422 else
423 PKTINFO_SKB_CB(skb)->ipi_ifindex = skb->dev->ifindex;
424
425 return true; 417 return true;
426} 418}
427 419