* Re: bfd_elf_set_group_contents assertion
@ 2022-08-23 9:05 Alan Modra
0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2022-08-23 9:05 UTC (permalink / raw)
To: binutils
Further to commit 7744e3278b9f.
* elf.c (bfd_elf_set_group_contents): Restrict loc in loop writing
contents, and add another assertion.
diff --git a/bfd/elf.c b/bfd/elf.c
index 35aa45e3b90..f186a2cffe9 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -3594,6 +3594,8 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
{
elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
loc -= 4;
+ if (loc == sec->contents)
+ break;
H_PUT_32 (abfd, elf_sec->rel.idx, loc);
}
if (elf_sec->rela.hdr != NULL
@@ -3603,9 +3605,13 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
{
elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
loc -= 4;
+ if (loc == sec->contents)
+ break;
H_PUT_32 (abfd, elf_sec->rela.idx, loc);
}
loc -= 4;
+ if (loc == sec->contents)
+ break;
H_PUT_32 (abfd, elf_sec->this_idx, loc);
}
elt = elf_next_in_group (elt);
@@ -3613,12 +3619,20 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
break;
}
- loc -= 4;
- if (loc != sec->contents)
+ /* We should always get here with loc == sec->contents + 4, but it is
+ possible to craft bogus SHT_GROUP sections that will cause segfaults
+ in objcopy without checking loc here and in the loop above. */
+ if (loc == sec->contents)
+ BFD_ASSERT (0);
+ else
{
- BFD_ASSERT (0);
- memset (sec->contents + 4, 0, loc - sec->contents);
- loc = sec->contents;
+ loc -= 4;
+ if (loc != sec->contents)
+ {
+ BFD_ASSERT (0);
+ memset (sec->contents + 4, 0, loc - sec->contents);
+ loc = sec->contents;
+ }
}
H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 2+ messages in thread
* bfd_elf_set_group_contents assertion
@ 2022-08-17 8:00 Alan Modra
0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2022-08-17 8:00 UTC (permalink / raw)
To: binutils
objcopy of broken SHT_GROUP sections shouldn't write garbage.
* elf.c (bfd_elf_set_group_contents): If number of entries is
unexpected, fill out section with zeros.
diff --git a/bfd/elf.c b/bfd/elf.c
index 2e01d7e9a44..35aa45e3b90 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -3614,7 +3614,12 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
}
loc -= 4;
- BFD_ASSERT (loc == sec->contents);
+ if (loc != sec->contents)
+ {
+ BFD_ASSERT (0);
+ memset (sec->contents + 4, 0, loc - sec->contents);
+ loc = sec->contents;
+ }
H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
}
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-08-23 9:05 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-23 9:05 bfd_elf_set_group_contents assertion Alan Modra
-- strict thread matches above, loose matches on Subject: below --
2022-08-17 8:00 Alan Modra
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).