Skip to content

Commit 0223ab0

Browse files
committed
[lld-macho] check minimum header length when opening linkable input files
Bifurcate the `readFile()` API into ... * `readRawFile()` which performs no checks, and * `readLinkableFile()` which enforces minimum length of 20 bytes, same as ld64 There are no new tests because tweaks to existing tests are sufficient. Differential Revision: https://reviews.llvm.org/D97610
1 parent e0b1df9 commit 0223ab0

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

lld/MachO/Driver.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static std::vector<ArchiveMember> getArchiveMembers(MemoryBufferRef mb) {
263263

264264
static InputFile *addFile(StringRef path, bool forceLoadArchive,
265265
bool isBundleLoader = false) {
266-
Optional<MemoryBufferRef> buffer = readFile(path);
266+
Optional<MemoryBufferRef> buffer = readLinkableFile(path);
267267
if (!buffer)
268268
return nullptr;
269269
MemoryBufferRef mbref = *buffer;
@@ -279,7 +279,7 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
279279
error(path + ": archive has no index; run ranlib to add one");
280280

281281
if (config->allLoad || forceLoadArchive) {
282-
if (Optional<MemoryBufferRef> buffer = readFile(path)) {
282+
if (Optional<MemoryBufferRef> buffer = readLinkableFile(path)) {
283283
for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
284284
if (Optional<InputFile *> file = loadArchiveMember(
285285
member.mbref, member.modTime, path, /*objCOnly=*/false)) {
@@ -300,7 +300,7 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
300300
// we already found that it contains an ObjC symbol. We should also
301301
// consider creating a LazyObjFile class in order to avoid double-loading
302302
// these files here and below (as part of the ArchiveFile).
303-
if (Optional<MemoryBufferRef> buffer = readFile(path)) {
303+
if (Optional<MemoryBufferRef> buffer = readLinkableFile(path)) {
304304
for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
305305
if (Optional<InputFile *> file = loadArchiveMember(
306306
member.mbref, member.modTime, path, /*objCOnly=*/true)) {
@@ -403,7 +403,7 @@ void macho::parseLCLinkerOption(InputFile* f, unsigned argc, StringRef data) {
403403
}
404404

405405
static void addFileList(StringRef path) {
406-
Optional<MemoryBufferRef> buffer = readFile(path);
406+
Optional<MemoryBufferRef> buffer = readRawFile(path);
407407
if (!buffer)
408408
return;
409409
MemoryBufferRef mbref = *buffer;
@@ -426,7 +426,7 @@ static void addFileList(StringRef path) {
426426
//
427427
// The file can also have line comments that start with '#'.
428428
static void parseOrderFile(StringRef path) {
429-
Optional<MemoryBufferRef> buffer = readFile(path);
429+
Optional<MemoryBufferRef> buffer = readRawFile(path);
430430
if (!buffer) {
431431
error("Could not read order file at " + path);
432432
return;
@@ -945,7 +945,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
945945
StringRef segName = arg->getValue(0);
946946
StringRef sectName = arg->getValue(1);
947947
StringRef fileName = arg->getValue(2);
948-
Optional<MemoryBufferRef> buffer = readFile(fileName);
948+
Optional<MemoryBufferRef> buffer = readRawFile(fileName);
949949
if (buffer)
950950
inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
951951
}

lld/MachO/DriverUtils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ std::string macho::createResponseFile(const opt::InputArgList &args) {
132132
os << "-o " << quote(path::filename(arg->getValue())) << "\n";
133133
break;
134134
case OPT_filelist:
135-
if (Optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
135+
if (Optional<MemoryBufferRef> buffer = readRawFile(arg->getValue()))
136136
for (StringRef path : args::getLines(*buffer))
137137
os << quote(rewritePath(path)) << "\n";
138138
break;

lld/MachO/InputFiles.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ std::unique_ptr<TarWriter> macho::tar;
9191
int InputFile::idCount = 0;
9292

9393
// Open a given file path and return it as a memory-mapped file.
94-
Optional<MemoryBufferRef> macho::readFile(StringRef path) {
94+
// Perform no sanity checks--just open, map & return.
95+
Optional<MemoryBufferRef> macho::readRawFile(StringRef path) {
9596
// Open a file.
9697
auto mbOrErr = MemoryBuffer::getFile(path);
9798
if (auto ec = mbOrErr.getError()) {
@@ -102,6 +103,27 @@ Optional<MemoryBufferRef> macho::readFile(StringRef path) {
102103
std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
103104
MemoryBufferRef mbref = mb->getMemBufferRef();
104105
make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
106+
return mbref;
107+
}
108+
109+
// Open a given file path and return it as a memory-mapped file.
110+
// Assume the file has one of a variety of linkable formats and
111+
// perform some basic sanity checks, notably minimum length.
112+
Optional<MemoryBufferRef> macho::readLinkableFile(StringRef path) {
113+
Optional<MemoryBufferRef> maybeMbref = readRawFile(path);
114+
if (!maybeMbref) {
115+
return None;
116+
}
117+
MemoryBufferRef mbref = *maybeMbref;
118+
119+
// LD64 hard-codes 20 as minimum header size, which is presumably
120+
// the smallest header among the the various linkable input formats
121+
// LLD are less demanding. We insist on having only enough data for
122+
// a magic number.
123+
if (mbref.getBufferSize() < sizeof(uint32_t)) {
124+
error("file is too small to contain a magic number: " + path);
125+
return None;
126+
}
105127

106128
// If this is a regular non-fat file, return it.
107129
const char *buf = mbref.getBufferStart();
@@ -544,7 +566,7 @@ void ObjFile::parseDebugInfo() {
544566

545567
// The path can point to either a dylib or a .tbd file.
546568
static Optional<DylibFile *> loadDylib(StringRef path, DylibFile *umbrella) {
547-
Optional<MemoryBufferRef> mbref = readFile(path);
569+
Optional<MemoryBufferRef> mbref = readLinkableFile(path);
548570
if (!mbref) {
549571
error("could not read dylib file at " + path);
550572
return {};

lld/MachO/InputFiles.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ class BitcodeFile : public InputFile {
173173

174174
extern llvm::SetVector<InputFile *> inputFiles;
175175

176-
llvm::Optional<MemoryBufferRef> readFile(StringRef path);
176+
llvm::Optional<MemoryBufferRef> readRawFile(StringRef path);
177+
llvm::Optional<MemoryBufferRef> readLinkableFile(StringRef path);
177178

178179
const llvm::MachO::load_command *
179180
findCommand(const llvm::MachO::mach_header_64 *, uint32_t type);

lld/test/MachO/invalid/tiny-input.s

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# REQUIRES: x86
2+
3+
## Check that files too short to have a magic number are rejected as inputs
4+
# RUN: echo -n 1 >%t-1.o
5+
# RUN: echo -n 12 >%t-2.o
6+
# RUN: echo -n 123 >%t-3.o
7+
# RUN: echo -n 1234 >%t-4.o
8+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
9+
# RUN: not %lld -o %t %t.o %t-1.o %t-2.o %t-3.o %t-4.o 2>&1 | FileCheck %s
10+
11+
# CHECK: error: file is too small to contain a magic number: {{.*}}-1.o
12+
# CHECK: error: file is too small to contain a magic number: {{.*}}-2.o
13+
# CHECK: error: file is too small to contain a magic number: {{.*}}-3.o
14+
# CHECK: error: {{.*}}-4.o: unhandled file type
15+
16+
.global _main
17+
_main:
18+
ret

lld/test/MachO/rename.s

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# BAD1-DAG: error: invalid name for segment or section: S/ASHY_SEG
1515
# BAD1-DAG: error: invalid name for segment or section: st*rry_sect
1616
# BAD1-DAG: error: invalid name for segment or section: -o
17-
# BAD1-DAG: error: /dev/null: unhandled file type
17+
# BAD1-DAG: error: file is too small to contain a magic number:
1818

1919
# RUN: not %lld \
2020
# RUN: -rename_segment H#SHY_SEG PL+SSY_SEG \
@@ -24,7 +24,7 @@
2424
# BAD2-DAG: error: invalid name for segment or section: H#SHY_SEG
2525
# BAD2-DAG: error: invalid name for segment or section: PL+SSY_SEG
2626
# BAD2-DAG: error: invalid name for segment or section: -o
27-
# BAD2-DAG: error: /dev/null: unhandled file type
27+
# BAD2-DAG: error: file is too small to contain a magic number:
2828

2929
## Check that section and segment renames happen
3030
# RUN: %lld \

0 commit comments

Comments
 (0)