From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA216C432C0 for ; Fri, 22 Nov 2019 06:17:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B1FA92068F for ; Fri, 22 Nov 2019 06:17:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574403466; bh=OFhAvqBTsQOpEXKhS5GL21ntd+n5NcdSkGS1/oIoNes=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=YdJ1syZRkYLfNDHWU8pAogk1J44G52EYy33Zjbup1Wmtocgz1coVTLNUDJfMQ8sJC bUE9MRXrWl9VGc0DvNg7KhQvRT/JVLxpoAsb3qcKwbRn5DjEOGvvjAPd1ieyFkNWFB jEKjPK4AadCSH9ZjMgCFFfgCpsNurq8otD+nis1g= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729742AbfKVGRp (ORCPT ); Fri, 22 Nov 2019 01:17:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:50616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728948AbfKVGNZ (ORCPT ); Fri, 22 Nov 2019 01:13:25 -0500 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 6ED7520708; Fri, 22 Nov 2019 06:13:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574403204; bh=OFhAvqBTsQOpEXKhS5GL21ntd+n5NcdSkGS1/oIoNes=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pTPdi7NiiRQ/QuQ0RtYEZXCwmhNIBLboO5kQ2ZFa3dX6tOxz0Fb41Wxbf/NiKRgmL pVpc4y9RjHMV/lpj0p/6yxBxLItEQ0g5UPgM9Opg6OqUcXFZebaRCf304w/YLek81q JV7aVL/o7kSzca3y3IvazmanHyELMptWx9m6pRBw= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Lepton Wu , Jorgen Hansen , "David S . Miller" , Sasha Levin , netdev@vger.kernel.org Subject: [PATCH AUTOSEL 4.4 20/68] VSOCK: bind to random port for VMADDR_PORT_ANY Date: Fri, 22 Nov 2019 01:12:13 -0500 Message-Id: <20191122061301.4947-19-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191122061301.4947-1-sashal@kernel.org> References: <20191122061301.4947-1-sashal@kernel.org> 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: Lepton Wu [ Upstream commit 8236b08cf50f85bbfaf48910a0b3ee68318b7c4b ] The old code always starts from fixed port for VMADDR_PORT_ANY. Sometimes when VMM crashed, there is still orphaned vsock which is waiting for close timer, then it could cause connection time out for new started VM if they are trying to connect to same port with same guest cid since the new packets could hit that orphaned vsock. We could also fix this by doing more in vhost_vsock_reset_orphans, but any way, it should be better to start from a random local port instead of a fixed one. Signed-off-by: Lepton Wu Reviewed-by: Jorgen Hansen Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/vmw_vsock/af_vsock.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 7f1d166ce6128..412d56614fd5e 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -89,6 +89,7 @@ #include #include #include +#include #include #include #include @@ -483,9 +484,13 @@ static void vsock_pending_work(struct work_struct *work) static int __vsock_bind_stream(struct vsock_sock *vsk, struct sockaddr_vm *addr) { - static u32 port = LAST_RESERVED_PORT + 1; + static u32 port = 0; struct sockaddr_vm new_addr; + if (!port) + port = LAST_RESERVED_PORT + 1 + + prandom_u32_max(U32_MAX - LAST_RESERVED_PORT); + vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port); if (addr->svm_port == VMADDR_PORT_ANY) { -- 2.20.1