VirtualBox

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

Last change on this file since 91521 was 87031, checked in by vboxsync, 4 years ago

Support,FE/VirtualBox: Move the issetugid() hack to the hardened support code to reduce the number of hacks in the VirtualBox frontend

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

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