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.

492 lines
13 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. psopen.c
  5. Abstract:
  6. This module implements Process and Thread open.
  7. This module also contains NtRegisterThreadTerminationPort.
  8. Author:
  9. Mark Lucovsky (markl) 20-Sep-1989
  10. Revision History:
  11. --*/
  12. #include "psp.h"
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text(PAGE, NtOpenProcess)
  15. #pragma alloc_text(PAGE, NtOpenThread)
  16. #endif
  17. NTSTATUS
  18. NtOpenProcess (
  19. OUT PHANDLE ProcessHandle,
  20. IN ACCESS_MASK DesiredAccess,
  21. IN POBJECT_ATTRIBUTES ObjectAttributes,
  22. IN PCLIENT_ID ClientId OPTIONAL
  23. )
  24. /*++
  25. Routine Description:
  26. This function opens a handle to a process object with the specified
  27. desired access.
  28. The object is located either by name, or by locating a thread whose
  29. Client ID matches the specified Client ID and then opening that thread's
  30. process.
  31. Arguments:
  32. ProcessHandle - Supplies a pointer to a variable that will receive
  33. the process object handle.
  34. DesiredAccess - Supplies the desired types of access for the process
  35. object.
  36. ObjectAttributes - Supplies a pointer to an object attributes structure.
  37. If the ObjectName field is specified, then ClientId must not be
  38. specified.
  39. ClientId - Supplies a pointer to a ClientId that if supplied
  40. specifies the thread whose process is to be opened. If this
  41. argument is specified, then ObjectName field of the ObjectAttributes
  42. structure must not be specified.
  43. Return Value:
  44. NTSTATUS - Status of call
  45. --*/
  46. {
  47. HANDLE Handle;
  48. KPROCESSOR_MODE PreviousMode;
  49. NTSTATUS Status;
  50. PEPROCESS Process;
  51. PETHREAD Thread;
  52. CLIENT_ID CapturedCid={0};
  53. BOOLEAN ObjectNamePresent;
  54. BOOLEAN ClientIdPresent;
  55. ACCESS_STATE AccessState;
  56. AUX_ACCESS_DATA AuxData;
  57. ULONG Attributes;
  58. PAGED_CODE();
  59. //
  60. // Make sure that only one of either ClientId or ObjectName is
  61. // present.
  62. //
  63. PreviousMode = KeGetPreviousMode();
  64. if (PreviousMode != KernelMode) {
  65. //
  66. // Since we need to look at the ObjectName field, probe
  67. // ObjectAttributes and capture object name present indicator.
  68. //
  69. try {
  70. ProbeForWriteHandle (ProcessHandle);
  71. ProbeForReadSmallStructure (ObjectAttributes,
  72. sizeof(OBJECT_ATTRIBUTES),
  73. sizeof(ULONG));
  74. ObjectNamePresent = (BOOLEAN)ARGUMENT_PRESENT (ObjectAttributes->ObjectName);
  75. Attributes = ObSanitizeHandleAttributes (ObjectAttributes->Attributes, UserMode);
  76. if (ARGUMENT_PRESENT (ClientId)) {
  77. ProbeForReadSmallStructure (ClientId, sizeof (CLIENT_ID), sizeof (ULONG));
  78. CapturedCid = *ClientId;
  79. ClientIdPresent = TRUE;
  80. } else {
  81. ClientIdPresent = FALSE;
  82. }
  83. } except (EXCEPTION_EXECUTE_HANDLER) {
  84. return GetExceptionCode();
  85. }
  86. } else {
  87. ObjectNamePresent = (BOOLEAN)ARGUMENT_PRESENT (ObjectAttributes->ObjectName);
  88. Attributes = ObSanitizeHandleAttributes (ObjectAttributes->Attributes, KernelMode);
  89. if (ARGUMENT_PRESENT (ClientId)) {
  90. CapturedCid = *ClientId;
  91. ClientIdPresent = TRUE;
  92. } else {
  93. ClientIdPresent = FALSE;
  94. }
  95. }
  96. if (ObjectNamePresent && ClientIdPresent) {
  97. return STATUS_INVALID_PARAMETER_MIX;
  98. }
  99. //
  100. // Create an AccessState here, because the caller may have
  101. // DebugPrivilege, which requires us to make special adjustments
  102. // to his desired access mask. We do this by modifying the
  103. // internal fields in the AccessState to achieve the effect
  104. // we desire.
  105. //
  106. Status = SeCreateAccessState(
  107. &AccessState,
  108. &AuxData,
  109. DesiredAccess,
  110. &PsProcessType->TypeInfo.GenericMapping
  111. );
  112. if ( !NT_SUCCESS(Status) ) {
  113. return Status;
  114. }
  115. //
  116. // Check here to see if the caller has SeDebugPrivilege. If
  117. // he does, we will allow him any access he wants to the process.
  118. // We do this by clearing the DesiredAccess in the AccessState
  119. // and recording what we want him to have in the PreviouslyGrantedAccess
  120. // field.
  121. //
  122. // Note that this routine performs auditing as appropriate.
  123. //
  124. if (SeSinglePrivilegeCheck( SeDebugPrivilege, PreviousMode )) {
  125. if ( AccessState.RemainingDesiredAccess & MAXIMUM_ALLOWED ) {
  126. AccessState.PreviouslyGrantedAccess |= PROCESS_ALL_ACCESS;
  127. } else {
  128. AccessState.PreviouslyGrantedAccess |= ( AccessState.RemainingDesiredAccess );
  129. }
  130. AccessState.RemainingDesiredAccess = 0;
  131. }
  132. if (ObjectNamePresent) {
  133. //
  134. // Open handle to the process object with the specified desired access,
  135. // set process handle value, and return service completion status.
  136. //
  137. Status = ObOpenObjectByName(
  138. ObjectAttributes,
  139. PsProcessType,
  140. PreviousMode,
  141. &AccessState,
  142. 0,
  143. NULL,
  144. &Handle
  145. );
  146. SeDeleteAccessState( &AccessState );
  147. if ( NT_SUCCESS(Status) ) {
  148. try {
  149. *ProcessHandle = Handle;
  150. } except (EXCEPTION_EXECUTE_HANDLER) {
  151. return GetExceptionCode ();
  152. }
  153. }
  154. return Status;
  155. }
  156. if ( ClientIdPresent ) {
  157. Thread = NULL;
  158. if (CapturedCid.UniqueThread) {
  159. Status = PsLookupProcessThreadByCid(
  160. &CapturedCid,
  161. &Process,
  162. &Thread
  163. );
  164. if (!NT_SUCCESS(Status)) {
  165. SeDeleteAccessState( &AccessState );
  166. return Status;
  167. }
  168. } else {
  169. Status = PsLookupProcessByProcessId(
  170. CapturedCid.UniqueProcess,
  171. &Process
  172. );
  173. if ( !NT_SUCCESS(Status) ) {
  174. SeDeleteAccessState( &AccessState );
  175. return Status;
  176. }
  177. }
  178. //
  179. // OpenObjectByAddress
  180. //
  181. Status = ObOpenObjectByPointer(
  182. Process,
  183. Attributes,
  184. &AccessState,
  185. 0,
  186. PsProcessType,
  187. PreviousMode,
  188. &Handle
  189. );
  190. SeDeleteAccessState( &AccessState );
  191. if (Thread) {
  192. ObDereferenceObject(Thread);
  193. }
  194. ObDereferenceObject(Process);
  195. if (NT_SUCCESS (Status)) {
  196. try {
  197. *ProcessHandle = Handle;
  198. } except (EXCEPTION_EXECUTE_HANDLER) {
  199. return GetExceptionCode ();
  200. }
  201. }
  202. return Status;
  203. }
  204. return STATUS_INVALID_PARAMETER_MIX;
  205. }
  206. NTSTATUS
  207. NtOpenThread (
  208. OUT PHANDLE ThreadHandle,
  209. IN ACCESS_MASK DesiredAccess,
  210. IN POBJECT_ATTRIBUTES ObjectAttributes,
  211. IN PCLIENT_ID ClientId OPTIONAL
  212. )
  213. /*++
  214. Routine Description:
  215. This function opens a handle to a thread object with the specified
  216. desired access.
  217. The object is located either by name, or by locating a thread whose
  218. Client ID matches the specified Client ID.
  219. Arguments:
  220. ThreadHandle - Supplies a pointer to a variable that will receive
  221. the thread object handle.
  222. DesiredAccess - Supplies the desired types of access for the Thread
  223. object.
  224. ObjectAttributes - Supplies a pointer to an object attributes structure.
  225. If the ObjectName field is specified, then ClientId must not be
  226. specified.
  227. ClientId - Supplies a pointer to a ClientId that if supplied
  228. specifies the thread whose thread is to be opened. If this
  229. argument is specified, then ObjectName field of the ObjectAttributes
  230. structure must not be specified.
  231. Return Value:
  232. TBS
  233. --*/
  234. {
  235. HANDLE Handle;
  236. KPROCESSOR_MODE PreviousMode;
  237. NTSTATUS Status;
  238. PETHREAD Thread;
  239. CLIENT_ID CapturedCid={0};
  240. BOOLEAN ObjectNamePresent;
  241. BOOLEAN ClientIdPresent;
  242. ACCESS_STATE AccessState;
  243. AUX_ACCESS_DATA AuxData;
  244. ULONG HandleAttributes;
  245. PAGED_CODE();
  246. //
  247. // Make sure that only one of either ClientId or ObjectName is
  248. // present.
  249. //
  250. PreviousMode = KeGetPreviousMode();
  251. if (PreviousMode != KernelMode) {
  252. //
  253. // Since we need to look at the ObjectName field, probe
  254. // ObjectAttributes and capture object name present indicator.
  255. //
  256. try {
  257. ProbeForWriteHandle(ThreadHandle);
  258. ProbeForReadSmallStructure (ObjectAttributes,
  259. sizeof(OBJECT_ATTRIBUTES),
  260. sizeof(ULONG));
  261. ObjectNamePresent = (BOOLEAN)ARGUMENT_PRESENT(ObjectAttributes->ObjectName);
  262. HandleAttributes = ObSanitizeHandleAttributes (ObjectAttributes->Attributes, UserMode);
  263. if (ARGUMENT_PRESENT(ClientId)) {
  264. ProbeForReadSmallStructure (ClientId, sizeof(CLIENT_ID), sizeof(ULONG));
  265. CapturedCid = *ClientId;
  266. ClientIdPresent = TRUE;
  267. } else {
  268. ClientIdPresent = FALSE;
  269. }
  270. } except (EXCEPTION_EXECUTE_HANDLER) {
  271. return GetExceptionCode();
  272. }
  273. } else {
  274. ObjectNamePresent = (BOOLEAN) ARGUMENT_PRESENT(ObjectAttributes->ObjectName);
  275. HandleAttributes = ObSanitizeHandleAttributes (ObjectAttributes->Attributes, KernelMode);
  276. if (ARGUMENT_PRESENT(ClientId)) {
  277. CapturedCid = *ClientId;
  278. ClientIdPresent = TRUE;
  279. } else {
  280. ClientIdPresent = FALSE;
  281. }
  282. }
  283. if (ObjectNamePresent && ClientIdPresent) {
  284. return STATUS_INVALID_PARAMETER_MIX;
  285. }
  286. Status = SeCreateAccessState(
  287. &AccessState,
  288. &AuxData,
  289. DesiredAccess,
  290. &PsProcessType->TypeInfo.GenericMapping
  291. );
  292. if (!NT_SUCCESS (Status)) {
  293. return Status;
  294. }
  295. //
  296. // Check here to see if the caller has SeDebugPrivilege. If
  297. // he does, we will allow him any access he wants to the process.
  298. // We do this by clearing the DesiredAccess in the AccessState
  299. // and recording what we want him to have in the PreviouslyGrantedAccess
  300. // field.
  301. if (SeSinglePrivilegeCheck( SeDebugPrivilege, PreviousMode )) {
  302. if ( AccessState.RemainingDesiredAccess & MAXIMUM_ALLOWED ) {
  303. AccessState.PreviouslyGrantedAccess |= THREAD_ALL_ACCESS;
  304. } else {
  305. AccessState.PreviouslyGrantedAccess |= ( AccessState.RemainingDesiredAccess );
  306. }
  307. AccessState.RemainingDesiredAccess = 0;
  308. }
  309. if ( ObjectNamePresent ) {
  310. //
  311. // Open handle to the Thread object with the specified desired access,
  312. // set Thread handle value, and return service completion status.
  313. //
  314. Status = ObOpenObjectByName(
  315. ObjectAttributes,
  316. PsThreadType,
  317. PreviousMode,
  318. &AccessState,
  319. 0,
  320. NULL,
  321. &Handle
  322. );
  323. SeDeleteAccessState( &AccessState );
  324. if ( NT_SUCCESS(Status) ) {
  325. try {
  326. *ThreadHandle = Handle;
  327. } except(EXCEPTION_EXECUTE_HANDLER) {
  328. return GetExceptionCode ();
  329. }
  330. }
  331. return Status;
  332. }
  333. if ( ClientIdPresent ) {
  334. if ( CapturedCid.UniqueProcess ) {
  335. Status = PsLookupProcessThreadByCid(
  336. &CapturedCid,
  337. NULL,
  338. &Thread
  339. );
  340. if ( !NT_SUCCESS(Status) ) {
  341. SeDeleteAccessState( &AccessState );
  342. return Status;
  343. }
  344. } else {
  345. Status = PsLookupThreadByThreadId(
  346. CapturedCid.UniqueThread,
  347. &Thread
  348. );
  349. if ( !NT_SUCCESS(Status) ) {
  350. SeDeleteAccessState( &AccessState );
  351. return Status;
  352. }
  353. }
  354. Status = ObOpenObjectByPointer(
  355. Thread,
  356. HandleAttributes,
  357. &AccessState,
  358. 0,
  359. PsThreadType,
  360. PreviousMode,
  361. &Handle
  362. );
  363. SeDeleteAccessState( &AccessState );
  364. ObDereferenceObject(Thread);
  365. if ( NT_SUCCESS(Status) ) {
  366. try {
  367. *ThreadHandle = Handle;
  368. } except (EXCEPTION_EXECUTE_HANDLER) {
  369. return GetExceptionCode ();
  370. }
  371. }
  372. return Status;
  373. }
  374. return STATUS_INVALID_PARAMETER_MIX;
  375. }