Source code of Windows XP (NT5)
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.

519 lines
14 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. obinsert.c
  5. Abstract:
  6. Object instantiation API
  7. Author:
  8. Steve Wood (stevewo) 31-Mar-1989
  9. Revision History:
  10. --*/
  11. #include "obp.h"
  12. #ifdef ALLOC_PRAGMA
  13. #pragma alloc_text(PAGE,ObInsertObject)
  14. #endif
  15. NTSTATUS
  16. ObInsertObject (
  17. IN PVOID Object,
  18. IN PACCESS_STATE AccessState OPTIONAL,
  19. IN ACCESS_MASK DesiredAccess OPTIONAL,
  20. IN ULONG ObjectPointerBias,
  21. OUT PVOID *NewObject OPTIONAL,
  22. OUT PHANDLE Handle OPTIONAL
  23. )
  24. /*++
  25. Routine Description:
  26. This routine inserts an object into the current processes handle table.
  27. The Object header includes a pointer to a SecurityDescriptor passed in
  28. an object creation call. This SecurityDescriptor is not assumed to have
  29. been captured. This routine is responsible for making an appropriate
  30. SecurityDescriptor and removing the reference in the object header.
  31. Arguments:
  32. Object - Supplies a pointer to the new object body
  33. AccessState - Optionally supplies the access state for the new
  34. handle
  35. DesiredAccess - Optionally supplies the desired access we want for the
  36. new handle
  37. ObjectPointerBias - Supplies a bias to apply for the pointer count for the
  38. object
  39. NewObject - Optionally receives the pointer to the new object that we've
  40. created a handle for
  41. Handle - Receives the new handle, If NULL then no handle is created.
  42. Objects that don't have handles created must be unnamed and
  43. have an object bias of zero.
  44. Return Value:
  45. An appropriate NTSTATUS value.
  46. --*/
  47. {
  48. POBJECT_CREATE_INFORMATION ObjectCreateInfo;
  49. POBJECT_HEADER ObjectHeader;
  50. PUNICODE_STRING ObjectName;
  51. POBJECT_TYPE ObjectType;
  52. POBJECT_HEADER_NAME_INFO NameInfo;
  53. PSECURITY_DESCRIPTOR ParentDescriptor = NULL;
  54. PVOID InsertObject;
  55. HANDLE NewHandle;
  56. OB_OPEN_REASON OpenReason;
  57. NTSTATUS Status = STATUS_SUCCESS;
  58. ACCESS_STATE LocalAccessState;
  59. AUX_ACCESS_DATA AuxData;
  60. BOOLEAN SecurityDescriptorAllocated;
  61. KPROCESSOR_MODE PreviousMode;
  62. NTSTATUS ReturnStatus;
  63. PVOID DirObject = NULL;
  64. OBP_LOOKUP_CONTEXT LookupContext;
  65. PAGED_CODE();
  66. ObpValidateIrql("ObInsertObject");
  67. //
  68. // Get the address of the object header, the object create information,
  69. // the object type, and the address of the object name descriptor, if
  70. // specified.
  71. //
  72. ObjectHeader = OBJECT_TO_OBJECT_HEADER(Object);
  73. #if DBG
  74. if ((ObjectHeader->Flags & OB_FLAG_NEW_OBJECT) == 0) {
  75. KdPrint(("OB: Attempting to insert existing object %08x\n", Object));
  76. KdBreakPoint();
  77. ObDereferenceObject(Object);
  78. return STATUS_INVALID_PARAMETER;
  79. }
  80. #endif
  81. ObjectCreateInfo = ObjectHeader->ObjectCreateInfo;
  82. ObjectType = ObjectHeader->Type;
  83. NameInfo = ObpReferenceNameInfo( ObjectHeader );
  84. ObjectName = NULL;
  85. if ((NameInfo != NULL) && (NameInfo->Name.Buffer != NULL)) {
  86. ObjectName = &NameInfo->Name;
  87. }
  88. ASSERT (ARGUMENT_PRESENT (Handle) || (ObjectPointerBias == 0 && ObjectName == NULL &&
  89. ObjectType->TypeInfo.SecurityRequired && NewObject == NULL));
  90. //
  91. // If security checks are not required and an object name is not
  92. // specified, insert an unnamed object, biasing the count
  93. // by one, dereference the bias, and return to our caller
  94. //
  95. PreviousMode = KeGetPreviousMode();
  96. if (!ObjectType->TypeInfo.SecurityRequired && (ObjectName == NULL)) {
  97. ObjectHeader->ObjectCreateInfo = NULL;
  98. *Handle = NULL;
  99. Status = ObpCreateUnnamedHandle( Object,
  100. DesiredAccess,
  101. 1 + ObjectPointerBias,
  102. ObjectCreateInfo->Attributes,
  103. PreviousMode,
  104. NewObject,
  105. Handle );
  106. //
  107. // Free the object creation information and dereference the object.
  108. //
  109. ObpFreeObjectCreateInformation(ObjectCreateInfo);
  110. ObpDereferenceNameInfo( NameInfo );
  111. ObDereferenceObject(Object);
  112. return Status;
  113. }
  114. //
  115. // The object is either named or requires full security checks. If the
  116. // caller hasn't specified an access state then dummy up a local one
  117. // using the requested desired access
  118. //
  119. if (!ARGUMENT_PRESENT(AccessState)) {
  120. AccessState = &LocalAccessState;
  121. Status = SeCreateAccessState( &LocalAccessState,
  122. &AuxData,
  123. DesiredAccess,
  124. &ObjectType->TypeInfo.GenericMapping );
  125. if (!NT_SUCCESS(Status)) {
  126. ObpDereferenceNameInfo( NameInfo );
  127. ObDereferenceObject(Object);
  128. return Status;
  129. }
  130. }
  131. AccessState->SecurityDescriptor = ObjectCreateInfo->SecurityDescriptor;
  132. //
  133. // Check the desired access mask against the security descriptor
  134. //
  135. Status = ObpValidateAccessMask( AccessState );
  136. if (!NT_SUCCESS( Status )) {
  137. ObpDereferenceNameInfo( NameInfo );
  138. ObDereferenceObject(Object);
  139. return( Status );
  140. }
  141. //
  142. // Set some local state variables
  143. //
  144. ObpInitializeLookupContext(&LookupContext);
  145. InsertObject = Object;
  146. OpenReason = ObCreateHandle;
  147. //
  148. // Check if we have an object name. If so then
  149. // lookup the name
  150. //
  151. if (ObjectName != NULL) {
  152. Status = ObpLookupObjectName( ObjectCreateInfo->RootDirectory,
  153. ObjectName,
  154. ObjectCreateInfo->Attributes,
  155. ObjectType,
  156. (KPROCESSOR_MODE)(ObjectHeader->Flags & OB_FLAG_KERNEL_OBJECT
  157. ? KernelMode : UserMode),
  158. ObjectCreateInfo->ParseContext,
  159. ObjectCreateInfo->SecurityQos,
  160. Object,
  161. AccessState,
  162. &LookupContext,
  163. &InsertObject );
  164. //
  165. // We found the name and it is not the object we have as our input.
  166. // So we cannot insert the object again so we'll return an
  167. // appropriate status
  168. //
  169. if (NT_SUCCESS(Status) &&
  170. (InsertObject != NULL) &&
  171. (InsertObject != Object)) {
  172. OpenReason = ObOpenHandle;
  173. if (ObjectCreateInfo->Attributes & OBJ_OPENIF) {
  174. if (ObjectType != OBJECT_TO_OBJECT_HEADER(InsertObject)->Type) {
  175. Status = STATUS_OBJECT_TYPE_MISMATCH;
  176. } else {
  177. Status = STATUS_OBJECT_NAME_EXISTS; // Warning only
  178. }
  179. } else {
  180. Status = STATUS_OBJECT_NAME_COLLISION;
  181. }
  182. }
  183. //
  184. // We did not find the name so we'll cleanup after ourselves
  185. // and return to our caller
  186. //
  187. if (!NT_SUCCESS( Status )) {
  188. ObpReleaseLookupContext( &LookupContext );
  189. ObpDereferenceNameInfo( NameInfo );
  190. ObDereferenceObject( Object );
  191. //
  192. // Free security information if we allocated it
  193. //
  194. if (AccessState == &LocalAccessState) {
  195. SeDeleteAccessState( AccessState );
  196. }
  197. return( Status );
  198. } else {
  199. //
  200. // Otherwise we did locate the object name
  201. //
  202. // If we just created a named symbolic link then call out to
  203. // handle any Dos Device name semanatics.
  204. //
  205. if (ObjectType == ObpSymbolicLinkObjectType) {
  206. ObpCreateSymbolicLinkName( (POBJECT_SYMBOLIC_LINK)InsertObject );
  207. }
  208. }
  209. }
  210. //
  211. // If we are creating a new object, then we need assign security
  212. // to it. A pointer to the captured caller-proposed security
  213. // descriptor is contained in the AccessState structure. The
  214. // SecurityDescriptor field in the object header must point to
  215. // the final security descriptor, or to NULL if no security is
  216. // to be assigned to the object.
  217. //
  218. if (InsertObject == Object) {
  219. //
  220. // Only the following objects have security descriptors:
  221. //
  222. // - Named Objects
  223. // - Unnamed objects whose object-type information explicitly
  224. // indicates a security descriptor is required.
  225. //
  226. if ((ObjectName != NULL) || ObjectType->TypeInfo.SecurityRequired) {
  227. //
  228. // Get the parent's descriptor, if there is one...
  229. //
  230. if ((NameInfo != NULL) && (NameInfo->Directory != NULL)) {
  231. //
  232. // This will allocate a block of memory and copy
  233. // the parent's security descriptor into it, and
  234. // return the pointer to the block.
  235. //
  236. // Call ObReleaseObjectSecurity to free up this
  237. // memory.
  238. //
  239. ObGetObjectSecurity( NameInfo->Directory,
  240. &ParentDescriptor,
  241. &SecurityDescriptorAllocated );
  242. }
  243. //
  244. // Take the captured security descriptor in the AccessState,
  245. // put it into the proper format, and call the object's
  246. // security method to assign the new security descriptor to
  247. // the new object.
  248. //
  249. Status = ObAssignSecurity( AccessState,
  250. ParentDescriptor,
  251. Object,
  252. ObjectType );
  253. if (ParentDescriptor != NULL) {
  254. ObReleaseObjectSecurity( ParentDescriptor,
  255. SecurityDescriptorAllocated );
  256. } else if (NT_SUCCESS( Status )) {
  257. SeReleaseSecurityDescriptor( ObjectCreateInfo->SecurityDescriptor,
  258. ObjectCreateInfo->ProbeMode,
  259. TRUE );
  260. ObjectCreateInfo->SecurityDescriptor = NULL;
  261. AccessState->SecurityDescriptor = NULL;
  262. }
  263. }
  264. if (!NT_SUCCESS( Status )) {
  265. //
  266. // The attempt to assign the security descriptor to
  267. // the object failed.
  268. //
  269. if (LookupContext.DirectoryLocked) {
  270. //
  271. // If ObpLookupObjectName already inserted the
  272. // object into the directory we have to backup this
  273. //
  274. //
  275. // Capture the object Directory
  276. //
  277. DirObject = NameInfo->Directory;
  278. ObpDeleteDirectoryEntry( &LookupContext );
  279. }
  280. ObpReleaseLookupContext( &LookupContext );
  281. //
  282. // If ObpLookupObjectName inserted the object into the directory
  283. // it added a reference to the object and to its directory
  284. // object. We should remove the extra-references
  285. //
  286. if (DirObject) {
  287. ObDereferenceObject( Object );
  288. ObDereferenceObject( DirObject );
  289. }
  290. //
  291. // The first backout logic used ObpDeleteNameCheck
  292. // which is wrong because the security descriptor for
  293. // the object is not initialized. Actually ObpDeleteNameCheck
  294. // had no effect because the object was removed before from
  295. // the directory
  296. //
  297. ObpDereferenceNameInfo( NameInfo );
  298. ObDereferenceObject( Object );
  299. //
  300. // Free security information if we allocated it
  301. //
  302. if (AccessState == &LocalAccessState) {
  303. SeDeleteAccessState( AccessState );
  304. }
  305. return( Status );
  306. }
  307. }
  308. ReturnStatus = Status;
  309. ObjectHeader->ObjectCreateInfo = NULL;
  310. //
  311. // Create a named handle for the object with a pointer bias
  312. // This call also will unlock the directory lock is necessary
  313. // on return
  314. //
  315. if (ARGUMENT_PRESENT (Handle)) {
  316. Status = ObpCreateHandle( OpenReason,
  317. InsertObject,
  318. NULL,
  319. AccessState,
  320. 1 + ObjectPointerBias,
  321. ObjectCreateInfo->Attributes,
  322. &LookupContext,
  323. PreviousMode,
  324. NewObject,
  325. &NewHandle );
  326. //
  327. // If the insertion failed, the following dereference will cause
  328. // the newly created object to be deallocated.
  329. //
  330. if (!NT_SUCCESS( Status )) {
  331. //
  332. // Make the name reference go away if an error.
  333. //
  334. if (ObjectName != NULL) {
  335. ObpDeleteNameCheck( Object );
  336. }
  337. *Handle = NULL;
  338. ReturnStatus = Status;
  339. } else {
  340. *Handle = NewHandle;
  341. }
  342. ObpDereferenceNameInfo( NameInfo );
  343. ObDereferenceObject( Object );
  344. } else {
  345. BOOLEAN IsNewObject;
  346. //
  347. // Charge the user quota for the object.
  348. //
  349. ObpLockObject( ObjectHeader );
  350. ReturnStatus = ObpChargeQuotaForObject( ObjectHeader, ObjectType, &IsNewObject );
  351. ObpUnlockObject( ObjectHeader );
  352. if (!NT_SUCCESS (ReturnStatus)) {
  353. ObDereferenceObject( Object );
  354. }
  355. }
  356. ObpFreeObjectCreateInformation( ObjectCreateInfo );
  357. //
  358. // Free security information if we allocated it
  359. //
  360. if (AccessState == &LocalAccessState) {
  361. SeDeleteAccessState( AccessState );
  362. }
  363. return( ReturnStatus );
  364. }