From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id B7B473858D20 for ; Thu, 29 Dec 2022 11:18:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B7B473858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672312731; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=n9nB34xMWwAbVtFd6pEX3E9IIKK2UN4TdK0/K3Lee44=; b=M/cPIcmSBig1E/jy5XblmOohbiFfV3kbafTVODC3pGPeT2l2HjJsJcaNvSUiP27JRJhPCP 1mL/wHFq9uwlnAqHf6SXgyvxLQjkBqSf5k0KihbuuS6rK0eASjTujSrTNI76RwkO5vlsZw kYqzh3kYzCD/YT1uF8d5OayMV/vHQYw= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-574-3XrnGsDRPB-z4i9D59Ddgg-1; Thu, 29 Dec 2022 06:18:49 -0500 X-MC-Unique: 3XrnGsDRPB-z4i9D59Ddgg-1 Received: by mail-qk1-f197.google.com with SMTP id ay34-20020a05620a17a200b00704bd9922c4so12478364qkb.12 for ; Thu, 29 Dec 2022 03:18:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:message-id:date:organization:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=n9nB34xMWwAbVtFd6pEX3E9IIKK2UN4TdK0/K3Lee44=; b=rpwrglT6wxo1hzVTeVdmEnP3gb0BvTJAuALBHp9HQS20dehMcWntFI5h4pIucVrtYx tfCyRCot8NUxDinjvCRlDPgVJNhSW6I/exbadijeiHievR3ThRrIYX4kAxRnGD7Rr/q7 9psW1ZgtNUM/uexy/BC1OXFqnAJIjbkjeTGPwzUsSfuy54MOTiBI3faBdMDrkIJmK9EM hFKlfIBgRzoUyc8b9cM4yAcThgv00vlk2dL/9co+CoRvSOgZMNs8R2j+H4R4+6Sjch1b C7PKV+X2U3wemC70uRNccD4JkynIaGROev6z0l2I7pUZ2sVkaQKRXKZg2Fj+hS4zJLfR 1Stw== X-Gm-Message-State: AFqh2kpi8A2XAnCa02v0bzMlnan1ilmElfs/p0KgPP4JQEqQZVcnl7ut 3I+EXKc2wVFCqte2l3C0EN8hiDA8TDG1r+BkO3ldCOF9l17LjSw7Askgfjy41WUVMgasngE1Qv+ DUr3MRjuX2n+Mcey2LDh2pFayxsqZvrFG0AJFnmzV8BWrafsfVCfYBYEtCbUlw2SQfkJm X-Received: by 2002:ac8:5191:0:b0:3a6:93cf:7dec with SMTP id c17-20020ac85191000000b003a693cf7decmr37019406qtn.48.1672312728986; Thu, 29 Dec 2022 03:18:48 -0800 (PST) X-Google-Smtp-Source: AMrXdXvO8LqhuEgeW+gwuDszrAPxR+XZ4U6ZwDWp5SNVGqu3IWoere58rQuA1oA/Ans4/rG5dEwT3Q== X-Received: by 2002:ac8:5191:0:b0:3a6:93cf:7dec with SMTP id c17-20020ac85191000000b003a693cf7decmr37019380qtn.48.1672312728512; Thu, 29 Dec 2022 03:18:48 -0800 (PST) Received: from localhost ([88.120.130.27]) by smtp.gmail.com with ESMTPSA id m5-20020ac807c5000000b003a530a32f67sm11450390qth.65.2022.12.29.03.18.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Dec 2022 03:18:48 -0800 (PST) Received: by localhost (Postfix, from userid 1000) id 09460A2E7E; Thu, 29 Dec 2022 12:18:44 +0100 (CET) From: Dodji Seketeli To: libabigail@sourceware.org Subject: [PATCH, applied] elf-reader: Don't free CTF resources too early Organization: Red Hat / France X-Operating-System: CentOS Stream release 9 X-URL: http://www.redhat.com Date: Thu, 29 Dec 2022 12:18:43 +0100 Message-ID: <87h6xebgq4.fsf@redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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: Hello, elf::reader::locate_alt_ctf_debug_info frees the memory for the alternate CTF debug info too early, leading to some segmentation violation down the road, when the rest of the code tries to access the CTF section afterwards. Many thanks to the Valgrind tool and its hackers for showing me this. This patch thus keeps the file descriptor and ELF data structure of the alternate CTF debug info around for the lifetime of the reader. * src/abg-elf-reader.cc (reader::priv::{alt_ctf_fd, alt_ctf_handle}): Add new data members. (reader::priv::clear_alt_ctf_debug_info_data): Define new member function. (reader::priv::~priv): Call the new priv::clear_alt_ctf_debug_info_data (reader::priv::initialize): Likewise. Initialize the new alt_ctf_handle and alt_ctf_fd data members. (reader::priv::locate_alt_ctf_debug_info): Do not free the fd and ELF resources early here. Store them in the new reader::priv::alt_ctf_{fd,handle} instead. Signed-off-by: Dodji Seketeli --- src/abg-elf-reader.cc | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/abg-elf-reader.cc b/src/abg-elf-reader.cc index 656418e3..16e8b022 100644 --- a/src/abg-elf-reader.cc +++ b/src/abg-elf-reader.cc @@ -271,6 +271,8 @@ struct reader::priv string alt_dwarf_path; int alt_dwarf_fd = 0; Elf_Scn* ctf_section = nullptr; + int alt_ctf_fd = 0; + Elf* alt_ctf_handle = nullptr; Elf_Scn* alt_ctf_section = nullptr; priv(reader& reeder, const std::string& elf_path, @@ -284,6 +286,7 @@ struct reader::priv ~priv() { clear_alt_dwarf_debug_info_data(); + clear_alt_ctf_debug_info_data(); } /// Reset the private data of @elf elf::reader. @@ -294,6 +297,7 @@ struct reader::priv initialize(const vector& debug_info_roots) { clear_alt_dwarf_debug_info_data(); + clear_alt_ctf_debug_info_data(); elf_handle = nullptr; symtab_section = nullptr; @@ -310,6 +314,8 @@ struct reader::priv alt_dwarf_fd = 0; ctf_section = nullptr; alt_ctf_section = nullptr; + alt_ctf_handle = nullptr; + alt_ctf_fd = 0; } /// Setup the necessary plumbing to open the ELF file and find all @@ -413,6 +419,22 @@ struct reader::priv alt_dwarf_fd); } + /// Clear the resources related to the alternate CTF data. + void + clear_alt_ctf_debug_info_data() + { + if (alt_ctf_fd) + { + close(alt_ctf_fd); + alt_ctf_fd = 0; + } + if (alt_ctf_handle) + { + elf_end(alt_ctf_handle); + alt_ctf_handle = nullptr; + } + } + /// Locate the CTF "alternate" debug information associated with the /// current ELF file ( and split out somewhere else). /// @@ -442,23 +464,17 @@ struct reader::priv if (!tools_utils::find_file_under_dir(*path, name, file_path)) continue; - int fd; - if ((fd = open(file_path.c_str(), O_RDONLY)) == -1) + if ((alt_ctf_fd = open(file_path.c_str(), O_RDONLY)) == -1) continue; - Elf *hdl; - if ((hdl = elf_begin(fd, ELF_C_READ, nullptr)) == nullptr) - { - close(fd); - continue; - } + if ((alt_ctf_handle = elf_begin(alt_ctf_fd, + ELF_C_READ, + nullptr)) == nullptr) + continue; // unlikely .ctf was designed to be present in stripped file alt_ctf_section = - elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); - - elf_end(hdl); - close(fd); + elf_helpers::find_section(alt_ctf_handle, ".ctf", SHT_PROGBITS); if (alt_ctf_section) break; -- 2.31.1 -- Dodji