Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2115350ybl; Thu, 29 Aug 2019 03:52:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqyMJV1uFcZ4wAhS99i0ld3wtD54FX/8Oez2gfYQVi024Vt7TZeMTlueYSvXfQPXMWHHO7Jq X-Received: by 2002:a63:4c46:: with SMTP id m6mr8018006pgl.59.1567075955969; Thu, 29 Aug 2019 03:52:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567075955; cv=none; d=google.com; s=arc-20160816; b=ZulTltJSCvn4ujLWaCk9RoXVjbUm00xBWQP5dWdUnPn7LQi2hkQzkjSFLy7rACpTm2 SbAVObZXMLiuAuplUZraS3g2XtaVFlG7jKhBWsI/RBtW0us0DhJ/YKv2FxW+ARHXaXei 8RJouGShV5frhG1qCXJTS6iaA7ojEMyrQO4QvPMRIxY4MNq/RjD5MTQC+pfGVShAAVXx gO5e6rwo0dNohfWPwDGfdvg2xokIPZVHCuuz7Gbfd+z/Hnx7obMfDyXaZYo0A9N47RVW jlbI24YfJs4YfisrcdjVESYBvXtRUroQNW2sCc2XhDTEr0KFu2BQOFXrOvzIpmjTbyEz 661w== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=JQTpKLeVgj0/eMJY2ZQqVy+C3ijTZX65NqIULqDpPL8=; b=pg+K/7cbiupb030rYFL9wN9XmYT9z2cs8oN5E9QGfc24XB98dzROYw+NxWD2fHgJ34 tkX7BVgOUHhp1qNEwTUS3CZXZLrI/VjBo78/S7Rb6w5kbaZSvxv0QGIVTDK9umfqSAlN ceYOKyq3Tn2SOMAOj2Rnu/bSTLmirIFhlv8V+Wn7qD6wuRZANrK1+i5qaGTnaUfZHd4x aWz1u92rnKlhNlJ2M8sXo7BooSE97BLt1PiKUw34IoQsVJzBjBdQ8UbauUM/+h/oPfF/ KtrCN1shSgtAaCeK7rCWwGYrQGUKcxTJ9ZUPXFEvLCOk8l/gDWgukYp8ghDgnSxl2Yl/ rXMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=WyGH4v+m; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t10si1830598pjr.62.2019.08.29.03.52.20; Thu, 29 Aug 2019 03:52:35 -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=WyGH4v+m; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728428AbfH2KvC (ORCPT + 99 others); Thu, 29 Aug 2019 06:51:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:58266 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728216AbfH2Kuq (ORCPT ); Thu, 29 Aug 2019 06:50:46 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A927623405; Thu, 29 Aug 2019 10:50:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567075845; bh=ONx14oMxgGVjejwC1H+cAhSrLULeYa5AfyZ3w2hjqZA=; h=From:To:Cc:Subject:Date:From; b=WyGH4v+mf7QdMMIM046pQ2TH5Ybho6KjBiDkCfBlN7IfP/2+L81/zLQV3fv6jmg72 4bg9GcF8wXcnojAH2yd/SckHzXMJkVcNb73fD8BbjV+5bgBTKEqFCVxLGJPAkmrE5S nI1HbyiWNHayBEEgFDDB1FfQzCdb0Xdq14wXgjpY= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Dexuan Cui , Sunil Muthuswamy , "David S . Miller" , Sasha Levin , netdev@vger.kernel.org, bcm-kernel-feedback-list@broadcom.com Subject: [PATCH AUTOSEL 4.14 01/14] hv_sock: Fix hang when a connection is closed Date: Thu, 29 Aug 2019 06:50:30 -0400 Message-Id: <20190829105043.2508-1-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dexuan Cui [ 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 52ac3e49c7efd..ec72a5edaa1b8 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) @@ -389,6 +394,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