Changeset 54998 in vbox for trunk/src/VBox
- Timestamp:
- Mar 28, 2015 7:57:12 PM (10 years ago)
- Location:
- trunk/src/VBox
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/Support/SUPLibInternal.h
r53002 r54998 92 92 # define supR3HardenedLogV supR3HardenedStaticLogV 93 93 # define supR3HardenedLog supR3HardenedStaticLog 94 # define supR3HardenedLogFlush supR3HardenedStaticLogFlush 94 95 # define supR3HardenedVerifyAll supR3HardenedStaticVerifyAll 95 96 # define supR3HardenedVerifyFixedDir supR3HardenedStaticVerifyFixedDir … … 433 434 */ 434 435 DECLHIDDEN(void) supR3HardenedLog(const char *pszFormat, ...); 436 437 /** 438 * Flushes the log file. 439 */ 440 DECLHIDDEN(void) supR3HardenedLogFlush(void); 435 441 436 442 -
trunk/src/VBox/HostDrivers/Support/SUPR3HardenedMain.cpp
r53045 r54998 169 169 * release log at runtime. */ 170 170 static PFNRTLOGRELPRINTF g_pfnRTLogRelPrintf = NULL; 171 /** Log volume name (for attempting volume flush). */ 172 static RTUTF16 g_wszStartupLogVol[16]; 171 173 #endif 172 174 … … 1002 1004 NULL); 1003 1005 if (RT_SUCCESS(rc)) 1006 { 1004 1007 SUP_DPRINTF(("Log file opened: " VBOX_VERSION_STRING "r%u g_hStartupLog=%p g_uNtVerCombined=%#x\n", 1005 1008 VBOX_SVN_REV, g_hStartupLog, g_uNtVerCombined)); 1009 1010 /* 1011 * If the path contains a drive volume, save it so we can 1012 * use it to flush the volume containing the log file. 1013 */ 1014 if (RT_C_IS_ALPHA(pszLogFile[0]) && pszLogFile[1] == ':') 1015 { 1016 RTUtf16CopyAscii(g_wszStartupLogVol, RT_ELEMENTS(g_wszStartupLogVol), "\\??\\"); 1017 g_wszStartupLogVol[sizeof("\\??\\") - 1] = RT_C_TO_UPPER(pszLogFile[0]); 1018 g_wszStartupLogVol[sizeof("\\??\\") + 0] = ':'; 1019 g_wszStartupLogVol[sizeof("\\??\\") + 1] = '\0'; 1020 } 1021 } 1006 1022 else 1007 1023 g_hStartupLog = NULL; … … 1051 1067 supR3HardenedLogV(pszFormat, va); 1052 1068 va_end(va); 1069 } 1070 1071 1072 DECLHIDDEN(void) supR3HardenedLogFlush(void) 1073 { 1074 #ifdef RT_OS_WINDOWS 1075 if ( g_hStartupLog != NULL 1076 && g_cbStartupLog < 16*_1M) 1077 { 1078 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER; 1079 NTSTATUS rcNt = NtFlushBuffersFile(g_hStartupLog, &Ios); 1080 1081 /* 1082 * Try flush the volume containing the log file too. 1083 */ 1084 if (g_wszStartupLogVol[0]) 1085 { 1086 HANDLE hLogVol = RTNT_INVALID_HANDLE_VALUE; 1087 UNICODE_STRING NtName; 1088 NtName.Buffer = g_wszStartupLogVol; 1089 NtName.Length = (USHORT)(RTUtf16Len(g_wszStartupLogVol) * sizeof(RTUTF16)); 1090 NtName.MaximumLength = NtName.Length + 1; 1091 OBJECT_ATTRIBUTES ObjAttr; 1092 InitializeObjectAttributes(&ObjAttr, &NtName, OBJ_CASE_INSENSITIVE, NULL /*hRootDir*/, NULL /*pSecDesc*/); 1093 RTNT_IO_STATUS_BLOCK_REINIT(&Ios); 1094 rcNt = NtCreateFile(&hLogVol, 1095 GENERIC_WRITE | GENERIC_READ | SYNCHRONIZE | FILE_READ_ATTRIBUTES, 1096 &ObjAttr, 1097 &Ios, 1098 NULL /* Allocation Size*/, 1099 0 /*FileAttributes*/, 1100 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 1101 FILE_OPEN, 1102 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, 1103 NULL /*EaBuffer*/, 1104 0 /*EaLength*/); 1105 if (NT_SUCCESS(rcNt)) 1106 rcNt = Ios.Status; 1107 if (NT_SUCCESS(rcNt)) 1108 { 1109 RTNT_IO_STATUS_BLOCK_REINIT(&Ios); 1110 rcNt = NtFlushBuffersFile(hLogVol, &Ios); 1111 NtClose(hLogVol); 1112 } 1113 else 1114 { 1115 /* This may have sideeffects similar to what we want... */ 1116 hLogVol = RTNT_INVALID_HANDLE_VALUE; 1117 RTNT_IO_STATUS_BLOCK_REINIT(&Ios); 1118 rcNt = NtCreateFile(&hLogVol, 1119 GENERIC_READ | SYNCHRONIZE | FILE_READ_ATTRIBUTES, 1120 &ObjAttr, 1121 &Ios, 1122 NULL /* Allocation Size*/, 1123 0 /*FileAttributes*/, 1124 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 1125 FILE_OPEN, 1126 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, 1127 NULL /*EaBuffer*/, 1128 0 /*EaLength*/); 1129 if (NT_SUCCESS(rcNt) && NT_SUCCESS(Ios.Status)) 1130 NtClose(hLogVol); 1131 } 1132 } 1133 } 1134 #else 1135 /* later */ 1136 #endif 1053 1137 } 1054 1138 -
trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyProcess-win.cpp
r54993 r54998 1545 1545 * Frees (or replaces) executable memory of allocation type private. 1546 1546 * 1547 * @returns VBox status code. 1547 * @returns True if nothing really bad happen, false if to quit ASAP because we 1548 * killed the process being scanned. 1548 1549 * @param pThis The process scanning state structure. Details 1549 1550 * about images are added to this. … … 1552 1553 * executable memory. 1553 1554 */ 1554 static voidsupHardNtVpFreeOrReplacePrivateExecMemory(PSUPHNTVPSTATE pThis, HANDLE hProcess,1555 static bool supHardNtVpFreeOrReplacePrivateExecMemory(PSUPHNTVPSTATE pThis, HANDLE hProcess, 1555 1556 MEMORY_BASIC_INFORMATION const *pMemInfo) 1556 1557 { … … 1596 1597 { 1597 1598 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED, "RTMemAllocZ(%#zx) failed", cbCopy); 1598 return ;1599 return true; 1599 1600 } 1600 1601 … … 1603 1604 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED, 1604 1605 "Error reading data from original alloc: %#x (%p LB %#zx)", rcNt, uCopySrc, cbCopy, rcNt); 1606 supR3HardenedLogFlush(); 1605 1607 } 1606 1608 … … 1613 1615 SIZE_T cbFreeInOut = 0; 1614 1616 rcNt = NtFreeVirtualMemory(hProcess, &pvFreeInOut, &cbFreeInOut, MEM_RELEASE); 1615 if (!NT_SUCCESS(rcNt)) 1617 if (NT_SUCCESS(rcNt)) 1618 { 1619 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #1 succeeded: %#x [%p/%p LB 0/%#zx]\n", 1620 rcNt, pvFree, pvFreeInOut, cbFreeInOut)); 1621 supR3HardenedLogFlush(); 1622 } 1623 else 1616 1624 { 1617 1625 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #1 failed: %#x [%p LB 0]\n", rcNt, pvFree)); 1626 supR3HardenedLogFlush(); 1618 1627 pvFreeInOut = pvFree; 1619 1628 cbFreeInOut = cbFree; 1620 1629 rcNt = NtFreeVirtualMemory(hProcess, &pvFreeInOut, &cbFreeInOut, MEM_RELEASE); 1621 if (!NT_SUCCESS(rcNt)) 1630 if (NT_SUCCESS(rcNt)) 1631 { 1632 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #2 succeeded: %#x [%p/%p LB %#zx/%#zx]\n", 1633 rcNt, pvFree, pvFreeInOut, cbFree, cbFreeInOut)); 1634 supR3HardenedLogFlush(); 1635 } 1636 else 1622 1637 { 1623 1638 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #2 failed: %#x [%p LB %#zx]\n", 1624 1639 rcNt, pvFree, cbFree)); 1640 supR3HardenedLogFlush(); 1625 1641 pvFreeInOut = pMemInfo->BaseAddress; 1626 1642 cbFreeInOut = pMemInfo->RegionSize; … … 1632 1648 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #3 succeeded [%p LB %#zx]\n", 1633 1649 pvFree, cbFree)); 1650 supR3HardenedLogFlush(); 1634 1651 } 1635 1652 else … … 1643 1660 * Query the region again, redo the free operation if there's still memory there. 1644 1661 */ 1645 if (!NT_SUCCESS(rcNt) || (pThis->fFlags & SUPHARDNTVP_F_EXEC_ALLOC_REPLACE_WITH_RW))1662 if (!NT_SUCCESS(rcNt) || !(pThis->fFlags & SUPHARDNTVP_F_EXEC_ALLOC_REPLACE_WITH_RW)) 1646 1663 break; 1647 1664 SIZE_T cbActual = 0; … … 1654 1671 i, MemInfo3.AllocationBase, MemInfo3.BaseAddress, MemInfo3.RegionSize, MemInfo3.State, 1655 1672 MemInfo3.AllocationProtect, MemInfo3.Protect)); 1673 supR3HardenedLogFlush(); 1656 1674 if (pMemInfo->State == MEM_FREE) 1657 1675 break; 1676 NtYieldExecution(); 1658 1677 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Retrying free...\n")); 1678 supR3HardenedLogFlush(); 1659 1679 } 1660 1680 … … 1669 1689 rcNt = NtAllocateVirtualMemory(hProcess, &pvAlloc, 0, &cbAlloc, MEM_COMMIT, PAGE_READWRITE); 1670 1690 if (!NT_SUCCESS(rcNt)) 1691 { 1671 1692 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED, 1672 1693 "NtAllocateVirtualMemory (%p LB %#zx) failed with rcNt=%#x allocating " … … 1674 1695 "See VBoxStartup.log for more details", 1675 1696 pvAlloc, cbFree, rcNt); 1676 else if ( (uintptr_t)pvFree < (uintptr_t)pvAlloc 1677 || (uintptr_t)pvFree + cbFree > (uintptr_t)pvAlloc + cbFree) 1697 supR3HardenedLogFlush(); 1698 NtTerminateProcess(hProcess, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED); 1699 return false; 1700 } 1701 1702 if ( (uintptr_t)pvFree < (uintptr_t)pvAlloc 1703 || (uintptr_t)pvFree + cbFree > (uintptr_t)pvAlloc + cbFree) 1704 { 1678 1705 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED, 1679 1706 "We wanted NtAllocateVirtualMemory to get us %p LB %#zx, but it returned %p LB %#zx.", 1680 1707 pMemInfo->BaseAddress, pMemInfo->RegionSize, pvFree, cbFree, rcNt); 1708 supR3HardenedLogFlush(); 1709 NtTerminateProcess(hProcess, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED); 1710 return false; 1711 } 1712 1713 /* 1714 * Copy what we can, considering the 2nd free attempt. 1715 */ 1716 uint8_t *pbDst = (uint8_t *)pvFree; 1717 size_t cbDst = cbFree; 1718 uint8_t *pbSrc = (uint8_t *)pvCopy; 1719 size_t cbSrc = cbCopy; 1720 if ((uintptr_t)pbDst != uCopySrc) 1721 { 1722 if ((uintptr_t)pbDst > uCopySrc) 1723 { 1724 uintptr_t cbAdj = (uintptr_t)pbDst - uCopySrc; 1725 pbSrc += cbAdj; 1726 cbSrc -= cbSrc; 1727 } 1728 else 1729 { 1730 uintptr_t cbAdj = uCopySrc - (uintptr_t)pbDst; 1731 pbDst += cbAdj; 1732 cbDst -= cbAdj; 1733 } 1734 } 1735 if (cbSrc > cbDst) 1736 cbSrc = cbDst; 1737 1738 SIZE_T cbWritten; 1739 rcNt = NtWriteVirtualMemory(hProcess, pbDst, pbSrc, cbSrc, &cbWritten); 1740 if (NT_SUCCESS(rcNt)) 1741 { 1742 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Restored the exec memory as non-exec.\n")); 1743 supR3HardenedLogFlush(); 1744 } 1681 1745 else 1682 1746 { 1683 /* 1684 * Copy what we can, considering the 2nd free attempt. 1685 */ 1686 uint8_t *pbDst = (uint8_t *)pvFree; 1687 size_t cbDst = cbFree; 1688 uint8_t *pbSrc = (uint8_t *)pvCopy; 1689 size_t cbSrc = cbCopy; 1690 if ((uintptr_t)pbDst != uCopySrc) 1691 { 1692 if ((uintptr_t)pbDst > uCopySrc) 1693 { 1694 uintptr_t cbAdj = (uintptr_t)pbDst - uCopySrc; 1695 pbSrc += cbAdj; 1696 cbSrc -= cbSrc; 1697 } 1698 else 1699 { 1700 uintptr_t cbAdj = uCopySrc - (uintptr_t)pbDst; 1701 pbDst += cbAdj; 1702 cbDst -= cbAdj; 1703 } 1704 } 1705 if (cbSrc > cbDst) 1706 cbSrc = cbDst; 1707 1708 SIZE_T cbWritten; 1709 rcNt = NtWriteVirtualMemory(hProcess, pbDst, pbSrc, cbSrc, &cbWritten); 1710 if (!NT_SUCCESS(rcNt)) 1711 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_FREE_VIRTUAL_MEMORY_FAILED, 1712 "NtWriteVirtualMemory (%p LB %#zx) failed: %#x", 1713 pMemInfo->BaseAddress, pMemInfo->RegionSize, rcNt); 1747 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_FREE_VIRTUAL_MEMORY_FAILED, 1748 "NtWriteVirtualMemory (%p LB %#zx) failed: %#x", 1749 pMemInfo->BaseAddress, pMemInfo->RegionSize, rcNt); 1750 supR3HardenedLogFlush(); 1751 NtTerminateProcess(hProcess, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED); 1752 return false; 1714 1753 } 1715 1754 } 1716 1755 if (pvCopy) 1717 1756 RTMemFree(pvCopy); 1757 return true; 1718 1758 } 1719 1759 #endif /* IN_RING3 */ … … 1857 1897 */ 1858 1898 if (MemInfo.Type == MEM_PRIVATE) 1859 supHardNtVpFreeOrReplacePrivateExecMemory(pThis, hProcess, &MemInfo); 1899 { 1900 if (!supHardNtVpFreeOrReplacePrivateExecMemory(pThis, hProcess, &MemInfo)) 1901 break; 1902 } 1860 1903 /* 1861 1904 * Unmap mapped memory, failing that, drop exec privileges. -
trunk/src/VBox/HostDrivers/Support/win/import-template-ntdll.h
r53822 r54998 8 8 SUPHARNT_IMPORT_SYSCALL(NtDeviceIoControlFile, 40) 9 9 SUPHARNT_IMPORT_SYSCALL(NtDuplicateObject, 28) 10 SUPHARNT_IMPORT_SYSCALL(NtFlushBuffersFile, 8) 10 11 SUPHARNT_IMPORT_SYSCALL(NtFreeVirtualMemory, 16) 11 12 SUPHARNT_IMPORT_SYSCALL(NtGetContextThread, 8) -
trunk/src/VBox/Runtime/r3/win/ntdll-mini-implib.def
r53819 r54998 5 5 6 6 ; 7 ; Copyright (C) 2010-201 4Oracle Corporation7 ; Copyright (C) 2010-2015 Oracle Corporation 8 8 ; 9 9 ; This file is part of VirtualBox Open Source Edition (OSE), as … … 42 42 NtDeviceIoControlFile ;;= _NtDeviceIoControlFile@40 43 43 NtDuplicateObject ;;= _NtDuplicateObject@28 44 NtFlushBuffersFile ;;= _NtFlushBuffersFile@8 44 45 NtFreeVirtualMemory ;;= _NtFreeVirtualMemory@16 45 46 NtGetContextThread ;;= _NtGetContextThread@8
Note:
See TracChangeset
for help on using the changeset viewer.