Page MenuHomeFreeBSD

libelf: Fix extended numbering detection
ClosedPublic

Authored by cem on Dec 3 2016, 11:01 PM.
Tags
None
Referenced Files
F84068624: D8701.diff
Sat, May 18, 8:48 PM
Unknown Object (File)
Thu, May 16, 5:22 AM
Unknown Object (File)
Sat, May 11, 10:00 PM
Unknown Object (File)
Sat, May 11, 9:59 PM
Unknown Object (File)
Sat, May 11, 7:50 PM
Unknown Object (File)
Sat, May 11, 9:42 AM
Unknown Object (File)
Fri, May 10, 11:18 AM
Unknown Object (File)
Sat, Apr 27, 11:33 PM
Subscribers

Details

Summary

Extended numbering is used for any of these fields overflowing.

Diff Detail

Lint
No Lint Coverage
Unit
No Test Coverage
Build Status
Buildable 6143
Build 6399: arc lint + arc unit

Event Timeline

cem retitled this revision from to libelf: Fix extended numbering detection.
cem updated this object.
cem edited the test plan for this revision. (Show Details)
cem added a reviewer: emaste.
contrib/elftoolchain/libelf/libelf_ehdr.c
196

What's the story with the or in the previous case?

contrib/elftoolchain/libelf/libelf_ehdr.c
190

This check should be shnum == 0, not !=.

196

My change to it is slightly wrong. I think the shoff check should just be removed.

The directly preceding if statement verifies that extended ELF objects have a section header table (shoff != 0). (Modulo a small bug.)

So in this condition, we can just check for an non-extended ELF object directly (shnum != 0 && phnum != PN_XNUM && strndx != SHN_XINDEX) and ignore shoff.

Fix extended ELF => section header invariant; remove useless shoff check.

contrib/elftoolchain/libelf/libelf_ehdr.c
173–176

I suspect this comment belongs below instead.

190

I think shnum != 0 is correct. We're looking for invalid values or combinations of values.

  • shnum >= SHN_LORESERVE is invalid
  • if shoff == 0LL we have no section headers, so the file is invalid if:
    • we have a section header count (shnum != 0)
    • phnum == PN_XNUM indicating extended numbering for the phdr count
    • strndx == SHN_XINDEX indicating extended numbering for the string section

Or maybe the test should be

(shoff == 0 || shnum == 0) && (phnum == PN_XNUM || strndx == SHN_XINDEX)

196

I think if we change to the test suggested above this can be just phnum != PN_XNUM && strndx != SHN_XINDEX

cem marked 3 inline comments as done.Dec 6 2016, 12:04 AM
cem added inline comments.
contrib/elftoolchain/libelf/libelf_ehdr.c
173–176

Yeah, agreed.

190

I think you're right re: shnum != 0.

http://docs.oracle.com/cd/E19253-01/817-1984/chapter6-46512/index.html:

If a file has no section header table, e_shnum holds the value zero.

If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), e_shnum has the value zero.

So zero is overloaded and we need shoff to determine whether this is an extended shnum or empty section header table.

I don't think the proposed test is correct, since shoff != 0 && shnum == 0 && phnum == PN_XNUM is valid (both shnum and phnum are extended).

196

Both are wrong, I think.

We need phnum != PN_XNUM && strndx != SHN_XINDEX and also need to exclude objects with only extended shnum (shnum == 0 && shoff != 0).

cem marked 6 inline comments as done.
  • Move old comment down to a more correct location.
  • Revert change in shnum sense. It was right before.
  • Fix check for extended values and simplify boolean expression by inverting the sense.
contrib/elftoolchain/libelf/libelf_ehdr.c
200

Should this be the opposite of the test above, i.e. shoff != 0 && (shnum == 0 || phnum == PN_XNUM || strndx == SHN_XINDEX)?

contrib/elftoolchain/libelf/libelf_ehdr.c
200

Those should be functionally identical after the previous if statement/return.

emaste edited edge metadata.

I believe this is correct. @kaiw if you're going to review as well I'll hold off on committing it to the upstream elftoolchain repo until then.

contrib/elftoolchain/libelf/libelf_ehdr.c
200

I was thinking the intent would be more clear with the other formulation, but I suppose shnum == 0 && shoff != 0 is a complete condition for one case of extended numbering. OK.

This revision is now accepted and ready to land.Dec 6 2016, 2:55 AM
This revision was automatically updated to reflect the committed changes.