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

Compute program header flags in rewriteSectionsLibrary #467

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cgzones
Copy link
Contributor

@cgzones cgzones commented Feb 22, 2023

Instead of using fixed flags of RW for the new program header, holding
the replaced sections, check if the write or execute flags is required
by any accompanying section.

If many sections are going to be replaced a more strict method would be
to split them up and add up-to 4 new program headers if necessary:
read-only, read-write, read-execute, read-write-execute.

p.s.:
also the harded flags for executables seems suspicious:

wri(phdr.p_flags, PF_R | PF_W);

Maybe that should be wri(phdr.p_flags, rdi(phdrs.at(splitIndex).p_flags));?

p.p.s:
Should these lines convert the read data

patchelf/src/patchelf.cc

Lines 558 to 560 in e37f892

wri(phdr.p_offset, phdrs.at(splitIndex).p_offset - splitShift - shift);
wri(phdr.p_paddr, phdrs.at(splitIndex).p_paddr - splitShift - shift);
wri(phdr.p_vaddr, phdrs.at(splitIndex).p_vaddr - splitShift - shift);

e.g.

 wri(phdr.p_offset, rdi(phdrs.at(splitIndex).p_offset) - splitShift - shift); 
 wri(phdr.p_paddr, rdi(phdrs.at(splitIndex).p_paddr) - splitShift - shift); 
 wri(phdr.p_vaddr, rdi(phdrs.at(splitIndex).p_vaddr) - splitShift - shift);

?

Use reference instead of a copy of the struct as lambda argument.
Use for loop to limit the scope of the iterator varible i.
Instead of using fixed flags of RW for the new program header, holding
the replaced sections, check if the write or execute flags is required
by any accompanying section.

If many sections are going to be replaced a more strict method would be
to split them up and add up-to 4 new program headers if necessary:
read-only, read-write, read-execute, read-write-execute.
@Mic92
Copy link
Member

Mic92 commented Feb 24, 2023

cc @brenoguim

Copy link
Collaborator

@brenoguim brenoguim left a comment

Choose a reason for hiding this comment

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

I'm glad that this is being done, because it's a bit concerning that some sections are exposed with different access rights. Even though it doesn't fix everything, it's already a good improvement.

This is the kind of bug that would fit in perfectly with #468 because it would be lovely have a test for it. And in the current state of things, it would be cumbersome to create a reliable one.

Comment on lines +847 to +854
for (const auto & rs : replacedSections) {
const auto flags = findSectionHeader(rs.first).sh_flags;
if (flags & SHF_WRITE)
need_write = true;
if (flags & SHF_EXECINSTR)
need_exec = true;
}
wri(phdr.p_flags, PF_R | (need_write ? PF_W : 0) | (need_exec ? PF_X : 0));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another way to write this would be:

auto flagUnion = PF_R;
for (const auto& rs : replacedSections) {
    const auto flags = findSectionHeader(rs.first).sh_flags;
    flagUnion |= (flags & SHF_WRITE) ? PF_W : 0;
    flagUnion |= (flags & SHF_EXECINSTR) ? PF_X : 0;
}
wri(phdr.p_flags, flagUnion);

But that's style, feel free to drop.

@brenoguim
Copy link
Collaborator

also the harded flags for executables seems suspicious:

Since these flags are not being tracked, RW is the "works for all". I don't think we ever move executable sessions (because they are usually progbits) so we never needed.

Should these lines convert the read data

I think so. I would expect things to break drastically if endianess is not respected. Do you know if we have tests for that?

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.

3 participants