Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4382 lines
136 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. psquery.c
  5. Abstract:
  6. This module implements the set and query functions for
  7. process and thread objects.
  8. Author:
  9. Mark Lucovsky (markl) 17-Aug-1989
  10. Revision History:
  11. --*/
  12. #include "psp.h"
  13. #include "winerror.h"
  14. #if defined(_WIN64)
  15. #include <wow64t.h>
  16. #endif
  17. //
  18. // Process Pooled Quota Usage and Limits
  19. // NtQueryInformationProcess using ProcessPooledUsageAndLimits
  20. //
  21. //
  22. // this is the csrss process !
  23. //
  24. extern PEPROCESS ExpDefaultErrorPortProcess;
  25. BOOLEAN PsWatchEnabled = FALSE;
  26. #ifdef ALLOC_DATA_PRAGMA
  27. #pragma const_seg("PAGECONST")
  28. #endif
  29. const KPRIORITY PspPriorityTable[PROCESS_PRIORITY_CLASS_ABOVE_NORMAL+1] = {8,4,8,13,24,6,10};
  30. NTSTATUS
  31. PsConvertToGuiThread(
  32. VOID
  33. );
  34. NTSTATUS
  35. PspQueryWorkingSetWatch(
  36. IN HANDLE ProcessHandle,
  37. IN PROCESSINFOCLASS ProcessInformationClass,
  38. OUT PVOID ProcessInformation,
  39. IN ULONG ProcessInformationLength,
  40. OUT PULONG ReturnLength OPTIONAL,
  41. IN KPROCESSOR_MODE PreviousMode
  42. );
  43. NTSTATUS
  44. PspQueryQuotaLimits(
  45. IN HANDLE ProcessHandle,
  46. IN PROCESSINFOCLASS ProcessInformationClass,
  47. OUT PVOID ProcessInformation,
  48. IN ULONG ProcessInformationLength,
  49. OUT PULONG ReturnLength OPTIONAL,
  50. IN KPROCESSOR_MODE PreviousMode
  51. );
  52. NTSTATUS
  53. PspQueryPooledQuotaLimits(
  54. IN HANDLE ProcessHandle,
  55. IN PROCESSINFOCLASS ProcessInformationClass,
  56. OUT PVOID ProcessInformation,
  57. IN ULONG ProcessInformationLength,
  58. OUT PULONG ReturnLength OPTIONAL,
  59. IN KPROCESSOR_MODE PreviousMode
  60. );
  61. NTSTATUS
  62. PspSetQuotaLimits(
  63. IN HANDLE ProcessHandle,
  64. IN PROCESSINFOCLASS ProcessInformationClass,
  65. IN PVOID ProcessInformation,
  66. IN ULONG ProcessInformationLength,
  67. IN KPROCESSOR_MODE PreviousMode
  68. );
  69. #ifdef ALLOC_PRAGMA
  70. #pragma alloc_text(PAGE, PsEstablishWin32Callouts)
  71. #pragma alloc_text(PAGE, PsConvertToGuiThread)
  72. #pragma alloc_text(PAGE, NtQueryInformationProcess)
  73. #pragma alloc_text(PAGE, NtSetInformationProcess)
  74. #pragma alloc_text(PAGE, NtQueryPortInformationProcess)
  75. #pragma alloc_text(PAGE, NtQueryInformationThread)
  76. #pragma alloc_text(PAGE, NtSetInformationThread)
  77. #pragma alloc_text(PAGE, PsSetProcessPriorityByClass)
  78. #pragma alloc_text(PAGE, PspSetPrimaryToken)
  79. #pragma alloc_text(PAGE, PspSetQuotaLimits)
  80. #pragma alloc_text(PAGE, PspQueryQuotaLimits)
  81. #pragma alloc_text(PAGE, PspQueryPooledQuotaLimits)
  82. #pragma alloc_text(PAGE, NtGetCurrentProcessorNumber)
  83. #pragma alloc_text(PAGELK, PspQueryWorkingSetWatch)
  84. #endif
  85. NTSTATUS
  86. PspQueryWorkingSetWatch(
  87. IN HANDLE ProcessHandle,
  88. IN PROCESSINFOCLASS ProcessInformationClass,
  89. OUT PVOID ProcessInformation,
  90. IN ULONG ProcessInformationLength,
  91. OUT PULONG ReturnLength OPTIONAL,
  92. IN KPROCESSOR_MODE PreviousMode
  93. )
  94. {
  95. PPAGEFAULT_HISTORY WorkingSetCatcher;
  96. ULONG SpaceNeeded;
  97. PEPROCESS Process;
  98. KIRQL OldIrql;
  99. NTSTATUS st;
  100. UNREFERENCED_PARAMETER (ProcessInformationClass);
  101. st = ObReferenceObjectByHandle (ProcessHandle,
  102. PROCESS_QUERY_INFORMATION,
  103. PsProcessType,
  104. PreviousMode,
  105. (PVOID *)&Process,
  106. NULL);
  107. if (!NT_SUCCESS (st)) {
  108. return st;
  109. }
  110. WorkingSetCatcher = Process->WorkingSetWatch;
  111. if (WorkingSetCatcher == NULL) {
  112. ObDereferenceObject (Process);
  113. return STATUS_UNSUCCESSFUL;
  114. }
  115. MmLockPagableSectionByHandle (ExPageLockHandle);
  116. ExAcquireSpinLock (&WorkingSetCatcher->SpinLock,&OldIrql);
  117. if (WorkingSetCatcher->CurrentIndex) {
  118. //
  119. // Null Terminate the first empty entry in the buffer
  120. //
  121. WorkingSetCatcher->WatchInfo[WorkingSetCatcher->CurrentIndex].FaultingPc = NULL;
  122. //Store a special Va value if the buffer was full and
  123. //page faults could have been lost
  124. if (WorkingSetCatcher->CurrentIndex != WorkingSetCatcher->MaxIndex) {
  125. WorkingSetCatcher->WatchInfo[WorkingSetCatcher->CurrentIndex].FaultingVa = NULL;
  126. } else {
  127. WorkingSetCatcher->WatchInfo[WorkingSetCatcher->CurrentIndex].FaultingVa = (PVOID) 1;
  128. }
  129. SpaceNeeded = (WorkingSetCatcher->CurrentIndex+1) * sizeof(PROCESS_WS_WATCH_INFORMATION);
  130. } else {
  131. ExReleaseSpinLock (&WorkingSetCatcher->SpinLock, OldIrql);
  132. MmUnlockPagableImageSection (ExPageLockHandle);
  133. ObDereferenceObject (Process);
  134. return STATUS_NO_MORE_ENTRIES;
  135. }
  136. if (ProcessInformationLength < SpaceNeeded) {
  137. ExReleaseSpinLock (&WorkingSetCatcher->SpinLock, OldIrql);
  138. MmUnlockPagableImageSection (ExPageLockHandle);
  139. ObDereferenceObject (Process);
  140. return STATUS_BUFFER_TOO_SMALL;
  141. }
  142. //
  143. // Mark the Working Set buffer as full and then drop the lock
  144. // and copy the bytes
  145. //
  146. WorkingSetCatcher->CurrentIndex = MAX_WS_CATCH_INDEX;
  147. ExReleaseSpinLock (&WorkingSetCatcher->SpinLock,OldIrql);
  148. try {
  149. RtlCopyMemory (ProcessInformation, &WorkingSetCatcher->WatchInfo[0], SpaceNeeded);
  150. if (ARGUMENT_PRESENT (ReturnLength) ) {
  151. *ReturnLength = SpaceNeeded;
  152. }
  153. } except (EXCEPTION_EXECUTE_HANDLER) {
  154. st = GetExceptionCode ();
  155. }
  156. ExAcquireSpinLock (&WorkingSetCatcher->SpinLock, &OldIrql);
  157. WorkingSetCatcher->CurrentIndex = 0;
  158. ExReleaseSpinLock (&WorkingSetCatcher->SpinLock, OldIrql);
  159. MmUnlockPagableImageSection (ExPageLockHandle);
  160. ObDereferenceObject (Process);
  161. return st;
  162. }
  163. NTSTATUS
  164. PspQueryQuotaLimits(
  165. IN HANDLE ProcessHandle,
  166. IN PROCESSINFOCLASS ProcessInformationClass,
  167. OUT PVOID ProcessInformation,
  168. IN ULONG ProcessInformationLength,
  169. OUT PULONG ReturnLength OPTIONAL,
  170. IN KPROCESSOR_MODE PreviousMode
  171. )
  172. {
  173. QUOTA_LIMITS_EX QuotaLimits={0};
  174. PEPROCESS Process;
  175. NTSTATUS Status;
  176. PEPROCESS_QUOTA_BLOCK QuotaBlock;
  177. SIZE_T PeakWorkingSetSize;
  178. SIZE_T WorkingSetSize;
  179. ULONG HardEnforcement;
  180. KAPC_STATE ApcState;
  181. UNREFERENCED_PARAMETER (ProcessInformationClass);
  182. if (ProcessInformationLength != sizeof (QUOTA_LIMITS) &&
  183. ProcessInformationLength != sizeof (QUOTA_LIMITS_EX)) {
  184. return STATUS_INFO_LENGTH_MISMATCH;
  185. }
  186. Status = ObReferenceObjectByHandle (ProcessHandle,
  187. PROCESS_QUERY_INFORMATION,
  188. PsProcessType,
  189. PreviousMode,
  190. &Process,
  191. NULL);
  192. if (!NT_SUCCESS (Status)) {
  193. return Status;
  194. }
  195. QuotaBlock = Process->QuotaBlock;
  196. if (QuotaBlock != &PspDefaultQuotaBlock) {
  197. QuotaLimits.PagedPoolLimit = QuotaBlock->QuotaEntry[PsPagedPool].Limit;
  198. QuotaLimits.NonPagedPoolLimit = QuotaBlock->QuotaEntry[PsNonPagedPool].Limit;
  199. QuotaLimits.PagefileLimit = QuotaBlock->QuotaEntry[PsPageFile].Limit;
  200. } else {
  201. QuotaLimits.PagedPoolLimit = (SIZE_T)-1;
  202. QuotaLimits.NonPagedPoolLimit = (SIZE_T)-1;
  203. QuotaLimits.PagefileLimit = (SIZE_T)-1;
  204. }
  205. QuotaLimits.TimeLimit.LowPart = 0xffffffff;
  206. QuotaLimits.TimeLimit.HighPart = 0xffffffff;
  207. KeStackAttachProcess (&Process->Pcb, &ApcState);
  208. Status = MmQueryWorkingSetInformation (&PeakWorkingSetSize,
  209. &WorkingSetSize,
  210. &QuotaLimits.MinimumWorkingSetSize,
  211. &QuotaLimits.MaximumWorkingSetSize,
  212. &HardEnforcement);
  213. KeUnstackDetachProcess (&ApcState);
  214. if (HardEnforcement & MM_WORKING_SET_MIN_HARD_ENABLE) {
  215. QuotaLimits.Flags = QUOTA_LIMITS_HARDWS_MIN_ENABLE;
  216. } else {
  217. QuotaLimits.Flags = QUOTA_LIMITS_HARDWS_MIN_DISABLE;
  218. }
  219. if (HardEnforcement & MM_WORKING_SET_MAX_HARD_ENABLE) {
  220. QuotaLimits.Flags |= QUOTA_LIMITS_HARDWS_MAX_ENABLE;
  221. } else {
  222. QuotaLimits.Flags |= QUOTA_LIMITS_HARDWS_MAX_DISABLE;
  223. }
  224. ObDereferenceObject (Process);
  225. if (!NT_SUCCESS (Status)) {
  226. return Status;
  227. }
  228. //
  229. // Either of these may cause an access violation. The
  230. // exception handler will return access violation as
  231. // status code.
  232. //
  233. try {
  234. ASSERT (ProcessInformationLength <= sizeof (QuotaLimits));
  235. RtlCopyMemory (ProcessInformation, &QuotaLimits, ProcessInformationLength);
  236. if (ARGUMENT_PRESENT (ReturnLength)) {
  237. *ReturnLength = ProcessInformationLength;
  238. }
  239. } except (EXCEPTION_EXECUTE_HANDLER) {
  240. Status = GetExceptionCode ();
  241. }
  242. return Status;
  243. }
  244. NTSTATUS
  245. PspQueryPooledQuotaLimits(
  246. IN HANDLE ProcessHandle,
  247. IN PROCESSINFOCLASS ProcessInformationClass,
  248. OUT PVOID ProcessInformation,
  249. IN ULONG ProcessInformationLength,
  250. OUT PULONG ReturnLength OPTIONAL,
  251. IN KPROCESSOR_MODE PreviousMode
  252. )
  253. {
  254. PEPROCESS Process;
  255. NTSTATUS st;
  256. PEPROCESS_QUOTA_BLOCK QuotaBlock;
  257. POOLED_USAGE_AND_LIMITS UsageAndLimits;
  258. UNREFERENCED_PARAMETER (ProcessInformationClass);
  259. if (ProcessInformationLength != (ULONG) sizeof (POOLED_USAGE_AND_LIMITS)) {
  260. return STATUS_INFO_LENGTH_MISMATCH;
  261. }
  262. st = ObReferenceObjectByHandle (ProcessHandle,
  263. PROCESS_QUERY_INFORMATION,
  264. PsProcessType,
  265. PreviousMode,
  266. &Process,
  267. NULL);
  268. if (!NT_SUCCESS (st)) {
  269. return st;
  270. }
  271. QuotaBlock = Process->QuotaBlock;
  272. UsageAndLimits.PagedPoolLimit = QuotaBlock->QuotaEntry[PsPagedPool].Limit;
  273. UsageAndLimits.NonPagedPoolLimit = QuotaBlock->QuotaEntry[PsNonPagedPool].Limit;
  274. UsageAndLimits.PagefileLimit = QuotaBlock->QuotaEntry[PsPageFile].Limit;
  275. UsageAndLimits.PagedPoolUsage = QuotaBlock->QuotaEntry[PsPagedPool].Usage;
  276. UsageAndLimits.NonPagedPoolUsage = QuotaBlock->QuotaEntry[PsNonPagedPool].Usage;
  277. UsageAndLimits.PagefileUsage = QuotaBlock->QuotaEntry[PsPageFile].Usage;
  278. UsageAndLimits.PeakPagedPoolUsage = QuotaBlock->QuotaEntry[PsPagedPool].Peak;
  279. UsageAndLimits.PeakNonPagedPoolUsage = QuotaBlock->QuotaEntry[PsNonPagedPool].Peak;
  280. UsageAndLimits.PeakPagefileUsage = QuotaBlock->QuotaEntry[PsPageFile].Peak;
  281. //
  282. // Since the quota charge and return are lock free we may see Peak and Limit out of step.
  283. // Usage <= Limit and Usage <= Peak
  284. // Since Limit is adjusted up and down it does not hold that Peak <= Limit.
  285. //
  286. #define PSMAX(a,b) (((a) > (b))?(a):(b))
  287. UsageAndLimits.PagedPoolLimit = PSMAX (UsageAndLimits.PagedPoolLimit, UsageAndLimits.PagedPoolUsage);
  288. UsageAndLimits.NonPagedPoolLimit = PSMAX (UsageAndLimits.NonPagedPoolLimit, UsageAndLimits.NonPagedPoolUsage);
  289. UsageAndLimits.PagefileLimit = PSMAX (UsageAndLimits.PagefileLimit, UsageAndLimits.PagefileUsage);
  290. UsageAndLimits.PeakPagedPoolUsage = PSMAX (UsageAndLimits.PeakPagedPoolUsage, UsageAndLimits.PagedPoolUsage);
  291. UsageAndLimits.PeakNonPagedPoolUsage = PSMAX (UsageAndLimits.PeakNonPagedPoolUsage, UsageAndLimits.NonPagedPoolUsage);
  292. UsageAndLimits.PeakPagefileUsage = PSMAX (UsageAndLimits.PeakPagefileUsage, UsageAndLimits.PagefileUsage);
  293. ObDereferenceObject(Process);
  294. //
  295. // Either of these may cause an access violation. The
  296. // exception handler will return access violation as
  297. // status code. No further cleanup needs to be done.
  298. //
  299. try {
  300. *(PPOOLED_USAGE_AND_LIMITS) ProcessInformation = UsageAndLimits;
  301. if (ARGUMENT_PRESENT(ReturnLength) ) {
  302. *ReturnLength = sizeof(POOLED_USAGE_AND_LIMITS);
  303. }
  304. } except (EXCEPTION_EXECUTE_HANDLER) {
  305. return GetExceptionCode ();
  306. }
  307. return STATUS_SUCCESS;
  308. }
  309. NTSTATUS
  310. PspSetPrimaryToken(
  311. IN HANDLE ProcessHandle OPTIONAL,
  312. IN PEPROCESS ProcessPointer OPTIONAL,
  313. IN HANDLE TokenHandle OPTIONAL,
  314. IN PACCESS_TOKEN TokenPointer OPTIONAL,
  315. IN BOOLEAN PrivilegeChecked
  316. )
  317. /*++
  318. Sets the primary token for a process.
  319. The token and process supplied can be either by
  320. handle or by pointer.
  321. --*/
  322. {
  323. NTSTATUS Status;
  324. BOOLEAN HasPrivilege;
  325. BOOLEAN IsChildToken;
  326. PEPROCESS Process;
  327. KPROCESSOR_MODE PreviousMode;
  328. ACCESS_MASK GrantedAccess;
  329. PACCESS_TOKEN Token;
  330. //
  331. // Check to see if the supplied token is a child of the caller's
  332. // token. If so, we don't need to do the privilege check.
  333. //
  334. PreviousMode = KeGetPreviousMode ();
  335. if (TokenPointer == NULL) {
  336. //
  337. // Reference the specified token, and make sure it can be assigned
  338. // as a primary token.
  339. //
  340. Status = ObReferenceObjectByHandle (TokenHandle,
  341. TOKEN_ASSIGN_PRIMARY,
  342. SeTokenObjectType,
  343. PreviousMode,
  344. &Token,
  345. NULL);
  346. if (!NT_SUCCESS (Status)) {
  347. return Status;
  348. }
  349. } else {
  350. Token = TokenPointer;
  351. }
  352. //
  353. // If the privilege check has already been done (when the token was
  354. // assign to a job for example). We don't want to do it here.
  355. //
  356. if (!PrivilegeChecked) {
  357. Status = SeIsChildTokenByPointer (Token,
  358. &IsChildToken);
  359. if (!NT_SUCCESS (Status)) {
  360. goto exit_and_deref_token;
  361. }
  362. if (!IsChildToken) {
  363. //
  364. // SeCheckPrivilegedObject will perform auditing as appropriate
  365. //
  366. HasPrivilege = SeCheckPrivilegedObject (SeAssignPrimaryTokenPrivilege,
  367. ProcessHandle,
  368. PROCESS_SET_INFORMATION,
  369. PreviousMode);
  370. if (!HasPrivilege) {
  371. Status = STATUS_PRIVILEGE_NOT_HELD;
  372. goto exit_and_deref_token;
  373. }
  374. }
  375. }
  376. if (ProcessPointer == NULL) {
  377. Status = ObReferenceObjectByHandle (ProcessHandle,
  378. PROCESS_SET_INFORMATION,
  379. PsProcessType,
  380. PreviousMode,
  381. &Process,
  382. NULL);
  383. if (!NT_SUCCESS (Status)) {
  384. goto exit_and_deref_token;
  385. }
  386. } else {
  387. Process = ProcessPointer;
  388. }
  389. //
  390. // Check for proper access to the token, and assign the primary
  391. // token for the process.
  392. //
  393. Status = PspAssignPrimaryToken (Process, NULL, Token);
  394. //
  395. // Recompute the process's access to itself for use
  396. // with the CurrentProcess() pseudo handle.
  397. //
  398. if (NT_SUCCESS (Status)) {
  399. NTSTATUS accesst;
  400. BOOLEAN AccessCheck;
  401. BOOLEAN MemoryAllocated;
  402. PSECURITY_DESCRIPTOR SecurityDescriptor;
  403. SECURITY_SUBJECT_CONTEXT SubjectContext;
  404. Status = ObGetObjectSecurity (Process,
  405. &SecurityDescriptor,
  406. &MemoryAllocated);
  407. if (NT_SUCCESS (Status)) {
  408. SubjectContext.ProcessAuditId = Process;
  409. SubjectContext.PrimaryToken = PsReferencePrimaryToken (Process);
  410. SubjectContext.ClientToken = NULL;
  411. AccessCheck = SeAccessCheck (SecurityDescriptor,
  412. &SubjectContext,
  413. FALSE,
  414. MAXIMUM_ALLOWED,
  415. 0,
  416. NULL,
  417. &PsProcessType->TypeInfo.GenericMapping,
  418. PreviousMode,
  419. &GrantedAccess,
  420. &accesst);
  421. PsDereferencePrimaryTokenEx(Process, SubjectContext.PrimaryToken);
  422. ObReleaseObjectSecurity (SecurityDescriptor,
  423. MemoryAllocated);
  424. if (!AccessCheck) {
  425. GrantedAccess = 0;
  426. }
  427. //
  428. // To keep consistency with process creation, grant these
  429. // bits otherwise CreateProcessAsUser messes up really badly for
  430. // restricted tokens and we end up with a process that has no
  431. // access to itself when new token is set on the suspended
  432. // process.
  433. //
  434. GrantedAccess |= (PROCESS_VM_OPERATION | PROCESS_VM_READ |
  435. PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION |
  436. PROCESS_TERMINATE | PROCESS_CREATE_THREAD |
  437. PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS |
  438. PROCESS_SET_INFORMATION | STANDARD_RIGHTS_ALL);
  439. Process->GrantedAccess = GrantedAccess;
  440. }
  441. //
  442. // Since the process token is being set,
  443. // Set the device map for process to NULL.
  444. // During the next reference to the process' device map,
  445. // the object manager will set the device map for the process
  446. //
  447. if (ObIsLUIDDeviceMapsEnabled() != 0) {
  448. ObDereferenceDeviceMap( Process );
  449. }
  450. }
  451. if (ProcessPointer == NULL) {
  452. ObDereferenceObject (Process);
  453. }
  454. exit_and_deref_token:
  455. if (TokenPointer == NULL) {
  456. ObDereferenceObject (Token);
  457. }
  458. return Status;
  459. }
  460. NTSTATUS
  461. NtQueryInformationProcess(
  462. IN HANDLE ProcessHandle,
  463. IN PROCESSINFOCLASS ProcessInformationClass,
  464. OUT PVOID ProcessInformation,
  465. IN ULONG ProcessInformationLength,
  466. OUT PULONG ReturnLength OPTIONAL
  467. )
  468. {
  469. PEPROCESS Process;
  470. KPROCESSOR_MODE PreviousMode;
  471. NTSTATUS st;
  472. PROCESS_BASIC_INFORMATION BasicInfo;
  473. VM_COUNTERS_EX VmCounters;
  474. IO_COUNTERS IoCounters;
  475. KERNEL_USER_TIMES SysUserTime;
  476. HANDLE DebugPort;
  477. ULONG HandleCount;
  478. ULONG DefaultHardErrorMode;
  479. ULONG DisableBoost;
  480. ULONG BreakOnTerminationEnabled;
  481. PPROCESS_DEVICEMAP_INFORMATION DeviceMapInfo;
  482. PROCESS_SESSION_INFORMATION SessionInfo;
  483. PROCESS_PRIORITY_CLASS PriorityClass;
  484. ULONG_PTR Wow64Info;
  485. ULONG Flags;
  486. PUNICODE_STRING pTempNameInfo;
  487. SIZE_T MinimumWorkingSetSize;
  488. SIZE_T MaximumWorkingSetSize;
  489. ULONG HardEnforcement;
  490. KAPC_STATE ApcState;
  491. PAGED_CODE();
  492. //
  493. // Get previous processor mode and probe output argument if necessary.
  494. //
  495. PreviousMode = KeGetPreviousMode();
  496. if (PreviousMode != KernelMode) {
  497. try {
  498. //
  499. // Since these functions don't change any state thats not reversible
  500. // in the error paths we only probe the output buffer for write access.
  501. // This improves performance by not touching the buffer multiple times
  502. // And only writing the portions of the buffer that change.
  503. //
  504. ProbeForRead (ProcessInformation,
  505. ProcessInformationLength,
  506. sizeof (ULONG));
  507. if (ARGUMENT_PRESENT (ReturnLength)) {
  508. ProbeForWriteUlong (ReturnLength);
  509. }
  510. } except(EXCEPTION_EXECUTE_HANDLER) {
  511. return GetExceptionCode();
  512. }
  513. }
  514. //
  515. // Check argument validity.
  516. //
  517. switch ( ProcessInformationClass ) {
  518. case ProcessImageFileName:
  519. {
  520. ULONG LengthNeeded = 0;
  521. st = ObReferenceObjectByHandle (ProcessHandle,
  522. PROCESS_QUERY_INFORMATION,
  523. PsProcessType,
  524. PreviousMode,
  525. &Process,
  526. NULL);
  527. if (!NT_SUCCESS (st)) {
  528. return st;
  529. }
  530. //
  531. // SeLocateProcessImageName will allocate space for a UNICODE_STRING and point pTempNameInfo
  532. // at that string. This memory will be freed later in the routine.
  533. //
  534. st = SeLocateProcessImageName (Process, &pTempNameInfo);
  535. if (!NT_SUCCESS(st)) {
  536. ObDereferenceObject(Process);
  537. return st;
  538. }
  539. LengthNeeded = sizeof(UNICODE_STRING) + pTempNameInfo->MaximumLength;
  540. //
  541. // Either of these may cause an access violation. The
  542. // exception handler will return access violation as
  543. // status code. No further cleanup needs to be done.
  544. //
  545. try {
  546. if (ARGUMENT_PRESENT(ReturnLength) ) {
  547. *ReturnLength = LengthNeeded;
  548. }
  549. if (ProcessInformationLength >= LengthNeeded) {
  550. RtlCopyMemory(
  551. ProcessInformation,
  552. pTempNameInfo,
  553. sizeof(UNICODE_STRING) + pTempNameInfo->MaximumLength
  554. );
  555. ((PUNICODE_STRING) ProcessInformation)->Buffer = (PWSTR)((PUCHAR) ProcessInformation + sizeof(UNICODE_STRING));
  556. } else {
  557. st = STATUS_INFO_LENGTH_MISMATCH;
  558. }
  559. } except(EXCEPTION_EXECUTE_HANDLER) {
  560. st = GetExceptionCode ();
  561. }
  562. ObDereferenceObject(Process);
  563. ExFreePool( pTempNameInfo );
  564. return st;
  565. }
  566. case ProcessWorkingSetWatch:
  567. return PspQueryWorkingSetWatch (ProcessHandle,
  568. ProcessInformationClass,
  569. ProcessInformation,
  570. ProcessInformationLength,
  571. ReturnLength,
  572. PreviousMode);
  573. case ProcessBasicInformation:
  574. if (ProcessInformationLength != (ULONG) sizeof(PROCESS_BASIC_INFORMATION)) {
  575. return STATUS_INFO_LENGTH_MISMATCH;
  576. }
  577. st = ObReferenceObjectByHandle (ProcessHandle,
  578. PROCESS_QUERY_INFORMATION,
  579. PsProcessType,
  580. PreviousMode,
  581. &Process,
  582. NULL);
  583. if (!NT_SUCCESS (st)) {
  584. return st;
  585. }
  586. BasicInfo.ExitStatus = Process->ExitStatus;
  587. BasicInfo.PebBaseAddress = Process->Peb;
  588. BasicInfo.AffinityMask = Process->Pcb.Affinity;
  589. BasicInfo.BasePriority = Process->Pcb.BasePriority;
  590. BasicInfo.UniqueProcessId = (ULONG_PTR)Process->UniqueProcessId;
  591. BasicInfo.InheritedFromUniqueProcessId = (ULONG_PTR)Process->InheritedFromUniqueProcessId;
  592. ObDereferenceObject(Process);
  593. //
  594. // Either of these may cause an access violation. The
  595. // exception handler will return access violation as
  596. // status code. No further cleanup needs to be done.
  597. //
  598. try {
  599. *(PPROCESS_BASIC_INFORMATION) ProcessInformation = BasicInfo;
  600. if (ARGUMENT_PRESENT (ReturnLength) ) {
  601. *ReturnLength = sizeof(PROCESS_BASIC_INFORMATION);
  602. }
  603. } except (EXCEPTION_EXECUTE_HANDLER) {
  604. return GetExceptionCode ();
  605. }
  606. return STATUS_SUCCESS;
  607. case ProcessDefaultHardErrorMode:
  608. if (ProcessInformationLength != sizeof(ULONG)) {
  609. return STATUS_INFO_LENGTH_MISMATCH;
  610. }
  611. st = ObReferenceObjectByHandle (ProcessHandle,
  612. PROCESS_QUERY_INFORMATION,
  613. PsProcessType,
  614. PreviousMode,
  615. &Process,
  616. NULL);
  617. if (!NT_SUCCESS (st)) {
  618. return st;
  619. }
  620. DefaultHardErrorMode = Process->DefaultHardErrorProcessing;
  621. ObDereferenceObject(Process);
  622. try {
  623. *(PULONG) ProcessInformation = DefaultHardErrorMode;
  624. if (ARGUMENT_PRESENT (ReturnLength) ) {
  625. *ReturnLength = sizeof (ULONG);
  626. }
  627. } except (EXCEPTION_EXECUTE_HANDLER) {
  628. return GetExceptionCode ();
  629. }
  630. return STATUS_SUCCESS;
  631. case ProcessQuotaLimits:
  632. return PspQueryQuotaLimits (ProcessHandle,
  633. ProcessInformationClass,
  634. ProcessInformation,
  635. ProcessInformationLength,
  636. ReturnLength,
  637. PreviousMode);
  638. case ProcessPooledUsageAndLimits:
  639. return PspQueryPooledQuotaLimits (ProcessHandle,
  640. ProcessInformationClass,
  641. ProcessInformation,
  642. ProcessInformationLength,
  643. ReturnLength,
  644. PreviousMode);
  645. case ProcessIoCounters:
  646. if (ProcessInformationLength != (ULONG) sizeof (IO_COUNTERS)) {
  647. return STATUS_INFO_LENGTH_MISMATCH;
  648. }
  649. st = ObReferenceObjectByHandle (ProcessHandle,
  650. PROCESS_QUERY_INFORMATION,
  651. PsProcessType,
  652. PreviousMode,
  653. &Process,
  654. NULL);
  655. if (!NT_SUCCESS (st)) {
  656. return st;
  657. }
  658. IoCounters.ReadOperationCount = Process->ReadOperationCount.QuadPart;
  659. IoCounters.WriteOperationCount = Process->WriteOperationCount.QuadPart;
  660. IoCounters.OtherOperationCount = Process->OtherOperationCount.QuadPart;
  661. IoCounters.ReadTransferCount = Process->ReadTransferCount.QuadPart;
  662. IoCounters.WriteTransferCount = Process->WriteTransferCount.QuadPart;
  663. IoCounters.OtherTransferCount = Process->OtherTransferCount.QuadPart;
  664. ObDereferenceObject (Process);
  665. //
  666. // Either of these may cause an access violation. The
  667. // exception handler will return access violation as
  668. // status code. No further cleanup needs to be done.
  669. //
  670. try {
  671. *(PIO_COUNTERS) ProcessInformation = IoCounters;
  672. if (ARGUMENT_PRESENT (ReturnLength) ) {
  673. *ReturnLength = sizeof(IO_COUNTERS);
  674. }
  675. } except (EXCEPTION_EXECUTE_HANDLER) {
  676. return GetExceptionCode ();
  677. }
  678. return STATUS_SUCCESS;
  679. case ProcessVmCounters:
  680. if (ProcessInformationLength != (ULONG) sizeof (VM_COUNTERS)
  681. && ProcessInformationLength != (ULONG) sizeof (VM_COUNTERS_EX)) {
  682. return STATUS_INFO_LENGTH_MISMATCH;
  683. }
  684. st = ObReferenceObjectByHandle (ProcessHandle,
  685. PROCESS_QUERY_INFORMATION,
  686. PsProcessType,
  687. PreviousMode,
  688. &Process,
  689. NULL);
  690. if (!NT_SUCCESS (st)) {
  691. return st;
  692. }
  693. //
  694. // Note: At some point, we might have to grab the statistics
  695. // lock to reliably read this stuff
  696. //
  697. VmCounters.PeakVirtualSize = Process->PeakVirtualSize;
  698. VmCounters.VirtualSize = Process->VirtualSize;
  699. VmCounters.PageFaultCount = Process->Vm.PageFaultCount;
  700. KeStackAttachProcess (&Process->Pcb, &ApcState);
  701. st = MmQueryWorkingSetInformation (&VmCounters.PeakWorkingSetSize,
  702. &VmCounters.WorkingSetSize,
  703. &MinimumWorkingSetSize,
  704. &MaximumWorkingSetSize,
  705. &HardEnforcement);
  706. KeUnstackDetachProcess (&ApcState);
  707. VmCounters.QuotaPeakPagedPoolUsage = Process->QuotaPeak[PsPagedPool];
  708. VmCounters.QuotaPagedPoolUsage = Process->QuotaUsage[PsPagedPool];
  709. VmCounters.QuotaPeakNonPagedPoolUsage = Process->QuotaPeak[PsNonPagedPool];
  710. VmCounters.QuotaNonPagedPoolUsage = Process->QuotaUsage[PsNonPagedPool];
  711. VmCounters.PagefileUsage = ((SIZE_T) Process->QuotaUsage[PsPageFile]) << PAGE_SHIFT;
  712. VmCounters.PeakPagefileUsage = ((SIZE_T) Process->QuotaPeak[PsPageFile]) << PAGE_SHIFT;
  713. VmCounters.PrivateUsage = ((SIZE_T) Process->CommitCharge) << PAGE_SHIFT;
  714. ObDereferenceObject (Process);
  715. if (!NT_SUCCESS (st)) {
  716. return st;
  717. }
  718. //
  719. // Either of these may cause an access violation. The
  720. // exception handler will return access violation as
  721. // status code. No further cleanup needs to be done.
  722. //
  723. try {
  724. RtlCopyMemory(ProcessInformation,
  725. &VmCounters,
  726. ProcessInformationLength);
  727. if (ARGUMENT_PRESENT (ReturnLength) ) {
  728. *ReturnLength = ProcessInformationLength;
  729. }
  730. } except (EXCEPTION_EXECUTE_HANDLER) {
  731. return GetExceptionCode ();
  732. }
  733. return STATUS_SUCCESS;
  734. case ProcessTimes:
  735. if ( ProcessInformationLength != (ULONG) sizeof(KERNEL_USER_TIMES) ) {
  736. return STATUS_INFO_LENGTH_MISMATCH;
  737. }
  738. st = ObReferenceObjectByHandle (ProcessHandle,
  739. PROCESS_QUERY_INFORMATION,
  740. PsProcessType,
  741. PreviousMode,
  742. &Process,
  743. NULL);
  744. if (!NT_SUCCESS (st)) {
  745. return st;
  746. }
  747. //
  748. // Need some type of interlock on KiTimeLock
  749. //
  750. SysUserTime.KernelTime.QuadPart = UInt32x32To64(Process->Pcb.KernelTime,
  751. KeMaximumIncrement);
  752. SysUserTime.UserTime.QuadPart = UInt32x32To64(Process->Pcb.UserTime,
  753. KeMaximumIncrement);
  754. SysUserTime.CreateTime = Process->CreateTime;
  755. SysUserTime.ExitTime = Process->ExitTime;
  756. ObDereferenceObject (Process);
  757. //
  758. // Either of these may cause an access violation. The
  759. // exception handler will return access violation as
  760. // status code. No further cleanup needs to be done.
  761. //
  762. try {
  763. *(PKERNEL_USER_TIMES) ProcessInformation = SysUserTime;
  764. if (ARGUMENT_PRESENT (ReturnLength) ) {
  765. *ReturnLength = sizeof (KERNEL_USER_TIMES);
  766. }
  767. } except (EXCEPTION_EXECUTE_HANDLER) {
  768. return GetExceptionCode ();
  769. }
  770. return STATUS_SUCCESS;
  771. case ProcessDebugPort :
  772. //
  773. if (ProcessInformationLength != (ULONG) sizeof (HANDLE)) {
  774. return STATUS_INFO_LENGTH_MISMATCH;
  775. }
  776. st = ObReferenceObjectByHandle (ProcessHandle,
  777. PROCESS_QUERY_INFORMATION,
  778. PsProcessType,
  779. PreviousMode,
  780. &Process,
  781. NULL);
  782. if (!NT_SUCCESS (st)) {
  783. return st;
  784. }
  785. if (Process->DebugPort == NULL) {
  786. DebugPort = NULL;
  787. } else {
  788. DebugPort = (HANDLE)-1;
  789. }
  790. ObDereferenceObject (Process);
  791. //
  792. // Either of these may cause an access violation. The
  793. // exception handler will return access violation as
  794. // status code. No further cleanup needs to be done.
  795. //
  796. try {
  797. *(PHANDLE) ProcessInformation = DebugPort;
  798. if (ARGUMENT_PRESENT (ReturnLength) ) {
  799. *ReturnLength = sizeof(HANDLE);
  800. }
  801. } except (EXCEPTION_EXECUTE_HANDLER) {
  802. return GetExceptionCode ();
  803. }
  804. return STATUS_SUCCESS;
  805. case ProcessDebugObjectHandle :
  806. //
  807. if (ProcessInformationLength != sizeof (HANDLE)) {
  808. return STATUS_INFO_LENGTH_MISMATCH;
  809. }
  810. st = ObReferenceObjectByHandle (ProcessHandle,
  811. PROCESS_QUERY_INFORMATION,
  812. PsProcessType,
  813. PreviousMode,
  814. &Process,
  815. NULL);
  816. if (!NT_SUCCESS (st)) {
  817. return st;
  818. }
  819. st = DbgkOpenProcessDebugPort (Process,
  820. PreviousMode,
  821. &DebugPort);
  822. if (!NT_SUCCESS (st)) {
  823. DebugPort = NULL;
  824. }
  825. ObDereferenceObject (Process);
  826. //
  827. // Either of these may cause an access violation. The
  828. // exception handler will return access violation as
  829. // status code. No further cleanup needs to be done.
  830. //
  831. try {
  832. *(PHANDLE) ProcessInformation = DebugPort;
  833. if (ARGUMENT_PRESENT (ReturnLength)) {
  834. *ReturnLength = sizeof (HANDLE);
  835. }
  836. } except (EXCEPTION_EXECUTE_HANDLER) {
  837. if (DebugPort != NULL) {
  838. ObCloseHandle (DebugPort, PreviousMode);
  839. }
  840. return GetExceptionCode ();
  841. }
  842. return st;
  843. case ProcessDebugFlags :
  844. if (ProcessInformationLength != sizeof (ULONG)) {
  845. return STATUS_INFO_LENGTH_MISMATCH;
  846. }
  847. st = ObReferenceObjectByHandle (ProcessHandle,
  848. PROCESS_QUERY_INFORMATION,
  849. PsProcessType,
  850. PreviousMode,
  851. &Process,
  852. NULL);
  853. if (!NT_SUCCESS (st)) {
  854. return st;
  855. }
  856. try {
  857. *(PULONG) ProcessInformation = (Process->Flags&PS_PROCESS_FLAGS_NO_DEBUG_INHERIT)?0:PROCESS_DEBUG_INHERIT;
  858. if (ARGUMENT_PRESENT (ReturnLength) ) {
  859. *ReturnLength = sizeof(HANDLE);
  860. }
  861. } except (EXCEPTION_EXECUTE_HANDLER) {
  862. st = GetExceptionCode ();
  863. }
  864. ObDereferenceObject (Process);
  865. return st;
  866. case ProcessHandleCount :
  867. if (ProcessInformationLength != (ULONG) sizeof (ULONG)) {
  868. return STATUS_INFO_LENGTH_MISMATCH;
  869. }
  870. st = ObReferenceObjectByHandle (ProcessHandle,
  871. PROCESS_QUERY_INFORMATION,
  872. PsProcessType,
  873. PreviousMode,
  874. &Process,
  875. NULL);
  876. if (!NT_SUCCESS (st)) {
  877. return st;
  878. }
  879. HandleCount = ObGetProcessHandleCount (Process);
  880. ObDereferenceObject (Process);
  881. //
  882. // Either of these may cause an access violation. The
  883. // exception handler will return access violation as
  884. // status code. No further cleanup needs to be done.
  885. //
  886. try {
  887. *(PULONG) ProcessInformation = HandleCount;
  888. if (ARGUMENT_PRESENT (ReturnLength) ) {
  889. *ReturnLength = sizeof (ULONG);
  890. }
  891. } except (EXCEPTION_EXECUTE_HANDLER) {
  892. return GetExceptionCode ();
  893. }
  894. return STATUS_SUCCESS;
  895. case ProcessLdtInformation :
  896. st = ObReferenceObjectByHandle (ProcessHandle,
  897. PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  898. PsProcessType,
  899. PreviousMode,
  900. &Process,
  901. NULL);
  902. if (!NT_SUCCESS (st)) {
  903. return st;
  904. }
  905. st = PspQueryLdtInformation (Process,
  906. ProcessInformation,
  907. ProcessInformationLength,
  908. ReturnLength);
  909. ObDereferenceObject(Process);
  910. return st;
  911. case ProcessWx86Information :
  912. if (ProcessInformationLength != sizeof (ULONG)) {
  913. return STATUS_INFO_LENGTH_MISMATCH;
  914. }
  915. st = ObReferenceObjectByHandle (ProcessHandle,
  916. PROCESS_QUERY_INFORMATION,
  917. PsProcessType,
  918. PreviousMode,
  919. &Process,
  920. NULL);
  921. if (!NT_SUCCESS (st)) {
  922. return st;
  923. }
  924. Flags = Process->Flags & PS_PROCESS_FLAGS_VDM_ALLOWED ? 1 : 0;
  925. ObDereferenceObject (Process);
  926. //
  927. // The returned flags is used as a BOOLEAN to indicate whether the
  928. // ProcessHandle specifies a NtVdm Process. In another words, the caller
  929. // can simply do a
  930. // if (ReturnedValue == TRUE) {
  931. // a ntvdm process;
  932. // } else {
  933. // NOT a ntvdm process;
  934. // }
  935. //
  936. try {
  937. *(PULONG)ProcessInformation = Flags;
  938. if (ARGUMENT_PRESENT (ReturnLength) ) {
  939. *ReturnLength = sizeof(ULONG);
  940. }
  941. } except (EXCEPTION_EXECUTE_HANDLER) {
  942. return GetExceptionCode ();
  943. }
  944. return st;
  945. case ProcessPriorityBoost:
  946. if (ProcessInformationLength != sizeof (ULONG)) {
  947. return STATUS_INFO_LENGTH_MISMATCH;
  948. }
  949. st = ObReferenceObjectByHandle (ProcessHandle,
  950. PROCESS_QUERY_INFORMATION,
  951. PsProcessType,
  952. PreviousMode,
  953. &Process,
  954. NULL);
  955. if (!NT_SUCCESS (st)) {
  956. return st;
  957. }
  958. DisableBoost = Process->Pcb.DisableBoost ? 1 : 0;
  959. ObDereferenceObject (Process);
  960. try {
  961. *(PULONG)ProcessInformation = DisableBoost;
  962. if (ARGUMENT_PRESENT( ReturnLength) ) {
  963. *ReturnLength = sizeof (ULONG);
  964. }
  965. } except (EXCEPTION_EXECUTE_HANDLER) {
  966. return GetExceptionCode ();
  967. }
  968. return st;
  969. case ProcessDeviceMap:
  970. DeviceMapInfo = (PPROCESS_DEVICEMAP_INFORMATION)ProcessInformation;
  971. if (ProcessInformationLength < sizeof (DeviceMapInfo->Query)) {
  972. return STATUS_INFO_LENGTH_MISMATCH;
  973. }
  974. if (ProcessInformationLength == sizeof (PROCESS_DEVICEMAP_INFORMATION_EX)) {
  975. try {
  976. Flags = ((PPROCESS_DEVICEMAP_INFORMATION_EX)DeviceMapInfo)->Flags;
  977. } except (EXCEPTION_EXECUTE_HANDLER) {
  978. return GetExceptionCode ();
  979. }
  980. if ( (Flags & ~(PROCESS_LUID_DOSDEVICES_ONLY)) ||
  981. (ObIsLUIDDeviceMapsEnabled () == 0) ) {
  982. return STATUS_INVALID_PARAMETER;
  983. }
  984. }
  985. else {
  986. if (ProcessInformationLength == sizeof (DeviceMapInfo->Query)) {
  987. Flags = 0;
  988. }
  989. else {
  990. return STATUS_INFO_LENGTH_MISMATCH;
  991. }
  992. }
  993. st = ObReferenceObjectByHandle (ProcessHandle,
  994. PROCESS_QUERY_INFORMATION,
  995. PsProcessType,
  996. PreviousMode,
  997. &Process,
  998. NULL);
  999. if (!NT_SUCCESS (st)) {
  1000. return st;
  1001. }
  1002. st = ObQueryDeviceMapInformation (Process, DeviceMapInfo, Flags);
  1003. ObDereferenceObject(Process);
  1004. return st;
  1005. case ProcessSessionInformation :
  1006. if (ProcessInformationLength != (ULONG) sizeof (PROCESS_SESSION_INFORMATION)) {
  1007. return STATUS_INFO_LENGTH_MISMATCH;
  1008. }
  1009. st = ObReferenceObjectByHandle (ProcessHandle,
  1010. PROCESS_QUERY_INFORMATION,
  1011. PsProcessType,
  1012. PreviousMode,
  1013. &Process,
  1014. NULL);
  1015. if (!NT_SUCCESS (st)) {
  1016. return st;
  1017. }
  1018. SessionInfo.SessionId = MmGetSessionId (Process);
  1019. ObDereferenceObject (Process);
  1020. try {
  1021. *(PPROCESS_SESSION_INFORMATION) ProcessInformation = SessionInfo;
  1022. if (ARGUMENT_PRESENT (ReturnLength)) {
  1023. *ReturnLength = sizeof(PROCESS_SESSION_INFORMATION);
  1024. }
  1025. } except (EXCEPTION_EXECUTE_HANDLER) {
  1026. return GetExceptionCode ();
  1027. }
  1028. return STATUS_SUCCESS;
  1029. case ProcessPriorityClass:
  1030. if (ProcessInformationLength != sizeof (PROCESS_PRIORITY_CLASS)) {
  1031. return STATUS_INFO_LENGTH_MISMATCH;
  1032. }
  1033. st = ObReferenceObjectByHandle (ProcessHandle,
  1034. PROCESS_QUERY_INFORMATION,
  1035. PsProcessType,
  1036. PreviousMode,
  1037. &Process,
  1038. NULL);
  1039. if (!NT_SUCCESS (st)) {
  1040. return st;
  1041. }
  1042. PriorityClass.Foreground = FALSE;
  1043. PriorityClass.PriorityClass = Process->PriorityClass;
  1044. ObDereferenceObject (Process);
  1045. try {
  1046. *(PPROCESS_PRIORITY_CLASS) ProcessInformation = PriorityClass;
  1047. if (ARGUMENT_PRESENT (ReturnLength)) {
  1048. *ReturnLength = sizeof(PROCESS_PRIORITY_CLASS);
  1049. }
  1050. } except (EXCEPTION_EXECUTE_HANDLER) {
  1051. return GetExceptionCode ();
  1052. }
  1053. return STATUS_SUCCESS;
  1054. case ProcessWow64Information:
  1055. if (ProcessInformationLength != sizeof (ULONG_PTR)) {
  1056. return STATUS_INFO_LENGTH_MISMATCH;
  1057. }
  1058. st = ObReferenceObjectByHandle (ProcessHandle,
  1059. PROCESS_QUERY_INFORMATION,
  1060. PsProcessType,
  1061. PreviousMode,
  1062. &Process,
  1063. NULL);
  1064. if (!NT_SUCCESS (st)) {
  1065. return st;
  1066. }
  1067. Wow64Info = 0;
  1068. //
  1069. // Acquire process rundown protection as we are about to look at process structures torn down at
  1070. // process exit.
  1071. //
  1072. if (ExAcquireRundownProtection (&Process->RundownProtect)) {
  1073. PWOW64_PROCESS Wow64Process;
  1074. if ((Wow64Process = PS_GET_WOW64_PROCESS (Process)) != NULL) {
  1075. Wow64Info = (ULONG_PTR)(Wow64Process->Wow64);
  1076. }
  1077. ExReleaseRundownProtection (&Process->RundownProtect);
  1078. }
  1079. ObDereferenceObject (Process);
  1080. try {
  1081. *(PULONG_PTR)ProcessInformation = Wow64Info;
  1082. if (ARGUMENT_PRESENT (ReturnLength)) {
  1083. *ReturnLength = sizeof (ULONG_PTR);
  1084. }
  1085. } except (EXCEPTION_EXECUTE_HANDLER) {
  1086. return GetExceptionCode ();
  1087. }
  1088. return STATUS_SUCCESS;
  1089. case ProcessLUIDDeviceMapsEnabled:
  1090. if (ProcessInformationLength != sizeof (ULONG)) {
  1091. return STATUS_INFO_LENGTH_MISMATCH;
  1092. }
  1093. try {
  1094. *(PULONG)ProcessInformation = ObIsLUIDDeviceMapsEnabled ();
  1095. if (ARGUMENT_PRESENT (ReturnLength)) {
  1096. *ReturnLength = sizeof (ULONG);
  1097. }
  1098. } except (EXCEPTION_EXECUTE_HANDLER) {
  1099. return GetExceptionCode ();
  1100. }
  1101. return STATUS_SUCCESS;
  1102. case ProcessBreakOnTermination:
  1103. if (ProcessInformationLength != sizeof (ULONG)) {
  1104. return STATUS_INFO_LENGTH_MISMATCH;
  1105. }
  1106. st = ObReferenceObjectByHandle (ProcessHandle,
  1107. PROCESS_QUERY_INFORMATION,
  1108. PsProcessType,
  1109. PreviousMode,
  1110. &Process,
  1111. NULL);
  1112. if (!NT_SUCCESS (st)) {
  1113. return st;
  1114. }
  1115. if (Process->Flags & PS_PROCESS_FLAGS_BREAK_ON_TERMINATION) {
  1116. BreakOnTerminationEnabled = 1;
  1117. } else {
  1118. BreakOnTerminationEnabled = 0;
  1119. }
  1120. ObDereferenceObject (Process);
  1121. try {
  1122. *(PULONG)ProcessInformation = BreakOnTerminationEnabled;
  1123. if (ARGUMENT_PRESENT (ReturnLength)) {
  1124. *ReturnLength = sizeof (ULONG);
  1125. }
  1126. } except (EXCEPTION_EXECUTE_HANDLER) {
  1127. return GetExceptionCode ();
  1128. }
  1129. return STATUS_SUCCESS;
  1130. case ProcessHandleTracing: {
  1131. PPROCESS_HANDLE_TRACING_QUERY Pht;
  1132. PHANDLE_TABLE HandleTable;
  1133. PHANDLE_TRACE_DEBUG_INFO DebugInfo;
  1134. HANDLE_TRACE_DB_ENTRY Trace;
  1135. PPROCESS_HANDLE_TRACING_ENTRY NextTrace;
  1136. ULONG StacksLeft;
  1137. ULONG i, j;
  1138. if (ProcessInformationLength < FIELD_OFFSET (PROCESS_HANDLE_TRACING_QUERY,
  1139. HandleTrace)) {
  1140. return STATUS_INFO_LENGTH_MISMATCH;
  1141. }
  1142. Pht = (PPROCESS_HANDLE_TRACING_QUERY) ProcessInformation;
  1143. StacksLeft = (ProcessInformationLength - FIELD_OFFSET (PROCESS_HANDLE_TRACING_QUERY,
  1144. HandleTrace)) /
  1145. sizeof (Pht->HandleTrace[0]);
  1146. NextTrace = &Pht->HandleTrace[0];
  1147. st = ObReferenceObjectByHandle (ProcessHandle,
  1148. PROCESS_QUERY_INFORMATION,
  1149. PsProcessType,
  1150. PreviousMode,
  1151. &Process,
  1152. NULL);
  1153. if (!NT_SUCCESS (st)) {
  1154. return st;
  1155. }
  1156. HandleTable = ObReferenceProcessHandleTable (Process);
  1157. if (HandleTable != NULL) {
  1158. DebugInfo = ExReferenceHandleDebugInfo (HandleTable);
  1159. if (DebugInfo != NULL) {
  1160. try {
  1161. Pht->TotalTraces = 0;
  1162. j = DebugInfo->CurrentStackIndex % DebugInfo->TableSize;
  1163. for (i = 0; i < DebugInfo->TableSize; i++) {
  1164. RtlCopyMemory (&Trace, &DebugInfo->TraceDb[j], sizeof (Trace));
  1165. if ((Pht->Handle == Trace.Handle || Pht->Handle == 0) && Trace.Type != 0) {
  1166. Pht->TotalTraces++;
  1167. if (StacksLeft > 0) {
  1168. StacksLeft--;
  1169. NextTrace->Handle = Trace.Handle;
  1170. NextTrace->ClientId = Trace.ClientId;
  1171. NextTrace->Type = Trace.Type;
  1172. RtlCopyMemory (NextTrace->Stacks,
  1173. Trace.StackTrace,
  1174. min (sizeof (NextTrace->Stacks),
  1175. sizeof (Trace.StackTrace)));
  1176. NextTrace++;
  1177. } else {
  1178. st = STATUS_INFO_LENGTH_MISMATCH;
  1179. }
  1180. }
  1181. if (j == 0) {
  1182. j = DebugInfo->TableSize - 1;
  1183. } else {
  1184. j--;
  1185. }
  1186. }
  1187. if (ARGUMENT_PRESENT (ReturnLength)) {
  1188. *ReturnLength = (ULONG) ((PUCHAR) NextTrace - (PUCHAR) Pht);
  1189. }
  1190. } except (EXCEPTION_EXECUTE_HANDLER) {
  1191. st = GetExceptionCode ();
  1192. }
  1193. ExDereferenceHandleDebugInfo (HandleTable, DebugInfo);
  1194. } else {
  1195. st = STATUS_INVALID_PARAMETER;
  1196. }
  1197. ObDereferenceProcessHandleTable (Process);
  1198. } else {
  1199. st = STATUS_PROCESS_IS_TERMINATING;
  1200. }
  1201. ObDereferenceObject(Process);
  1202. return st;
  1203. }
  1204. default:
  1205. return STATUS_INVALID_INFO_CLASS;
  1206. }
  1207. }
  1208. NTSTATUS
  1209. NtQueryPortInformationProcess(
  1210. VOID
  1211. )
  1212. /*++
  1213. Routine Description:
  1214. This function tests whether a debug port or an exception port is attached
  1215. to the current process and returns a corresponding value. This function is
  1216. used to bypass raising an exception through the system when no associated
  1217. ports are present.
  1218. N.B. This improves performance considerably with respect to raising
  1219. software exceptions in user mode on AMD64 and IA64 systems.
  1220. Arguments:
  1221. None.
  1222. Return Value:
  1223. A success value of TRUE is returned if either a debug or exception port
  1224. is associated with the current process. Otherwise, a success value of
  1225. FALSE is returned.
  1226. --*/
  1227. {
  1228. PEPROCESS Process;
  1229. PETHREAD Thread;
  1230. //
  1231. // If the process has a debug port and it is not being hidden from the
  1232. // debugger, then return a success status of TRUE. Otherwise, is the
  1233. // process has an exception port, then return a success status of TRUE.
  1234. // Otherwise, return a success status of FALSE.
  1235. //
  1236. Thread = PsGetCurrentThread();
  1237. Process = PsGetCurrentProcessByThread (Thread);
  1238. if ((Process->DebugPort != NULL) &&
  1239. ((Thread->CrossThreadFlags & PS_CROSS_THREAD_FLAGS_HIDEFROMDBG) == 0)) {
  1240. return TRUE;
  1241. } else if (Process->ExceptionPort != NULL) {
  1242. return TRUE;
  1243. } else {
  1244. return FALSE;
  1245. }
  1246. }
  1247. NTSTATUS
  1248. PspSetQuotaLimits(
  1249. IN HANDLE ProcessHandle,
  1250. IN PROCESSINFOCLASS ProcessInformationClass,
  1251. IN PVOID ProcessInformation,
  1252. IN ULONG ProcessInformationLength,
  1253. IN KPROCESSOR_MODE PreviousMode
  1254. )
  1255. {
  1256. PEPROCESS Process;
  1257. PETHREAD CurrentThread;
  1258. QUOTA_LIMITS_EX RequestedLimits;
  1259. PEPROCESS_QUOTA_BLOCK NewQuotaBlock;
  1260. NTSTATUS st, ReturnStatus;
  1261. BOOLEAN OkToIncrease, IgnoreError;
  1262. PEJOB Job;
  1263. KAPC_STATE ApcState;
  1264. ULONG EnableHardLimits;
  1265. BOOLEAN PurgeRequest;
  1266. UNREFERENCED_PARAMETER (ProcessInformationClass);
  1267. try {
  1268. if (ProcessInformationLength == sizeof (QUOTA_LIMITS)) {
  1269. RtlCopyMemory (&RequestedLimits,
  1270. ProcessInformation,
  1271. sizeof (QUOTA_LIMITS));
  1272. RequestedLimits.Reserved1 = 0;
  1273. RequestedLimits.Reserved2 = 0;
  1274. RequestedLimits.Reserved3 = 0;
  1275. RequestedLimits.Reserved4 = 0;
  1276. RequestedLimits.Reserved5 = 0;
  1277. RequestedLimits.Flags = 0;
  1278. } else if (ProcessInformationLength == sizeof (QUOTA_LIMITS_EX)) {
  1279. RequestedLimits = *(PQUOTA_LIMITS_EX) ProcessInformation;
  1280. } else {
  1281. return STATUS_INFO_LENGTH_MISMATCH;
  1282. }
  1283. } except (EXCEPTION_EXECUTE_HANDLER) {
  1284. return GetExceptionCode ();
  1285. }
  1286. //
  1287. // All unused flags must be zero
  1288. //
  1289. if (RequestedLimits.Flags & ~(QUOTA_LIMITS_HARDWS_MAX_ENABLE|QUOTA_LIMITS_HARDWS_MAX_DISABLE|
  1290. QUOTA_LIMITS_HARDWS_MIN_ENABLE|QUOTA_LIMITS_HARDWS_MIN_DISABLE)) {
  1291. return STATUS_INVALID_PARAMETER;
  1292. }
  1293. //
  1294. // Disallow both enable and disable bits set at the same time.
  1295. //
  1296. if (PS_TEST_ALL_BITS_SET (RequestedLimits.Flags, QUOTA_LIMITS_HARDWS_MIN_ENABLE|QUOTA_LIMITS_HARDWS_MIN_DISABLE) ||
  1297. PS_TEST_ALL_BITS_SET (RequestedLimits.Flags, QUOTA_LIMITS_HARDWS_MAX_ENABLE|QUOTA_LIMITS_HARDWS_MAX_DISABLE)) {
  1298. return STATUS_INVALID_PARAMETER;
  1299. }
  1300. //
  1301. // See if we are changing the hard limits or not
  1302. //
  1303. EnableHardLimits = 0;
  1304. if (RequestedLimits.Flags&QUOTA_LIMITS_HARDWS_MIN_ENABLE) {
  1305. EnableHardLimits = MM_WORKING_SET_MIN_HARD_ENABLE;
  1306. } else if (RequestedLimits.Flags&QUOTA_LIMITS_HARDWS_MIN_DISABLE) {
  1307. EnableHardLimits = MM_WORKING_SET_MIN_HARD_DISABLE;
  1308. }
  1309. if (RequestedLimits.Flags&QUOTA_LIMITS_HARDWS_MAX_ENABLE) {
  1310. EnableHardLimits |= MM_WORKING_SET_MAX_HARD_ENABLE;
  1311. } else if (RequestedLimits.Flags&QUOTA_LIMITS_HARDWS_MAX_DISABLE) {
  1312. EnableHardLimits |= MM_WORKING_SET_MAX_HARD_DISABLE;
  1313. }
  1314. //
  1315. // All reserved fields must be zero
  1316. //
  1317. if (RequestedLimits.Reserved1 != 0 || RequestedLimits.Reserved2 != 0 ||
  1318. RequestedLimits.Reserved3 != 0 || RequestedLimits.Reserved4 != 0 ||
  1319. RequestedLimits.Reserved5 != 0) {
  1320. return STATUS_INVALID_PARAMETER;
  1321. }
  1322. st = ObReferenceObjectByHandle (ProcessHandle,
  1323. PROCESS_SET_QUOTA,
  1324. PsProcessType,
  1325. PreviousMode,
  1326. &Process,
  1327. NULL);
  1328. if (!NT_SUCCESS (st)) {
  1329. return st;
  1330. }
  1331. CurrentThread = PsGetCurrentThread ();
  1332. //
  1333. // Now we are ready to set the quota limits for the process
  1334. //
  1335. // If the process already has a quota block, then all we allow
  1336. // is working set changes.
  1337. //
  1338. // If the process has no quota block, all that can be done is a
  1339. // quota set operation.
  1340. //
  1341. // If a quota field is zero, we pick the value.
  1342. //
  1343. // Setting quotas requires the SeIncreaseQuotaPrivilege (except for
  1344. // working set size since this is only advisory).
  1345. //
  1346. ReturnStatus = STATUS_SUCCESS;
  1347. if ((Process->QuotaBlock == &PspDefaultQuotaBlock) &&
  1348. (RequestedLimits.MinimumWorkingSetSize == 0 || RequestedLimits.MaximumWorkingSetSize == 0)) {
  1349. //
  1350. // You must have a privilege to assign quotas
  1351. //
  1352. if (!SeSinglePrivilegeCheck (SeIncreaseQuotaPrivilege, PreviousMode)) {
  1353. ObDereferenceObject (Process);
  1354. return STATUS_PRIVILEGE_NOT_HELD;
  1355. }
  1356. NewQuotaBlock = ExAllocatePoolWithTag (NonPagedPool, sizeof(*NewQuotaBlock), 'bQsP');
  1357. if (NewQuotaBlock == NULL) {
  1358. ObDereferenceObject (Process);
  1359. return STATUS_NO_MEMORY;
  1360. }
  1361. RtlZeroMemory (NewQuotaBlock, sizeof (*NewQuotaBlock));
  1362. //
  1363. // Initialize the quota block
  1364. //
  1365. NewQuotaBlock->ReferenceCount = 1;
  1366. NewQuotaBlock->ProcessCount = 1;
  1367. NewQuotaBlock->QuotaEntry[PsNonPagedPool].Peak = Process->QuotaPeak[PsNonPagedPool];
  1368. NewQuotaBlock->QuotaEntry[PsPagedPool].Peak = Process->QuotaPeak[PsPagedPool];
  1369. NewQuotaBlock->QuotaEntry[PsPageFile].Peak = Process->QuotaPeak[PsPageFile];
  1370. //
  1371. // Now compute limits
  1372. //
  1373. //
  1374. // Get the defaults that the system would pick.
  1375. //
  1376. NewQuotaBlock->QuotaEntry[PsPagedPool].Limit = PspDefaultPagedLimit;
  1377. NewQuotaBlock->QuotaEntry[PsNonPagedPool].Limit = PspDefaultNonPagedLimit;
  1378. NewQuotaBlock->QuotaEntry[PsPageFile].Limit = PspDefaultPagefileLimit;
  1379. // Everything is set. Now double check to quota block field
  1380. // If we still have no quota block then assign and succeed.
  1381. // Otherwise punt.
  1382. //
  1383. if (InterlockedCompareExchangePointer (&Process->QuotaBlock,
  1384. NewQuotaBlock,
  1385. &PspDefaultQuotaBlock) != &PspDefaultQuotaBlock) {
  1386. ExFreePool (NewQuotaBlock);
  1387. } else {
  1388. PspInsertQuotaBlock (NewQuotaBlock);
  1389. }
  1390. } else {
  1391. //
  1392. // Only allow a working set size change
  1393. //
  1394. if (RequestedLimits.MinimumWorkingSetSize &&
  1395. RequestedLimits.MaximumWorkingSetSize) {
  1396. //
  1397. // See if the caller just wants to purge the working set.
  1398. // This is an unprivileged operation.
  1399. //
  1400. if (RequestedLimits.MinimumWorkingSetSize == (SIZE_T)-1 &&
  1401. RequestedLimits.MaximumWorkingSetSize == (SIZE_T)-1) {
  1402. PurgeRequest = TRUE;
  1403. OkToIncrease = FALSE;
  1404. } else {
  1405. PurgeRequest = FALSE;
  1406. if (SeSinglePrivilegeCheck (SeIncreaseBasePriorityPrivilege,
  1407. PreviousMode)) {
  1408. OkToIncrease = TRUE;
  1409. } else {
  1410. OkToIncrease = FALSE;
  1411. }
  1412. }
  1413. do {
  1414. IgnoreError = FALSE;
  1415. KeStackAttachProcess (&Process->Pcb, &ApcState);
  1416. KeEnterGuardedRegionThread (&CurrentThread->Tcb);
  1417. Job = Process->Job;
  1418. if (Job != NULL) {
  1419. ExAcquireResourceExclusiveLite (&Job->JobLock, TRUE);
  1420. if (Job->LimitFlags & JOB_OBJECT_LIMIT_WORKINGSET) {
  1421. //
  1422. // Don't let a process in a job change if job limits are applied
  1423. // except purge requests which can always be done.
  1424. //
  1425. EnableHardLimits = MM_WORKING_SET_MAX_HARD_ENABLE;
  1426. OkToIncrease = TRUE;
  1427. IgnoreError = TRUE; // we must always set enforcement value
  1428. if (!PurgeRequest) {
  1429. RequestedLimits.MinimumWorkingSetSize = Job->MinimumWorkingSetSize;
  1430. RequestedLimits.MaximumWorkingSetSize = Job->MaximumWorkingSetSize;
  1431. }
  1432. }
  1433. PspLockWorkingSetChangeExclusiveUnsafe ();
  1434. ExReleaseResourceLite (&Job->JobLock);
  1435. }
  1436. ReturnStatus = MmAdjustWorkingSetSizeEx (RequestedLimits.MinimumWorkingSetSize,
  1437. RequestedLimits.MaximumWorkingSetSize,
  1438. FALSE,
  1439. OkToIncrease,
  1440. EnableHardLimits);
  1441. if (!NT_SUCCESS (ReturnStatus) && IgnoreError) {
  1442. MmEnforceWorkingSetLimit (Process,
  1443. EnableHardLimits);
  1444. }
  1445. if (Job != NULL) {
  1446. PspUnlockWorkingSetChangeExclusiveUnsafe ();
  1447. }
  1448. KeLeaveGuardedRegionThread (&CurrentThread->Tcb);
  1449. KeUnstackDetachProcess (&ApcState);
  1450. //
  1451. // We loop here in case this process was added to a job
  1452. // after we checked but before we set the limits
  1453. //
  1454. } while (Process->Job != Job);
  1455. }
  1456. }
  1457. ObDereferenceObject(Process);
  1458. return ReturnStatus;
  1459. }
  1460. NTSTATUS
  1461. NtSetInformationProcess(
  1462. IN HANDLE ProcessHandle,
  1463. IN PROCESSINFOCLASS ProcessInformationClass,
  1464. IN PVOID ProcessInformation,
  1465. IN ULONG ProcessInformationLength
  1466. )
  1467. /*++
  1468. Routine Description:
  1469. This function sets the state of a process object.
  1470. Arguments:
  1471. ProcessHandle - Supplies a handle to a process object.
  1472. ProcessInformationClass - Supplies the class of information being
  1473. set.
  1474. ProcessInformation - Supplies a pointer to a record that contains the
  1475. information to set.
  1476. ProcessInformationLength - Supplies the length of the record that contains
  1477. the information to set.
  1478. Return Value:
  1479. TBS
  1480. --*/
  1481. {
  1482. PEPROCESS Process;
  1483. PETHREAD Thread;
  1484. PETHREAD CurrentThread;
  1485. KPROCESSOR_MODE PreviousMode;
  1486. NTSTATUS st;
  1487. KPRIORITY BasePriority;
  1488. ULONG BoostValue;
  1489. ULONG DefaultHardErrorMode;
  1490. PVOID ExceptionPort;
  1491. BOOLEAN EnableAlignmentFaultFixup;
  1492. HANDLE ExceptionPortHandle;
  1493. ULONG ProbeAlignment;
  1494. HANDLE PrimaryTokenHandle;
  1495. BOOLEAN HasPrivilege = FALSE;
  1496. UCHAR MemoryPriority;
  1497. PROCESS_PRIORITY_CLASS LocalPriorityClass;
  1498. PROCESS_FOREGROUND_BACKGROUND LocalForeground;
  1499. KAFFINITY Affinity, AffinityWithMasks;
  1500. ULONG DisableBoost;
  1501. BOOLEAN bDisableBoost;
  1502. PPROCESS_DEVICEMAP_INFORMATION DeviceMapInfo;
  1503. HANDLE DirectoryHandle;
  1504. PROCESS_SESSION_INFORMATION SessionInfo;
  1505. ULONG EnableBreakOnTermination;
  1506. PEJOB Job;
  1507. PAGED_CODE();
  1508. //
  1509. // Get previous processor mode and probe input argument if necessary.
  1510. //
  1511. CurrentThread = PsGetCurrentThread ();
  1512. PreviousMode = KeGetPreviousModeByThread (&CurrentThread->Tcb);
  1513. if (PreviousMode != KernelMode) {
  1514. if (ProcessInformationClass == ProcessBasePriority) {
  1515. ProbeAlignment = sizeof (KPRIORITY);
  1516. } else if (ProcessInformationClass == ProcessEnableAlignmentFaultFixup) {
  1517. ProbeAlignment = sizeof (BOOLEAN);
  1518. } else if (ProcessInformationClass == ProcessForegroundInformation) {
  1519. ProbeAlignment = sizeof (PROCESS_FOREGROUND_BACKGROUND);
  1520. } else if (ProcessInformationClass == ProcessPriorityClass) {
  1521. ProbeAlignment = sizeof (BOOLEAN);
  1522. } else if (ProcessInformationClass == ProcessAffinityMask) {
  1523. ProbeAlignment = sizeof (ULONG_PTR);
  1524. } else {
  1525. ProbeAlignment = sizeof (ULONG);
  1526. }
  1527. try {
  1528. ProbeForRead (ProcessInformation,
  1529. ProcessInformationLength,
  1530. ProbeAlignment);
  1531. } except (EXCEPTION_EXECUTE_HANDLER) {
  1532. return GetExceptionCode ();
  1533. }
  1534. }
  1535. //
  1536. // Check argument validity.
  1537. //
  1538. switch (ProcessInformationClass) {
  1539. case ProcessWorkingSetWatch: {
  1540. PPAGEFAULT_HISTORY WorkingSetCatcher;
  1541. st = ObReferenceObjectByHandle (ProcessHandle,
  1542. PROCESS_SET_INFORMATION,
  1543. PsProcessType,
  1544. PreviousMode,
  1545. &Process,
  1546. NULL);
  1547. if (!NT_SUCCESS (st)) {
  1548. return st;
  1549. }
  1550. st = PsChargeProcessNonPagedPoolQuota (Process, WS_CATCH_SIZE);
  1551. if (NT_SUCCESS (st)) {
  1552. WorkingSetCatcher = ExAllocatePoolWithTag (NonPagedPool, WS_CATCH_SIZE, 'sWsP');
  1553. if (!WorkingSetCatcher) {
  1554. st = STATUS_NO_MEMORY;
  1555. } else {
  1556. PsWatchEnabled = TRUE;
  1557. WorkingSetCatcher->CurrentIndex = 0;
  1558. WorkingSetCatcher->MaxIndex = MAX_WS_CATCH_INDEX;
  1559. KeInitializeSpinLock (&WorkingSetCatcher->SpinLock);
  1560. //
  1561. // This only ever goes on the process and isn't removed till process object deletion.
  1562. // We just need to protect against multiple callers here.
  1563. //
  1564. if (InterlockedCompareExchangePointer (&Process->WorkingSetWatch,
  1565. WorkingSetCatcher, NULL) == NULL) {
  1566. st = STATUS_SUCCESS;
  1567. } else {
  1568. ExFreePool (WorkingSetCatcher);
  1569. st = STATUS_PORT_ALREADY_SET;
  1570. }
  1571. }
  1572. if (!NT_SUCCESS (st)) {
  1573. PsReturnProcessNonPagedPoolQuota (Process, WS_CATCH_SIZE);
  1574. }
  1575. }
  1576. ObDereferenceObject (Process);
  1577. return st;
  1578. }
  1579. case ProcessBasePriority: {
  1580. //
  1581. // THIS ITEM CODE IS OBSOLETE !
  1582. //
  1583. if (ProcessInformationLength != sizeof (KPRIORITY)) {
  1584. return STATUS_INFO_LENGTH_MISMATCH;
  1585. }
  1586. try {
  1587. BasePriority = *(KPRIORITY *)ProcessInformation;
  1588. } except (EXCEPTION_EXECUTE_HANDLER) {
  1589. return GetExceptionCode();
  1590. }
  1591. if (BasePriority & 0x80000000) {
  1592. MemoryPriority = MEMORY_PRIORITY_FOREGROUND;
  1593. BasePriority &= ~0x80000000;
  1594. } else {
  1595. MemoryPriority = MEMORY_PRIORITY_BACKGROUND;
  1596. }
  1597. if (BasePriority > HIGH_PRIORITY ||
  1598. BasePriority <= LOW_PRIORITY) {
  1599. return STATUS_INVALID_PARAMETER;
  1600. }
  1601. st = ObReferenceObjectByHandle (ProcessHandle,
  1602. PROCESS_SET_INFORMATION,
  1603. PsProcessType,
  1604. PreviousMode,
  1605. &Process,
  1606. NULL);
  1607. if (!NT_SUCCESS (st)) {
  1608. return st;
  1609. }
  1610. if (BasePriority > Process->Pcb.BasePriority) {
  1611. //
  1612. // Increasing the base priority of a process is a
  1613. // privileged operation. Check for the privilege
  1614. // here.
  1615. //
  1616. HasPrivilege = SeCheckPrivilegedObject (SeIncreaseBasePriorityPrivilege,
  1617. ProcessHandle,
  1618. PROCESS_SET_INFORMATION,
  1619. PreviousMode);
  1620. if (!HasPrivilege) {
  1621. ObDereferenceObject (Process);
  1622. return STATUS_PRIVILEGE_NOT_HELD;
  1623. }
  1624. }
  1625. KeSetPriorityProcess (&Process->Pcb, BasePriority);
  1626. MmSetMemoryPriorityProcess (Process, MemoryPriority);
  1627. ObDereferenceObject (Process);
  1628. return STATUS_SUCCESS;
  1629. }
  1630. case ProcessPriorityClass: {
  1631. if (ProcessInformationLength != sizeof (PROCESS_PRIORITY_CLASS)) {
  1632. return STATUS_INFO_LENGTH_MISMATCH;
  1633. }
  1634. try {
  1635. LocalPriorityClass = *(PPROCESS_PRIORITY_CLASS)ProcessInformation;
  1636. } except(EXCEPTION_EXECUTE_HANDLER) {
  1637. return GetExceptionCode ();
  1638. }
  1639. if (LocalPriorityClass.PriorityClass > PROCESS_PRIORITY_CLASS_ABOVE_NORMAL) {
  1640. return STATUS_INVALID_PARAMETER;
  1641. }
  1642. st = ObReferenceObjectByHandle (ProcessHandle,
  1643. PROCESS_SET_INFORMATION,
  1644. PsProcessType,
  1645. PreviousMode,
  1646. &Process,
  1647. NULL);
  1648. if (!NT_SUCCESS (st)) {
  1649. return st;
  1650. }
  1651. if (LocalPriorityClass.PriorityClass != Process->PriorityClass &&
  1652. LocalPriorityClass.PriorityClass == PROCESS_PRIORITY_CLASS_REALTIME) {
  1653. //
  1654. // Increasing the base priority of a process is a
  1655. // privileged operation. Check for the privilege
  1656. // here.
  1657. //
  1658. HasPrivilege = SeCheckPrivilegedObject (SeIncreaseBasePriorityPrivilege,
  1659. ProcessHandle,
  1660. PROCESS_SET_INFORMATION,
  1661. PreviousMode);
  1662. if (!HasPrivilege) {
  1663. ObDereferenceObject (Process);
  1664. return STATUS_PRIVILEGE_NOT_HELD;
  1665. }
  1666. }
  1667. //
  1668. // If the process has a job object, override whatever the process
  1669. // is calling with with the value from the job object
  1670. //
  1671. Job = Process->Job;
  1672. if (Job != NULL) {
  1673. KeEnterCriticalRegionThread (&CurrentThread->Tcb);
  1674. ExAcquireResourceSharedLite (&Job->JobLock, TRUE);
  1675. if (Job->LimitFlags & JOB_OBJECT_LIMIT_PRIORITY_CLASS) {
  1676. LocalPriorityClass.PriorityClass = Job->PriorityClass;
  1677. }
  1678. ExReleaseResourceLite (&Job->JobLock);
  1679. KeLeaveCriticalRegionThread (&CurrentThread->Tcb);
  1680. }
  1681. Process->PriorityClass = LocalPriorityClass.PriorityClass;
  1682. PsSetProcessPriorityByClass (Process,
  1683. LocalPriorityClass.Foreground ?
  1684. PsProcessPriorityForeground : PsProcessPriorityBackground);
  1685. ObDereferenceObject (Process);
  1686. return STATUS_SUCCESS;
  1687. }
  1688. case ProcessForegroundInformation: {
  1689. if (ProcessInformationLength != sizeof (PROCESS_FOREGROUND_BACKGROUND)) {
  1690. return STATUS_INFO_LENGTH_MISMATCH;
  1691. }
  1692. try {
  1693. LocalForeground = *(PPROCESS_FOREGROUND_BACKGROUND)ProcessInformation;
  1694. } except (EXCEPTION_EXECUTE_HANDLER) {
  1695. return GetExceptionCode ();
  1696. }
  1697. st = ObReferenceObjectByHandle (ProcessHandle,
  1698. PROCESS_SET_INFORMATION,
  1699. PsProcessType,
  1700. PreviousMode,
  1701. &Process,
  1702. NULL);
  1703. if (!NT_SUCCESS (st)) {
  1704. return st;
  1705. }
  1706. PsSetProcessPriorityByClass (Process,
  1707. LocalForeground.Foreground ?
  1708. PsProcessPriorityForeground : PsProcessPriorityBackground);
  1709. ObDereferenceObject (Process);
  1710. return STATUS_SUCCESS;
  1711. }
  1712. case ProcessRaisePriority: {
  1713. //
  1714. // This code is used to boost the priority of all threads
  1715. // within a process. It cannot be used to change a thread into
  1716. // a realtime class, or to lower the priority of a thread. The
  1717. // argument is a boost value that is added to the base priority
  1718. // of the specified process.
  1719. //
  1720. if (ProcessInformationLength != sizeof (ULONG)) {
  1721. return STATUS_INFO_LENGTH_MISMATCH;
  1722. }
  1723. try {
  1724. BoostValue = *(PULONG)ProcessInformation;
  1725. } except (EXCEPTION_EXECUTE_HANDLER) {
  1726. return GetExceptionCode ();
  1727. }
  1728. st = ObReferenceObjectByHandle (ProcessHandle,
  1729. PROCESS_SET_INFORMATION,
  1730. PsProcessType,
  1731. PreviousMode,
  1732. &Process,
  1733. NULL);
  1734. if (!NT_SUCCESS (st)) {
  1735. return st;
  1736. }
  1737. //
  1738. // Get the process create/delete lock and walk through the
  1739. // thread list boosting each thread.
  1740. //
  1741. if (ExAcquireRundownProtection (&Process->RundownProtect)) {
  1742. for (Thread = PsGetNextProcessThread (Process, NULL);
  1743. Thread != NULL;
  1744. Thread = PsGetNextProcessThread (Process, Thread)) {
  1745. KeBoostPriorityThread (&Thread->Tcb, (KPRIORITY)BoostValue);
  1746. }
  1747. ExReleaseRundownProtection (&Process->RundownProtect);
  1748. } else {
  1749. st = STATUS_PROCESS_IS_TERMINATING;
  1750. }
  1751. ObDereferenceObject (Process);
  1752. return st;
  1753. }
  1754. case ProcessDefaultHardErrorMode: {
  1755. if (ProcessInformationLength != sizeof (ULONG)) {
  1756. return STATUS_INFO_LENGTH_MISMATCH;
  1757. }
  1758. try {
  1759. DefaultHardErrorMode = *(PULONG)ProcessInformation;
  1760. } except (EXCEPTION_EXECUTE_HANDLER) {
  1761. return GetExceptionCode ();
  1762. }
  1763. st = ObReferenceObjectByHandle (ProcessHandle,
  1764. PROCESS_SET_INFORMATION,
  1765. PsProcessType,
  1766. PreviousMode,
  1767. &Process,
  1768. NULL);
  1769. if (!NT_SUCCESS (st)) {
  1770. return st;
  1771. }
  1772. Process->DefaultHardErrorProcessing = DefaultHardErrorMode;
  1773. if (DefaultHardErrorMode & PROCESS_HARDERROR_ALIGNMENT_BIT) {
  1774. KeSetAutoAlignmentProcess (&Process->Pcb,TRUE);
  1775. } else {
  1776. KeSetAutoAlignmentProcess (&Process->Pcb,FALSE);
  1777. }
  1778. ObDereferenceObject (Process);
  1779. return STATUS_SUCCESS;
  1780. }
  1781. case ProcessQuotaLimits: {
  1782. return PspSetQuotaLimits (ProcessHandle,
  1783. ProcessInformationClass,
  1784. ProcessInformation,
  1785. ProcessInformationLength,
  1786. PreviousMode);
  1787. }
  1788. case ProcessExceptionPort : {
  1789. if (ProcessInformationLength != sizeof (HANDLE)) {
  1790. return STATUS_INFO_LENGTH_MISMATCH;
  1791. }
  1792. try {
  1793. ExceptionPortHandle = *(PHANDLE) ProcessInformation;
  1794. } except (EXCEPTION_EXECUTE_HANDLER) {
  1795. return GetExceptionCode ();
  1796. }
  1797. if (!SeSinglePrivilegeCheck (SeTcbPrivilege, PreviousMode)) {
  1798. return STATUS_PRIVILEGE_NOT_HELD;
  1799. }
  1800. st = ObReferenceObjectByHandle (ExceptionPortHandle,
  1801. 0,
  1802. LpcPortObjectType,
  1803. PreviousMode,
  1804. &ExceptionPort,
  1805. NULL);
  1806. if (!NT_SUCCESS (st)) {
  1807. return st;
  1808. }
  1809. st = ObReferenceObjectByHandle (ProcessHandle,
  1810. PROCESS_SET_PORT,
  1811. PsProcessType,
  1812. PreviousMode,
  1813. &Process,
  1814. NULL);
  1815. if (!NT_SUCCESS (st)) {
  1816. ObDereferenceObject (ExceptionPort);
  1817. return st;
  1818. }
  1819. //
  1820. // We are only allowed to put the exception port on. It doesn't get removed till process delete.
  1821. //
  1822. if (InterlockedCompareExchangePointer (&Process->ExceptionPort, ExceptionPort, NULL) == NULL) {
  1823. st = STATUS_SUCCESS;
  1824. } else {
  1825. ObDereferenceObject (ExceptionPort);
  1826. st = STATUS_PORT_ALREADY_SET;
  1827. }
  1828. ObDereferenceObject (Process);
  1829. return st;
  1830. }
  1831. case ProcessAccessToken : {
  1832. if (ProcessInformationLength != sizeof (PROCESS_ACCESS_TOKEN)) {
  1833. return STATUS_INFO_LENGTH_MISMATCH;
  1834. }
  1835. try {
  1836. PrimaryTokenHandle = ((PROCESS_ACCESS_TOKEN *)ProcessInformation)->Token;
  1837. } except(EXCEPTION_EXECUTE_HANDLER) {
  1838. return GetExceptionCode ();
  1839. }
  1840. st = PspSetPrimaryToken (ProcessHandle,
  1841. NULL,
  1842. PrimaryTokenHandle,
  1843. NULL,
  1844. FALSE);
  1845. return st;
  1846. }
  1847. case ProcessLdtInformation:
  1848. st = ObReferenceObjectByHandle (ProcessHandle,
  1849. PROCESS_SET_INFORMATION | PROCESS_VM_WRITE,
  1850. PsProcessType,
  1851. PreviousMode,
  1852. &Process,
  1853. NULL);
  1854. if (!NT_SUCCESS (st)) {
  1855. return st;
  1856. }
  1857. st = PspSetLdtInformation (Process,
  1858. ProcessInformation,
  1859. ProcessInformationLength);
  1860. ObDereferenceObject (Process);
  1861. return st;
  1862. case ProcessLdtSize:
  1863. st = ObReferenceObjectByHandle (ProcessHandle,
  1864. PROCESS_SET_INFORMATION | PROCESS_VM_WRITE,
  1865. PsProcessType,
  1866. PreviousMode,
  1867. &Process,
  1868. NULL);
  1869. if (!NT_SUCCESS (st)) {
  1870. return st;
  1871. }
  1872. st = PspSetLdtSize (Process,
  1873. ProcessInformation,
  1874. ProcessInformationLength);
  1875. ObDereferenceObject(Process);
  1876. return st;
  1877. case ProcessIoPortHandlers:
  1878. st = ObReferenceObjectByHandle (ProcessHandle,
  1879. PROCESS_SET_INFORMATION,
  1880. PsProcessType,
  1881. PreviousMode,
  1882. &Process,
  1883. NULL);
  1884. if (!NT_SUCCESS (st)) {
  1885. return st;
  1886. }
  1887. st = PspSetProcessIoHandlers (Process,
  1888. ProcessInformation,
  1889. ProcessInformationLength);
  1890. ObDereferenceObject (Process);
  1891. return st;
  1892. case ProcessUserModeIOPL:
  1893. //
  1894. // Must make sure the caller is a trusted subsystem with the
  1895. // appropriate privilege level before executing this call.
  1896. // If the calls returns FALSE we must return an error code.
  1897. //
  1898. if (!SeSinglePrivilegeCheck (SeTcbPrivilege, PreviousMode)) {
  1899. return STATUS_PRIVILEGE_NOT_HELD;
  1900. }
  1901. st = ObReferenceObjectByHandle (ProcessHandle,
  1902. PROCESS_SET_INFORMATION,
  1903. PsProcessType,
  1904. PreviousMode,
  1905. &Process,
  1906. NULL);
  1907. if (NT_SUCCESS (st)) {
  1908. #if defined (_X86_)
  1909. Ke386SetIOPL ();
  1910. #endif
  1911. ObDereferenceObject (Process);
  1912. }
  1913. return st;
  1914. //
  1915. // Enable/disable auto-alignment fixup for a process and all its threads.
  1916. //
  1917. case ProcessEnableAlignmentFaultFixup: {
  1918. if (ProcessInformationLength != sizeof (BOOLEAN)) {
  1919. return STATUS_INFO_LENGTH_MISMATCH;
  1920. }
  1921. try {
  1922. EnableAlignmentFaultFixup = *(PBOOLEAN)ProcessInformation;
  1923. } except (EXCEPTION_EXECUTE_HANDLER) {
  1924. return GetExceptionCode ();
  1925. }
  1926. st = ObReferenceObjectByHandle (ProcessHandle,
  1927. PROCESS_SET_INFORMATION,
  1928. PsProcessType,
  1929. PreviousMode,
  1930. &Process,
  1931. NULL);
  1932. if (!NT_SUCCESS (st)) {
  1933. return st;
  1934. }
  1935. if (EnableAlignmentFaultFixup) {
  1936. Process->DefaultHardErrorProcessing |= PROCESS_HARDERROR_ALIGNMENT_BIT;
  1937. } else {
  1938. Process->DefaultHardErrorProcessing &= ~PROCESS_HARDERROR_ALIGNMENT_BIT;
  1939. }
  1940. KeSetAutoAlignmentProcess (&(Process->Pcb), EnableAlignmentFaultFixup);
  1941. ObDereferenceObject (Process);
  1942. return STATUS_SUCCESS;
  1943. }
  1944. case ProcessWx86Information : {
  1945. ULONG VdmAllowedFlags;
  1946. if (ProcessInformationLength != sizeof (ULONG)) {
  1947. return STATUS_INFO_LENGTH_MISMATCH;
  1948. }
  1949. try {
  1950. VdmAllowedFlags = *(PULONG)ProcessInformation;
  1951. } except (EXCEPTION_EXECUTE_HANDLER) {
  1952. return GetExceptionCode ();
  1953. }
  1954. //
  1955. // Must make sure the caller is a trusted subsystem with the
  1956. // appropriate privilege level before executing this call.
  1957. // If the calls returns FALSE we must return an error code.
  1958. //
  1959. if (!SeSinglePrivilegeCheck (SeTcbPrivilege, PreviousMode)) {
  1960. return STATUS_PRIVILEGE_NOT_HELD;
  1961. }
  1962. //
  1963. // Make sure the ProcessHandle is indeed a process handle.
  1964. //
  1965. st = ObReferenceObjectByHandle (ProcessHandle,
  1966. PROCESS_SET_INFORMATION,
  1967. PsProcessType,
  1968. PreviousMode,
  1969. &Process,
  1970. NULL);
  1971. if (NT_SUCCESS (st)) {
  1972. //
  1973. // For now, non zero Flags will allowed VDM.
  1974. //
  1975. if (VdmAllowedFlags) {
  1976. PS_SET_BITS(&Process->Flags, PS_PROCESS_FLAGS_VDM_ALLOWED);
  1977. } else {
  1978. PS_CLEAR_BITS(&Process->Flags, PS_PROCESS_FLAGS_VDM_ALLOWED);
  1979. }
  1980. ObDereferenceObject(Process);
  1981. }
  1982. return st;
  1983. }
  1984. case ProcessAffinityMask:
  1985. if (ProcessInformationLength != sizeof (KAFFINITY)) {
  1986. return STATUS_INFO_LENGTH_MISMATCH;
  1987. }
  1988. try {
  1989. Affinity = *(PKAFFINITY)ProcessInformation;
  1990. } except (EXCEPTION_EXECUTE_HANDLER) {
  1991. return GetExceptionCode ();
  1992. }
  1993. AffinityWithMasks = Affinity & KeActiveProcessors;
  1994. if (!Affinity || (AffinityWithMasks != Affinity)) {
  1995. return STATUS_INVALID_PARAMETER;
  1996. }
  1997. st = ObReferenceObjectByHandle (ProcessHandle,
  1998. PROCESS_SET_INFORMATION,
  1999. PsProcessType,
  2000. PreviousMode,
  2001. &Process,
  2002. NULL);
  2003. if (!NT_SUCCESS (st)) {
  2004. return st;
  2005. }
  2006. //
  2007. // If the process has a job object, override whatever the process
  2008. // is calling with with the value from the job object
  2009. //
  2010. Job = Process->Job;
  2011. if (Job != NULL) {
  2012. KeEnterCriticalRegionThread (&CurrentThread->Tcb);
  2013. ExAcquireResourceSharedLite (&Job->JobLock, TRUE);
  2014. if (Job->LimitFlags & JOB_OBJECT_LIMIT_AFFINITY) {
  2015. AffinityWithMasks = Job->Affinity;
  2016. }
  2017. ExReleaseResourceLite (&Job->JobLock);
  2018. KeLeaveCriticalRegionThread (&CurrentThread->Tcb);
  2019. }
  2020. if (ExAcquireRundownProtection (&Process->RundownProtect)) {
  2021. PspLockProcessExclusive (Process, CurrentThread);
  2022. KeSetAffinityProcess (&Process->Pcb, AffinityWithMasks);
  2023. PspUnlockProcessExclusive (Process, CurrentThread);
  2024. ExReleaseRundownProtection (&Process->RundownProtect);
  2025. st = STATUS_SUCCESS;
  2026. } else {
  2027. st = STATUS_PROCESS_IS_TERMINATING;
  2028. }
  2029. ObDereferenceObject (Process);
  2030. return st;
  2031. case ProcessPriorityBoost:
  2032. if (ProcessInformationLength != sizeof (ULONG)) {
  2033. return STATUS_INFO_LENGTH_MISMATCH;
  2034. }
  2035. try {
  2036. DisableBoost = *(PULONG)ProcessInformation;
  2037. } except(EXCEPTION_EXECUTE_HANDLER) {
  2038. return GetExceptionCode ();
  2039. }
  2040. bDisableBoost = (DisableBoost ? TRUE : FALSE);
  2041. st = ObReferenceObjectByHandle (ProcessHandle,
  2042. PROCESS_SET_INFORMATION,
  2043. PsProcessType,
  2044. PreviousMode,
  2045. &Process,
  2046. NULL);
  2047. if (!NT_SUCCESS (st)) {
  2048. return st;
  2049. }
  2050. //
  2051. // Acquire rundown protection to give back the correct error
  2052. // if the process has or is being terminated.
  2053. //
  2054. if (!ExAcquireRundownProtection (&Process->RundownProtect)) {
  2055. st = STATUS_PROCESS_IS_TERMINATING;
  2056. } else {
  2057. PLIST_ENTRY Next;
  2058. PspLockProcessExclusive (Process, CurrentThread);
  2059. Process->Pcb.DisableBoost = bDisableBoost;
  2060. for (Next = Process->ThreadListHead.Flink;
  2061. Next != &Process->ThreadListHead;
  2062. Next = Next->Flink) {
  2063. Thread = (PETHREAD)(CONTAINING_RECORD(Next, ETHREAD, ThreadListEntry));
  2064. KeSetDisableBoostThread (&Thread->Tcb, bDisableBoost);
  2065. }
  2066. PspUnlockProcessExclusive (Process, CurrentThread);
  2067. ExReleaseRundownProtection (&Process->RundownProtect);
  2068. }
  2069. ObDereferenceObject (Process);
  2070. return st;
  2071. case ProcessDebugFlags : {
  2072. ULONG Flags;
  2073. if (ProcessInformationLength != sizeof (ULONG)) {
  2074. return STATUS_INFO_LENGTH_MISMATCH;
  2075. }
  2076. st = ObReferenceObjectByHandle (ProcessHandle,
  2077. PROCESS_SET_INFORMATION,
  2078. PsProcessType,
  2079. PreviousMode,
  2080. &Process,
  2081. NULL);
  2082. if (!NT_SUCCESS (st)) {
  2083. return st;
  2084. }
  2085. try {
  2086. Flags = *(PULONG) ProcessInformation;
  2087. } except (EXCEPTION_EXECUTE_HANDLER) {
  2088. Flags = 0;
  2089. st = GetExceptionCode ();
  2090. }
  2091. if (NT_SUCCESS (st)) {
  2092. if (Flags & ~PROCESS_DEBUG_INHERIT) {
  2093. st = STATUS_INVALID_PARAMETER;
  2094. } else {
  2095. if (Flags&PROCESS_DEBUG_INHERIT) {
  2096. PS_CLEAR_BITS (&Process->Flags, PS_PROCESS_FLAGS_NO_DEBUG_INHERIT);
  2097. } else {
  2098. PS_SET_BITS (&Process->Flags, PS_PROCESS_FLAGS_NO_DEBUG_INHERIT);
  2099. }
  2100. }
  2101. }
  2102. ObDereferenceObject (Process);
  2103. return st;
  2104. }
  2105. case ProcessDeviceMap:
  2106. DeviceMapInfo = (PPROCESS_DEVICEMAP_INFORMATION)ProcessInformation;
  2107. if (ProcessInformationLength != sizeof (DeviceMapInfo->Set)) {
  2108. return STATUS_INFO_LENGTH_MISMATCH;
  2109. }
  2110. try {
  2111. DirectoryHandle = DeviceMapInfo->Set.DirectoryHandle;
  2112. } except (EXCEPTION_EXECUTE_HANDLER) {
  2113. return GetExceptionCode ();
  2114. }
  2115. st = ObReferenceObjectByHandle (ProcessHandle,
  2116. PROCESS_SET_INFORMATION,
  2117. PsProcessType,
  2118. PreviousMode,
  2119. &Process,
  2120. NULL);
  2121. if (!NT_SUCCESS (st)) {
  2122. return st;
  2123. }
  2124. //
  2125. // The devmap fields here are synchronized using a private ob spinlock. We don't need to protect with a
  2126. // lock at this level.
  2127. //
  2128. st = ObSetDeviceMap (Process, DirectoryHandle);
  2129. ObDereferenceObject (Process);
  2130. return st;
  2131. case ProcessSessionInformation :
  2132. //
  2133. // Update Multi-User session specific process information
  2134. //
  2135. if (ProcessInformationLength != (ULONG) sizeof (PROCESS_SESSION_INFORMATION)) {
  2136. return STATUS_INFO_LENGTH_MISMATCH;
  2137. }
  2138. try {
  2139. SessionInfo = *(PPROCESS_SESSION_INFORMATION) ProcessInformation;
  2140. } except (EXCEPTION_EXECUTE_HANDLER) {
  2141. return GetExceptionCode ();
  2142. }
  2143. //
  2144. // We only allow TCB to set SessionId's
  2145. //
  2146. if (!SeSinglePrivilegeCheck (SeTcbPrivilege, PreviousMode)) {
  2147. return STATUS_PRIVILEGE_NOT_HELD;
  2148. }
  2149. //
  2150. // Reference process object
  2151. //
  2152. st = ObReferenceObjectByHandle (ProcessHandle,
  2153. PROCESS_SET_INFORMATION | PROCESS_SET_SESSIONID,
  2154. PsProcessType,
  2155. PreviousMode,
  2156. &Process,
  2157. NULL);
  2158. if (!NT_SUCCESS (st)) {
  2159. return st;
  2160. }
  2161. //
  2162. // Update SessionId in the Token
  2163. //
  2164. if (SessionInfo.SessionId != MmGetSessionId (Process)) {
  2165. st = STATUS_ACCESS_DENIED;
  2166. } else {
  2167. st = STATUS_SUCCESS;
  2168. }
  2169. ObDereferenceObject (Process);
  2170. return( st );
  2171. case ProcessBreakOnTermination:
  2172. if (ProcessInformationLength != sizeof (ULONG)) {
  2173. return STATUS_INFO_LENGTH_MISMATCH;
  2174. }
  2175. try {
  2176. EnableBreakOnTermination = *(PULONG)ProcessInformation;
  2177. } except (EXCEPTION_EXECUTE_HANDLER) {
  2178. return GetExceptionCode ();
  2179. }
  2180. if (!SeSinglePrivilegeCheck (SeDebugPrivilege, PreviousMode)) {
  2181. return STATUS_PRIVILEGE_NOT_HELD;
  2182. }
  2183. st = ObReferenceObjectByHandle (ProcessHandle,
  2184. PROCESS_SET_INFORMATION,
  2185. PsProcessType,
  2186. PreviousMode,
  2187. &Process,
  2188. NULL);
  2189. if (!NT_SUCCESS (st)) {
  2190. return st;
  2191. }
  2192. if ( EnableBreakOnTermination ) {
  2193. PS_SET_BITS (&Process->Flags, PS_PROCESS_FLAGS_BREAK_ON_TERMINATION);
  2194. } else {
  2195. PS_CLEAR_BITS (&Process->Flags, PS_PROCESS_FLAGS_BREAK_ON_TERMINATION);
  2196. }
  2197. ObDereferenceObject (Process);
  2198. return STATUS_SUCCESS;
  2199. case ProcessHandleTracing: {
  2200. PPROCESS_HANDLE_TRACING_ENABLE_EX Pht;
  2201. PHANDLE_TABLE HandleTable;
  2202. ULONG Slots;
  2203. Slots = 0;
  2204. //
  2205. // Zero length disables otherwise we enable
  2206. //
  2207. if (ProcessInformationLength != 0) {
  2208. if (ProcessInformationLength != sizeof (PROCESS_HANDLE_TRACING_ENABLE) &&
  2209. ProcessInformationLength != sizeof (PROCESS_HANDLE_TRACING_ENABLE_EX)) {
  2210. return STATUS_INFO_LENGTH_MISMATCH;
  2211. }
  2212. Pht = (PPROCESS_HANDLE_TRACING_ENABLE_EX) ProcessInformation;
  2213. try {
  2214. if (Pht->Flags != 0) {
  2215. return STATUS_INVALID_PARAMETER;
  2216. }
  2217. if (ProcessInformationLength == sizeof (PROCESS_HANDLE_TRACING_ENABLE_EX)) {
  2218. Slots = Pht->TotalSlots;
  2219. }
  2220. } except (EXCEPTION_EXECUTE_HANDLER) {
  2221. return GetExceptionCode ();
  2222. }
  2223. }
  2224. st = ObReferenceObjectByHandle (ProcessHandle,
  2225. PROCESS_SET_INFORMATION,
  2226. PsProcessType,
  2227. PreviousMode,
  2228. &Process,
  2229. NULL);
  2230. if (!NT_SUCCESS (st)) {
  2231. return st;
  2232. }
  2233. HandleTable = ObReferenceProcessHandleTable (Process);
  2234. if (HandleTable != NULL) {
  2235. if (ProcessInformationLength != 0) {
  2236. st = ExEnableHandleTracing (HandleTable, Slots);
  2237. } else {
  2238. st = ExDisableHandleTracing (HandleTable);
  2239. }
  2240. ObDereferenceProcessHandleTable (Process);
  2241. } else {
  2242. st = STATUS_PROCESS_IS_TERMINATING;
  2243. }
  2244. ObDereferenceObject(Process);
  2245. return st;
  2246. }
  2247. default:
  2248. return STATUS_INVALID_INFO_CLASS;
  2249. }
  2250. }
  2251. NTSTATUS
  2252. NtQueryInformationThread(
  2253. IN HANDLE ThreadHandle,
  2254. IN THREADINFOCLASS ThreadInformationClass,
  2255. OUT PVOID ThreadInformation,
  2256. IN ULONG ThreadInformationLength,
  2257. OUT PULONG ReturnLength OPTIONAL
  2258. )
  2259. /*++
  2260. Routine Description:
  2261. This function queries the state of a thread object and returns the
  2262. requested information in the specified record structure.
  2263. Arguments:
  2264. ThreadHandle - Supplies a handle to a thread object.
  2265. ThreadInformationClass - Supplies the class of information being
  2266. requested.
  2267. ThreadInformation - Supplies a pointer to a record that is to
  2268. receive the requested information.
  2269. ThreadInformationLength - Supplies the length of the record that is
  2270. to receive the requested information.
  2271. ReturnLength - Supplies an optional pointer to a variable that is to
  2272. receive the actual length of information that is returned.
  2273. Return Value:
  2274. TBS
  2275. --*/
  2276. {
  2277. LARGE_INTEGER PerformanceCount;
  2278. PETHREAD Thread;
  2279. PEPROCESS Process;
  2280. ULONG LastThread;
  2281. KPROCESSOR_MODE PreviousMode;
  2282. NTSTATUS st;
  2283. THREAD_BASIC_INFORMATION BasicInfo;
  2284. KERNEL_USER_TIMES SysUserTime;
  2285. PVOID Win32StartAddressValue;
  2286. ULONG DisableBoost;
  2287. ULONG IoPending ;
  2288. ULONG BreakOnTerminationEnabled;
  2289. PETHREAD CurrentThread;
  2290. //
  2291. // Get previous processor mode and probe output argument if necessary.
  2292. //
  2293. PAGED_CODE();
  2294. CurrentThread = PsGetCurrentThread ();
  2295. PreviousMode = KeGetPreviousModeByThread (&CurrentThread->Tcb);
  2296. if (PreviousMode != KernelMode) {
  2297. try {
  2298. //
  2299. // Since these functions don't change any state thats not reversible
  2300. // in the error paths we only probe the output buffer for write access.
  2301. // This improves performance by not touching the buffer multiple times
  2302. // And only writing the portions of the buffer that change.
  2303. //
  2304. ProbeForRead (ThreadInformation,
  2305. ThreadInformationLength,
  2306. sizeof(ULONG));
  2307. if (ARGUMENT_PRESENT( ReturnLength)) {
  2308. ProbeForWriteUlong (ReturnLength);
  2309. }
  2310. } except (EXCEPTION_EXECUTE_HANDLER) {
  2311. return GetExceptionCode ();
  2312. }
  2313. }
  2314. //
  2315. // Check argument validity.
  2316. //
  2317. switch (ThreadInformationClass) {
  2318. case ThreadBasicInformation:
  2319. if (ThreadInformationLength != (ULONG) sizeof (THREAD_BASIC_INFORMATION)) {
  2320. return STATUS_INFO_LENGTH_MISMATCH;
  2321. }
  2322. st = ObReferenceObjectByHandle (ThreadHandle,
  2323. THREAD_QUERY_INFORMATION,
  2324. PsThreadType,
  2325. PreviousMode,
  2326. &Thread,
  2327. NULL);
  2328. if (!NT_SUCCESS (st)) {
  2329. return st;
  2330. }
  2331. if (KeReadStateThread (&Thread->Tcb)) {
  2332. BasicInfo.ExitStatus = Thread->ExitStatus;
  2333. } else {
  2334. BasicInfo.ExitStatus = STATUS_PENDING;
  2335. }
  2336. BasicInfo.TebBaseAddress = (PTEB) Thread->Tcb.Teb;
  2337. BasicInfo.ClientId = Thread->Cid;
  2338. BasicInfo.AffinityMask = Thread->Tcb.Affinity;
  2339. BasicInfo.Priority = Thread->Tcb.Priority;
  2340. BasicInfo.BasePriority = KeQueryBasePriorityThread (&Thread->Tcb);
  2341. ObDereferenceObject (Thread);
  2342. //
  2343. // Either of these may cause an access violation. The
  2344. // exception handler will return access violation as
  2345. // status code. No further cleanup needs to be done.
  2346. //
  2347. try {
  2348. *(PTHREAD_BASIC_INFORMATION) ThreadInformation = BasicInfo;
  2349. if (ARGUMENT_PRESENT (ReturnLength) ) {
  2350. *ReturnLength = sizeof (THREAD_BASIC_INFORMATION);
  2351. }
  2352. } except (EXCEPTION_EXECUTE_HANDLER) {
  2353. return GetExceptionCode ();
  2354. }
  2355. return STATUS_SUCCESS;
  2356. case ThreadTimes:
  2357. if (ThreadInformationLength != (ULONG) sizeof (KERNEL_USER_TIMES)) {
  2358. return STATUS_INFO_LENGTH_MISMATCH;
  2359. }
  2360. st = ObReferenceObjectByHandle (ThreadHandle,
  2361. THREAD_QUERY_INFORMATION,
  2362. PsThreadType,
  2363. PreviousMode,
  2364. &Thread,
  2365. NULL);
  2366. if (!NT_SUCCESS (st)) {
  2367. return st;
  2368. }
  2369. SysUserTime.KernelTime.QuadPart = UInt32x32To64(Thread->Tcb.KernelTime,
  2370. KeMaximumIncrement);
  2371. SysUserTime.UserTime.QuadPart = UInt32x32To64(Thread->Tcb.UserTime,
  2372. KeMaximumIncrement);
  2373. SysUserTime.CreateTime.QuadPart = PS_GET_THREAD_CREATE_TIME(Thread);
  2374. if (KeReadStateThread(&Thread->Tcb)) {
  2375. SysUserTime.ExitTime = Thread->ExitTime;
  2376. } else {
  2377. SysUserTime.ExitTime.QuadPart = 0;
  2378. }
  2379. ObDereferenceObject (Thread);
  2380. //
  2381. // Either of these may cause an access violation. The
  2382. // exception handler will return access violation as
  2383. // status code. No further cleanup needs to be done.
  2384. //
  2385. try {
  2386. *(PKERNEL_USER_TIMES) ThreadInformation = SysUserTime;
  2387. if (ARGUMENT_PRESENT (ReturnLength) ) {
  2388. *ReturnLength = sizeof (KERNEL_USER_TIMES);
  2389. }
  2390. } except (EXCEPTION_EXECUTE_HANDLER) {
  2391. return GetExceptionCode ();
  2392. }
  2393. return STATUS_SUCCESS;
  2394. case ThreadDescriptorTableEntry :
  2395. st = ObReferenceObjectByHandle (ThreadHandle,
  2396. THREAD_QUERY_INFORMATION,
  2397. PsThreadType,
  2398. PreviousMode,
  2399. &Thread,
  2400. NULL);
  2401. if (!NT_SUCCESS (st)) {
  2402. return st;
  2403. }
  2404. st = PspQueryDescriptorThread (Thread,
  2405. ThreadInformation,
  2406. ThreadInformationLength,
  2407. ReturnLength);
  2408. ObDereferenceObject (Thread);
  2409. return st;
  2410. case ThreadQuerySetWin32StartAddress:
  2411. if (ThreadInformationLength != sizeof (ULONG_PTR)) {
  2412. return STATUS_INFO_LENGTH_MISMATCH;
  2413. }
  2414. st = ObReferenceObjectByHandle (ThreadHandle,
  2415. THREAD_QUERY_INFORMATION,
  2416. PsThreadType,
  2417. PreviousMode,
  2418. &Thread,
  2419. NULL);
  2420. if (!NT_SUCCESS (st)) {
  2421. return st;
  2422. }
  2423. Win32StartAddressValue = Thread->Win32StartAddress;
  2424. ObDereferenceObject (Thread);
  2425. try {
  2426. *(PVOID *) ThreadInformation = Win32StartAddressValue;
  2427. if (ARGUMENT_PRESENT (ReturnLength) ) {
  2428. *ReturnLength = sizeof (ULONG_PTR);
  2429. }
  2430. } except (EXCEPTION_EXECUTE_HANDLER) {
  2431. return GetExceptionCode ();
  2432. }
  2433. return st;
  2434. //
  2435. // Query thread cycle counter.
  2436. //
  2437. case ThreadPerformanceCount:
  2438. if (ThreadInformationLength != sizeof (LARGE_INTEGER)) {
  2439. return STATUS_INFO_LENGTH_MISMATCH;
  2440. }
  2441. st = ObReferenceObjectByHandle (ThreadHandle,
  2442. THREAD_QUERY_INFORMATION,
  2443. PsThreadType,
  2444. PreviousMode,
  2445. &Thread,
  2446. NULL);
  2447. if (!NT_SUCCESS (st)) {
  2448. return st;
  2449. }
  2450. #if defined (PERF_DATA)
  2451. PerformanceCount.LowPart = Thread->PerformanceCountLow;
  2452. PerformanceCount.HighPart = Thread->PerformanceCountHigh;
  2453. #else
  2454. PerformanceCount.QuadPart = 0;
  2455. #endif
  2456. ObDereferenceObject(Thread);
  2457. try {
  2458. *(PLARGE_INTEGER)ThreadInformation = PerformanceCount;
  2459. if (ARGUMENT_PRESENT (ReturnLength) ) {
  2460. *ReturnLength = sizeof (LARGE_INTEGER);
  2461. }
  2462. } except (EXCEPTION_EXECUTE_HANDLER) {
  2463. return GetExceptionCode ();
  2464. }
  2465. return st;
  2466. case ThreadAmILastThread:
  2467. if (ThreadInformationLength != sizeof (ULONG)) {
  2468. return STATUS_INFO_LENGTH_MISMATCH;
  2469. }
  2470. Process = THREAD_TO_PROCESS (CurrentThread);
  2471. if (Process->ActiveThreads == 1) {
  2472. LastThread = 1;
  2473. } else {
  2474. LastThread = 0;
  2475. }
  2476. try {
  2477. *(PULONG)ThreadInformation = LastThread;
  2478. if (ARGUMENT_PRESENT (ReturnLength)) {
  2479. *ReturnLength = sizeof (ULONG);
  2480. }
  2481. } except (EXCEPTION_EXECUTE_HANDLER) {
  2482. return GetExceptionCode ();
  2483. }
  2484. return STATUS_SUCCESS;
  2485. case ThreadPriorityBoost:
  2486. if (ThreadInformationLength != sizeof (ULONG)) {
  2487. return STATUS_INFO_LENGTH_MISMATCH;
  2488. }
  2489. st = ObReferenceObjectByHandle (ThreadHandle,
  2490. THREAD_QUERY_INFORMATION,
  2491. PsThreadType,
  2492. PreviousMode,
  2493. &Thread,
  2494. NULL);
  2495. if (!NT_SUCCESS (st)) {
  2496. return st;
  2497. }
  2498. DisableBoost = Thread->Tcb.DisableBoost ? 1 : 0;
  2499. ObDereferenceObject (Thread);
  2500. try {
  2501. *(PULONG)ThreadInformation = DisableBoost;
  2502. if (ARGUMENT_PRESENT (ReturnLength) ) {
  2503. *ReturnLength = sizeof (ULONG);
  2504. }
  2505. } except (EXCEPTION_EXECUTE_HANDLER) {
  2506. return GetExceptionCode ();
  2507. }
  2508. return st;
  2509. case ThreadIsIoPending:
  2510. if (ThreadInformationLength != sizeof (ULONG)) {
  2511. return STATUS_INFO_LENGTH_MISMATCH;
  2512. }
  2513. st = ObReferenceObjectByHandle (ThreadHandle,
  2514. THREAD_QUERY_INFORMATION,
  2515. PsThreadType,
  2516. PreviousMode,
  2517. &Thread,
  2518. NULL);
  2519. if (!NT_SUCCESS (st)) {
  2520. return st;
  2521. }
  2522. //
  2523. // Its impossible to synchronize this cross thread.
  2524. // Since the result is worthless the second its fetched
  2525. // this isn't a problem.
  2526. //
  2527. IoPending = !IsListEmpty (&Thread->IrpList);
  2528. ObDereferenceObject (Thread);
  2529. try {
  2530. *(PULONG)ThreadInformation = IoPending ;
  2531. if (ARGUMENT_PRESENT (ReturnLength) ) {
  2532. *ReturnLength = sizeof (ULONG);
  2533. }
  2534. } except (EXCEPTION_EXECUTE_HANDLER) {
  2535. return GetExceptionCode();
  2536. }
  2537. return STATUS_SUCCESS ;
  2538. case ThreadBreakOnTermination:
  2539. if (ThreadInformationLength != sizeof (ULONG)) {
  2540. return STATUS_INFO_LENGTH_MISMATCH;
  2541. }
  2542. st = ObReferenceObjectByHandle (ThreadHandle,
  2543. THREAD_QUERY_INFORMATION,
  2544. PsThreadType,
  2545. PreviousMode,
  2546. &Thread,
  2547. NULL);
  2548. if (!NT_SUCCESS (st)) {
  2549. return st;
  2550. }
  2551. if (Thread->CrossThreadFlags & PS_CROSS_THREAD_FLAGS_BREAK_ON_TERMINATION) {
  2552. BreakOnTerminationEnabled = 1;
  2553. } else {
  2554. BreakOnTerminationEnabled = 0;
  2555. }
  2556. ObDereferenceObject(Thread);
  2557. try {
  2558. *(PULONG) ThreadInformation = BreakOnTerminationEnabled;
  2559. if (ARGUMENT_PRESENT(ReturnLength) ) {
  2560. *ReturnLength = sizeof(ULONG);
  2561. }
  2562. } except (EXCEPTION_EXECUTE_HANDLER) {
  2563. return GetExceptionCode ();
  2564. }
  2565. return STATUS_SUCCESS;
  2566. default:
  2567. return STATUS_INVALID_INFO_CLASS;
  2568. }
  2569. }
  2570. NTSTATUS
  2571. NtSetInformationThread(
  2572. IN HANDLE ThreadHandle,
  2573. IN THREADINFOCLASS ThreadInformationClass,
  2574. IN PVOID ThreadInformation,
  2575. IN ULONG ThreadInformationLength
  2576. )
  2577. /*++
  2578. Routine Description:
  2579. This function sets the state of a thread object.
  2580. Arguments:
  2581. ThreadHandle - Supplies a handle to a thread object.
  2582. ThreadInformationClass - Supplies the class of information being
  2583. set.
  2584. ThreadInformation - Supplies a pointer to a record that contains the
  2585. information to set.
  2586. ThreadInformationLength - Supplies the length of the record that contains
  2587. the information to set.
  2588. Return Value:
  2589. TBS
  2590. --*/
  2591. {
  2592. PETHREAD Thread;
  2593. PETHREAD CurrentThread;
  2594. PEPROCESS Process;
  2595. KPROCESSOR_MODE PreviousMode;
  2596. NTSTATUS st;
  2597. KAFFINITY Affinity, AffinityWithMasks;
  2598. KPRIORITY Priority;
  2599. LONG BasePriority;
  2600. ULONG TlsIndex;
  2601. PVOID TlsArrayAddress;
  2602. PVOID Win32StartAddressValue;
  2603. ULONG ProbeAlignment;
  2604. BOOLEAN EnableAlignmentFaultFixup;
  2605. ULONG EnableBreakOnTermination;
  2606. ULONG IdealProcessor;
  2607. ULONG DisableBoost;
  2608. PVOID *ExpansionSlots;
  2609. HANDLE ImpersonationTokenHandle;
  2610. BOOLEAN HasPrivilege;
  2611. PEJOB Job;
  2612. PTEB Teb;
  2613. PAGED_CODE();
  2614. //
  2615. // Get previous processor mode and probe input argument if necessary.
  2616. //
  2617. CurrentThread = PsGetCurrentThread ();
  2618. PreviousMode = KeGetPreviousModeByThread (&CurrentThread->Tcb);
  2619. if (PreviousMode != KernelMode) {
  2620. try {
  2621. switch (ThreadInformationClass) {
  2622. case ThreadPriority :
  2623. ProbeAlignment = sizeof(KPRIORITY);
  2624. break;
  2625. case ThreadAffinityMask :
  2626. case ThreadQuerySetWin32StartAddress :
  2627. ProbeAlignment = sizeof (ULONG_PTR);
  2628. break;
  2629. case ThreadEnableAlignmentFaultFixup :
  2630. ProbeAlignment = sizeof (BOOLEAN);
  2631. break;
  2632. default :
  2633. ProbeAlignment = sizeof(ULONG);
  2634. }
  2635. ProbeForRead(
  2636. ThreadInformation,
  2637. ThreadInformationLength,
  2638. ProbeAlignment);
  2639. } except (EXCEPTION_EXECUTE_HANDLER) {
  2640. return GetExceptionCode ();
  2641. }
  2642. }
  2643. //
  2644. // Check argument validity.
  2645. //
  2646. switch (ThreadInformationClass) {
  2647. case ThreadPriority:
  2648. if (ThreadInformationLength != sizeof (KPRIORITY)) {
  2649. return STATUS_INFO_LENGTH_MISMATCH;
  2650. }
  2651. try {
  2652. Priority = *(KPRIORITY *)ThreadInformation;
  2653. } except (EXCEPTION_EXECUTE_HANDLER) {
  2654. return GetExceptionCode ();
  2655. }
  2656. if (Priority > HIGH_PRIORITY ||
  2657. Priority <= LOW_PRIORITY) {
  2658. return STATUS_INVALID_PARAMETER;
  2659. }
  2660. if (Priority >= LOW_REALTIME_PRIORITY) {
  2661. //
  2662. // Increasing the priority of a thread beyond
  2663. // LOW_REALTIME_PRIORITY is a privileged operation.
  2664. //
  2665. HasPrivilege = SeCheckPrivilegedObject (SeIncreaseBasePriorityPrivilege,
  2666. ThreadHandle,
  2667. THREAD_SET_INFORMATION,
  2668. PreviousMode);
  2669. if (!HasPrivilege) {
  2670. return STATUS_PRIVILEGE_NOT_HELD;
  2671. }
  2672. }
  2673. st = ObReferenceObjectByHandle (ThreadHandle,
  2674. THREAD_SET_INFORMATION,
  2675. PsThreadType,
  2676. PreviousMode,
  2677. &Thread,
  2678. NULL);
  2679. if (!NT_SUCCESS (st)) {
  2680. return st;
  2681. }
  2682. KeSetPriorityThread (&Thread->Tcb, Priority);
  2683. ObDereferenceObject (Thread);
  2684. return STATUS_SUCCESS;
  2685. case ThreadBasePriority:
  2686. if (ThreadInformationLength != sizeof (LONG)) {
  2687. return STATUS_INFO_LENGTH_MISMATCH;
  2688. }
  2689. try {
  2690. BasePriority = *(PLONG)ThreadInformation;
  2691. } except (EXCEPTION_EXECUTE_HANDLER) {
  2692. return GetExceptionCode ();
  2693. }
  2694. st = ObReferenceObjectByHandle (ThreadHandle,
  2695. THREAD_SET_INFORMATION,
  2696. PsThreadType,
  2697. PreviousMode,
  2698. &Thread,
  2699. NULL);
  2700. if (!NT_SUCCESS (st)) {
  2701. return st;
  2702. }
  2703. Process = THREAD_TO_PROCESS (Thread);
  2704. if (BasePriority > THREAD_BASE_PRIORITY_MAX ||
  2705. BasePriority < THREAD_BASE_PRIORITY_MIN) {
  2706. if (BasePriority == THREAD_BASE_PRIORITY_LOWRT+1 ||
  2707. BasePriority == THREAD_BASE_PRIORITY_IDLE-1) {
  2708. ;
  2709. } else {
  2710. //
  2711. // Allow csrss, or realtime processes to select any
  2712. // priority
  2713. //
  2714. if (PsGetCurrentProcessByThread (CurrentThread) == ExpDefaultErrorPortProcess ||
  2715. Process->PriorityClass == PROCESS_PRIORITY_CLASS_REALTIME) {
  2716. ;
  2717. } else {
  2718. ObDereferenceObject (Thread);
  2719. return STATUS_INVALID_PARAMETER;
  2720. }
  2721. }
  2722. }
  2723. //
  2724. // If the thread is running within a job object, and the job
  2725. // object has a priority class limit, do not allow
  2726. // priority adjustments that raise the thread's priority, unless
  2727. // the priority class is realtime
  2728. //
  2729. Job = Process->Job;
  2730. if (Job != NULL && (Job->LimitFlags & JOB_OBJECT_LIMIT_PRIORITY_CLASS)) {
  2731. if (Process->PriorityClass != PROCESS_PRIORITY_CLASS_REALTIME){
  2732. if (BasePriority > 0) {
  2733. ObDereferenceObject (Thread);
  2734. return STATUS_SUCCESS;
  2735. }
  2736. }
  2737. }
  2738. KeSetBasePriorityThread (&Thread->Tcb, BasePriority);
  2739. ObDereferenceObject (Thread);
  2740. return STATUS_SUCCESS;
  2741. case ThreadEnableAlignmentFaultFixup:
  2742. if (ThreadInformationLength != sizeof (BOOLEAN) ) {
  2743. return STATUS_INFO_LENGTH_MISMATCH;
  2744. }
  2745. try {
  2746. EnableAlignmentFaultFixup = *(PBOOLEAN)ThreadInformation;
  2747. } except (EXCEPTION_EXECUTE_HANDLER) {
  2748. return GetExceptionCode ();
  2749. }
  2750. st = ObReferenceObjectByHandle (ThreadHandle,
  2751. THREAD_SET_INFORMATION,
  2752. PsThreadType,
  2753. PreviousMode,
  2754. &Thread,
  2755. NULL);
  2756. if (!NT_SUCCESS (st)) {
  2757. return st;
  2758. }
  2759. KeSetAutoAlignmentThread (&Thread->Tcb, EnableAlignmentFaultFixup);
  2760. ObDereferenceObject (Thread);
  2761. return STATUS_SUCCESS;
  2762. case ThreadAffinityMask:
  2763. if (ThreadInformationLength != sizeof (KAFFINITY)) {
  2764. return STATUS_INFO_LENGTH_MISMATCH;
  2765. }
  2766. try {
  2767. Affinity = *(PKAFFINITY)ThreadInformation;
  2768. } except (EXCEPTION_EXECUTE_HANDLER) {
  2769. return GetExceptionCode ();
  2770. }
  2771. if (!Affinity) {
  2772. return STATUS_INVALID_PARAMETER;
  2773. }
  2774. st = ObReferenceObjectByHandle (ThreadHandle,
  2775. THREAD_SET_INFORMATION,
  2776. PsThreadType,
  2777. PreviousMode,
  2778. &Thread,
  2779. NULL);
  2780. if (!NT_SUCCESS (st)) {
  2781. return st;
  2782. }
  2783. Process = THREAD_TO_PROCESS (Thread);
  2784. if (ExAcquireRundownProtection (&Process->RundownProtect)) {
  2785. PspLockProcessShared (Process, CurrentThread);
  2786. AffinityWithMasks = Affinity & Process->Pcb.Affinity;
  2787. if (AffinityWithMasks != Affinity) {
  2788. st = STATUS_INVALID_PARAMETER;
  2789. } else {
  2790. KeSetAffinityThread (&Thread->Tcb,
  2791. AffinityWithMasks);
  2792. st = STATUS_SUCCESS;
  2793. }
  2794. PspUnlockProcessShared (Process, CurrentThread);
  2795. ExReleaseRundownProtection (&Process->RundownProtect);
  2796. } else {
  2797. st = STATUS_PROCESS_IS_TERMINATING;
  2798. }
  2799. ObDereferenceObject (Thread);
  2800. return st;
  2801. case ThreadImpersonationToken:
  2802. if (ThreadInformationLength != sizeof (HANDLE)) {
  2803. return STATUS_INFO_LENGTH_MISMATCH;
  2804. }
  2805. try {
  2806. ImpersonationTokenHandle = *(PHANDLE) ThreadInformation;
  2807. } except (EXCEPTION_EXECUTE_HANDLER) {
  2808. return GetExceptionCode ();
  2809. }
  2810. st = ObReferenceObjectByHandle (ThreadHandle,
  2811. THREAD_SET_THREAD_TOKEN,
  2812. PsThreadType,
  2813. PreviousMode,
  2814. &Thread,
  2815. NULL);
  2816. if (!NT_SUCCESS (st)) {
  2817. return st;
  2818. }
  2819. //
  2820. // Check for proper access to (and type of) the token, and assign
  2821. // it as the thread's impersonation token.
  2822. //
  2823. st = PsAssignImpersonationToken (Thread, ImpersonationTokenHandle);
  2824. ObDereferenceObject (Thread);
  2825. return st;
  2826. case ThreadQuerySetWin32StartAddress:
  2827. if (ThreadInformationLength != sizeof (ULONG_PTR)) {
  2828. return STATUS_INFO_LENGTH_MISMATCH;
  2829. }
  2830. try {
  2831. Win32StartAddressValue = *(PVOID *) ThreadInformation;
  2832. } except (EXCEPTION_EXECUTE_HANDLER) {
  2833. return GetExceptionCode ();
  2834. }
  2835. st = ObReferenceObjectByHandle (ThreadHandle,
  2836. THREAD_SET_INFORMATION,
  2837. PsThreadType,
  2838. PreviousMode,
  2839. &Thread,
  2840. NULL);
  2841. if (!NT_SUCCESS (st)) {
  2842. return st;
  2843. }
  2844. Thread->Win32StartAddress = (PVOID)Win32StartAddressValue;
  2845. ObDereferenceObject (Thread);
  2846. return st;
  2847. case ThreadIdealProcessor:
  2848. if (ThreadInformationLength != sizeof (ULONG)) {
  2849. return STATUS_INFO_LENGTH_MISMATCH;
  2850. }
  2851. try {
  2852. IdealProcessor = *(PULONG)ThreadInformation;
  2853. } except (EXCEPTION_EXECUTE_HANDLER) {
  2854. return GetExceptionCode ();
  2855. }
  2856. if (IdealProcessor > MAXIMUM_PROCESSORS) {
  2857. return STATUS_INVALID_PARAMETER;
  2858. }
  2859. st = ObReferenceObjectByHandle (ThreadHandle,
  2860. THREAD_SET_INFORMATION,
  2861. PsThreadType,
  2862. PreviousMode,
  2863. &Thread,
  2864. NULL);
  2865. if (!NT_SUCCESS (st)) {
  2866. return st;
  2867. }
  2868. //
  2869. // this is sort of a slimey way of returning info from this set only
  2870. // api
  2871. //
  2872. st = (NTSTATUS)KeSetIdealProcessorThread (&Thread->Tcb, (CCHAR)IdealProcessor);
  2873. //
  2874. // We could be making cross process and/or cross thread references here.
  2875. // Acquire rundown protection to make sure the teb can't go away.
  2876. //
  2877. Teb = Thread->Tcb.Teb;
  2878. if (Teb != NULL && ExAcquireRundownProtection (&Thread->RundownProtect)) {
  2879. PEPROCESS TargetProcess;
  2880. BOOLEAN Attached;
  2881. KAPC_STATE ApcState;
  2882. Attached = FALSE;
  2883. //
  2884. // See if we are crossing process boundaries and if so attach to the target
  2885. //
  2886. TargetProcess = THREAD_TO_PROCESS (Thread);
  2887. if (TargetProcess != PsGetCurrentProcessByThread (CurrentThread)) {
  2888. KeStackAttachProcess (&TargetProcess->Pcb, &ApcState);
  2889. Attached = TRUE;
  2890. }
  2891. try {
  2892. Teb->IdealProcessor = Thread->Tcb.IdealProcessor;
  2893. } except (EXCEPTION_EXECUTE_HANDLER) {
  2894. }
  2895. if (Attached) {
  2896. KeUnstackDetachProcess (&ApcState);
  2897. }
  2898. ExReleaseRundownProtection (&Thread->RundownProtect);
  2899. }
  2900. ObDereferenceObject (Thread);
  2901. return st;
  2902. case ThreadPriorityBoost:
  2903. if (ThreadInformationLength != sizeof (ULONG)) {
  2904. return STATUS_INFO_LENGTH_MISMATCH;
  2905. }
  2906. try {
  2907. DisableBoost = *(PULONG)ThreadInformation;
  2908. } except (EXCEPTION_EXECUTE_HANDLER) {
  2909. return GetExceptionCode ();
  2910. }
  2911. st = ObReferenceObjectByHandle (ThreadHandle,
  2912. THREAD_SET_INFORMATION,
  2913. PsThreadType,
  2914. PreviousMode,
  2915. &Thread,
  2916. NULL);
  2917. if (!NT_SUCCESS (st)) {
  2918. return st;
  2919. }
  2920. KeSetDisableBoostThread (&Thread->Tcb,DisableBoost ? TRUE : FALSE);
  2921. ObDereferenceObject (Thread);
  2922. return st;
  2923. case ThreadZeroTlsCell:
  2924. if (ThreadInformationLength != sizeof (ULONG)) {
  2925. return STATUS_INFO_LENGTH_MISMATCH;
  2926. }
  2927. try {
  2928. TlsIndex = *(PULONG) ThreadInformation;
  2929. } except (EXCEPTION_EXECUTE_HANDLER) {
  2930. return GetExceptionCode ();
  2931. }
  2932. st = ObReferenceObjectByHandle (ThreadHandle,
  2933. THREAD_SET_INFORMATION,
  2934. PsThreadType,
  2935. PreviousMode,
  2936. &Thread,
  2937. NULL);
  2938. if (!NT_SUCCESS (st)) {
  2939. return st;
  2940. }
  2941. ObDereferenceObject (Thread);
  2942. if (Thread != CurrentThread) {
  2943. return STATUS_INVALID_PARAMETER;
  2944. }
  2945. Process = THREAD_TO_PROCESS (Thread);
  2946. // The 32bit TEB needs to be set if this is a WOW64 process on a 64BIT system.
  2947. // This code isn't 100% correct since threads have a conversion state where they
  2948. // are chaning from 64 to 32 and they don't have a TEB32 yet. Fortunatly, the slots
  2949. // will be zero when the thread is created so no damage is done by not clearing it here.
  2950. // Note that the test for the process type is inside the inner loop. This
  2951. // is bad programming, but this function is hardly time constrained and
  2952. // fixing this with complex macros would not be worth it due to the loss of clairity.
  2953. for (Thread = PsGetNextProcessThread (Process, NULL);
  2954. Thread != NULL;
  2955. Thread = PsGetNextProcessThread (Process, Thread)) {
  2956. //
  2957. // We are doing cross thread TEB references and need to prevent TEB deletion.
  2958. //
  2959. if (ExAcquireRundownProtection (&Thread->RundownProtect)) {
  2960. Teb = Thread->Tcb.Teb;
  2961. if (Teb != NULL) {
  2962. try {
  2963. #if defined(_WIN64)
  2964. PTEB32 Teb32 = NULL;
  2965. PLONG ExpansionSlots32;
  2966. if (Process->Wow64Process) { //wow64 process
  2967. Teb32 = WOW64_GET_TEB32(Teb); //No probing needed on regular TEB.
  2968. }
  2969. #endif
  2970. if (TlsIndex > TLS_MINIMUM_AVAILABLE-1) {
  2971. if ( TlsIndex < (TLS_MINIMUM_AVAILABLE+TLS_EXPANSION_SLOTS) - 1 ) {
  2972. //
  2973. // This is an expansion slot, so see if the thread
  2974. // has an expansion cell
  2975. //
  2976. #if defined(_WIN64)
  2977. if (Process->Wow64Process) { //Wow64 process.
  2978. if (Teb32) {
  2979. ExpansionSlots32 = ULongToPtr(ProbeAndReadUlong(&(Teb32->TlsExpansionSlots)));
  2980. if (ExpansionSlots32) {
  2981. ProbeAndWriteLong(ExpansionSlots32 + TlsIndex - TLS_MINIMUM_AVAILABLE, 0);
  2982. }
  2983. }
  2984. } else {
  2985. #endif
  2986. ExpansionSlots = Teb->TlsExpansionSlots;
  2987. ProbeForReadSmallStructure (ExpansionSlots, TLS_EXPANSION_SLOTS*4, 8);
  2988. if ( ExpansionSlots ) {
  2989. ExpansionSlots[TlsIndex-TLS_MINIMUM_AVAILABLE] = 0;
  2990. }
  2991. #if defined(_WIN64)
  2992. }
  2993. #endif
  2994. }
  2995. } else {
  2996. #if defined(_WIN64)
  2997. if (Process->Wow64Process) { //wow64 process
  2998. if(Teb32) {
  2999. ProbeAndWriteUlong(Teb32->TlsSlots + TlsIndex, 0);
  3000. }
  3001. } else {
  3002. #endif
  3003. Teb->TlsSlots[TlsIndex] = NULL;
  3004. #if defined(_WIN64)
  3005. }
  3006. #endif
  3007. }
  3008. } except (EXCEPTION_EXECUTE_HANDLER) {
  3009. st = GetExceptionCode ();
  3010. }
  3011. }
  3012. ExReleaseRundownProtection (&Thread->RundownProtect);
  3013. }
  3014. }
  3015. return st;
  3016. break;
  3017. case ThreadSetTlsArrayAddress:
  3018. if (ThreadInformationLength != sizeof (PVOID)) {
  3019. return STATUS_INFO_LENGTH_MISMATCH;
  3020. }
  3021. try {
  3022. TlsArrayAddress = *(PVOID *)ThreadInformation;
  3023. } except (EXCEPTION_EXECUTE_HANDLER) {
  3024. return GetExceptionCode ();
  3025. }
  3026. st = ObReferenceObjectByHandle (ThreadHandle,
  3027. THREAD_SET_INFORMATION,
  3028. PsThreadType,
  3029. PreviousMode,
  3030. &Thread,
  3031. NULL);
  3032. if (!NT_SUCCESS (st)) {
  3033. return st;
  3034. }
  3035. Thread->Tcb.TlsArray = TlsArrayAddress;
  3036. ObDereferenceObject (Thread);
  3037. return st;
  3038. break;
  3039. case ThreadHideFromDebugger:
  3040. if (ThreadInformationLength != 0) {
  3041. return STATUS_INFO_LENGTH_MISMATCH;
  3042. }
  3043. st = ObReferenceObjectByHandle (ThreadHandle,
  3044. THREAD_SET_INFORMATION,
  3045. PsThreadType,
  3046. PreviousMode,
  3047. &Thread,
  3048. NULL);
  3049. if (!NT_SUCCESS (st)) {
  3050. return st;
  3051. }
  3052. PS_SET_BITS (&Thread->CrossThreadFlags, PS_CROSS_THREAD_FLAGS_HIDEFROMDBG);
  3053. ObDereferenceObject (Thread);
  3054. return st;
  3055. break;
  3056. case ThreadBreakOnTermination:
  3057. if (ThreadInformationLength != sizeof (ULONG)) {
  3058. return STATUS_INFO_LENGTH_MISMATCH;
  3059. }
  3060. try {
  3061. EnableBreakOnTermination = *(PULONG)ThreadInformation;
  3062. } except (EXCEPTION_EXECUTE_HANDLER) {
  3063. return GetExceptionCode();
  3064. }
  3065. if (!SeSinglePrivilegeCheck (SeDebugPrivilege, PreviousMode)) {
  3066. return STATUS_PRIVILEGE_NOT_HELD;
  3067. }
  3068. st = ObReferenceObjectByHandle (ThreadHandle,
  3069. THREAD_SET_INFORMATION,
  3070. PsThreadType,
  3071. PreviousMode,
  3072. &Thread,
  3073. NULL);
  3074. if (!NT_SUCCESS (st)) {
  3075. return st;
  3076. }
  3077. if (EnableBreakOnTermination) {
  3078. PS_SET_BITS (&Thread->CrossThreadFlags, PS_CROSS_THREAD_FLAGS_BREAK_ON_TERMINATION);
  3079. } else {
  3080. PS_CLEAR_BITS (&Thread->CrossThreadFlags, PS_CROSS_THREAD_FLAGS_BREAK_ON_TERMINATION);
  3081. }
  3082. ObDereferenceObject (Thread);
  3083. return STATUS_SUCCESS;
  3084. default:
  3085. return STATUS_INVALID_INFO_CLASS;
  3086. }
  3087. }
  3088. ULONG
  3089. NtGetCurrentProcessorNumber(
  3090. VOID
  3091. )
  3092. {
  3093. return KeGetCurrentProcessorNumber();
  3094. }
  3095. VOID
  3096. PsWatchWorkingSet(
  3097. IN NTSTATUS Status,
  3098. IN PVOID PcValue,
  3099. IN PVOID Va
  3100. )
  3101. /*++
  3102. Routine Description:
  3103. This function collects data about page faults and stores information
  3104. about the page fault in the current process's data structure.
  3105. Arguments:
  3106. Status - Supplies the success completion status.
  3107. PcValue - Supplies the instruction address that caused the page fault.
  3108. Va - Supplies the virtual address that caused the page fault.
  3109. --*/
  3110. {
  3111. PEPROCESS Process;
  3112. PPAGEFAULT_HISTORY WorkingSetCatcher;
  3113. KIRQL OldIrql;
  3114. BOOLEAN TransitionFault = FALSE;
  3115. //
  3116. // Both transition and demand zero faults count as soft faults. Only disk
  3117. // reads count as hard faults.
  3118. //
  3119. if ( Status <= STATUS_PAGE_FAULT_DEMAND_ZERO ) {
  3120. TransitionFault = TRUE;
  3121. }
  3122. Process = PsGetCurrentProcess();
  3123. WorkingSetCatcher = Process->WorkingSetWatch;
  3124. if (WorkingSetCatcher == NULL) {
  3125. return;
  3126. }
  3127. ExAcquireSpinLock(&WorkingSetCatcher->SpinLock,&OldIrql);
  3128. if (WorkingSetCatcher->CurrentIndex >= WorkingSetCatcher->MaxIndex) {
  3129. ExReleaseSpinLock(&WorkingSetCatcher->SpinLock,OldIrql);
  3130. return;
  3131. }
  3132. //
  3133. // Store the Pc and Va values in the buffer. Use the least sig. bit
  3134. // of the Va to store whether it was a soft or hard fault
  3135. //
  3136. WorkingSetCatcher->WatchInfo[WorkingSetCatcher->CurrentIndex].FaultingPc = PcValue;
  3137. WorkingSetCatcher->WatchInfo[WorkingSetCatcher->CurrentIndex].FaultingVa = TransitionFault ? (PVOID)((ULONG_PTR)Va | 1) : (PVOID)((ULONG_PTR)Va & 0xfffffffe) ;
  3138. WorkingSetCatcher->CurrentIndex++;
  3139. ExReleaseSpinLock(&WorkingSetCatcher->SpinLock,OldIrql);
  3140. return;
  3141. }
  3142. #ifdef ALLOC_DATA_PRAGMA
  3143. #pragma data_seg("PAGEDATA")
  3144. #endif
  3145. PKWIN32_PROCESS_CALLOUT PspW32ProcessCallout = NULL;
  3146. PKWIN32_THREAD_CALLOUT PspW32ThreadCallout = NULL;
  3147. PKWIN32_JOB_CALLOUT PspW32JobCallout = NULL;
  3148. #ifdef ALLOC_DATA_PRAGMA
  3149. #pragma data_seg()
  3150. #endif
  3151. extern PKWIN32_POWEREVENT_CALLOUT PopEventCallout;
  3152. extern PKWIN32_POWERSTATE_CALLOUT PopStateCallout;
  3153. NTKERNELAPI
  3154. VOID
  3155. PsEstablishWin32Callouts(
  3156. IN PKWIN32_CALLOUTS_FPNS pWin32Callouts )
  3157. /*++
  3158. Routine Description:
  3159. This function is used by the Win32 kernel mode component to
  3160. register callout functions for process/thread init/deinit functions
  3161. and to report the sizes of the structures.
  3162. Arguments:
  3163. ProcessCallout - Supplies the address of the function to be called when
  3164. a process is either created or deleted.
  3165. ThreadCallout - Supplies the address of the function to be called when
  3166. a thread is either created or deleted.
  3167. GlobalAtomTableCallout - Supplies the address of the function to be called
  3168. to get the correct global atom table for the current process
  3169. PowerEventCallout - Supplies the address of a function to be called when
  3170. a power event occurs.
  3171. PowerStateCallout - Supplies the address of a function to be called when
  3172. the power state changes.
  3173. JobCallout - Supplies the address of a function to be called when
  3174. the job state changes or a process is assigned to a job.
  3175. BatchFlushRoutine - Supplies the address of the function to be called
  3176. Return Value:
  3177. None.
  3178. --*/
  3179. {
  3180. PAGED_CODE();
  3181. PspW32ProcessCallout = pWin32Callouts->ProcessCallout;
  3182. PspW32ThreadCallout = pWin32Callouts->ThreadCallout;
  3183. ExGlobalAtomTableCallout = pWin32Callouts->GlobalAtomTableCallout;
  3184. KeGdiFlushUserBatch = (PGDI_BATCHFLUSH_ROUTINE)pWin32Callouts->BatchFlushRoutine;
  3185. PopEventCallout = pWin32Callouts->PowerEventCallout;
  3186. PopStateCallout = pWin32Callouts->PowerStateCallout;
  3187. PspW32JobCallout = pWin32Callouts->JobCallout;
  3188. // PoSetSystemState(ES_SYSTEM_REQUIRED);
  3189. ExDesktopOpenProcedureCallout = pWin32Callouts->DesktopOpenProcedure;
  3190. ExDesktopOkToCloseProcedureCallout = pWin32Callouts->DesktopOkToCloseProcedure;
  3191. ExDesktopCloseProcedureCallout = pWin32Callouts->DesktopCloseProcedure;
  3192. ExDesktopDeleteProcedureCallout = pWin32Callouts->DesktopDeleteProcedure;
  3193. ExWindowStationOkToCloseProcedureCallout = pWin32Callouts->WindowStationOkToCloseProcedure;
  3194. ExWindowStationCloseProcedureCallout = pWin32Callouts->WindowStationCloseProcedure;
  3195. ExWindowStationDeleteProcedureCallout = pWin32Callouts->WindowStationDeleteProcedure;
  3196. ExWindowStationParseProcedureCallout = pWin32Callouts->WindowStationParseProcedure;
  3197. ExWindowStationOpenProcedureCallout = pWin32Callouts->WindowStationOpenProcedure;
  3198. }
  3199. VOID
  3200. PsSetProcessPriorityByClass(
  3201. IN PEPROCESS Process,
  3202. IN PSPROCESSPRIORITYMODE PriorityMode
  3203. )
  3204. {
  3205. KPRIORITY BasePriority;
  3206. UCHAR MemoryPriority;
  3207. ULONG QuantumIndex;
  3208. PEJOB Job;
  3209. PAGED_CODE();
  3210. BasePriority = PspPriorityTable[Process->PriorityClass];
  3211. if (PriorityMode == PsProcessPriorityForeground ) {
  3212. QuantumIndex = PsPrioritySeperation;
  3213. MemoryPriority = MEMORY_PRIORITY_FOREGROUND;
  3214. } else {
  3215. QuantumIndex = 0;
  3216. MemoryPriority = MEMORY_PRIORITY_BACKGROUND;
  3217. }
  3218. if (Process->PriorityClass != PROCESS_PRIORITY_CLASS_IDLE) {
  3219. Job = Process->Job;
  3220. if (Job != NULL && PspUseJobSchedulingClasses ) {
  3221. Process->Pcb.ThreadQuantum = PspJobSchedulingClasses[Job->SchedulingClass];
  3222. } else {
  3223. Process->Pcb.ThreadQuantum = PspForegroundQuantum[QuantumIndex];
  3224. }
  3225. } else {
  3226. Process->Pcb.ThreadQuantum = THREAD_QUANTUM;
  3227. }
  3228. KeSetPriorityProcess (&Process->Pcb,BasePriority);
  3229. if (PriorityMode != PsProcessPrioritySpinning ) {
  3230. MmSetMemoryPriorityProcess(Process, MemoryPriority);
  3231. }
  3232. }
  3233. #if defined(_X86_)
  3234. #pragma optimize ("y",off)
  3235. #endif
  3236. NTSTATUS
  3237. PsConvertToGuiThread(
  3238. VOID
  3239. )
  3240. /*++
  3241. Routine Description:
  3242. This function converts a thread to a GUI thread. This involves giving the
  3243. thread a larger variable sized stack, and allocating appropriate w32
  3244. thread and process objects.
  3245. Arguments:
  3246. None.
  3247. Environment:
  3248. On x86 this function needs to build an EBP frame. The function
  3249. KeSwitchKernelStack depends on this fact. The '#pragma optimize
  3250. ("y",off)' below disables frame pointer omission for all builds.
  3251. Return Value:
  3252. TBD
  3253. --*/
  3254. {
  3255. PVOID NewStack;
  3256. PVOID OldStack;
  3257. PETHREAD Thread;
  3258. PEPROCESS Process;
  3259. NTSTATUS Status;
  3260. PKNODE Node;
  3261. PAGED_CODE();
  3262. Thread = PsGetCurrentThread();
  3263. if (KeGetPreviousModeByThread(&Thread->Tcb) == KernelMode) {
  3264. return STATUS_INVALID_PARAMETER;
  3265. }
  3266. if (!PspW32ProcessCallout) {
  3267. return STATUS_ACCESS_DENIED;
  3268. }
  3269. //
  3270. // If the thread is using the shadow service table, then an attempt is
  3271. // being made to convert a thread that has already been converted, or
  3272. // a limit violation has occured on the Win32k system service table.
  3273. //
  3274. if (Thread->Tcb.ServiceTable != (PVOID)&KeServiceDescriptorTable[0]) {
  3275. return STATUS_ALREADY_WIN32;
  3276. }
  3277. Process = PsGetCurrentProcessByThread (Thread);
  3278. //
  3279. // Get a larger kernel stack if we haven't already.
  3280. //
  3281. if (!Thread->Tcb.LargeStack) {
  3282. Node = KiProcessorBlock[Thread->Tcb.IdealProcessor]->ParentNode;
  3283. NewStack = MmCreateKernelStack(TRUE,
  3284. Node->NodeNumber);
  3285. if ( !NewStack ) {
  3286. try {
  3287. NtCurrentTeb()->LastErrorValue = (LONG)ERROR_NOT_ENOUGH_MEMORY;
  3288. } except (EXCEPTION_EXECUTE_HANDLER) {
  3289. }
  3290. return STATUS_NO_MEMORY;
  3291. }
  3292. //
  3293. // Switching kernel stacks will copy the base trap frame. This needs
  3294. // to be protected from context changes by disabline kernel APC's.
  3295. //
  3296. KeEnterGuardedRegionThread (&Thread->Tcb);
  3297. #if defined(_IA64_)
  3298. OldStack = KeSwitchKernelStack(NewStack,
  3299. (UCHAR *)NewStack - KERNEL_LARGE_STACK_COMMIT,
  3300. (UCHAR *)NewStack + KERNEL_LARGE_BSTORE_COMMIT);
  3301. #else
  3302. OldStack = KeSwitchKernelStack(NewStack,
  3303. (UCHAR *)NewStack - KERNEL_LARGE_STACK_COMMIT);
  3304. #endif // defined(_IA64_)
  3305. KeLeaveGuardedRegionThread (&Thread->Tcb);
  3306. MmDeleteKernelStack(OldStack, FALSE);
  3307. }
  3308. PERFINFO_CONVERT_TO_GUI_THREAD(Thread);
  3309. //
  3310. // We are all clean on the stack, now call out and then link the Win32 structures
  3311. // to the base exec structures
  3312. //
  3313. Status = (PspW32ProcessCallout) (Process, TRUE);
  3314. if (!NT_SUCCESS (Status)) {
  3315. return Status;
  3316. }
  3317. //
  3318. // Switch the thread to use the shadow system serive table which will
  3319. // enable it to execute Win32k services.
  3320. //
  3321. Thread->Tcb.ServiceTable = (PVOID)&KeServiceDescriptorTableShadow[0];
  3322. ASSERT (Thread->Tcb.Win32Thread == 0);
  3323. //
  3324. // Make the thread callout.
  3325. //
  3326. Status = (PspW32ThreadCallout)(Thread,PsW32ThreadCalloutInitialize);
  3327. if (!NT_SUCCESS (Status)) {
  3328. Thread->Tcb.ServiceTable = (PVOID)&KeServiceDescriptorTable[0];
  3329. }
  3330. return Status;
  3331. }
  3332. #if defined(_X86_)
  3333. #pragma optimize ("y",on)
  3334. #endif
  3335. #ifdef ALLOC_DATA_PRAGMA
  3336. #pragma const_seg()
  3337. #endif