Skip to content

Commit 5cd3db3

Browse files
cabbakenRuoyu Qiujh7370
authored
[llvm][ELF]Add Shdr check for getBuildID (#126537)
Add Section Header check for getBuildID, fix crash with invalid Program Header. Fixes: #126418 --------- Signed-off-by: Ruoyu Qiu <cabbaken@outlook.com> Signed-off-by: Ruoyu Qiu <qiuruoyu@xiaomi.com> Co-authored-by: Ruoyu Qiu <qiuruoyu@xiaomi.com> Co-authored-by: James Henderson <James.Henderson@sony.com>
1 parent ff4aec5 commit 5cd3db3

File tree

4 files changed

+142
-6
lines changed

4 files changed

+142
-6
lines changed

llvm/lib/Object/BuildID.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ using namespace llvm::object;
2424
namespace {
2525

2626
template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
27+
auto findBuildID = [&Obj](const auto &ShdrOrPhdr,
28+
uint64_t Alignment) -> std::optional<BuildIDRef> {
29+
Error Err = Error::success();
30+
for (auto N : Obj.notes(ShdrOrPhdr, Err))
31+
if (N.getType() == ELF::NT_GNU_BUILD_ID &&
32+
N.getName() == ELF::ELF_NOTE_GNU)
33+
return N.getDesc(Alignment);
34+
consumeError(std::move(Err));
35+
return std::nullopt;
36+
};
37+
38+
auto Sections = cantFail(Obj.sections());
39+
for (const auto &S : Sections) {
40+
if (S.sh_type != ELF::SHT_NOTE)
41+
continue;
42+
if (std::optional<BuildIDRef> ShdrRes = findBuildID(S, S.sh_addralign))
43+
return ShdrRes.value();
44+
}
2745
auto PhdrsOrErr = Obj.program_headers();
2846
if (!PhdrsOrErr) {
2947
consumeError(PhdrsOrErr.takeError());
@@ -32,12 +50,8 @@ template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
3250
for (const auto &P : *PhdrsOrErr) {
3351
if (P.p_type != ELF::PT_NOTE)
3452
continue;
35-
Error Err = Error::success();
36-
for (auto N : Obj.notes(P, Err))
37-
if (N.getType() == ELF::NT_GNU_BUILD_ID &&
38-
N.getName() == ELF::ELF_NOTE_GNU)
39-
return N.getDesc(P.p_align);
40-
consumeError(std::move(Err));
53+
if (std::optional<BuildIDRef> PhdrRes = findBuildID(P, P.p_align))
54+
return PhdrRes.value();
4155
}
4256
return {};
4357
}

llvm/test/DebugInfo/symbolize-build-id.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Sections:
2121
Type: SHT_NOTE
2222
Flags: [ SHF_ALLOC ]
2323
Content: 040000000800000003000000474e5500abb50d82b6bdc861
24+
AddressAlign: 4
2425
ProgramHeaders:
2526
- Type: PT_NOTE
2627
Flags: [ PF_R ]
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//===- BuildIDTest.cpp - Tests for getBuildID ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Object/BuildID.h"
10+
#include "llvm/ADT/SmallString.h"
11+
#include "llvm/ADT/StringRef.h"
12+
#include "llvm/Object/ELFObjectFile.h"
13+
#include "llvm/ObjectYAML/yaml2obj.h"
14+
#include "llvm/Support/YAMLTraits.h"
15+
#include "llvm/Testing/Support/Error.h"
16+
17+
#include "gtest/gtest.h"
18+
19+
using namespace llvm;
20+
using namespace llvm::object;
21+
22+
template <class ELFT>
23+
static Expected<ELFObjectFile<ELFT>> toBinary(SmallVectorImpl<char> &Storage,
24+
StringRef Yaml) {
25+
raw_svector_ostream OS(Storage);
26+
yaml::Input YIn(Yaml);
27+
if (!yaml::convertYAML(YIn, OS, [](const Twine &Msg) {}))
28+
return createStringError(std::errc::invalid_argument,
29+
"unable to convert YAML");
30+
return ELFObjectFile<ELFT>::create(MemoryBufferRef(OS.str(), "dummyELF"));
31+
}
32+
33+
static StringRef getInvalidNoteELF(bool WithShdr) {
34+
static std::string WithSection(R"(
35+
--- !ELF
36+
FileHeader:
37+
Class: ELFCLASS64
38+
Data: ELFDATA2LSB
39+
Type: ET_EXEC
40+
Machine: EM_X86_64
41+
ProgramHeaders:
42+
- Type: PT_NOTE
43+
FileSize: 0x1a
44+
FirstSec: .note.gnu.build-id
45+
LastSec: .note.gnu.build-id
46+
Sections:
47+
- Name: .note.gnu.build-id
48+
Type: SHT_NOTE
49+
AddressAlign: 0x04
50+
Notes:
51+
- Name: "GNU"
52+
Desc: "abb50d82b6bdc861"
53+
Type: 3
54+
)");
55+
static std::string WithoutSection(WithSection + R"(
56+
- Type: SectionHeaderTable
57+
NoHeaders: true
58+
)");
59+
if (WithShdr)
60+
return WithSection;
61+
return WithoutSection;
62+
}
63+
64+
// The BuildID can be looked up from a section header, if there is no program
65+
// header.
66+
TEST(BuildIDTest, InvalidPhdrFileSizeWithShdrs) {
67+
SmallString<0> Storage;
68+
Expected<ELFObjectFile<ELF64LE>> ElfOrErr =
69+
toBinary<ELF64LE>(Storage, getInvalidNoteELF(true));
70+
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
71+
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
72+
EXPECT_EQ(
73+
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
74+
"\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
75+
}
76+
77+
// The code handles a malformed program header that points at data outside the
78+
// file.
79+
TEST(BuildIDTest, InvalidPhdrFileSizeNoShdrs) {
80+
SmallString<0> Storage;
81+
Expected<ELFObjectFile<ELF64LE>> ElfOrErr =
82+
toBinary<ELF64LE>(Storage, getInvalidNoteELF(false));
83+
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
84+
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
85+
EXPECT_EQ(
86+
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
87+
"");
88+
}
89+
90+
// The code handles a malformed section header that points at data outside the
91+
// file.
92+
TEST(BuildIDTest, InvalidSectionHeader) {
93+
SmallString<0> Storage;
94+
Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"(
95+
--- !ELF
96+
FileHeader:
97+
Class: ELFCLASS64
98+
Data: ELFDATA2LSB
99+
Type: ET_EXEC
100+
Machine: EM_X86_64
101+
ProgramHeaders:
102+
- Type: PT_NOTE
103+
FirstSec: .note.gnu.build-id
104+
LastSec: .note.gnu.build-id
105+
Sections:
106+
- Name: .note.gnu.build-id
107+
Type: SHT_NOTE
108+
AddressAlign: 0x04
109+
ShOffset: 0x1a1
110+
Notes:
111+
- Name: "GNU"
112+
Desc: "abb50d82b6bdc861"
113+
Type: 3
114+
)");
115+
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
116+
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
117+
EXPECT_EQ(
118+
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
119+
"\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
120+
}

llvm/unittests/Object/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
77

88
add_llvm_unittest(ObjectTests
99
ArchiveTest.cpp
10+
BuildIDTest.cpp
1011
COFFObjectFileTest.cpp
1112
DXContainerTest.cpp
1213
ELFObjectFileTest.cpp

0 commit comments

Comments
 (0)