26 #pragma alloc_text(PAGE, KpiOpenProcess)
27 #pragma alloc_text(PAGE, KpiOpenProcessToken)
28 #pragma alloc_text(PAGE, KpiOpenProcessJob)
29 #pragma alloc_text(PAGE, KpiSuspendProcess)
30 #pragma alloc_text(PAGE, KpiResumeProcess)
31 #pragma alloc_text(PAGE, KphTerminateProcessInternal)
32 #pragma alloc_text(PAGE, KpiTerminateProcess)
33 #pragma alloc_text(PAGE, KpiQueryInformationProcess)
34 #pragma alloc_text(PAGE, KpiSetInformationProcess)
48 __out PHANDLE ProcessHandle,
49 __in ACCESS_MASK DesiredAccess,
51 __in KPROCESSOR_MODE AccessMode
62 if (AccessMode != KernelMode)
66 ProbeForWrite(ProcessHandle,
sizeof(HANDLE),
sizeof(HANDLE));
67 ProbeForRead(ClientId,
sizeof(
CLIENT_ID),
sizeof(ULONG));
70 __except (EXCEPTION_EXECUTE_HANDLER)
72 return GetExceptionCode();
88 ObDereferenceObject(thread);
93 status = PsLookupProcessByProcessId(clientId.
UniqueProcess, &process);
100 status = ObOpenObjectByPointer(
109 ObDereferenceObject(process);
113 if (AccessMode != KernelMode)
117 *ProcessHandle = processHandle;
119 __except (EXCEPTION_EXECUTE_HANDLER)
121 status = GetExceptionCode();
126 *ProcessHandle = processHandle;
142 __in HANDLE ProcessHandle,
143 __in ACCESS_MASK DesiredAccess,
144 __out PHANDLE TokenHandle,
145 __in KPROCESSOR_MODE AccessMode
150 PACCESS_TOKEN primaryToken;
155 if (AccessMode != KernelMode)
159 ProbeForWrite(TokenHandle,
sizeof(HANDLE),
sizeof(HANDLE));
161 __except (EXCEPTION_EXECUTE_HANDLER)
163 return GetExceptionCode();
167 status = ObReferenceObjectByHandle(
179 primaryToken = PsReferencePrimaryToken(process);
181 status = ObOpenObjectByPointer(
191 PsDereferencePrimaryToken(primaryToken);
192 ObDereferenceObject(process);
196 if (AccessMode != KernelMode)
200 *TokenHandle = tokenHandle;
202 __except (EXCEPTION_EXECUTE_HANDLER)
204 status = GetExceptionCode();
209 *TokenHandle = tokenHandle;
225 __in HANDLE ProcessHandle,
226 __in ACCESS_MASK DesiredAccess,
227 __out PHANDLE JobHandle,
228 __in KPROCESSOR_MODE AccessMode
234 HANDLE jobHandle = NULL;
238 if (AccessMode != KernelMode)
242 ProbeForWrite(JobHandle,
sizeof(HANDLE),
sizeof(HANDLE));
244 __except (EXCEPTION_EXECUTE_HANDLER)
246 return GetExceptionCode();
250 status = ObReferenceObjectByHandle(
266 status = ObOpenObjectByPointer(
278 status = STATUS_NOT_FOUND;
281 ObDereferenceObject(process);
285 if (AccessMode != KernelMode)
289 *JobHandle = jobHandle;
291 __except (EXCEPTION_EXECUTE_HANDLER)
293 status = GetExceptionCode();
298 *JobHandle = jobHandle;
312 __in HANDLE ProcessHandle,
313 __in KPROCESSOR_MODE AccessMode
322 return STATUS_NOT_SUPPORTED;
324 status = ObReferenceObjectByHandle(
337 ObDereferenceObject(process);
349 __in HANDLE ProcessHandle,
350 __in KPROCESSOR_MODE AccessMode
359 return STATUS_NOT_SUPPORTED;
361 status = ObReferenceObjectByHandle(
374 ObDereferenceObject(process);
387 __in PEPROCESS Process,
388 __in NTSTATUS ExitStatus
397 return STATUS_NOT_SUPPORTED;
401 if (!PsTerminateProcess_I)
403 dprintf(
"Unable to find PsTerminateProcess\n");
404 return STATUS_NOT_SUPPORTED;
415 dprintf(
"Calling XP/03/8-style PsTerminateProcess\n");
419 status = PsTerminateProcess_I(Process, ExitStatus);
426 dprintf(
"Calling Vista/7-style PsTerminateProcess\n");
434 call [PsTerminateProcess_I]
440 dprintf(
"Calling 8.1-style PsTerminateProcess\n");
447 return STATUS_NOT_SUPPORTED;
452 status = PsTerminateProcess_I(Process, ExitStatus);
468 __in HANDLE ProcessHandle,
469 __in NTSTATUS ExitStatus,
470 __in KPROCESSOR_MODE AccessMode
478 status = ObReferenceObjectByHandle(
490 if (process != PsGetCurrentProcess())
492 dprintf(
"Calling KphTerminateProcessInternal from KpiTerminateProcess\n");
495 if (status == STATUS_NOT_SUPPORTED)
497 HANDLE newProcessHandle;
500 if (
NT_SUCCESS(status = ObOpenObjectByPointer(
517 status = STATUS_CANT_TERMINATE_SELF;
520 ObDereferenceObject(process);
538 __in HANDLE ProcessHandle,
540 __out_bcount(ProcessInformationLength) PVOID ProcessInformation,
541 __in ULONG ProcessInformationLength,
542 __out_opt PULONG ReturnLength,
543 __in KPROCESSOR_MODE AccessMode
552 if (AccessMode != KernelMode)
556 switch (ProcessInformationClass)
562 alignment =
sizeof(ULONG);
568 ProbeForWrite(ProcessInformation, ProcessInformationLength, alignment);
571 ProbeForWrite(ReturnLength,
sizeof(ULONG),
sizeof(ULONG));
573 __except (EXCEPTION_EXECUTE_HANDLER)
575 return GetExceptionCode();
579 status = ObReferenceObjectByHandle(
591 switch (ProcessInformationClass)
595 BOOLEAN protectedProcess =
FALSE;
600 status = STATUS_NOT_SUPPORTED;
610 __except (EXCEPTION_EXECUTE_HANDLER)
612 status = GetExceptionCode();
617 status = STATUS_INFO_LENGTH_MISMATCH;
629 KeStackAttachProcess(process, &apcState);
637 KeUnstackDetachProcess(&apcState);
641 if (ProcessInformationLength ==
sizeof(ULONG))
645 *(PULONG)ProcessInformation = executeFlags;
647 __except (EXCEPTION_EXECUTE_HANDLER)
649 status = GetExceptionCode();
654 status = STATUS_INFO_LENGTH_MISMATCH;
658 returnLength =
sizeof(ULONG);
663 HANDLE newProcessHandle;
666 if (
NT_SUCCESS(status = ObOpenObjectByPointer(
684 if (ProcessInformationLength ==
sizeof(ULONG))
688 *(PULONG)ProcessInformation = ioPriority;
690 __except (EXCEPTION_EXECUTE_HANDLER)
692 status = GetExceptionCode();
697 status = STATUS_INFO_LENGTH_MISMATCH;
704 returnLength =
sizeof(ULONG);
708 status = STATUS_INVALID_INFO_CLASS;
713 ObDereferenceObject(process);
717 if (AccessMode != KernelMode)
721 *ReturnLength = returnLength;
723 __except (EXCEPTION_EXECUTE_HANDLER)
730 *ReturnLength = returnLength;
748 __in HANDLE ProcessHandle,
750 __in_bcount(ProcessInformationLength) PVOID ProcessInformation,
751 __in ULONG ProcessInformationLength,
752 __in KPROCESSOR_MODE AccessMode
760 if (AccessMode != KernelMode)
764 switch (ProcessInformationClass)
767 alignment =
sizeof(ULONG);
773 ProbeForRead(ProcessInformation, ProcessInformationLength, alignment);
775 __except (EXCEPTION_EXECUTE_HANDLER)
777 return GetExceptionCode();
781 status = ObReferenceObjectByHandle(
793 switch (ProcessInformationClass)
800 if (ProcessInformationLength ==
sizeof(ULONG))
804 executeFlags = *(PULONG)ProcessInformation;
806 __except (EXCEPTION_EXECUTE_HANDLER)
808 status = GetExceptionCode();
813 status = STATUS_INFO_LENGTH_MISMATCH;
824 KeStackAttachProcess(process, &apcState);
831 KeUnstackDetachProcess(&apcState);
837 status = STATUS_PROCESS_IS_TERMINATING;
845 HANDLE newProcessHandle;
847 if (ProcessInformationLength ==
sizeof(ULONG))
851 ioPriority = *(PULONG)ProcessInformation;
853 __except (EXCEPTION_EXECUTE_HANDLER)
855 status = GetExceptionCode();
860 status = STATUS_INFO_LENGTH_MISMATCH;
865 if (
NT_SUCCESS(status = ObOpenObjectByPointer(
887 status = STATUS_INVALID_INFO_CLASS;
891 ObDereferenceObject(process);
905 __in PEPROCESS Process
916 if (KphDynEpRundownProtect == -1)
919 return ExAcquireRundownProtection((PEX_RUNDOWN_REF)((ULONG_PTR)Process + KphDynEpRundownProtect));
928 __in PEPROCESS Process
937 if (KphDynEpRundownProtect == -1)
940 ExReleaseRundownProtection((PEX_RUNDOWN_REF)((ULONG_PTR)Process + KphDynEpRundownProtect));