Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp5046835pxb; Mon, 15 Feb 2021 08:09:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJx600+5W1XCvQ7WxPm+sTNnirSmccrauIU2/SK8STnRi9rW6AOxogZvU9XzugEm4qW3KS1W X-Received: by 2002:a17:906:b055:: with SMTP id bj21mr16536826ejb.355.1613405391194; Mon, 15 Feb 2021 08:09:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613405391; cv=none; d=google.com; s=arc-20160816; b=pjBlzjFZPoTnC/F0vmfcLbAJCtEqH4Wo4VlLXqcJjwu5gCTy5gVKjQColj9RO6qYva acbXb3iPiaYQJF8PBKsVmkqFR3MOQeAJoIm7r/YCvLYGJYgdupjFl1X1r3xlHqKi6fcu mk8YXQBg7ACYDWPMsBneXOnmGMmua+BAhA0RdgQbU3m1R3Hj7dkBaWdWHPreBOrM04Oe xuRcU4okivVW8WiseiIxBP+tsRG5TdiDe2nBoHvCnC5js9m9MLlGbyLje7uB7/54Lilx /xmo86Dti7qkg1AP+Yu/YxN/g1wP3+OqOJU9+MQ8U6pf2SXK+aV5iJgxFdNAe5YocgyA a8aw== 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=SSVcn4Y+flGGAI3IBYySuXPtitry9xXNVGJ6ftOC2ro=; b=eP4KqKv/vNJGeXLZO07VxMKdHuMiHr21mnP3/s53YFBEf7yGcd9rTEhpm48kQ3+clY bSfYQcumX1huP4N6FfEPCT2pVThDo/G/xQctZKt/HfDNir/XWKW51majELl1G29UbYks nmknPiQ9Rq5bl4RGfEM9zxJPCNSYjt5h0bln6e0LyCpCTKZVLFfo9n3eUsWNACwNJgYB NsJ0ajllK24sEqE4Da/OLsLvianCG9j7TRiLg4m3IiU5VwIw6wVDfBHXVA5e+uYhjrrR G76HhmQPZ1j/7eYRKWAvJJSH55n/bdBXE0rtVo1TBmZfkcNvSVsQmekH4tRayowZQPvw ALWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ToiBGUFv; 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 jz7si12894334ejb.476.2021.02.15.08.09.28; Mon, 15 Feb 2021 08:09:51 -0800 (PST) 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=ToiBGUFv; 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 S232370AbhBOQIV (ORCPT + 99 others); Mon, 15 Feb 2021 11:08:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:45570 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231231AbhBOPdH (ORCPT ); Mon, 15 Feb 2021 10:33:07 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C2DFA64DC3; Mon, 15 Feb 2021 15:30:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1613403043; bh=7r2slUzHjXhtR3I88T96YfUhJ/cqHH7y2q3RscEMlgw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ToiBGUFvMytrg7tRWPZSCg4QcIdPwlM//SfzcT7tFEpiE6QkpdeeACmjx6qe5FsKF cHJjApv9hYTt1PCyN9BCNyV6FGaqwsjVBiPv6ozSWuY3B2b2EV0OUrnvtuobv38GTL aNb4kZBE2e9iExkLbzWgjMu97FlkeLOKSPDqyNQs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stefano Garzarella , "David S. Miller" Subject: [PATCH 5.4 57/60] vsock: fix locking in vsock_shutdown() Date: Mon, 15 Feb 2021 16:27:45 +0100 Message-Id: <20210215152717.203922591@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210215152715.401453874@linuxfoundation.org> References: <20210215152715.401453874@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: Stefano Garzarella commit 1c5fae9c9a092574398a17facc31c533791ef232 upstream. In vsock_shutdown() we touched some socket fields without holding the socket lock, such as 'state' and 'sk_flags'. Also, after the introduction of multi-transport, we are accessing 'vsk->transport' in vsock_send_shutdown() without holding the lock and this call can be made while the connection is in progress, so the transport can change in the meantime. To avoid issues, we hold the socket lock when we enter in vsock_shutdown() and release it when we leave. Among the transports that implement the 'shutdown' callback, only hyperv_transport acquired the lock. Since the caller now holds it, we no longer take it. Fixes: d021c344051a ("VSOCK: Introduce VM Sockets") Signed-off-by: Stefano Garzarella Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/vmw_vsock/af_vsock.c | 8 +++++--- net/vmw_vsock/hyperv_transport.c | 4 ---- 2 files changed, 5 insertions(+), 7 deletions(-) --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -808,10 +808,12 @@ static int vsock_shutdown(struct socket */ sk = sock->sk; + + lock_sock(sk); if (sock->state == SS_UNCONNECTED) { err = -ENOTCONN; if (sk->sk_type == SOCK_STREAM) - return err; + goto out; } else { sock->state = SS_DISCONNECTING; err = 0; @@ -820,10 +822,8 @@ static int vsock_shutdown(struct socket /* Receive and send shutdowns are treated alike. */ mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN); if (mode) { - lock_sock(sk); sk->sk_shutdown |= mode; sk->sk_state_change(sk); - release_sock(sk); if (sk->sk_type == SOCK_STREAM) { sock_reset_flag(sk, SOCK_DONE); @@ -831,6 +831,8 @@ static int vsock_shutdown(struct socket } } +out: + release_sock(sk); return err; } --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -464,14 +464,10 @@ static void hvs_shutdown_lock_held(struc static int hvs_shutdown(struct vsock_sock *vsk, int mode) { - struct sock *sk = sk_vsock(vsk); - if (!(mode & SEND_SHUTDOWN)) return 0; - lock_sock(sk); hvs_shutdown_lock_held(vsk->trans, mode); - release_sock(sk); return 0; }