elf_memory again, readonly or readwrite...

Mark Wielaard mark@klomp.org
Fri Oct 18 15:03:21 GMT 2024


Hi,

On Thu, 2024-08-29 at 15:47 +0200, Mark Wielaard wrote:
> So we changed elf_memory so it pretends the in-memory Elf image is
> read with ELF_C_READ_MMAP. This helps when calling elf_memory on
> read-only memory which still wants to change some things about the Elf
> like uncompress some sections (which changes the section header).
> 
> With ELF_C_READ_MMAP libelf will make a copy of the section headers,
> so any changes aren't written to the memory image (because that would
> crash if the underlying memory is read-only).
> 
> But that breaks another use case where elf_memory is used on rw memory
> and then the section headers are updated using libelf and it is
> expected those in-memory section headers reflect the changes.
> 
> The problem of course is the elf_memory function doesn't take a "mode"
> argument. So we have to guess. And make one or the other usage
> unusable.
> 
> I think we should assume the memory is read/write and at least header
> updates are written back to the memory image. But that when only just
> reading the image then nothing is changed and written back to the
> image.
> 
> This does mean that when explicitly uncompressing sections you have to
> make sure the image is writable (because that updates the shdrs).

I waited too long deciding what to do here. Sorry. We are about to make
the 0.192 release today. If it isn't too late then I would like to
apply my proposed patch (as attached).

It isn't ideal, but it basically reverts to the behavior of elfutils
0.190. So you can again do direct manipulation on ELF images created
through elf_memory. So in that way it was a regression in 0.191, fixed
for 0.192.

It does mean that if the memory you gave elf_memory is read-only and
you call elf_compress on one of the sections things might crash
(because elf_compress will try to update the Elf_Shdr structure in
place to set the new size). Which indeed could be seen as a regression
from 0.191 (but was there before). If you do want to decompress (or
otherwise manipulate) the ELF image created through elf_memory you have
to do what the testcase (now) does. mmap the image using MMAP_PRIVATE
so it is (copy-on-)writable.

Longer term we probably want a new elf_memory2 function that takes an
argument describing the protection bits of the given memory.

Aaron, is it OK to commit this before you start the 0.192 release
process?

Cheers,

Mark
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-libelf-Treat-elf_memory-image-as-writable.patch
Type: text/x-patch
Size: 2516 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/elfutils-devel/attachments/20241018/b743db85/attachment.bin>


More information about the Elfutils-devel mailing list