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

"ELF Number of sections >= SHN_LORESERVE (0xff00)" is not implemented in library #89

Closed
mocakturk opened this issue Nov 10, 2022 · 5 comments

Comments

@mocakturk
Copy link

Hello,

Thx for this library.
I noticed an exception when reading an ELF file:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
at ELFSharp.ELF.ELF1.ReadSectionHeader(Int32 index) at ELFSharp.ELF.ELF1.ReadStringTable()
at ELFSharp.ELF.ELF`1..ctor(String fileName)
at ELFSharp.ELF.ELFReader.TryLoad(String fileName, IELF& elf)
at ELFSharp.ELF.ELFReader.Load(String fileName)

e_shnum (number of entries in the section header table) is 0. Then, library tries to read section index = 1... That causes exception.

After some googling, I found below:
If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), this member has the value zero and the actual number of section header table entries is contained in the sh_size field of the section header at index 0. (Otherwise, the sh_size member of the initial entry contains 0.)

I believe, if the above description is implemented, the problem will be solved.

Thx,
Murat

@mocakturk
Copy link
Author

I changed the code myself. Issue solved.
Here my modifications:

  • I changed type of "sectionHeaderEntryCount" to "uint".
  • Then I added below check after reading "sectionHeaderEntryCount".
sectionHeaderEntryCount = reader.ReadUInt16();
            // If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), this member has the
            // value zero and the actual number of section header table entries is contained in the sh_size field
            // of the section header at index 0. (Otherwise, the sh_size member of the initial entry contains 0.)
            if (sectionHeaderEntryCount == 0)
            {
                var tempPosition = reader.BaseStream.Position;
                reader.BaseStream.Seek(
                    sectionHeaderOffset + 0 * sectionHeaderEntrySize + (Class==Class.Bit32?0x14:0x20),
                    SeekOrigin.Begin
                );
                var sh_size = Class == Class.Bit32 ? reader.ReadUInt32() : reader.ReadUInt64();
                sectionHeaderEntryCount = (uint) sh_size;
                reader.BaseStream.Seek(tempPosition, SeekOrigin.Begin);
            }```

@konrad-kruczynski
Copy link
Owner

Thanks for the input, I'll try to implement your solution. Would it be possible for you to provide a test binary?

@mocakturk
Copy link
Author

mocakturk commented Nov 14, 2022

Hi Konrad,

Thanks for the update. However, this solution does not work. Because you inserted (sectionHeaderEntryCount == 0) case into ReadSections method. However, exception occurs inside "ReadStringTable" method. (Reading string table also reads a section)

When I put "(sectionHeaderEntryCount == 0)" case to the end of "ReadFields" method, it works.
Just move lines 231-238 into the end of "ReadFields" method.

Sorry, but I cannot provide you the binary. First, it is very big (0.5GB). Second, it is a software of my company.
Just let me know for testing...

Thanks for the effort...

@konrad-kruczynski
Copy link
Owner

Right. Please check version 2.15.2.

@mocakturk
Copy link
Author

Konrad, v2.15.2 works good. Thank you.

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

No branches or pull requests

2 participants