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.

1699 lines
47 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. oblink.c
  5. Abstract:
  6. Symbolic Link Object routines
  7. Author:
  8. Steve Wood (stevewo) 3-Aug-1989
  9. Revision History:
  10. --*/
  11. #include "obp.h"
  12. VOID
  13. ObpProcessDosDeviceSymbolicLink (
  14. POBJECT_SYMBOLIC_LINK SymbolicLink,
  15. ULONG Action
  16. );
  17. #if defined(ALLOC_PRAGMA)
  18. #pragma alloc_text(PAGE,NtCreateSymbolicLinkObject)
  19. #pragma alloc_text(PAGE,NtOpenSymbolicLinkObject)
  20. #pragma alloc_text(PAGE,NtQuerySymbolicLinkObject)
  21. #pragma alloc_text(PAGE,ObpParseSymbolicLink)
  22. #pragma alloc_text(PAGE,ObpDeleteSymbolicLink)
  23. #pragma alloc_text(PAGE,ObpDeleteSymbolicLinkName)
  24. #pragma alloc_text(PAGE,ObpCreateSymbolicLinkName)
  25. #pragma alloc_text(PAGE,ObpProcessDosDeviceSymbolicLink)
  26. #endif
  27. //
  28. // This is the object type for device objects.
  29. //
  30. extern POBJECT_TYPE IoDeviceObjectType;
  31. //
  32. // Global that enables/disables LUID device maps
  33. //
  34. extern ULONG ObpLUIDDeviceMapsEnabled;
  35. //
  36. // Local procedure prototypes
  37. //
  38. #define CREATE_SYMBOLIC_LINK 0
  39. #define DELETE_SYMBOLIC_LINK 1
  40. NTSTATUS
  41. NtCreateSymbolicLinkObject (
  42. OUT PHANDLE LinkHandle,
  43. IN ACCESS_MASK DesiredAccess,
  44. IN POBJECT_ATTRIBUTES ObjectAttributes,
  45. IN PUNICODE_STRING LinkTarget
  46. )
  47. /*++
  48. Routine Description:
  49. This function creates a symbolic link object, sets it initial value to
  50. value specified in the LinkTarget parameter, and opens a handle to the
  51. object with the specified desired access.
  52. Arguments:
  53. LinkHandle - Supplies a pointer to a variable that will receive the
  54. symbolic link object handle.
  55. DesiredAccess - Supplies the desired types of access for the symbolic link
  56. object.
  57. ObjectAttributes - Supplies a pointer to an object attributes structure.
  58. LinkTarget - Supplies the target name for the symbolic link object.
  59. Return Value:
  60. An appropriate status value
  61. --*/
  62. {
  63. KPROCESSOR_MODE PreviousMode;
  64. NTSTATUS Status;
  65. POBJECT_SYMBOLIC_LINK SymbolicLink;
  66. PVOID Object;
  67. HANDLE Handle;
  68. UNICODE_STRING CapturedLinkTarget;
  69. PAGED_CODE();
  70. //
  71. // Get previous processor mode and probe output arguments if necessary.
  72. //
  73. PreviousMode = KeGetPreviousMode();
  74. if (PreviousMode != KernelMode) {
  75. try {
  76. ProbeForReadSmallStructure( ObjectAttributes,
  77. sizeof( OBJECT_ATTRIBUTES ),
  78. sizeof( ULONG ));
  79. ProbeForReadSmallStructure( LinkTarget,
  80. sizeof( *LinkTarget ),
  81. sizeof( UCHAR ));
  82. CapturedLinkTarget = *LinkTarget;
  83. ProbeForRead( CapturedLinkTarget.Buffer,
  84. CapturedLinkTarget.MaximumLength,
  85. sizeof( UCHAR ));
  86. ProbeForWriteHandle( LinkHandle );
  87. } except( EXCEPTION_EXECUTE_HANDLER ) {
  88. return( GetExceptionCode() );
  89. }
  90. } else {
  91. CapturedLinkTarget = *LinkTarget;
  92. }
  93. //
  94. // Check if there is an odd MaximumLength
  95. //
  96. if (CapturedLinkTarget.MaximumLength % sizeof( WCHAR )) {
  97. //
  98. // Round down the MaximumLength to a valid even size
  99. //
  100. CapturedLinkTarget.MaximumLength = (CapturedLinkTarget.MaximumLength / sizeof( WCHAR )) * sizeof( WCHAR );
  101. }
  102. //
  103. // Error if link target name length is odd, the length is greater than
  104. // the maximum length, or zero and creating.
  105. //
  106. if ((CapturedLinkTarget.MaximumLength == 0) ||
  107. (CapturedLinkTarget.Length > CapturedLinkTarget.MaximumLength) ||
  108. (CapturedLinkTarget.Length % sizeof( WCHAR ))) {
  109. KdPrint(( "OB: Invalid symbolic link target - %wZ\n", &CapturedLinkTarget ));
  110. return( STATUS_INVALID_PARAMETER );
  111. }
  112. //
  113. // Create the symbolic link object
  114. //
  115. Status = ObCreateObject( PreviousMode,
  116. ObpSymbolicLinkObjectType,
  117. ObjectAttributes,
  118. PreviousMode,
  119. NULL,
  120. sizeof( *SymbolicLink ),
  121. 0,
  122. 0,
  123. (PVOID *)&SymbolicLink );
  124. if (!NT_SUCCESS( Status )) {
  125. return( Status );
  126. }
  127. //
  128. // Fill in symbolic link object with link target name string
  129. //
  130. KeQuerySystemTime( &SymbolicLink->CreationTime );
  131. SymbolicLink->DosDeviceDriveIndex = 0;
  132. SymbolicLink->LinkTargetObject = NULL;
  133. RtlInitUnicodeString( &SymbolicLink->LinkTargetRemaining, NULL );
  134. SymbolicLink->LinkTarget.MaximumLength = CapturedLinkTarget.MaximumLength;
  135. SymbolicLink->LinkTarget.Length = CapturedLinkTarget.Length;
  136. SymbolicLink->LinkTarget.Buffer = (PWCH)ExAllocatePoolWithTag( PagedPool,
  137. CapturedLinkTarget.MaximumLength,
  138. 'tmyS' );
  139. if (SymbolicLink->LinkTarget.Buffer == NULL) {
  140. ObDereferenceObject( SymbolicLink );
  141. return STATUS_NO_MEMORY;
  142. }
  143. try {
  144. RtlCopyMemory( SymbolicLink->LinkTarget.Buffer,
  145. CapturedLinkTarget.Buffer,
  146. CapturedLinkTarget.MaximumLength );
  147. } except( EXCEPTION_EXECUTE_HANDLER ) {
  148. ObDereferenceObject( SymbolicLink );
  149. return( GetExceptionCode() );
  150. }
  151. //
  152. // Insert symbolic link object in the current processes object table,
  153. // set symbolic link handle value and return status.
  154. //
  155. Status = ObInsertObject( SymbolicLink,
  156. NULL,
  157. DesiredAccess,
  158. 0,
  159. (PVOID *)&Object,
  160. &Handle );
  161. try {
  162. *LinkHandle = Handle;
  163. } except( EXCEPTION_EXECUTE_HANDLER ) {
  164. //
  165. // Fall through, since we do not want to undo what we have done.
  166. //
  167. }
  168. return( Status );
  169. }
  170. NTSTATUS
  171. NtOpenSymbolicLinkObject (
  172. OUT PHANDLE LinkHandle,
  173. IN ACCESS_MASK DesiredAccess,
  174. IN POBJECT_ATTRIBUTES ObjectAttributes
  175. )
  176. /*++
  177. Routine Description:
  178. This function opens a handle to an symbolic link object with the specified
  179. desired access.
  180. Arguments:
  181. LinkHandle - Supplies a pointer to a variable that will receive the
  182. symbolic link object handle.
  183. DesiredAccess - Supplies the desired types of access for the symbolic link
  184. object.
  185. ObjectAttributes - Supplies a pointer to an object attributes structure.
  186. Return Value:
  187. An appropriate status value
  188. --*/
  189. {
  190. KPROCESSOR_MODE PreviousMode;
  191. NTSTATUS Status;
  192. HANDLE Handle;
  193. PAGED_CODE();
  194. //
  195. // Get previous processor mode and probe output arguments if necessary.
  196. // The object attributes does not need to be probed because the
  197. // ObOpenObjectByName does the probe for us
  198. //
  199. PreviousMode = KeGetPreviousMode();
  200. if (PreviousMode != KernelMode) {
  201. try {
  202. ProbeForWriteHandle( LinkHandle );
  203. } except( EXCEPTION_EXECUTE_HANDLER ) {
  204. return( GetExceptionCode() );
  205. }
  206. }
  207. //
  208. // Open handle to the symbolic link object with the specified desired
  209. // access, set symbolic link handle value, and return service completion
  210. // status.
  211. //
  212. Status = ObOpenObjectByName( ObjectAttributes,
  213. ObpSymbolicLinkObjectType,
  214. PreviousMode,
  215. NULL,
  216. DesiredAccess,
  217. NULL,
  218. &Handle );
  219. try {
  220. *LinkHandle = Handle;
  221. } except( EXCEPTION_EXECUTE_HANDLER ) {
  222. //
  223. // Fall through, since we do not want to undo what we have done.
  224. //
  225. }
  226. return( Status );
  227. }
  228. NTSTATUS
  229. NtQuerySymbolicLinkObject (
  230. IN HANDLE LinkHandle,
  231. IN OUT PUNICODE_STRING LinkTarget,
  232. OUT PULONG ReturnedLength OPTIONAL
  233. )
  234. /*++
  235. Routine Description:
  236. This function queries the state of a symbolic link object and returns the
  237. requested information in the specified record structure.
  238. Arguments:
  239. LinkHandle - Supplies a handle to a symbolic link object. This handle
  240. must have SYMBOLIC_LINK_QUERY access granted.
  241. LinkTarget - Supplies a pointer to a record that is to receive the
  242. target name of the symbolic link object.
  243. ReturnedLength - Optionally receives the maximum length, in bytes, of
  244. the link target on return
  245. Return Value:
  246. An appropriate status value
  247. --*/
  248. {
  249. KPROCESSOR_MODE PreviousMode;
  250. NTSTATUS Status;
  251. POBJECT_SYMBOLIC_LINK SymbolicLink;
  252. UNICODE_STRING CapturedLinkTarget;
  253. //
  254. // Get previous processor mode and probe output arguments if necessary.
  255. //
  256. PAGED_CODE();
  257. PreviousMode = KeGetPreviousMode();
  258. if (PreviousMode != KernelMode) {
  259. try {
  260. ProbeForReadSmallStructure( LinkTarget,
  261. sizeof( *LinkTarget ),
  262. sizeof( WCHAR ) );
  263. ProbeForWriteUshort( &LinkTarget->Length );
  264. ProbeForWriteUshort( &LinkTarget->MaximumLength );
  265. CapturedLinkTarget = *LinkTarget;
  266. ProbeForWrite( CapturedLinkTarget.Buffer,
  267. CapturedLinkTarget.MaximumLength,
  268. sizeof( UCHAR ) );
  269. if (ARGUMENT_PRESENT( ReturnedLength )) {
  270. ProbeForWriteUlong( ReturnedLength );
  271. }
  272. } except( EXCEPTION_EXECUTE_HANDLER ) {
  273. return( GetExceptionCode() );
  274. }
  275. } else {
  276. CapturedLinkTarget = *LinkTarget;
  277. }
  278. //
  279. // Reference symbolic link object by handle, read current state, deference
  280. // symbolic link object, fill in target name structure and return service
  281. // status.
  282. //
  283. Status = ObReferenceObjectByHandle( LinkHandle,
  284. SYMBOLIC_LINK_QUERY,
  285. ObpSymbolicLinkObjectType,
  286. PreviousMode,
  287. (PVOID *)&SymbolicLink,
  288. NULL );
  289. if (NT_SUCCESS( Status )) {
  290. POBJECT_HEADER ObjectHeader;
  291. ObjectHeader = OBJECT_TO_OBJECT_HEADER( SymbolicLink );
  292. ObpLockObject( ObjectHeader );
  293. //
  294. // If the caller wants a return length and what we found can easily
  295. // fit in the output buffer then we copy into the output buffer all
  296. // the bytes from the link.
  297. //
  298. // If the caller did not want a return length and we found can still
  299. // easily fit in the output buffer then copy over the bytes that just
  300. // make up the string and nothing extra
  301. //
  302. if ((ARGUMENT_PRESENT( ReturnedLength ) &&
  303. (SymbolicLink->LinkTarget.MaximumLength <= CapturedLinkTarget.MaximumLength))
  304. ||
  305. (!ARGUMENT_PRESENT( ReturnedLength ) &&
  306. (SymbolicLink->LinkTarget.Length <= CapturedLinkTarget.MaximumLength)) ) {
  307. try {
  308. RtlCopyMemory( CapturedLinkTarget.Buffer,
  309. SymbolicLink->LinkTarget.Buffer,
  310. ARGUMENT_PRESENT( ReturnedLength ) ? SymbolicLink->LinkTarget.MaximumLength
  311. : SymbolicLink->LinkTarget.Length );
  312. LinkTarget->Length = SymbolicLink->LinkTarget.Length;
  313. if (ARGUMENT_PRESENT( ReturnedLength )) {
  314. *ReturnedLength = SymbolicLink->LinkTarget.MaximumLength;
  315. }
  316. } except( EXCEPTION_EXECUTE_HANDLER ) {
  317. //
  318. // Fall through, since we do cannot undo what we have done.
  319. //
  320. }
  321. } else {
  322. //
  323. // The output buffer is just too small for the link target, but
  324. // we'll tell the user how much is needed if they asked for that
  325. // return value
  326. //
  327. if (ARGUMENT_PRESENT( ReturnedLength )) {
  328. try {
  329. *ReturnedLength = SymbolicLink->LinkTarget.MaximumLength;
  330. } except( EXCEPTION_EXECUTE_HANDLER ) {
  331. //
  332. // Fall through, since we do cannot undo what we have done.
  333. //
  334. }
  335. }
  336. Status = STATUS_BUFFER_TOO_SMALL;
  337. }
  338. ObpUnlockObject( ObjectHeader );
  339. ObDereferenceObject( SymbolicLink );
  340. }
  341. return( Status );
  342. }
  343. NTSTATUS
  344. ObpParseSymbolicLink (
  345. IN PVOID ParseObject,
  346. IN PVOID ObjectType,
  347. IN PACCESS_STATE AccessState,
  348. IN KPROCESSOR_MODE AccessMode,
  349. IN ULONG Attributes,
  350. IN OUT PUNICODE_STRING CompleteName,
  351. IN OUT PUNICODE_STRING RemainingName,
  352. IN OUT PVOID Context OPTIONAL,
  353. IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
  354. OUT PVOID *Object
  355. )
  356. /*++
  357. Routine Description:
  358. This is the call back routine for parsing symbolic link objects. It is invoked
  359. as part of ObpLookupObjectName
  360. Arguments:
  361. ParseObject - This will actually be a symbolic link object
  362. ObjectType - Specifies the type of the object to lookup
  363. AccessState - Current access state, describing already granted access
  364. types, the privileges used to get them, and any access types yet to
  365. be granted. The access masks may not contain any generic access
  366. types.
  367. AccessMode - Specifies the callers processor mode
  368. Attributes - Specifies the attributes for the lookup (e.g., case
  369. insensitive)
  370. CompleteName - Supplies a pointer to the complete name that we are trying
  371. to open. On return this could be modified to fit the new reparse
  372. buffer
  373. RemainingName - Supplies a pointer to the remaining name that we are
  374. trying to open. On return this will point to what remains after
  375. we processed the symbolic link.
  376. Context - Unused
  377. SecurityQos - Unused
  378. Object - Receives a pointer to the symbolic link object that we
  379. resolve to
  380. Return Value:
  381. STATUS_REPARSE_OBJECT if the parse object is already a snapped
  382. symbolic link meaning we've modified the remaining name and
  383. and have returned the target object of the symbolic link
  384. STATUS_REPARSE if the parse object has not been snapped. In this
  385. case the Complete name has been modified with the link target
  386. name added in front of the remaining name. The parameters
  387. remaining name and object must now be ignored by the caller
  388. An appropriate error value
  389. --*/
  390. {
  391. ULONG NewLength;
  392. USHORT Length;
  393. USHORT MaximumLength;
  394. PWCHAR NewName, NewRemainingName;
  395. ULONG InsertAmount;
  396. NTSTATUS Status;
  397. POBJECT_SYMBOLIC_LINK SymbolicLink;
  398. PUNICODE_STRING LinkTargetName;
  399. PAGED_CODE();
  400. //
  401. // This routine needs to be synchonized with the delete symbolic link
  402. // operation. Which uses the root directory mutex.
  403. //
  404. try {
  405. *Object = NULL;
  406. //
  407. // If there isn't any name remaining and the caller gave us
  408. // an object type then we'll reference the parse object. If
  409. // this is successful then that's the object we return. Otherwise
  410. // if the status is anything but a type mismatch then we'll
  411. // return that error status
  412. //
  413. if (RemainingName->Length == 0) {
  414. if ( ObjectType ) {
  415. Status = ObReferenceObjectByPointer( ParseObject,
  416. 0,
  417. ObjectType,
  418. AccessMode );
  419. if (NT_SUCCESS( Status )) {
  420. *Object = ParseObject;
  421. leave;
  422. } else if (Status != STATUS_OBJECT_TYPE_MISMATCH) {
  423. leave;
  424. }
  425. }
  426. //
  427. // If the remaining name does not start with a "\" then
  428. // its is illformed and we'll call it a type mismatch
  429. //
  430. } else if (*(RemainingName->Buffer) != OBJ_NAME_PATH_SEPARATOR) {
  431. Status = STATUS_OBJECT_TYPE_MISMATCH;
  432. leave;
  433. }
  434. //
  435. // A symbolic link has been encountered. See if this link has been snapped
  436. // to a particular object.
  437. //
  438. SymbolicLink = (POBJECT_SYMBOLIC_LINK)ParseObject;
  439. if (SymbolicLink->LinkTargetObject != NULL) {
  440. //
  441. // This is a snapped link. Get the remaining portion of the
  442. // symbolic link target, if any.
  443. //
  444. LinkTargetName = &SymbolicLink->LinkTargetRemaining;
  445. if (LinkTargetName->Length == 0) {
  446. //
  447. // Remaining link target string is zero, so return to caller
  448. // quickly with snapped object pointer and remaining object name
  449. // which we haven't touched yet.
  450. //
  451. *Object = SymbolicLink->LinkTargetObject;
  452. Status = STATUS_REPARSE_OBJECT;
  453. leave;
  454. }
  455. //
  456. // We have a snapped symbolic link that has additional text.
  457. // Insert that in front of the current remaining name, preserving
  458. // and text between CompleteName and RemainingName
  459. //
  460. InsertAmount = LinkTargetName->Length;
  461. if ((LinkTargetName->Buffer[ (InsertAmount / sizeof( WCHAR )) - 1 ] == OBJ_NAME_PATH_SEPARATOR)
  462. &&
  463. (*(RemainingName->Buffer) == OBJ_NAME_PATH_SEPARATOR)) {
  464. //
  465. // Both the link target name ends in a "\" and the remaining
  466. // starts with a "\" but we only need one when we're done
  467. //
  468. InsertAmount -= sizeof( WCHAR );
  469. }
  470. //
  471. // We need to bias the differnce between two
  472. // pointers with * sizeof(wchar) because the difference is in wchar
  473. // and we need the length in bytes
  474. //
  475. NewLength = (ULONG)(((RemainingName->Buffer - CompleteName->Buffer) * sizeof( WCHAR )) +
  476. InsertAmount +
  477. RemainingName->Length);
  478. if (NewLength > 0xFFF0) {
  479. Status = STATUS_NAME_TOO_LONG;
  480. leave;
  481. }
  482. Length = (USHORT)NewLength;
  483. //
  484. // Now check if the new computed length is too big for the input
  485. // buffer containing the complete name
  486. //
  487. if (CompleteName->MaximumLength <= Length) {
  488. //
  489. // The new concatentated name is larger than the buffer supplied for
  490. // the complete name. Allocate space for this new string
  491. //
  492. MaximumLength = Length + sizeof( UNICODE_NULL );
  493. NewName = ExAllocatePoolWithTag( OB_NAMESPACE_POOL_TYPE, MaximumLength, 'mNbO' );
  494. if (NewName == NULL) {
  495. Status = STATUS_INSUFFICIENT_RESOURCES;
  496. leave;
  497. }
  498. //
  499. // Calculate the pointer within this buffer for the remaining
  500. // name. This value has not been biased by the new link
  501. // target name
  502. //
  503. NewRemainingName = NewName + (RemainingName->Buffer - CompleteName->Buffer);
  504. //
  505. // Copy over all the names that we've processed so far
  506. //
  507. RtlCopyMemory( NewName,
  508. CompleteName->Buffer,
  509. ((RemainingName->Buffer - CompleteName->Buffer) * sizeof( WCHAR )));
  510. //
  511. // If we have some remaining names then those over at the
  512. // the location offset to hold the link target name
  513. //
  514. if (RemainingName->Length != 0) {
  515. RtlCopyMemory( (PVOID)((PUCHAR)NewRemainingName + InsertAmount),
  516. RemainingName->Buffer,
  517. RemainingName->Length );
  518. }
  519. //
  520. // Now insert the link target name
  521. //
  522. RtlCopyMemory( NewRemainingName, LinkTargetName->Buffer, InsertAmount );
  523. //
  524. // Free the old complete name buffer and reset the input
  525. // strings to use the new buffer
  526. //
  527. ExFreePool( CompleteName->Buffer );
  528. CompleteName->Buffer = NewName;
  529. CompleteName->Length = Length;
  530. CompleteName->MaximumLength = MaximumLength;
  531. RemainingName->Buffer = NewRemainingName;
  532. RemainingName->Length = Length - (USHORT)((PCHAR)NewRemainingName - (PCHAR)NewName);
  533. RemainingName->MaximumLength = RemainingName->Length + sizeof( UNICODE_NULL );
  534. } else {
  535. //
  536. // Insert extra text associated with this symbolic link name before
  537. // existing remaining name, if any.
  538. //
  539. // First shove over the remaining name to make a hole for the
  540. // link target name
  541. //
  542. if (RemainingName->Length != 0) {
  543. RtlMoveMemory( (PVOID)((PUCHAR)RemainingName->Buffer + InsertAmount),
  544. RemainingName->Buffer,
  545. RemainingName->Length );
  546. }
  547. //
  548. // Now insert the link target name
  549. //
  550. RtlCopyMemory( RemainingName->Buffer, LinkTargetName->Buffer, InsertAmount );
  551. //
  552. // Adjust input strings to account for this inserted text
  553. //
  554. CompleteName->Length += LinkTargetName->Length;
  555. RemainingName->Length += LinkTargetName->Length;
  556. RemainingName->MaximumLength += RemainingName->Length + sizeof( UNICODE_NULL );
  557. CompleteName->Buffer[ CompleteName->Length / sizeof( WCHAR ) ] = UNICODE_NULL;
  558. }
  559. //
  560. // Return the object address associated with snapped symbolic link
  561. // and the reparse object status code.
  562. //
  563. *Object = SymbolicLink->LinkTargetObject;
  564. Status = STATUS_REPARSE_OBJECT;
  565. leave;
  566. }
  567. //
  568. // The symbolic has not yet been snapped
  569. //
  570. // Compute the size of the new name and check if the name will
  571. // fit in the existing complete name buffer.
  572. //
  573. LinkTargetName = &SymbolicLink->LinkTarget;
  574. InsertAmount = LinkTargetName->Length;
  575. if ((InsertAmount != 0)
  576. &&
  577. (LinkTargetName->Buffer[ (InsertAmount / sizeof( WCHAR )) - 1 ] == OBJ_NAME_PATH_SEPARATOR)
  578. &&
  579. (RemainingName->Length != 0)
  580. &&
  581. (*(RemainingName->Buffer) == OBJ_NAME_PATH_SEPARATOR)) {
  582. //
  583. // Both the link target name ends in a "\" and the remaining
  584. // starts with a "\" but we only need one when we're done
  585. //
  586. InsertAmount -= sizeof( WCHAR );
  587. }
  588. NewLength = InsertAmount + RemainingName->Length;
  589. if (NewLength > 0xFFF0) {
  590. Status = STATUS_NAME_TOO_LONG;
  591. leave;
  592. }
  593. Length = (USHORT)NewLength;
  594. if (CompleteName->MaximumLength <= Length) {
  595. //
  596. // The new concatentated name is larger than the buffer supplied for
  597. // the complete name.
  598. //
  599. MaximumLength = Length + sizeof( UNICODE_NULL );
  600. NewName = ExAllocatePoolWithTag( OB_NAMESPACE_POOL_TYPE, MaximumLength, 'mNbO' );
  601. if (NewName == NULL) {
  602. Status = STATUS_INSUFFICIENT_RESOURCES;
  603. leave;
  604. }
  605. } else {
  606. MaximumLength = CompleteName->MaximumLength;
  607. NewName = CompleteName->Buffer;
  608. }
  609. //
  610. // Concatenate the symbolic link name with the remaining name,
  611. // if any. What this does is overwrite the front of the complete
  612. // name up to the remaining name with the links target name
  613. //
  614. if (RemainingName->Length != 0) {
  615. RtlMoveMemory( (PVOID)((PUCHAR)NewName + InsertAmount),
  616. RemainingName->Buffer,
  617. RemainingName->Length );
  618. }
  619. RtlCopyMemory( NewName, LinkTargetName->Buffer, InsertAmount );
  620. NewName[ Length / sizeof( WCHAR ) ] = UNICODE_NULL;
  621. //
  622. // If a new name buffer was allocated, then free the original complete
  623. // name buffer.
  624. //
  625. if (NewName != CompleteName->Buffer) {
  626. ExFreePool( CompleteName->Buffer );
  627. }
  628. //
  629. // Set the new complete name buffer parameters and return a reparse
  630. // status.
  631. //
  632. CompleteName->Buffer = NewName;
  633. CompleteName->Length = Length;
  634. CompleteName->MaximumLength = MaximumLength;
  635. Status = STATUS_REPARSE;
  636. } finally {
  637. //
  638. // Nothing to do here
  639. //
  640. }
  641. return Status;
  642. }
  643. VOID
  644. ObpDeleteSymbolicLink (
  645. IN PVOID Object
  646. )
  647. /*++
  648. Routine Description:
  649. This routine is called when a reference to a symbolic link goes to zero.
  650. Its job is to clean up the memory used to the symbolic link string
  651. Arguments:
  652. Object - Supplies a pointer to the symbolic link object being deleted
  653. Return Value:
  654. None.
  655. --*/
  656. {
  657. POBJECT_SYMBOLIC_LINK SymbolicLink = (POBJECT_SYMBOLIC_LINK)Object;
  658. PAGED_CODE();
  659. //
  660. // The only cleaning up we need to do is to free the link target
  661. // buffer
  662. //
  663. if (SymbolicLink->LinkTarget.Buffer != NULL) {
  664. ExFreePool( SymbolicLink->LinkTarget.Buffer );
  665. }
  666. SymbolicLink->LinkTarget.Buffer = NULL;
  667. //
  668. // And return to our caller
  669. //
  670. return;
  671. }
  672. VOID
  673. ObpDeleteSymbolicLinkName (
  674. POBJECT_SYMBOLIC_LINK SymbolicLink
  675. )
  676. /*++
  677. Routine Description:
  678. This routine delete the symbolic from the system
  679. Arguments:
  680. SymbolicLink - Supplies a pointer to the object body for the symbolic
  681. link object to delete
  682. Return Value:
  683. None.
  684. This function must be called with the symbolic link object locked.
  685. That lock is protecting the symlink specific fields.
  686. --*/
  687. {
  688. ObpProcessDosDeviceSymbolicLink( SymbolicLink, DELETE_SYMBOLIC_LINK );
  689. return;
  690. }
  691. VOID
  692. ObpCreateSymbolicLinkName (
  693. POBJECT_SYMBOLIC_LINK SymbolicLink
  694. )
  695. /*++
  696. Routine Description:
  697. This routine does extra processing for symbolic links being created in
  698. object directories controlled by device map objects.
  699. This processing consists of:
  700. 1. Determine if the name of the symbolic link is a drive letter.
  701. If so, then we will need to update the drive type in the
  702. associated device map object.
  703. 2. Process the link target, trying to resolve it into a pointer to
  704. an object other than a object directory object. All object
  705. directories traversed must grant world traverse access other
  706. wise we bail out. If we successfully find a non object
  707. directory object, then reference the object pointer and store it
  708. in the symbolic link object, along with a remaining string if
  709. any. ObpLookupObjectName will used this cache object pointer to
  710. short circuit the name lookup directly to the cached object's
  711. parse routine. For any object directory objects traversed along
  712. the way, increment their symbolic link SymbolicLinkUsageCount
  713. field. This field is used whenever an object directory is
  714. deleted or its security is changed such that it no longer grants
  715. world traverse access. In either case, if the field is non-zero
  716. we walk all the symbolic links and resnap them.
  717. Arguments:
  718. SymbolicLink - pointer to symbolic link object being created.
  719. Return Value:
  720. None.
  721. --*/
  722. {
  723. POBJECT_HEADER ObjectHeader;
  724. POBJECT_HEADER_NAME_INFO NameInfo;
  725. WCHAR DosDeviceDriveLetter;
  726. ULONG DosDeviceDriveIndex;
  727. //
  728. // Now see if this symbolic link is being created in an object directory
  729. // controlled by a device map object. Since we are only called from
  730. // NtCreateSymbolicLinkObject, after the handle to this symbolic link
  731. // has been created but before it is returned to the caller the handle can't
  732. // be closed while we are executing, unless via a random close,
  733. // So no need to hold the type specific mutex while we look at the name.
  734. //
  735. ObjectHeader = OBJECT_TO_OBJECT_HEADER( SymbolicLink );
  736. NameInfo = ObpReferenceNameInfo( ObjectHeader );
  737. if ((NameInfo == NULL) ||
  738. (NameInfo->Directory == NULL) ||
  739. (NameInfo->Directory->DeviceMap == NULL)) {
  740. ObpDereferenceNameInfo( NameInfo );
  741. return;
  742. }
  743. //
  744. // Here if we are creating a symbolic link in an object directory controlled
  745. // by a device map object. See if this is a drive letter definition. If so
  746. // calculate the drive letter index and remember in the symbolic link object.
  747. //
  748. DosDeviceDriveIndex = 0;
  749. if ((NameInfo->Name.Length == (2 * sizeof( WCHAR ))) &&
  750. (NameInfo->Name.Buffer[ 1 ] == L':')) {
  751. DosDeviceDriveLetter = RtlUpcaseUnicodeChar( NameInfo->Name.Buffer[ 0 ] );
  752. if ((DosDeviceDriveLetter >= L'A') && (DosDeviceDriveLetter <= L'Z')) {
  753. DosDeviceDriveIndex = DosDeviceDriveLetter - L'A';
  754. DosDeviceDriveIndex += 1;
  755. SymbolicLink->DosDeviceDriveIndex = DosDeviceDriveIndex;
  756. }
  757. }
  758. //
  759. // Now traverse the target path seeing if we can snap the link now.
  760. //
  761. ObpProcessDosDeviceSymbolicLink( SymbolicLink, CREATE_SYMBOLIC_LINK );
  762. ObpDereferenceNameInfo( NameInfo );
  763. return;
  764. }
  765. //
  766. // Local support routine
  767. //
  768. #define MAX_DEPTH 16
  769. VOID
  770. ObpProcessDosDeviceSymbolicLink (
  771. POBJECT_SYMBOLIC_LINK SymbolicLink,
  772. ULONG Action
  773. )
  774. /*++
  775. Routine Description:
  776. This function is called whenever a symbolic link is created or deleted
  777. in an object directory controlled by a device map object.
  778. For creates, it attempts to snap the symbolic link to a non-object
  779. directory object. It does this by walking the symbolic link target
  780. string, until it sees a non-directory object or a directory object
  781. that does NOT allow World traverse access. It stores a referenced
  782. pointer to this object in the symbolic link object. It also
  783. increments a count in each of the object directory objects that it
  784. walked over. This count is used to disallow any attempt to remove
  785. World traverse access from a directory object after it has
  786. participated in a snapped symbolic link.
  787. For deletes, it repeats the walk of the target string, decrementing
  788. the count associated with each directory object walked over. It also
  789. dereferences the snapped object pointer.
  790. Arguments:
  791. SymbolicLink - pointer to symbolic link object being created or deleted.
  792. Action - describes whether this is a create or a delete action
  793. Return Value:
  794. None.
  795. --*/
  796. {
  797. NTSTATUS Status;
  798. PVOID Object;
  799. POBJECT_HEADER ObjectHeader;
  800. POBJECT_HEADER_NAME_INFO NameInfo;
  801. PSECURITY_DESCRIPTOR SecurityDescriptor;
  802. BOOLEAN MemoryAllocated;
  803. UNICODE_STRING RemainingName;
  804. UNICODE_STRING ComponentName;
  805. BOOLEAN HaveWorldTraverseAccess;
  806. ULONG Depth;
  807. POBJECT_DIRECTORY Directories[ MAX_DEPTH ], Directory, ParentDirectory;
  808. PDEVICE_OBJECT DeviceObject;
  809. PDEVICE_MAP DeviceMap = NULL;
  810. ULONG DosDeviceDriveType;
  811. BOOLEAN DeviceMapUsed = FALSE;
  812. UNICODE_STRING RemainingTarget;
  813. ULONG MaxReparse = OBJ_MAX_REPARSE_ATTEMPTS;
  814. OBP_LOOKUP_CONTEXT LookupContext;
  815. POBJECT_DIRECTORY SymLinkDirectory = NULL;
  816. BOOLEAN PreviousLockingState;
  817. ObjectHeader = OBJECT_TO_OBJECT_HEADER( SymbolicLink );
  818. NameInfo = OBJECT_HEADER_TO_NAME_INFO( ObjectHeader );
  819. if (NameInfo != NULL) {
  820. SymLinkDirectory = NameInfo->Directory;
  821. }
  822. Object = NULL;
  823. RtlInitUnicodeString( &RemainingTarget, NULL );
  824. ObpInitializeLookupContext( &LookupContext );
  825. //
  826. // Check if we are creating a symbolic link or if the link has already
  827. // been snapped
  828. //
  829. if ((Action == CREATE_SYMBOLIC_LINK) ||
  830. (SymbolicLink->LinkTargetObject != NULL)) {
  831. ParentDirectory = NULL;
  832. Depth = 0;
  833. Directory = ObpRootDirectoryObject;
  834. RemainingName = SymbolicLink->LinkTarget;
  835. //
  836. // If LUID device maps are enabled,
  837. // then use the Object Manager's pointer to the global
  838. // device map
  839. // With LUID device maps enabled, the process' device map pointer
  840. // may be NULL
  841. //
  842. if (ObpLUIDDeviceMapsEnabled != 0) {
  843. DeviceMap = ObSystemDeviceMap;
  844. }
  845. else {
  846. //
  847. // use the device map associated with the process
  848. //
  849. DeviceMap = PsGetCurrentProcess()->DeviceMap;
  850. }
  851. ReCalcDeviceMap:
  852. if (DeviceMap) {
  853. if (!((ULONG_PTR)(RemainingName.Buffer) & (sizeof(ULONGLONG)-1))
  854. &&
  855. (DeviceMap->DosDevicesDirectory != NULL )) {
  856. //
  857. // Check if the object name is actually equal to the
  858. // global dos devices short name prefix "\??\"
  859. //
  860. if ((RemainingName.Length >= ObpDosDevicesShortName.Length)
  861. &&
  862. (*(PULONGLONG)(RemainingName.Buffer) == ObpDosDevicesShortNamePrefix.Alignment.QuadPart)) {
  863. //
  864. // The user gave us the dos short name prefix so we'll
  865. // look down the directory, and start the search at the
  866. // dos device directory
  867. //
  868. Directory = DeviceMap->DosDevicesDirectory;
  869. RemainingName.Buffer += (ObpDosDevicesShortName.Length / sizeof( WCHAR ));
  870. RemainingName.Length -= ObpDosDevicesShortName.Length;
  871. DeviceMapUsed = TRUE;
  872. }
  873. }
  874. }
  875. //
  876. // The following loop will dissect the link target checking
  877. // that each directory exists and that we have access to
  878. // the directory. When we pop out the local directories
  879. // array will contain a list of directories that we need
  880. // to traverse to process this action.
  881. //
  882. while (TRUE) {
  883. //
  884. // Gobble up the "\" in the remaining name
  885. //
  886. if (*(RemainingName.Buffer) == OBJ_NAME_PATH_SEPARATOR) {
  887. RemainingName.Buffer++;
  888. RemainingName.Length -= sizeof( OBJ_NAME_PATH_SEPARATOR );
  889. }
  890. //
  891. // And dissect the name into its first component and any
  892. // remaining part
  893. //
  894. ComponentName = RemainingName;
  895. while (RemainingName.Length != 0) {
  896. if (*(RemainingName.Buffer) == OBJ_NAME_PATH_SEPARATOR) {
  897. break;
  898. }
  899. RemainingName.Buffer++;
  900. RemainingName.Length -= sizeof( OBJ_NAME_PATH_SEPARATOR );
  901. }
  902. ComponentName.Length -= RemainingName.Length;
  903. if (ComponentName.Length == 0) {
  904. ObpReleaseLookupContext(&LookupContext);
  905. return;
  906. }
  907. //
  908. // See if we have world traverse access to look this name up
  909. //
  910. if (ParentDirectory != NULL) {
  911. HaveWorldTraverseAccess = FALSE;
  912. //
  913. // Obtain the object's security descriptor
  914. //
  915. Status = ObGetObjectSecurity( ParentDirectory,
  916. &SecurityDescriptor,
  917. &MemoryAllocated );
  918. if (NT_SUCCESS( Status )) {
  919. //
  920. // Check to see if WORLD has TRAVERSE access and then release
  921. // the security descriptor
  922. //
  923. HaveWorldTraverseAccess = SeFastTraverseCheck( SecurityDescriptor,
  924. DIRECTORY_TRAVERSE,
  925. UserMode );
  926. ObReleaseObjectSecurity( SecurityDescriptor,
  927. MemoryAllocated );
  928. }
  929. if (!HaveWorldTraverseAccess) {
  930. Object = NULL;
  931. break;
  932. }
  933. if (Depth >= MAX_DEPTH) {
  934. Object = NULL;
  935. break;
  936. }
  937. Directories[ Depth++ ] = ParentDirectory;
  938. }
  939. //
  940. // Look this component name up in this directory. If not found, then
  941. // bail.
  942. //
  943. //
  944. // If we are searching the same directory that contains the sym link
  945. // we have already the directory exclusively locked. We need to adjust
  946. // the lookupcontext state and avoid recursive locking
  947. //
  948. if (Directory == SymLinkDirectory) {
  949. PreviousLockingState = LookupContext.DirectoryLocked;
  950. LookupContext.DirectoryLocked = TRUE;
  951. }
  952. Object = ObpLookupDirectoryEntry( Directory,
  953. &ComponentName,
  954. 0,
  955. FALSE ,
  956. &LookupContext);
  957. if (Directory == SymLinkDirectory) {
  958. LookupContext.DirectoryLocked = PreviousLockingState;
  959. }
  960. if (Object == NULL) {
  961. break;
  962. }
  963. //
  964. // See if this is a object directory object. If so, keep going
  965. //
  966. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  967. if (ObjectHeader->Type == ObpDirectoryObjectType) {
  968. ParentDirectory = Directory;
  969. Directory = (POBJECT_DIRECTORY)Object;
  970. } else if ((ObjectHeader->Type == ObpSymbolicLinkObjectType) &&
  971. (((POBJECT_SYMBOLIC_LINK)Object)->DosDeviceDriveIndex == 0)) {
  972. //
  973. // To prevent Denial of Service attacks from parsing
  974. // symbolic links infinitely.
  975. // Check the number of symbolic link parse attempts
  976. //
  977. if (MaxReparse == 0) {
  978. Object = NULL;
  979. break;
  980. }
  981. MaxReparse--;
  982. //
  983. // Found a symbolic link to another symbolic link that is
  984. // not already snapped. So switch to its target string
  985. // so we can chase down the real device object
  986. //
  987. ParentDirectory = NULL;
  988. Depth = 0;
  989. Directory = ObpRootDirectoryObject;
  990. //
  991. // Save the remaining name
  992. //
  993. if (RemainingTarget.Length == 0) {
  994. RemainingTarget = RemainingName;
  995. }
  996. RemainingName = ((POBJECT_SYMBOLIC_LINK)Object)->LinkTarget;
  997. goto ReCalcDeviceMap;
  998. } else {
  999. //
  1000. // Not an object directory object, or a symbolic link to an
  1001. // unsnapped symbolic link, so all done. Exit the loop
  1002. //
  1003. break;
  1004. }
  1005. }
  1006. //
  1007. // Done walking the target path. Now update the counts associated
  1008. // with each directory object walked over.
  1009. //
  1010. while (Depth--) {
  1011. Directory = Directories[ Depth ];
  1012. if (Action == CREATE_SYMBOLIC_LINK) {
  1013. if (Object != NULL) {
  1014. Directory->SymbolicLinkUsageCount += 1;
  1015. }
  1016. } else {
  1017. Directory->SymbolicLinkUsageCount -= 1;
  1018. }
  1019. }
  1020. }
  1021. //
  1022. // Done processing symbolic link target path. Update symbolic link
  1023. // object as appropriate for passed in reason
  1024. //
  1025. //
  1026. // If this is a drive letter symbolic link, get the address of the device
  1027. // map object that is controlling the containing object directory so we
  1028. // can update the drive type in the device map.
  1029. //
  1030. DeviceMap = NULL;
  1031. if (SymbolicLink->DosDeviceDriveIndex != 0) {
  1032. ObjectHeader = OBJECT_TO_OBJECT_HEADER( SymbolicLink );
  1033. NameInfo = ObpReferenceNameInfo( ObjectHeader );
  1034. if (NameInfo != NULL && NameInfo->Directory) {
  1035. DeviceMap = NameInfo->Directory->DeviceMap;
  1036. }
  1037. ObpDereferenceNameInfo( NameInfo );
  1038. }
  1039. //
  1040. // Check if we are creating a symbolic link
  1041. //
  1042. if (Action == CREATE_SYMBOLIC_LINK) {
  1043. DosDeviceDriveType = DOSDEVICE_DRIVE_CALCULATE;
  1044. if (Object != NULL) {
  1045. //
  1046. // We only want to do snapping for console session. When we create a
  1047. // remote session all the symbolic links stored in the console dos devices
  1048. // directory (\??) are copied into the per session DosDevices object directory
  1049. // (\Session\<id>\DosDevices). We don't want to do snapping for the copied
  1050. // symbolic links since for each copy we will increment the ref count on the
  1051. // target object. All these counts have to go to zero before the device can be
  1052. // deleted.
  1053. //
  1054. // Disable snapping until we come up with a delete scheme for it
  1055. //
  1056. if (FALSE /*( PsGetCurrentProcess()->SessionId == 0) || (DeviceMapUsed)*/) {
  1057. //
  1058. // Create action. Store a referenced pointer to the snapped object
  1059. // along with the description of any remaining name string. Also,
  1060. // for Dos drive letters, update the drive type in the appropriate
  1061. // device map object.
  1062. //
  1063. ObReferenceObject( Object );
  1064. SymbolicLink->LinkTargetObject = Object;
  1065. //
  1066. // If we have saved a remaining target string
  1067. // we'll set it to the symbolic link object
  1068. //
  1069. if ( RemainingTarget.Length ) {
  1070. RemainingName = RemainingTarget;
  1071. }
  1072. if ((*(RemainingName.Buffer) == OBJ_NAME_PATH_SEPARATOR) &&
  1073. (RemainingName.Length == sizeof( OBJ_NAME_PATH_SEPARATOR))) {
  1074. RtlInitUnicodeString( &SymbolicLink->LinkTargetRemaining, NULL );
  1075. } else {
  1076. SymbolicLink->LinkTargetRemaining = RemainingName;
  1077. }
  1078. }
  1079. if (SymbolicLink->DosDeviceDriveIndex != 0) {
  1080. //
  1081. // Default is to calculate the drive type in user mode if we are
  1082. // unable to snap the symbolic link or it does not resolve to a
  1083. // DEVICE_OBJECT we know about.
  1084. //
  1085. ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
  1086. if (ObjectHeader->Type == IoDeviceObjectType) {
  1087. DeviceObject = (PDEVICE_OBJECT)Object;
  1088. switch (DeviceObject->DeviceType) {
  1089. case FILE_DEVICE_CD_ROM:
  1090. case FILE_DEVICE_CD_ROM_FILE_SYSTEM:
  1091. DosDeviceDriveType = DOSDEVICE_DRIVE_CDROM;
  1092. break;
  1093. case FILE_DEVICE_DISK:
  1094. case FILE_DEVICE_DISK_FILE_SYSTEM:
  1095. case FILE_DEVICE_FILE_SYSTEM:
  1096. if (DeviceObject->Characteristics & FILE_REMOVABLE_MEDIA) {
  1097. DosDeviceDriveType = DOSDEVICE_DRIVE_REMOVABLE;
  1098. } else {
  1099. DosDeviceDriveType = DOSDEVICE_DRIVE_FIXED;
  1100. }
  1101. break;
  1102. case FILE_DEVICE_MULTI_UNC_PROVIDER:
  1103. case FILE_DEVICE_NETWORK:
  1104. case FILE_DEVICE_NETWORK_BROWSER:
  1105. case FILE_DEVICE_NETWORK_REDIRECTOR:
  1106. DosDeviceDriveType = DOSDEVICE_DRIVE_REMOTE;
  1107. break;
  1108. case FILE_DEVICE_NETWORK_FILE_SYSTEM:
  1109. #if defined(REMOTE_BOOT)
  1110. //
  1111. // If this is a remote boot workstation, the X:
  1112. // drive is a redirected drive, but needs to look
  1113. // like a local drive.
  1114. //
  1115. if (IoRemoteBootClient &&
  1116. (SymbolicLink->DosDeviceDriveIndex == 24)) {
  1117. DosDeviceDriveType = DOSDEVICE_DRIVE_FIXED;
  1118. } else
  1119. #endif // defined(REMOTE_BOOT)
  1120. {
  1121. DosDeviceDriveType = DOSDEVICE_DRIVE_REMOTE;
  1122. }
  1123. break;
  1124. case FILE_DEVICE_VIRTUAL_DISK:
  1125. DosDeviceDriveType = DOSDEVICE_DRIVE_RAMDISK;
  1126. break;
  1127. default:
  1128. DosDeviceDriveType = DOSDEVICE_DRIVE_UNKNOWN;
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. //
  1135. // If this is a drive letter symbolic link, update the drive type and
  1136. // and mark as valid drive letter.
  1137. //
  1138. if (DeviceMap != NULL) {
  1139. ObpLockDeviceMap();
  1140. DeviceMap->DriveType[ SymbolicLink->DosDeviceDriveIndex-1 ] = (UCHAR)DosDeviceDriveType;
  1141. DeviceMap->DriveMap |= 1 << (SymbolicLink->DosDeviceDriveIndex-1) ;
  1142. ObpUnlockDeviceMap();
  1143. }
  1144. } else {
  1145. //
  1146. // Deleting the symbolic link. Dereference the snapped object pointer if any
  1147. // and zero out the snapped object fields.
  1148. //
  1149. RtlInitUnicodeString( &SymbolicLink->LinkTargetRemaining, NULL );
  1150. Object = SymbolicLink->LinkTargetObject;
  1151. if (Object != NULL) {
  1152. SymbolicLink->LinkTargetObject = NULL;
  1153. ObDereferenceObject( Object );
  1154. }
  1155. //
  1156. // If this is a drive letter symbolic link, set the drive type to
  1157. // unknown and clear the bit in the drive letter bit map.
  1158. //
  1159. if (DeviceMap != NULL) {
  1160. ObpLockDeviceMap();
  1161. DeviceMap->DriveMap &= ~(1 << (SymbolicLink->DosDeviceDriveIndex-1));
  1162. DeviceMap->DriveType[ SymbolicLink->DosDeviceDriveIndex-1 ] = DOSDEVICE_DRIVE_UNKNOWN;
  1163. ObpUnlockDeviceMap();
  1164. SymbolicLink->DosDeviceDriveIndex = 0;
  1165. }
  1166. //
  1167. // N.B. The original code freed here the target buffer. This is
  1168. // illegal because the parse routine for symbolic links reads the buffer unsynchronized
  1169. // The buffer will be released in the delete procedure, when the sym link goes away
  1170. //
  1171. }
  1172. ObpReleaseLookupContext(&LookupContext);
  1173. return;
  1174. }