SUPR3HardenedVerify.cpp revision 79fe674227c3f2b82ab22a6b7d340283b610fb83
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox Support Library - Verification of Hardened Installation.
f8e804dad2cc0262b6384e97c12be107cf7e19e0vboxsync * Copyright (C) 2006-2010 Oracle Corporation
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * available from http://www.virtualbox.org. This file is free software;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * you can redistribute it and/or modify it under the terms of the GNU
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * General Public License (GPL) as published by the Free Software
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The contents of this file may alternatively be used under the terms
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * of the Common Development and Distribution License Version 1.0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox OSE distribution, in which case the provisions of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * CDDL are applicable instead of those of the GPL.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * You may elect to license modified versions of this file under the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * terms and conditions of either the GPL or the CDDL or both.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Header Files *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else /* UNIXes */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/*******************************************************************************
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync* Defined Constants And Macros *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync*******************************************************************************/
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/** The max path length acceptable for a trusted path. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Global Variables *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The files that gets verified.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @todo This needs reviewing against the linux packages.
54d3b0107d9bf326fe6e0de92e012c791dbb1587vboxsync * @todo The excessive use of kSupID_SharedLib needs to be reviewed at some point. For
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * the time being we're building the linux packages with SharedLib pointing to
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * AppPrivArch (lazy bird).
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync /* type, dir, fOpt, "pszFile" */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* ---------------------------------------------------------------------- */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VMMR0.r0" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDDR0.r0" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDD2R0.r0" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VMMGC.gc" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDDGC.gc" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDD2GC.gc" },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxRT" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxVMM" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxREM" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM32" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM64" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD2" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDDU" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_DEBUGGER_GUI
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxDbg" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxDbg3" SUPLIB_DLL_SUFF },
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#ifdef VBOX_WITH_SHARED_CLIPBOARD
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedClipboard" SUPLIB_DLL_SUFF },
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#ifdef VBOX_WITH_SHARED_FOLDERS
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedFolders" SUPLIB_DLL_SUFF },
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#ifdef VBOX_WITH_GUEST_PROPS
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxGuestPropSvc" SUPLIB_DLL_SUFF },
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync//#ifdef VBOX_WITH_GUEST_CONTROL
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxGuestControlSvc" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedCrOpenGL" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLhostcrutil" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLhosterrorspu" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxOGLrenderspu" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxManage" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, false, "VBoxSVC" SUPLIB_EXE_SUFF },
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxC" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppPrivArch, false, "VBoxXPCOMIPCD" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxXPCOM" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxXPCOMIPCC" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxC" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxSVCM" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Data, kSupID_AppPrivArchComp, false, "VBoxXPCOMBase.xpt" },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VRDPAuth" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxVRDP" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_HEADLESS
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxHeadless" SUPLIB_EXE_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxHeadless" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxFFmpegFB" SUPLIB_DLL_SUFF },
8e0c2ca3abd721979958f95b9af73b60665478c8vboxsync//#ifdef VBOX_WITH_QTGUI
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VirtualBox" SUPLIB_EXE_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VirtualBox" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxKeyboard" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_VBOXSDL
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxSDL" SUPLIB_EXE_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSDL" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_VBOXBFE
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxBFE" SUPLIB_EXE_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxBFE" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_WEBSERVICES
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "vboxwebsrv" SUPLIB_EXE_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxTunctl" SUPLIB_EXE_SUFF },
39592d8ff3243f6116c4e99be391bcf30a4ad187vboxsync//#ifdef VBOX_WITH_NETFLT
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxNetDHCP" SUPLIB_EXE_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxNetDHCP" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Array parallel to g_aSupInstallFiles containing per-file status info. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic SUPVERIFIEDFILE g_aSupVerifiedFiles[RT_ELEMENTS(g_aSupInstallFiles)];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Array index by install directory specifier containing info about verified directories. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic SUPVERIFIEDDIR g_aSupVerifiedDirs[kSupID_End];
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Assembles the path to a directory.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success, some error code on failure (fFatal
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * decides whether it returns or not).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param enmDir The directory.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszDst Where to assemble the path.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param cchDst The size of the buffer.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether failures should be treated as fatal (true) or not (false).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedMakePath(SUPINSTDIR enmDir, char *pszDst, size_t cchDst, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync case kSupID_AppBin: /** @todo fix this AppBin crap (uncertain wtf some binaries actually are installed). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync memcpy(&pszDst[off], "/components", sizeof("/components"));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathAppPrivateNoArch(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedMakePath: enmDir=%d rc=%d\n", enmDir, rc);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Assembles the path to a file table entry, with or without the actual filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success, some error code on failure (fFatal
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * decides whether it returns or not).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pFile The file table entry.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszDst Where to assemble the path.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param cchDst The size of the buffer.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fWithFilename If set, the filename is included, otherwise it is omitted (no trailing slash).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether failures should be treated as fatal (true) or not (false).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedMakeFilePath(PCSUPINSTFILE pFile, char *pszDst, size_t cchDst, bool fWithFilename, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Combine supR3HardenedMakePath and the filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedMakePath(pFile->enmDir, pszDst, cchDst, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_BUFFER_OVERFLOW, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedMakeFilePath: pszFile=%s off=%lu\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a directory.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success. On failure, an error code is returned if
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fFatal is clear and if it's set the function wont return.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param enmDir The directory specifier.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncDECLHIDDEN(int) supR3HardenedVerifyFixedDir(SUPINSTDIR enmDir, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Validate the index just to be on the safe side...
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (enmDir <= kSupID_Invalid || enmDir >= kSupID_End)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Already validated?
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* initialize the entry. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: hDir=%p enmDir=%d\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Make the path and open the directory.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedMakePath(enmDir, szPath, sizeof(szPath), fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo check the type */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* That's all on windows, for now at least... */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Failed to open \"%s\": err=%d\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On unixy systems we'll make sure the directory is owned by root
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and not writable by the group and user.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_NOT_A_DIRECTORY, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: \"%s\" is not a directory\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Cannot trust the directory \"%s\": not owned by root (st_uid=%ld)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Cannot trust the directory \"%s\": group and/or other writable (st_mode=0%lo)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Failed to fstat \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Failed to open \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a file entry.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success. On failure, an error code is returned if
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fFatal is clear and if it's set the function wont return.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param iFile The file table index of the file to be verified.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fLeaveFileOpen Whether the file should be left open.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedVerifyFileInternal(int iFile, bool fFatal, bool fLeaveFileOpen)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PSUPVERIFIEDFILE pVerified = &g_aSupVerifiedFiles[iFile];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Already done?
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* initialize the entry. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: hFile=%p (%s)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verify the directory then proceed to open it.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * (This'll make sure the directory is opened and that we can (later)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * use openat if we wish.)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = supR3HardenedVerifyFixedDir(pFile->enmDir, fFatal);
bac59dd15f093cbb8dae97ebd8f94f94786d1439vboxsync rc = supR3HardenedMakeFilePath(pFile, szPath, sizeof(szPath), true, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo Check the type, and verify the signature (separate function so we can skip it). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* it's valid. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!pFile->fOptional || err != ERROR_FILE_NOT_FOUND)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Failed to open \"%s\": err=%d\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On unixy systems we'll make sure the directory is owned by root
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and not writable by the group and user.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* it's valid. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_IS_A_DIRECTORY, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: \"%s\" is not a regular file\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Cannot trust the file \"%s\": not owned by root (st_uid=%ld)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Cannot trust the file \"%s\": group and/or other writable (st_mode=0%lo)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Failed to fstat \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Failed to open \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies that the specified table entry matches the given filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS if matching. On mismatch fFatal indicates whether an
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * error is returned or we terminate the application.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param iFile The file table index.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFilename The filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedVerifySameFile(int iFile, const char *pszFilename, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Construct the full path for the file table entry
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and compare it with the specified file.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Normalize the two paths and compare again.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( GetFullPathName(szName, RT_ELEMENTS(szName2), &szName2[0], &pszIgnored)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && GetFullPathName(pszFilename, RT_ELEMENTS(szName), &szName[0], &pszIgnored))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifySameFile: \"%s\" isn't the same as \"%s\"\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check more stuff like the stat info if it's an already open file?
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a file.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VERR_NOT_FOUND if the file isn't in the table, this isn't ever a fatal error.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * On verification failure, an error code will be returned when fFatal is clear,
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * otherwise the program will be terminated.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFilename The filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncDECLHIDDEN(int) supR3HardenedVerifyFixedFile(const char *pszFilename, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Lookup the file and check if it's the same file.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync const char *pszName = supR3HardenedPathFilename(pszFilename);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!strcmp(pszName, g_aSupInstallFiles[iFile].pszFile))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedVerifySameFile(iFile, pszFilename, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedVerifyFileInternal(iFile, fFatal, false /* fLeaveFileOpen */);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a program, worker for supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns See supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszProgName See supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal See supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedVerifyProgram(const char *pszProgName, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Search the table looking for the executable and the DLL/DYLIB/SO.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync bool fExe = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync bool fDll = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!strncmp(pszProgName, g_aSupInstallFiles[iFile].pszFile, cchProgName))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Dll
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && !strcmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_DLL_SUFF))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* This only has to be found (once). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: duplicate DLL entry for \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Exe
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && !strcmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_EXE_SUFF))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Here we'll have to check that the specific program is the same as the entry. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: duplicate EXE entry for \"%s\"\n", pszProgName);
c77e7bff89c7639353778366984d51ff165ea0e3vboxsync int rc2 = supR3HardenedPathExecDir(szFilename, sizeof(szFilename) - cchProgName - sizeof(SUPLIB_EXE_SUFF));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync strcat(szFilename, g_aSupInstallFiles[iFile].pszFile);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supR3HardenedVerifySameFile(iFile, szFilename, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: failed to query program path: rc=%d\n", rc2);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check the findings.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: Couldn't find the program \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: Couldn't find the EXE entry for \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: Couldn't find the DLL entry for \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies all the known files.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * On verification failure, an error code will be returned when fFatal is clear,
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * otherwise the program will be terminated.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * @param fLeaveFilesOpen If set, all the verified files are left open.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszProgName Optional program name. This is used by SUPR3HardenedMain
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * to verify that both the executable and corresponding
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * DLL/DYLIB/SO are valid.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(int) supR3HardenedVerifyAll(bool fFatal, bool fLeaveFilesOpen, const char *pszProgName)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The verify all the files.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc2 = supR3HardenedVerifyFileInternal(iFile, fFatal, fLeaveFilesOpen);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verify the program name if specified, that is to say, just check that
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * it's in the table (=> we've already verified it).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc2 = supR3HardenedVerifyProgram(pszProgName, fFatal);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Copies the N messages into the error buffer and returns @a rc.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns Returns @a rc
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param rc The return code.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cMsgs The number of messages in the ellipsis.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param ... Message parts.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetErrorN(int rc, char *pszErr, size_t cbErr, unsigned cMsgs, ...)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync size_t cchMsg = VALID_PTR(pszMsg) ? strlen(pszMsg) : 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Copies the three messages into the error buffer and returns @a rc.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns Returns @a rc
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param rc The return code.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszMsg1 The first message part.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszMsg2 The second message part.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszMsg3 The third message part.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetError3(int rc, char *pszErr, size_t cbErr, const char *pszMsg1,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(rc, pszErr, cbErr, 3, pszMsg1, pszMsg2, pszMsg3);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Copies the two messages into the error buffer and returns @a rc.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns Returns @a rc
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param rc The return code.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszMsg1 The first message part.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszMsg2 The second message part.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetError2(int rc, char *pszErr, size_t cbErr, const char *pszMsg1,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszMsg2)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(rc, pszErr, cbErr, 2, pszMsg1, pszMsg2);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Copies the error message to the error buffer and returns @a rc.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns Returns @a rc
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param rc The return code.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr The error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr The size of the error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszMsg The message.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetError(int rc, char *pszErr, size_t cbErr, const char *pszMsg)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(rc, pszErr, cbErr, 1, pszMsg);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Output from a successfull supR3HardenedVerifyPathSanity call.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** The length of the path in szCopy. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** The number of path components. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** Set if the path ends with slash, indicating that it's a directory
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * reference and not a file reference. The slash has been removed from
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * the copy. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** The offset where each path component starts, i.e. the char after the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * slash. The array has cComponents + 1 entries, where the final one is
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * cch + 1 so that one can always terminate the current component by
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * szPath[aoffComponent[i] - 1] = '\0'. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** A normalized copy of the path.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Reserve some extra space so we can be more relaxed about overflow
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * checks and terminator paddings, especially when recursing. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/** Pointer to a parsed path. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsynctypedef SUPR3HARDENEDPATHINFO *PSUPR3HARDENEDPATHINFO;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies that the path is absolutely sane, it also parses the path.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * A sane path starts at the root (w/ drive letter on DOS derived systems) and
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * does not have any relative bits (/../) or unnecessary slashes (/bin//ls).
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Sane paths are less or equal to SUPR3HARDENED_MAX_PATH bytes in length. UNC
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * paths are not supported.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns VBox status code.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszPath The path to check.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr The error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pInfo Where to return a copy of the path along with
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * parsing information.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifyPathSanity(const char *pszPath, char *pszErr, size_t cbErr, PSUPR3HARDENEDPATHINFO pInfo)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check that it's an absolute path and copy the volume/root specifier.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pszErr, cbErr, "The path is not absolute: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pszErr, cbErr, "The path is not absolute: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * No path specifying the root or something very shortly thereafter will
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * be approved of.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_IS_ROOT, pszErr, cbErr, "The path is root: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_SHORT, pszErr, cbErr, "The path is too short: '", pszPath, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Check each component. No parent references or double slashes.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Sanity checks. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RTPATH_IS_SLASH(pszSrc[0])) /* can be relaxed if we care. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_CLEAN, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The path is not clean of double slashes: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Record the start of the component. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pInfo->cComponents >= RT_ELEMENTS(pInfo->aoffComponents) - 1)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_MANY_COMPONENTS, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The path has too many components: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pInfo->aoffComponents[pInfo->cComponents++] = pszDst - &pInfo->szPath[0];
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Traverse to the end of the component, copying it as we go along. */
79fe674227c3f2b82ab22a6b7d340283b610fb83vboxsync if ((uintptr_t)(pszDst - &pInfo->szPath[0]) >= SUPR3HARDENED_MAX_PATH)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_LONG, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Terminate the string and enter its length. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pInfo->cch = (uint16_t)(pszDst - &pInfo->szPath[0]);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pInfo->aoffComponents[pInfo->cComponents] = pInfo->cch + 1;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * The state information collected by supR3HardenedVerifyFsObject.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This can be used to verify that a directory we've opened for enumeration is
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * the same as the one that supR3HardenedVerifyFsObject just verified. It can
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * equally be used to verify a native specfied by the user.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** Not implemented for windows yet. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** The stat output. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/** Pointer to a file system object state. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsynctypedef SUPR3HARDENEDFSOBJSTATE *PSUPR3HARDENEDFSOBJSTATE;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/** Pointer to a const file system object state. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsynctypedef SUPR3HARDENEDFSOBJSTATE const *PCSUPR3HARDENEDFSOBJSTATE;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Query information about a file system object by path.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns VBox status code, error buffer filled on failure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszPath The path to the object.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pFsObjState Where to return the state information.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedQueryFsObjectByPath(char const *pszPath, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo Windows hardening. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Stat the object, do not follow links.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Ignore access errors */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(VERR_SUPLIB_STAT_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "stat failed with ", strerror(errno), " on: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Read ACLs.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Query information about a file system object by native handle.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns VBox status code, error buffer filled on failure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param hNative The native handle to the object @a pszPath
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * specifies and this should be verified to be the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * same file system object.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pFsObjState Where to return the state information.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszPath The path to the object. (For the error message
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedQueryFsObjectByHandle(RTHCUINTPTR hNative, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo Windows hardening. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Stat the object, do not follow links.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(VERR_SUPLIB_STAT_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "fstat failed with ", strerror(errno), " on '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Read ACLs.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies that the file system object indicated by the native handle is the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * same as the one @a pFsObjState indicates.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns VBox status code, error buffer filled on failure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pFsObjState1 File system object information/state by path.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pFsObjState2 File system object information/state by handle.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszPath The path to the object @a pFsObjState
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * describes. (For the error message.)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedIsSameFsObject(PCSUPR3HARDENEDFSOBJSTATE pFsObjState1, PCSUPR3HARDENEDFSOBJSTATE pFsObjState2,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo Windows hardening. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Compare the ino+dev, then the uid+gid and finally the important mode
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * bits. Technically the first one should be enough, but we're paranoid.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( pFsObjState1->Stat.st_ino != pFsObjState2->Stat.st_ino
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || pFsObjState1->Stat.st_dev != pFsObjState2->Stat.st_dev)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_NOT_SAME_OBJECT, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The native handle is not the same as '", pszPath, "' (ino/dev)");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( pFsObjState1->Stat.st_uid != pFsObjState2->Stat.st_uid
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || pFsObjState1->Stat.st_gid != pFsObjState2->Stat.st_gid)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_NOT_SAME_OBJECT, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The native handle is not the same as '", pszPath, "' (uid/gid)");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( (pFsObjState1->Stat.st_mode & (S_IFMT | S_IWUSR | S_IWGRP | S_IWOTH))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync != (pFsObjState2->Stat.st_mode & (S_IFMT | S_IWUSR | S_IWGRP | S_IWOTH)))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_NOT_SAME_OBJECT, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The native handle is not the same as '", pszPath, "' (mode)");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies a file system object (file or directory).
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns VBox status code, error buffer filled on failure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pFsObjState The file system object information/state to be
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * verified.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param fDir Whether this is a directory or a file.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param fRelaxed Whether we can be more relaxed about this
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * directory (only used for grand parent
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * directories).
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszPath The path to the object. (For error messages
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr The error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifyFsObject(PCSUPR3HARDENEDFSOBJSTATE pFsObjState, bool fDir, bool fRelaxed,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /** @todo Windows hardening. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* No hardening here - it's a single user system. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * The owner must be root.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This can be extended to include predefined system users if necessary.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_OWNER_NOT_ROOT, pszErr, cbErr, "The owner is not root: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * The group does not matter if it does not have write access, if it has
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * write access it must be group 0 (root/wheel/whatever).
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This can be extended to include predefined system groups or groups that
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * only root is member of.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* HACK ALERT: On Darwin /Applications is root:admin with admin having
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync full access. So, to work around we relax the hardening a bit and
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync permit grand parents and beyond to be group writable by admin. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pFsObjState->Stat.st_gid != 80 /*admin*/) /** @todo dynamically resolve the admin group? */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_WRITE_NON_SYS_GROUP, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The group is not a system group and it has write access to '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * World must not have write access. There is no relaxing this rule.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_WORLD_WRITABLE, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check the ACLs.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check the object type.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_SYMLINKS_ARE_NOT_PERMITTED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_NOT_DIR_NOT_FILE, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_IS_DIRECTORY, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "Expected file but found directory: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_IS_FILE, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "Expected directory but found file: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies that the file system object indicated by the native handle is the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * same as the one @a pFsObjState indicates.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns VBox status code, error buffer filled on failure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param hNative The native handle to the object @a pszPath
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * specifies and this should be verified to be the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * same file system object.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pFsObjState The information/state returned by a previous
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * query call.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszPath The path to the object @a pFsObjState
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * describes. (For the error message.)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifySameFsObject(RTHCUINTPTR hNative, PCSUPR3HARDENEDFSOBJSTATE pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedQueryFsObjectByHandle(hNative, &FsObjState2, pszPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedIsSameFsObject(pFsObjState, &FsObjState2, pszPath, pszErr, cbErr);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Does the recursive directory enumeration.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns VBox status code, error buffer filled on failure.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszDirPath The path buffer containing the subdirectory to
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * enumerate followed by a slash (this is never
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * the root slash). The buffer is RTPATH_MAX in
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * size and anything starting at @a cchDirPath
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * - 1 and beyond is scratch space.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cchDirPath The length of the directory path + slash.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pFsObjState Pointer to the file system object state buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * On input this will hold the stats for
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * the directory @a pszDirPath indicates and will
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * be used to verified that we're opening the same
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param fRecursive Whether to recurse into subdirectories.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr The error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifyDirRecursive(char *pszDirPath, size_t cchDirPath, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /** @todo Windows hardening. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* No hardening here - it's a single user system. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Open the directory. Now, we could probably eliminate opendir here
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * and go down on kernel API level (open + getdents for instance), however
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * that's not very portable and hopefully not necessary.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* Ignore access errors. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(VERR_SUPLIB_DIR_ENUM_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "opendir failed with ", strerror(errno), " on '", pszDirPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedVerifySameFsObject(dirfd(pDir), pFsObjState, pszDirPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Enumerate the directory, check all the requested bits.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszDirPath[cchDirPath] = '\0'; /* for error messages. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedSetErrorN(VERR_SUPLIB_DIR_ENUM_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "readdir_r failed with ", strerror(iErr), " in '", pszDirPath, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Check the length and copy it into the path buffer so it can be
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * stat()'ed.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedSetErrorN(VERR_SUPLIB_PATH_TOO_LONG, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 4, "Path grew too long during recursion: '", pszDirPath, pEntry->d_name, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync memcpy(&pszDirPath[cchName], pEntry->d_name, cchName + 1);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Query the information about the entry and verify it.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * (We don't bother skipping '.' and '..' at this point, a little bit
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * of extra checks doesn't hurt and neither requires relaxed handling.)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedQueryFsObjectByPath(pszDirPath, pFsObjState, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyFsObject(pFsObjState, S_ISDIR(pFsObjState->Stat.st_mode), false /*fRelaxed*/,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Recurse into subdirectories if requested.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyDirRecursive(pszDirPath, cchDirPath + cchName + 1, pFsObjState,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Worker for SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns See SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszDirPath See SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param fRecursive See SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param fCheckFiles See SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr See SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr See SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncDECLHIDDEN(int) supR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, char *pszErr, size_t cbErr)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Validate the input path and parse it.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedVerifyPathSanity(pszDirPath, pszErr, cbErr, &Info);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verify each component from the root up.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (uint32_t iComponent = 0; iComponent < cComponents; iComponent++)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedQueryFsObjectByPath(Info.szPath, &FsObjState, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyFsObject(&FsObjState,true /*fDir*/, fRelaxed, Info.szPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = iComponent + 1 == cComponents ? RTPATH_SLASH : '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check files and subdirectories if requested.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedVerifyDirRecursive(Info.szPath, Info.cch + 1, &FsObjState,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Verfies a file.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns VBox status code, error buffer filled on failure.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszFilename The file to verify.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param hNativeFile Handle to the file, verify that it's the same
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * as we ended up with when verifying the path.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * RTHCUINTPTR_MAX means NIL here.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr The error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncDECLHIDDEN(int) supR3HardenedVerifyFile(const char *pszFilename, RTHCUINTPTR hNativeFile, char *pszErr, size_t cbErr)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Validate the input path and parse it.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedVerifyPathSanity(pszFilename, pszErr, cbErr, &Info);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_IS_DIRECTORY, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The file path specifies a directory: '", pszFilename, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verify each component from the root up.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (uint32_t iComponent = 0; iComponent < cComponents; iComponent++)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedQueryFsObjectByPath(Info.szPath, &FsObjState, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyFsObject(&FsObjState, !fFinal /*fDir*/, fRelaxed, Info.szPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = fFinal ? RTPATH_SLASH : '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verify the file.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedVerifySameFsObject(hNativeFile, &FsObjState, Info.szPath, pszErr, cbErr);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Gets the pre-init data for the hand-over to the other version
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * of this code.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The reason why we pass this information on is that it contains
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * open directories and files. Later it may include even more info
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * (int the verified arrays mostly).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The receiver is supR3HardenedRecvPreInitData.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pPreInitData Where to store it.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(void) supR3HardenedGetPreInitData(PSUPPREINITDATA pPreInitData)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->cInstallFiles = RT_ELEMENTS(g_aSupInstallFiles);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->paInstallFiles = &g_aSupInstallFiles[0];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->paVerifiedFiles = &g_aSupVerifiedFiles[0];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->cVerifiedDirs = RT_ELEMENTS(g_aSupVerifiedDirs);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->paVerifiedDirs = &g_aSupVerifiedDirs[0];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Receives the pre-init data from the static executable stub.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VBox status code. Will not bitch on failure since the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * runtime isn't ready for it, so that is left to the exe stub.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pPreInitData The hand-over data.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(int) supR3HardenedRecvPreInitData(PCSUPPREINITDATA pPreInitData)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Compare the array lengths and the contents of g_aSupInstallFiles.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( pPreInitData->cInstallFiles != RT_ELEMENTS(g_aSupInstallFiles)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync || pPreInitData->cVerifiedDirs != RT_ELEMENTS(g_aSupVerifiedDirs))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync SUPINSTFILE const *paInstallFiles = pPreInitData->paInstallFiles;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( g_aSupInstallFiles[iFile].enmDir != paInstallFiles[iFile].enmDir
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync || g_aSupInstallFiles[iFile].enmType != paInstallFiles[iFile].enmType
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync || g_aSupInstallFiles[iFile].fOptional != paInstallFiles[iFile].fOptional
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync || strcmp(g_aSupInstallFiles[iFile].pszFile, paInstallFiles[iFile].pszFile))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check that we're not called out of order.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * If dynamic linking it screwed up, we may end up here.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( ASMMemIsAll8(&g_aSupVerifiedFiles[0], sizeof(g_aSupVerifiedFiles), 0) != NULL
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync || ASMMemIsAll8(&g_aSupVerifiedDirs[0], sizeof(g_aSupVerifiedDirs), 0) != NULL)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Copy the verification data over.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync memcpy(&g_aSupVerifiedFiles[0], pPreInitData->paVerifiedFiles, sizeof(g_aSupVerifiedFiles));