VirtualBox

Changeset 52632 in vbox


Ignore:
Timestamp:
Sep 5, 2014 11:00:50 PM (10 years ago)
Author:
vboxsync
Message:

bugfix in previous commit with some new parent watcher code (disabled).

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/thread.h

    r47572 r52632  
    276276RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
    277277                           RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
     278/** @copydoc RTThreadCreate */
     279typedef DECLCALLBACKPTR(int, PFNRTTHREADCREATE)(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
     280                                                RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
     281
    278282
    279283/**
  • trunk/src/VBox/HostDrivers/Support/SUPLibInternal.h

    r52523 r52632  
    443443DECLHIDDEN(bool)    supR3HardenedWinIsReSpawnNeeded(int iWhich, int cArgs, char **papszArgs);
    444444DECLHIDDEN(int)     supR3HardenedWinReSpawn(int iWhich);
     445# ifdef _WINDEF_
     446DECLHIDDEN(void)    supR3HardenedWinCreateParentWatcherThread(HMODULE hVBoxRT);
     447# endif
    445448DECLHIDDEN(void *)  supR3HardenedWinLoadLibrary(const char *pszName, bool fSystem32Only);
    446449extern RTUTF16      g_wszSupLibHardenedExePath[1024];
  • trunk/src/VBox/HostDrivers/Support/SUPR3HardenedMain.cpp

    r52527 r52632  
    15241524        supR3HardenedFatalMsg("supR3HardenedMainInitRuntime", kSupInitOp_IPRT, rc,
    15251525                              "RTR3InitEx failed with rc=%d", rc);
     1526
     1527#if defined(RT_OS_WINDOWS)
     1528    /*
     1529     * Windows: Create thread that terminates the process when the parent stub
     1530     *          process terminates (VBoxNetDHCP, Ctrl-C, etc).
     1531     */
     1532    if (!(fFlags & SUPSECMAIN_FLAGS_DONT_OPEN_DEV))
     1533        supR3HardenedWinCreateParentWatcherThread(hMod);
     1534#endif
    15261535}
    15271536
  • trunk/src/VBox/HostDrivers/Support/win/SUPR3HardenedMain-win.cpp

    r52627 r52632  
    4747#include <iprt/param.h>
    4848#include <iprt/path.h>
     49#include <iprt/thread.h>
    4950#include <iprt/zero.h>
    5051
     
    7576    do { \
    7677        if (!(a_Expr)) \
    77             supR3HardenedFatal("%s: %s", __FUNCTION__, #a_Expr); \
     78            supR3HardenedFatal("%s: %s\n", __FUNCTION__, #a_Expr); \
    7879    } while (0)
    7980
     
    8384        NTSTATUS rcNtAssert = (a_Expr); \
    8485        if (!NT_SUCCESS(rcNtAssert)) \
    85             supR3HardenedFatal("%s: %s -> %#x", __FUNCTION__, #a_Expr, rcNtAssert); \
     86            supR3HardenedFatal("%s: %s -> %#x\n", __FUNCTION__, #a_Expr, rcNtAssert); \
    8687    } while (0)
    8788
     
    9192        BOOL fRcAssert = (a_Expr); \
    9293        if (fRcAssert == FALSE) \
    93             supR3HardenedFatal("%s: %s -> %#x", __FUNCTION__, #a_Expr, GetLastError()); \
     94            supR3HardenedFatal("%s: %s -> %#x\n", __FUNCTION__, #a_Expr, GetLastError()); \
    9495    } while (0)
    9596
     
    820821                    UniStrStatic.Buffer = &wszPath[cwcName + 1];
    821822                    UniStrStatic.Length = 0;
    822                     UniStrStatic.MaximumLength = (USHORT)sizeof(wszPath) - UniStrStatic.MaximumLength - sizeof(WCHAR);
     823                    UniStrStatic.MaximumLength = (USHORT)(sizeof(wszPath) - cwcName * sizeof(WCHAR) - sizeof(WCHAR));
    823824
    824825                    static UNICODE_STRING const s_DefaultSuffix = RTNT_CONSTANT_UNISTR(L".dll");
     
    19531954
    19541955/**
     1956 * IPRT thread that waits for the parent process to terminate and reacts by
     1957 * exiting the current process.
     1958 *
     1959 * @returns VINF_SUCCESS
     1960 * @param   hSelf               The current thread.  Ignored.
     1961 * @param   pvUser              The handle of the parent process.
     1962 */
     1963static DECLCALLBACK(int) supR3HardenedWinParentWatcherThread(RTTHREAD hSelf, void *pvUser)
     1964{
     1965    HANDLE hProcWait = (HANDLE)pvUser;
     1966    NOREF(hSelf);
     1967
     1968    /*
     1969     * Wait for the parent to terminate.
     1970     */
     1971    NTSTATUS rcNt;
     1972    for (;;)
     1973    {
     1974        rcNt = NtWaitForSingleObject(hProcWait, TRUE /*Alertable*/, NULL /*pTimeout*/);
     1975        if (   rcNt == STATUS_WAIT_0
     1976            || rcNt == STATUS_ABANDONED_WAIT_0)
     1977            break;
     1978        if (   rcNt != STATUS_TIMEOUT
     1979            && rcNt != STATUS_USER_APC
     1980            && rcNt != STATUS_ALERTED)
     1981            supR3HardenedFatal("NtWaitForSingleObject returned %#x\n", rcNt);
     1982    }
     1983
     1984    /*
     1985     * Proxy the termination code of the child, if it exited already.
     1986     */
     1987    PROCESS_BASIC_INFORMATION BasicInfo;
     1988    NTSTATUS rcNt2 = NtQueryInformationProcess(hProcWait, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
     1989    if (   !NT_SUCCESS(rcNt2)
     1990        || BasicInfo.ExitStatus == STATUS_PENDING)
     1991        BasicInfo.ExitStatus = RTEXITCODE_FAILURE;
     1992
     1993    NtClose(hProcWait);
     1994    SUP_DPRINTF(("supR3HardenedWinParentWatcherThread: Quitting: ExitCode=%#x rcNt=%#x\n", BasicInfo.ExitStatus, rcNt));
     1995    suplibHardenedExit((RTEXITCODE)BasicInfo.ExitStatus);
     1996
     1997    return VINF_SUCCESS; /* won't be reached. */
     1998}
     1999
     2000
     2001/**
     2002 * Creates the parent watcher thread that will make sure this process exits when
     2003 * the parent does.
     2004 *
     2005 * This is a necessary evil to make VBoxNetDhcp and VBoxNetNat termination from
     2006 * Main work without too much new magic.  It also makes Ctrl-C or similar work
     2007 * in on the hardened processes in the windows console.
     2008 *
     2009 * @param   hVBoxRT             The VBoxRT.dll handle.  We use RTThreadCreate to
     2010 *                              spawn the thread to avoid duplicating thread
     2011 *                              creation and thread naming code from IPRT.
     2012 */
     2013DECLHIDDEN(void) supR3HardenedWinCreateParentWatcherThread(HMODULE hVBoxRT)
     2014{
     2015    /*
     2016     * Resolve runtime methods that we need.
     2017     */
     2018    PFNRTTHREADCREATE pfnRTThreadCreate = (PFNRTTHREADCREATE)GetProcAddress(hVBoxRT, "RTThreadCreate");
     2019    SUPR3HARDENED_ASSERT(pfnRTThreadCreate != NULL);
     2020
     2021    /*
     2022     * Find the parent process ID.
     2023     */
     2024    PROCESS_BASIC_INFORMATION BasicInfo;
     2025    NTSTATUS rcNt = NtQueryInformationProcess(NtCurrentProcess(), ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
     2026    if (!NT_SUCCESS(rcNt))
     2027        supR3HardenedFatal("supR3HardenedWinCreateParentWatcherThread: NtQueryInformationProcess failed: %#x\n", rcNt);
     2028
     2029    /*
     2030     * Open the parent process for waiting and exitcode query.
     2031     */
     2032    OBJECT_ATTRIBUTES ObjAttr;
     2033    InitializeObjectAttributes(&ObjAttr, NULL, 0, NULL /*hRootDir*/, NULL /*pSecDesc*/);
     2034
     2035    CLIENT_ID ClientId;
     2036    ClientId.UniqueProcess = (HANDLE)BasicInfo.InheritedFromUniqueProcessId;
     2037    ClientId.UniqueThread  = NULL;
     2038#if 0 /** @todo fix me later. */
     2039    HANDLE hParent;
     2040    rcNt = NtOpenProcess(&hParent, SYNCHRONIZE | PROCESS_QUERY_INFORMATION, &ObjAttr, &ClientId);
     2041    if (!NT_SUCCESS(rcNt))
     2042        supR3HardenedFatalMsg("supR3HardenedWinCreateParentWatcherThread", kSupInitOp_Misc, VERR_GENERAL_FAILUREps,
     2043                              "NtOpenProcess(%p.0) failed: %#x\n", ClientId.UniqueProcess, rcNt);
     2044
     2045    /*
     2046     * Create the thread that should do the waiting.
     2047     */
     2048    int rc = pfnRTThreadCreate(NULL, supR3HardenedWinParentWatcherThread, hParent, _64K /* stack */,
     2049                               RTTHREADTYPE_DEFAULT, 0 /*fFlags*/, "ParentWatcher");
     2050    if (RT_FAILURE(rc))
     2051        supR3HardenedFatal("supR3HardenedWinCreateParentWatcherThread: RTThreadCreate failed: %Rrc\n", rc);
     2052#endif
     2053}
     2054
     2055
     2056/**
    19552057 * Install hooks for intercepting calls dealing with mapping shared libraries
    19562058 * into the process.
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette