Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmachparser.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
6
7#include <qendian.h>
8
9#include <mach-o/loader.h>
10#include <mach-o/fat.h>
11
13
14using namespace Qt::StringLiterals;
15
16// Whether we include some extra validity checks
17// (checks to ensure we don't read out-of-bounds are always included)
18static constexpr bool IncludeValidityChecks = true;
19
20#if defined(Q_PROCESSOR_X86_64)
21# define MACHO64
22static const cpu_type_t my_cputype = CPU_TYPE_X86_64;
23#elif defined(Q_PROCESSOR_X86_32)
24static const cpu_type_t my_cputype = CPU_TYPE_X86;
25#elif defined(Q_PROCESSOR_POWER_64)
26# define MACHO64
27static const cpu_type_t my_cputype = CPU_TYPE_POWERPC64;
28#elif defined(Q_PROCESSOR_POWER_32)
29static const cpu_type_t my_cputype = CPU_TYPE_POWERPC;
30#elif defined(Q_PROCESSOR_ARM_64)
31# define MACHO64
32static const cpu_type_t my_cputype = CPU_TYPE_ARM64;
33#elif defined(Q_PROCESSOR_ARM)
34static const cpu_type_t my_cputype = CPU_TYPE_ARM;
35#else
36# error "Unknown CPU type"
37#endif
38
39#ifdef MACHO64
40# undef MACHO64
41typedef mach_header_64 my_mach_header;
42typedef segment_command_64 my_segment_command;
43typedef section_64 my_section;
44static const uint32_t my_magic = MH_MAGIC_64;
45#else
49static const uint32_t my_magic = MH_MAGIC;
50#endif
51
52Q_DECL_COLD_FUNCTION
53static QLibraryScanResult notfound(const QString &reason, QString *errorString)
54{
55 *errorString = QLibrary::tr("'%1' is not a valid Mach-O binary (%2)")
56 .arg(*errorString, reason.isEmpty() ? QLibrary::tr("file is corrupt") : reason);
57 return {};
58}
59
60static bool isEncrypted(const my_mach_header *header)
61{
62 auto commandCursor = uintptr_t(header) + sizeof(my_mach_header);
63 for (uint32_t i = 0; i < header->ncmds; ++i) {
64 load_command *loadCommand = reinterpret_cast<load_command *>(commandCursor);
65 if (loadCommand->cmd == LC_ENCRYPTION_INFO || loadCommand->cmd == LC_ENCRYPTION_INFO_64) {
66 // The layout of encryption_info_command and encryption_info_command_64 is the same
67 // up until and including cryptid, so we can treat it as encryption_info_command.
68 auto encryptionInfoCommand = reinterpret_cast<encryption_info_command*>(loadCommand);
69 return encryptionInfoCommand->cryptid != 0;
70 }
71 commandCursor += loadCommand->cmdsize;
72 }
73
74 return false;
75}
76
77QLibraryScanResult QMachOParser::parse(const char *m_s, ulong fdlen, QString *errorString)
78{
79 // The minimum size of a Mach-O binary we're interested in.
80 // It must have a full Mach header, at least one segment and at least one
81 // section. It's probably useless with just the "qtmetadata" section, but
82 // it's valid nonetheless.
83 // A fat binary must have this plus the fat header, of course.
84 static const size_t MinFileSize = sizeof(my_mach_header) + sizeof(my_segment_command) + sizeof(my_section);
85 static const size_t MinFatHeaderSize = sizeof(fat_header) + 2 * sizeof(fat_arch);
86
87 if (Q_UNLIKELY(fdlen < MinFileSize))
88 return notfound(QLibrary::tr("file too small"), errorString);
89
90 // find out if this is a fat Mach-O binary first
91 const my_mach_header *header = nullptr;
92 const fat_header *fat = reinterpret_cast<const fat_header *>(m_s);
93 if (fat->magic == qToBigEndian(FAT_MAGIC)) {
94 // find our architecture in the binary
95 const fat_arch *arch = reinterpret_cast<const fat_arch *>(fat + 1);
96 if (Q_UNLIKELY(fdlen < MinFatHeaderSize)) {
97 return notfound(QLibrary::tr("file too small"), errorString);
98 }
99
100 int count = qFromBigEndian(fat->nfat_arch);
101 if (Q_UNLIKELY(fdlen < sizeof(*fat) + sizeof(*arch) * count))
102 return notfound(QString(), errorString);
103
104 for (int i = 0; i < count; ++i) {
105 if (arch[i].cputype == qToBigEndian(my_cputype)) {
106 // ### should we check the CPU subtype? Maybe on ARM?
107 uint32_t size = qFromBigEndian(arch[i].size);
108 uint32_t offset = qFromBigEndian(arch[i].offset);
109 if (Q_UNLIKELY(size > fdlen) || Q_UNLIKELY(offset > fdlen)
110 || Q_UNLIKELY(size + offset > fdlen) || Q_UNLIKELY(size < MinFileSize))
111 return notfound(QString(), errorString);
112
113 header = reinterpret_cast<const my_mach_header *>(m_s + offset);
114 fdlen = size;
115 break;
116 }
117 }
118 if (!header)
119 return notfound(QLibrary::tr("no suitable architecture in fat binary"), errorString);
120
121 // check the magic again
122 if (Q_UNLIKELY(header->magic != my_magic))
123 return notfound(QString(), errorString);
124 } else {
125 header = reinterpret_cast<const my_mach_header *>(m_s);
126 fat = 0;
127
128 // check magic
129 if (header->magic != my_magic)
130 return notfound(QLibrary::tr("invalid magic %1").arg(qFromBigEndian(header->magic),
131 8, 16, '0'_L1),
132 errorString);
133 }
134
135 // from this point on, everything is in host byte order
136
137 // (re-)check the CPU type
138 // ### should we check the CPU subtype? Maybe on ARM?
139 if (header->cputype != my_cputype) {
140 if (fat)
141 return notfound(QString(), errorString);
142 return notfound(QLibrary::tr("wrong architecture"), errorString);
143 }
144
145 // check the file type
146 if (Q_UNLIKELY(header->filetype != MH_BUNDLE && header->filetype != MH_DYLIB))
147 return notfound(QLibrary::tr("not a dynamic library"), errorString);
148
149 // find the __TEXT segment, "qtmetadata" section
150 const my_segment_command *seg = reinterpret_cast<const my_segment_command *>(header + 1);
151 ulong minsize = sizeof(*header);
152
153 for (uint i = 0; i < header->ncmds; ++i,
154 seg = reinterpret_cast<const my_segment_command *>(reinterpret_cast<const char *>(seg) + seg->cmdsize)) {
155 // We're sure that the file size includes at least one load command
156 // but we have to check anyway if we're past the first
157 if (Q_UNLIKELY(fdlen < minsize + sizeof(load_command)))
158 return notfound(QString(), errorString);
159
160 // cmdsize can't be trusted until validated
161 // so check it against fdlen anyway
162 // (these are unsigned operations, with overflow behavior specified in the standard)
163 minsize += seg->cmdsize;
164 if (Q_UNLIKELY(fdlen < minsize) || Q_UNLIKELY(fdlen < seg->cmdsize))
165 return notfound(QString(), errorString);
166
167 const uint32_t MyLoadCommand = sizeof(void *) > 4 ? LC_SEGMENT_64 : LC_SEGMENT;
168 if (seg->cmd != MyLoadCommand)
169 continue;
170
171 // is this the __TEXT segment?
172 if (strcmp(seg->segname, "__TEXT") == 0) {
173 const my_section *sect = reinterpret_cast<const my_section *>(seg + 1);
174 for (uint j = 0; j < seg->nsects; ++j) {
175 // is this the "qtmetadata" section?
176 if (strcmp(sect[j].sectname, "qtmetadata") != 0)
177 continue;
178
179 // found it!
180 if (Q_UNLIKELY(fdlen < sect[j].offset) || Q_UNLIKELY(fdlen < sect[j].size)
181 || Q_UNLIKELY(fdlen < sect[j].offset + sect[j].size))
182 return notfound(QString(), errorString);
183
184 if (sect[j].size < sizeof(QPluginMetaData::MagicHeader))
185 return notfound(QLibrary::tr(".qtmetadata section is too small"), errorString);
186
187 const bool binaryIsEncrypted = isEncrypted(header);
188 qsizetype pos = reinterpret_cast<const char *>(header) - m_s + sect[j].offset;
189
190 // We can not read the section data of encrypted libraries until they
191 // have been dlopened(), so skip validity check if that's the case.
192 if (IncludeValidityChecks && !binaryIsEncrypted) {
193 QByteArrayView expectedMagic = QByteArrayView::fromArray(QPluginMetaData::MagicString);
194 QByteArrayView actualMagic = QByteArrayView(m_s + pos, expectedMagic.size());
195 if (expectedMagic != actualMagic)
196 return notfound(QLibrary::tr(".qtmetadata section has incorrect magic"), errorString);
197 }
198
199 pos += sizeof(QPluginMetaData::MagicString);
200 return { pos, qsizetype(sect[j].size - sizeof(QPluginMetaData::MagicString)), binaryIsEncrypted };
201 }
202 }
203
204 // other type of segment
205 seg = reinterpret_cast<const my_segment_command *>(reinterpret_cast<const char *>(seg) + seg->cmdsize);
206 }
207
208 // No .qtmetadata section was found
209 *errorString = QLibrary::tr("'%1' is not a Qt plugin").arg(*errorString);
210 return {};
211}
212
213QT_END_NAMESPACE
Combined button and popup list for selecting options.
mach_header my_mach_header
static bool isEncrypted(const my_mach_header *header)
static const uint32_t my_magic
segment_command my_segment_command
static constexpr bool IncludeValidityChecks
section my_section