Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp803999ybe; Fri, 13 Sep 2019 06:38:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqzCyW2KFj4TnIUcOOJw7Yht2uML9RAg4j1pwRbRtc5ObhjxuTiZoEmlxQh2zNB3qKZRlRLg X-Received: by 2002:a17:906:3443:: with SMTP id d3mr17192202ejb.221.1568381914755; Fri, 13 Sep 2019 06:38:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568381914; cv=none; d=google.com; s=arc-20160816; b=dKifK6o8JhVude3fqlnXgaj/4/nLk3c43QvuMMWo5NSh4JqPVspu4tZHW/ebba41JJ +lrLLDjVmpP3F0II9Y6qxHtOpTMXfwt64RxzaWAp8uTgxOxIfbIRSWMCavQgzrrU66PJ vGC1DV4Rdie/8J/r36q0bOBanhs/mMtU8+r7YKGe9nYAwb0f+wdSeQAwHrP5knOF/v6U pDvPvmnmJPMEkQJv0yTTgqa/B2ol0DfXbLIhWFzCN1uECl8NiWUvqmMXvvU9eR7ggZRu qCrugb3Xvuumc0YzQLpNhgV+Cd6Cr0qyEWO4rHtSuVHL8ownx6FZ2SiWqkCZEls2f64u DGGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qMHJHLrWzlRdnXFHo6xoch64nLLHU4+02O59sZRoch4=; b=R5rV9V45+YnX8R4d3x7dTZKmR184eBdHoAGV6Ba4QXqCLqNSacif0QleY98JY1GMFP znFwsEq2YJE7MDMu3rJYMkqjBeMMgpNaCRJxIF7RYMvhD2XGsNQS/CYkxWWEfCTjiEXJ ma6FQC+Ovk4YZjSJizFKZMUzlYOeUUc32t8Wk3Yyea3SwsHBDcvUWDrnWx7hPkFWeORQ M0ONWkAw4n4YJQri+mG5Dk4HbPps8X6pi4hDhpytamtlAKLOA9zuexiE8F7A59rvi0GA AluMx2vCUB9riDqzPi/k+08+d3PKn1AQiY0fHdA1uJR1R40ejcMrVBDpLtydNh59/jCR 5EMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pYVZ7PdT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x19si1148886eje.336.2019.09.13.06.38.11; Fri, 13 Sep 2019 06:38:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pYVZ7PdT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391755AbfIMNdN (ORCPT + 99 others); Fri, 13 Sep 2019 09:33:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:36322 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387788AbfIMNLR (ORCPT ); Fri, 13 Sep 2019 09:11:17 -0400 Received: from localhost (unknown [104.132.45.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 97413208C2; Fri, 13 Sep 2019 13:11:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568380277; bh=PAKaJQyWgyfTcBY6l1xp4O9eU92shj7WJb6AV3Ggkrk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pYVZ7PdTvHp+g+kAsK8+OpwvXIMBM5zSGQMQxApDLeI+lpnkW7KJmJ/e0vDvO/WR9 3ZNkZJd/xpvPeJqVZdhkNESABvksFl8o/oxSJK6ib3a4lvajXdpNu6krhP8BbMbsS8 tRffp5CuleArV8hPJc6fpWMcWiQr4YZ1fQs3KeE8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dexuan Cui , Sunil Muthuswamy , "David S. Miller" , Sasha Levin Subject: [PATCH 4.19 013/190] hv_sock: Fix hang when a connection is closed Date: Fri, 13 Sep 2019 14:04:28 +0100 Message-Id: <20190913130600.658336649@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190913130559.669563815@linuxfoundation.org> References: <20190913130559.669563815@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 685703b497bacea8765bb409d6b73455b73c540e ] There is a race condition for an established connection that is being closed by the guest: the refcnt is 4 at the end of hvs_release() (Note: here the 'remove_sock' is false): 1 for the initial value; 1 for the sk being in the bound list; 1 for the sk being in the connected list; 1 for the delayed close_work. After hvs_release() finishes, __vsock_release() -> sock_put(sk) *may* decrease the refcnt to 3. Concurrently, hvs_close_connection() runs in another thread: calls vsock_remove_sock() to decrease the refcnt by 2; call sock_put() to decrease the refcnt to 0, and free the sk; next, the "release_sock(sk)" may hang due to use-after-free. In the above, after hvs_release() finishes, if hvs_close_connection() runs faster than "__vsock_release() -> sock_put(sk)", then there is not any issue, because at the beginning of hvs_close_connection(), the refcnt is still 4. The issue can be resolved if an extra reference is taken when the connection is established. Fixes: a9eeb998c28d ("hv_sock: Add support for delayed close") Signed-off-by: Dexuan Cui Reviewed-by: Sunil Muthuswamy Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/vmw_vsock/hyperv_transport.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c index 9c7da811d130f..98f193fd5315e 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -320,6 +320,11 @@ static void hvs_close_connection(struct vmbus_channel *chan) lock_sock(sk); hvs_do_close_lock_held(vsock_sk(sk), true); release_sock(sk); + + /* Release the refcnt for the channel that's opened in + * hvs_open_connection(). + */ + sock_put(sk); } static void hvs_open_connection(struct vmbus_channel *chan) @@ -388,6 +393,9 @@ static void hvs_open_connection(struct vmbus_channel *chan) } set_per_channel_state(chan, conn_from_host ? new : sk); + + /* This reference will be dropped by hvs_close_connection(). */ + sock_hold(conn_from_host ? new : sk); vmbus_set_chn_rescind_callback(chan, hvs_close_connection); /* Set the pending send size to max packet size to always get -- 2.20.1