Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp3863326ioa; Tue, 26 Apr 2022 11:13:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJF4Qu6D5j3nkGU+O7aqk+hvATX3XnZ7oHvtdknxaiAZbgUMrdGZ1rqiab6YZuKfEKkzWN X-Received: by 2002:a17:902:d58a:b0:15d:1cf6:644c with SMTP id k10-20020a170902d58a00b0015d1cf6644cmr10172354plh.67.1650996824834; Tue, 26 Apr 2022 11:13:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650996824; cv=none; d=google.com; s=arc-20160816; b=etvQKGb+4RgzrR2M8+vCOJpzXJ5YFO+btMPFm2pE8xch0wHU45d5X022EuuydfVgeI jfQeSImAab62WojoWksmk8EunTbORi4a7ZtZRWhAuivoLKJuUEniacNfyr2y3ia0YwrQ 0jXRs9cxtkmSlKvLXwf90ZMhxX19LNsfaZXBFffU+8s9qjzEWjl9/ccMOV89zI0Egjzd 9WLSmVDpEKS0604NkGb2jV9MUg/3CyEFDJRFYw0aD7LgK8r3pRlB1KVri0GPdFKHOemv kt8O1pFiNoVrcnHaqhwsI2NmgWQrfO9rBPIRfDZw6sGHTkT308Tzg+EdH+5EwQvfjSx/ /NMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=BiApgeHPa/gWSOnfip1X6MtA82gQAt9uu5NXEZZ1KAU=; b=WNaPatYznFown0ZEXWAnI0nRaH9NyRHXJ1w3sKdnuP6TqYPQbQfJvM4H/Ew9TUvYkE 9vjDNCv6MeKmgllgHTH/9W4zASlJ4OFH81mGJczGJ5XWrEOYfQ8lH4ZwtXEURqdxcn1+ 9KypjPO+S/miZeZSDM1DVciObYbM8ZbxVKFTEDlU8UssJojxNQEPHG0iInnc3+2XDFVi ePXsKpC9VGKhKTpS4J9UoX/UD6DN6a1Y8t9XArCJqYo7hXvCxuFnpAMF8qJn2zRZ+tvh J+mv6mh3SXT4x0BbaqxZVZaVp39AIO10pTrp0GzA0C8we1g7DK9ToSP5tr3o0zo9P9v9 Z6MA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cj18-20020a056a00299200b005061eb330a1si19048624pfb.351.2022.04.26.11.13.15; Tue, 26 Apr 2022 11:13:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239345AbiDZNEg (ORCPT + 99 others); Tue, 26 Apr 2022 09:04:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345226AbiDZNEf (ORCPT ); Tue, 26 Apr 2022 09:04:35 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 104D11DA7D; Tue, 26 Apr 2022 06:01:25 -0700 (PDT) Received: from kwepemi500007.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4KnhlW5VlwzGp36; Tue, 26 Apr 2022 20:58:47 +0800 (CST) Received: from kwepemm600001.china.huawei.com (7.193.23.3) by kwepemi500007.china.huawei.com (7.221.188.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 26 Apr 2022 21:01:23 +0800 Received: from huawei.com (10.175.113.133) by kwepemm600001.china.huawei.com (7.193.23.3) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 26 Apr 2022 21:01:22 +0800 From: Wang Hai To: , , , , , CC: , , , Subject: [PATCH net] SUNRPC: Fix local socket leak in xs_local_setup_socket() Date: Tue, 26 Apr 2022 21:20:11 +0800 Message-ID: <20220426132011.25418-1-wanghai38@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.113.133] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600001.china.huawei.com (7.193.23.3) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org If the connection to a local endpoint in xs_local_setup_socket() fails, fput() is missing in the error path, which will result in a socket leak. It can be reproduced in simple script below. while true do systemctl stop rpcbind.service systemctl stop rpc-statd.service systemctl stop nfs-server.service systemctl restart rpcbind.service systemctl restart rpc-statd.service systemctl restart nfs-server.service done When executing the script, you can observe that the "cat /proc/net/unix | wc -l" count keeps growing. Add the missing fput(), and restore transport to old socket. Signed-off-by: Wang Hai --- net/sunrpc/xprtsock.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/net/sunrpc/xprtsock.c b/net/sunrpc/xprtsock.c index 0f39e08ee580..7219c545385e 100644 --- a/net/sunrpc/xprtsock.c +++ b/net/sunrpc/xprtsock.c @@ -1819,6 +1819,9 @@ static int xs_local_finish_connecting(struct rpc_xprt *xprt, { struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); + struct socket *trans_sock = NULL; + struct sock *trans_inet = NULL; + int ret; if (!transport->inet) { struct sock *sk = sock->sk; @@ -1835,6 +1838,9 @@ static int xs_local_finish_connecting(struct rpc_xprt *xprt, xprt_clear_connected(xprt); + trans_sock = transport->sock; + trans_inet = transport->inet; + /* Reset to new socket */ transport->sock = sock; transport->inet = sk; @@ -1844,7 +1850,14 @@ static int xs_local_finish_connecting(struct rpc_xprt *xprt, xs_stream_start_connect(transport); - return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); + ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); + /* Restore to old socket */ + if (ret && trans_inet) { + transport->sock = trans_sock; + transport->inet = trans_inet; + } + + return ret; } /** @@ -1887,7 +1900,7 @@ static int xs_local_setup_socket(struct sock_xprt *transport) xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; xprt_set_connected(xprt); - break; + goto out; case -ENOBUFS: break; case -ENOENT: @@ -1904,6 +1917,9 @@ static int xs_local_setup_socket(struct sock_xprt *transport) xprt->address_strings[RPC_DISPLAY_ADDR]); } + transport->file = NULL; + fput(filp); + out: xprt_clear_connecting(xprt); xprt_wake_pending_tasks(xprt, status); -- 2.17.1