On Tue, 2014-06-10 at 15:31 +0200, Thilo Schulz wrote:
On Tuesday 10 June 2014 11:48:15 Mark Wielaard wrote:
On Mon, 2014-06-09 at 21:05 +0200, Thilo Schulz wrote:
When adding data to existing sections in ELF files, libelf may corrupt those sections, i.e. overwrite the existing data if certain conditions are met.
If an Elf_Scn structure has seen a call to elf_rawdata(scn) before but no call to elf_getdata(scn), scn->read_data flag is set, but not scn->data_list_rear.
Do you happen to have a small testcase that shows the buggy behavior?
Sure. This is an excerpt from the final program.
Thanks. It think that shows the second bug you describe. It helped me write a specific test case for both issues (attached).
I was wondering whether we want to check scn->rawdata.s directly, or if we could rely on ELF_F_FILEDATA being set for scn->flags?
Seems reasonable though I don't know the code as well as you do I guess.
I wish I understood the code very well :) But now that I wrote the testcase and you pointed out the second bug, I am not sure of the fix anymore. It does seem to fix the first issue, but then you immediately hit the second.
As a further note: A similar bug, albeit for slightly different reasons, occurs when adding relocations. Adding a relocation with elf_newdata() then elf_update() results in the old data being "forgotten" if there was no elf_getdata() call before to load that data into memory. The cause is a bit different because in this case, there was not a call to elf_rawdata() before and this still happened. I imagine, this might also be a problem for string tables.
Indeed. The attached testcase shows both issues. Calling elf_getdata() and then elf_newdata() works as expected. But elf_newdata drops all existing data when elf_rawdata is called before and elf_newdata keeps the size, but not the actual content bytes of existing data of a section if elf_getdata isn't called before.
Still scratching my head a little how to resolve both issues properly.
Thanks,
Mark