VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/darwin/SUPR3HardenedMain-darwin.cpp@ 95067

Last change on this file since 95067 was 95067, checked in by vboxsync, 3 years ago

SUPHardDarwin: Corrected macOS 11+ detection. bugref:9836

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1/* $Id: SUPR3HardenedMain-darwin.cpp 95067 2022-05-23 21:37:30Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Hardened main(), posix bits.
4 */
5
6/*
7 * Copyright (C) 2017-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#include <VBox/err.h>
32#include <VBox/sup.h>
33
34#include <iprt/path.h>
35#include <iprt/string.h>
36
37#include <dlfcn.h>
38#include <sys/mman.h>
39#include <errno.h>
40#include <sys/sysctl.h> /* sysctlbyname() */
41#include <stdio.h>
42#include <stdint.h>
43#include <unistd.h> /* issetugid() */
44#include <mach-o/dyld.h>
45
46#include "SUPLibInternal.h"
47
48
49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
52
53
54/*********************************************************************************************************************************
55* Structures and Typedefs *
56*********************************************************************************************************************************/
57
58/**
59 * Interpose table entry.
60 */
61typedef struct DYLDINTERPOSE
62{
63 /** The symbol address to replace with. */
64 const void *pvReplacement;
65 /** The replaced symbol address. */
66 const void *pvReplacee;
67} DYLDINTERPOSE;
68/** Pointer to an interposer table entry. */
69typedef DYLDINTERPOSE *PDYLDINTERPOSE;
70/** Pointer to a const interposer table entry. */
71typedef const DYLDINTERPOSE *PCDYLDINTERPOSE;
72
73/** @sa dyld_dynamic_interpose(). */
74typedef const mach_header *FNDYLDDYNAMICINTERPOSE(const struct mach_header *mh, PCDYLDINTERPOSE paSym, size_t cSyms);
75/** Pointer to dyld_dynamic_interpose. */
76typedef FNDYLDDYNAMICINTERPOSE *PFNDYLDDYNAMICINTERPOSE;
77
78/** @sa dlopen(). */
79typedef void *FNDLOPEN(const char *path, int mode);
80/** Pointer to dlopen. */
81typedef FNDLOPEN *PFNDLOPEN;
82
83
84/*********************************************************************************************************************************
85* Internal Functions *
86*********************************************************************************************************************************/
87extern "C" void _dyld_register_func_for_add_image(void (*func)(const struct mach_header *mh, intptr_t vmaddr_slide));
88
89static void *supR3HardenedDarwinDlopenInterpose(const char *path, int mode);
90static int supR3HardenedDarwinIssetugidInterpose(void);
91
92
93/*********************************************************************************************************************************
94* Global Variables *
95*********************************************************************************************************************************/
96/** Flag whether macOS 11.x (BigSur) or later was detected.
97 * See comments in supR3HardenedDarwinDlopenInterpose for details. */
98static bool g_fMacOs11Plus = false;
99/** Resolved dyld_dynamic_interpose() value. */
100static PFNDYLDDYNAMICINTERPOSE g_pfnDyldDynamicInterpose = NULL;
101/** Pointer to the real dlopen() function used from the interposer when verification succeeded. */
102static PFNDLOPEN g_pfnDlopenReal = NULL;
103/**
104 * The interposer table.
105 */
106static const DYLDINTERPOSE g_aInterposers[] =
107{
108 { (const void *)(uintptr_t)&supR3HardenedDarwinDlopenInterpose, (const void *)(uintptr_t)&dlopen },
109 { (const void *)(uintptr_t)&supR3HardenedDarwinIssetugidInterpose, (const void *)(uintptr_t)&issetugid }
110};
111
112
113/**
114 * dlopen() interposer which verifies that the path to be loaded meets the criteria for hardened builds.
115 *
116 * @sa dlopen() man page.
117 */
118static void *supR3HardenedDarwinDlopenInterpose(const char *path, int mode)
119{
120 /*
121 * Giving NULL as the filename indicates opening the main program which is fine
122 * We are already loaded and executing after all.
123 *
124 * Filenames without any path component (whether absolute or relative) are allowed
125 * unconditionally too as the loader will only search the default paths configured by root.
126 */
127 if ( path
128 && strchr(path, '/') != NULL)
129 {
130 int rc = VINF_SUCCESS;
131
132 /*
133 * Starting with macOS 11.0 (BigSur) system provided libraries
134 * under /System/Libraries are not stored on the filesystem anymore
135 * but in a dynamic linker cache. The integrity of the linker cache
136 * is maintained by the system and dyld. Our verification code fails because
137 * it can't find the file.
138 * The obvious solution is to exclude paths starting with /System/Libraries
139 * when we run on BigSur. Other paths are still subject to verification.
140 */
141 if ( !g_fMacOs11Plus
142 || strncmp(path, RT_STR_TUPLE("/System/Library")))
143 rc = supR3HardenedVerifyFileFollowSymlinks(path, RTHCUINTPTR_MAX, true /* fMaybe3rdParty */,
144 NULL /* pErrInfo */);
145 if (RT_FAILURE(rc))
146 return NULL;
147 }
148
149 return g_pfnDlopenReal(path, mode);
150}
151
152
153/**
154 * Override this one to try hide the fact that we're setuid to root orginially.
155 *
156 * @sa issetugid() man page.
157 *
158 * Mac OS X: Really ugly hack to bypass a set-uid check in AppKit.
159 *
160 * This will modify the issetugid() function to always return zero. This must
161 * be done _before_ AppKit is initialized, otherwise it will refuse to play ball
162 * with us as it distrusts set-uid processes since Snow Leopard. We, however,
163 * have carefully dropped all root privileges at this point and there should be
164 * no reason for any security concern here.
165 */
166static int supR3HardenedDarwinIssetugidInterpose(void)
167{
168#ifdef DEBUG
169 Dl_info Info = {0};
170 char szMsg[512];
171 size_t cchMsg;
172 const void * uCaller = __builtin_return_address(0);
173 if (dladdr(uCaller, &Info))
174 cchMsg = snprintf(szMsg, sizeof(szMsg), "DEBUG: issetugid_for_AppKit was called by %p %s::%s+%p (via %p)\n",
175 uCaller, Info.dli_fname, Info.dli_sname, (void *)((uintptr_t)uCaller - (uintptr_t)Info.dli_saddr), __builtin_return_address(1));
176 else
177 cchMsg = snprintf(szMsg, sizeof(szMsg), "DEBUG: issetugid_for_AppKit was called by %p (via %p)\n", uCaller, __builtin_return_address(1));
178 write(2, szMsg, cchMsg);
179#endif
180 return 0;
181}
182
183
184/**
185 * Callback to get notified of new images being loaded to be able to apply our dlopn() interposer.
186 *
187 * @returns nothing.
188 * @param mh Pointer to the mach header of the loaded image.
189 * @param vmaddr_slide The slide value for ASLR.
190 */
191static DECLCALLBACK(void) supR3HardenedDarwinAddImage(const struct mach_header *mh, intptr_t vmaddr_slide)
192{
193 RT_NOREF(vmaddr_slide);
194
195 g_pfnDyldDynamicInterpose(mh, &g_aInterposers[0], RT_ELEMENTS(g_aInterposers));
196}
197
198
199/**
200 * Hardening initialization for macOS hosts.
201 *
202 * @returns nothing.
203 *
204 * @note Doesn't return on error.
205 */
206DECLHIDDEN(void) supR3HardenedDarwinInit(void)
207{
208 /*
209 * Check whether we are running on macOS BigSur by checking kern.osproductversion
210 * available since some point in 2018.
211 */
212 char szVers[256]; RT_ZERO(szVers);
213 size_t cbVers = sizeof(szVers);
214 int rc = sysctlbyname("kern.osproductversion", &szVers[0], &cbVers, NULL, 0);
215 if ( !rc
216 && memcmp(&szVers[0], RT_STR_TUPLE("10.16")) >= 0)
217 g_fMacOs11Plus = true;
218
219 /* Saved to call real dlopen() later on, as we will interpose dlopen() from the main binary in the next step as well. */
220 g_pfnDlopenReal = (PFNDLOPEN)dlsym(RTLD_DEFAULT, "dlopen");
221 g_pfnDyldDynamicInterpose = (PFNDYLDDYNAMICINTERPOSE)dlsym(RTLD_DEFAULT, "dyld_dynamic_interpose");
222 if (!g_pfnDyldDynamicInterpose)
223 supR3HardenedFatalMsg("supR3HardenedDarwinInit", kSupInitOp_Integrity, VERR_SYMBOL_NOT_FOUND,
224 "Failed to find dyld_dynamic_interpose()");
225
226 /*
227 * The following will causes our add image notification to be called for all images loaded so far.
228 * The callback will set up the interposer.
229 */
230 _dyld_register_func_for_add_image(supR3HardenedDarwinAddImage);
231}
232
233
234
235/*
236 * assert.cpp
237 *
238 * ASSUMES working DECLHIDDEN or there will be symbol confusion!
239 */
240
241RTDATADECL(char) g_szRTAssertMsg1[1024];
242RTDATADECL(char) g_szRTAssertMsg2[4096];
243RTDATADECL(const char * volatile) g_pszRTAssertExpr;
244RTDATADECL(const char * volatile) g_pszRTAssertFile;
245RTDATADECL(uint32_t volatile) g_u32RTAssertLine;
246RTDATADECL(const char * volatile) g_pszRTAssertFunction;
247
248RTDECL(bool) RTAssertMayPanic(void)
249{
250 return true;
251}
252
253
254RTDECL(void) RTAssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
255{
256 /*
257 * Fill in the globals.
258 */
259 g_pszRTAssertExpr = pszExpr;
260 g_pszRTAssertFile = pszFile;
261 g_pszRTAssertFunction = pszFunction;
262 g_u32RTAssertLine = uLine;
263 snprintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
264 "\n!!Assertion Failed!!\n"
265 "Expression: %s\n"
266 "Location : %s(%u) %s\n",
267 pszExpr, pszFile, uLine, pszFunction);
268}
269
270
271RTDECL(void) RTAssertMsg2V(const char *pszFormat, va_list va)
272{
273 vsnprintf(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, va);
274 if (g_enmSupR3HardenedMainState < SUPR3HARDENEDMAINSTATE_CALLED_TRUSTED_MAIN)
275 supR3HardenedFatalMsg(g_pszRTAssertExpr, kSupInitOp_Misc, VERR_INTERNAL_ERROR,
276 "%s%s", g_szRTAssertMsg1, g_szRTAssertMsg2);
277 else
278 supR3HardenedError(VERR_INTERNAL_ERROR, false/*fFatal*/, "%s%s", g_szRTAssertMsg1, g_szRTAssertMsg2);
279}
280
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette