Skip to content

Commit

Permalink
Merge pull request #452 from processhacker/master
Browse files Browse the repository at this point in the history
[pull] master from processhacker:master
  • Loading branch information
pull[bot] authored Apr 16, 2022
2 parents 60d3e4f + 4e01ab2 commit acf1fc2
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 140 deletions.
276 changes: 138 additions & 138 deletions ProcessHacker/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1791,141 +1791,141 @@ VOID PhpEnablePrivileges(
// breaking backwards compatibility. (dmex)
// TODO: Move to a better location. (dmex)

PH_QUEUED_LOCK PhDelayLoadImportLock = PH_QUEUED_LOCK_INIT;
ULONG PhDelayLoadOldProtection = PAGE_WRITECOPY;
ULONG PhDelayLoadLockCount = 0;

// based on \MSVC\14.31.31103\include\dloadsup.h (dmex)
VOID PhDelayLoadImportAcquire(
_In_ PVOID ImportDirectorySectionAddress,
_In_ SIZE_T ImportDirectorySectionSize
)
{
PhAcquireQueuedLockExclusive(&PhDelayLoadImportLock);
PhDelayLoadLockCount += 1;

if (PhDelayLoadLockCount == 1)
{
NTSTATUS status;

if (!NT_SUCCESS(status = NtProtectVirtualMemory(
NtCurrentProcess(),
&ImportDirectorySectionAddress,
&ImportDirectorySectionSize,
PAGE_READWRITE,
&PhDelayLoadOldProtection
)))
{
PhRaiseStatus(status);
}
}

PhReleaseQueuedLockExclusive(&PhDelayLoadImportLock);
}

VOID PhDelayLoadImportRelease(
_In_ PVOID ImportDirectorySectionAddress,
_In_ SIZE_T ImportDirectorySectionSize
)
{
PhAcquireQueuedLockExclusive(&PhDelayLoadImportLock);
PhDelayLoadLockCount -= 1;

if (PhDelayLoadLockCount == 0)
{
ULONG importSectionOldProtection;
NtProtectVirtualMemory(
NtCurrentProcess(),
&ImportDirectorySectionAddress,
&ImportDirectorySectionSize,
PhDelayLoadOldProtection,
&importSectionOldProtection
);
}

PhReleaseQueuedLockExclusive(&PhDelayLoadImportLock);
}

_Success_(return != NULL)
PVOID WINAPI __delayLoadHelper2(
_In_ PIMAGE_DELAYLOAD_DESCRIPTOR Entry,
_Inout_ PVOID* ImportAddress
)
{
BOOLEAN importNeedsFree = FALSE;
PSTR importDllName;
PVOID procedureAddress;
PVOID moduleHandle;
PVOID* importHandle;
PIMAGE_THUNK_DATA importEntry;
PIMAGE_THUNK_DATA importTable;
PIMAGE_THUNK_DATA importNameTable;
PIMAGE_NT_HEADERS imageNtHeaders;
SIZE_T importDirectorySectionSize;
PVOID importDirectorySectionAddress;

importDllName = PTR_ADD_OFFSET(PhInstanceHandle, Entry->DllNameRVA);
importHandle = PTR_ADD_OFFSET(PhInstanceHandle, Entry->ModuleHandleRVA);
importTable = PTR_ADD_OFFSET(PhInstanceHandle, Entry->ImportAddressTableRVA);
importNameTable = PTR_ADD_OFFSET(PhInstanceHandle, Entry->ImportNameTableRVA);

if (!(moduleHandle = *importHandle))
{
PPH_STRING importDllNameSr = PhZeroExtendToUtf16(importDllName);

if (!(moduleHandle = PhLoadLibrary(importDllNameSr->Buffer)))
{
PhDereferenceObject(importDllNameSr);
return NULL;
}

PhDereferenceObject(importDllNameSr);
importNeedsFree = TRUE;
}

importEntry = PTR_ADD_OFFSET(importNameTable, PTR_SUB_OFFSET(ImportAddress, importTable));

if (IMAGE_SNAP_BY_ORDINAL(importEntry->u1.Ordinal))
{
USHORT procedureOrdinal = IMAGE_ORDINAL(importEntry->u1.Ordinal);
procedureAddress = PhGetDllBaseProcedureAddress(moduleHandle, NULL, procedureOrdinal);
}
else
{
PIMAGE_IMPORT_BY_NAME importByName = PTR_ADD_OFFSET(PhInstanceHandle, importEntry->u1.AddressOfData);
procedureAddress = PhGetDllBaseProcedureAddressWithHint(moduleHandle, importByName->Name, importByName->Hint);
}

if (!procedureAddress)
return NULL;

if (!NT_SUCCESS(PhGetLoaderEntryImageNtHeaders(
PhInstanceHandle,
&imageNtHeaders
)))
{
return NULL;
}

if (!NT_SUCCESS(PhGetLoaderEntryImageVaToSection(
PhInstanceHandle,
imageNtHeaders,
importTable,
&importDirectorySectionAddress,
&importDirectorySectionSize
)))
{
return NULL;
}

PhDelayLoadImportAcquire(importDirectorySectionAddress, importDirectorySectionSize);
InterlockedExchangePointer(ImportAddress, procedureAddress);
PhDelayLoadImportRelease(importDirectorySectionAddress, importDirectorySectionSize);

if ((InterlockedExchangePointer(importHandle, moduleHandle) == moduleHandle) && importNeedsFree)
{
FreeLibrary(moduleHandle); // A different thread has already updated the cache. (dmex)
}

return procedureAddress;
}
//PH_QUEUED_LOCK PhDelayLoadImportLock = PH_QUEUED_LOCK_INIT;
//ULONG PhDelayLoadOldProtection = PAGE_WRITECOPY;
//ULONG PhDelayLoadLockCount = 0;
//
//// based on \MSVC\14.31.31103\include\dloadsup.h (dmex)
//VOID PhDelayLoadImportAcquire(
// _In_ PVOID ImportDirectorySectionAddress,
// _In_ SIZE_T ImportDirectorySectionSize
// )
//{
// PhAcquireQueuedLockExclusive(&PhDelayLoadImportLock);
// PhDelayLoadLockCount += 1;
//
// if (PhDelayLoadLockCount == 1)
// {
// NTSTATUS status;
//
// if (!NT_SUCCESS(status = NtProtectVirtualMemory(
// NtCurrentProcess(),
// &ImportDirectorySectionAddress,
// &ImportDirectorySectionSize,
// PAGE_READWRITE,
// &PhDelayLoadOldProtection
// )))
// {
// PhRaiseStatus(status);
// }
// }
//
// PhReleaseQueuedLockExclusive(&PhDelayLoadImportLock);
//}
//
//VOID PhDelayLoadImportRelease(
// _In_ PVOID ImportDirectorySectionAddress,
// _In_ SIZE_T ImportDirectorySectionSize
// )
//{
// PhAcquireQueuedLockExclusive(&PhDelayLoadImportLock);
// PhDelayLoadLockCount -= 1;
//
// if (PhDelayLoadLockCount == 0)
// {
// ULONG importSectionOldProtection;
// NtProtectVirtualMemory(
// NtCurrentProcess(),
// &ImportDirectorySectionAddress,
// &ImportDirectorySectionSize,
// PhDelayLoadOldProtection,
// &importSectionOldProtection
// );
// }
//
// PhReleaseQueuedLockExclusive(&PhDelayLoadImportLock);
//}
//
//_Success_(return != NULL)
//PVOID WINAPI __delayLoadHelper2(
// _In_ PIMAGE_DELAYLOAD_DESCRIPTOR Entry,
// _Inout_ PVOID* ImportAddress
// )
//{
// BOOLEAN importNeedsFree = FALSE;
// PSTR importDllName;
// PVOID procedureAddress;
// PVOID moduleHandle;
// PVOID* importHandle;
// PIMAGE_THUNK_DATA importEntry;
// PIMAGE_THUNK_DATA importTable;
// PIMAGE_THUNK_DATA importNameTable;
// PIMAGE_NT_HEADERS imageNtHeaders;
// SIZE_T importDirectorySectionSize;
// PVOID importDirectorySectionAddress;
//
// importDllName = PTR_ADD_OFFSET(PhInstanceHandle, Entry->DllNameRVA);
// importHandle = PTR_ADD_OFFSET(PhInstanceHandle, Entry->ModuleHandleRVA);
// importTable = PTR_ADD_OFFSET(PhInstanceHandle, Entry->ImportAddressTableRVA);
// importNameTable = PTR_ADD_OFFSET(PhInstanceHandle, Entry->ImportNameTableRVA);
//
// if (!(moduleHandle = *importHandle))
// {
// PPH_STRING importDllNameSr = PhZeroExtendToUtf16(importDllName);
//
// if (!(moduleHandle = PhLoadLibrary(importDllNameSr->Buffer)))
// {
// PhDereferenceObject(importDllNameSr);
// return NULL;
// }
//
// PhDereferenceObject(importDllNameSr);
// importNeedsFree = TRUE;
// }
//
// importEntry = PTR_ADD_OFFSET(importNameTable, PTR_SUB_OFFSET(ImportAddress, importTable));
//
// if (IMAGE_SNAP_BY_ORDINAL(importEntry->u1.Ordinal))
// {
// USHORT procedureOrdinal = IMAGE_ORDINAL(importEntry->u1.Ordinal);
// procedureAddress = PhGetDllBaseProcedureAddress(moduleHandle, NULL, procedureOrdinal);
// }
// else
// {
// PIMAGE_IMPORT_BY_NAME importByName = PTR_ADD_OFFSET(PhInstanceHandle, importEntry->u1.AddressOfData);
// procedureAddress = PhGetDllBaseProcedureAddressWithHint(moduleHandle, importByName->Name, importByName->Hint);
// }
//
// if (!procedureAddress)
// return NULL;
//
// if (!NT_SUCCESS(PhGetLoaderEntryImageNtHeaders(
// PhInstanceHandle,
// &imageNtHeaders
// )))
// {
// return NULL;
// }
//
// if (!NT_SUCCESS(PhGetLoaderEntryImageVaToSection(
// PhInstanceHandle,
// imageNtHeaders,
// importTable,
// &importDirectorySectionAddress,
// &importDirectorySectionSize
// )))
// {
// return NULL;
// }
//
// PhDelayLoadImportAcquire(importDirectorySectionAddress, importDirectorySectionSize);
// InterlockedExchangePointer(ImportAddress, procedureAddress);
// PhDelayLoadImportRelease(importDirectorySectionAddress, importDirectorySectionSize);
//
// if ((InterlockedExchangePointer(importHandle, moduleHandle) == moduleHandle) && importNeedsFree)
// {
// FreeLibrary(moduleHandle); // A different thread has already updated the cache. (dmex)
// }
//
// return procedureAddress;
//}
9 changes: 7 additions & 2 deletions phlib/hndlinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,17 @@ PPH_STRING PhGetPnPDeviceName(
else
deviceDesc = PhCreateString2(&displayDesc);

if (deviceName->Length >= sizeof(UNICODE_NULL) && deviceName->Buffer[deviceName->Length / sizeof(WCHAR)] == UNICODE_NULL)
deviceName->Length -= sizeof(UNICODE_NULL); // PhTrimToNullTerminatorString(deviceName);
if (deviceDesc->Length >= sizeof(UNICODE_NULL) && deviceDesc->Buffer[deviceDesc->Length / sizeof(WCHAR)] == UNICODE_NULL)
deviceDesc->Length -= sizeof(UNICODE_NULL); // PhTrimToNullTerminatorString(deviceDesc);

if (!PhIsNullOrEmptyString(deviceDesc))
{
PH_FORMAT format[4];

PhInitFormatSR(&format[0], deviceDesc->sr);
PhInitFormatS(&format[1], L"(PDO: ");
PhInitFormatS(&format[1], L" (PDO: ");
PhInitFormatSR(&format[2], ObjectName->sr);
PhInitFormatC(&format[3], ')');

Expand All @@ -693,7 +698,7 @@ PPH_STRING PhGetPnPDeviceName(
PH_FORMAT format[4];

PhInitFormatSR(&format[0], deviceName->sr);
PhInitFormatS(&format[1], L"(PDO: ");
PhInitFormatS(&format[1], L" (PDO: ");
PhInitFormatSR(&format[2], ObjectName->sr);
PhInitFormatC(&format[3], ')');

Expand Down

0 comments on commit acf1fc2

Please sign in to comment.