Message ID | 20230531160406.3932028-2-lancelot.six@amd.com |
---|---|
State | New |
Headers |
Return-Path: <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> 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 32FC93857707 for <patchwork@sourceware.org>; Wed, 31 May 2023 16:06:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 32FC93857707 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685549206; bh=7rTxLzZoEAMNLD0Z7mzvtdtyk1kvSRBYhF8yIqBxz7g=; h=To:CC:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=NiviaN5f8+i/34BvDFpZ9vjrPis7OkooDQilR6lQA6kBpqtX35K3uY5NrO7Hcfwds j1jccDGLRLSCBND2ADAM5BC2z+FRuvbKpsADNLlBmCiKFQUPR3E3wFUu6UYnUojTTL QqxKtPbQ3/F1JpHvPBJmySLv+aY0uheGhssVVsx8= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2084.outbound.protection.outlook.com [40.107.93.84]) by sourceware.org (Postfix) with ESMTPS id 3BDF13858CD1 for <gdb-patches@sourceware.org>; Wed, 31 May 2023 16:05:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3BDF13858CD1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XTYd2QVjvA2TjEd+/hcV4WdhBtcFtdCiXdxAsKvBUXK2CdOFLvFGTZvuPG0vEXHpC32khjo7UwfyYaR+C22npumX0Yp/DfF7bQ5IDjdnmPT0sMqPPXrFk6crX7pmY0/PkvMooHHLOP3BnLy5QMAbO57kIH5OeFgXY1nMHLc6ygfqeC7qyk+qh1D8Eqqf3ZA4uR6uKoOgUXE4zgAR4KyJRqZBATw7C3QsJBqpItzmOtC0TKIdat8wnhwndYNVIYC2/tkkzFGJTD3znlz+4wcCtqcbVHSmKV0uQDXS7/JzbrKY2/hwwvdPTphZl8DyNh7pFhRGHxJjxFDFjeaoRnamWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7rTxLzZoEAMNLD0Z7mzvtdtyk1kvSRBYhF8yIqBxz7g=; b=DjBWFjc5KpJgzwUz0Yt9rxhttaYlusOzxK+HZmEq3FA/sSqxtSpnysGc9wZ+XwaNCoemZ+9U2fcz35JYuVhxBd0V71dDlGyrJTlXoGs/ot1y6Nb5k8FmYHwnccwxfLDcZmtplVhkrL9tJxIOcQYW/iNK+a/IT5+kBEhppD9Qu0Q7WZP2T3J4sJPe2NWgOILk9t2Jd/OR2CVgmwitaYv0J8e1ykGXbbUJMpmpvEpQ/IY1hlt9WR/qSxaA8wiFVZccMdlECERy4EU6KKA5DijBMOk54R/wypsAcyl82b3OnbwB7VfnKBsmnwNggBzj0ySifsCEPz3j8GJs2O2lzEPO8g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=sourceware.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none Received: from MN2PR05CA0052.namprd05.prod.outlook.com (2603:10b6:208:236::21) by MW6PR12MB8898.namprd12.prod.outlook.com (2603:10b6:303:246::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.23; Wed, 31 May 2023 16:05:37 +0000 Received: from BL02EPF000145B8.namprd05.prod.outlook.com (2603:10b6:208:236:cafe::7d) by MN2PR05CA0052.outlook.office365.com (2603:10b6:208:236::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.22 via Frontend Transport; Wed, 31 May 2023 16:05:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BL02EPF000145B8.mail.protection.outlook.com (10.167.241.208) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6455.18 via Frontend Transport; Wed, 31 May 2023 16:05:36 +0000 Received: from khazad-dum.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 31 May 2023 11:05:34 -0500 To: <gdb-patches@sourceware.org> CC: <lsix@lancelotsix.com>, Lancelot SIX <lancelot.six@amd.com> Subject: [PATCH 1/3] gdb/corelow.c: fix use-after-free in build_file_mappings Date: Wed, 31 May 2023 17:04:04 +0100 Message-ID: <20230531160406.3932028-2-lancelot.six@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230531160406.3932028-1-lancelot.six@amd.com> References: <20230531160406.3932028-1-lancelot.six@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000145B8:EE_|MW6PR12MB8898:EE_ X-MS-Office365-Filtering-Correlation-Id: 94d32c63-d0d4-4339-3229-08db61f0df3e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QKL333gjTuNToBcHHSpN5uX6EkaTI63s45/NTn+KkAbH71FtT/V2fLy6ezvscg7NSkc4e5Q6Dn9kWjhLQ9fgEdfjzBDGp+c1A4V2m2TyTZN5fgihg1KjWAQ43wQpzQzSdCj40+OmWU3IIhBhw4aZ8wNFRyptEsIcf8txykAMIKEBS+cTFTT19IHFCl7uvsUkKAhyxy/wNrAulUfuy7wbZbafI1e+nMNpU7AwHQXZ8xcbcgSpj9RWWFJm/4FtEppKUxN9Jm9mG99nsC4Kt2FH/+M0FXdTljuyf3sDFJSyHaBGp5A+87hKlXxbV+oi9W5V5CsyDA375vg78IHyHoZmTg5+VoS+a3YGk1Hc3HvZq50JaHEHiKJzxXEy3QWqi9vTpXZAH3LP8KVt7sH+ljcGoGWbe+jBz+5GKIDXXcB4+MVAL9QsmLAzF5emfm3uaB8HjjOio4cREPP1URsbzttSF5mNy+WmQ/YPSX8R6NfE125aduv+8dLYT+A9o8JRVMd1vVr91sl801sxyeD4Hbgi1jczQ4qdIzidLyu6I2ybktj8kbbcvFIBHpcR0TQQ39fTWDxvM7J7L97jc35LfLHnyxS8l3WftwxZPAKuHz/0RJQBK1GiMPMvRVfkUxuSk2wLRM5YZkohiwELDL8kRCM6JTZzIWcvkAUPOn2hmTjAlF13vY00dt31fYF2bNOQF5bodweIHCTh/CMeiGWgW/Ejr+rmzREc+oOU1+KlEhlVlWGZ04NaSJ4EZVf2fHNE1OHG3c36B+LU7CI9dBAMggACCg== X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB04.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(396003)(136003)(346002)(376002)(451199021)(36840700001)(46966006)(40470700004)(82310400005)(478600001)(86362001)(41300700001)(7696005)(40480700001)(40460700003)(6916009)(4326008)(6666004)(316002)(36756003)(70586007)(70206006)(36860700001)(5660300002)(186003)(16526019)(2906002)(26005)(1076003)(426003)(336012)(47076005)(83380400001)(2616005)(8936002)(8676002)(54906003)(82740400003)(81166007)(356005)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2023 16:05:36.8028 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 94d32c63-d0d4-4339-3229-08db61f0df3e X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF000145B8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB8898 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FORGED_SPF_HELO, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 <gdb-patches.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=subscribe> From: Lancelot SIX via Gdb-patches <gdb-patches@sourceware.org> Reply-To: Lancelot SIX <lancelot.six@amd.com> Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> |
Series |
Fix use-after-free in gdb/corelow.c + cleanups
|
|
Commit Message
Lancelot SIX
May 31, 2023, 4:04 p.m. UTC
In core_target::build_file_mappings, GDB tries to open files referenced in the core dump. The process goes like this: struct bfd *bfd = bfd_map[filename]; if (bfd == nullptr) { bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (), "binary"); if (bfd == nullptr || !bfd_check_format (bfd, bfd_object)) { if (bfd != nullptr) bfd_close (bfd); return; } } asection *sec = bfd_make_section_anyway (bfd, "load"); ... The problem is that if bfd_check_format fails, we close the bfd but keep a reference to it in the bfd_map. If the same filename appears another time in the NT_FILE note, we enter this code again. The second time, bfd_map[filename] is not nullptr and we try to call bfd_make_section_anyway on an already closed BFD, which is a use-after-free error. This patch makes sure that after closing the bfd, it is removed from the bfd_map. This error got exposed by a recent change in BFD (014a602b86f "Don't optimise bfd_seek to same position"). Since this change, opening a coredump which contains mapping to some special files such as a DRI render node (/dev/dri/renderD$NUM) exposes the issue. This happens for example for processes using AMDGPU devices to offload compute tasks. --- gdb/corelow.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Comments
On 5/31/23 9:04 AM, Lancelot SIX via Gdb-patches wrote: > In core_target::build_file_mappings, GDB tries to open files referenced > in the core dump. > > The process goes like this: > > struct bfd *bfd = bfd_map[filename]; > if (bfd == nullptr) > { > bfd = bfd_map[filename] > = bfd_openr (expanded_fname.get (), "binary"); > if (bfd == nullptr || !bfd_check_format (bfd, bfd_object)) > { > if (bfd != nullptr) > bfd_close (bfd); > return; > } > } > asection *sec = bfd_make_section_anyway (bfd, "load"); > ... > > The problem is that if bfd_check_format fails, we close the bfd but keep > a reference to it in the bfd_map. > > If the same filename appears another time in the NT_FILE note, we enter > this code again. The second time, bfd_map[filename] is not nullptr and > we try to call bfd_make_section_anyway on an already closed BFD, which > is a use-after-free error. > > This patch makes sure that after closing the bfd, it is removed from the > bfd_map. > > This error got exposed by a recent change in BFD (014a602b86f "Don't > optimise bfd_seek to same position"). Since this change, opening a > coredump which contains mapping to some special files such as a DRI > render node (/dev/dri/renderD$NUM) exposes the issue. This happens for > example for processes using AMDGPU devices to offload compute tasks. > --- > gdb/corelow.c | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/gdb/corelow.c b/gdb/corelow.c > index db489b4280e..77fc4453f94 100644 > --- a/gdb/corelow.c > +++ b/gdb/corelow.c > @@ -277,7 +277,10 @@ core_target::build_file_mappings () > "during file-backed mapping note processing"), > filename, expanded_fname.get ()); > if (bfd != nullptr) > - bfd_close (bfd); > + { > + bfd_close (bfd); > + bfd_map.erase (filename); > + } > return; > } > /* Ensure that the bfd will be closed when core_bfd is closed. Maybe only insert the bfd into the map on success instead of having to do the erase, that is something like: if (bfd == nullptr) { bfd = bfd_openr (...); if (bfd == nullptr) return; if (!bfd_check_format (...)) { bfd_close (bfd); return; } bfd_map[filename] = bfd; }
John Baldwin <jhb@FreeBSD.org> writes: > On 5/31/23 9:04 AM, Lancelot SIX via Gdb-patches wrote: >> In core_target::build_file_mappings, GDB tries to open files referenced >> in the core dump. >> >> The process goes like this: >> >> struct bfd *bfd = bfd_map[filename]; >> if (bfd == nullptr) >> { >> bfd = bfd_map[filename] >> = bfd_openr (expanded_fname.get (), "binary"); >> if (bfd == nullptr || !bfd_check_format (bfd, bfd_object)) >> { >> if (bfd != nullptr) >> bfd_close (bfd); >> return; >> } >> } >> asection *sec = bfd_make_section_anyway (bfd, "load"); >> ... >> >> The problem is that if bfd_check_format fails, we close the bfd but keep >> a reference to it in the bfd_map. >> >> If the same filename appears another time in the NT_FILE note, we enter >> this code again. The second time, bfd_map[filename] is not nullptr and >> we try to call bfd_make_section_anyway on an already closed BFD, which >> is a use-after-free error. >> >> This patch makes sure that after closing the bfd, it is removed from the >> bfd_map. >> >> This error got exposed by a recent change in BFD (014a602b86f "Don't >> optimise bfd_seek to same position"). Since this change, opening a >> coredump which contains mapping to some special files such as a DRI >> render node (/dev/dri/renderD$NUM) exposes the issue. This happens for >> example for processes using AMDGPU devices to offload compute tasks. >> --- >> gdb/corelow.c | 5 ++++- >> 1 file changed, 4 insertions(+), 1 deletion(-) >> >> diff --git a/gdb/corelow.c b/gdb/corelow.c >> index db489b4280e..77fc4453f94 100644 >> --- a/gdb/corelow.c >> +++ b/gdb/corelow.c >> @@ -277,7 +277,10 @@ core_target::build_file_mappings () >> "during file-backed mapping note processing"), >> filename, expanded_fname.get ()); >> if (bfd != nullptr) >> - bfd_close (bfd); >> + { >> + bfd_close (bfd); >> + bfd_map.erase (filename); >> + } >> return; >> } >> /* Ensure that the bfd will be closed when core_bfd is closed. > > Maybe only insert the bfd into the map on success instead of having to do the > erase, that is something like: > > if (bfd == nullptr) > { > bfd = bfd_openr (...); > if (bfd == nullptr) > return; > if (!bfd_check_format (...)) > { > bfd_close (bfd); > return; > } > bfd_map[filename] = bfd; > } +1 for this approach. Thanks, Andrew > > -- > John Baldwin
diff --git a/gdb/corelow.c b/gdb/corelow.c index db489b4280e..77fc4453f94 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -277,7 +277,10 @@ core_target::build_file_mappings () "during file-backed mapping note processing"), filename, expanded_fname.get ()); if (bfd != nullptr) - bfd_close (bfd); + { + bfd_close (bfd); + bfd_map.erase (filename); + } return; } /* Ensure that the bfd will be closed when core_bfd is closed.