From patchwork Mon Dec 12 18:38:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 61820 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 396563850091 for ; Mon, 12 Dec 2022 18:39:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 396563850091 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670870367; bh=9mLynWr/92EyPDXxAZ5rfdcNF3BJOJ7JmQwN/XRjrUM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=FdML3+3Ptp+fsJdD2PALxmP44jx1TfGSSien8ANSR0YU9qq0tnwr4EM6uJYwl+aNf 0Ch6LqVZBKQV2hWX/rKCJFYXpaiaUD/M0l3ZCw9jc2lD7B+Ta7XmrAFvJrG1EOuptd As/sarNp+Sqa/5YfvJuvRfMaj+qlOD7SqeUkjXxY= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by sourceware.org (Postfix) with ESMTPS id A097D3850B3B for ; Mon, 12 Dec 2022 18:39:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A097D3850B3B Received: by mail-io1-xd2f.google.com with SMTP id n63so425797iod.7 for ; Mon, 12 Dec 2022 10:39:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9mLynWr/92EyPDXxAZ5rfdcNF3BJOJ7JmQwN/XRjrUM=; b=7GNiM3Zv/jiLTKystl8rWWOo+fyWhq6w5XjiyXpxgd+pE3vDtcvQNBduvI25WJiteq cgX1T8jYykPlY+AasbF+RJMBDpYVeJwRxbDdhX92v/efqOd3nnbdTA5R0UlNY9ccsOo1 Z6drjcdxV3sB//T+x8zhwiIXbcCZMjNjAr60SBbYTXQWYVY81/e21JeaWBikRZq1gyil Y8Ihe8fTLToQeLSaFH8ao8sh/t173wiKNLxvvrriQ1zsInLpOtFPp5wA5fChKy4uV4wt tz4O5tmIGSCjuOFYixJN+KpZ8uKCgODMpVOIGsV7+fn/jQfk5GwdH2vVGmKPkC8p8uZj hDXg== X-Gm-Message-State: ANoB5pkUJuWUz3ehx877AaOTK0nlv6NRhjDH59H80k3T8o04AfSJm5eg mos9mDpJPic4RzW6BLvajxm+s/qwcz9svmA1 X-Google-Smtp-Source: AA0mqf4Xk0nsY4EvzKkHlfZw14vvYtqjoHD3Y9/2zI1J2XyODbsBSAfLxaOQ+mN8t2i3zYHaEiFVaQ== X-Received: by 2002:a6b:c402:0:b0:6e0:27a2:1768 with SMTP id y2-20020a6bc402000000b006e027a21768mr9657080ioa.18.1670870341964; Mon, 12 Dec 2022 10:39:01 -0800 (PST) Received: from localhost.localdomain (97-122-76-186.hlrn.qwest.net. [97.122.76.186]) by smtp.gmail.com with ESMTPSA id s4-20020a5d9284000000b006df19c8671fsm4275868iom.27.2022.12.12.10.39.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 10:39:01 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Avoid double-free with debuginfod Date: Mon, 12 Dec 2022 11:38:52 -0700 Message-Id: <20221212183852.1502020-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.3 MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" PR gdb/29257 points out a possible double free when debuginfod is in use. Aside from some ugly warts in the symbol code (an ongoing issue), the underlying issue in this particular case is that elfread.c seems to assume that symfile_bfd_open will return NULL on error, whereas in reality it throws an exception. As this code isn't prepared for an exception, bad things result. This patch fixes the problem by introducing a non-throwing variant of symfile_bfd_open and using it in the affected places. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29257 --- gdb/elfread.c | 19 ++++++++++--------- gdb/symfile.c | 17 +++++++++++++++++ gdb/symfile.h | 5 +++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/gdb/elfread.c b/gdb/elfread.c index 64aeb239670..57ff19182af 100644 --- a/gdb/elfread.c +++ b/gdb/elfread.c @@ -1220,10 +1220,12 @@ elf_symfile_read_dwarf2 (struct objfile *objfile, if (!debugfile.empty ()) { - gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (debugfile.c_str ())); + gdb_bfd_ref_ptr debug_bfd + (symfile_bfd_open_no_error (debugfile.c_str ())); - symbol_file_add_separate (debug_bfd, debugfile.c_str (), - symfile_flags, objfile); + if (debug_bfd != nullptr) + symbol_file_add_separate (debug_bfd, debugfile.c_str (), + symfile_flags, objfile); } else { @@ -1243,13 +1245,12 @@ elf_symfile_read_dwarf2 (struct objfile *objfile, if (fd.get () >= 0) { /* File successfully retrieved from server. */ - gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (symfile_path.get ())); + gdb_bfd_ref_ptr debug_bfd + (symfile_bfd_open_no_error (symfile_path.get ())); - if (debug_bfd == nullptr) - warning (_("File \"%s\" from debuginfod cannot be opened as bfd"), - filename); - else if (build_id_verify (debug_bfd.get (), build_id->size, - build_id->data)) + if (debug_bfd != nullptr + && build_id_verify (debug_bfd.get (), build_id->size, + build_id->data)) { symbol_file_add_separate (debug_bfd, symfile_path.get (), symfile_flags, objfile); diff --git a/gdb/symfile.c b/gdb/symfile.c index eb27668f9d3..b9994e35ae1 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -1744,6 +1744,23 @@ symfile_bfd_open (const char *name) return sym_bfd; } +/* See symfile.h. */ + +gdb_bfd_ref_ptr +symfile_bfd_open_no_error (const char *name) +{ + try + { + return symfile_bfd_open (name); + } + catch (const gdb_exception_error &err) + { + warning ("%s", err.what ()); + } + + return nullptr; +} + /* Return the section index for SECTION_NAME on OBJFILE. Return -1 if the section was not found. */ diff --git a/gdb/symfile.h b/gdb/symfile.h index ffd1acddfdb..61d69c4e412 100644 --- a/gdb/symfile.h +++ b/gdb/symfile.h @@ -269,6 +269,11 @@ extern void set_initial_language (void); extern gdb_bfd_ref_ptr symfile_bfd_open (const char *); +/* Like symfile_bfd_open, but will not throw an exception on error. + Instead, it issues a warning and returns nullptr. */ + +extern gdb_bfd_ref_ptr symfile_bfd_open_no_error (const char *); + extern int get_section_index (struct objfile *, const char *); extern int print_symbol_loading_p (int from_tty, int mainline, int full);