From patchwork Fri Jun 6 12:56:15 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 356802 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3F2CD14009A for ; Fri, 6 Jun 2014 22:56:23 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751463AbaFFM4S (ORCPT ); Fri, 6 Jun 2014 08:56:18 -0400 Received: from mail-pb0-f52.google.com ([209.85.160.52]:34403 "EHLO mail-pb0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750921AbaFFM4R (ORCPT ); Fri, 6 Jun 2014 08:56:17 -0400 Received: by mail-pb0-f52.google.com with SMTP id rr13so2443673pbb.25 for ; Fri, 06 Jun 2014 05:56:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:subject:from:to:cc:date:in-reply-to:references :content-type:content-transfer-encoding:mime-version; bh=H+AjM/YgVL+W30h7vlaLNcQ1EMTzkctMlLuLdCZLV0o=; b=hrA8/UPyNC//9xAlk8csNlrQVxjkzXcT3ulV010ZhFDMDBGehafP55lr1AgJmbc9C3 jmBjX5asonZGOE1jrAKV4vxN6q/9y45Nc5IZ/M56Cm/8+w6n+Fc2XrGMc1O6X9TSsKHH oF/gtIeZgE10xavlTPUiUBxqcpsl9MxAytZfHnBgjc7tli6mQIavbG3wkuyO09XGQP6v JO/j1h/+2uIE1Aw+sf3dII1FrdhNWOcjRTFTeDvQ2TBUuvfIZ7/3DH77ZBxPDwoddEeV pYYQ2jX9W6hGqNXY7W8rWWFNE28QzPjbAS8LhxuByFOSZ1ukx4RhQOFiYNo4We5CfxuL U2Wg== X-Received: by 10.68.95.225 with SMTP id dn1mr495941pbb.126.1402059377261; Fri, 06 Jun 2014 05:56:17 -0700 (PDT) Received: from [172.19.253.212] ([172.19.253.212]) by mx.google.com with ESMTPSA id ko10sm35648248pbd.52.2014.06.06.05.56.16 for (version=SSLv3 cipher=RC4-SHA bits=128/128); Fri, 06 Jun 2014 05:56:16 -0700 (PDT) Message-ID: <1402059375.3645.284.camel@edumazet-glaptop2.roam.corp.google.com> Subject: Re: Potential race in ip4_datagram_release_cb From: Eric Dumazet To: Alexey Preobrazhensky Cc: netdev@vger.kernel.org, Kostya Serebryany , Dmitry Vyukov , Lars Bull , Eric Dumazet , Bruce Curtis , Maciej =?UTF-8?Q?=C5=BBenczykowski?= Date: Fri, 06 Jun 2014 05:56:15 -0700 In-Reply-To: References: X-Mailer: Evolution 3.2.3-0ubuntu6 Mime-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Fri, 2014-06-06 at 15:29 +0400, Alexey Preobrazhensky wrote: > Hello, > > I’m working on AddressSanitizer[1] -- a tool that detects > use-after-free and out-of-bounds bugs in kernel. > > We’ve encountered a heap-use-after-free in ip4_datagram_release_cb() > in linux kernel 3.15-rc5 (revision > 60b5f90d0fac7585f1a43ccdad06787b97eda0ab). > > It seems to be a race between dst_release() and > ip4_datagram_release_cb() on an object from ip_dst_cache slab, all > during the ip4_datagram_connect() call. > > This heap-use-after-free was triggered under trinity syscall fuzzer, > so there is no reproducer. > > It would be great if someone familiar with the code took time to look > into this report. > > Thanks, > Alexey > > [1] https://code.google.com/p/address-sanitizer/wiki/AddressSanitizerForKernel > > > AddressSanitizer: heap-use-after-free in ipv4_dst_check > Read of size 2 by thread T15453: > [] ipv4_dst_check+0x1a/0x90 ./net/ipv4/route.c:1116 > [] __sk_dst_check+0x89/0xe0 ./net/core/sock.c:531 > [] ip4_datagram_release_cb+0x46/0x390 ??:0 > [] release_sock+0x17a/0x230 ./net/core/sock.c:2413 > [] ip4_datagram_connect+0x462/0x5d0 ??:0 > [] inet_dgram_connect+0x76/0xd0 ./net/ipv4/af_inet.c:534 > [] SYSC_connect+0x15c/0x1c0 ./net/socket.c:1701 > [] SyS_connect+0xe/0x10 ./net/socket.c:1682 > [] system_call_fastpath+0x16/0x1b > ./arch/x86/kernel/entry_64.S:629 > > Freed by thread T15455: > [] dst_destroy+0xa8/0x160 ./net/core/dst.c:251 > [] dst_release+0x45/0x80 ./net/core/dst.c:280 > [] ip4_datagram_connect+0xa1/0x5d0 ??:0 > [] inet_dgram_connect+0x76/0xd0 ./net/ipv4/af_inet.c:534 > [] SYSC_connect+0x15c/0x1c0 ./net/socket.c:1701 > [] SyS_connect+0xe/0x10 ./net/socket.c:1682 > [] system_call_fastpath+0x16/0x1b > ./arch/x86/kernel/entry_64.S:629 > > Allocated by thread T15453: > [] dst_alloc+0x81/0x2b0 ./net/core/dst.c:171 > [] rt_dst_alloc+0x47/0x50 ./net/ipv4/route.c:1406 > [< inlined >] __ip_route_output_key+0x3e8/0xf70 > __mkroute_output ./net/ipv4/route.c:1939 > [] __ip_route_output_key+0x3e8/0xf70 ./net/ipv4/route.c:2161 > [] ip_route_output_flow+0x14/0x30 ./net/ipv4/route.c:2249 > [] ip4_datagram_connect+0x317/0x5d0 ??:0 > [] inet_dgram_connect+0x76/0xd0 ./net/ipv4/af_inet.c:534 > [] SYSC_connect+0x15c/0x1c0 ./net/socket.c:1701 > [] SyS_connect+0xe/0x10 ./net/socket.c:1682 > [] system_call_fastpath+0x16/0x1b > ./arch/x86/kernel/entry_64.S:629 > > The buggy address ffff880024ff2266 is located 102 bytes inside > of 192-byte region [ffff880024ff2200, ffff880024ff22c0) > > Memory state around the buggy address: > ffff880024ff1d00: ffffffff fffrrrrr rrrrrrrr rrrrrrrr > ffff880024ff1e00: ffffffff ffffffff ffffffff fffrrrrr > ffff880024ff1f00: rrrrrrrr rrrrrrrr rrrrrrrr rrrrrrrr > ffff880024ff2000: rrrrrrrr rrrrrrrr rrrrrrrr rrrrrrrr > ffff880024ff2100: rrrrrrrr rrrrrrrr rrrrrrrr rrrrrrrr > >ffff880024ff2200: ffffffff ffffffff ffffffff rrrrrrrr > ^ > ffff880024ff2300: rrrrrrrr rrrrrrrr ........ ........ > ffff880024ff2400: ........ rrrrrrrr rrrrrrrr rrrrrrrr > ffff880024ff2500: ffffffff ffffffff ffffffff rrrrrrrr > ffff880024ff2600: rrrrrrrr rrrrrrrr ffffffff ffffffff > ffff880024ff2700: ffffffff rrrrrrrr rrrrrrrr rrrrrrrr > Legend: > f - 8 freed bytes > r - 8 redzone bytes > . - 8 allocated bytes > x=1..7 - x allocated bytes + (8-x) redzone bytes > -- Yeah, we had many reports in the past that something was wrong ... Your nice report made me take a look, finally :( Problem comes from net/ipv4/udp.c:1008: sk_dst_set(sk, dst_clone(&rt->dst)); Could you try following patch ? Thanks ! --- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 4468e1adc094..d5e68ee63b8f 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1004,8 +1004,11 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) goto out; - if (connected) - sk_dst_set(sk, dst_clone(&rt->dst)); + if (connected) { + spin_lock_bh(&sk->sk_lock.slock); + __sk_dst_set(sk, dst_clone(&rt->dst)); + spin_unlock_bh(&sk->sk_lock.slock); + } } if (msg->msg_flags&MSG_CONFIRM)