From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id B43403858D28 for ; Thu, 28 Mar 2024 21:12:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B43403858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B43403858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=45.83.234.184 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711660340; cv=none; b=wnRzu48GHiQoiZFjGmNRBwbT5pjxyjwYAK/B61aMePC7zwQjZnRry705didO9eeuHDtGnjNqg/ezMDRB+QRaz9USzi7s1AzQtp0y/HYklvbN7v1++MJRLgNumt2Cahfw3A3zbIXqe5uHhl53rqpLiRTTIKkHEs0P1T2/dIXQhmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711660340; c=relaxed/simple; bh=i2psknM3EXWmO5CZlYKFynNGrzNyQWquk5SitgEHRKY=; h=Date:From:To:Subject:Message-ID:MIME-Version; b=vNuOrnVsSaq698ZrmwS1/YarmpqCwiQOiuH7kKA3dr0p4FjEwKpXHxuz7MU0RKWBWY2OkM2bKDDXRdQqKmEnu/BMHm998Y1KJPNbH0+BLO267rG6y64g7J4rQsRAeFTezbzvCHdO/ast9TNCnirN1w/DNxeRccAmkhmuOI7iWCI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by gnu.wildebeest.org (Postfix, from userid 1000) id D59BA300046F; Thu, 28 Mar 2024 22:12:17 +0100 (CET) Date: Thu, 28 Mar 2024 22:12:17 +0100 From: Mark Wielaard To: Maks Mishin Cc: elfutils-devel@sourceware.org Subject: Re: [PATCH] nm: Fix descriptor leak Message-ID: <20240328211217.GQ9427@gnu.wildebeest.org> References: <20240328204958.9657-1-maks.mishinFZ@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240328204958.9657-1-maks.mishinFZ@gmail.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hi, On Thu, Mar 28, 2024 at 11:49:58PM +0300, Maks Mishin wrote: > The descriptor 'dwfl_fd' is created at nm.c:1278 by calling > function 'dup' and lost at nm.c:1593. Sorry, I don't follow, the code at nm.c:1278 says: /* Duplicate an fd for dwfl_report_offline to swallow. */ int dwfl_fd = dup (fd); if (likely (dwfl_fd >= 0)) And then the code tracks whether the dwfl_report_offline call is executed successfully and otherwise closed dwfl_fd. Specifically the code does: if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL) { /* Consumed on success, not on failure. */ close (dwfl_fd); } The dwfl_report functions are documented as "On success, FD is consumed by the library". Which means fd is freed/closed when dwfl_end is called. > Found by RASU JSC. > > Signed-off-by: Maks Mishin > --- > src/nm.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/src/nm.c b/src/nm.c > index 3675f59b..fee397dd 100644 > --- a/src/nm.c > +++ b/src/nm.c > @@ -1521,6 +1521,8 @@ show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr, > } > if (dwfl != NULL) > dwfl_end (dwfl); > + if (dwfl_fd != NULL) > + close(dwfl_fd); > } So this would double close dwfl_fd. Also dwfl_fd is an int (file descriptor) and so shouldn't be compared to NULL, but checked as above (dwfl_fd >= 0).