From patchwork Mon May 10 15:58:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 43306 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 3DC263857401; Mon, 10 May 2021 15:58:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DC263857401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620662325; bh=CVqQs8+n5I/2rnZvbi+kd9KljOtDd/lXulpxEWEJfv8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ByYiaWXi4N45O8DMJxNb4og/iUIkz7gB/1JLakru6d3vujCdpHIELmg9K8u/1WvZz NXPz2/1MrQwyuPkqVBbDUVzYItBIGZdEHlZdiMYhGmGC7muaMn0/ffTeVVO2+ZH2SI +OKPkWRmoFtDeLCgiOd2kpq8FKrhxtInm5KOE86A= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from dormouse.elm.relay.mailchannels.net (dormouse.elm.relay.mailchannels.net [23.83.212.50]) by sourceware.org (Postfix) with ESMTPS id 0E9623858039 for ; Mon, 10 May 2021 15:58:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0E9623858039 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 4E86C542CAE for ; Mon, 10 May 2021 15:58:40 +0000 (UTC) Received: from pdx1-sub0-mail-a48.g.dreamhost.com (100-98-55-119.trex.outbound.svc.cluster.local [100.98.55.119]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id E235A542D23 for ; Mon, 10 May 2021 15:58:39 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a48.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.98.55.119 (trex/6.2.1); Mon, 10 May 2021 15:58:40 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Inform-Madly: 6e0398cc189406b1_1620662320130_4032096374 X-MC-Loop-Signature: 1620662320129:806292558 X-MC-Ingress-Time: 1620662320129 Received: from pdx1-sub0-mail-a48.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a48.g.dreamhost.com (Postfix) with ESMTP id 87B7E854BE for ; Mon, 10 May 2021 08:58:39 -0700 (PDT) Received: from rhbox.intra.reserved-bit.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-a48.g.dreamhost.com (Postfix) with ESMTPSA id 3D1138515F for ; Mon, 10 May 2021 08:58:37 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a48 To: libc-alpha@sourceware.org Subject: [PATCH] elf/cache.c: Fix resource leaks identified by static analyzers Date: Mon, 10 May 2021 21:28:27 +0530 Message-Id: <20210510155827.131051-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_H2, 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" A coverity run identified a number of resource leaks in cache.c. There are a couple of simple memory leaks where a local allocation is not freed before function return. Then there is a mmap leak and a file descriptor leak where a map is not unmapped in the error case and a file descriptor remains open respectively. Reviewed-by: Adhemerval Zanella --- elf/cache.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/elf/cache.c b/elf/cache.c index c01d302072..8a3404923c 100644 --- a/elf/cache.c +++ b/elf/cache.c @@ -547,6 +547,7 @@ write_extensions (int fd, uint32_t str_offset, || write (fd, generator, strlen (generator)) != strlen (generator)) error (EXIT_FAILURE, errno, _("Writing of cache extension data failed")); + free (hwcaps_array); free (ext); } @@ -778,6 +779,7 @@ save_cache (const char *cache_name) free (file_entries_new); free (file_entries); free (strings_finalized.strings); + free (temp_name); while (entries) { @@ -1034,6 +1036,9 @@ load_aux_cache (const char *aux_cache_name) + aux_cache->nlibs * sizeof (struct aux_cache_file_entry) + aux_cache->len_strings)) { + if (aux_cache != MAP_FAILED) + munmap (aux_cache, aux_cache_size); + close (fd); init_aux_cache (); return; @@ -1143,10 +1148,13 @@ save_aux_cache (const char *aux_cache_name) if (fd < 0) goto out_fail; - if (write (fd, file_entries, file_entries_size + total_strlen) - != (ssize_t) (file_entries_size + total_strlen) - || fdatasync (fd) != 0 - || close (fd) != 0) + bool fail = ((write (fd, file_entries, file_entries_size + total_strlen) + != (ssize_t) (file_entries_size + total_strlen)) + || fdatasync (fd) != 0); + + fail |= close (fd) != 0; + + if (fail) { unlink (temp_name); goto out_fail;