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

Bugfix for CompPhdr::operator() that could cause sort to fail. #139

Merged
merged 2 commits into from
Jan 8, 2018
Merged

Bugfix for CompPhdr::operator() that could cause sort to fail. #139

merged 2 commits into from
Jan 8, 2018

Conversation

kmillar
Copy link
Contributor

@kmillar kmillar commented Dec 7, 2017

Modified CompPhdr::operator() so that it provides a strict weak ordering as required by std::sort.

weak ordering as required by std::sort.
@domenkozar
Copy link
Member

Do you have a test case?

src/patchelf.cc Outdated
if (x.p_type == PT_PHDR) {
if (y.p_type == PT_PHDR) return false;
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

This could be simplified a bit (mostly removing the duplicated check of y.p_type == PT_PHDR:

// Nothing is less than a PHDR:
if (y.p_type == PT_PHDR) return false;

// PHDR's are less than everything (that's not a PHDR)
if (x.p_type == PT_PHDR) return true;

// Sort non-PHDR's by address:
...

Comments are just suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

While it does seem worth fixing this, according to documents such as [1][2] it seems that only a single PT_PHDR entry may legally exist in a file.

Thinking about what PT_PHDR actually means, this makes sense :). Also my comments are indeed a bit silly, I didn't have my "ELF-knowledge hat" on.

So this is probably harmless as-is: for any legal set of segments this operator will define a valid (strict weak) ordering.

Might be worth ensuring we reject files that attempt to have multiple PT_PHDR segments?

[1] http://www.sco.com/developers/gabi/latest/ch5.pheader.html
[2] http://www.skyfree.org/linux/references/ELF_Format.pdf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've simplified the code now. Thanks.

Yes, this was found using a debug version of std::sort, not by obviously incorrect results. Since there's only a single PT_PHDR entry, It probably doesn't any difference on any sane implementation of std::sort, but it is undefined behavior, so there's no guarantees about that.

@dtzWill
Copy link
Member

dtzWill commented Jan 5, 2018

@domenkozar see my comment on the commit -- I'm not sure a test case (as an ELF input file) could be produced with normal tools. Even if such a file were constructed, patchelf likely wouldn't do anything observably incorrect, unless running with some sort of debug version of std::sort?

Although if @kmillar has such a file that certainly would be interesting! :)

@edolstra edolstra merged commit 71e99c5 into NixOS:master Jan 8, 2018
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.

None yet

4 participants