From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755781AbdDLX1G (ORCPT ); Wed, 12 Apr 2017 19:27:06 -0400 Received: from mail-qt0-f194.google.com ([209.85.216.194]:35787 "EHLO mail-qt0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755360AbdDLX1D (ORCPT ); Wed, 12 Apr 2017 19:27:03 -0400 MIME-Version: 1.0 In-Reply-To: References: <1492030046.10587.72.camel@edumazet-glaptop3.roam.corp.google.com> From: Willem de Bruijn Date: Wed, 12 Apr 2017 19:26:22 -0400 Message-ID: Subject: Re: net/ipv4: use-after-free in ipv4_datagram_support_cmsg To: Eric Dumazet Cc: Cong Wang , Andrey Konovalov , "David S. Miller" , Alexey Kuznetsov , James Morris , Hideaki YOSHIFUJI , Patrick McHardy , netdev , LKML , Eric Dumazet , Dmitry Vyukov , Kostya Serebryany , syzkaller Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 12, 2017 at 6:25 PM, Willem de Bruijn wrote: > On Wed, Apr 12, 2017 at 4:47 PM, Eric Dumazet wrote: >> On Wed, 2017-04-12 at 13:07 -0700, Cong Wang wrote: >>> On Wed, Apr 12, 2017 at 8:39 AM, Willem de Bruijn >>> wrote: >>> > =================== >>> >> BUG: KASAN: use-after-free in ipv4_datagram_support_cmsg >>> >> net/ipv4/ip_sockglue.c:500 [inline] at addr ffff880059be0128 >>> > >>> > Thanks for the report. This is accessing skb->dev from within recvmsg() at line >>> > >>> > info->ipi_ifindex = skb->dev->ifindex; >>> > >>> > Introduced in 829ae9d61165 ("net-timestamp: allow reading recv cmsg on >>> > errqueue with origin tstamp"). At this time the device may indeed have >>> > gone away. I'm having a look at a way to read this in the receive BH >>> > and store the ifindex. >>> >>> Why not use skb_iif? > > This code is called from the error path for transmit timestamps. > > We can make use of the fact that SKB_EXT_ERR used on enqueue has iif as > the first field in its control block. This also holds for the PKTINFO_SKB_CB > struct to which skb->cb is cast on dequeue when it copies pktinfo to userspace. > So if set on enqueue in __skb_complete_tx_timestamp, no conversion operation > is even needed on dequeue, let alone the currently buggy line that touches > skb->dev. > > This iif cast was added for this purpose in the receive path in 0b922b7a829c > ("net: original ingress device index in PKTINFO"). > > The device pointer is valid on enqueue for all paths called from device drivers, > as well as from dev_queue_xmit for SCM_TSTAMP_SCHED generation in > __dev_queue_xmit. The exception is SCM_TSTAMP_ACK generation, but > there skb->dev is NULL. > > The v6 path does need a conversion, but already does this in > ip6_datagram_recv_common_ctl. There, too, we can remove the buggy > logic to set it from skb->dev->ifindex in ip6_datagram_support_cmsg. > > I will send a patch. Sent http://patchwork.ozlabs.org/patch/750197