Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BFE4FC433EF for ; Wed, 24 Nov 2021 12:42:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245150AbhKXMpu (ORCPT ); Wed, 24 Nov 2021 07:45:50 -0500 Received: from mail.kernel.org ([198.145.29.99]:51012 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344277AbhKXMnE (ORCPT ); Wed, 24 Nov 2021 07:43:04 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id B978D61222; Wed, 24 Nov 2021 12:25:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1637756735; bh=ibt9JvqigcOkavofXQ4optZRvoO6N3kghVPFWsKBl2Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0YkjhTg5LcboTXIqjtVNvmABC9GHQPsH3BlLgqoYW9cRMcCaETKij2JfiuQY8Tqdy lxhD9Sgib399hTb5YBJwLaeQ9sfF/SigdKIKXj/ipXcnGJmB/yRx4CFsF7dDA3q1vF 4l3Ilf3NjTTnSaexTAVsGEnig8sNdEEDd/wylyKI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stefano Garzarella , Eiichi Tsukata , "David S. Miller" , Sasha Levin Subject: [PATCH 4.14 187/251] vsock: prevent unnecessary refcnt inc for nonblocking connect Date: Wed, 24 Nov 2021 12:57:09 +0100 Message-Id: <20211124115716.772116788@linuxfoundation.org> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20211124115710.214900256@linuxfoundation.org> References: <20211124115710.214900256@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: Eiichi Tsukata [ Upstream commit c7cd82b90599fa10915f41e3dd9098a77d0aa7b6 ] Currently vosck_connect() increments sock refcount for nonblocking socket each time it's called, which can lead to memory leak if it's called multiple times because connect timeout function decrements sock refcount only once. Fixes it by making vsock_connect() return -EALREADY immediately when sock state is already SS_CONNECTING. Fixes: d021c344051a ("VSOCK: Introduce VM Sockets") Reviewed-by: Stefano Garzarella Signed-off-by: Eiichi Tsukata Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/vmw_vsock/af_vsock.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 02a171916dd2b..8b211d164beea 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1166,6 +1166,8 @@ static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr, * non-blocking call. */ err = -EALREADY; + if (flags & O_NONBLOCK) + goto out; break; default: if ((sk->sk_state == TCP_LISTEN) || -- 2.33.0