Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3813060pxv; Mon, 28 Jun 2021 13:34:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJjqWJLUOWdrwa0VDBztmOtqx//d9N3xDXcSewFIL6oM4E3QjkQ0Raorg4emZFHxK0f2uD X-Received: by 2002:a92:c642:: with SMTP id 2mr20066803ill.109.1624912488828; Mon, 28 Jun 2021 13:34:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624912488; cv=none; d=google.com; s=arc-20160816; b=ArGRv62peEgDEeeV+ezVHMMuZB2pNkzYl3G7yoQfSNONowoUfkqHPDD1k6qUtqwrYD j+RFQoDMohZZ3al2bOwQzYPujvYqKtz5MNYpOBS+EWTs+xZtcMZ4HoTV3+BjO6ceqXCr /CI0/mbndfOTNzWkvnGD5lkX1aehTbL9PESEl5rcRO9rtz+/WSy2VR1y/e/O69FL4siy 31fzsz1UmKWrw769HWGTjoHp3LiRpIVL9L8pfDMI623upDNowGBbylzVRrqH4iyA3nd6 tRliq00t5lTdPAI0f8PQlDHJpRZknRytl5kxlVDDOfEcFQUCInJ7jfB4747OIb2ZH1bS CB5g== 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=qdlN3GZN8UT1QVhVM7SOaLbc6x3HgfAw201QcqPXRIo=; b=VfCgpfGob83aZbDTHYyM3AcOJ5+jOIpeDnwbxiFmKBDwaqANR3/LpPgM6iTwphKuI3 aJgT/ThBrqaKNd6TJ87cJKDvS1sTOPqsi2tjmYhj1zeC9VMDsjiy+ldMxsclM34dvas/ v5YIhCNrM6brkebRMfsWxbcq/6nfyc8V2kHxm1t693PVgXvWRYUttjHZLLLJcfWgfQ/d PIz0TYEnVoMFNG4jJ8Ie5+a2s2d9NIjRdBL8pj5slS9v5dmDgznhJPz6i+tGMT1lXHc+ HXl9HmOhYMC2fABO2RWcF/LmsFyUtGNbsbievv4jTyejoDGKdV7qopzcFJngoM+0Dlj1 dz+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eEqVSH+e; 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 h15si8520201ili.7.2021.06.28.13.34.33; Mon, 28 Jun 2021 13:34:48 -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=eEqVSH+e; 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 S233593AbhF1PKu (ORCPT + 99 others); Mon, 28 Jun 2021 11:10:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:55974 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235464AbhF1Ous (ORCPT ); Mon, 28 Jun 2021 10:50:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id ADAE761D23; Mon, 28 Jun 2021 14:37:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624891021; bh=C0lEDO6jNYOHF7tQ9FSRf52mFhZv9K5DAiQlUtVHw9A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eEqVSH+eggfnuJjgH3ZGhOz5eyUrBqSIdyI4Xot7+g02V2reVGoRnhsAf2fBaJXXC RhRMpLSHw/PAwydZHwDT0c1SszADU+rfGQDi4sV3hclfdc441JdKbNLwMOuztEVFMA xsKODiQkVf2saBFWbLVAiNCn0++1BtS+8iEoguDxK3+y+2qxvNgXLvO0YXah74UyOC uazvLCzK86KQohLQWjD3zzQA2f+FfqUkS2RSpQd7tSaHT5LitMKrKDIVHHPudN5s/A DqU4UneUqsPfj5jDd+3MfEeyQkUvTwT/wGm32FJp1/gYHdsVVX/zIcHFasKAJIqWsL BAdtbTJKal/Pw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Eric Dumazet , syzbot , "David S . Miller" , Sasha Levin Subject: [PATCH 4.14 35/88] net/af_unix: fix a data-race in unix_dgram_sendmsg / unix_release_sock Date: Mon, 28 Jun 2021 10:35:35 -0400 Message-Id: <20210628143628.33342-36-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: Eric Dumazet [ Upstream commit a494bd642d9120648b06bb7d28ce6d05f55a7819 ] While unix_may_send(sk, osk) is called while osk is locked, it appears unix_release_sock() can overwrite unix_peer() after this lock has been released, making KCSAN unhappy. Changing unix_release_sock() to access/change unix_peer() before lock is released should fix this issue. BUG: KCSAN: data-race in unix_dgram_sendmsg / unix_release_sock write to 0xffff88810465a338 of 8 bytes by task 20852 on cpu 1: unix_release_sock+0x4ed/0x6e0 net/unix/af_unix.c:558 unix_release+0x2f/0x50 net/unix/af_unix.c:859 __sock_release net/socket.c:599 [inline] sock_close+0x6c/0x150 net/socket.c:1258 __fput+0x25b/0x4e0 fs/file_table.c:280 ____fput+0x11/0x20 fs/file_table.c:313 task_work_run+0xae/0x130 kernel/task_work.c:164 tracehook_notify_resume include/linux/tracehook.h:189 [inline] exit_to_user_mode_loop kernel/entry/common.c:175 [inline] exit_to_user_mode_prepare+0x156/0x190 kernel/entry/common.c:209 __syscall_exit_to_user_mode_work kernel/entry/common.c:291 [inline] syscall_exit_to_user_mode+0x20/0x40 kernel/entry/common.c:302 do_syscall_64+0x56/0x90 arch/x86/entry/common.c:57 entry_SYSCALL_64_after_hwframe+0x44/0xae read to 0xffff88810465a338 of 8 bytes by task 20888 on cpu 0: unix_may_send net/unix/af_unix.c:189 [inline] unix_dgram_sendmsg+0x923/0x1610 net/unix/af_unix.c:1712 sock_sendmsg_nosec net/socket.c:654 [inline] sock_sendmsg net/socket.c:674 [inline] ____sys_sendmsg+0x360/0x4d0 net/socket.c:2350 ___sys_sendmsg net/socket.c:2404 [inline] __sys_sendmmsg+0x315/0x4b0 net/socket.c:2490 __do_sys_sendmmsg net/socket.c:2519 [inline] __se_sys_sendmmsg net/socket.c:2516 [inline] __x64_sys_sendmmsg+0x53/0x60 net/socket.c:2516 do_syscall_64+0x4a/0x90 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0xffff888167905400 -> 0x0000000000000000 Reported by Kernel Concurrency Sanitizer on: CPU: 0 PID: 20888 Comm: syz-executor.0 Not tainted 5.13.0-rc5-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet Reported-by: syzbot Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/unix/af_unix.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 44ff3f5c22df..8e7054fc27f8 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -535,12 +535,14 @@ static void unix_release_sock(struct sock *sk, int embrion) u->path.mnt = NULL; state = sk->sk_state; sk->sk_state = TCP_CLOSE; + + skpair = unix_peer(sk); + unix_peer(sk) = NULL; + unix_state_unlock(sk); wake_up_interruptible_all(&u->peer_wait); - skpair = unix_peer(sk); - if (skpair != NULL) { if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) { unix_state_lock(skpair); @@ -555,7 +557,6 @@ static void unix_release_sock(struct sock *sk, int embrion) unix_dgram_peer_wake_disconnect(sk, skpair); sock_put(skpair); /* It may now die */ - unix_peer(sk) = NULL; } /* Try to flush out this socket. Throw out buffers at least */ -- 2.30.2