1 | /* $Id: mach_kernel-r0drv-darwin.cpp 37561 2011-06-20 16:22:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - mach_kernel symbol resolving hack, R0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #ifdef IN_RING0
|
---|
32 | # include "the-darwin-kernel.h"
|
---|
33 | #endif
|
---|
34 | #include "../../include/internal/iprt.h"
|
---|
35 |
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/file.h>
|
---|
41 | #include <iprt/log.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <iprt/string.h>
|
---|
44 | #include "../../include/internal/ldrMach-O.h"
|
---|
45 |
|
---|
46 | /** @def MY_CPU_TYPE
|
---|
47 | * The CPU type targeted by the compiler. */
|
---|
48 | /** @def MY_CPU_TYPE
|
---|
49 | * The "ALL" CPU subtype targeted by the compiler. */
|
---|
50 | /** @def MY_MACHO_HEADER
|
---|
51 | * The Mach-O header targeted by the compiler. */
|
---|
52 | /** @def MY_MACHO_MAGIC
|
---|
53 | * The Mach-O header magic we're targeting. */
|
---|
54 | /** @def MY_SEGMENT_COMMAND
|
---|
55 | * The segment command targeted by the compiler. */
|
---|
56 | /** @def MY_SECTION
|
---|
57 | * The section struture targeted by the compiler. */
|
---|
58 | /** @def MY_NLIST
|
---|
59 | * The symbol table entry targeted by the compiler. */
|
---|
60 | #ifdef RT_ARCH_X86
|
---|
61 | # define MY_CPU_TYPE CPU_TYPE_I386
|
---|
62 | # define MY_CPU_SUBTYPE_ALL CPU_SUBTYPE_I386_ALL
|
---|
63 | # define MY_MACHO_HEADER mach_header_32_t
|
---|
64 | # define MY_MACHO_MAGIC IMAGE_MACHO32_SIGNATURE
|
---|
65 | # define MY_SEGMENT_COMMAND segment_command_32_t
|
---|
66 | # define MY_SECTION section_32_t
|
---|
67 | # define MY_NLIST macho_nlist_32_t
|
---|
68 |
|
---|
69 | #elif defined(RT_ARCH_AMD64)
|
---|
70 | # define MY_CPU_TYPE CPU_TYPE_X86_64
|
---|
71 | # define MY_CPU_SUBTYPE_ALL CPU_SUBTYPE_X86_64_ALL
|
---|
72 | # define MY_MACHO_HEADER mach_header_64_t
|
---|
73 | # define MY_MACHO_MAGIC IMAGE_MACHO64_SIGNATURE
|
---|
74 | # define MY_SEGMENT_COMMAND segment_command_64_t
|
---|
75 | # define MY_SECTION section_64_t
|
---|
76 | # define MY_NLIST macho_nlist_64_t
|
---|
77 |
|
---|
78 | #else
|
---|
79 | # error "Port me!"
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | /** @name Return macros for make it simpler to track down too paranoid code.
|
---|
83 | * @{
|
---|
84 | */
|
---|
85 | #ifdef DEBUG
|
---|
86 | # define RETURN_VERR_BAD_EXE_FORMAT \
|
---|
87 | do { Assert(!g_fBreakpointOnError); return VERR_BAD_EXE_FORMAT; } while (0)
|
---|
88 | # define RETURN_VERR_LDR_UNEXPECTED \
|
---|
89 | do { Assert(!g_fBreakpointOnError); return VERR_LDR_UNEXPECTED; } while (0)
|
---|
90 | # define RETURN_VERR_LDR_ARCH_MISMATCH \
|
---|
91 | do { Assert(!g_fBreakpointOnError); return VERR_LDR_ARCH_MISMATCH; } while (0)
|
---|
92 | #else
|
---|
93 | # define RETURN_VERR_BAD_EXE_FORMAT do { return VERR_BAD_EXE_FORMAT; } while (0)
|
---|
94 | # define RETURN_VERR_LDR_UNEXPECTED do { return VERR_LDR_UNEXPECTED; } while (0)
|
---|
95 | # define RETURN_VERR_LDR_ARCH_MISMATCH do { return VERR_LDR_ARCH_MISMATCH; } while (0)
|
---|
96 | #endif
|
---|
97 | /** @} */
|
---|
98 |
|
---|
99 | #define VERR_LDR_UNEXPECTED (-641)
|
---|
100 |
|
---|
101 |
|
---|
102 | /*******************************************************************************
|
---|
103 | * Structures and Typedefs *
|
---|
104 | *******************************************************************************/
|
---|
105 | /**
|
---|
106 | * Our internal representation of the mach_kernel after loading it's symbols
|
---|
107 | * and successfully resolving their addresses.
|
---|
108 | */
|
---|
109 | typedef struct RTR0DARWINKERNEL
|
---|
110 | {
|
---|
111 | /** @name Result.
|
---|
112 | * @{ */
|
---|
113 | /** Pointer to the string table. */
|
---|
114 | char *pachStrTab;
|
---|
115 | /** The size of the string table. */
|
---|
116 | uint32_t cbStrTab;
|
---|
117 | /** The file offset of the string table. */
|
---|
118 | uint32_t offStrTab;
|
---|
119 | /** Pointer to the symbol table. */
|
---|
120 | MY_NLIST *paSyms;
|
---|
121 | /** The size of the symbol table. */
|
---|
122 | uint32_t cSyms;
|
---|
123 | /** The file offset of the symbol table. */
|
---|
124 | uint32_t offSyms;
|
---|
125 | /** @} */
|
---|
126 |
|
---|
127 | /** @name Used during loading.
|
---|
128 | * @{ */
|
---|
129 | /** The file handle. */
|
---|
130 | RTFILE hFile;
|
---|
131 | /** The architecture image offset (fat_arch_t::offset). */
|
---|
132 | uint64_t offArch;
|
---|
133 | /** The architecture image size (fat_arch_t::size). */
|
---|
134 | uint32_t cbArch;
|
---|
135 | /** The number of load commands (mach_header_XX_t::ncmds). */
|
---|
136 | uint32_t cLoadCmds;
|
---|
137 | /** The size of the load commands. */
|
---|
138 | uint32_t cbLoadCmds;
|
---|
139 | /** The load commands. */
|
---|
140 | load_command_t *pLoadCmds;
|
---|
141 | /** Section pointer table (points into the load commands). */
|
---|
142 | MY_SECTION const *apSections[MACHO_MAX_SECT];
|
---|
143 | /** The number of sections. */
|
---|
144 | uint32_t cSections;
|
---|
145 | /** @} */
|
---|
146 |
|
---|
147 | /** Buffer space. */
|
---|
148 | char abBuf[_4K];
|
---|
149 | } RTR0DARWINKERNEL;
|
---|
150 | typedef RTR0DARWINKERNEL *PRTR0DARWINKERNEL;
|
---|
151 |
|
---|
152 |
|
---|
153 | /*******************************************************************************
|
---|
154 | * Structures and Typedefs *
|
---|
155 | *******************************************************************************/
|
---|
156 | #ifdef DEBUG
|
---|
157 | static bool g_fBreakpointOnError = true;
|
---|
158 | #endif
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Frees up the internal scratch data when done looking up symbols.
|
---|
163 | *
|
---|
164 | * @param pKernel The internal scratch data.
|
---|
165 | */
|
---|
166 | static void rtR0DarwinMachKernelClose(PRTR0DARWINKERNEL pKernel)
|
---|
167 | {
|
---|
168 | RTMemFree(pKernel->pachStrTab);
|
---|
169 | pKernel->pachStrTab = NULL;
|
---|
170 | RTMemFree(pKernel->paSyms);
|
---|
171 | pKernel->paSyms = NULL;
|
---|
172 |
|
---|
173 | RTMemFree(pKernel);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Close and free up resources we no longer needs.
|
---|
179 | *
|
---|
180 | * @param pKernel The internal scratch data.
|
---|
181 | */
|
---|
182 | static void rtR0DarwinMachKernelLoadDone(PRTR0DARWINKERNEL pKernel)
|
---|
183 | {
|
---|
184 | RTFileClose(pKernel->hFile);
|
---|
185 | pKernel->hFile = NIL_RTFILE;
|
---|
186 |
|
---|
187 | RTMemFree(pKernel->pLoadCmds);
|
---|
188 | pKernel->pLoadCmds = NULL;
|
---|
189 | RT_ZERO(pKernel->apSections);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Looks up a kernel symbol.
|
---|
195 | *
|
---|
196 | *
|
---|
197 | * @returns The symbol address on success, 0 on failure.
|
---|
198 | * @param pKernel The internal scratch data.
|
---|
199 | * @param pszSymbol The symbol to resolve. Automatically prefixed
|
---|
200 | * with an underscore.
|
---|
201 | */
|
---|
202 | static uintptr_t rtR0DarwinMachKernelLookup(PRTR0DARWINKERNEL pKernel, const char *pszSymbol)
|
---|
203 | {
|
---|
204 | uint32_t const cSyms = pKernel->cSyms;
|
---|
205 | MY_NLIST const *pSym = pKernel->paSyms;
|
---|
206 |
|
---|
207 | #if 1
|
---|
208 | /* linear search. */
|
---|
209 | for (uint32_t iSym = 0; iSym < cSyms; iSym++, pSym++)
|
---|
210 | {
|
---|
211 | if (pSym->n_type & MACHO_N_STAB)
|
---|
212 | continue;
|
---|
213 | const char *pszSym = &pKernel->pachStrTab[(uint32_t)pSym->n_un.n_strx];
|
---|
214 | if ( *pszSym == '_'
|
---|
215 | && strcmp(pszSym + 1, pszSymbol) == 0)
|
---|
216 | return pSym->n_value;
|
---|
217 | }
|
---|
218 | #else
|
---|
219 | /** @todo binary search. */
|
---|
220 |
|
---|
221 | #endif
|
---|
222 | return 0;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | static int rtR0DarwinMachKernelCheckStandardSymbols(PRTR0DARWINKERNEL pKernel)
|
---|
227 | {
|
---|
228 | static struct
|
---|
229 | {
|
---|
230 | const char *pszName;
|
---|
231 | uintptr_t uAddr;
|
---|
232 | } const s_aStandardCandles[] =
|
---|
233 | {
|
---|
234 | #if 0/// @todo def IN_RING0
|
---|
235 | # define KNOWN_ENTRY(a_Sym) { #a_Sym, (uintptr_t)&a_Sym }
|
---|
236 | #else
|
---|
237 | # define KNOWN_ENTRY(a_Sym) { #a_Sym, 0 }
|
---|
238 | #endif
|
---|
239 | KNOWN_ENTRY(IOMAlloc),
|
---|
240 | KNOWN_ENTRY(IOFree),
|
---|
241 | KNOWN_ENTRY(OSRuntimeFinalizeCPP),
|
---|
242 | KNOWN_ENTRY(OSRuntimeInitializeCPP)
|
---|
243 | };
|
---|
244 |
|
---|
245 | for (unsigned i = 0; i < RT_ELEMENTS(s_aStandardCandles); i++)
|
---|
246 | {
|
---|
247 | uintptr_t uAddr = rtR0DarwinMachKernelLookup(pKernel, s_aStandardCandles[i].pszName);
|
---|
248 | #ifdef IN_RING0
|
---|
249 | if (uAddr != s_aStandardCandles[i].uAddr)
|
---|
250 | #else
|
---|
251 | if (uAddr == 0)
|
---|
252 | #endif
|
---|
253 | {
|
---|
254 | AssertLogRelMsgFailed(("%s (%p != %p)\n", s_aStandardCandles[i].pszName, uAddr, s_aStandardCandles[i].uAddr));
|
---|
255 | return VERR_INTERNAL_ERROR_2;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | return VINF_SUCCESS;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Loads and validates the symbol and string tables.
|
---|
264 | *
|
---|
265 | * @returns IPRT status code.
|
---|
266 | * @param pKernel The internal scratch data.
|
---|
267 | */
|
---|
268 | static int rtR0DarwinMachKernelLoadSymTab(PRTR0DARWINKERNEL pKernel)
|
---|
269 | {
|
---|
270 | /*
|
---|
271 | * Load the tables.
|
---|
272 | */
|
---|
273 | pKernel->paSyms = (MY_NLIST *)RTMemAllocZ(pKernel->cSyms * sizeof(MY_NLIST));
|
---|
274 | if (!pKernel->paSyms)
|
---|
275 | return VERR_NO_MEMORY;
|
---|
276 |
|
---|
277 | int rc = RTFileReadAt(pKernel->hFile, pKernel->offArch + pKernel->offSyms,
|
---|
278 | pKernel->paSyms, pKernel->cSyms * sizeof(MY_NLIST), NULL);
|
---|
279 | if (RT_FAILURE(rc))
|
---|
280 | return rc;
|
---|
281 |
|
---|
282 | pKernel->pachStrTab = (char *)RTMemAllocZ(pKernel->cbStrTab + 1);
|
---|
283 | if (!pKernel->pachStrTab)
|
---|
284 | return VERR_NO_MEMORY;
|
---|
285 |
|
---|
286 | rc = RTFileReadAt(pKernel->hFile, pKernel->offArch + pKernel->offStrTab,
|
---|
287 | pKernel->pachStrTab, pKernel->cbStrTab, NULL);
|
---|
288 | if (RT_FAILURE(rc))
|
---|
289 | return rc;
|
---|
290 |
|
---|
291 | /*
|
---|
292 | * The first string table symbol must be a zero length name.
|
---|
293 | */
|
---|
294 | if (pKernel->pachStrTab[0] != '\0')
|
---|
295 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Validate the symbol table.
|
---|
299 | */
|
---|
300 | const char *pszPrev = "";
|
---|
301 | uint32_t const cSyms = pKernel->cSyms;
|
---|
302 | MY_NLIST const *pSym = pKernel->paSyms;
|
---|
303 | for (uint32_t iSym = 0; iSym < cSyms; iSym++, pSym++)
|
---|
304 | {
|
---|
305 | if ((uint32_t)pSym->n_un.n_strx >= pKernel->cbStrTab)
|
---|
306 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
307 | const char *pszSym = &pKernel->pachStrTab[(uint32_t)pSym->n_un.n_strx];
|
---|
308 | #ifdef IN_RING3
|
---|
309 | RTAssertMsg2("%05i: %02x:%08x %02x %04x %s\n", iSym, pSym->n_sect, pSym->n_value, pSym->n_type, pSym->n_desc, pszSym);
|
---|
310 | #endif
|
---|
311 |
|
---|
312 | if (strcmp(pszSym, pszPrev) < 0)
|
---|
313 | RETURN_VERR_BAD_EXE_FORMAT; /* not sorted */
|
---|
314 |
|
---|
315 | if (!(pSym->n_type & MACHO_N_STAB))
|
---|
316 | {
|
---|
317 | switch (pSym->n_type & MACHO_N_TYPE)
|
---|
318 | {
|
---|
319 | case MACHO_N_SECT:
|
---|
320 | if (pSym->n_sect == MACHO_NO_SECT)
|
---|
321 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
322 | if (pSym->n_sect > pKernel->cSections)
|
---|
323 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
324 | if (pSym->n_desc != 0)
|
---|
325 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
326 | if (pSym->n_value < pKernel->apSections[pSym->n_sect - 1]->addr)
|
---|
327 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
328 | if ( pSym->n_value - pKernel->apSections[pSym->n_sect - 1]->addr
|
---|
329 | > pKernel->apSections[pSym->n_sect - 1]->size)
|
---|
330 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
331 | break;
|
---|
332 |
|
---|
333 | case MACHO_N_ABS:
|
---|
334 | if (pSym->n_sect != MACHO_NO_SECT)
|
---|
335 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
336 | if (pSym->n_desc != 0)
|
---|
337 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
338 | break;
|
---|
339 |
|
---|
340 | case MACHO_N_UNDF:
|
---|
341 | /* No undefined or common symbols in the kernel. */
|
---|
342 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
343 |
|
---|
344 | case MACHO_N_INDR:
|
---|
345 | /* No indirect symbols in the kernel. */
|
---|
346 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
347 |
|
---|
348 | case MACHO_N_PBUD:
|
---|
349 | /* No prebound symbols in the kernel. */
|
---|
350 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
351 |
|
---|
352 | default:
|
---|
353 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
354 | }
|
---|
355 | }
|
---|
356 | /* else: Ignore debug symbols. */
|
---|
357 | }
|
---|
358 |
|
---|
359 | return VINF_SUCCESS;
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Loads the load commands and validates them.
|
---|
365 | *
|
---|
366 | * @returns IPRT status code.
|
---|
367 | * @param pKernel The internal scratch data.
|
---|
368 | */
|
---|
369 | static int rtR0DarwinMachKernelLoadCommands(PRTR0DARWINKERNEL pKernel)
|
---|
370 | {
|
---|
371 | pKernel->offStrTab = 0;
|
---|
372 | pKernel->cbStrTab = 0;
|
---|
373 | pKernel->offSyms = 0;
|
---|
374 | pKernel->cSyms = 0;
|
---|
375 | pKernel->cSections = 0;
|
---|
376 |
|
---|
377 | pKernel->pLoadCmds = (load_command_t *)RTMemAlloc(pKernel->cbLoadCmds);
|
---|
378 | if (!pKernel->pLoadCmds)
|
---|
379 | return VERR_NO_MEMORY;
|
---|
380 |
|
---|
381 | int rc = RTFileReadAt(pKernel->hFile, pKernel->offArch + sizeof(MY_MACHO_HEADER),
|
---|
382 | pKernel->pLoadCmds, pKernel->cbLoadCmds, NULL);
|
---|
383 | if (RT_FAILURE(rc))
|
---|
384 | return rc;
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Validate the relevant commands, picking up sections and the symbol
|
---|
388 | * table location.
|
---|
389 | */
|
---|
390 | load_command_t const *pCmd = pKernel->pLoadCmds;
|
---|
391 | for (uint32_t iCmd = 0; ; iCmd++)
|
---|
392 | {
|
---|
393 | /* cmd index & offset. */
|
---|
394 | uintptr_t offCmd = (uintptr_t)pCmd - (uintptr_t)pKernel->pLoadCmds;
|
---|
395 | if (offCmd == pKernel->cbLoadCmds && iCmd == pKernel->cLoadCmds)
|
---|
396 | break;
|
---|
397 | if (offCmd + sizeof(*pCmd) > pKernel->cbLoadCmds)
|
---|
398 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
399 | if (iCmd >= pKernel->cLoadCmds)
|
---|
400 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
401 |
|
---|
402 | /* cmdsize */
|
---|
403 | if (pCmd->cmdsize < sizeof(*pCmd))
|
---|
404 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
405 | if (pCmd->cmdsize > pKernel->cbLoadCmds)
|
---|
406 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
407 | if (RT_ALIGN_32(pCmd->cmdsize, 4) != pCmd->cmdsize)
|
---|
408 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
409 |
|
---|
410 | /* cmd */
|
---|
411 | switch (pCmd->cmd & ~LC_REQ_DYLD)
|
---|
412 | {
|
---|
413 | /* Validate and store the symbol table details. */
|
---|
414 | case LC_SYMTAB:
|
---|
415 | {
|
---|
416 | struct symtab_command const *pSymTab = (struct symtab_command const *)pCmd;
|
---|
417 | if (pSymTab->cmdsize != sizeof(*pSymTab))
|
---|
418 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
419 | if (pSymTab->nsyms > _1M)
|
---|
420 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
421 | if (pSymTab->strsize > _2M)
|
---|
422 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
423 |
|
---|
424 | pKernel->offStrTab = pSymTab->stroff;
|
---|
425 | pKernel->cbStrTab = pSymTab->strsize;
|
---|
426 | pKernel->offSyms = pSymTab->symoff;
|
---|
427 | pKernel->cSyms = pSymTab->nsyms;
|
---|
428 | break;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /* Validate the segment. */
|
---|
432 | #if ARCH_BITS == 32
|
---|
433 | case LC_SEGMENT_32:
|
---|
434 | #elif ARCH_BITS == 64
|
---|
435 | case LC_SEGMENT_64:
|
---|
436 | #else
|
---|
437 | # error ARCH_BITS
|
---|
438 | #endif
|
---|
439 | {
|
---|
440 | MY_SEGMENT_COMMAND const *pSeg = (MY_SEGMENT_COMMAND const *)pCmd;
|
---|
441 | if (pSeg->cmdsize < sizeof(*pSeg))
|
---|
442 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
443 |
|
---|
444 | if (pSeg->segname[0] == '\0')
|
---|
445 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
446 |
|
---|
447 | if (pSeg->nsects > MACHO_MAX_SECT)
|
---|
448 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
449 | if (pSeg->nsects * sizeof(MY_SECTION) + sizeof(*pSeg) != pSeg->cmdsize)
|
---|
450 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
451 |
|
---|
452 | if (pSeg->flags & ~(SG_HIGHVM | SG_FVMLIB | SG_NORELOC | SG_PROTECTED_VERSION_1))
|
---|
453 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
454 |
|
---|
455 | if (pSeg->vmaddr != 0)
|
---|
456 | {
|
---|
457 | if (pSeg->vmaddr + RT_ALIGN_Z(pSeg->vmsize, RT_BIT_32(12)) < pSeg->vmaddr)
|
---|
458 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
459 | }
|
---|
460 | else if (pSeg->vmsize)
|
---|
461 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
462 |
|
---|
463 | if (pSeg->maxprot & ~VM_PROT_ALL)
|
---|
464 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
465 | if (pSeg->initprot & ~VM_PROT_ALL)
|
---|
466 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
467 |
|
---|
468 | /* Validate the sections. */
|
---|
469 | uint32_t uAlignment = 0;
|
---|
470 | uintptr_t uAddr = pSeg->vmaddr;
|
---|
471 | MY_SECTION const *paSects = (MY_SECTION const *)(pSeg + 1);
|
---|
472 | for (uint32_t i = 0; i < pSeg->nsects; i++)
|
---|
473 | {
|
---|
474 | if (paSects[i].sectname[0] == '\0')
|
---|
475 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
476 | if (memcmp(paSects[i].segname, pSeg->segname, sizeof(pSeg->segname)))
|
---|
477 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
478 |
|
---|
479 | switch (paSects[i].flags & SECTION_TYPE)
|
---|
480 | {
|
---|
481 | case S_REGULAR:
|
---|
482 | case S_CSTRING_LITERALS:
|
---|
483 | case S_NON_LAZY_SYMBOL_POINTERS:
|
---|
484 | case S_MOD_INIT_FUNC_POINTERS:
|
---|
485 | case S_MOD_TERM_FUNC_POINTERS:
|
---|
486 | case S_COALESCED:
|
---|
487 | if ( pSeg->filesize != 0
|
---|
488 | ? paSects[i].offset - pSeg->fileoff >= pSeg->filesize
|
---|
489 | : paSects[i].offset - pSeg->fileoff != pSeg->filesize)
|
---|
490 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
491 | if ( paSects[i].addr != 0
|
---|
492 | && paSects[i].offset - pSeg->fileoff != paSects[i].addr - pSeg->vmaddr)
|
---|
493 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
494 | break;
|
---|
495 |
|
---|
496 | case S_ZEROFILL:
|
---|
497 | if (paSects[i].offset != 0)
|
---|
498 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
499 | break;
|
---|
500 |
|
---|
501 | /* not observed */
|
---|
502 | case S_SYMBOL_STUBS:
|
---|
503 | case S_INTERPOSING:
|
---|
504 | case S_4BYTE_LITERALS:
|
---|
505 | case S_8BYTE_LITERALS:
|
---|
506 | case S_16BYTE_LITERALS:
|
---|
507 | case S_DTRACE_DOF:
|
---|
508 | case S_LAZY_SYMBOL_POINTERS:
|
---|
509 | case S_LAZY_DYLIB_SYMBOL_POINTERS:
|
---|
510 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
511 | case S_GB_ZEROFILL:
|
---|
512 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
513 | default:
|
---|
514 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
515 | }
|
---|
516 |
|
---|
517 | if (paSects[i].align > 12)
|
---|
518 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
519 | if (paSects[i].align > uAlignment)
|
---|
520 | uAlignment = paSects[i].align;
|
---|
521 |
|
---|
522 | /* Add to the section table. */
|
---|
523 | if (pKernel->cSections == MACHO_MAX_SECT)
|
---|
524 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
525 | pKernel->apSections[pKernel->cSections++] = &paSects[i];
|
---|
526 | }
|
---|
527 |
|
---|
528 | if (RT_ALIGN_Z(pSeg->vmaddr, RT_BIT_32(uAlignment)) != pSeg->vmaddr)
|
---|
529 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
530 | if ( pSeg->filesize > RT_ALIGN_Z(pSeg->vmsize, RT_BIT_32(uAlignment))
|
---|
531 | && pSeg->vmsize != 0)
|
---|
532 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
533 | break;
|
---|
534 | }
|
---|
535 |
|
---|
536 | case LC_UUID:
|
---|
537 | if (pCmd->cmdsize != sizeof(uuid_command))
|
---|
538 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
539 | break;
|
---|
540 |
|
---|
541 | case LC_DYSYMTAB:
|
---|
542 | case LC_UNIXTHREAD:
|
---|
543 | break;
|
---|
544 |
|
---|
545 | /* not observed */
|
---|
546 | case LC_SYMSEG:
|
---|
547 | #if ARCH_BITS == 32
|
---|
548 | case LC_SEGMENT_64:
|
---|
549 | #elif ARCH_BITS == 64
|
---|
550 | case LC_SEGMENT_32:
|
---|
551 | #endif
|
---|
552 | case LC_ROUTINES_64:
|
---|
553 | case LC_ROUTINES:
|
---|
554 | case LC_THREAD:
|
---|
555 | case LC_LOADFVMLIB:
|
---|
556 | case LC_IDFVMLIB:
|
---|
557 | case LC_IDENT:
|
---|
558 | case LC_FVMFILE:
|
---|
559 | case LC_PREPAGE:
|
---|
560 | case LC_TWOLEVEL_HINTS:
|
---|
561 | case LC_PREBIND_CKSUM:
|
---|
562 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
563 |
|
---|
564 | /* dylib */
|
---|
565 | case LC_LOAD_DYLIB:
|
---|
566 | case LC_ID_DYLIB:
|
---|
567 | case LC_LOAD_DYLINKER:
|
---|
568 | case LC_ID_DYLINKER:
|
---|
569 | case LC_PREBOUND_DYLIB:
|
---|
570 | case LC_LOAD_WEAK_DYLIB & ~LC_REQ_DYLD:
|
---|
571 | case LC_SUB_FRAMEWORK:
|
---|
572 | case LC_SUB_UMBRELLA:
|
---|
573 | case LC_SUB_CLIENT:
|
---|
574 | case LC_SUB_LIBRARY:
|
---|
575 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
576 |
|
---|
577 | default:
|
---|
578 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
579 | }
|
---|
580 |
|
---|
581 | /* next */
|
---|
582 | pCmd = (load_command_t *)((uintptr_t)pCmd + pCmd->cmdsize);
|
---|
583 | }
|
---|
584 |
|
---|
585 | return VINF_SUCCESS;
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Loads the FAT and MACHO headers, noting down the relevant info.
|
---|
591 | *
|
---|
592 | * @returns IPRT status code.
|
---|
593 | * @param pKernel The internal scratch data.
|
---|
594 | */
|
---|
595 | static int rtR0DarwinMachKernelLoadFileHeaders(PRTR0DARWINKERNEL pKernel)
|
---|
596 | {
|
---|
597 | uint32_t i;
|
---|
598 |
|
---|
599 | pKernel->offArch = 0;
|
---|
600 | pKernel->cbArch = 0;
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * Read the first bit of the file, parse the FAT if found there.
|
---|
604 | */
|
---|
605 | int rc = RTFileReadAt(pKernel->hFile, 0, pKernel->abBuf, sizeof(fat_header_t) + sizeof(fat_arch_t) * 16, NULL);
|
---|
606 | if (RT_FAILURE(rc))
|
---|
607 | return rc;
|
---|
608 |
|
---|
609 | fat_header_t *pFat = (fat_header *)pKernel->abBuf;
|
---|
610 | fat_arch_t *paFatArches = (fat_arch_t *)(pFat + 1);
|
---|
611 |
|
---|
612 | /* Correct FAT endian first. */
|
---|
613 | if (pFat->magic == IMAGE_FAT_SIGNATURE_OE)
|
---|
614 | {
|
---|
615 | pFat->magic = RT_BSWAP_U32(pFat->magic);
|
---|
616 | pFat->nfat_arch = RT_BSWAP_U32(pFat->nfat_arch);
|
---|
617 | i = RT_MIN(pFat->nfat_arch, 16);
|
---|
618 | while (i-- > 0)
|
---|
619 | {
|
---|
620 | paFatArches[i].cputype = RT_BSWAP_U32(paFatArches[i].cputype);
|
---|
621 | paFatArches[i].cpusubtype = RT_BSWAP_U32(paFatArches[i].cpusubtype);
|
---|
622 | paFatArches[i].offset = RT_BSWAP_U32(paFatArches[i].offset);
|
---|
623 | paFatArches[i].size = RT_BSWAP_U32(paFatArches[i].size);
|
---|
624 | paFatArches[i].align = RT_BSWAP_U32(paFatArches[i].align);
|
---|
625 | }
|
---|
626 | }
|
---|
627 |
|
---|
628 | /* Lookup our architecture in the FAT. */
|
---|
629 | if (pFat->magic == IMAGE_FAT_SIGNATURE)
|
---|
630 | {
|
---|
631 | if (pFat->nfat_arch > 16)
|
---|
632 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
633 |
|
---|
634 | for (i = 0; i < pFat->nfat_arch; i++)
|
---|
635 | {
|
---|
636 | if ( paFatArches[i].cputype == MY_CPU_TYPE
|
---|
637 | && paFatArches[i].cpusubtype == MY_CPU_SUBTYPE_ALL)
|
---|
638 | {
|
---|
639 | pKernel->offArch = paFatArches[i].offset;
|
---|
640 | pKernel->cbArch = paFatArches[i].size;
|
---|
641 | if (!pKernel->cbArch)
|
---|
642 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
643 | if (pKernel->offArch < sizeof(fat_header_t) + sizeof(fat_arch_t) * pFat->nfat_arch)
|
---|
644 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
645 | if (pKernel->offArch + pKernel->cbArch <= pKernel->offArch)
|
---|
646 | RETURN_VERR_LDR_ARCH_MISMATCH;
|
---|
647 | break;
|
---|
648 | }
|
---|
649 | }
|
---|
650 | if (i >= pFat->nfat_arch)
|
---|
651 | RETURN_VERR_LDR_ARCH_MISMATCH;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /*
|
---|
655 | * Read the Mach-O header and validate it.
|
---|
656 | */
|
---|
657 | rc = RTFileReadAt(pKernel->hFile, pKernel->offArch, pKernel->abBuf, sizeof(MY_MACHO_HEADER), NULL);
|
---|
658 | if (RT_FAILURE(rc))
|
---|
659 | return rc;
|
---|
660 | MY_MACHO_HEADER const *pHdr = (MY_MACHO_HEADER const *)pKernel->abBuf;
|
---|
661 | if (pHdr->magic != MY_MACHO_MAGIC)
|
---|
662 | {
|
---|
663 | if ( pHdr->magic == IMAGE_MACHO32_SIGNATURE
|
---|
664 | || pHdr->magic == IMAGE_MACHO32_SIGNATURE_OE
|
---|
665 | || pHdr->magic == IMAGE_MACHO64_SIGNATURE
|
---|
666 | || pHdr->magic == IMAGE_MACHO64_SIGNATURE_OE)
|
---|
667 | RETURN_VERR_LDR_ARCH_MISMATCH;
|
---|
668 | RETURN_VERR_BAD_EXE_FORMAT;
|
---|
669 | }
|
---|
670 |
|
---|
671 | if (pHdr->cputype != MY_CPU_TYPE)
|
---|
672 | RETURN_VERR_LDR_ARCH_MISMATCH;
|
---|
673 | if (pHdr->cpusubtype != MY_CPU_SUBTYPE_ALL)
|
---|
674 | RETURN_VERR_LDR_ARCH_MISMATCH;
|
---|
675 | if (pHdr->filetype != MH_EXECUTE)
|
---|
676 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
677 | if (pHdr->ncmds < 4)
|
---|
678 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
679 | if (pHdr->ncmds > 256)
|
---|
680 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
681 | if (pHdr->sizeofcmds <= pHdr->ncmds * sizeof(load_command_t))
|
---|
682 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
683 | if (pHdr->sizeofcmds >= _1M)
|
---|
684 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
685 | if (pHdr->flags & ~MH_VALID_FLAGS)
|
---|
686 | RETURN_VERR_LDR_UNEXPECTED;
|
---|
687 |
|
---|
688 | pKernel->cLoadCmds = pHdr->ncmds;
|
---|
689 | pKernel->cbLoadCmds = pHdr->sizeofcmds;
|
---|
690 | return VINF_SUCCESS;
|
---|
691 | }
|
---|
692 |
|
---|
693 |
|
---|
694 | static int rtR0DarwinMachKernelOpen(const char *pszMachKernel, PRTR0DARWINKERNEL *ppHandle)
|
---|
695 | {
|
---|
696 | PRTR0DARWINKERNEL pKernel = (PRTR0DARWINKERNEL)RTMemAllocZ(sizeof(*pKernel));
|
---|
697 | if (!pKernel)
|
---|
698 | return VERR_NO_MEMORY;
|
---|
699 | pKernel->hFile = NIL_RTFILE;
|
---|
700 |
|
---|
701 | int rc = RTFileOpen(&pKernel->hFile, pszMachKernel, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
702 | if (RT_SUCCESS(rc))
|
---|
703 | rc = rtR0DarwinMachKernelLoadFileHeaders(pKernel);
|
---|
704 | if (RT_SUCCESS(rc))
|
---|
705 | rc = rtR0DarwinMachKernelLoadCommands(pKernel);
|
---|
706 | if (RT_SUCCESS(rc))
|
---|
707 | rc = rtR0DarwinMachKernelLoadSymTab(pKernel);
|
---|
708 | if (RT_SUCCESS(rc))
|
---|
709 | rc = rtR0DarwinMachKernelCheckStandardSymbols(pKernel);
|
---|
710 |
|
---|
711 | rtR0DarwinMachKernelLoadDone(pKernel);
|
---|
712 | if (RT_FAILURE(rc))
|
---|
713 | rtR0DarwinMachKernelClose(pKernel);
|
---|
714 | return rc;
|
---|
715 | }
|
---|
716 |
|
---|