| Message ID | 20250908021729.19560-1-amerey@redhat.com |
|---|---|
| State | Superseded |
| Headers |
Return-Path: <elfutils-devel-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 E25723858D37 for <patchwork@sourceware.org>; Mon, 8 Sep 2025 02:18:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E25723858D37 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=b/5nAeiR X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 4ACD13858D1E for <elfutils-devel@sourceware.org>; Mon, 8 Sep 2025 02:17:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4ACD13858D1E Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4ACD13858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757297855; cv=none; b=r+IsXymx2JVrekyg3/oAa+SQuJ8AHuhaawUaBeu/i3xNxTuzzld8CTzSbiZ/QfgAyNNhwOu8jj7nmdEzRTW/KW64Pk7dwDW/YnRnsJdOfNM3Wg9z/VZbgODLcBWuAw/LxtkuDr0jR5B0pA9Hc5c2UKoezXqbLeYhc9F7ThaAFLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757297855; c=relaxed/simple; bh=VzXpczQts+HUUgPvtQxbAlut2u77JbW+9cqaqwScYEs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CswFMD6hNPlkdobTWKC6bu7QO6I+Wc8aXLh9CGQTEgHS5JHdTC6OzNoJF/4rH2HefTJP8hEQP1g7qibXjGOc+hQOL+6EcRfnGD34XRXihcN1NVkXivM3nKRggyzL3miVQjfENOXsaBm9eIHF+EYgFUSUfKWTw7wJ8s9gik0aRS0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4ACD13858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757297855; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=iHt1BUwfVRBA2+swlhN6kQO7Oy9LtK0/JdfMv/cOqrQ=; b=b/5nAeiR790UHy5/0GaGqc1gSWIuu4ohnOORLWZraskfXFnQmhjLOejReLd4NXtA6j7Tk6 +aCtXrWJd11mnNgYAnIo1EeF7pfmp0Lz5jxpxxwV73nyDhvoY3mzXEO6Ys2VtuqN6tNiOx iTjD4LAm2+S9MGiQF7GVNXwQnWIh974= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-17-JJVulswfPdCzYntctpAPDw-1; Sun, 07 Sep 2025 22:17:33 -0400 X-MC-Unique: JJVulswfPdCzYntctpAPDw-1 X-Mimecast-MFC-AGG-ID: JJVulswfPdCzYntctpAPDw_1757297852 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 902A71800366 for <elfutils-devel@sourceware.org>; Mon, 8 Sep 2025 02:17:32 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.80.68]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B22E11955F24; Mon, 8 Sep 2025 02:17:31 +0000 (UTC) From: Aaron Merey <amerey@redhat.com> To: elfutils-devel@sourceware.org Cc: Aaron Merey <amerey@redhat.com> Subject: [PATCH 1/3] elf_getarhdr.c: Return correct header for archive within an archive Date: Sun, 7 Sep 2025 22:17:27 -0400 Message-ID: <20250908021729.19560-1-amerey@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: TrvA1VNAKnWXfJ2knKrWB5CgHOjeZ-IVXAqvqnfAQfg_1757297852 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.3 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_H5, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, 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 X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Elfutils-devel mailing list <elfutils-devel.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/elfutils-devel>, <mailto:elfutils-devel-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/elfutils-devel/> List-Post: <mailto:elfutils-devel@sourceware.org> List-Help: <mailto:elfutils-devel-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/elfutils-devel>, <mailto:elfutils-devel-request@sourceware.org?subject=subscribe> Errors-To: elfutils-devel-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
[1/3] elf_getarhdr.c: Return correct header for archive within an archive
|
|
Commit Message
Aaron Merey
Sept. 8, 2025, 2:17 a.m. UTC
If elf_getarhdr is called on a descriptor that refers to an archive that
is itself a member of an outer archive, it may return the Elf_Arhdr of
the current member of the inner archive instead of Elf_Arhdr of the inner
archive itself.
This also causes a memory leak: elf_end only attempts to free
Elf_Arhdr fields ar_name and ar_rawname for descriptors that are not
ELF_K_AR.
Fix this by adding a new field ar_ar_hdr to Elf->state.ar. The
ar_ar_hdr field stores the Elf_Arhdr of an archive which is itself a member
of an archive. elf_getarhdr now returns ar_ar_hdr for ELF_K_AR descriptors
associated with a parent archive and elf_end will free ar_ar_hdr.ar_name and
ar_ar_hdr.ar_rawname.
Siged-off-by: Aaron Merey <amerey@redhat.com>
---
This series addresses the following bugs:
https://issues.oss-fuzz.com/issues/440209723
https://issues.oss-fuzz.com/issues/440177309
There is at least one other bug in eu-readelf:process_elf where the code
does not account for archives within archives:
off_t aroff = elf_getaroff (elf);
pure_elf = dwelf_elf_begin (fd);
if (aroff > 0)
{
/* Archive member. */
(void) elf_rand (pure_elf, aroff);
Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
elf_end (pure_elf);
pure_elf = armem;
}
if (pure_elf == NULL)
{
error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
return;
}
process_elf attempts to re-read archive members to avoid printing
relocations that may have been applied earlier. But for members of
an inner archive this fails. elf_rand is called with the outer archive
descriptor (pure_elf) but aroff is relative to the inner archive.
I will add tests for archives-within-archives cases and check other
eu-* tools that may be affected.
libelf/elf_begin.c | 6 +++++-
libelf/elf_end.c | 8 ++++++++
libelf/elf_getarhdr.c | 5 ++++-
libelf/libelfP.h | 2 ++
4 files changed, 19 insertions(+), 2 deletions(-)
Comments
Hi Aaron, On Sun, 2025-09-07 at 22:17 -0400, Aaron Merey wrote: > If elf_getarhdr is called on a descriptor that refers to an archive that > is itself a member of an outer archive, it may return the Elf_Arhdr of > the current member of the inner archive instead of Elf_Arhdr of the inner > archive itself. > > This also causes a memory leak: elf_end only attempts to free > Elf_Arhdr fields ar_name and ar_rawname for descriptors that are not > ELF_K_AR. > > Fix this by adding a new field ar_ar_hdr to Elf->state.ar. The > ar_ar_hdr field stores the Elf_Arhdr of an archive which is itself a member > of an archive. elf_getarhdr now returns ar_ar_hdr for ELF_K_AR descriptors > associated with a parent archive and elf_end will free ar_ar_hdr.ar_name and > ar_ar_hdr.ar_rawname. The naming confused me a little. ar_ar_hdr for the ar union member plays the same role as the elf_ar_hdr for the elf[32|64] union members. But the ar.elf_ar_hdr field us used for a different purpose (holding the current/offset ar_hdr). It might make sense to rename these and even maybe move the elf_ar_hdr /ar_ar_hdr out of the union members and just have them as part of the Elf struct itself because now every Elf has an "am I an Elf ar member" field. Maybe do this after you added more tests though and after playing with pahole to see if there is an ideal layout of the struct/union members. > Siged-off-by: Aaron Merey <amerey@redhat.com> > --- > > This series addresses the following bugs: > https://issues.oss-fuzz.com/issues/440209723 > https://issues.oss-fuzz.com/issues/440177309 > > There is at least one other bug in eu-readelf:process_elf where the code > does not account for archives within archives: > > off_t aroff = elf_getaroff (elf); > pure_elf = dwelf_elf_begin (fd); > if (aroff > 0) > { > /* Archive member. */ > (void) elf_rand (pure_elf, aroff); > Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf); > elf_end (pure_elf); > pure_elf = armem; > } > if (pure_elf == NULL) > { > error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1)); > return; > } > > process_elf attempts to re-read archive members to avoid printing > relocations that may have been applied earlier. But for members of > an inner archive this fails. elf_rand is called with the outer archive > descriptor (pure_elf) but aroff is relative to the inner archive. > > I will add tests for archives-within-archives cases and check other > eu-* tools that may be affected. Yes, more tests would be nice. > libelf/elf_begin.c | 6 +++++- > libelf/elf_end.c | 8 ++++++++ > libelf/elf_getarhdr.c | 5 ++++- > libelf/libelfP.h | 2 ++ > 4 files changed, 19 insertions(+), 2 deletions(-) > > diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c > index d3ab887d..823a4324 100644 > --- a/libelf/elf_begin.c > +++ b/libelf/elf_begin.c > @@ -1128,8 +1128,12 @@ dup_elf (int fildes, Elf_Cmd cmd, Elf *ref) > { > /* Enlist this new descriptor in the list of children. */ > result->next = ref->state.ar.children; > - result->state.elf.elf_ar_hdr = ar_hdr; > ref->state.ar.children = result; > + > + if (result->kind == ELF_K_AR) > + result->state.ar.ar_ar_hdr = ar_hdr; > + else > + result->state.elf.elf_ar_hdr = ar_hdr; > } > else > { Looks OK. > diff --git a/libelf/elf_end.c b/libelf/elf_end.c > index 1d366127..460b09b7 100644 > --- a/libelf/elf_end.c > +++ b/libelf/elf_end.c > @@ -124,6 +124,14 @@ elf_end (Elf *elf) > if (elf->state.elf.elf_ar_hdr.ar_rawname != NULL) > free (elf->state.elf.elf_ar_hdr.ar_rawname); > } > + else > + { > + if (elf->state.ar.ar_ar_hdr.ar_name != NULL) > + free (elf->state.ar.ar_ar_hdr.ar_name); > + > + if (elf->state.ar.ar_ar_hdr.ar_rawname != NULL) > + free (elf->state.ar.ar_ar_hdr.ar_rawname); > + } > > /* This was the last activation. Free all resources. */ > switch (elf->kind) Looks OK. > diff --git a/libelf/elf_getarhdr.c b/libelf/elf_getarhdr.c > index 9211fc2e..a5fde49b 100644 > --- a/libelf/elf_getarhdr.c > +++ b/libelf/elf_getarhdr.c > @@ -51,5 +51,8 @@ elf_getarhdr (Elf *elf) > return NULL; > } > > - return &elf->state.elf.elf_ar_hdr; > + if (elf->kind == ELF_K_AR) > + return &elf->state.ar.ar_ar_hdr; > + else > + return &elf->state.elf.elf_ar_hdr; > } Looks OK. So in all these cases it would be simpler if there was just an state.elf_ar_hdr field so you could use that instead of having to select on elf->kind. > diff --git a/libelf/libelfP.h b/libelf/libelfP.h > index 1b93da88..568d4b26 100644 > --- a/libelf/libelfP.h > +++ b/libelf/libelfP.h > @@ -403,6 +403,8 @@ struct Elf > char ar_name[16]; /* NUL terminated ar_name of elf_ar_hdr. */ > char raw_name[17]; /* This is a buffer for the NUL terminated > named raw_name used in the elf_ar_hdr. */ > + Elf_Arhdr ar_ar_hdr; /* Archive header of this archive. Used when > + an archive is a member of an archive. */ > } ar; > } state; > Might want to check placement of this field with pahole. It now comes after a 17 element array which means there is at least a small gap for alignment. Cheers, Mark
Hi Mark, On Tue, Sep 9, 2025 at 10:24 AM Mark Wielaard <mark@klomp.org> wrote: > > Hi Aaron, > > On Sun, 2025-09-07 at 22:17 -0400, Aaron Merey wrote: > > If elf_getarhdr is called on a descriptor that refers to an archive that > > is itself a member of an outer archive, it may return the Elf_Arhdr of > > the current member of the inner archive instead of Elf_Arhdr of the inner > > archive itself. > > > > This also causes a memory leak: elf_end only attempts to free > > Elf_Arhdr fields ar_name and ar_rawname for descriptors that are not > > ELF_K_AR. > > > > Fix this by adding a new field ar_ar_hdr to Elf->state.ar. The > > ar_ar_hdr field stores the Elf_Arhdr of an archive which is itself a member > > of an archive. elf_getarhdr now returns ar_ar_hdr for ELF_K_AR descriptors > > associated with a parent archive and elf_end will free ar_ar_hdr.ar_name and > > ar_ar_hdr.ar_rawname. > > The naming confused me a little. ar_ar_hdr for the ar union member > plays the same role as the elf_ar_hdr for the elf[32|64] union members. > But the ar.elf_ar_hdr field us used for a different purpose (holding > the current/offset ar_hdr). > > It might make sense to rename these and even maybe move the elf_ar_hdr > /ar_ar_hdr out of the union members and just have them as part of the > Elf struct itself because now every Elf has an "am I an Elf ar member" > field. > > Maybe do this after you added more tests though and after playing with > pahole to see if there is an ideal layout of the struct/union members. I agree that it's better to combine state.elf[32|64].elf_ar_hdr and state.ar.ar_ar_hdr into one struct Elf field outside of elf->state. It's less confusing and we can set/get elf_ar_hdr without needing to check elf->kind, like you said. I used pahole to compare this suggested layout with the layout given in this patch (new ar_ar_hdr field in state.ar). There is no difference in the struct Elf size or the amount of padding: with --enable-thread-safety: /* size: 368, cachelines: 6, members: 14 */ /* last cacheline: 48 bytes */ with --disable-thread-safety: /* size: 312, cachelines: 5, members: 13 */ /* sum members: 308, holes: 1, sum holes: 4 */ /* last cacheline: 56 bytes */ I will revise this patch and add more tests. Aaron
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c index d3ab887d..823a4324 100644 --- a/libelf/elf_begin.c +++ b/libelf/elf_begin.c @@ -1128,8 +1128,12 @@ dup_elf (int fildes, Elf_Cmd cmd, Elf *ref) { /* Enlist this new descriptor in the list of children. */ result->next = ref->state.ar.children; - result->state.elf.elf_ar_hdr = ar_hdr; ref->state.ar.children = result; + + if (result->kind == ELF_K_AR) + result->state.ar.ar_ar_hdr = ar_hdr; + else + result->state.elf.elf_ar_hdr = ar_hdr; } else { diff --git a/libelf/elf_end.c b/libelf/elf_end.c index 1d366127..460b09b7 100644 --- a/libelf/elf_end.c +++ b/libelf/elf_end.c @@ -124,6 +124,14 @@ elf_end (Elf *elf) if (elf->state.elf.elf_ar_hdr.ar_rawname != NULL) free (elf->state.elf.elf_ar_hdr.ar_rawname); } + else + { + if (elf->state.ar.ar_ar_hdr.ar_name != NULL) + free (elf->state.ar.ar_ar_hdr.ar_name); + + if (elf->state.ar.ar_ar_hdr.ar_rawname != NULL) + free (elf->state.ar.ar_ar_hdr.ar_rawname); + } /* This was the last activation. Free all resources. */ switch (elf->kind) diff --git a/libelf/elf_getarhdr.c b/libelf/elf_getarhdr.c index 9211fc2e..a5fde49b 100644 --- a/libelf/elf_getarhdr.c +++ b/libelf/elf_getarhdr.c @@ -51,5 +51,8 @@ elf_getarhdr (Elf *elf) return NULL; } - return &elf->state.elf.elf_ar_hdr; + if (elf->kind == ELF_K_AR) + return &elf->state.ar.ar_ar_hdr; + else + return &elf->state.elf.elf_ar_hdr; } diff --git a/libelf/libelfP.h b/libelf/libelfP.h index 1b93da88..568d4b26 100644 --- a/libelf/libelfP.h +++ b/libelf/libelfP.h @@ -403,6 +403,8 @@ struct Elf char ar_name[16]; /* NUL terminated ar_name of elf_ar_hdr. */ char raw_name[17]; /* This is a buffer for the NUL terminated named raw_name used in the elf_ar_hdr. */ + Elf_Arhdr ar_ar_hdr; /* Archive header of this archive. Used when + an archive is a member of an archive. */ } ar; } state;