SUPR3HardenedVerify.cpp revision 79fe674227c3f2b82ab22a6b7d340283b610fb83
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/* $Id$ */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @file
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox Support Library - Verification of Hardened Installation.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*
f8e804dad2cc0262b6384e97c12be107cf7e19e0vboxsync * Copyright (C) 2006-2010 Oracle Corporation
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
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>
628ddfbd43ad5365d69fddda4007598242956577vboxsync# include <stdlib.h>
628ddfbd43ad5365d69fddda4007598242956577vboxsync# include <unistd.h>
628ddfbd43ad5365d69fddda4007598242956577vboxsync# include <sys/fcntl.h>
628ddfbd43ad5365d69fddda4007598242956577vboxsync# include <sys/errno.h>
628ddfbd43ad5365d69fddda4007598242956577vboxsync# include <sys/syslimits.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>
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync# include <dirent.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>
2f0d866e126dd288169fed591c259c1c6b4016e5vboxsync#include <iprt/asm.h>
ae5379e3e7573369566d4628ef6c597da693cc55vboxsync#include <iprt/ctype.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <iprt/param.h>
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#include <iprt/path.h>
ae5379e3e7573369566d4628ef6c597da693cc55vboxsync#include <iprt/string.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include "SUPLibInternal.h"
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/*******************************************************************************
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync* Defined Constants And Macros *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync*******************************************************************************/
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/** The max path length acceptable for a trusted path. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#define SUPR3HARDENED_MAX_PATH 260U
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Global Variables *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The files that gets verified.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic SUPINSTFILE const g_aSupInstallFiles[] =
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
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
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
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 },
77cce7691847be5aef145f31ba3f9d66fc2cf594vboxsync#if HC_ARCH_BITS == 32
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM32" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxREM64" SUPLIB_DLL_SUFF },
54d3b0107d9bf326fe6e0de92e012c791dbb1587vboxsync#endif
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
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 },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#ifdef VBOX_WITH_SHARED_CLIPBOARD
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedClipboard" SUPLIB_DLL_SUFF },
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#endif
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#ifdef VBOX_WITH_SHARED_FOLDERS
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxSharedFolders" SUPLIB_DLL_SUFF },
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#endif
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#ifdef VBOX_WITH_GUEST_PROPS
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxGuestPropSvc" SUPLIB_DLL_SUFF },
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync//#endif
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync//#ifdef VBOX_WITH_GUEST_CONTROL
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync { kSupIFT_Dll, kSupID_AppPrivArch, true, "VBoxGuestControlSvc" SUPLIB_DLL_SUFF },
9e293277b378073ce86910209a246b744b4caa2cvboxsync//#endif
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 },
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxManage" SUPLIB_EXE_SUFF },
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync#ifdef VBOX_WITH_MAIN
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync { kSupIFT_Exe, kSupID_AppBin, false, "VBoxSVC" SUPLIB_EXE_SUFF },
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync #ifdef RT_OS_WINDOWS
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync { kSupIFT_Dll, kSupID_AppPrivArchComp, false, "VBoxC" SUPLIB_DLL_SUFF },
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync #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" },
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync #endif
551d9b8ee3568ad3e11b65ce6ef2867c36375f37vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VRDPAuth" SUPLIB_DLL_SUFF },
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Dll, kSupID_SharedLib, true, "VBoxVRDP" SUPLIB_DLL_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
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 },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
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# endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
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//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
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//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#ifdef VBOX_WITH_WEBSERVICES
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "vboxwebsrv" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync//#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#ifdef RT_OS_LINUX
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync { kSupIFT_Exe, kSupID_AppBin, true, "VBoxTunctl" SUPLIB_EXE_SUFF },
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
39592d8ff3243f6116c4e99be391bcf30a4ad187vboxsync
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 },
39592d8ff3243f6116c4e99be391bcf30a4ad187vboxsync//#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/**
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Assembles the path to a directory.
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:
c77e7bff89c7639353778366984d51ff165ea0e3vboxsync rc = supR3HardenedPathExecDir(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 */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncDECLHIDDEN(int) supR3HardenedVerifyFixedDir(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 */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = supR3HardenedVerifyFixedDir(pFile->enmDir, fFatal);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync char szPath[RTPATH_MAX];
bac59dd15f093cbb8dae97ebd8f94f94786d1439vboxsync 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.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * On verification failure, an error code will be returned when fFatal is clear,
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * otherwise the program will be terminated.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFilename The filename.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param fFatal Whether validation failures should be treated as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * fatal (true) or not (false).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncDECLHIDDEN(int) supR3HardenedVerifyFixedFile(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];
c77e7bff89c7639353778366984d51ff165ea0e3vboxsync int rc2 = supR3HardenedPathExecDir(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 * Verifies all the known files.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
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 *
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.
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
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Copies the N messages into the error buffer and returns @a rc.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetErrorN(int rc, char *pszErr, size_t cbErr, unsigned cMsgs, ...)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync va_list va;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync va_start(va, cMsgs);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync while (cMsgs-- > 0 && cbErr > 0)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszMsg = va_arg(va, const char *);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync size_t cchMsg = VALID_PTR(pszMsg) ? strlen(pszMsg) : 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (cchMsg >= cbErr)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync cchMsg = cbErr - 1;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync memcpy(pszErr, pszMsg, cchMsg);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszErr[cchMsg] = '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszErr += cchMsg;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync cbErr -= cchMsg;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync va_end(va);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Copies the three messages into the error buffer and returns @a rc.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetError3(int rc, char *pszErr, size_t cbErr, const char *pszMsg1,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszMsg2, const char *pszMsg3)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(rc, pszErr, cbErr, 3, pszMsg1, pszMsg2, pszMsg3);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Copies the two messages into the error buffer and returns @a rc.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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 */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetError2(int rc, char *pszErr, size_t cbErr, const char *pszMsg1,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszMsg2)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(rc, pszErr, cbErr, 2, pszMsg1, pszMsg2);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Copies the error message to the error buffer and returns @a rc.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
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.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedSetError(int rc, char *pszErr, size_t cbErr, const char *pszMsg)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(rc, pszErr, cbErr, 1, pszMsg);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Output from a successfull supR3HardenedVerifyPathSanity call.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsynctypedef struct SUPR3HARDENEDPATHINFO
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** The length of the path in szCopy. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint16_t cch;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** The number of path components. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint16_t cComponents;
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 bool fDirSlash;
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 uint16_t aoffComponents[32+1];
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 char szPath[SUPR3HARDENED_MAX_PATH * 2];
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync} SUPR3HARDENEDPATHINFO;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/** Pointer to a parsed path. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsynctypedef SUPR3HARDENEDPATHINFO *PSUPR3HARDENEDPATHINFO;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies that the path is absolutely sane, it also parses the path.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
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 *
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.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifyPathSanity(const char *pszPath, char *pszErr, size_t cbErr, PSUPR3HARDENEDPATHINFO pInfo)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszSrc = pszPath;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync char *pszDst = pInfo->szPath;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check that it's an absolute path and copy the volume/root specifier.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( RT_C_IS_ALPHA(pszSrc[0])
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || pszSrc[1] != ':'
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || !RTPATH_IS_SLASH(pszSrc[2]))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pszErr, cbErr, "The path is not absolute: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *pszDst++ = RT_C_TO_UPPER(pszSrc[0]);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *pszDst++ = ':';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *pszDst++ = RTPATH_SLASH;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszSrc += 3;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (!RTPATH_IS_SLASH(pszSrc[0]))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pszErr, cbErr, "The path is not absolute: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *pszDst++ = RTPATH_SLASH;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszSrc += 1;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * No path specifying the root or something very shortly thereafter will
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * be approved of.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pszSrc[0] == '\0')
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_IS_ROOT, pszErr, cbErr, "The path is root: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( pszSrc[1] == '\0'
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || pszSrc[2] == '\0')
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_SHORT, pszErr, cbErr, "The path is too short: '", pszPath, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Check each component. No parent references or double slashes.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pInfo->fDirSlash = false;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync while (pszSrc[0])
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
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 if ( pszSrc[0] == '.'
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && pszSrc[1] == '.'
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && RTPATH_IS_SLASH(pszSrc[2]))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_NOT_ABSOLUTE, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The path is not absolute: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
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
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Traverse to the end of the component, copying it as we go along. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync while (pszSrc[0])
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RTPATH_IS_SLASH(pszSrc[0]))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszSrc++;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (*pszSrc)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *pszDst++ = RTPATH_SLASH;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pInfo->fDirSlash = true;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync break;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *pszDst++ = *pszSrc++;
79fe674227c3f2b82ab22a6b7d340283b610fb83vboxsync if ((uintptr_t)(pszDst - &pInfo->szPath[0]) >= SUPR3HARDENED_MAX_PATH)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_PATH_TOO_LONG, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The path is too long: '", pszPath, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Terminate the string and enter its length. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszDst[0] = '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszDst[1] = '\0'; /* for aoffComponents */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pInfo->cch = (uint16_t)(pszDst - &pInfo->szPath[0]);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pInfo->aoffComponents[pInfo->cComponents] = pInfo->cch + 1;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * The state information collected by supR3HardenedVerifyFsObject.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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 */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsynctypedef struct SUPR3HARDENEDFSOBJSTATE
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifdef RT_OS_WINDOWS
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** Not implemented for windows yet. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync char chTodo;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** The stat output. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync struct stat Stat;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync} SUPR3HARDENEDFSOBJSTATE;
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
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Query information about a file system object by path.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedQueryFsObjectByPath(char const *pszPath, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync char *pszErr, size_t cbErr)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#if defined(RT_OS_WINDOWS)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo Windows hardening. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pFsObjState->chTodo = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Stat the object, do not follow links.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (lstat(pszPath, &pFsObjState->Stat) != 0)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Ignore access errors */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (errno != EACCES)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(VERR_SUPLIB_STAT_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "stat failed with ", strerror(errno), " on: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Read ACLs.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Query information about a file system object by native handle.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
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 * only.)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pszErr The error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cbErr The size of the error buffer.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedQueryFsObjectByHandle(RTHCUINTPTR hNative, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync char const *pszPath, char *pszErr, size_t cbErr)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#if defined(RT_OS_WINDOWS)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo Windows hardening. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pFsObjState->chTodo = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Stat the object, do not follow links.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (fstat((int)hNative, &pFsObjState->Stat) != 0)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(VERR_SUPLIB_STAT_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "fstat failed with ", strerror(errno), " on '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Read ACLs.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies that the file system object indicated by the native handle is the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * same as the one @a pFsObjState indicates.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedIsSameFsObject(PCSUPR3HARDENEDFSOBJSTATE pFsObjState1, PCSUPR3HARDENEDFSOBJSTATE pFsObjState2,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszPath, char *pszErr, size_t cbErr)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#if defined(RT_OS_WINDOWS)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo Windows hardening. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#elif defined(RT_OS_OS2)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
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 */
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 return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies a file system object (file or directory).
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * only.)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr The error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr The size of the error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifyFsObject(PCSUPR3HARDENEDFSOBJSTATE pFsObjState, bool fDir, bool fRelaxed,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszPath, char *pszErr, size_t cbErr)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#if defined(RT_OS_WINDOWS)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /** @todo Windows hardening. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#elif defined(RT_OS_OS2)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* No hardening here - it's a single user system. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#else
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * The owner must be root.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This can be extended to include predefined system users if necessary.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pFsObjState->Stat.st_uid != 0)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_OWNER_NOT_ROOT, pszErr, cbErr, "The owner is not root: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
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 *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This can be extended to include predefined system groups or groups that
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * only root is member of.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( (pFsObjState->Stat.st_mode & S_IWGRP)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && pFsObjState->Stat.st_gid != 0)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifdef RT_OS_DARWIN
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#endif
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, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * World must not have write access. There is no relaxing this rule.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pFsObjState->Stat.st_mode & S_IWOTH)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_WORLD_WRITABLE, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "World writable: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check the ACLs.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check the object type.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( !S_ISDIR(pFsObjState->Stat.st_mode)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && !S_ISREG(pFsObjState->Stat.st_mode))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (S_ISLNK(pFsObjState->Stat.st_mode))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_SYMLINKS_ARE_NOT_PERMITTED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "Symlinks are not permitted: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_NOT_DIR_NOT_FILE, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "Not regular file or directory: '", pszPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (fDir != !!S_ISDIR(pFsObjState->Stat.st_mode))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (S_ISDIR(pFsObjState->Stat.st_mode))
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 }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verifies that the file system object indicated by the native handle is the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * same as the one @a pFsObjState indicates.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
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.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifySameFsObject(RTHCUINTPTR hNative, PCSUPR3HARDENEDFSOBJSTATE pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync const char *pszPath, char *pszErr, size_t cbErr)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR3HARDENEDFSOBJSTATE FsObjState2;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedQueryFsObjectByHandle(hNative, &FsObjState2, pszPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_SUCCESS(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedIsSameFsObject(pFsObjState, &FsObjState2, pszPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return rc;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Does the recursive directory enumeration.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
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
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * thing.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param fRecursive Whether to recurse into subdirectories.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pszErr The error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cbErr The size of the error buffer.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int supR3HardenedVerifyDirRecursive(char *pszDirPath, size_t cchDirPath, PSUPR3HARDENEDFSOBJSTATE pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync bool fRecursive, char *pszErr, size_t cbErr)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#if defined(RT_OS_WINDOWS)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /** @todo Windows hardening. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#elif defined(RT_OS_OS2)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* No hardening here - it's a single user system. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
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.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync DIR *pDir = opendir(pszDirPath);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (!pDir)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* Ignore access errors. */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (errno == EACCES)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetErrorN(VERR_SUPLIB_DIR_ENUM_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "opendir failed with ", strerror(errno), " on '", pszDirPath, "'");
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (dirfd(pDir) != -1)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedVerifySameFsObject(dirfd(pDir), pFsObjState, pszDirPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_FAILURE(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync closedir(pDir);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Enumerate the directory, check all the requested bits.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync for (;;)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszDirPath[cchDirPath] = '\0'; /* for error messages. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync struct dirent Entry;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync struct dirent *pEntry;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int iErr = readdir_r(pDir, &Entry, &pEntry);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (iErr)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedSetErrorN(VERR_SUPLIB_DIR_ENUM_FAILED, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 5, "readdir_r failed with ", strerror(iErr), " in '", pszDirPath, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync break;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (!pEntry)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync break;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Check the length and copy it into the path buffer so it can be
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * stat()'ed.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync size_t cchName = strlen(pEntry->d_name);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (cchName + cchDirPath > SUPR3HARDENED_MAX_PATH)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedSetErrorN(VERR_SUPLIB_PATH_TOO_LONG, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync 4, "Path grew too long during recursion: '", pszDirPath, pEntry->d_name, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync break;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync memcpy(&pszDirPath[cchName], pEntry->d_name, cchName + 1);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
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.)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedQueryFsObjectByPath(pszDirPath, pFsObjState, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_SUCCESS(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync break;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyFsObject(pFsObjState, S_ISDIR(pFsObjState->Stat.st_mode), false /*fRelaxed*/,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pszDirPath, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync break;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Recurse into subdirectories if requested.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if ( fRecursive
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && S_ISDIR(pFsObjState->Stat.st_mode)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync && strcmp(pEntry->d_name, ".")
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync && strcmp(pEntry->d_name, ".."))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pszDirPath[cchDirPath + cchName] = RTPATH_SLASH;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pszDirPath[cchDirPath + cchName + 1] = '\0';
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyDirRecursive(pszDirPath, cchDirPath + cchName + 1, pFsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync fRecursive, pszErr, cbErr);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync break;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync closedir(pDir);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync#endif
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Worker for SUPR3HardenedVerifyDir.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
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.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncDECLHIDDEN(int) supR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, char *pszErr, size_t cbErr)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Validate the input path and parse it.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR3HARDENEDPATHINFO Info;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedVerifyPathSanity(pszDirPath, pszErr, cbErr, &Info);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return rc;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verify each component from the root up.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR3HARDENEDFSOBJSTATE FsObjState;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint32_t const cComponents = Info.cComponents;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (uint32_t iComponent = 0; iComponent < cComponents; iComponent++)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync bool fRelaxed = iComponent + 2 < cComponents;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedQueryFsObjectByPath(Info.szPath, &FsObjState, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_SUCCESS(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyFsObject(&FsObjState,true /*fDir*/, fRelaxed, Info.szPath, pszErr, cbErr);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = iComponent + 1 == cComponents ? RTPATH_SLASH : '\0';
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check files and subdirectories if requested.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (fCheckFiles || fRecursive)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.cch] = RTPATH_SLASH;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.cch + 1] = '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedVerifyDirRecursive(Info.szPath, Info.cch + 1, &FsObjState,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync fRecursive, pszErr, cbErr);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Verfies a file.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
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.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncDECLHIDDEN(int) supR3HardenedVerifyFile(const char *pszFilename, RTHCUINTPTR hNativeFile, char *pszErr, size_t cbErr)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Validate the input path and parse it.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR3HARDENEDPATHINFO Info;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = supR3HardenedVerifyPathSanity(pszFilename, pszErr, cbErr, &Info);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (Info.fDirSlash)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedSetError3(VERR_SUPLIB_IS_DIRECTORY, pszErr, cbErr,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync "The file path specifies a directory: '", pszFilename, "'");
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verify each component from the root up.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR3HARDENEDFSOBJSTATE FsObjState;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint32_t const cComponents = Info.cComponents;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (uint32_t iComponent = 0; iComponent < cComponents; iComponent++)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync bool fFinal = iComponent + 1 == cComponents;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync bool fRelaxed = iComponent + 2 < cComponents;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = '\0';
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedQueryFsObjectByPath(Info.szPath, &FsObjState, pszErr, cbErr);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_SUCCESS(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = supR3HardenedVerifyFsObject(&FsObjState, !fFinal /*fDir*/, fRelaxed, Info.szPath, pszErr, cbErr);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Info.szPath[Info.aoffComponents[iComponent + 1] - 1] = fFinal ? RTPATH_SLASH : '\0';
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Verify the file.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (hNativeFile != RTHCUINTPTR_MAX)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return supR3HardenedVerifySameFsObject(hNativeFile, &FsObjState, Info.szPath, pszErr, cbErr);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return VINF_SUCCESS;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
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}