Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3777412pxv; Mon, 28 Jun 2021 12:36:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmCKygoTYfYVoEiSzLXr59kPu4hI9nzRZT3FTemOMul3Cnnw/BubA1kzjc2bu0FFhlnuI8 X-Received: by 2002:a92:c210:: with SMTP id j16mr19863905ilo.91.1624908987617; Mon, 28 Jun 2021 12:36:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624908987; cv=none; d=google.com; s=arc-20160816; b=ajxXHyXXQdMo6Kz2Ktv9Zd+kjuTpMhmhcoPlaOQij0XpoFxCO/1Bx/cllztkRCMhW6 qnfeDU4F7uxCYiMHrTBM1gQcgUtbUrgrBbMteB+huDH9Rg2H62Fg9+Qu5bqKYRRM5NDD 5uZwxr2gZcA5kGcCZRALEs8Zvp/r/xBhcUAIW/SCjLaLsrHZpJ6bZ0P2KdgZNtho/zro 0i1AWpVr3qNxJLrc9IU9ura0snLsAAfxhaIUUmmvuZzJ9vP6ZmzgH0cRtHpgztNUNOkL 8kyOrIvsNmpAi9eIFsZx+MQd6W9DuN2plMvDvZ/2E5vzZ3Yxq6YRIxxXDe19JdSbzbNw ehiw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9MhBYsywu9CZDwh2YjOmG2RVQyVkAlWUxcfTafPLQso=; b=oBW1z1zNEUOL6XthlmBiJyp6TUKRHndFRyh8Phobi9YtJnd0PSQMzSb67msqyyBCok 3kTDRCckVtBHLWKCLUFgy3JrftmIR4S2JOq1WxPzu0AKifUfXs+aaaksvX13tHKRKnmT H6Ynboz+C8ePHqMJ8aa3a/hGUBQYv61zIpJ6FZBXsO99izVKthUlE0LioSSmV9OO+vRe ea02epXJUnHUMveZ6j3w4unS52aAQFzXxYigx495518Ql17H7Wn5Kn/XjGvmHZGpFdYg Z1nmtaEryjykm819z3T/YhlEmV2dSvs+BErLRWLG2mqFQOStSY1jL7hO0NKmcHm7/clP EcxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=L4u8noM6; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d6si10940947ilu.46.2021.06.28.12.36.14; Mon, 28 Jun 2021 12:36:27 -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=@kernel.org header.s=k20201202 header.b=L4u8noM6; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232527AbhF1O4m (ORCPT + 99 others); Mon, 28 Jun 2021 10:56:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:51694 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234753AbhF1OmE (ORCPT ); Mon, 28 Jun 2021 10:42:04 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3848961CDE; Mon, 28 Jun 2021 14:33:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624890813; bh=SF2bmANnvRYxYI66X36HvX9n5DFTpDxUrV8yJ69/GgM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L4u8noM6n5WkOqERrhOx3YWp8spTZFXsDcCb1CMPNaEP61ia/HKPh8lp2xks8lMBz iPrAJIbyzo57IcaZEiJVjJVrlCw/RZm7aWkqrsvFztQ2zZwN0IBSEc511nWX8WDt7U W+yWBsC65YD/nqs9Zv/JWuffyXmBMGWyCnjloa3vlwURXhYMyqX6nx3gDc+Zi/lXYO 1tS6ATpsPdDXhB4od3P+HhKWGmWioNovGItf7m0PfJRp4y/buei4dvHtjeAGo30SsL 0KbLsuYV07dULbwHI7meidmph0whzKNDc0A5CWEePMYG0pfr+wGXytqlDUCVe8vwwd CBqb0t3YajlUA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Paolo Abeni , Kaustubh Pandey , "David S . Miller" , Sasha Levin Subject: [PATCH 4.19 029/109] udp: fix race between close() and udp_abort() Date: Mon, 28 Jun 2021 10:31:45 -0400 Message-Id: <20210628143305.32978-30-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628143305.32978-1-sashal@kernel.org> References: <20210628143305.32978-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.19.196-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-4.19.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 4.19.196-rc1 X-KernelTest-Deadline: 2021-06-30T14:32+00:00 X-stable: review X-Patchwork-Hint: Ignore 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 110af0e7dc7b..2ff9f774d446 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -2432,6 +2432,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) && up->encap_type) { @@ -2673,10 +2676,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 6799ad462be3..c4a76c6af205 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1476,6 +1476,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