VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPR3HardenedVerify.cpp@ 52004

Last change on this file since 52004 was 51912, checked in by vboxsync, 11 years ago

SUPR3HardenedVerify.cpp: Two more cases of CreateFileW with UTF-16.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.8 KB
Line 
1/* $Id: SUPR3HardenedVerify.cpp 51912 2014-07-07 21:46:57Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Verification of Hardened Installation.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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* Header Files *
29*******************************************************************************/
30#if defined(RT_OS_OS2)
31# define INCL_BASE
32# define INCL_ERRORS
33# include <os2.h>
34# include <stdio.h>
35# include <stdlib.h>
36# include <unistd.h>
37# include <sys/fcntl.h>
38# include <sys/errno.h>
39# include <sys/syslimits.h>
40
41#elif defined(RT_OS_WINDOWS)
42# include <Windows.h>
43# ifndef IN_SUP_HARDENED_R3
44# include <stdio.h>
45# endif
46
47#else /* UNIXes */
48# include <sys/types.h>
49# include <stdio.h>
50# include <stdlib.h>
51# include <dirent.h>
52# include <dlfcn.h>
53# include <fcntl.h>
54# include <limits.h>
55# include <errno.h>
56# include <unistd.h>
57# include <sys/stat.h>
58# include <sys/time.h>
59# include <sys/fcntl.h>
60# include <pwd.h>
61# ifdef RT_OS_DARWIN
62# include <mach-o/dyld.h>
63# endif
64
65#endif
66
67#include <VBox/sup.h>
68#include <VBox/err.h>
69#include <iprt/asm.h>
70#include <iprt/ctype.h>
71#include <iprt/param.h>
72#include <iprt/path.h>
73#include <iprt/string.h>
74
75#include "SUPLibInternal.h"
76#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
77# define SUPHNTVI_NO_NT_STUFF
78# include "win/SUPHardenedVerify-win.h"
79#endif
80
81
82/*******************************************************************************
83* Defined Constants And Macros *
84*******************************************************************************/
85/** The max path length acceptable for a trusted path. */
86#define SUPR3HARDENED_MAX_PATH 260U
87
88#ifdef RT_OS_SOLARIS
89# define dirfd(d) ((d)->d_fd)
90#endif
91
92/** Compare table file names with externally supplied names. */
93#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
94# define SUP_COMP_FILENAME suplibHardenedStrICmp
95#else
96# define SUP_COMP_FILENAME suplibHardenedStrCmp
97#endif
98
99
100/*******************************************************************************
101* Global Variables *
102*******************************************************************************/
103/**
104 * The files that gets verified.
105 *
106 * @todo This needs reviewing against the linux packages.
107 * @todo The excessive use of kSupID_SharedLib needs to be reviewed at some point. For
108 * the time being we're building the linux packages with SharedLib pointing to
109 * AppPrivArch (lazy bird).
110 */
111static SUPINSTFILE const g_aSupInstallFiles[] =
112{
113 /* type, dir, fOpt, "pszFile" */
114 /* ---------------------------------------------------------------------- */
115 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VMMR0.r0" },
116 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDDR0.r0" },
117 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDD2R0.r0" },
118
119#ifdef VBOX_WITH_RAW_MODE
120 { kSupIFT_Rc, kSupID_AppPrivArch, false, "VMMGC.gc" },
121 { kSupIFT_Rc, kSupID_AppPrivArch, false, "VBoxDDGC.gc" },
122 { kSupIFT_Rc, kSupID_AppPrivArch, false, "VBoxDD2GC.gc" },
123#endif
124
125 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxRT" SUPLIB_DLL_SUFF },
126 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxVMM" SUPLIB_DLL_SUFF },
127 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxREM" SUPLIB_DLL_SUFF },
128#if HC_ARCH_BITS == 32
129 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM32" SUPLIB_DLL_SUFF },
130 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM64" SUPLIB_DLL_SUFF },
131#endif
132 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD" SUPLIB_DLL_SUFF },
133 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD2" SUPLIB_DLL_SUFF },
134 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDDU" SUPLIB_DLL_SUFF },
135
136//#ifdef VBOX_WITH_DEBUGGER_GUI
137 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxDbg" SUPLIB_DLL_SUFF },
138 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxDbg3" SUPLIB_DLL_SUFF },
139//#endif
140
141//#ifdef VBOX_WITH_SHARED_CLIPBOARD
142 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedClipboard" SUPLIB_DLL_SUFF },
143//#endif
144//#ifdef VBOX_WITH_SHARED_FOLDERS
145 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedFolders" SUPLIB_DLL_SUFF },
146//#endif
147//#ifdef VBOX_WITH_DRAG_AND_DROP
148 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxDragAndDropSvc" SUPLIB_DLL_SUFF },
149//#endif
150//#ifdef VBOX_WITH_GUEST_PROPS
151 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxGuestPropSvc" SUPLIB_DLL_SUFF },
152//#endif
153//#ifdef VBOX_WITH_GUEST_CONTROL
154 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxGuestControlSvc" SUPLIB_DLL_SUFF },
155//#endif
156 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxHostChannel" SUPLIB_DLL_SUFF },
157 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedCrOpenGL" SUPLIB_DLL_SUFF },
158 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLhostcrutil" SUPLIB_DLL_SUFF },
159 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLhosterrorspu" SUPLIB_DLL_SUFF },
160 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLrenderspu" SUPLIB_DLL_SUFF },
161
162 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxManage" SUPLIB_EXE_SUFF },
163
164#ifdef VBOX_WITH_MAIN
165 { kSupIFT_Exe, kSupID_AppBin, false, "VBoxSVC" SUPLIB_EXE_SUFF },
166 #ifdef RT_OS_WINDOWS
167 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxC" SUPLIB_DLL_SUFF },
168 #else
169 { kSupIFT_Exe, kSupID_AppPrivArch, false, "VBoxXPCOMIPCD" SUPLIB_EXE_SUFF },
170 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxXPCOM" SUPLIB_DLL_SUFF },
171 { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxXPCOMIPCC" SUPLIB_DLL_SUFF },
172 { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxC" SUPLIB_DLL_SUFF },
173 { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxSVCM" SUPLIB_DLL_SUFF },
174 { kSupIFT_Data, kSupID_AppPrivArchComp, false, "VBoxXPCOMBase.xpt" },
175 #endif
176#endif
177
178 { kSupIFT_Dll, kSupID_SharedLib, true, "VRDPAuth" SUPLIB_DLL_SUFF },
179 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxAuth" SUPLIB_DLL_SUFF },
180 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxVRDP" SUPLIB_DLL_SUFF },
181
182//#ifdef VBOX_WITH_HEADLESS
183 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxHeadless" SUPLIB_EXE_SUFF },
184 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxHeadless" SUPLIB_DLL_SUFF },
185 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxVideoRecFB" SUPLIB_DLL_SUFF },
186//#endif
187
188//#ifdef VBOX_WITH_QTGUI
189 { kSupIFT_Exe, kSupID_AppBin, true, "VirtualBox" SUPLIB_EXE_SUFF },
190 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VirtualBox" SUPLIB_DLL_SUFF },
191# if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
192 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxKeyboard" SUPLIB_DLL_SUFF },
193# endif
194//#endif
195
196//#ifdef VBOX_WITH_VBOXSDL
197 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxSDL" SUPLIB_EXE_SUFF },
198 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSDL" SUPLIB_DLL_SUFF },
199//#endif
200
201//#ifdef VBOX_WITH_WEBSERVICES
202 { kSupIFT_Exe, kSupID_AppBin, true, "vboxwebsrv" SUPLIB_EXE_SUFF },
203//#endif
204
205#ifdef RT_OS_LINUX
206 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxTunctl" SUPLIB_EXE_SUFF },
207#endif
208
209//#ifdef VBOX_WITH_NETFLT
210 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxNetDHCP" SUPLIB_EXE_SUFF },
211 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxNetDHCP" SUPLIB_DLL_SUFF },
212//#endif
213
214//#ifdef VBOX_WITH_LWIP_NAT
215 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxNetNAT" SUPLIB_EXE_SUFF },
216 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxNetNAT" SUPLIB_DLL_SUFF },
217//#endif
218#if defined(VBOX_WITH_HARDENING) && defined(RT_OS_WINDOWS)
219# define HARDENED_TESTCASE_BIN_ENTRY(a_szName) \
220 { kSupIFT_TestExe, kSupID_AppBin, true, a_szName SUPLIB_EXE_SUFF }, \
221 { kSupIFT_TestDll, kSupID_AppBin, true, a_szName SUPLIB_DLL_SUFF }
222 HARDENED_TESTCASE_BIN_ENTRY("tstMicro"),
223 HARDENED_TESTCASE_BIN_ENTRY("tstPDMAsyncCompletion"),
224 HARDENED_TESTCASE_BIN_ENTRY("tstPDMAsyncCompletionStress"),
225 HARDENED_TESTCASE_BIN_ENTRY("tstVMM"),
226 HARDENED_TESTCASE_BIN_ENTRY("tstVMREQ"),
227# define HARDENED_TESTCASE_ENTRY(a_szName) \
228 { kSupIFT_TestExe, kSupID_Testcase, true, a_szName SUPLIB_EXE_SUFF }, \
229 { kSupIFT_TestDll, kSupID_Testcase, true, a_szName SUPLIB_DLL_SUFF }
230 HARDENED_TESTCASE_ENTRY("tstCFGM"),
231 HARDENED_TESTCASE_ENTRY("tstIntNet-1"),
232 HARDENED_TESTCASE_ENTRY("tstMMHyperHeap"),
233 HARDENED_TESTCASE_ENTRY("tstR0ThreadPreemptionDriver"),
234 HARDENED_TESTCASE_ENTRY("tstRTR0MemUserKernelDriver"),
235 HARDENED_TESTCASE_ENTRY("tstRTR0SemMutexDriver"),
236 HARDENED_TESTCASE_ENTRY("tstRTR0TimerDriver"),
237 HARDENED_TESTCASE_ENTRY("tstSSM"),
238#endif
239};
240
241
242/** Array parallel to g_aSupInstallFiles containing per-file status info. */
243static SUPVERIFIEDFILE g_aSupVerifiedFiles[RT_ELEMENTS(g_aSupInstallFiles)];
244
245/** Array index by install directory specifier containing info about verified directories. */
246static SUPVERIFIEDDIR g_aSupVerifiedDirs[kSupID_End];
247
248
249/**
250 * Assembles the path to a directory.
251 *
252 * @returns VINF_SUCCESS on success, some error code on failure (fFatal
253 * decides whether it returns or not).
254 *
255 * @param enmDir The directory.
256 * @param pszDst Where to assemble the path.
257 * @param cchDst The size of the buffer.
258 * @param fFatal Whether failures should be treated as fatal (true) or not (false).
259 */
260static int supR3HardenedMakePath(SUPINSTDIR enmDir, char *pszDst, size_t cchDst, bool fFatal)
261{
262 int rc;
263 switch (enmDir)
264 {
265 case kSupID_AppBin: /** @todo fix this AppBin crap (uncertain wtf some binaries actually are installed). */
266 case kSupID_Bin:
267 rc = supR3HardenedPathExecDir(pszDst, cchDst);
268 break;
269 case kSupID_SharedLib:
270 rc = supR3HardenedPathSharedLibs(pszDst, cchDst);
271 break;
272 case kSupID_AppPrivArch:
273 rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
274 break;
275 case kSupID_AppPrivArchComp:
276 rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
277 if (RT_SUCCESS(rc))
278 {
279 size_t off = suplibHardenedStrLen(pszDst);
280 if (cchDst - off >= sizeof("/components"))
281 suplibHardenedMemCopy(&pszDst[off], "/components", sizeof("/components"));
282 else
283 rc = VERR_BUFFER_OVERFLOW;
284 }
285 break;
286 case kSupID_AppPrivNoArch:
287 rc = supR3HardenedPathAppPrivateNoArch(pszDst, cchDst);
288 break;
289 case kSupID_Testcase:
290 rc = supR3HardenedPathExecDir(pszDst, cchDst);
291 if (RT_SUCCESS(rc))
292 {
293 size_t off = suplibHardenedStrLen(pszDst);
294 if (cchDst - off >= sizeof("/testcase"))
295 suplibHardenedMemCopy(&pszDst[off], "/testcase", sizeof("/testcase"));
296 else
297 rc = VERR_BUFFER_OVERFLOW;
298 }
299 break;
300 default:
301 return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
302 "supR3HardenedMakePath: enmDir=%d\n", enmDir);
303 }
304 if (RT_FAILURE(rc))
305 supR3HardenedError(rc, fFatal,
306 "supR3HardenedMakePath: enmDir=%d rc=%d\n", enmDir, rc);
307 return rc;
308}
309
310
311
312/**
313 * Assembles the path to a file table entry, with or without the actual filename.
314 *
315 * @returns VINF_SUCCESS on success, some error code on failure (fFatal
316 * decides whether it returns or not).
317 *
318 * @param pFile The file table entry.
319 * @param pszDst Where to assemble the path.
320 * @param cchDst The size of the buffer.
321 * @param fWithFilename If set, the filename is included, otherwise it is omitted (no trailing slash).
322 * @param fFatal Whether failures should be treated as fatal (true) or not (false).
323 */
324static int supR3HardenedMakeFilePath(PCSUPINSTFILE pFile, char *pszDst, size_t cchDst, bool fWithFilename, bool fFatal)
325{
326 /*
327 * Combine supR3HardenedMakePath and the filename.
328 */
329 int rc = supR3HardenedMakePath(pFile->enmDir, pszDst, cchDst, fFatal);
330 if (RT_SUCCESS(rc) && fWithFilename)
331 {
332 size_t cchFile = suplibHardenedStrLen(pFile->pszFile);
333 size_t off = suplibHardenedStrLen(pszDst);
334 if (cchDst - off >= cchFile + 2)
335 {
336 pszDst[off++] = '/';
337 suplibHardenedMemCopy(&pszDst[off], pFile->pszFile, cchFile + 1);
338 }
339 else
340 rc = supR3HardenedError(VERR_BUFFER_OVERFLOW, fFatal,
341 "supR3HardenedMakeFilePath: pszFile=%s off=%lu\n",
342 pFile->pszFile, (long)off);
343 }
344 return rc;
345}
346
347
348/**
349 * Verifies a directory.
350 *
351 * @returns VINF_SUCCESS on success. On failure, an error code is returned if
352 * fFatal is clear and if it's set the function wont return.
353 * @param enmDir The directory specifier.
354 * @param fFatal Whether validation failures should be treated as
355 * fatal (true) or not (false).
356 */
357DECLHIDDEN(int) supR3HardenedVerifyFixedDir(SUPINSTDIR enmDir, bool fFatal)
358{
359 /*
360 * Validate the index just to be on the safe side...
361 */
362 if (enmDir <= kSupID_Invalid || enmDir >= kSupID_End)
363 return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
364 "supR3HardenedVerifyDir: enmDir=%d\n", enmDir);
365
366 /*
367 * Already validated?
368 */
369 if (g_aSupVerifiedDirs[enmDir].fValidated)
370 return VINF_SUCCESS; /** @todo revalidate? */
371
372 /* initialize the entry. */
373 if (g_aSupVerifiedDirs[enmDir].hDir != 0)
374 supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
375 "supR3HardenedVerifyDir: hDir=%p enmDir=%d\n",
376 (void *)g_aSupVerifiedDirs[enmDir].hDir, enmDir);
377 g_aSupVerifiedDirs[enmDir].hDir = -1;
378 g_aSupVerifiedDirs[enmDir].fValidated = false;
379
380 /*
381 * Make the path and open the directory.
382 */
383 char szPath[RTPATH_MAX];
384 int rc = supR3HardenedMakePath(enmDir, szPath, sizeof(szPath), fFatal);
385 if (RT_SUCCESS(rc))
386 {
387#if defined(RT_OS_WINDOWS)
388 PRTUTF16 pwszPath;
389 rc = RTStrToUtf16(szPath, &pwszPath);
390 if (RT_SUCCESS(rc))
391 {
392 HANDLE hDir = CreateFileW(pwszPath,
393 GENERIC_READ,
394 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
395 NULL,
396 OPEN_EXISTING,
397 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
398 NULL);
399 if (hDir != INVALID_HANDLE_VALUE)
400 {
401 /** @todo check the type */
402 /* That's all on windows, for now at least... */
403 g_aSupVerifiedDirs[enmDir].hDir = (intptr_t)hDir;
404 g_aSupVerifiedDirs[enmDir].fValidated = true;
405 }
406 else if (enmDir == kSupID_Testcase)
407 {
408 g_aSupVerifiedDirs[enmDir].fValidated = true;
409 rc = VINF_SUCCESS; /* Optional directory, ignore if missing. */
410 }
411 else
412 {
413 int err = GetLastError();
414 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
415 "supR3HardenedVerifyDir: Failed to open \"%s\": err=%d\n",
416 szPath, err);
417 }
418 RTUtf16Free(pwszPath);
419 }
420 else
421 rc = supR3HardenedError(rc, fFatal,
422 "supR3HardenedVerifyDir: Failed to convert \"%s\" to UTF-16: err=%d\n", szPath, rc);
423
424#else /* UNIXY */
425 int fd = open(szPath, O_RDONLY, 0);
426 if (fd >= 0)
427 {
428 /*
429 * On unixy systems we'll make sure the directory is owned by root
430 * and not writable by the group and user.
431 */
432 struct stat st;
433 if (!fstat(fd, &st))
434 {
435
436 if ( st.st_uid == 0
437 && !(st.st_mode & (S_IWGRP | S_IWOTH))
438 && S_ISDIR(st.st_mode))
439 {
440 g_aSupVerifiedDirs[enmDir].hDir = fd;
441 g_aSupVerifiedDirs[enmDir].fValidated = true;
442 }
443 else
444 {
445 if (!S_ISDIR(st.st_mode))
446 rc = supR3HardenedError(VERR_NOT_A_DIRECTORY, fFatal,
447 "supR3HardenedVerifyDir: \"%s\" is not a directory\n",
448 szPath, (long)st.st_uid);
449 else if (st.st_uid)
450 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
451 "supR3HardenedVerifyDir: Cannot trust the directory \"%s\": not owned by root (st_uid=%ld)\n",
452 szPath, (long)st.st_uid);
453 else
454 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
455 "supR3HardenedVerifyDir: Cannot trust the directory \"%s\": group and/or other writable (st_mode=0%lo)\n",
456 szPath, (long)st.st_mode);
457 close(fd);
458 }
459 }
460 else
461 {
462 int err = errno;
463 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
464 "supR3HardenedVerifyDir: Failed to fstat \"%s\": %s (%d)\n",
465 szPath, strerror(err), err);
466 close(fd);
467 }
468 }
469 else if (enmDir == kSupID_Testcase)
470 {
471 g_aSupVerifiedDirs[enmDir].fValidated = true;
472 rc = VINF_SUCCESS; /* Optional directory, ignore if missing. */
473 }
474 else
475 {
476 int err = errno;
477 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
478 "supR3HardenedVerifyDir: Failed to open \"%s\": %s (%d)\n",
479 szPath, strerror(err), err);
480 }
481#endif /* UNIXY */
482 }
483
484 return rc;
485}
486
487
488/**
489 * Verifies a file entry.
490 *
491 * @returns VINF_SUCCESS on success. On failure, an error code is returned if
492 * fFatal is clear and if it's set the function wont return.
493 *
494 * @param iFile The file table index of the file to be verified.
495 * @param fFatal Whether validation failures should be treated as
496 * fatal (true) or not (false).
497 * @param fLeaveFileOpen Whether the file should be left open.
498 */
499static int supR3HardenedVerifyFileInternal(int iFile, bool fFatal, bool fLeaveFileOpen)
500{
501 PCSUPINSTFILE pFile = &g_aSupInstallFiles[iFile];
502 PSUPVERIFIEDFILE pVerified = &g_aSupVerifiedFiles[iFile];
503
504 /*
505 * Already done?
506 */
507 if (pVerified->fValidated)
508 return VINF_SUCCESS; /** @todo revalidate? */
509
510
511 /* initialize the entry. */
512 if (pVerified->hFile != 0)
513 supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
514 "supR3HardenedVerifyFileInternal: hFile=%p (%s)\n",
515 (void *)pVerified->hFile, pFile->pszFile);
516 pVerified->hFile = -1;
517 pVerified->fValidated = false;
518
519 /*
520 * Verify the directory then proceed to open it.
521 * (This'll make sure the directory is opened and that we can (later)
522 * use openat if we wish.)
523 */
524 int rc = supR3HardenedVerifyFixedDir(pFile->enmDir, fFatal);
525 if (RT_SUCCESS(rc))
526 {
527 char szPath[RTPATH_MAX];
528 rc = supR3HardenedMakeFilePath(pFile, szPath, sizeof(szPath), true /*fWithFilename*/, fFatal);
529 if (RT_SUCCESS(rc))
530 {
531#if defined(RT_OS_WINDOWS)
532 PRTUTF16 pwszPath;
533 rc = RTStrToUtf16(szPath, &pwszPath);
534 if (RT_SUCCESS(rc))
535 {
536 HANDLE hFile = CreateFileW(pwszPath,
537 GENERIC_READ,
538 FILE_SHARE_READ,
539 NULL,
540 OPEN_EXISTING,
541 FILE_ATTRIBUTE_NORMAL,
542 NULL);
543 if (hFile != INVALID_HANDLE_VALUE)
544 {
545# if defined(VBOX_WITH_HARDENING) && !defined(IN_SUP_R3_STATIC) /* Latter: Not in VBoxCpuReport and friends. */
546
547 char szErr[1024];
548 RTERRINFO ErrInfo;
549 RTErrInfoInit(&ErrInfo, szErr, sizeof(szErr));
550
551 uint32_t fFlags = SUPHNTVI_F_REQUIRE_BUILD_CERT;
552 if (pFile->enmType == kSupIFT_Rc)
553 fFlags |= SUPHNTVI_F_RC_IMAGE;
554
555 rc = supHardenedWinVerifyImageByHandleNoName(hFile, fFlags, &ErrInfo);
556 if (RT_FAILURE(rc))
557 {
558 rc = supR3HardenedError(rc, fFatal, "supR3HardenedVerifyFileInternal: '%s': Image verify error rc=%Rrc: %s\n",
559 szPath, rc, szErr);
560 CloseHandle(hFile);
561 }
562 else
563# endif
564 {
565 /* it's valid. */
566 if (fLeaveFileOpen)
567 pVerified->hFile = (intptr_t)hFile;
568 else
569 CloseHandle(hFile);
570 pVerified->fValidated = true;
571 }
572 }
573 else
574 {
575 int err = GetLastError();
576 if ( !pFile->fOptional
577 || ( err != ERROR_FILE_NOT_FOUND
578 && (err != ERROR_PATH_NOT_FOUND || pFile->enmDir != kSupID_Testcase) ) )
579 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
580 "supR3HardenedVerifyFileInternal: Failed to open '%s': err=%d\n", szPath, err);
581 }
582 RTUtf16Free(pwszPath);
583 }
584 else
585 rc = supR3HardenedError(rc, fFatal, "supR3HardenedVerifyFileInternal: Failed to convert '%s' to UTF-16: %Rrc\n",
586 szPath, rc);
587#else /* UNIXY */
588 int fd = open(szPath, O_RDONLY, 0);
589 if (fd >= 0)
590 {
591 /*
592 * On unixy systems we'll make sure the directory is owned by root
593 * and not writable by the group and user.
594 */
595 struct stat st;
596 if (!fstat(fd, &st))
597 {
598 if ( st.st_uid == 0
599 && !(st.st_mode & (S_IWGRP | S_IWOTH))
600 && S_ISREG(st.st_mode))
601 {
602 /* it's valid. */
603 if (fLeaveFileOpen)
604 pVerified->hFile = fd;
605 else
606 close(fd);
607 pVerified->fValidated = true;
608 }
609 else
610 {
611 if (!S_ISREG(st.st_mode))
612 rc = supR3HardenedError(VERR_IS_A_DIRECTORY, fFatal,
613 "supR3HardenedVerifyFileInternal: \"%s\" is not a regular file\n",
614 szPath, (long)st.st_uid);
615 else if (st.st_uid)
616 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
617 "supR3HardenedVerifyFileInternal: Cannot trust the file \"%s\": not owned by root (st_uid=%ld)\n",
618 szPath, (long)st.st_uid);
619 else
620 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
621 "supR3HardenedVerifyFileInternal: Cannot trust the file \"%s\": group and/or other writable (st_mode=0%lo)\n",
622 szPath, (long)st.st_mode);
623 close(fd);
624 }
625 }
626 else
627 {
628 int err = errno;
629 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
630 "supR3HardenedVerifyFileInternal: Failed to fstat \"%s\": %s (%d)\n",
631 szPath, strerror(err), err);
632 close(fd);
633 }
634 }
635 else
636 {
637 int err = errno;
638 if (!pFile->fOptional || err != ENOENT)
639 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
640 "supR3HardenedVerifyFileInternal: Failed to open \"%s\": %s (%d)\n",
641 szPath, strerror(err), err);
642 }
643#endif /* UNIXY */
644 }
645 }
646
647 return rc;
648}
649
650
651/**
652 * Verifies that the specified table entry matches the given filename.
653 *
654 * @returns VINF_SUCCESS if matching. On mismatch fFatal indicates whether an
655 * error is returned or we terminate the application.
656 *
657 * @param iFile The file table index.
658 * @param pszFilename The filename.
659 * @param fFatal Whether validation failures should be treated as
660 * fatal (true) or not (false).
661 */
662static int supR3HardenedVerifySameFile(int iFile, const char *pszFilename, bool fFatal)
663{
664 PCSUPINSTFILE pFile = &g_aSupInstallFiles[iFile];
665
666 /*
667 * Construct the full path for the file table entry
668 * and compare it with the specified file.
669 */
670 char szName[RTPATH_MAX];
671 int rc = supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
672 if (RT_FAILURE(rc))
673 return rc;
674 if (SUP_COMP_FILENAME(szName, pszFilename))
675 {
676 /*
677 * Normalize the two paths and compare again.
678 */
679 rc = VERR_NOT_SAME_DEVICE;
680#if defined(RT_OS_WINDOWS)
681 LPSTR pszIgnored;
682 char szName2[RTPATH_MAX];
683 if ( GetFullPathName(szName, RT_ELEMENTS(szName2), &szName2[0], &pszIgnored)
684 && GetFullPathName(pszFilename, RT_ELEMENTS(szName), &szName[0], &pszIgnored))
685 if (!SUP_COMP_FILENAME(szName2, szName))
686 rc = VINF_SUCCESS;
687#else
688 AssertCompile(RTPATH_MAX >= PATH_MAX);
689 char szName2[RTPATH_MAX];
690 if ( realpath(szName, szName2) != NULL
691 && realpath(pszFilename, szName) != NULL)
692 if (!SUP_COMP_FILENAME(szName2, szName))
693 rc = VINF_SUCCESS;
694#endif
695
696 if (RT_FAILURE(rc))
697 {
698 supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
699 return supR3HardenedError(rc, fFatal,
700 "supR3HardenedVerifySameFile: \"%s\" isn't the same as \"%s\"\n",
701 pszFilename, szName);
702 }
703 }
704
705 /*
706 * Check more stuff like the stat info if it's an already open file?
707 */
708
709
710
711 return VINF_SUCCESS;
712}
713
714
715/**
716 * Verifies a file.
717 *
718 * @returns VINF_SUCCESS on success.
719 * VERR_NOT_FOUND if the file isn't in the table, this isn't ever a fatal error.
720 * On verification failure, an error code will be returned when fFatal is clear,
721 * otherwise the program will be terminated.
722 *
723 * @param pszFilename The filename.
724 * @param fFatal Whether validation failures should be treated as
725 * fatal (true) or not (false).
726 */
727DECLHIDDEN(int) supR3HardenedVerifyFixedFile(const char *pszFilename, bool fFatal)
728{
729 /*
730 * Lookup the file and check if it's the same file.
731 */
732 const char *pszName = supR3HardenedPathFilename(pszFilename);
733 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
734 if (!SUP_COMP_FILENAME(pszName, g_aSupInstallFiles[iFile].pszFile))
735 {
736 int rc = supR3HardenedVerifySameFile(iFile, pszFilename, fFatal);
737 if (RT_SUCCESS(rc))
738 rc = supR3HardenedVerifyFileInternal(iFile, fFatal, false /* fLeaveFileOpen */);
739 return rc;
740 }
741
742 return VERR_NOT_FOUND;
743}
744
745
746/**
747 * Verifies a program, worker for supR3HardenedVerifyAll.
748 *
749 * @returns See supR3HardenedVerifyAll.
750 * @param pszProgName See supR3HardenedVerifyAll.
751 * @param fFatal See supR3HardenedVerifyAll.
752 */
753static int supR3HardenedVerifyProgram(const char *pszProgName, bool fFatal)
754{
755 /*
756 * Search the table looking for the executable and the DLL/DYLIB/SO.
757 */
758 int rc = VINF_SUCCESS;
759 bool fExe = false;
760 bool fDll = false;
761 size_t const cchProgName = suplibHardenedStrLen(pszProgName);
762 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
763 if (!suplibHardenedStrNCmp(pszProgName, g_aSupInstallFiles[iFile].pszFile, cchProgName))
764 {
765 if ( ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Dll
766 || g_aSupInstallFiles[iFile].enmType == kSupIFT_TestDll)
767 && !suplibHardenedStrCmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_DLL_SUFF))
768 {
769 /* This only has to be found (once). */
770 if (fDll)
771 rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
772 "supR3HardenedVerifyProgram: duplicate DLL entry for \"%s\"\n", pszProgName);
773 fDll = true;
774 }
775 else if ( ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Exe
776 || g_aSupInstallFiles[iFile].enmType == kSupIFT_TestExe)
777 && !suplibHardenedStrCmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_EXE_SUFF))
778 {
779 /* Here we'll have to check that the specific program is the same as the entry. */
780 if (fExe)
781 rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
782 "supR3HardenedVerifyProgram: duplicate EXE entry for \"%s\"\n", pszProgName);
783 fExe = true;
784
785 char szFilename[RTPATH_MAX];
786 int rc2 = supR3HardenedPathExecDir(szFilename, sizeof(szFilename) - cchProgName - sizeof(SUPLIB_EXE_SUFF));
787 if (RT_SUCCESS(rc2))
788 {
789 suplibHardenedStrCat(szFilename, "/");
790 suplibHardenedStrCat(szFilename, g_aSupInstallFiles[iFile].pszFile);
791 supR3HardenedVerifySameFile(iFile, szFilename, fFatal);
792 }
793 else
794 rc = supR3HardenedError(rc2, fFatal,
795 "supR3HardenedVerifyProgram: failed to query program path: rc=%d\n", rc2);
796 }
797 }
798
799 /*
800 * Check the findings.
801 */
802 if (!fDll && !fExe)
803 rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
804 "supR3HardenedVerifyProgram: Couldn't find the program \"%s\"\n", pszProgName);
805 else if (!fExe)
806 rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
807 "supR3HardenedVerifyProgram: Couldn't find the EXE entry for \"%s\"\n", pszProgName);
808 else if (!fDll)
809 rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
810 "supR3HardenedVerifyProgram: Couldn't find the DLL entry for \"%s\"\n", pszProgName);
811 return rc;
812}
813
814
815/**
816 * Verifies all the known files.
817 *
818 * @returns VINF_SUCCESS on success.
819 * On verification failure, an error code will be returned when fFatal is clear,
820 * otherwise the program will be terminated.
821 *
822 * @param fFatal Whether validation failures should be treated as
823 * fatal (true) or not (false).
824 * @param fLeaveFilesOpen If set, all the verified files are left open.
825 * @param pszProgName Optional program name. This is used by SUPR3HardenedMain
826 * to verify that both the executable and corresponding
827 * DLL/DYLIB/SO are valid.
828 */
829DECLHIDDEN(int) supR3HardenedVerifyAll(bool fFatal, bool fLeaveFilesOpen, const char *pszProgName)
830{
831 /*
832 * The verify all the files.
833 */
834 int rc = VINF_SUCCESS;
835 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
836 {
837 int rc2 = supR3HardenedVerifyFileInternal(iFile, fFatal, fLeaveFilesOpen);
838 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
839 rc = rc2;
840 }
841
842 /*
843 * Verify the program name if specified, that is to say, just check that
844 * it's in the table (=> we've already verified it).
845 */
846 if (pszProgName)
847 {
848 int rc2 = supR3HardenedVerifyProgram(pszProgName, fFatal);
849 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
850 rc2 = rc;
851 }
852
853 return rc;
854}
855
856
857/**
858 * Copies the N messages into the error buffer and returns @a rc.
859 *
860 * @returns Returns @a rc
861 * @param rc The return code.
862 * @param pErrInfo The error info structure.
863 * @param cMsgs The number of messages in the ellipsis.
864 * @param ... Message parts.
865 */
866static int supR3HardenedSetErrorN(int rc, PRTERRINFO pErrInfo, unsigned cMsgs, ...)
867{
868 if (pErrInfo)
869 {
870 size_t cbErr = pErrInfo->cbMsg;
871 char *pszErr = pErrInfo->pszMsg;
872
873 va_list va;
874 va_start(va, cMsgs);
875 while (cMsgs-- > 0 && cbErr > 0)
876 {
877 const char *pszMsg = va_arg(va, const char *);
878 size_t cchMsg = VALID_PTR(pszMsg) ? suplibHardenedStrLen(pszMsg) : 0;
879 if (cchMsg >= cbErr)
880 cchMsg = cbErr - 1;
881 suplibHardenedMemCopy(pszErr, pszMsg, cchMsg);
882 pszErr[cchMsg] = '\0';
883 pszErr += cchMsg;
884 cbErr -= cchMsg;
885 }
886 va_end(va);
887
888 pErrInfo->rc = rc;
889 pErrInfo->fFlags |= RTERRINFO_FLAGS_SET;
890 }
891
892 return rc;
893}
894
895
896/**
897 * Copies the three messages into the error buffer and returns @a rc.
898 *
899 * @returns Returns @a rc
900 * @param rc The return code.
901 * @param pErrInfo The error info structure.
902 * @param pszMsg1 The first message part.
903 * @param pszMsg2 The second message part.
904 * @param pszMsg3 The third message part.
905 */
906static int supR3HardenedSetError3(int rc, PRTERRINFO pErrInfo, const char *pszMsg1,
907 const char *pszMsg2, const char *pszMsg3)
908{
909 return supR3HardenedSetErrorN(rc, pErrInfo, 3, pszMsg1, pszMsg2, pszMsg3);
910}
911
912#ifdef SOME_UNUSED_FUNCTION
913
914/**
915 * Copies the two messages into the error buffer and returns @a rc.
916 *
917 * @returns Returns @a rc
918 * @param rc The return code.
919 * @param pErrInfo The error info structure.
920 * @param pszMsg1 The first message part.
921 * @param pszMsg2 The second message part.
922 */
923static int supR3HardenedSetError2(int rc, PRTERRINFO pErrInfo, const char *pszMsg1,
924 const char *pszMsg2)
925{
926 return supR3HardenedSetErrorN(rc, pErrInfo, 2, pszMsg1, pszMsg2);
927}
928
929
930/**
931 * Copies the error message to the error buffer and returns @a rc.
932 *
933 * @returns Returns @a rc
934 * @param rc The return code.
935 * @param pErrInfo The error info structure.
936 * @param pszMsg The message.
937 */
938static int supR3HardenedSetError(int rc, PRTERRINFO pErrInfo, const char *pszMsg)
939{
940 return supR3HardenedSetErrorN(rc, pErrInfo, 1, pszMsg);
941}
942
943#endif /* SOME_UNUSED_FUNCTION */
944
945/**
946 * Output from a successfull supR3HardenedVerifyPathSanity call.
947 */
948typedef struct SUPR3HARDENEDPATHINFO
949{
950 /** The length of the path in szCopy. */
951 uint16_t cch;
952 /** The number of path components. */
953 uint16_t cComponents;
954 /** Set if the path ends with slash, indicating that it's a directory
955 * reference and not a file reference. The slash has been removed from
956 * the copy. */
957 bool fDirSlash;
958 /** The offset where each path component starts, i.e. the char after the
959 * slash. The array has cComponents + 1 entries, where the final one is
960 * cch + 1 so that one can always terminate the current component by
961 * szPath[aoffComponent[i] - 1] = '\0'. */
962 uint16_t aoffComponents[32+1];
963 /** A normalized copy of the path.
964 * Reserve some extra space so we can be more relaxed about overflow
965 * checks and terminator paddings, especially when recursing. */
966 char szPath[SUPR3HARDENED_MAX_PATH * 2];
967} SUPR3HARDENEDPATHINFO;
968/** Pointer to a parsed path. */
969typedef SUPR3HARDENEDPATHINFO *PSUPR3HARDENEDPATHINFO;
970
971
972/**
973 * Verifies that the path is absolutely sane, it also parses the path.
974 *
975 * A sane path starts at the root (w/ drive letter on DOS derived systems) and
976 * does not have any relative bits (/../) or unnecessary slashes (/bin//ls).
977 * Sane paths are less or equal to SUPR3HARDENED_MAX_PATH bytes in length. UNC
978 * paths are not supported.
979 *
980 * @returns VBox status code.
981 * @param pszPath The path to check.
982 * @param pErrInfo The error info structure.
983 * @param pInfo Where to return a copy of the path along with
984 * parsing information.
985 */
986static int supR3HardenedVerifyPathSanity(const char *pszPath, PRTERRINFO pErrInfo, PSUPR3HARDENEDPATHINFO pInfo)
987{
988 const char *pszSrc = pszPath;
989 char *pszDst = pInfo->szPath;
990
991 /*
992 * Check that it's an absolute path and copy the volume/root specifier.
993 */
994#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
995 if ( !RT_C_IS_ALPHA(pszSrc[0])
996 || pszSrc[1] != ':'
997 || !RTPATH_IS_SLASH(pszSrc[2]))
998 return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pErrInfo, "The path is not absolute: '", pszPath, "'");
999
1000 *pszDst++ = RT_C_TO_UPPER(pszSrc[0]);
1001 *pszDst++ = ':';
1002 *pszDst++ = RTPATH_SLASH;
1003 pszSrc += 3;
1004
1005#else
1006 if (!RTPATH_IS_SLASH(pszSrc[0]))
1007 return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pErrInfo, "The path is not absolute: '", pszPath, "'");
1008
1009 *pszDst++ = RTPATH_SLASH;
1010 pszSrc += 1;
1011#endif
1012
1013 /*
1014 * No path specifying the root or something very shortly thereafter will
1015 * be approved of.
1016 */
1017 if (pszSrc[0] == '\0')
1018 return supR3HardenedSetError3(VERR_SUPLIB_PATH_IS_ROOT, pErrInfo, "The path is root: '", pszPath, "'");
1019 if ( pszSrc[1] == '\0'
1020 || pszSrc[2] == '\0')
1021 return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_SHORT, pErrInfo, "The path is too short: '", pszPath, "'");
1022
1023 /*
1024 * Check each component. No parent references or double slashes.
1025 */
1026 pInfo->cComponents = 0;
1027 pInfo->fDirSlash = false;
1028 while (pszSrc[0])
1029 {
1030 /* Sanity checks. */
1031 if (RTPATH_IS_SLASH(pszSrc[0])) /* can be relaxed if we care. */
1032 return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_CLEAN, pErrInfo,
1033 "The path is not clean of double slashes: '", pszPath, "'");
1034 if ( pszSrc[0] == '.'
1035 && pszSrc[1] == '.'
1036 && RTPATH_IS_SLASH(pszSrc[2]))
1037 return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pErrInfo,
1038 "The path is not absolute: '", pszPath, "'");
1039
1040 /* Record the start of the component. */
1041 if (pInfo->cComponents >= RT_ELEMENTS(pInfo->aoffComponents) - 1)
1042 return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_MANY_COMPONENTS, pErrInfo,
1043 "The path has too many components: '", pszPath, "'");
1044 pInfo->aoffComponents[pInfo->cComponents++] = pszDst - &pInfo->szPath[0];
1045
1046 /* Traverse to the end of the component, copying it as we go along. */
1047 while (pszSrc[0])
1048 {
1049 if (RTPATH_IS_SLASH(pszSrc[0]))
1050 {
1051 pszSrc++;
1052 if (*pszSrc)
1053 *pszDst++ = RTPATH_SLASH;
1054 else
1055 pInfo->fDirSlash = true;
1056 break;
1057 }
1058 *pszDst++ = *pszSrc++;
1059 if ((uintptr_t)(pszDst - &pInfo->szPath[0]) >= SUPR3HARDENED_MAX_PATH)
1060 return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_LONG, pErrInfo,
1061 "The path is too long: '", pszPath, "'");
1062 }
1063 }
1064
1065 /* Terminate the string and enter its length. */
1066 pszDst[0] = '\0';
1067 pszDst[1] = '\0'; /* for aoffComponents */
1068 pInfo->cch = (uint16_t)(pszDst - &pInfo->szPath[0]);
1069 pInfo->aoffComponents[pInfo->cComponents] = pInfo->cch + 1;
1070
1071 return VINF_SUCCESS;
1072}
1073
1074
1075/**
1076 * The state information collected by supR3HardenedVerifyFsObject.
1077 *
1078 * This can be used to verify that a directory we've opened for enumeration is
1079 * the same as the one that supR3HardenedVerifyFsObject just verified. It can
1080 * equally be used to verify a native specfied by the user.
1081 */
1082typedef struct SUPR3HARDENEDFSOBJSTATE
1083{
1084#ifdef RT_OS_WINDOWS
1085 /** Not implemented for windows yet. */
1086 char chTodo;
1087#else
1088 /** The stat output. */
1089 struct stat Stat;
1090#endif
1091} SUPR3HARDENEDFSOBJSTATE;
1092/** Pointer to a file system object state. */
1093typedef SUPR3HARDENEDFSOBJSTATE *PSUPR3HARDENEDFSOBJSTATE;
1094/** Pointer to a const file system object state. */
1095typedef SUPR3HARDENEDFSOBJSTATE const *PCSUPR3HARDENEDFSOBJSTATE;
1096
1097
1098/**
1099 * Query information about a file system object by path.
1100 *
1101 * @returns VBox status code, error buffer filled on failure.
1102 * @param pszPath The path to the object.
1103 * @param pFsObjState Where to return the state information.
1104 * @param pErrInfo The error info structure.
1105 */
1106static int supR3HardenedQueryFsObjectByPath(char const *pszPath, PSUPR3HARDENEDFSOBJSTATE pFsObjState, PRTERRINFO pErrInfo)
1107{
1108#if defined(RT_OS_WINDOWS)
1109 /** @todo Windows hardening. */
1110 pFsObjState->chTodo = 0;
1111 return VINF_SUCCESS;
1112
1113#else
1114 /*
1115 * Stat the object, do not follow links.
1116 */
1117 if (lstat(pszPath, &pFsObjState->Stat) != 0)
1118 {
1119 /* Ignore access errors */
1120 if (errno != EACCES)
1121 return supR3HardenedSetErrorN(VERR_SUPLIB_STAT_FAILED, pErrInfo,
1122 5, "stat failed with ", strerror(errno), " on: '", pszPath, "'");
1123 }
1124
1125 /*
1126 * Read ACLs.
1127 */
1128 /** @todo */
1129
1130 return VINF_SUCCESS;
1131#endif
1132}
1133
1134
1135/**
1136 * Query information about a file system object by native handle.
1137 *
1138 * @returns VBox status code, error buffer filled on failure.
1139 * @param hNative The native handle to the object @a pszPath
1140 * specifies and this should be verified to be the
1141 * same file system object.
1142 * @param pFsObjState Where to return the state information.
1143 * @param pszPath The path to the object. (For the error message
1144 * only.)
1145 * @param pErrInfo The error info structure.
1146 */
1147static int supR3HardenedQueryFsObjectByHandle(RTHCUINTPTR hNative, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
1148 char const *pszPath, PRTERRINFO pErrInfo)
1149{
1150#if defined(RT_OS_WINDOWS)
1151 /** @todo Windows hardening. */
1152 pFsObjState->chTodo = 0;
1153 return VINF_SUCCESS;
1154
1155#else
1156 /*
1157 * Stat the object, do not follow links.
1158 */
1159 if (fstat((int)hNative, &pFsObjState->Stat) != 0)
1160 return supR3HardenedSetErrorN(VERR_SUPLIB_STAT_FAILED, pErrInfo,
1161 5, "fstat failed with ", strerror(errno), " on '", pszPath, "'");
1162
1163 /*
1164 * Read ACLs.
1165 */
1166 /** @todo */
1167
1168 return VINF_SUCCESS;
1169#endif
1170}
1171
1172
1173/**
1174 * Verifies that the file system object indicated by the native handle is the
1175 * same as the one @a pFsObjState indicates.
1176 *
1177 * @returns VBox status code, error buffer filled on failure.
1178 * @param pFsObjState1 File system object information/state by path.
1179 * @param pFsObjState2 File system object information/state by handle.
1180 * @param pszPath The path to the object @a pFsObjState
1181 * describes. (For the error message.)
1182 * @param pErrInfo The error info structure.
1183 */
1184static int supR3HardenedIsSameFsObject(PCSUPR3HARDENEDFSOBJSTATE pFsObjState1, PCSUPR3HARDENEDFSOBJSTATE pFsObjState2,
1185 const char *pszPath, PRTERRINFO pErrInfo)
1186{
1187#if defined(RT_OS_WINDOWS)
1188 /** @todo Windows hardening. */
1189 return VINF_SUCCESS;
1190
1191#elif defined(RT_OS_OS2)
1192 return VINF_SUCCESS;
1193
1194#else
1195 /*
1196 * Compare the ino+dev, then the uid+gid and finally the important mode
1197 * bits. Technically the first one should be enough, but we're paranoid.
1198 */
1199 if ( pFsObjState1->Stat.st_ino != pFsObjState2->Stat.st_ino
1200 || pFsObjState1->Stat.st_dev != pFsObjState2->Stat.st_dev)
1201 return supR3HardenedSetError3(VERR_SUPLIB_NOT_SAME_OBJECT, pErrInfo,
1202 "The native handle is not the same as '", pszPath, "' (ino/dev)");
1203 if ( pFsObjState1->Stat.st_uid != pFsObjState2->Stat.st_uid
1204 || pFsObjState1->Stat.st_gid != pFsObjState2->Stat.st_gid)
1205 return supR3HardenedSetError3(VERR_SUPLIB_NOT_SAME_OBJECT, pErrInfo,
1206 "The native handle is not the same as '", pszPath, "' (uid/gid)");
1207 if ( (pFsObjState1->Stat.st_mode & (S_IFMT | S_IWUSR | S_IWGRP | S_IWOTH))
1208 != (pFsObjState2->Stat.st_mode & (S_IFMT | S_IWUSR | S_IWGRP | S_IWOTH)))
1209 return supR3HardenedSetError3(VERR_SUPLIB_NOT_SAME_OBJECT, pErrInfo,
1210 "The native handle is not the same as '", pszPath, "' (mode)");
1211 return VINF_SUCCESS;
1212#endif
1213}
1214
1215
1216/**
1217 * Verifies a file system object (file or directory).
1218 *
1219 * @returns VBox status code, error buffer filled on failure.
1220 * @param pFsObjState The file system object information/state to be
1221 * verified.
1222 * @param fDir Whether this is a directory or a file.
1223 * @param fRelaxed Whether we can be more relaxed about this
1224 * directory (only used for grand parent
1225 * directories).
1226 * @param pszPath The path to the object. For error messages and
1227 * securing a couple of hacks.
1228 * @param pErrInfo The error info structure.
1229 */
1230static int supR3HardenedVerifyFsObject(PCSUPR3HARDENEDFSOBJSTATE pFsObjState, bool fDir, bool fRelaxed,
1231 const char *pszPath, PRTERRINFO pErrInfo)
1232{
1233#if defined(RT_OS_WINDOWS)
1234 /** @todo Windows hardening. */
1235 NOREF(pFsObjState); NOREF(fDir); NOREF(fRelaxed); NOREF(pszPath); NOREF(pErrInfo);
1236 return VINF_SUCCESS;
1237
1238#elif defined(RT_OS_OS2)
1239 /* No hardening here - it's a single user system. */
1240 NOREF(pFsObjState); NOREF(fDir); NOREF(fRelaxed); NOREF(pszPath); NOREF(pErrInfo);
1241 return VINF_SUCCESS;
1242
1243#else
1244 /*
1245 * The owner must be root.
1246 *
1247 * This can be extended to include predefined system users if necessary.
1248 */
1249 if (pFsObjState->Stat.st_uid != 0)
1250 return supR3HardenedSetError3(VERR_SUPLIB_OWNER_NOT_ROOT, pErrInfo, "The owner is not root: '", pszPath, "'");
1251
1252 /*
1253 * The object type must be directory or file, no symbolic links or other
1254 * risky stuff (sorry dude, but we're paranoid on purpose here).
1255 */
1256 if ( !S_ISDIR(pFsObjState->Stat.st_mode)
1257 && !S_ISREG(pFsObjState->Stat.st_mode))
1258 {
1259 if (S_ISLNK(pFsObjState->Stat.st_mode))
1260 return supR3HardenedSetError3(VERR_SUPLIB_SYMLINKS_ARE_NOT_PERMITTED, pErrInfo,
1261 "Symlinks are not permitted: '", pszPath, "'");
1262 return supR3HardenedSetError3(VERR_SUPLIB_NOT_DIR_NOT_FILE, pErrInfo,
1263 "Not regular file or directory: '", pszPath, "'");
1264 }
1265 if (fDir != !!S_ISDIR(pFsObjState->Stat.st_mode))
1266 {
1267 if (S_ISDIR(pFsObjState->Stat.st_mode))
1268 return supR3HardenedSetError3(VERR_SUPLIB_IS_DIRECTORY, pErrInfo,
1269 "Expected file but found directory: '", pszPath, "'");
1270 return supR3HardenedSetError3(VERR_SUPLIB_IS_FILE, pErrInfo,
1271 "Expected directory but found file: '", pszPath, "'");
1272 }
1273
1274 /*
1275 * The group does not matter if it does not have write access, if it has
1276 * write access it must be group 0 (root/wheel/whatever).
1277 *
1278 * This can be extended to include predefined system groups or groups that
1279 * only root is member of.
1280 */
1281 if ( (pFsObjState->Stat.st_mode & S_IWGRP)
1282 && pFsObjState->Stat.st_gid != 0)
1283 {
1284#ifdef RT_OS_DARWIN
1285 /* HACK ALERT: On Darwin /Applications is root:admin with admin having
1286 full access. So, to work around we relax the hardening a bit and
1287 permit grand parents and beyond to be group writable by admin. */
1288 /** @todo dynamically resolve the admin group? */
1289 bool fBad = !fRelaxed || pFsObjState->Stat.st_gid != 80 /*admin*/ || suplibHardenedStrCmp(pszPath, "/Applications");
1290
1291#elif defined(RT_OS_FREEBSD)
1292 /* HACK ALERT: PC-BSD 9 has group-writable /usr/pib directory which is
1293 similar to /Applications on OS X (see above).
1294 On FreeBSD root is normally the only member of this group, on
1295 PC-BSD the default user is a member. */
1296 /** @todo dynamically resolve the operator group? */
1297 bool fBad = !fRelaxed || pFsObjState->Stat.st_gid != 5 /*operator*/ || suplibHardenedStrCmp(pszPath, "/usr/pbi");
1298 NOREF(fRelaxed);
1299#else
1300 NOREF(fRelaxed);
1301 bool fBad = true;
1302#endif
1303 if (fBad)
1304 return supR3HardenedSetError3(VERR_SUPLIB_WRITE_NON_SYS_GROUP, pErrInfo,
1305 "The group is not a system group and it has write access to '", pszPath, "'");
1306 }
1307
1308 /*
1309 * World must not have write access. There is no relaxing this rule.
1310 */
1311 if (pFsObjState->Stat.st_mode & S_IWOTH)
1312 return supR3HardenedSetError3(VERR_SUPLIB_WORLD_WRITABLE, pErrInfo,
1313 "World writable: '", pszPath, "'");
1314
1315 /*
1316 * Check the ACLs.
1317 */
1318 /** @todo */
1319
1320 return VINF_SUCCESS;
1321#endif
1322}
1323
1324
1325/**
1326 * Verifies that the file system object indicated by the native handle is the
1327 * same as the one @a pFsObjState indicates.
1328 *
1329 * @returns VBox status code, error buffer filled on failure.
1330 * @param hNative The native handle to the object @a pszPath
1331 * specifies and this should be verified to be the
1332 * same file system object.
1333 * @param pFsObjState The information/state returned by a previous
1334 * query call.
1335 * @param pszPath The path to the object @a pFsObjState
1336 * describes. (For the error message.)
1337 * @param pErrInfo The error info structure.
1338 */
1339static int supR3HardenedVerifySameFsObject(RTHCUINTPTR hNative, PCSUPR3HARDENEDFSOBJSTATE pFsObjState,
1340 const char *pszPath, PRTERRINFO pErrInfo)
1341{
1342 SUPR3HARDENEDFSOBJSTATE FsObjState2;
1343 int rc = supR3HardenedQueryFsObjectByHandle(hNative, &FsObjState2, pszPath, pErrInfo);
1344 if (RT_SUCCESS(rc))
1345 rc = supR3HardenedIsSameFsObject(pFsObjState, &FsObjState2, pszPath, pErrInfo);
1346 return rc;
1347}
1348
1349
1350/**
1351 * Does the recursive directory enumeration.
1352 *
1353 * @returns VBox status code, error buffer filled on failure.
1354 * @param pszDirPath The path buffer containing the subdirectory to
1355 * enumerate followed by a slash (this is never
1356 * the root slash). The buffer is RTPATH_MAX in
1357 * size and anything starting at @a cchDirPath
1358 * - 1 and beyond is scratch space.
1359 * @param cchDirPath The length of the directory path + slash.
1360 * @param pFsObjState Pointer to the file system object state buffer.
1361 * On input this will hold the stats for
1362 * the directory @a pszDirPath indicates and will
1363 * be used to verified that we're opening the same
1364 * thing.
1365 * @param fRecursive Whether to recurse into subdirectories.
1366 * @param pErrInfo The error info structure.
1367 */
1368static int supR3HardenedVerifyDirRecursive(char *pszDirPath, size_t cchDirPath, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
1369 bool fRecursive, PRTERRINFO pErrInfo)
1370{
1371#if defined(RT_OS_WINDOWS)
1372 /** @todo Windows hardening. */
1373 return VINF_SUCCESS;
1374
1375#elif defined(RT_OS_OS2)
1376 /* No hardening here - it's a single user system. */
1377 return VINF_SUCCESS;
1378
1379#else
1380 /*
1381 * Open the directory. Now, we could probably eliminate opendir here
1382 * and go down on kernel API level (open + getdents for instance), however
1383 * that's not very portable and hopefully not necessary.
1384 */
1385 DIR *pDir = opendir(pszDirPath);
1386 if (!pDir)
1387 {
1388 /* Ignore access errors. */
1389 if (errno == EACCES)
1390 return VINF_SUCCESS;
1391 return supR3HardenedSetErrorN(VERR_SUPLIB_DIR_ENUM_FAILED, pErrInfo,
1392 5, "opendir failed with ", strerror(errno), " on '", pszDirPath, "'");
1393 }
1394 if (dirfd(pDir) != -1)
1395 {
1396 int rc = supR3HardenedVerifySameFsObject(dirfd(pDir), pFsObjState, pszDirPath, pErrInfo);
1397 if (RT_FAILURE(rc))
1398 {
1399 closedir(pDir);
1400 return rc;
1401 }
1402 }
1403
1404 /*
1405 * Enumerate the directory, check all the requested bits.
1406 */
1407 int rc = VINF_SUCCESS;
1408 for (;;)
1409 {
1410 pszDirPath[cchDirPath] = '\0'; /* for error messages. */
1411
1412 struct dirent Entry;
1413 struct dirent *pEntry;
1414 int iErr = readdir_r(pDir, &Entry, &pEntry);
1415 if (iErr)
1416 {
1417 rc = supR3HardenedSetErrorN(VERR_SUPLIB_DIR_ENUM_FAILED, pErrInfo,
1418 5, "readdir_r failed with ", strerror(iErr), " in '", pszDirPath, "'");
1419 break;
1420 }
1421 if (!pEntry)
1422 break;
1423
1424 /*
1425 * Check the length and copy it into the path buffer so it can be
1426 * stat()'ed.
1427 */
1428 size_t cchName = suplibHardenedStrLen(pEntry->d_name);
1429 if (cchName + cchDirPath > SUPR3HARDENED_MAX_PATH)
1430 {
1431 rc = supR3HardenedSetErrorN(VERR_SUPLIB_PATH_TOO_LONG, pErrInfo,
1432 4, "Path grew too long during recursion: '", pszDirPath, pEntry->d_name, "'");
1433 break;
1434 }
1435 suplibHardenedMemCopy(&pszDirPath[cchName], pEntry->d_name, cchName + 1);
1436
1437 /*
1438 * Query the information about the entry and verify it.
1439 * (We don't bother skipping '.' and '..' at this point, a little bit
1440 * of extra checks doesn't hurt and neither requires relaxed handling.)
1441 */
1442 rc = supR3HardenedQueryFsObjectByPath(pszDirPath, pFsObjState, pErrInfo);
1443 if (RT_SUCCESS(rc))
1444 break;
1445 rc = supR3HardenedVerifyFsObject(pFsObjState, S_ISDIR(pFsObjState->Stat.st_mode), false /*fRelaxed*/,
1446 pszDirPath, pErrInfo);
1447 if (RT_FAILURE(rc))
1448 break;
1449
1450 /*
1451 * Recurse into subdirectories if requested.
1452 */
1453 if ( fRecursive
1454 && S_ISDIR(pFsObjState->Stat.st_mode)
1455 && suplibHardenedStrCmp(pEntry->d_name, ".")
1456 && suplibHardenedStrCmp(pEntry->d_name, ".."))
1457 {
1458 pszDirPath[cchDirPath + cchName] = RTPATH_SLASH;
1459 pszDirPath[cchDirPath + cchName + 1] = '\0';
1460
1461 rc = supR3HardenedVerifyDirRecursive(pszDirPath, cchDirPath + cchName + 1, pFsObjState,
1462 fRecursive, pErrInfo);
1463 if (RT_FAILURE(rc))
1464 break;
1465 }
1466 }
1467
1468 closedir(pDir);
1469 return VINF_SUCCESS;
1470#endif
1471}
1472
1473
1474/**
1475 * Worker for SUPR3HardenedVerifyDir.
1476 *
1477 * @returns See SUPR3HardenedVerifyDir.
1478 * @param pszDirPath See SUPR3HardenedVerifyDir.
1479 * @param fRecursive See SUPR3HardenedVerifyDir.
1480 * @param fCheckFiles See SUPR3HardenedVerifyDir.
1481 * @param pErrInfo See SUPR3HardenedVerifyDir.
1482 */
1483DECLHIDDEN(int) supR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo)
1484{
1485 /*
1486 * Validate the input path and parse it.
1487 */
1488 SUPR3HARDENEDPATHINFO Info;
1489 int rc = supR3HardenedVerifyPathSanity(pszDirPath, pErrInfo, &Info);
1490 if (RT_FAILURE(rc))
1491 return rc;
1492
1493 /*
1494 * Verify each component from the root up.
1495 */
1496 SUPR3HARDENEDFSOBJSTATE FsObjState;
1497 uint32_t const cComponents = Info.cComponents;
1498 for (uint32_t iComponent = 0; iComponent < cComponents; iComponent++)
1499 {
1500 bool fRelaxed = iComponent + 2 < cComponents;
1501 Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = '\0';
1502 rc = supR3HardenedQueryFsObjectByPath(Info.szPath, &FsObjState, pErrInfo);
1503 if (RT_SUCCESS(rc))
1504 rc = supR3HardenedVerifyFsObject(&FsObjState, true /*fDir*/, fRelaxed, Info.szPath, pErrInfo);
1505 if (RT_FAILURE(rc))
1506 return rc;
1507 Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = iComponent + 1 != cComponents ? RTPATH_SLASH : '\0';
1508 }
1509
1510 /*
1511 * Check files and subdirectories if requested.
1512 */
1513 if (fCheckFiles || fRecursive)
1514 {
1515 Info.szPath[Info.cch] = RTPATH_SLASH;
1516 Info.szPath[Info.cch + 1] = '\0';
1517 return supR3HardenedVerifyDirRecursive(Info.szPath, Info.cch + 1, &FsObjState,
1518 fRecursive, pErrInfo);
1519 }
1520
1521 return VINF_SUCCESS;
1522}
1523
1524
1525/**
1526 * Verfies a file.
1527 *
1528 * @returns VBox status code, error buffer filled on failure.
1529 * @param pszFilename The file to verify.
1530 * @param hNativeFile Handle to the file, verify that it's the same
1531 * as we ended up with when verifying the path.
1532 * RTHCUINTPTR_MAX means NIL here.
1533 * @param fMaybe3rdParty Set if the file is could be a supplied by a
1534 * third party. Different validation rules may
1535 * apply to 3rd party code on some platforms.
1536 * @param pErrInfo Where to return extended error information.
1537 * Optional.
1538 */
1539DECLHIDDEN(int) supR3HardenedVerifyFile(const char *pszFilename, RTHCUINTPTR hNativeFile,
1540 bool fMaybe3rdParty, PRTERRINFO pErrInfo)
1541{
1542 /*
1543 * Validate the input path and parse it.
1544 */
1545 SUPR3HARDENEDPATHINFO Info;
1546 int rc = supR3HardenedVerifyPathSanity(pszFilename, pErrInfo, &Info);
1547 if (RT_FAILURE(rc))
1548 return rc;
1549 if (Info.fDirSlash)
1550 return supR3HardenedSetError3(VERR_SUPLIB_IS_DIRECTORY, pErrInfo,
1551 "The file path specifies a directory: '", pszFilename, "'");
1552
1553 /*
1554 * Verify each component from the root up.
1555 */
1556 SUPR3HARDENEDFSOBJSTATE FsObjState;
1557 uint32_t const cComponents = Info.cComponents;
1558 for (uint32_t iComponent = 0; iComponent < cComponents; iComponent++)
1559 {
1560 bool fFinal = iComponent + 1 == cComponents;
1561 bool fRelaxed = iComponent + 2 < cComponents;
1562 Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = '\0';
1563 rc = supR3HardenedQueryFsObjectByPath(Info.szPath, &FsObjState, pErrInfo);
1564 if (RT_SUCCESS(rc))
1565 rc = supR3HardenedVerifyFsObject(&FsObjState, !fFinal /*fDir*/, fRelaxed, Info.szPath, pErrInfo);
1566 if (RT_FAILURE(rc))
1567 return rc;
1568 Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = !fFinal ? RTPATH_SLASH : '\0';
1569 }
1570
1571 /*
1572 * Verify the file handle against the last component, if specified.
1573 */
1574 if (hNativeFile != RTHCUINTPTR_MAX)
1575 {
1576 rc = supR3HardenedVerifySameFsObject(hNativeFile, &FsObjState, Info.szPath, pErrInfo);
1577 if (RT_FAILURE(rc))
1578 return rc;
1579 }
1580
1581#ifdef RT_OS_WINDOWS
1582 /*
1583 * The files shall be signed on windows, verify that.
1584 */
1585 rc = VINF_SUCCESS;
1586 HANDLE hVerify;
1587 if (hNativeFile == RTHCUINTPTR_MAX)
1588 {
1589 PRTUTF16 pwszPath;
1590 rc = RTStrToUtf16(pszFilename, &pwszPath);
1591 if (RT_SUCCESS(rc))
1592 {
1593 hVerify = CreateFileW(pwszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1594 RTUtf16Free(pwszPath);
1595 }
1596 else
1597 rc = RTErrInfoSetF(pErrInfo, rc, "Error converting '%s' to UTF-16: %Rrc", pszFilename, rc);
1598 }
1599 else if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)hNativeFile, GetCurrentProcess(), &hVerify,
1600 GENERIC_READ, false /*bInheritHandle*/, 0 /*dwOptions*/))
1601 hVerify = INVALID_HANDLE_VALUE;
1602 if (hVerify != INVALID_HANDLE_VALUE)
1603 {
1604# ifdef VBOX_WITH_HARDENING
1605 uint32_t fFlags = SUPHNTVI_F_REQUIRE_KERNEL_CODE_SIGNING;
1606 if (!fMaybe3rdParty)
1607 fFlags = SUPHNTVI_F_REQUIRE_BUILD_CERT;
1608 const char *pszSuffix = RTPathSuffix(pszFilename);
1609 if ( pszSuffix
1610 && pszSuffix[0] == '.'
1611 && ( RT_C_TO_LOWER(pszSuffix[1]) == 'r'
1612 || RT_C_TO_LOWER(pszSuffix[1]) == 'g')
1613 && RT_C_TO_LOWER(pszSuffix[2]) == 'c'
1614 && pszSuffix[3] == '\0' )
1615 fFlags |= SUPHNTVI_F_RC_IMAGE;
1616# ifndef IN_SUP_R3_STATIC /* Not in VBoxCpuReport and friends. */
1617 rc = supHardenedWinVerifyImageByHandleNoName(hVerify, fFlags, pErrInfo);
1618# endif
1619# endif
1620 CloseHandle(hVerify);
1621 }
1622 else if (RT_SUCCESS(rc))
1623 rc = RTErrInfoSetF(pErrInfo, RTErrConvertFromWin32(GetLastError()),
1624 "Error %u trying to open (or duplicate handle for) '%s'", GetLastError(), pszFilename);
1625 if (RT_FAILURE(rc))
1626 return rc;
1627#endif
1628
1629 return VINF_SUCCESS;
1630}
1631
1632
1633/**
1634 * Gets the pre-init data for the hand-over to the other version
1635 * of this code.
1636 *
1637 * The reason why we pass this information on is that it contains
1638 * open directories and files. Later it may include even more info
1639 * (int the verified arrays mostly).
1640 *
1641 * The receiver is supR3HardenedRecvPreInitData.
1642 *
1643 * @param pPreInitData Where to store it.
1644 */
1645DECLHIDDEN(void) supR3HardenedGetPreInitData(PSUPPREINITDATA pPreInitData)
1646{
1647 pPreInitData->cInstallFiles = RT_ELEMENTS(g_aSupInstallFiles);
1648 pPreInitData->paInstallFiles = &g_aSupInstallFiles[0];
1649 pPreInitData->paVerifiedFiles = &g_aSupVerifiedFiles[0];
1650
1651 pPreInitData->cVerifiedDirs = RT_ELEMENTS(g_aSupVerifiedDirs);
1652 pPreInitData->paVerifiedDirs = &g_aSupVerifiedDirs[0];
1653}
1654
1655
1656/**
1657 * Receives the pre-init data from the static executable stub.
1658 *
1659 * @returns VBox status code. Will not bitch on failure since the
1660 * runtime isn't ready for it, so that is left to the exe stub.
1661 *
1662 * @param pPreInitData The hand-over data.
1663 */
1664DECLHIDDEN(int) supR3HardenedRecvPreInitData(PCSUPPREINITDATA pPreInitData)
1665{
1666 /*
1667 * Compare the array lengths and the contents of g_aSupInstallFiles.
1668 */
1669 if ( pPreInitData->cInstallFiles != RT_ELEMENTS(g_aSupInstallFiles)
1670 || pPreInitData->cVerifiedDirs != RT_ELEMENTS(g_aSupVerifiedDirs))
1671 return VERR_VERSION_MISMATCH;
1672 SUPINSTFILE const *paInstallFiles = pPreInitData->paInstallFiles;
1673 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
1674 if ( g_aSupInstallFiles[iFile].enmDir != paInstallFiles[iFile].enmDir
1675 || g_aSupInstallFiles[iFile].enmType != paInstallFiles[iFile].enmType
1676 || g_aSupInstallFiles[iFile].fOptional != paInstallFiles[iFile].fOptional
1677 || suplibHardenedStrCmp(g_aSupInstallFiles[iFile].pszFile, paInstallFiles[iFile].pszFile))
1678 return VERR_VERSION_MISMATCH;
1679
1680 /*
1681 * Check that we're not called out of order.
1682 * If dynamic linking it screwed up, we may end up here.
1683 */
1684 if ( ASMMemIsAll8(&g_aSupVerifiedFiles[0], sizeof(g_aSupVerifiedFiles), 0) != NULL
1685 || ASMMemIsAll8(&g_aSupVerifiedDirs[0], sizeof(g_aSupVerifiedDirs), 0) != NULL)
1686 return VERR_WRONG_ORDER;
1687
1688 /*
1689 * Copy the verification data over.
1690 */
1691 suplibHardenedMemCopy(&g_aSupVerifiedFiles[0], pPreInitData->paVerifiedFiles, sizeof(g_aSupVerifiedFiles));
1692 suplibHardenedMemCopy(&g_aSupVerifiedDirs[0], pPreInitData->paVerifiedDirs, sizeof(g_aSupVerifiedDirs));
1693 return VINF_SUCCESS;
1694}
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