Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: Handle SHN_XINDEX for strtab section index (#304) #316

Merged
merged 1 commit into from
Jul 18, 2022

Conversation

tux3
Copy link
Contributor

@tux3 tux3 commented Jul 7, 2022

This attempts to fix #304.
With this patch, I no longer reproduce the issue using the same large object file that triggered it before.

I've also updated the test code to handle XINDEX for the strtab, however I don't know a good way to generate a test case for an ELF file with >=2^16 sections without embedding a very large blob, so I've left it for future work.
(If I've missed a simple way to do this, happy to do it now)

src/elf/mod.rs Outdated Show resolved Hide resolved
Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this LGTM, thanks for adding this, quite surprised wasn't done before! feel free to check about logging, otherwise we're good.

@@ -51,7 +51,12 @@ fn parse_text_section_size_lazy(base: &[u8]) -> Result<u64, &'static str> {
SectionHeader::parse(base, header.e_shoff as usize, header.e_shnum as usize, ctx)
.map_err(|_| "parse section headers error")?;

let strtab_idx = header.e_shstrndx as usize;
let strtab_idx = if header.e_shstrndx as u32 == SHN_XINDEX {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird i feel like this was added already, long time ago, surprised to see it now

src/elf/mod.rs Outdated Show resolved Hide resolved
src/elf/mod.rs Show resolved Hide resolved
// FIXME: warn! here
return Ok(Strtab::default())
}
section_idx = section_headers[0].sh_link as usize;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious (can't remember why) it's always the first section header's sh_link that is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to. I actually looked what Gimli was doing, but the ELF docs at Oracle says:

If the section name string table section index is greater than or equal to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff) and the actual index of the section name string table section is contained in the sh_link field of the section header at index 0. Otherwise, the sh_link member of the initial section header entry contains the value zero. See Table 12-6 and Table 12-7.

@m4b
Copy link
Owner

m4b commented Jul 11, 2022

And thank you for stepping up and fixing that issue!

This happens when the number of sections in an ELF object reaches 2^16,
at which point the u16 shstrndx takes the special value SHN_XINDEX.

Fix based on algorithm from:
github.com/gimli-rs/object/blob/c476071/src/read/elf/file.rs#L582-L600
@tux3 tux3 force-pushed the elf_strtab_xindex branch from f54ae35 to 35663eb Compare July 11, 2022 21:25
@m4b m4b merged commit 0ac017b into m4b:master Jul 18, 2022
@m4b
Copy link
Owner

m4b commented Jul 18, 2022

released in 0.5.3 on crates; thanks for fixing this @tux3!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

goblin >= 0.4.2 fails to parse valid ELF Strtab with "bad input invalid utf8"
2 participants