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