-
Couldn't load subscription status.
- Fork 15k
[ELF]Add overflow check to ELF note iterator #160451
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
Changes from 4 commits
4e91e50
a630026
93dbf91
3a3b4ea
00ff33a
646ed0b
55dd452
c4c91cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ | |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/Object/ELF.h" | ||
| #include "llvm/Object/ELFObjectFile.h" | ||
| #include "llvm/ObjectYAML/yaml2obj.h" | ||
| #include "llvm/Support/Error.h" | ||
| #include "llvm/Support/YAMLTraits.h" | ||
| #include "llvm/Testing/Support/Error.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
|
|
@@ -310,3 +314,69 @@ TEST(ELFTest, Hash) { | |
| // presuming 32-bit long. Thus make sure that extra bit doesn't appear. | ||
| EXPECT_EQ(hashSysV("ZZZZZW9p"), 0U); | ||
| } | ||
|
|
||
| template <class ELFT> | ||
| static Expected<ELFObjectFile<ELFT>> toBinary(SmallVectorImpl<char> &Storage, | ||
| StringRef Yaml) { | ||
| raw_svector_ostream OS(Storage); | ||
| yaml::Input YIn(Yaml); | ||
| if (!yaml::convertYAML(YIn, OS, [](const Twine &Msg) {})) | ||
| return createStringError(std::errc::invalid_argument, | ||
| "unable to convert YAML"); | ||
| return ELFObjectFile<ELFT>::create(MemoryBufferRef(OS.str(), "dummyELF")); | ||
| } | ||
|
|
||
| TEST(ELFObjectFileTest, ELFNoteIteratorOverflow) { | ||
| SmallString<0> Storage; | ||
| Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"( | ||
| --- !ELF | ||
| FileHeader: | ||
| Class: ELFCLASS64 | ||
| Data: ELFDATA2LSB | ||
| Type: ET_EXEC | ||
| Machine: EM_X86_64 | ||
| ProgramHeaders: | ||
| - Type: PT_NOTE | ||
| FileSize: 0xffffffffffffff88 | ||
| FirstSec: .note.gnu.build-id | ||
| LastSec: .note.gnu.build-id | ||
|
|
||
| Sections: | ||
| - Name: .note.gnu.build-id | ||
| Type: SHT_NOTE | ||
| AddressAlign: 0x04 | ||
| ShOffset: 0xffffffffffffff88 | ||
| Notes: | ||
| - Name: "GNU" | ||
| Desc: "abb50d82b6bdc861" | ||
| Type: 3 | ||
| )"); | ||
| ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded()); | ||
| ELFFile<ELF64LE> Obj = ElfOrErr.get().getELFFile(); | ||
|
|
||
| auto CheckOverflow = [&](auto &&PhdrOrShdr, uint64_t Offset, uint64_t Size) { | ||
| Error Err = Error::success(); | ||
| Obj.notes(PhdrOrShdr, Err); | ||
|
|
||
| std::string ErrMessage; | ||
| handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) { | ||
| ErrMessage = EI.message(); | ||
| }); | ||
|
|
||
| EXPECT_EQ(ErrMessage, ("invalid offset (0x" + Twine::utohexstr(Offset) + | ||
| ") or size (0x" + Twine::utohexstr(Size) + ")") | ||
| .str()); | ||
| }; | ||
|
|
||
| auto PhdrsOrErr = Obj.program_headers(); | ||
|
||
| EXPECT_FALSE(!PhdrsOrErr); | ||
| for (auto P : *PhdrsOrErr) | ||
|
||
| if (P.p_type == ELF::PT_NOTE) | ||
| CheckOverflow(P, P.p_offset, P.p_filesz); | ||
|
|
||
| auto ShdrsOrErr = Obj.sections(); | ||
| EXPECT_FALSE(!ShdrsOrErr); | ||
| for (auto S : *ShdrsOrErr) | ||
| if (S.sh_type == ELF::SHT_NOTE) | ||
| CheckOverflow(S, S.sh_offset, S.sh_size); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: delete this blank line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed