| Message ID | CAMe9rOpYfRF0DXt0j80YKTMQWd_4xHtCzT5q7qM9wkv85LaKrA@mail.gmail.com |
|---|---|
| State | New |
| Headers |
Return-Path: <binutils-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 B0C193858C24 for <patchwork@sourceware.org>; Tue, 23 Sep 2025 02:21:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0C193858C24 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=UqTjLGEz X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-yx1-xb12b.google.com (mail-yx1-xb12b.google.com [IPv6:2607:f8b0:4864:20::b12b]) by sourceware.org (Postfix) with ESMTPS id 136143858D38 for <binutils@sourceware.org>; Tue, 23 Sep 2025 02:17:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 136143858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 136143858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b12b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758593848; cv=none; b=r7aynoce04qURPJ28eqE+Db04eDAr9G1WLXc5e1NjSmTI441RzsyLEZ4hn0BbGGgggn+oewZ43/GwZNk0xPlcU1M2ozexCzVqGMOrQyV6F0AEMgCLa5abW1pbpQtgD45h0+UP12gXguMJW7on28qWxOEcrD1J73dwBuG9VUtaJE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758593848; c=relaxed/simple; bh=dHi2NngMb2HCm3rrfF70mRXGW8rYOiLsj2CCPEAJHcU=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=imRatvljbq2sdCFcwSxwzEfA1ZyyTgoooFcI1noPquIe5OmkM+BzAoUWRQnF8OWg70w2B4crmP8kINt6LpKaFimnArlzOaLq6innYTaUIvVqBdN72neFWCsGJkkfUmqkRK28Djv7Gi/RsN0DieHryaEx99jtvgtrWNOTltty6jg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 136143858D38 Received: by mail-yx1-xb12b.google.com with SMTP id 956f58d0204a3-63470a6f339so2859458d50.0 for <binutils@sourceware.org>; Mon, 22 Sep 2025 19:17:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758593847; x=1759198647; darn=sourceware.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=bDOOjS36Qherjy5Jdd8Y00xD5aHUcVU2ZysTTUOXaFQ=; b=UqTjLGEzykHLiRMZSUWCqASmevMdU7kZYw16/ABcaLDjcCA9Xt04oDXcGoDWhptfod NUizyzoSUqjbWv+8lujhnxI8FeRzD8U+ZmNA1kwypkUMv5+ktkUz0y79qT2Y4y2fVOTp 1vvlVAxt7J86OD4Bj3LkguUHPuZ/AVpZPfk0f+IaauGG8Ge5UszQXVW9xpre2f92hAm4 ar5y2+M2KjNzhY9Kx2V9lLSleChfphqXyVJ5WbY9OL9rbyYV0U8JhmlBR//A2WNI6pm6 VDmMupESucU0M/0OFT0gtqfpEfsLyF5dBmLUmWltr+DHZ+jEvBiqjlkwhXg+5qKa/yNa EVgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758593847; x=1759198647; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=bDOOjS36Qherjy5Jdd8Y00xD5aHUcVU2ZysTTUOXaFQ=; b=b2v6CtI0iViJZEr9zEyPUxZI723bZvzgYrM0Y0MvKWK7fQGkoQDlaqkp17lD1gieXt pTTXbdVek6PhiI9ji/cxVxWu807clmZpYGJhczjpoI90wNgw43X+PXzKyOURPpYyWap4 vNJi9fziqJG07f3iUWiUqVTTghgOlgLjICsE4odHTlSq2b6OHqZ3NiQLdek9VnfJ8NBM P7twq93RKdjCPRxH7Gv6jscyS74/jSWARoW65zpr6hkcbDoGZ58TXuuYm3AKa/E4bfuq TEXSMM6X4MEK06DwwH5IpA2ukVdLvmkfNgHyqi4xCVqQ6jzMCYMqGhziIRniEJ/oNDtz lkSw== X-Gm-Message-State: AOJu0YzJjFMcWnSUMoF/k/N2BsUt60Xe2jRJSNpINfZhsck1wsyADwnt 9CZKWf3Hn69qE3nf+3cK88qKCEhOtYzVJGGGwXSqj108tD02hBn3xqTnswy2+M4zRc3M+foS7iO 2w6Meep8ggPck7borNQoPFhP7XE7pDz8YXFdaJ5A= X-Gm-Gg: ASbGnctgwLeDdJQr7yka7DB7RblrErgYT8IMK1pIDdvTwMg1dvpKsW5nPS1gmBSPGuq P6+diNwZUhjyHYXs1fmwrNBi6vAW3L9VJ1QwbnF9kPc4qF3runkW71zeZLzN3+BTjfTnBbC7Dy6 TjZtQOQEHPQ+R4yIDOTnGvp7rW7S4GszZo50Kbabfn1lD1rMGG2e4/RE3AI9WnJXkq0szOqdXCU dFdQiJt X-Google-Smtp-Source: AGHT+IEfW3YwjRiLdQonfianiL7kq9edP9EhMsd3gfxJ5pWz8P8HUfpYgul6Qn6KY9Gu5yusMiTEfc0NBchMQVw5rn8= X-Received: by 2002:a05:690e:4348:b0:635:4ed0:5759 with SMTP id 956f58d0204a3-6360479280cmr672655d50.39.1758593847085; Mon, 22 Sep 2025 19:17:27 -0700 (PDT) MIME-Version: 1.0 From: "H.J. Lu" <hjl.tools@gmail.com> Date: Tue, 23 Sep 2025 10:16:50 +0800 X-Gm-Features: AS18NWBZD4SG8vTd6CKMMao7p0BKCSU1cxm25sJauBeW8qsI2DzVCs7yDixBWO4 Message-ID: <CAMe9rOpYfRF0DXt0j80YKTMQWd_4xHtCzT5q7qM9wkv85LaKrA@mail.gmail.com> Subject: elf: Check corrupt VTENTRY relocation addend To: Binutils <binutils@sourceware.org>, Alan Modra <amodra@gmail.com>, Nick Clifton <nickc@redhat.com>, Jan Beulich <JBeulich@suse.com> Content-Type: multipart/mixed; boundary="0000000000008f66ae063f6e8924" X-Spam-Status: No, score=-3014.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list <binutils.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/binutils>, <mailto:binutils-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/binutils/> List-Post: <mailto:binutils@sourceware.org> List-Help: <mailto:binutils-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/binutils>, <mailto:binutils-request@sourceware.org?subject=subscribe> Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
elf: Check corrupt VTENTRY relocation addend
|
|
Commit Message
H.J. Lu
Sept. 23, 2025, 2:16 a.m. UTC
Check corrupt VTENTRY relocation addend to avoid linker crash on h->u2.vtable->used[addend >> log_file_align] = true; PR ld/33452 * elflink.c (bfd_elf_gc_record_vtentry): Return false if VTENTRY relocation addend is too large.
Comments
On Tue, Sep 23, 2025 at 10:16:50AM +0800, H.J. Lu wrote: > Check corrupt VTENTRY relocation addend to avoid linker crash on > > h->u2.vtable->used[addend >> log_file_align] = true; > > PR ld/33452 > * elflink.c (bfd_elf_gc_record_vtentry): Return false if VTENTRY > relocation addend is too large. > > > -- > H.J. > From d604dda058572d6f12937e6499df2558a0ca896a Mon Sep 17 00:00:00 2001 > From: "H.J. Lu" <hjl.tools@gmail.com> > Date: Tue, 23 Sep 2025 05:35:09 +0800 > Subject: [PATCH 2/4] elf: Check corrupt VTENTRY relocation addend > > Check corrupt VTENTRY relocation addend to avoid linker crash on > > h->u2.vtable->used[addend >> log_file_align] = true; > > PR ld/33452 > * elflink.c (bfd_elf_gc_record_vtentry): Return false if VTENTRY > relocation addend is too large. > > Signed-off-by: H.J. Lu <hjl.tools@gmail.com> > --- > bfd/elflink.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/bfd/elflink.c b/bfd/elflink.c > index 66982f82b94..f366e8f569e 100644 > --- a/bfd/elflink.c > +++ b/bfd/elflink.c > @@ -14895,6 +14895,15 @@ bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, > } > size = (size + file_align - 1) & -file_align; > > + if (addend > size) For this to happen you must have had an overflow in prior expressions calculating size from addend, I think. Perhaps it might be better to limit vtentry reloc addends to something a lot smaller than -8ul (which I think is effectively what your patch does), to prevent insane vtable memory allocation. What is a reasonable limit to c++ vtable size? > + { > + /* xgettext:c-format */ > + _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY relocation"), > + abfd, sec); > + bfd_set_error (bfd_error_bad_value); > + return false; > + } > + > /* Allocate one extra entry for use as a "done" flag for the > consolidation pass. */ > bytes = ((size >> log_file_align) + 1) * sizeof (bool); > -- > 2.51.0 >
On 23.09.2025 13:57, Alan Modra wrote: > On Tue, Sep 23, 2025 at 10:16:50AM +0800, H.J. Lu wrote: >> --- a/bfd/elflink.c >> +++ b/bfd/elflink.c >> @@ -14895,6 +14895,15 @@ bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, >> } >> size = (size + file_align - 1) & -file_align; >> >> + if (addend > size) > > For this to happen you must have had an overflow in prior expressions > calculating size from addend, I think. Perhaps it might be better to > limit vtentry reloc addends to something a lot smaller than -8ul > (which I think is effectively what your patch does), to prevent insane > vtable memory allocation. What is a reasonable limit to c++ vtable > size? Can we really build in a heuristic like that? Arbitrarily complex class hierarchies can have arbitrarily large vtables, I suppose. Jan
On Tue, Sep 23, 2025 at 04:42:51PM +0200, Jan Beulich wrote: > On 23.09.2025 13:57, Alan Modra wrote: > > On Tue, Sep 23, 2025 at 10:16:50AM +0800, H.J. Lu wrote: > >> --- a/bfd/elflink.c > >> +++ b/bfd/elflink.c > >> @@ -14895,6 +14895,15 @@ bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, > >> } > >> size = (size + file_align - 1) & -file_align; > >> > >> + if (addend > size) > > > > For this to happen you must have had an overflow in prior expressions > > calculating size from addend, I think. Perhaps it might be better to > > limit vtentry reloc addends to something a lot smaller than -8ul > > (which I think is effectively what your patch does), to prevent insane > > vtable memory allocation. What is a reasonable limit to c++ vtable > > size? > > Can we really build in a heuristic like that? Arbitrarily complex class > hierarchies can have arbitrarily large vtables, I suppose. We can in this case. I doubt anyone would notice if gas .vtable_inherit and .vtable_entry disappeared along with all the support for VTINHERIT and VTENTRY relocs. See gcc commit a0c8285b03a4. I am committing the following patch. PR 33452 SEGV in bfd_elf_gc_record_vtentry Limit addends on vtentry relocs, otherwise ld might attempt to allocate a stupidly large array. This also fixes the expression overflow leading to pr33452. A vtable of 33M entries on a 64-bit host is surely large enough, especially considering that VTINHERIT and VTENTRY relocations are to support -fvtable-gc that disappeared from gcc over 20 years ago. PR ld/33452 * elflink.c (bfd_elf_gc_record_vtentry): Sanity check addend. diff --git a/bfd/elflink.c b/bfd/elflink.c index 54f0d6e957e..0a0456177c2 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -14865,7 +14865,7 @@ bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, const struct elf_backend_data *bed = get_elf_backend_data (abfd); unsigned int log_file_align = bed->s->log_file_align; - if (!h) + if (!h || addend > 1u << 28) { /* xgettext:c-format */ _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
On 25.09.2025 02:39, Alan Modra wrote: > On Tue, Sep 23, 2025 at 04:42:51PM +0200, Jan Beulich wrote: >> On 23.09.2025 13:57, Alan Modra wrote: >>> On Tue, Sep 23, 2025 at 10:16:50AM +0800, H.J. Lu wrote: >>>> --- a/bfd/elflink.c >>>> +++ b/bfd/elflink.c >>>> @@ -14895,6 +14895,15 @@ bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, >>>> } >>>> size = (size + file_align - 1) & -file_align; >>>> >>>> + if (addend > size) >>> >>> For this to happen you must have had an overflow in prior expressions >>> calculating size from addend, I think. Perhaps it might be better to >>> limit vtentry reloc addends to something a lot smaller than -8ul >>> (which I think is effectively what your patch does), to prevent insane >>> vtable memory allocation. What is a reasonable limit to c++ vtable >>> size? >> >> Can we really build in a heuristic like that? Arbitrarily complex class >> hierarchies can have arbitrarily large vtables, I suppose. > > We can in this case. I doubt anyone would notice if gas > .vtable_inherit and .vtable_entry disappeared along with all the > support for VTINHERIT and VTENTRY relocs. See gcc commit a0c8285b03a4. > I am committing the following patch. > > > PR 33452 SEGV in bfd_elf_gc_record_vtentry > > Limit addends on vtentry relocs, otherwise ld might attempt to > allocate a stupidly large array. This also fixes the expression > overflow leading to pr33452. A vtable of 33M entries on a 64-bit > host is surely large enough, especially considering that VTINHERIT > and VTENTRY relocations are to support -fvtable-gc that disappeared > from gcc over 20 years ago. Oh, I didn't know this was only historic functionality. Jan
From d604dda058572d6f12937e6499df2558a0ca896a Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.tools@gmail.com> Date: Tue, 23 Sep 2025 05:35:09 +0800 Subject: [PATCH 2/4] elf: Check corrupt VTENTRY relocation addend Check corrupt VTENTRY relocation addend to avoid linker crash on h->u2.vtable->used[addend >> log_file_align] = true; PR ld/33452 * elflink.c (bfd_elf_gc_record_vtentry): Return false if VTENTRY relocation addend is too large. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> --- bfd/elflink.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bfd/elflink.c b/bfd/elflink.c index 66982f82b94..f366e8f569e 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -14895,6 +14895,15 @@ bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, } size = (size + file_align - 1) & -file_align; + if (addend > size) + { + /* xgettext:c-format */ + _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY relocation"), + abfd, sec); + bfd_set_error (bfd_error_bad_value); + return false; + } + /* Allocate one extra entry for use as a "done" flag for the consolidation pass. */ bytes = ((size >> log_file_align) + 1) * sizeof (bool); -- 2.51.0