Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3923748pxj; Mon, 21 Jun 2021 09:28:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwWhPEEonkvlNvD5cJCUP9diNznnDb2PyrN3fs9RMetSf0ip2cqPXhTo3EBuqNb7oNawdnD X-Received: by 2002:aa7:ccd4:: with SMTP id y20mr22611309edt.151.1624292898852; Mon, 21 Jun 2021 09:28:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624292898; cv=none; d=google.com; s=arc-20160816; b=zYVWP5SxgqSOqufe1tG3tB4FCMjzeDJGZvOFyP+q4CjsPLdax1WxpUoxOUU9l+36z1 66ZNXb62Nz2H6cJHSf806nvqD+0gJj7GoztTOP9dcUpmcwPoA2B+j7MmG4092AfhdVst +VSFoq6O4NFpadJJeCDHb5cjYOHxTEKT+eapsOYLcoi6r4HQskWyK48Pg4PrXBfR+b9k oaY562S0C3oSziV1sij/UhPg2McCP0sooJDxkoGwSenSqSRaxvuFJLB7g4St9BIFoa+i sVa4p8tK3K0m/HcMHC+9DAaPel1nQV8e9ZaHnqS6H90SjevvEFCAp4GJyVjY28InFP+V zxmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HE1sJjULb1vgZLfP7weQw/XUSJjPK9vKIxvMrHRZ4ig=; b=s9vAlVjsVsqPWDX78+gfAGfv74GoTmB54gj5vZM1Ra18Jg8kk74aKuH7+qUHp1VXDM 7gehe//AyI0jnIsKJRarYV4cJGJUSsKjosINyLkKbG2o4VRAMmnWBfywY40KB1vclGNo xbiyrHVg9c5x+rVp4ghjw/B87zppIAyq7sUphSYe6PTcP6OfwL9o2OZsfF7jNcjZl3F0 GVKF9fjMzSzY3JFiHevbreYtfly8Iz3+tN605zhlf+x8wnj0YVc3o9htRboCU0HJRLoE wi90Oe5e4UIVtNZiMuSOweck9MkgC6Vktfxo/m4YP9EN+FbZ9yWTuVMpv4IStSRyRYv7 E+zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XDGkoPe8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce26si7611704ejc.40.2021.06.21.09.27.56; Mon, 21 Jun 2021 09:28:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XDGkoPe8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231705AbhFUQ1I (ORCPT + 99 others); Mon, 21 Jun 2021 12:27:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:42340 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232113AbhFUQZU (ORCPT ); Mon, 21 Jun 2021 12:25:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CE9BF611C1; Mon, 21 Jun 2021 16:21:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292505; bh=QyJQeR/1wF27IRXTZXQA8A07iAp10orJQxYuY6Pohas=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XDGkoPe85/QAskcX2FNY2NxlHKCzvuIQsiQuCXUHgNL49LWApopl8XdBwGSTksqdc 4a9EPRq8WxCcbRB0qSF+2/cQwtOjt6jfPdSiE0vhTqlRcMjLUKuJqHm7e0bMMD8IN8 AsgU4dU+s/6UID4Va3Wyfyod60ClSOlWzjt4JIQY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paolo Abeni , "David S. Miller" , Sasha Levin , Kaustubh Pandey Subject: [PATCH 5.10 025/146] udp: fix race between close() and udp_abort() Date: Mon, 21 Jun 2021 18:14:15 +0200 Message-Id: <20210621154912.127290897@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154911.244649123@linuxfoundation.org> References: <20210621154911.244649123@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paolo Abeni [ Upstream commit a8b897c7bcd47f4147d066e22cc01d1026d7640e ] Kaustubh reported and diagnosed a panic in udp_lib_lookup(). The root cause is udp_abort() racing with close(). Both racing functions acquire the socket lock, but udp{v6}_destroy_sock() release it before performing destructive actions. We can't easily extend the socket lock scope to avoid the race, instead use the SOCK_DEAD flag to prevent udp_abort from doing any action when the critical race happens. Diagnosed-and-tested-by: Kaustubh Pandey Fixes: 5d77dca82839 ("net: diag: support SOCK_DESTROY for UDP sockets") Signed-off-by: Paolo Abeni Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ipv4/udp.c | 10 ++++++++++ net/ipv6/udp.c | 3 +++ 2 files changed, 13 insertions(+) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 9d28b2778e8f..fbb9a11fe4a3 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -2569,6 +2569,9 @@ void udp_destroy_sock(struct sock *sk) { struct udp_sock *up = udp_sk(sk); bool slow = lock_sock_fast(sk); + + /* protects from races with udp_abort() */ + sock_set_flag(sk, SOCK_DEAD); udp_flush_pending_frames(sk); unlock_sock_fast(sk, slow); if (static_branch_unlikely(&udp_encap_needed_key)) { @@ -2819,10 +2822,17 @@ int udp_abort(struct sock *sk, int err) { lock_sock(sk); + /* udp{v6}_destroy_sock() sets it under the sk lock, avoid racing + * with close() + */ + if (sock_flag(sk, SOCK_DEAD)) + goto out; + sk->sk_err = err; sk->sk_error_report(sk); __udp_disconnect(sk, 0); +out: release_sock(sk); return 0; diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index 29d9691359b9..e2de58d6cdce 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1596,6 +1596,9 @@ void udpv6_destroy_sock(struct sock *sk) { struct udp_sock *up = udp_sk(sk); lock_sock(sk); + + /* protects from races with udp_abort() */ + sock_set_flag(sk, SOCK_DEAD); udp_v6_flush_pending_frames(sk); release_sock(sk); -- 2.30.2