Skip to content

Commit 3c1cb4f

Browse files
committed
Fix instance bug updating/running with multiple logged on users
1 parent c776627 commit 3c1cb4f

File tree

4 files changed

+142
-126
lines changed

4 files changed

+142
-126
lines changed

ProcessHacker/main.c

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -277,31 +277,19 @@ INT WINAPI wWinMain(
277277

278278
// Create a mutant for the installer.
279279
{
280-
HANDLE mutantHandle;
281-
OBJECT_ATTRIBUTES oa;
282-
UNICODE_STRING mutantName;
283-
PPH_STRING objectName;
284-
PH_FORMAT format[4];
285-
286-
PhInitFormatS(&format[0], L"PhMainWindow_");
287-
PhInitFormatU(&format[1], NtCurrentPeb()->SessionId);
288-
PhInitFormatS(&format[2], L"_");
289-
PhInitFormatU(&format[3], HandleToUlong(NtCurrentProcessId()));
290-
291-
objectName = PhFormat(format, 4, 16);
292-
PhStringRefToUnicodeString(&objectName->sr, &mutantName);
280+
static UNICODE_STRING objectNameUs = RTL_CONSTANT_STRING(L"PhMutant");
281+
OBJECT_ATTRIBUTES objectAttributes;
282+
HANDLE objectHandle;
293283

294284
InitializeObjectAttributes(
295-
&oa,
296-
&mutantName,
285+
&objectAttributes,
286+
&objectNameUs,
297287
OBJ_CASE_INSENSITIVE,
298288
PhGetNamespaceHandle(),
299289
NULL
300290
);
301291

302-
NtCreateMutant(&mutantHandle, MUTANT_ALL_ACCESS, &oa, TRUE);
303-
304-
PhDereferenceObject(objectName);
292+
NtCreateMutant(&objectHandle, MUTANT_ALL_ACCESS, &objectAttributes, TRUE);
305293
}
306294

307295
// Set the default priority.
@@ -463,27 +451,68 @@ static BOOLEAN NTAPI PhpPreviousInstancesCallback(
463451
_In_opt_ PVOID Context
464452
)
465453
{
466-
if (PhStartsWithStringRef2(Name, L"PhMainWindow_", TRUE))
454+
static PH_STRINGREF objectNameSr = PH_STRINGREF_INIT(L"PhMutant");
455+
HANDLE objectHandle;
456+
UNICODE_STRING objectNameUs;
457+
OBJECT_ATTRIBUTES objectAttributes;
458+
MUTANT_OWNER_INFORMATION objectInfo;
459+
460+
if (!PhEqualStringRef(Name, &objectNameSr, FALSE))
461+
return TRUE;
462+
if (!PhStringRefToUnicodeString(Name, &objectNameUs))
463+
return TRUE;
464+
465+
InitializeObjectAttributes(
466+
&objectAttributes,
467+
&objectNameUs,
468+
OBJ_CASE_INSENSITIVE,
469+
PhGetNamespaceHandle(),
470+
NULL
471+
);
472+
473+
if (!NT_SUCCESS(NtOpenMutant(
474+
&objectHandle,
475+
MUTANT_QUERY_STATE,
476+
&objectAttributes
477+
)))
478+
{
479+
return TRUE;
480+
}
481+
482+
if (NT_SUCCESS(NtQueryMutant(
483+
objectHandle,
484+
MutantOwnerInformation,
485+
&objectInfo,
486+
sizeof(MUTANT_OWNER_INFORMATION),
487+
NULL
488+
)))
467489
{
468490
HWND hwnd;
469-
ULONG64 sessionId64;
470-
ULONG64 processId64;
471-
PH_STRINGREF remaining;
472-
PH_STRINGREF sessionIdPart;
473-
PH_STRINGREF processIdPart;
474-
475-
if (!PhSplitStringRefAtChar(Name, L'_', &remaining, &remaining))
476-
return TRUE;
477-
if (!PhSplitStringRefAtChar(&remaining, L'_', &sessionIdPart, &processIdPart))
478-
return TRUE;
479-
if (!PhStringToInteger64(&sessionIdPart, 10, &sessionId64))
480-
return TRUE;
481-
if (!PhStringToInteger64(&processIdPart, 10, &processId64))
482-
return TRUE;
483-
if (NtCurrentPeb()->SessionId != sessionId64)
484-
return TRUE;
485-
486-
if (hwnd = PhGetProcessMainWindowEx(UlongToHandle((ULONG)processId64), NULL, FALSE))
491+
HANDLE processHandle = NULL;
492+
HANDLE tokenHandle = NULL;
493+
PTOKEN_USER tokenCurrent = NULL;
494+
PTOKEN_USER tokenUser = NULL;
495+
496+
if (objectInfo.ClientId.UniqueProcess == NtCurrentProcessId())
497+
goto CleanupExit;
498+
if (!NT_SUCCESS(PhOpenProcess(&processHandle, ProcessQueryAccess, objectInfo.ClientId.UniqueProcess)))
499+
goto CleanupExit;
500+
if (!NT_SUCCESS(PhOpenProcessToken(processHandle, TOKEN_QUERY, &tokenHandle)))
501+
goto CleanupExit;
502+
if (!NT_SUCCESS(PhGetTokenUser(tokenHandle, &tokenUser)))
503+
goto CleanupExit;
504+
if (!NT_SUCCESS(PhGetTokenUser(PhGetOwnTokenAttributes().TokenHandle, &tokenCurrent)))
505+
goto CleanupExit;
506+
if (!RtlEqualSid(tokenUser->User.Sid, tokenCurrent->User.Sid))
507+
goto CleanupExit;
508+
509+
hwnd = PhGetProcessMainWindowEx(
510+
objectInfo.ClientId.UniqueProcess,
511+
processHandle,
512+
FALSE
513+
);
514+
515+
if (hwnd)
487516
{
488517
ULONG_PTR result;
489518

@@ -495,8 +524,15 @@ static BOOLEAN NTAPI PhpPreviousInstancesCallback(
495524
RtlExitUserProcess(STATUS_SUCCESS);
496525
}
497526
}
527+
528+
CleanupExit:
529+
if (tokenUser) PhFree(tokenUser);
530+
if (tokenCurrent) PhFree(tokenCurrent);
531+
if (tokenHandle) NtClose(tokenHandle);
532+
if (processHandle) NtClose(processHandle);
498533
}
499534

535+
NtClose(objectHandle);
500536
return TRUE;
501537
}
502538

tools/CustomSetupTool/CustomSetupTool/appsup.c

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -432,76 +432,80 @@ static BOOLEAN NTAPI PhpPreviousInstancesCallback(
432432
_In_opt_ PVOID Context
433433
)
434434
{
435-
if (
436-
PhStartsWithStringRef2(Name, L"PhMainWindow_", TRUE) ||
437-
PhStartsWithStringRef2(Name, L"PhSetupWindow_", TRUE) ||
438-
PhStartsWithStringRef2(Name, L"PeViewerWindow_", TRUE)
439-
)
435+
HANDLE objectHandle;
436+
UNICODE_STRING objectNameUs;
437+
OBJECT_ATTRIBUTES objectAttributes;
438+
MUTANT_OWNER_INFORMATION objectInfo;
439+
440+
if (!PhEqualStringRef2(Name, L"PhMutant", TRUE) &&
441+
!PhEqualStringRef2(Name, L"PhSetupMutant", TRUE) &&
442+
!PhEqualStringRef2(Name, L"PeViewerMutant", TRUE))
443+
{
444+
return TRUE;
445+
}
446+
447+
if (!PhStringRefToUnicodeString(Name, &objectNameUs))
448+
return TRUE;
449+
450+
InitializeObjectAttributes(
451+
&objectAttributes,
452+
&objectNameUs,
453+
OBJ_CASE_INSENSITIVE,
454+
PhGetNamespaceHandle(),
455+
NULL
456+
);
457+
458+
if (!NT_SUCCESS(NtOpenMutant(
459+
&objectHandle,
460+
MUTANT_QUERY_STATE,
461+
&objectAttributes
462+
)))
463+
{
464+
return TRUE;
465+
}
466+
467+
if (NT_SUCCESS(NtQueryMutant(
468+
objectHandle,
469+
MutantOwnerInformation,
470+
&objectInfo,
471+
sizeof(MUTANT_OWNER_INFORMATION),
472+
NULL
473+
)))
440474
{
441-
HANDLE processHandle;
442475
HWND hwnd;
443-
ULONG64 sessionId64;
444-
ULONG64 processId64;
445-
PH_STRINGREF remaining;
446-
PH_STRINGREF sessionIdPart;
447-
PH_STRINGREF processIdPart;
476+
HANDLE processHandle = NULL;
448477

449-
if (!PhSplitStringRefAtChar(Name, L'_', &remaining, &remaining))
450-
return TRUE;
451-
if (!PhSplitStringRefAtChar(&remaining, L'_', &sessionIdPart, &processIdPart))
452-
return TRUE;
453-
if (!PhStringToInteger64(&sessionIdPart, 10, &sessionId64))
454-
return TRUE;
455-
if (!PhStringToInteger64(&processIdPart, 10, &processId64))
456-
return TRUE;
457-
if (UlongToHandle((ULONG)processId64) == NtCurrentProcessId())
458-
return TRUE;
478+
if (objectInfo.ClientId.UniqueProcess == NtCurrentProcessId())
479+
goto CleanupExit;
459480

460481
PhOpenProcess(
461-
&processHandle,
462-
PROCESS_TERMINATE | SYNCHRONIZE,
463-
ULongToHandle((ULONG)processId64)
482+
&processHandle,
483+
ProcessQueryAccess,
484+
objectInfo.ClientId.UniqueProcess
485+
);
486+
487+
hwnd = PhGetProcessMainWindowEx(
488+
objectInfo.ClientId.UniqueProcess,
489+
processHandle,
490+
FALSE
464491
);
465492

466-
if (sessionId64 == NtCurrentPeb()->SessionId)
493+
if (hwnd)
467494
{
468-
if (hwnd = PhGetProcessMainWindowEx(UlongToHandle((ULONG)processId64), NULL, FALSE))
469-
{
470-
SendMessageTimeout(hwnd, WM_QUIT, 0, 0, SMTO_BLOCK, 5000, NULL);
471-
}
495+
SendMessageTimeout(hwnd, WM_QUIT, 0, 0, SMTO_BLOCK, 5000, NULL);
472496
}
473497

474498
if (processHandle)
475499
{
476500
NtTerminateProcess(processHandle, 1);
477-
NtClose(processHandle);
478501
}
479-
}
480-
481-
{
482-
ULONG64 processId64;
483-
PH_STRINGREF firstPart;
484-
PH_STRINGREF secondPart;
485-
486-
if ((
487-
PhStartsWithStringRef2(Name, L"PhMutant_", TRUE) ||
488-
PhStartsWithStringRef2(Name, L"PhSetupMutant_", TRUE) ||
489-
PhStartsWithStringRef2(Name, L"PeViewer_", TRUE)
490-
) &&
491-
PhSplitStringRefAtChar(Name, L'_', &firstPart, &secondPart) &&
492-
PhStringToInteger64(&secondPart, 10, &processId64)
493-
)
494-
{
495-
HANDLE processHandle;
496502

497-
if (NT_SUCCESS(PhOpenProcess(&processHandle, PROCESS_TERMINATE | SYNCHRONIZE, ULongToHandle((ULONG)processId64))))
498-
{
499-
NtTerminateProcess(processHandle, 1);
500-
NtClose(processHandle);
501-
}
502-
}
503+
CleanupExit:
504+
if (processHandle) NtClose(processHandle);
503505
}
504506

507+
NtClose(objectHandle);
508+
505509
return TRUE;
506510
}
507511

tools/CustomSetupTool/CustomSetupTool/main.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -217,31 +217,19 @@ VOID SetupInitializeMutant(
217217
VOID
218218
)
219219
{
220-
HANDLE mutantHandle;
221-
OBJECT_ATTRIBUTES oa;
222-
UNICODE_STRING mutantName;
223-
PPH_STRING objectName;
224-
PH_FORMAT format[4];
225-
226-
PhInitFormatS(&format[0], L"PhSetupWindow_");
227-
PhInitFormatU(&format[1], NtCurrentPeb()->SessionId);
228-
PhInitFormatS(&format[2], L"_");
229-
PhInitFormatU(&format[3], HandleToUlong(NtCurrentProcessId()));
230-
231-
objectName = PhFormat(format, 4, 16);
232-
PhStringRefToUnicodeString(&objectName->sr, &mutantName);
220+
static UNICODE_STRING objectNameUs = RTL_CONSTANT_STRING(L"PhSetupMutant");
221+
OBJECT_ATTRIBUTES objectAttributes;
222+
HANDLE objectHandle;
233223

234224
InitializeObjectAttributes(
235-
&oa,
236-
&mutantName,
225+
&objectAttributes,
226+
&objectNameUs,
237227
OBJ_CASE_INSENSITIVE,
238228
PhGetNamespaceHandle(),
239229
NULL
240230
);
241231

242-
NtCreateMutant(&mutantHandle, MUTANT_ALL_ACCESS, &oa, TRUE);
243-
244-
PhDereferenceObject(objectName);
232+
NtCreateMutant(&objectHandle, MUTANT_ALL_ACCESS, &objectAttributes, TRUE);
245233
}
246234

247235
INT WINAPI wWinMain(

tools/peview/main.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,19 @@ INT WINAPI wWinMain(
5757

5858
// Create a mutant for the installer.
5959
{
60-
HANDLE mutantHandle;
61-
OBJECT_ATTRIBUTES oa;
62-
UNICODE_STRING mutantName;
63-
PPH_STRING objectName;
64-
PH_FORMAT format[4];
65-
66-
PhInitFormatS(&format[0], L"PeViewerWindow_");
67-
PhInitFormatU(&format[1], NtCurrentPeb()->SessionId);
68-
PhInitFormatS(&format[2], L"_");
69-
PhInitFormatU(&format[3], HandleToUlong(NtCurrentProcessId()));
70-
71-
objectName = PhFormat(format, 4, 16);
72-
PhStringRefToUnicodeString(&objectName->sr, &mutantName);
60+
static UNICODE_STRING objectNameUs = RTL_CONSTANT_STRING(L"PeViewerMutant");
61+
OBJECT_ATTRIBUTES objectAttributes;
62+
HANDLE objectHandle;
7363

7464
InitializeObjectAttributes(
75-
&oa,
76-
&mutantName,
65+
&objectAttributes,
66+
&objectNameUs,
7767
OBJ_CASE_INSENSITIVE,
7868
PhGetNamespaceHandle(),
7969
NULL
8070
);
8171

82-
NtCreateMutant(&mutantHandle, MUTANT_ALL_ACCESS, &oa, TRUE);
83-
84-
PhDereferenceObject(objectName);
72+
NtCreateMutant(&objectHandle, MUTANT_ALL_ACCESS, &objectAttributes, TRUE);
8573
}
8674

8775
PhGuiSupportInitialization();

0 commit comments

Comments
 (0)