SUPR3HardenedVerify.cpp revision 1c2c968fd241148110002d75b2c0fdeddc211e14
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/* $Id$ */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @file
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox Support Library - Verification of Hardened Installation.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Copyright (C) 2006-2008 Sun Microsystems, Inc.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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 *
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 *
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * additional information or have any questions.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Header Files *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(RT_OS_OS2)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define INCL_BASE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define INCL_ERRORS
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <os2.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <stdio.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#elif defined(RT_OS_WINDOWS)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <Windows.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <stdio.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else /* UNIXes */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <sys/types.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <stdio.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <stdlib.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <dlfcn.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <fcntl.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <limits.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <errno.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <unistd.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <sys/stat.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <sys/time.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <sys/fcntl.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <stdio.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <pwd.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# ifdef RT_OS_DARWIN
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# include <mach-o/dyld.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <VBox/sup.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <VBox/err.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <iprt/string.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <iprt/param.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include "SUPLibInternal.h"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Global Variables *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The files that gets verified.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @todo This needs reviewing against the linux packages.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @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).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic SUPINSTFILE const g_aSupInstallFiles[] =
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* type, dir,fOptional, 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
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VMMGC.gc" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDDGC.gc" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxDD2GC.gc" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxRT" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxVMM" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxREM" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDD2" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxDDU" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_DEBUGGER_GUI
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxDbg" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxSharedClipboard" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxSharedFolders" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, false, "VBoxGuestPropSvc" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, false, "VBoxManage" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, false, "VBoxSVC" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, false, "VBoxSettings" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#ifdef RT_OS_WINDOWS
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
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" },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_VRDP
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VRDPAuth" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxVRDP" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_HEADLESS
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxHeadless" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxHeadless" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_QT4GUI
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VirtualBox" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VirtualBox" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxKeyboard" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_QTGUI
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VirtualBox3" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VirtualBox3" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxKeyboard3" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_VBOXSDL
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxSDL" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSDL" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_VBOXBFE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxBFE" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxBFE" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_WEBSERVICES
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "vboxwebsrv" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#ifdef RT_OS_LINUX
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxTunctl" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync};
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Array parallel to g_aSupInstallFiles containing per-file status info. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic SUPVERIFIEDFILE g_aSupVerifiedFiles[RT_ELEMENTS(g_aSupInstallFiles)];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Array index by install directory specifier containing info about verified directories. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic SUPVERIFIEDDIR g_aSupVerifiedDirs[kSupID_End];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Assembles the path to a dirtory.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success, some error code on failure (fFatal
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * decides whether it returns or not).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedMakePath(SUPINSTDIR enmDir, char *pszDst, size_t cchDst, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync switch (enmDir)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync case kSupID_AppBin: /** @todo fix this AppBin crap (uncertain wtf some binaries actually are installed). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync case kSupID_Bin:
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathProgram(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync break;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync case kSupID_SharedLib:
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathSharedLibs(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync break;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync case kSupID_AppPrivArch:
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync break;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync case kSupID_AppPrivArchComp:
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathAppPrivateArch(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync size_t off = strlen(pszDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (cchDst - off >= sizeof("/components"))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync memcpy(&pszDst[off], "/components", sizeof("/components"));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = VERR_BUFFER_OVERFLOW;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync break;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync case kSupID_AppPrivNoArch:
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedPathAppPrivateNoArch(pszDst, cchDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync break;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync default:
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedMakePath: enmDir=%d\n", enmDir);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_FAILURE(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supR3HardenedError(rc, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedMakePath: enmDir=%d rc=%d\n", enmDir, rc);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Assembles the path to a file table entry, with or without the actual filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success, some error code on failure (fFatal
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * decides whether it returns or not).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedMakeFilePath(PCSUPINSTFILE pFile, char *pszDst, size_t cchDst, bool fWithFilename, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Combine supR3HardenedMakePath and the filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedMakePath(pFile->enmDir, pszDst, cchDst, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync size_t cchFile = strlen(pFile->pszFile);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync size_t off = strlen(pszDst);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (cchDst - off >= cchFile + 2)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pszDst[off++] = '/';
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync memcpy(&pszDst[off], pFile->pszFile, cchFile + 1);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_BUFFER_OVERFLOW, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedMakeFilePath: pszFile=%s off=%lu\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pFile->pszFile, (long)off);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a directory.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(int) supR3HardenedVerifyDir(SUPINSTDIR enmDir, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Validate the index just to be on the safe side...
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (enmDir <= kSupID_Invalid || enmDir >= kSupID_End)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: enmDir=%d\n", enmDir);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Already validated?
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (g_aSupVerifiedDirs[enmDir].fValidated)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VINF_SUCCESS; /** @todo revalidate? */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* initialize the entry. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (g_aSupVerifiedDirs[enmDir].hDir != 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: hDir=%p enmDir=%d\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync (void *)g_aSupVerifiedDirs[enmDir].hDir, enmDir);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aSupVerifiedDirs[enmDir].hDir = -1;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aSupVerifiedDirs[enmDir].fValidated = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Make the path and open the directory.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char szPath[RTPATH_MAX];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedMakePath(enmDir, szPath, sizeof(szPath), fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(RT_OS_WINDOWS)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync HANDLE hDir = CreateFile(szPath,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GENERIC_READ,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync NULL,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync OPEN_ALWAYS,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync NULL);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (hDir != INVALID_HANDLE_VALUE)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo check the type */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* That's all on windows, for now at least... */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aSupVerifiedDirs[enmDir].hDir = (intptr_t)hDir;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aSupVerifiedDirs[enmDir].fValidated = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int err = GetLastError();
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Failed to open \"%s\": err=%d\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, err);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int fd = open(szPath, O_RDONLY, 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (fd >= 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On unixy systems we'll make sure the directory is owned by root
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and not writable by the group and user.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct stat st;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!fstat(fd, &st))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( st.st_uid == 0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && !(st.st_mode & (S_IWGRP | S_IWOTH))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && S_ISDIR(st.st_mode))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aSupVerifiedDirs[enmDir].hDir = fd;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_aSupVerifiedDirs[enmDir].fValidated = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!S_ISDIR(st.st_mode))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_NOT_A_DIRECTORY, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: \"%s\" is not a directory\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, (long)st.st_uid);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if (st.st_uid)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Cannot trust the directory \"%s\": not owned by root (st_uid=%ld)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, (long)st.st_uid);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
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 szPath, (long)st.st_mode);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync close(fd);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int err = errno;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Failed to fstat \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, strerror(err), err);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync close(fd);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int err = errno;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyDir: Failed to open \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, strerror(err), err);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a file entry.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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 *
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.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedVerifyFileInternal(int iFile, bool fFatal, bool fLeaveFileOpen)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PCSUPINSTFILE pFile = &g_aSupInstallFiles[iFile];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PSUPVERIFIEDFILE pVerified = &g_aSupVerifiedFiles[iFile];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Already done?
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (pVerified->fValidated)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VINF_SUCCESS; /** @todo revalidate? */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* initialize the entry. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (pVerified->hFile != 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: hFile=%p (%s)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync (void *)pVerified->hFile, pFile->pszFile);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pVerified->hFile = -1;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pVerified->fValidated = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
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.)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedVerifyDir(pFile->enmDir, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char szPath[RTPATH_MAX];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedMakeFilePath(pFile, szPath, sizeof(szPath), true, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(RT_OS_WINDOWS)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync HANDLE hFile = CreateFile(szPath,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GENERIC_READ,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync FILE_SHARE_READ,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync NULL,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync OPEN_ALWAYS,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync FILE_ATTRIBUTE_NORMAL,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync NULL);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (hFile != INVALID_HANDLE_VALUE)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo Check the type, and verify the signature (separate function so we can skip it). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* it's valid. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (fLeaveFileOpen)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pVerified->hFile = (intptr_t)hFile;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync CloseHandle(hFile);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pVerified->fValidated = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int err = GetLastError();
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 szPath, err);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int fd = open(szPath, O_RDONLY, 0);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (fd >= 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On unixy systems we'll make sure the directory is owned by root
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and not writable by the group and user.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct stat st;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!fstat(fd, &st))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( st.st_uid == 0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && !(st.st_mode & (S_IWGRP | S_IWOTH))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && S_ISREG(st.st_mode))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* it's valid. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (fLeaveFileOpen)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pVerified->hFile = fd;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync close(fd);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pVerified->fValidated = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!S_ISREG(st.st_mode))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_IS_A_DIRECTORY, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: \"%s\" is not a regular file\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, (long)st.st_uid);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if (st.st_uid)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Cannot trust the file \"%s\": not owned by root (st_uid=%ld)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, (long)st.st_uid);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
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 szPath, (long)st.st_mode);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync close(fd);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int err = errno;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_ACCESS_DENIED, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Failed to fstat \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, strerror(err), err);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync close(fd);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int err = errno;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!pFile->fOptional || err != ENOENT)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_PATH_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyFileInternal: Failed to open \"%s\": %s (%d)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync szPath, strerror(err), err);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif /* UNIXY */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies that the specified table entry matches the given filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS if matching. On mismatch fFatal indicates whether an
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * error is returned or we terminate the application.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedVerifySameFile(int iFile, const char *pszFilename, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PCSUPINSTFILE pFile = &g_aSupInstallFiles[iFile];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Construct the full path for the file table entry
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and compare it with the specified file.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char szName[RTPATH_MAX];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_FAILURE(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (stricmp(szName, pszFilename))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (strcmp(szName, pszFilename))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Normalize the two paths and compare again.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = VERR_NOT_SAME_DEVICE;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(RT_OS_WINDOWS)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LPSTR pszIgnored;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char szName2[RTPATH_MAX];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( GetFullPathName(szName, RT_ELEMENTS(szName2), &szName2[0], &pszIgnored)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && GetFullPathName(pszFilename, RT_ELEMENTS(szName), &szName[0], &pszIgnored))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!stricmp(szName2, szName))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = VINF_SUCCESS;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync AssertCompile(RTPATH_MAX >= PATH_MAX);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char szName2[RTPATH_MAX];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( realpath(szName, szName2) != NULL
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && realpath(pszFilename, szName) != NULL)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!strcmp(szName2, szName))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = VINF_SUCCESS;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_FAILURE(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supR3HardenedMakeFilePath(pFile, szName, sizeof(szName), true /*fWithFilename*/, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return supR3HardenedError(rc, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifySameFile: \"%s\" isn't the same as \"%s\"\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pszFilename, szName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check more stuff like the stat info if it's an already open file?
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VINF_SUCCESS;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a file.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On verfication failure, an error code will be returned when fFatal is clear,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * otherwise the program will be termindated.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFilename The filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(int) supR3HardenedVerifyFile(const char *pszFilename, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Lookup the file and check if it's the same file.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
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 {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = supR3HardenedVerifySameFile(iFile, pszFilename, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedVerifyFileInternal(iFile, fFatal, false /* fLeaveFileOpen */);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VERR_NOT_FOUND;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies a program, worker for supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns See supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszProgName See supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal See supR3HardenedVerifyAll.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int supR3HardenedVerifyProgram(const char *pszProgName, bool fFatal)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Search the table looking for the executable and the DLL/DYLIB/SO.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = VINF_SUCCESS;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync bool fExe = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync bool fDll = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync size_t const cchProgName = strlen(pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!strncmp(pszProgName, g_aSupInstallFiles[iFile].pszFile, cchProgName))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Dll
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && !strcmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_DLL_SUFF))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* This only has to be found (once). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (fDll)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: duplicate DLL entry for \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync fDll = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if ( g_aSupInstallFiles[iFile].enmType == kSupIFT_Exe
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync && !strcmp(&g_aSupInstallFiles[iFile].pszFile[cchProgName], SUPLIB_EXE_SUFF))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Here we'll have to check that the specific program is the same as the entry. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (fExe)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_INTERNAL_ERROR, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: duplicate EXE entry for \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync fExe = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char szFilename[RTPATH_MAX];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc2 = supR3HardenedPathProgram(szFilename, sizeof(szFilename) - cchProgName - sizeof(SUPLIB_EXE_SUFF));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc2))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync strcat(szFilename, "/");
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync strcat(szFilename, g_aSupInstallFiles[iFile].pszFile);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync supR3HardenedVerifySameFile(iFile, szFilename, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(rc2, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: failed to query program path: rc=%d\n", rc2);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check the findings.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!fDll && !fExe)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: Couldn't find the program \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if (!fExe)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: Couldn't find the EXE entry for \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if (!fDll)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = supR3HardenedError(VERR_NOT_FOUND, fFatal,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync "supR3HardenedVerifyProgram: Couldn't find the DLL entry for \"%s\"\n", pszProgName);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Verifies all the known files.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VINF_SUCCESS on success.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On verfication failure, an error code will be returned when fFatal is clear,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * otherwise the program will be termindated.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fLeaveFilesOpen If set, all the verfied 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.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(int) supR3HardenedVerifyAll(bool fFatal, bool fLeaveFilesOpen, const char *pszProgName)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The verify all the files.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = VINF_SUCCESS;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned iFile = 0; iFile < RT_ELEMENTS(g_aSupInstallFiles); iFile++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc2 = supR3HardenedVerifyFileInternal(iFile, fFatal, fLeaveFilesOpen);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = rc2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
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 */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (pszProgName)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc2 = supR3HardenedVerifyProgram(pszProgName, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc2 = rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Gets the pre-init data for the hand-over to the other version
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * of this code.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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 *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The receiver is supR3HardenedRecvPreInitData.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pPreInitData Where to store it.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(void) supR3HardenedGetPreInitData(PSUPPREINITDATA pPreInitData)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->cInstallFiles = RT_ELEMENTS(g_aSupInstallFiles);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->paInstallFiles = &g_aSupInstallFiles[0];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->paVerifiedFiles = &g_aSupVerifiedFiles[0];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->cVerifiedDirs = RT_ELEMENTS(g_aSupVerifiedDirs);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pPreInitData->paVerifiedDirs = &g_aSupVerifiedDirs[0];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Receives the pre-init data from the static executable stub.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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 *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pPreInitData The hand-over data.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLHIDDEN(int) supR3HardenedRecvPreInitData(PCSUPPREINITDATA pPreInitData)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Compare the array lengths and the contents of g_aSupInstallFiles.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( pPreInitData->cInstallFiles != RT_ELEMENTS(g_aSupInstallFiles)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync || pPreInitData->cVerifiedDirs != RT_ELEMENTS(g_aSupVerifiedDirs))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VERR_VERSION_MISMATCH;
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 return VERR_VERSION_MISMATCH;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check that we're not called out of order.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * If dynamic linking it screwed up, we may end up here.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if ( ASMMemIsAll8(&g_aSupVerifiedFiles[0], sizeof(g_aSupVerifiedFiles), 0) != NULL
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync || ASMMemIsAll8(&g_aSupVerifiedDirs[0], sizeof(g_aSupVerifiedDirs), 0) != NULL)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VERR_WRONG_ORDER;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Copy the verification data over.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync memcpy(&g_aSupVerifiedFiles[0], pPreInitData->paVerifiedFiles, sizeof(g_aSupVerifiedFiles));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync memcpy(&g_aSupVerifiedDirs[0], pPreInitData->paVerifiedDirs, sizeof(g_aSupVerifiedDirs));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VINF_SUCCESS;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}