VirtualBox

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

Last change on this file since 17935 was 17794, checked in by vboxsync, 16 years ago

SUPR3/Hardend builds: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.4 KB
Line 
1/* $Id: SUPR3HardenedVerify.cpp 17794 2009-03-13 09:36:06Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Verification of Hardened Installation.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#if defined(RT_OS_OS2)
35# define INCL_BASE
36# define INCL_ERRORS
37# include <os2.h>
38# include <stdio.h>
39# include <stdlib.h>
40# include <unistd.h>
41# include <sys/fcntl.h>
42# include <sys/errno.h>
43# include <sys/syslimits.h>
44
45#elif defined(RT_OS_WINDOWS)
46# include <Windows.h>
47# include <stdio.h>
48
49#else /* UNIXes */
50# include <sys/types.h>
51# include <stdio.h>
52# include <stdlib.h>
53# include <dlfcn.h>
54# include <fcntl.h>
55# include <limits.h>
56# include <errno.h>
57# include <unistd.h>
58# include <sys/stat.h>
59# include <sys/time.h>
60# include <sys/fcntl.h>
61# include <stdio.h>
62# include <pwd.h>
63# ifdef RT_OS_DARWIN
64# include <mach-o/dyld.h>
65# endif
66
67#endif
68
69#include <VBox/sup.h>
70#include <VBox/err.h>
71#include <iprt/string.h>
72#include <iprt/param.h>
73
74#include "SUPLibInternal.h"
75
76
77
78
79/*******************************************************************************
80* Global Variables *
81*******************************************************************************/
82/**
83 * The files that gets verified.
84 *
85 * @todo This needs reviewing against the linux packages.
86 * @todo The excessive use of kSupID_SharedLib needs to be reviewed at some point. For
87 * the time being we're building the linux packages with SharedLib pointing to
88 * AppPrivArch (lazy bird).
89 */
90static SUPINSTFILE const g_aSupInstallFiles[] =
91{
92 /* type, dir,fOptional, pszFile */
93 /* ---------------------------------------------------------------------- */
94 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VMMR0.r0" },
95 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDDR0.r0" },
96 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDD2R0.r0" },
97
98 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VMMGC.gc" },
99 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDDGC.gc" },
100 { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDD2GC.gc" },
101
102 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxRT" SUPLIB_DLL_SUFF },
103 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxVMM" SUPLIB_DLL_SUFF },
104 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxREM" SUPLIB_DLL_SUFF },
105#if HC_ARCH_BITS == 32
106 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM32" SUPLIB_DLL_SUFF },
107 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM64" SUPLIB_DLL_SUFF },
108#endif
109 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD" SUPLIB_DLL_SUFF },
110 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD2" SUPLIB_DLL_SUFF },
111 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDDU" SUPLIB_DLL_SUFF },
112
113//#ifdef VBOX_WITH_DEBUGGER_GUI
114 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxDbg" SUPLIB_DLL_SUFF },
115 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxDbg3" SUPLIB_DLL_SUFF },
116//#endif
117
118//#ifdef VBOX_WITH_SHARED_CLIPBOARD
119 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedClipboard" SUPLIB_DLL_SUFF },
120//#endif
121//#ifdef VBOX_WITH_SHARED_FOLDERS
122 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedFolders" SUPLIB_DLL_SUFF },
123//#endif
124//#ifdef VBOX_WITH_GUEST_PROPS
125 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxGuestPropSvc" SUPLIB_DLL_SUFF },
126//#endif
127 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedCrOpenGL" SUPLIB_DLL_SUFF },
128 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLhostcrutil" SUPLIB_DLL_SUFF },
129 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLhosterrorspu" SUPLIB_DLL_SUFF },
130 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLrenderspu" SUPLIB_DLL_SUFF },
131
132 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxManage" SUPLIB_EXE_SUFF },
133
134#ifdef VBOX_WITH_MAIN
135 { kSupIFT_Exe, kSupID_AppBin, false, "VBoxSVC" SUPLIB_EXE_SUFF },
136 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxSettings" SUPLIB_DLL_SUFF },
137 #ifdef RT_OS_WINDOWS
138 { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxC" SUPLIB_DLL_SUFF },
139 #else
140 { kSupIFT_Exe, kSupID_AppPrivArch, false, "VBoxXPCOMIPCD" SUPLIB_EXE_SUFF },
141 { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxXPCOM" SUPLIB_DLL_SUFF },
142 { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxXPCOMIPCC" SUPLIB_DLL_SUFF },
143 { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxC" SUPLIB_DLL_SUFF },
144 { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxSVCM" SUPLIB_DLL_SUFF },
145 { kSupIFT_Data, kSupID_AppPrivArchComp, false, "VBoxXPCOMBase.xpt" },
146 #endif
147#endif
148
149//#ifdef VBOX_WITH_VRDP
150 { kSupIFT_Dll, kSupID_SharedLib, true, "VRDPAuth" SUPLIB_DLL_SUFF },
151 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxVRDP" SUPLIB_DLL_SUFF },
152//#endif
153
154//#ifdef VBOX_WITH_HEADLESS
155 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxHeadless" SUPLIB_EXE_SUFF },
156 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxHeadless" SUPLIB_DLL_SUFF },
157 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxFFmpegFB" SUPLIB_DLL_SUFF },
158//#endif
159
160//#ifdef VBOX_WITH_QTGUI
161 { kSupIFT_Exe, kSupID_AppBin, true, "VirtualBox" SUPLIB_EXE_SUFF },
162 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VirtualBox" SUPLIB_DLL_SUFF },
163# if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
164 { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxKeyboard" SUPLIB_DLL_SUFF },
165# endif
166//#endif
167
168//#ifdef VBOX_WITH_VBOXSDL
169 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxSDL" SUPLIB_EXE_SUFF },
170 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSDL" SUPLIB_DLL_SUFF },
171//#endif
172
173//#ifdef VBOX_WITH_VBOXBFE
174 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxBFE" SUPLIB_EXE_SUFF },
175 { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxBFE" SUPLIB_DLL_SUFF },
176//#endif
177
178//#ifdef VBOX_WITH_WEBSERVICES
179 { kSupIFT_Exe, kSupID_AppBin, true, "vboxwebsrv" SUPLIB_EXE_SUFF },
180//#endif
181
182#ifdef RT_OS_LINUX
183 { kSupIFT_Exe, kSupID_AppBin, true, "VBoxTunctl" SUPLIB_EXE_SUFF },
184#endif
185};
186
187
188/** Array parallel to g_aSupInstallFiles containing per-file status info. */
189static SUPVERIFIEDFILE g_aSupVerifiedFiles[RT_ELEMENTS(g_aSupInstallFiles)];
190
191/** Array index by install directory specifier containing info about verified directories. */
192static SUPVERIFIEDDIR g_aSupVerifiedDirs[kSupID_End];
193
194
195/**
196 * Assembles the path to a dirtory.
197 *
198 * @returns VINF_SUCCESS on success, some error code on failure (fFatal
199 * decides whether it returns or not).
200 *
201 * @param enmDir The directory.
202 * @param pszDst Where to assemble the path.
203 * @param cchDst The size of the buffer.
204 * @param fFatal Whether failures should be treated as fatal (true) or not (false).
205 */
206static int supR3HardenedMakePath(SUPINSTDIR enmDir, char *pszDst, size_t cchDst, bool fFatal)
207{
208 int rc;
209 switch (enmDir)
210 {
211 case kSupID_AppBin: /** @todo fix this AppBin crap (uncertain wtf some binaries actually are installed). */
212 case kSupID_Bin:
213 rc = supR3HardenedPathProgram(pszDst, cchDst);
214 break;
215 case kSupID_SharedLib:
216 rc = supR3HardenedPathSharedLibs(pszDst, cchDst);
217 break;
218 case kSupID_AppPrivArch:
219 rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
220 break;
221 case kSupID_AppPrivArchComp:
222 rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
223 if (RT_SUCCESS(rc))
224 {
225 size_t off = strlen(pszDst);
226 if (cchDst - off >= sizeof("/components"))
227 memcpy(&pszDst[off], "/components", sizeof("/components"));
228 else
229 rc = VERR_BUFFER_OVERFLOW;
230 }
231 break;
232 case kSupID_AppPrivNoArch:
233 rc = supR3HardenedPathAppPrivateNoArch(pszDst, cchDst);
234 break;
235 default:
236 return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
237 "supR3HardenedMakePath: enmDir=%d\n", enmDir);
238 }
239 if (RT_FAILURE(rc))
240 supR3HardenedError(rc, fFatal,
241 "supR3HardenedMakePath: enmDir=%d rc=%d\n", enmDir, rc);
242 return rc;
243}
244
245
246
247/**
248 * Assembles the path to a file table entry, with or without the actual filename.
249 *
250 * @returns VINF_SUCCESS on success, some error code on failure (fFatal
251 * decides whether it returns or not).
252 *
253 * @param pFile The file table entry.
254 * @param pszDst Where to assemble the path.
255 * @param cchDst The size of the buffer.
256 * @param fWithFilename If set, the filename is included, otherwise it is omitted (no trailing slash).
257 * @param fFatal Whether failures should be treated as fatal (true) or not (false).
258 */
259static int supR3HardenedMakeFilePath(PCSUPINSTFILE pFile, char *pszDst, size_t cchDst, bool fWithFilename, bool fFatal)
260{
261 /*
262 * Combine supR3HardenedMakePath and the filename.
263 */
264 int rc = supR3HardenedMakePath(pFile->enmDir, pszDst, cchDst, fFatal);
265 if (RT_SUCCESS(rc))
266 {
267 size_t cchFile = strlen(pFile->pszFile);
268 size_t off = strlen(pszDst);
269 if (cchDst - off >= cchFile + 2)
270 {
271 pszDst[off++] = '/';
272 memcpy(&pszDst[off], pFile->pszFile, cchFile + 1);
273 }
274 else
275 rc = supR3HardenedError(VERR_BUFFER_OVERFLOW, fFatal,
276 "supR3HardenedMakeFilePath: pszFile=%s off=%lu\n",
277 pFile->pszFile, (long)off);
278 }
279 return rc;
280}
281
282
283/**
284 * Verifies a directory.
285 *
286 * @returns VINF_SUCCESS on success. On failure, an error code is returned if
287 * fFatal is clear and if it's set the function wont return.
288 * @param enmDir The directory specifier.
289 * @param fFatal Whether validation failures should be treated as
290 * fatal (true) or not (false).
291 */
292DECLHIDDEN(int) supR3HardenedVerifyDir(SUPINSTDIR enmDir, bool fFatal)
293{
294 /*
295 * Validate the index just to be on the safe side...
296 */
297 if (enmDir <= kSupID_Invalid || enmDir >= kSupID_End)
298 return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
299 "supR3HardenedVerifyDir: enmDir=%d\n", enmDir);
300
301 /*
302 * Already validated?
303 */
304 if (g_aSupVerifiedDirs[enmDir].fValidated)
305 return VINF_SUCCESS; /** @todo revalidate? */
306
307 /* initialize the entry. */
308 if (g_aSupVerifiedDirs[enmDir].hDir != 0)
309 supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
310 "supR3HardenedVerifyDir: hDir=%p enmDir=%d\n",
311 (void *)g_aSupVerifiedDirs[enmDir].hDir, enmDir);
312 g_aSupVerifiedDirs[enmDir].hDir = -1;
313 g_aSupVerifiedDirs[enmDir].fValidated = false;
314
315 /*
316 * Make the path and open the directory.
317 */
318 char szPath[RTPATH_MAX];
319 int rc = supR3HardenedMakePath(enmDir, szPath, sizeof(szPath), fFatal);
320 if (RT_SUCCESS(rc))
321 {
322#if defined(RT_OS_WINDOWS)
323 HANDLE hDir = CreateFile(szPath,
324 GENERIC_READ,
325 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
326 NULL,
327 OPEN_ALWAYS,
328 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
329 NULL);
330 if (hDir != INVALID_HANDLE_VALUE)
331 {
332 /** @todo check the type */
333 /* That's all on windows, for now at least... */
334 g_aSupVerifiedDirs[enmDir].hDir = (intptr_t)hDir;
335 g_aSupVerifiedDirs[enmDir].fValidated = true;
336 }
337 else
338 {
339 int err = GetLastError();
340 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
341 "supR3HardenedVerifyDir: Failed to open \"%s\": err=%d\n",
342 szPath, err);
343 }
344#else /* UNIXY */
345 int fd = open(szPath, O_RDONLY, 0);
346 if (fd >= 0)
347 {
348 /*
349 * On unixy systems we'll make sure the directory is owned by root
350 * and not writable by the group and user.
351 */
352 struct stat st;
353 if (!fstat(fd, &st))
354 {
355
356 if ( st.st_uid == 0
357 && !(st.st_mode & (S_IWGRP | S_IWOTH))
358 && S_ISDIR(st.st_mode))
359 {
360 g_aSupVerifiedDirs[enmDir].hDir = fd;
361 g_aSupVerifiedDirs[enmDir].fValidated = true;
362 }
363 else
364 {
365 if (!S_ISDIR(st.st_mode))
366 rc = supR3HardenedError(VERR_NOT_A_DIRECTORY, fFatal,
367 "supR3HardenedVerifyDir: \"%s\" is not a directory\n",
368 szPath, (long)st.st_uid);
369 else if (st.st_uid)
370 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
371 "supR3HardenedVerifyDir: Cannot trust the directory \"%s\": not owned by root (st_uid=%ld)\n",
372 szPath, (long)st.st_uid);
373 else
374 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
375 "supR3HardenedVerifyDir: Cannot trust the directory \"%s\": group and/or other writable (st_mode=0%lo)\n",
376 szPath, (long)st.st_mode);
377 close(fd);
378 }
379 }
380 else
381 {
382 int err = errno;
383 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
384 "supR3HardenedVerifyDir: Failed to fstat \"%s\": %s (%d)\n",
385 szPath, strerror(err), err);
386 close(fd);
387 }
388 }
389 else
390 {
391 int err = errno;
392 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
393 "supR3HardenedVerifyDir: Failed to open \"%s\": %s (%d)\n",
394 szPath, strerror(err), err);
395 }
396#endif /* UNIXY */
397 }
398
399 return rc;
400}
401
402
403/**
404 * Verifies a file entry.
405 *
406 * @returns VINF_SUCCESS on success. On failure, an error code is returned if
407 * fFatal is clear and if it's set the function wont return.
408 *
409 * @param iFile The file table index of the file to be verified.
410 * @param fFatal Whether validation failures should be treated as
411 * fatal (true) or not (false).
412 * @param fLeaveFileOpen Whether the file should be left open.
413 */
414static int supR3HardenedVerifyFileInternal(int iFile, bool fFatal, bool fLeaveFileOpen)
415{
416 PCSUPINSTFILE pFile = &g_aSupInstallFiles[iFile];
417 PSUPVERIFIEDFILE pVerified = &g_aSupVerifiedFiles[iFile];
418
419 /*
420 * Already done?
421 */
422 if (pVerified->fValidated)
423 return VINF_SUCCESS; /** @todo revalidate? */
424
425
426 /* initialize the entry. */
427 if (pVerified->hFile != 0)
428 supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
429 "supR3HardenedVerifyFileInternal: hFile=%p (%s)\n",
430 (void *)pVerified->hFile, pFile->pszFile);
431 pVerified->hFile = -1;
432 pVerified->fValidated = false;
433
434 /*
435 * Verify the directory then proceed to open it.
436 * (This'll make sure the directory is opened and that we can (later)
437 * use openat if we wish.)
438 */
439 int rc = supR3HardenedVerifyDir(pFile->enmDir, fFatal);
440 if (RT_SUCCESS(rc))
441 {
442 char szPath[RTPATH_MAX];
443 int rc = supR3HardenedMakeFilePath(pFile, szPath, sizeof(szPath), true, fFatal);
444 if (RT_SUCCESS(rc))
445 {
446#if defined(RT_OS_WINDOWS)
447 HANDLE hFile = CreateFile(szPath,
448 GENERIC_READ,
449 FILE_SHARE_READ,
450 NULL,
451 OPEN_ALWAYS,
452 FILE_ATTRIBUTE_NORMAL,
453 NULL);
454 if (hFile != INVALID_HANDLE_VALUE)
455 {
456 /** @todo Check the type, and verify the signature (separate function so we can skip it). */
457 {
458 /* it's valid. */
459 if (fLeaveFileOpen)
460 pVerified->hFile = (intptr_t)hFile;
461 else
462 CloseHandle(hFile);
463 pVerified->fValidated = true;
464 }
465 }
466 else
467 {
468 int err = GetLastError();
469 if (!pFile->fOptional || err != ERROR_FILE_NOT_FOUND)
470 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
471 "supR3HardenedVerifyFileInternal: Failed to open \"%s\": err=%d\n",
472 szPath, err);
473 }
474#else /* UNIXY */
475 int fd = open(szPath, O_RDONLY, 0);
476 if (fd >= 0)
477 {
478 /*
479 * On unixy systems we'll make sure the directory is owned by root
480 * and not writable by the group and user.
481 */
482 struct stat st;
483 if (!fstat(fd, &st))
484 {
485 if ( st.st_uid == 0
486 && !(st.st_mode & (S_IWGRP | S_IWOTH))
487 && S_ISREG(st.st_mode))
488 {
489 /* it's valid. */
490 if (fLeaveFileOpen)
491 pVerified->hFile = fd;
492 else
493 close(fd);
494 pVerified->fValidated = true;
495 }
496 else
497 {
498 if (!S_ISREG(st.st_mode))
499 rc = supR3HardenedError(VERR_IS_A_DIRECTORY, fFatal,
500 "supR3HardenedVerifyFileInternal: \"%s\" is not a regular file\n",
501 szPath, (long)st.st_uid);
502 else if (st.st_uid)
503 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
504 "supR3HardenedVerifyFileInternal: Cannot trust the file \"%s\": not owned by root (st_uid=%ld)\n",
505 szPath, (long)st.st_uid);
506 else
507 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
508 "supR3HardenedVerifyFileInternal: Cannot trust the file \"%s\": group and/or other writable (st_mode=0%lo)\n",
509 szPath, (long)st.st_mode);
510 close(fd);
511 }
512 }
513 else
514 {
515 int err = errno;
516 rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
517 "supR3HardenedVerifyFileInternal: Failed to fstat \"%s\": %s (%d)\n",
518 szPath, strerror(err), err);
519 close(fd);
520 }
521 }
522 else
523 {
524 int err = errno;
525 if (!pFile->fOptional || err != ENOENT)
526 rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
527 "supR3HardenedVerifyFileInternal: Failed to open \"%s\": %s (%d)\n",
528 szPath, strerror(err), err);
529 }
530#endif /* UNIXY */
531 }
532 }
533
534 return rc;
535}
536
537
538/**
539 * Verifies that the specified table entry matches the given filename.
540 *
541 * @returns VINF_SUCCESS if matching. On mismatch fFatal indicates whether an
542 * error is returned or we terminate the application.
543 *
544 * @param iFile The file table index.
545 * @param pszFilename The filename.
546 * @param fFatal Whether validation failures should be treated as
547 * fatal (true) or not (false).
548 */
549static int supR3HardenedVerifySameFile(int iFile, const char *pszFilename, bool fFatal)
550{
551 PCSUPINSTFILE pFile = &g_aSupInstallFiles[iFile];
552
553 /*
554 * Construct the full path for the file table entry
555 * and compare it with the specified file.
556 */
557 char szName[RTPATH_MAX];
558 int rc = supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
559 if (RT_FAILURE(rc))
560 return rc;
561#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
562 if (stricmp(szName, pszFilename))
563#else
564 if (strcmp(szName, pszFilename))
565#endif
566 {
567 /*
568 * Normalize the two paths and compare again.
569 */
570 rc = VERR_NOT_SAME_DEVICE;
571#if defined(RT_OS_WINDOWS)
572 LPSTR pszIgnored;
573 char szName2[RTPATH_MAX];
574 if ( GetFullPathName(szName, RT_ELEMENTS(szName2), &szName2[0], &pszIgnored)
575 && GetFullPathName(pszFilename, RT_ELEMENTS(szName), &szName[0], &pszIgnored))
576 if (!stricmp(szName2, szName))
577 rc = VINF_SUCCESS;
578#else
579 AssertCompile(RTPATH_MAX >= PATH_MAX);
580 char szName2[RTPATH_MAX];
581 if ( realpath(szName, szName2) != NULL
582 && realpath(pszFilename, szName) != NULL)
583 if (!strcmp(szName2, szName))
584 rc = VINF_SUCCESS;
585#endif
586
587 if (RT_FAILURE(rc))
588 {
589 supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
590 return supR3HardenedError(rc, fFatal,
591 "supR3HardenedVerifySameFile: \"%s\" isn't the same as \"%s\"\n",
592 pszFilename, szName);
593 }
594 }
595
596 /*
597 * Check more stuff like the stat info if it's an already open file?
598 */
599
600
601
602 return VINF_SUCCESS;
603}
604
605
606/**
607 * Verifies a file.
608 *
609 * @returns VINF_SUCCESS on success.
610 * VERR_NOT_FOUND if the file isn't in the table, this isn't ever a fatal error.
611 * On verfication failure, an error code will be returned when fFatal is clear,
612 * otherwise the program will be termindated.
613 *
614 * @param pszFilename The filename.
615 * @param fFatal Whether validation failures should be treated as
616 * fatal (true) or not (false).
617 */
618DECLHIDDEN(int) supR3HardenedVerifyFile(const char *pszFilename, bool fFatal)
619{
620 /*
621 * Lookup the file and check if it's the same file.
622 */
623 const char *pszName = supR3HardenedPathFilename(pszFilename);
624 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
625 if (!strcmp(pszName, g_aSupInstallFiles[iFile].pszFile))
626 {
627 int rc = supR3HardenedVerifySameFile(iFile, pszFilename, fFatal);
628 if (RT_SUCCESS(rc))
629 rc = supR3HardenedVerifyFileInternal(iFile, fFatal, false /* fLeaveFileOpen */);
630 return rc;
631 }
632
633 return VERR_NOT_FOUND;
634}
635
636
637/**
638 * Verifies a program, worker for supR3HardenedVerifyAll.
639 *
640 * @returns See supR3HardenedVerifyAll.
641 * @param pszProgName See supR3HardenedVerifyAll.
642 * @param fFatal See supR3HardenedVerifyAll.
643 */
644static int supR3HardenedVerifyProgram(const char *pszProgName, bool fFatal)
645{
646 /*
647 * Search the table looking for the executable and the DLL/DYLIB/SO.
648 */
649 int rc = VINF_SUCCESS;
650 bool fExe = false;
651 bool fDll = false;
652 size_t const cchProgName = strlen(pszProgName);
653 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
654 if (!strncmp(pszProgName, g_aSupInstallFiles[iFile].pszFile, cchProgName))
655 {
656 if ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Dll
657 && !strcmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_DLL_SUFF))
658 {
659 /* This only has to be found (once). */
660 if (fDll)
661 rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
662 "supR3HardenedVerifyProgram: duplicate DLL entry for \"%s\"\n", pszProgName);
663 fDll = true;
664 }
665 else if ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Exe
666 && !strcmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_EXE_SUFF))
667 {
668 /* Here we'll have to check that the specific program is the same as the entry. */
669 if (fExe)
670 rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
671 "supR3HardenedVerifyProgram: duplicate EXE entry for \"%s\"\n", pszProgName);
672 fExe = true;
673
674 char szFilename[RTPATH_MAX];
675 int rc2 = supR3HardenedPathProgram(szFilename, sizeof(szFilename) - cchProgName - sizeof(SUPLIB_EXE_SUFF));
676 if (RT_SUCCESS(rc2))
677 {
678 strcat(szFilename, "/");
679 strcat(szFilename, g_aSupInstallFiles[iFile].pszFile);
680 supR3HardenedVerifySameFile(iFile, szFilename, fFatal);
681 }
682 else
683 rc = supR3HardenedError(rc2, fFatal,
684 "supR3HardenedVerifyProgram: failed to query program path: rc=%d\n", rc2);
685 }
686 }
687
688 /*
689 * Check the findings.
690 */
691 if (!fDll && !fExe)
692 rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
693 "supR3HardenedVerifyProgram: Couldn't find the program \"%s\"\n", pszProgName);
694 else if (!fExe)
695 rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
696 "supR3HardenedVerifyProgram: Couldn't find the EXE entry for \"%s\"\n", pszProgName);
697 else if (!fDll)
698 rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
699 "supR3HardenedVerifyProgram: Couldn't find the DLL entry for \"%s\"\n", pszProgName);
700 return rc;
701}
702
703
704
705/**
706 * Verifies all the known files.
707 *
708 * @returns VINF_SUCCESS on success.
709 * On verfication failure, an error code will be returned when fFatal is clear,
710 * otherwise the program will be termindated.
711 *
712 * @param fFatal Whether validation failures should be treated as
713 * fatal (true) or not (false).
714 * @param fLeaveFilesOpen If set, all the verfied files are left open.
715 * @param pszProgName Optional program name. This is used by SUPR3HardenedMain
716 * to verify that both the executable and corresponding
717 * DLL/DYLIB/SO are valid.
718 */
719DECLHIDDEN(int) supR3HardenedVerifyAll(bool fFatal, bool fLeaveFilesOpen, const char *pszProgName)
720{
721 /*
722 * The verify all the files.
723 */
724 int rc = VINF_SUCCESS;
725 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
726 {
727 int rc2 = supR3HardenedVerifyFileInternal(iFile, fFatal, fLeaveFilesOpen);
728 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
729 rc = rc2;
730 }
731
732 /*
733 * Verify the program name if specified, that is to say, just check that
734 * it's in the table (=> we've already verified it).
735 */
736 if (pszProgName)
737 {
738 int rc2 = supR3HardenedVerifyProgram(pszProgName, fFatal);
739 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
740 rc2 = rc;
741 }
742
743 return rc;
744}
745
746
747/**
748 * Gets the pre-init data for the hand-over to the other version
749 * of this code.
750 *
751 * The reason why we pass this information on is that it contains
752 * open directories and files. Later it may include even more info
753 * (int the verified arrays mostly).
754 *
755 * The receiver is supR3HardenedRecvPreInitData.
756 *
757 * @param pPreInitData Where to store it.
758 */
759DECLHIDDEN(void) supR3HardenedGetPreInitData(PSUPPREINITDATA pPreInitData)
760{
761 pPreInitData->cInstallFiles = RT_ELEMENTS(g_aSupInstallFiles);
762 pPreInitData->paInstallFiles = &g_aSupInstallFiles[0];
763 pPreInitData->paVerifiedFiles = &g_aSupVerifiedFiles[0];
764
765 pPreInitData->cVerifiedDirs = RT_ELEMENTS(g_aSupVerifiedDirs);
766 pPreInitData->paVerifiedDirs = &g_aSupVerifiedDirs[0];
767}
768
769
770/**
771 * Receives the pre-init data from the static executable stub.
772 *
773 * @returns VBox status code. Will not bitch on failure since the
774 * runtime isn't ready for it, so that is left to the exe stub.
775 *
776 * @param pPreInitData The hand-over data.
777 */
778DECLHIDDEN(int) supR3HardenedRecvPreInitData(PCSUPPREINITDATA pPreInitData)
779{
780 /*
781 * Compare the array lengths and the contents of g_aSupInstallFiles.
782 */
783 if ( pPreInitData->cInstallFiles != RT_ELEMENTS(g_aSupInstallFiles)
784 || pPreInitData->cVerifiedDirs != RT_ELEMENTS(g_aSupVerifiedDirs))
785 return VERR_VERSION_MISMATCH;
786 SUPINSTFILE const *paInstallFiles = pPreInitData->paInstallFiles;
787 for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
788 if ( g_aSupInstallFiles[iFile].enmDir != paInstallFiles[iFile].enmDir
789 || g_aSupInstallFiles[iFile].enmType != paInstallFiles[iFile].enmType
790 || g_aSupInstallFiles[iFile].fOptional != paInstallFiles[iFile].fOptional
791 || strcmp(g_aSupInstallFiles[iFile].pszFile, paInstallFiles[iFile].pszFile))
792 return VERR_VERSION_MISMATCH;
793
794 /*
795 * Check that we're not called out of order.
796 * If dynamic linking it screwed up, we may end up here.
797 */
798 if ( ASMMemIsAll8(&g_aSupVerifiedFiles[0], sizeof(g_aSupVerifiedFiles), 0) != NULL
799 || ASMMemIsAll8(&g_aSupVerifiedDirs[0], sizeof(g_aSupVerifiedDirs), 0) != NULL)
800 return VERR_WRONG_ORDER;
801
802 /*
803 * Copy the verification data over.
804 */
805 memcpy(&g_aSupVerifiedFiles[0], pPreInitData->paVerifiedFiles, sizeof(g_aSupVerifiedFiles));
806 memcpy(&g_aSupVerifiedDirs[0], pPreInitData->paVerifiedDirs, sizeof(g_aSupVerifiedDirs));
807 return VINF_SUCCESS;
808}
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