Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3593837pxv; Mon, 28 Jun 2021 08:10:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyefTb1D4ot5QfX/XpIGIGDwF20YHA6T6lHNWu6b/EY3Xh2C96+az87NmVm/oKGPRIR5cVb X-Received: by 2002:a05:6e02:1cac:: with SMTP id x12mr16840294ill.273.1624893027031; Mon, 28 Jun 2021 08:10:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624893027; cv=none; d=google.com; s=arc-20160816; b=nPiDS5ruwUqEGfu054tFE+gbAwpHNXhNjrevAXhAL3UTpvlPv2W8U8zMZkgvGarwiC hijlIsZONdLQ06+F14l0ipdca/vkIoUn82SVf2nspCv4e10ITd0SKVY5qsJRGT8cIxmF VvkKf5MYzhdwSbTYzii1by0JYi8Z16/jlRexAE1YYpRDtkKRgzQTk0tVL5Vkfsk67gjy BIdt5pr6KtGMwsfRycwufel+zIjyLLVb8I9kuwhIDd/jabpkCJJa6W5aCCtf5nN9rXPy dh+3ddzb65D+6PhPbuldQR3EJbZW/4c5/RrCJGPRcx6e7lZNfhE46HZFI5gAvTCkaK0K IOqA== 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=pET8rDwa6ztV7aqUfQoucxTpyAafZgvpED+A/xM5IAw=; b=O2yU6R72IBuXhzNyiyJJrFq8Iq0YNgMcfaZz/5Jbxszd/PwcG7Rv1nhcc3XJYV8LC6 xZrT+PfiCdSiZBZhWtfgFr5TjMh4gzAQShWuJN+MwMi+w3CZ5bsm5lwjOLwW/YJ9BeQ/ ItIAZSxjujPAu/8DePc+1HpPAEi1eh1ZcFEFH1GHUnoMvs7M8HycApcTfruT20NGef/K qO+uGKOrn4P1NxSK8ToP1Qjsz8HLLsYNZrngA28xrKmhrbkI7vSBGF5SrOvA9KgvcY+L WQBjecZTDBmInlAuPx6U1A9A5vLF1O15SSKYevvcgg62g3SH1lwh8srfeGbuILYKfr/S dT9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cAL4pC2i; 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 p11si16522457ioo.38.2021.06.28.08.10.09; Mon, 28 Jun 2021 08:10: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=cAL4pC2i; 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 S237669AbhF1PKI (ORCPT + 99 others); Mon, 28 Jun 2021 11:10:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:54512 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236453AbhF1OtV (ORCPT ); Mon, 28 Jun 2021 10:49:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6583561D20; Mon, 28 Jun 2021 14:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624891013; bh=OptOz2fisIGCvH3Zsww+79CKj7rj2Pu5G7SE5nhXbGQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cAL4pC2iDrsP++yTgz12hSRmZxJ+SQVByeQOKHxuarcavCLz5foE391ElwUDRVZ1z w5y7kVzk0cbTcmcmmKiji+BIo77l4AYOUDkkRUs6kDSQiNQ3LvjJClrp4Efhd4+JDg 6bCDHY1AFYDcgfHQR7suqeX8GlFvTVAwofYuAwUDUi+J91M4U7M2J5CPn3z8yyUjQj UmUsoVdIVFiYheALSVZssRf8M9vID6SqnjyMupuyPjVBqn0V+vUTpAXSSyUpBJOmNu 84VmOE2ronfoSZ9ygiuNczCG8IwFVzPR5Y2fAOmK0zgM/8QMwwZok38cDd5ElqJZyM n2Su0LLZZDveg== 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.14 25/88] udp: fix race between close() and udp_abort() Date: Mon, 28 Jun 2021 10:35:25 -0400 Message-Id: <20210628143628.33342-26-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628143628.33342-1-sashal@kernel.org> References: <20210628143628.33342-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.14.238-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-4.14.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 4.14.238-rc1 X-KernelTest-Deadline: 2021-06-30T14:36+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 8f298f27f6ec..cf5c4d2f68c1 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -2337,6 +2337,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_key_false(&udp_encap_needed) && up->encap_type) { @@ -2570,10 +2573,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 38ad3fac8c37..d9d25b9c07ae 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1434,6 +1434,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