From patchwork Wed May 12 03:50:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 43380 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 661603857819; Wed, 12 May 2021 03:53:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 661603857819 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620791589; bh=6cZk6I0gOfvx0WT41dvnKfa65XAJlNUGVlLf4v1VYJU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=nkRT/evJlu5Y1NLO1xdFRusdc4FqxTlDjyfbk9mw64Qrp16MObK49RBEb+A6izAMN L8Fqk4E0bV7ie2PDZFQsRTaHPMmi9dXs573TNfd1HbtZYde8qFxVl3ubFTDjPSGbZD ez5e5yJIT2VEGazb9PezUGWOC7/lMMixyghnP0Z8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from bee.birch.relay.mailchannels.net (bee.birch.relay.mailchannels.net [23.83.209.14]) by sourceware.org (Postfix) with ESMTPS id 956423858034 for ; Wed, 12 May 2021 03:53:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 956423858034 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id F3E70642BB8 for ; Wed, 12 May 2021 03:53:05 +0000 (UTC) Received: from pdx1-sub0-mail-a35.g.dreamhost.com (100-96-27-184.trex.outbound.svc.cluster.local [100.96.27.184]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 964B0642B2E for ; Wed, 12 May 2021 03:53:05 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a35.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.27.184 (trex/6.2.1); Wed, 12 May 2021 03:53:05 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Army-Shrill: 5a0e4ee06ff8e175_1620791585837_3214126059 X-MC-Loop-Signature: 1620791585837:3465393704 X-MC-Ingress-Time: 1620791585837 Received: from pdx1-sub0-mail-a35.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a35.g.dreamhost.com (Postfix) with ESMTP id 402F586719 for ; Tue, 11 May 2021 20:53:05 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a35.g.dreamhost.com (Postfix) with ESMTPSA id E6A4C86127 for ; Tue, 11 May 2021 20:53:03 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a35 To: libc-alpha@sourceware.org Subject: [PATCH] linux/check_native: Always close socket on return Date: Wed, 12 May 2021 09:20:30 +0530 Message-Id: <20210512035030.404349-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3494.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The error paths of __check_native would leave the socket FD open on return, resulting in an FD leak. Rework function exit paths so that the fd is always closed on return. --- sysdeps/unix/sysv/linux/check_native.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/sysdeps/unix/sysv/linux/check_native.c b/sysdeps/unix/sysv/linux/check_native.c index e4e6e80dbc..47b339b629 100644 --- a/sysdeps/unix/sysv/linux/check_native.c +++ b/sysdeps/unix/sysv/linux/check_native.c @@ -49,11 +49,13 @@ __check_native (uint32_t a1_index, int *a1_native, socklen_t addr_len = sizeof (nladdr); - if (fd < 0 - || __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0 - || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0) + if (fd < 0) return; + if (__bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0 + || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0) + goto out; + pid_t pid = nladdr.nl_pid; struct req { @@ -96,7 +98,7 @@ __check_native (uint32_t a1_index, int *a1_native, if (buf != NULL) use_malloc = true; else - goto out_fail; + goto out; } struct iovec iov = { buf, buf_size }; @@ -104,7 +106,7 @@ __check_native (uint32_t a1_index, int *a1_native, if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0, (struct sockaddr *) &nladdr, sizeof (nladdr))) < 0) - goto out_fail; + goto out; bool done = false; do @@ -123,10 +125,10 @@ __check_native (uint32_t a1_index, int *a1_native, ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0)); __netlink_assert_response (fd, read_len); if (read_len < 0) - goto out_fail; + goto out; if (msg.msg_flags & MSG_TRUNC) - goto out_fail; + goto out; struct nlmsghdr *nlmh; for (nlmh = (struct nlmsghdr *) buf; @@ -166,12 +168,9 @@ __check_native (uint32_t a1_index, int *a1_native, } while (! done); - out: +out: __close_nocancel_nostatus (fd); - return; - -out_fail: if (use_malloc) free (buf); }