11#include <mach-o/loader.h>
12#include <mach-o/fat.h>
16using namespace Qt::StringLiterals;
22#if defined(Q_PROCESSOR_X86_64)
24static const cpu_type_t my_cputype = CPU_TYPE_X86_64;
25#elif defined(Q_PROCESSOR_X86_32)
26static const cpu_type_t my_cputype = CPU_TYPE_X86;
27#elif defined(Q_PROCESSOR_POWER_64)
29static const cpu_type_t my_cputype = CPU_TYPE_POWERPC64;
30#elif defined(Q_PROCESSOR_POWER_32)
31static const cpu_type_t my_cputype = CPU_TYPE_POWERPC;
32#elif defined(Q_PROCESSOR_ARM_64)
34static const cpu_type_t my_cputype = CPU_TYPE_ARM64;
35#elif defined(Q_PROCESSOR_ARM)
36static const cpu_type_t my_cputype = CPU_TYPE_ARM;
38# error "Unknown CPU type"
43typedef mach_header_64 my_mach_header;
44typedef segment_command_64 my_segment_command;
45typedef section_64 my_section;
46static const uint32_t my_magic = MH_MAGIC_64;
55static QLibraryScanResult notfound(
const QString &reason, QString *errorString)
57 *errorString = QLibrary::tr(
"'%1' is not a valid Mach-O binary (%2)")
58 .arg(*errorString, reason.isEmpty() ? QLibrary::tr(
"file is corrupt") : reason);
71 for (uint32_t i = 0; i < header->ncmds; ++i) {
73 if (Q_UNLIKELY(fdlen -
sizeof(load_command) < minsize))
76 load_command *loadCommand =
reinterpret_cast<load_command *>(commandCursor);
77 const uint32_t cmdsize = loadCommand->cmdsize;
80 if (Q_UNLIKELY(cmdsize <
sizeof(load_command)))
84 if (qAddOverflow(minsize, ulong(cmdsize), &minsize) || Q_UNLIKELY(minsize > fdlen))
87 if (loadCommand->cmd == LC_ENCRYPTION_INFO || loadCommand->cmd == LC_ENCRYPTION_INFO_64) {
91 if (Q_UNLIKELY(cmdsize <
sizeof(encryption_info_command)))
93 auto encryptionInfoCommand =
reinterpret_cast<encryption_info_command*>(loadCommand);
94 return encryptionInfoCommand->cryptid != 0;
97 commandCursor += cmdsize;
103QLibraryScanResult QMachOParser::parse(
const char *m_s, ulong fdlen, QString *errorString)
110 static const size_t MinFileSize =
sizeof(my_mach_header) +
sizeof(my_segment_command) +
sizeof(my_section);
111 static const size_t MinFatHeaderSize =
sizeof(fat_header) + 2 *
sizeof(fat_arch);
113 if (Q_UNLIKELY(fdlen < MinFileSize))
114 return notfound(QLibrary::tr(
"file too small"), errorString);
117 const my_mach_header *header =
nullptr;
118 const fat_header *fat =
reinterpret_cast<
const fat_header *>(m_s);
119 if (fat->magic == qToBigEndian(FAT_MAGIC)) {
121 const fat_arch *arch =
reinterpret_cast<
const fat_arch *>(fat + 1);
122 if (Q_UNLIKELY(fdlen < MinFatHeaderSize)) {
123 return notfound(QLibrary::tr(
"file too small"), errorString);
126 int count = qFromBigEndian(fat->nfat_arch);
127 if (Q_UNLIKELY(fdlen <
sizeof(*fat) +
sizeof(*arch) * count))
128 return notfound(QString(), errorString);
130 for (
int i = 0; i < count; ++i) {
131 if (arch[i].cputype == qToBigEndian(my_cputype)) {
133 uint32_t size = qFromBigEndian(arch[i].size);
134 uint32_t offset = qFromBigEndian(arch[i].offset);
135 if (Q_UNLIKELY(size > fdlen) || Q_UNLIKELY(offset > fdlen)
136 || Q_UNLIKELY(size + offset > fdlen) || Q_UNLIKELY(size < MinFileSize))
137 return notfound(QString(), errorString);
139 header =
reinterpret_cast<
const my_mach_header *>(m_s + offset);
145 return notfound(QLibrary::tr(
"no suitable architecture in fat binary"), errorString);
148 if (Q_UNLIKELY(header->magic != my_magic))
149 return notfound(QString(), errorString);
151 header =
reinterpret_cast<
const my_mach_header *>(m_s);
155 if (header->magic != my_magic)
156 return notfound(QLibrary::tr(
"invalid magic %1").arg(qFromBigEndian(header->magic),
165 if (header->cputype != my_cputype) {
167 return notfound(QString(), errorString);
168 return notfound(QLibrary::tr(
"wrong architecture"), errorString);
172 if (Q_UNLIKELY(header->filetype != MH_BUNDLE && header->filetype != MH_DYLIB))
173 return notfound(QLibrary::tr(
"not a dynamic library"), errorString);
176 const my_segment_command *seg =
reinterpret_cast<
const my_segment_command *>(header + 1);
177 ulong minsize =
sizeof(*header);
179 for (uint i = 0; i < header->ncmds; ++i,
180 seg =
reinterpret_cast<
const my_segment_command *>(
reinterpret_cast<
const char *>(seg) + seg->cmdsize)) {
183 if (Q_UNLIKELY(fdlen < minsize +
sizeof(load_command)))
184 return notfound(QString(), errorString);
189 minsize += seg->cmdsize;
190 if (Q_UNLIKELY(fdlen < minsize) || Q_UNLIKELY(fdlen < seg->cmdsize))
191 return notfound(QString(), errorString);
193 const uint32_t MyLoadCommand =
sizeof(
void *) > 4 ? LC_SEGMENT_64 : LC_SEGMENT;
194 if (seg->cmd != MyLoadCommand)
198 if (strcmp(seg->segname,
"__TEXT") == 0) {
199 const my_section *sect =
reinterpret_cast<
const my_section *>(seg + 1);
200 for (uint j = 0; j < seg->nsects; ++j) {
202 if (strcmp(sect[j].sectname,
"qtmetadata") != 0)
206 if (Q_UNLIKELY(fdlen < sect[j].offset) || Q_UNLIKELY(fdlen < sect[j].size)
207 || Q_UNLIKELY(fdlen < sect[j].offset + sect[j].size))
208 return notfound(QString(), errorString);
210 if (sect[j].size <
sizeof(QPluginMetaData::MagicHeader))
211 return notfound(QLibrary::tr(
".qtmetadata section is too small"), errorString);
213 const std::optional<
bool> binaryIsEncrypted = isEncrypted(header, fdlen);
214 if (Q_UNLIKELY(!binaryIsEncrypted))
215 return notfound(QLibrary::tr(
"corrupted encryption_info section"), errorString);
216 qsizetype pos =
reinterpret_cast<
const char *>(header) - m_s + sect[j].offset;
220 if (IncludeValidityChecks && !*binaryIsEncrypted) {
221 QByteArrayView expectedMagic = QByteArrayView::fromArray(QPluginMetaData::MagicString);
222 QByteArrayView actualMagic = QByteArrayView(m_s + pos, expectedMagic.size());
223 if (expectedMagic != actualMagic)
224 return notfound(QLibrary::tr(
".qtmetadata section has incorrect magic"), errorString);
227 pos +=
sizeof(QPluginMetaData::MagicString);
228 return { pos, qsizetype(sect[j].size -
sizeof(QPluginMetaData::MagicString)), *binaryIsEncrypted };
233 seg =
reinterpret_cast<
const my_segment_command *>(
reinterpret_cast<
const char *>(seg) + seg->cmdsize);
237 *errorString = QLibrary::tr(
"'%1' is not a Qt plugin").arg(*errorString);
mach_header my_mach_header
static const uint32_t my_magic
segment_command my_segment_command
static constexpr bool IncludeValidityChecks
static std::optional< bool > isEncrypted(const my_mach_header *header, ulong fdlen)