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.

747 lines
23 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. SafeCann.c (WinSAFER Filename Canonicalization)
  5. Abstract:
  6. This module implements the WinSAFER APIs that produce canonicalized
  7. filenames from a caller-supplied .
  8. Author:
  9. Jeffrey Lawson (JLawson) - Nov 1999
  10. Environment:
  11. User mode only.
  12. Exported Functions:
  13. CodeAuthzFullyQualifyFilename
  14. Revision History:
  15. Created - Nov 2000
  16. --*/
  17. #include "pch.h"
  18. #pragma hdrstop
  19. #include <winsafer.h>
  20. #include <winsaferp.h>
  21. #include "saferp.h"
  22. //
  23. // Defines the maximum recursion depth that will be used when attempting
  24. // to resolve the final mapping of SUBST'ed drives. For worst-case, value
  25. // shouldn't be greater than 26 (the number of possible drive letters).
  26. //
  27. #define MAX_RECURSE_DRIVE_LETTER 10
  28. //
  29. // Some static name prefixes in the NT object namespace.
  30. //
  31. static const UNICODE_STRING UnicodeDeviceWinDfs =
  32. RTL_CONSTANT_STRING( L"\\Device\\WinDfs\\" );
  33. static const UNICODE_STRING UnicodeDeviceLanman =
  34. RTL_CONSTANT_STRING( L"\\Device\\LanmanRedirector\\" );
  35. static const UNICODE_STRING UnicodeDosDevicesUncPrefix =
  36. RTL_CONSTANT_STRING( L"\\??\\UNC\\" );
  37. static const UNICODE_STRING UnicodeDosDevicesPrefix =
  38. RTL_CONSTANT_STRING( L"\\??\\" );
  39. static const UNICODE_STRING UnicodeDevicePrefix =
  40. RTL_CONSTANT_STRING( L"\\Device\\" );
  41. static BOOLEAN FORCEINLINE
  42. SaferpIsAlphaLetter(
  43. IN WCHAR inwcletter
  44. )
  45. {
  46. #if 1
  47. if ((inwcletter >= L'A' && inwcletter <= L'Z') ||
  48. (inwcletter >= L'a' && inwcletter <= L'z'))
  49. return TRUE;
  50. else
  51. return FALSE;
  52. #else
  53. inwcletter = RtlUpcaseUnicodeChar(inwcletter);
  54. return (inwcletter >= L'A' && inwcletter <= 'Z') ? TRUE : FALSE;
  55. #endif
  56. }
  57. static BOOLEAN NTAPI
  58. SaferpQueryActualDriveLetterFromDriveLetter(
  59. IN WCHAR inDriveLetter,
  60. OUT WCHAR *outDriveLetter,
  61. IN SHORT MaxRecurseCount
  62. )
  63. /*++
  64. Routine Description:
  65. Attempts to determine if a specified drive letter is a SUBST'ed
  66. drive letter, a network mapped drive letter, or a physical drive
  67. letter. Unknown cases result in a failure.
  68. Arguments:
  69. inDriveLetter - Drive leter to obtain information about. This must
  70. be an alphabetic character.
  71. outDriveLetter - Receives the result of the evaluation and indicates
  72. what drive letter the requested one actually points to:
  73. --> If the drive letter is a SUBST'ed drive, then the result
  74. will be the drive letter of the original drive.
  75. --> If the drive letter is a network mapped drive, then the
  76. result will be UNICODE_NULL, indicating a network volume.
  77. --> If the drive letter is a local, physical drive, then
  78. the result will be the same as the input letter.
  79. MaxRecurseCount - used for limiting maximum recursion depth.
  80. Recommend specifying a reasonable positive value.
  81. Return Value:
  82. Returns TRUE on successful operation, FALSE if the determination
  83. could not be made.
  84. --*/
  85. {
  86. NTSTATUS Status;
  87. HANDLE LinkHandle;
  88. UNICODE_STRING UnicodeFileName;
  89. OBJECT_ATTRIBUTES Attributes;
  90. const WCHAR FileNameBuffer[7] = { L'\\', L'?', L'?', L'\\',
  91. inDriveLetter, L':', UNICODE_NULL };
  92. UNICODE_STRING LinkValue;
  93. WCHAR LinkValueBuffer[2*MAX_PATH];
  94. ULONG ReturnedLength;
  95. //
  96. // Require that the input drive letter be alphabetic.
  97. //
  98. if (!SaferpIsAlphaLetter(inDriveLetter)) {
  99. // Input drive letter was not uppercase alphabetic.
  100. return FALSE;
  101. }
  102. //
  103. // Open a reference to see if there are any links.
  104. //
  105. RtlInitUnicodeString(&UnicodeFileName, FileNameBuffer);
  106. InitializeObjectAttributes(&Attributes, &UnicodeFileName,
  107. OBJ_CASE_INSENSITIVE, NULL, NULL);
  108. Status = NtOpenSymbolicLinkObject (&LinkHandle,
  109. SYMBOLIC_LINK_QUERY,
  110. &Attributes);
  111. if (!NT_SUCCESS(Status)) {
  112. // Unable to open the drive letter so it must not exist.
  113. return FALSE;
  114. }
  115. //
  116. // Now query the link and see if there is a redirection
  117. //
  118. LinkValue.Buffer = LinkValueBuffer;
  119. LinkValue.Length = 0;
  120. LinkValue.MaximumLength = (USHORT)(sizeof(LinkValueBuffer));
  121. ReturnedLength = 0;
  122. Status = NtQuerySymbolicLinkObject( LinkHandle,
  123. &LinkValue,
  124. &ReturnedLength
  125. );
  126. NtClose( LinkHandle );
  127. if (!NT_SUCCESS(Status)) {
  128. // Could not retrieve final link destination.
  129. return FALSE;
  130. }
  131. //
  132. // Analyze the resulting link destination and extract the
  133. // actual destination drive letter or network path.
  134. //
  135. if (RtlPrefixUnicodeString(
  136. (PUNICODE_STRING) &UnicodeDeviceWinDfs,
  137. &LinkValue, TRUE) ||
  138. RtlPrefixUnicodeString(
  139. (PUNICODE_STRING) &UnicodeDeviceLanman,
  140. &LinkValue, TRUE) ||
  141. RtlPrefixUnicodeString(
  142. (PUNICODE_STRING) &UnicodeDosDevicesUncPrefix,
  143. &LinkValue, TRUE))
  144. // Note: Other network redirectors (Netware, NFS, etc) will not be known as such.
  145. // Maybe there is a way to query if a device is a "network redirector"?
  146. {
  147. // This is a network volume.
  148. *outDriveLetter = UNICODE_NULL;
  149. return TRUE;
  150. }
  151. else if (RtlPrefixUnicodeString(
  152. (PUNICODE_STRING) &UnicodeDosDevicesPrefix,
  153. &LinkValue, TRUE) &&
  154. LinkValue.Length >= 6 * sizeof(WCHAR) &&
  155. LinkValue.Buffer[5] == L':' &&
  156. SaferpIsAlphaLetter(LinkValue.Buffer[4]))
  157. {
  158. // This is a SUBST'ed drive letter.
  159. // We need to recurse, since you can SUBST multiple times,
  160. // or SUBST a network mapped drive to a second drive letter.
  161. if (MaxRecurseCount > 0) {
  162. // Tail recursion here would be nice.
  163. return SaferpQueryActualDriveLetterFromDriveLetter(
  164. LinkValue.Buffer[4], outDriveLetter, MaxRecurseCount - 1);
  165. }
  166. return FALSE;
  167. }
  168. else if (RtlPrefixUnicodeString(
  169. (PUNICODE_STRING) &UnicodeDevicePrefix,
  170. &LinkValue, TRUE))
  171. {
  172. // Otherwise this drive letter is an actual device and is
  173. // apparently its own identity. However, network redirectors
  174. // that we did not know about will also fall into this bucket.
  175. *outDriveLetter = inDriveLetter;
  176. return TRUE;
  177. } else {
  178. // Otherwise we don't know what it is.
  179. return FALSE;
  180. }
  181. }
  182. static BOOLEAN NTAPI
  183. SaferpQueryCanonicalizedDriveLetterFromDosPathname(
  184. IN LPCWSTR szDosPathname,
  185. OUT WCHAR *wcDriveLetter
  186. )
  187. /*++
  188. Routine Description:
  189. Arguments:
  190. Return Value:
  191. --*/
  192. {
  193. RTL_PATH_TYPE PathType;
  194. //
  195. // Verify input arguments were supplied.
  196. //
  197. if (!ARGUMENT_PRESENT(szDosPathname) ||
  198. !ARGUMENT_PRESENT(wcDriveLetter)) {
  199. return FALSE;
  200. }
  201. //
  202. // Determine what syntax this DOS pathname was supplied to us as.
  203. //
  204. PathType = RtlDetermineDosPathNameType_U(szDosPathname);
  205. switch (PathType) {
  206. case RtlPathTypeUncAbsolute:
  207. // definitely a network volume.
  208. *wcDriveLetter = UNICODE_NULL;
  209. return TRUE;
  210. case RtlPathTypeDriveAbsolute:
  211. case RtlPathTypeDriveRelative:
  212. // explicitly specified drive letter, but need to handle subst or network mapped.
  213. {
  214. WCHAR CurDrive = RtlUpcaseUnicodeChar( szDosPathname[0] );
  215. if (SaferpQueryActualDriveLetterFromDriveLetter(
  216. CurDrive, wcDriveLetter, MAX_RECURSE_DRIVE_LETTER)) {
  217. return TRUE;
  218. }
  219. break;
  220. }
  221. case RtlPathTypeRooted:
  222. case RtlPathTypeRelative:
  223. // relative to current drive, but still need to handle subst or network mapped.
  224. {
  225. PCURDIR CurDir;
  226. WCHAR CurDrive;
  227. CurDir = &(NtCurrentPeb()->ProcessParameters->CurrentDirectory);
  228. CurDrive = RtlUpcaseUnicodeChar( CurDir->DosPath.Buffer[0] );
  229. if (SaferpQueryActualDriveLetterFromDriveLetter(
  230. CurDrive, wcDriveLetter, MAX_RECURSE_DRIVE_LETTER)) {
  231. return TRUE;
  232. }
  233. break;
  234. }
  235. // Everything else gets rejected:
  236. // RtlPathTypeUnknown
  237. // RtlPathTypeLocalDevice
  238. // RtlPathTypeRootLocalDevice
  239. }
  240. return FALSE;
  241. }
  242. static BOOLEAN NTAPI
  243. SaferpQueryCanonicalizedDriveLetterFromNtPathname(
  244. IN LPCWSTR szNtPathname,
  245. OUT WCHAR *wcDriveLetter
  246. )
  247. /*++
  248. Routine Description:
  249. Arguments:
  250. Return Value:
  251. --*/
  252. {
  253. UNICODE_STRING LinkValue;
  254. RtlInitUnicodeString(&LinkValue, szNtPathname);
  255. //
  256. // Analyze the resulting link destination and extract the
  257. // actual destination drive letter or network path.
  258. //
  259. if (RtlPrefixUnicodeString(
  260. (PUNICODE_STRING) &UnicodeDeviceWinDfs,
  261. &LinkValue, TRUE) ||
  262. RtlPrefixUnicodeString(
  263. (PUNICODE_STRING) &UnicodeDeviceLanman,
  264. &LinkValue, TRUE) ||
  265. RtlPrefixUnicodeString(
  266. (PUNICODE_STRING) &UnicodeDosDevicesUncPrefix,
  267. &LinkValue, TRUE))
  268. // Note: Other network redirectors (Netware, NFS, etc) will not be known as such.
  269. // Maybe there is a way to query if a device is a "network redirector"?
  270. {
  271. // This is a network volume.
  272. *wcDriveLetter = UNICODE_NULL;
  273. return TRUE;
  274. }
  275. else if (RtlPrefixUnicodeString(
  276. (PUNICODE_STRING) &UnicodeDosDevicesPrefix,
  277. &LinkValue, TRUE) &&
  278. LinkValue.Length >= 6 * sizeof(WCHAR) &&
  279. LinkValue.Buffer[5] == L':' &&
  280. SaferpIsAlphaLetter(LinkValue.Buffer[4]))
  281. {
  282. // This is a SUBST'ed drive letter.
  283. // We need to recurse, since you can SUBST multiple times,
  284. // or SUBST a network mapped drive to a second drive letter.
  285. return SaferpQueryActualDriveLetterFromDriveLetter(
  286. LinkValue.Buffer[4], wcDriveLetter, MAX_RECURSE_DRIVE_LETTER);
  287. }
  288. else {
  289. // Otherwise we don't know what it is.
  290. return FALSE;
  291. }
  292. }
  293. static NTSTATUS NTAPI
  294. SaferpQueryFilenameFromHandle(
  295. IN HANDLE hFileHandle,
  296. IN WCHAR wcDriveLetter,
  297. OUT PUNICODE_STRING pUnicodeOutput
  298. )
  299. /*++
  300. Routine Description:
  301. Attempts to determine the fully qualified, canonicalized long
  302. filename version of the file associated with a given file handle.
  303. Note that the behavior provided by this function is a frequently
  304. requested API by Win32 developers because this information is
  305. normally not available by any other way through documented
  306. Win32 API calls. However, even this implementation is not able to
  307. generally satisfy the general case very well due the limited access
  308. to the full path information from user-mode.
  309. Arguments:
  310. hFileHandle -
  311. wcDriveLetter -
  312. pUnicodeOutput - Canonicalized DOS namespace filename, or
  313. potentially a UNC network path.
  314. Return Value:
  315. Returns STATUS_SUCCESS on successful completion, otherwise an error code.
  316. --*/
  317. {
  318. NTSTATUS Status;
  319. IO_STATUS_BLOCK IoStatusBlock;
  320. PWCHAR szLongFileNameBuffer = NULL;
  321. PBYTE FileNameInfoBuffer = NULL;
  322. PFILE_NAME_INFORMATION pFileNameInfo = NULL;
  323. DWORD Size = (sizeof(WCHAR) * MAX_PATH) + sizeof(FILE_NAME_INFORMATION);
  324. PUCHAR Buffer = NULL;
  325. UNICODE_STRING UnicodeFileName = {0};
  326. Buffer = (PUCHAR) RtlAllocateHeap(RtlProcessHeap(), 0, Size);
  327. if (Buffer == NULL)
  328. {
  329. return STATUS_NO_MEMORY;
  330. }
  331. pFileNameInfo = (PFILE_NAME_INFORMATION) Buffer;
  332. //
  333. // Query the full path and filename (minus the drive letter).
  334. //
  335. Status = NtQueryInformationFile(
  336. hFileHandle,
  337. &IoStatusBlock,
  338. pFileNameInfo,
  339. Size,
  340. FileNameInformation);
  341. if (!NT_SUCCESS(Status)) {
  342. goto Cleanup;
  343. }
  344. //
  345. // Initialize the UNICODE_STRING reference to the output string.
  346. //
  347. UnicodeFileName.Buffer = pFileNameInfo->FileName;
  348. UnicodeFileName.Length = (USHORT) pFileNameInfo->FileNameLength;
  349. UnicodeFileName.MaximumLength = (USHORT) (sizeof(WCHAR) * MAX_PATH);
  350. ASSERT(UnicodeFileName.Length <= UnicodeFileName.MaximumLength);
  351. //
  352. // Perform some additional fixups depending upon whether we
  353. // were told that the file eventually comes from a local drive
  354. // letter or a network/dfs share.
  355. //
  356. if (wcDriveLetter == UNICODE_NULL)
  357. {
  358. // Ensure there is room for one more character.
  359. if (UnicodeFileName.Length + sizeof(WCHAR) >
  360. UnicodeFileName.MaximumLength) {
  361. Status = STATUS_BUFFER_OVERFLOW;
  362. goto Cleanup;
  363. }
  364. // We've been told that this comes from a network volume,
  365. // so we need to prepend another backslash to the front.
  366. RtlMoveMemory(&UnicodeFileName.Buffer[1],
  367. &UnicodeFileName.Buffer[0],
  368. UnicodeFileName.Length);
  369. ASSERT(UnicodeFileName.Buffer[0] == L'\\' &&
  370. UnicodeFileName.Buffer[1] == L'\\');
  371. UnicodeFileName.Length += sizeof(WCHAR);
  372. }
  373. else if (SaferpIsAlphaLetter(wcDriveLetter))
  374. {
  375. // Ensure there is room for two more characters.
  376. if (UnicodeFileName.Length + 2 * sizeof(WCHAR) >
  377. UnicodeFileName.MaximumLength) {
  378. Status = STATUS_BUFFER_OVERFLOW;
  379. goto Cleanup;
  380. }
  381. // We've been told that this comes from a local drive.
  382. RtlMoveMemory(&UnicodeFileName.Buffer[2],
  383. &UnicodeFileName.Buffer[0],
  384. UnicodeFileName.Length);
  385. UnicodeFileName.Buffer[0] = RtlUpcaseUnicodeChar(wcDriveLetter);
  386. UnicodeFileName.Buffer[1] = L':';
  387. ASSERT(UnicodeFileName.Buffer[2] == L'\\');
  388. UnicodeFileName.Length += 2 * sizeof(WCHAR);
  389. }
  390. else {
  391. // Otherwise invalid input.
  392. Status = STATUS_INVALID_PARAMETER;
  393. goto Cleanup;
  394. }
  395. //
  396. // Make sure the string is NULL terminated
  397. //
  398. UnicodeFileName.Buffer[(UnicodeFileName.Length)/sizeof(WCHAR)] = L'\0';
  399. szLongFileNameBuffer = (PWCHAR) RtlAllocateHeap(RtlProcessHeap(), 0, (MAX_PATH * sizeof(WCHAR)));
  400. if (szLongFileNameBuffer == NULL)
  401. {
  402. Status = STATUS_NO_MEMORY;
  403. goto Cleanup;
  404. }
  405. szLongFileNameBuffer[0] = L'\0';
  406. if (GetLongPathNameW(UnicodeFileName.Buffer,
  407. szLongFileNameBuffer,
  408. MAX_PATH)) {
  409. RtlInitUnicodeString(&UnicodeFileName, szLongFileNameBuffer);
  410. }
  411. //
  412. // Duplicate the local string into a new memory buffer so we
  413. // can pass it back to the caller.
  414. Status = RtlDuplicateUnicodeString(
  415. RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
  416. &UnicodeFileName,
  417. pUnicodeOutput);
  418. Cleanup:
  419. if (Buffer != NULL)
  420. {
  421. RtlFreeHeap(RtlProcessHeap(), 0, Buffer);
  422. }
  423. if (szLongFileNameBuffer != NULL)
  424. {
  425. RtlFreeHeap(RtlProcessHeap(), 0, szLongFileNameBuffer);
  426. }
  427. return Status;
  428. }
  429. NTSTATUS NTAPI
  430. CodeAuthzFullyQualifyFilename(
  431. IN HANDLE hFileHandle OPTIONAL,
  432. IN BOOLEAN bSourceIsNtPath,
  433. IN LPCWSTR szSourceFilePath,
  434. OUT PUNICODE_STRING pUnicodeResult
  435. )
  436. /*++
  437. Routine Description:
  438. Attempts to return fully qualified, canonicalized filename using a
  439. caller-supplied filename and optionally an opened file handle.
  440. The method used by this function is significantly more reliable
  441. and consistent if an opened file handle can additionally be provided.
  442. Arguments:
  443. hFileHandle - optionally supplies the file handle to the file that
  444. is being canonicalized. The handle is used to obtain a more
  445. definitive canonicalization result.
  446. Unfortunately, since NT does not currently allow full information
  447. to be queried from strictly the file handle, the original filename
  448. used to open the file needs to also be supplied. No explicit
  449. verification is done to ensure that the supplied file handle
  450. actually corresponds with the filename that is also supplied.
  451. bSourceIsNtPath - boolean indicator of whether the filename being
  452. supplied is a DOS namespace or an NT-namespace filename.
  453. szSourceFilePath - string of the filename to canonicalize. This
  454. filename may either be a DOS or an NT-namespace filename.
  455. pUnicodeResult - output UNICODE_STRING structure that receives an
  456. allocated string of the resulting canonicalized path.
  457. The resulting path will always be a DOS namespace filename.
  458. Return Value:
  459. Returns STATUS_SUCCESS if successful, otherwise the error code.
  460. --*/
  461. {
  462. NTSTATUS Status = STATUS_UNSUCCESSFUL;
  463. PWCHAR FileBuffer = NULL;
  464. PWCHAR FileBufferTwo = NULL;
  465. if (ARGUMENT_PRESENT(hFileHandle) && ARGUMENT_PRESENT(szSourceFilePath))
  466. {
  467. //
  468. // When we are given a file handle, or are able to open
  469. // the file ourselves, use the handle to derive the full name.
  470. // First, determine the drive letter by looking at the supplied
  471. // file path itself. This step is necessary because the
  472. // NtQueryInformationFile API that we use later is unable to
  473. // supply the full prefix of the filename.
  474. //
  475. WCHAR wcDriveLetter;
  476. Status = STATUS_SUCCESS;
  477. if (bSourceIsNtPath) {
  478. if (!SaferpQueryCanonicalizedDriveLetterFromNtPathname(
  479. szSourceFilePath, &wcDriveLetter))
  480. Status = STATUS_UNSUCCESSFUL;
  481. } else {
  482. if (!SaferpQueryCanonicalizedDriveLetterFromDosPathname(
  483. szSourceFilePath, &wcDriveLetter))
  484. Status = STATUS_UNSUCCESSFUL;
  485. }
  486. if (NT_SUCCESS(Status)) {
  487. Status = SaferpQueryFilenameFromHandle(
  488. hFileHandle,
  489. wcDriveLetter,
  490. pUnicodeResult);
  491. if (NT_SUCCESS(Status)) return Status;
  492. }
  493. }
  494. if (szSourceFilePath != NULL)
  495. {
  496. //
  497. // Allow the case where a pathname was supplied, but not a
  498. // handle and we were unable to open the file. This case
  499. // will not be very common, so it can be less efficient.
  500. //
  501. UNICODE_STRING UnicodeInput;
  502. //
  503. // Transform the name into a fully qualified name.
  504. //
  505. RtlInitUnicodeString(&UnicodeInput, szSourceFilePath);
  506. if ( bSourceIsNtPath )
  507. {
  508. if (RtlPrefixUnicodeString(
  509. (PUNICODE_STRING) &UnicodeDosDevicesPrefix,
  510. &UnicodeInput, TRUE) &&
  511. UnicodeInput.Length >= 6 * sizeof(WCHAR) &&
  512. UnicodeInput.Buffer[5] == L':' &&
  513. SaferpIsAlphaLetter(UnicodeInput.Buffer[4]) &&
  514. UnicodeInput.Buffer[6] == L'\\')
  515. {
  516. // Absolute NT style filename, and assumed to already be
  517. // fully-qualified. Since we want the DOS-namespace,
  518. // the leading NT prefix stuff needs to be chopped.
  519. UnicodeInput.Buffer = &UnicodeInput.Buffer[4];
  520. UnicodeInput.Length -= (4 * sizeof(WCHAR));
  521. Status = STATUS_SUCCESS;
  522. } else {
  523. Status = STATUS_UNSUCCESSFUL;
  524. }
  525. } else {
  526. // Need to possibly fully qualify the path first.
  527. ULONG ulResult;
  528. FileBufferTwo = (PWCHAR) RtlAllocateHeap(RtlProcessHeap(), 0, (MAX_PATH * sizeof(WCHAR)));
  529. if (FileBufferTwo == NULL)
  530. {
  531. Status = STATUS_NO_MEMORY;
  532. goto Cleanup;
  533. }
  534. ulResult = RtlGetFullPathName_U(
  535. UnicodeInput.Buffer,
  536. (MAX_PATH * sizeof(WCHAR)), // yes, BYTEs not WCHARs!
  537. FileBufferTwo,
  538. NULL);
  539. if (ulResult != 0 && ulResult < (MAX_PATH * sizeof(WCHAR))) {
  540. UnicodeInput.Buffer = FileBufferTwo;
  541. UnicodeInput.Length = (USHORT) ulResult;
  542. UnicodeInput.MaximumLength = MAX_PATH * sizeof(WCHAR);
  543. Status = STATUS_SUCCESS;
  544. } else {
  545. Status = STATUS_UNSUCCESSFUL;
  546. }
  547. }
  548. //
  549. // Convert any short 8.3 filenames to their full versions.
  550. //
  551. if (!NT_SUCCESS(Status))
  552. {
  553. goto Cleanup;
  554. }
  555. FileBuffer = (PWCHAR) RtlAllocateHeap(RtlProcessHeap(), 0, (MAX_PATH * sizeof(WCHAR)));
  556. if (FileBuffer == NULL)
  557. {
  558. Status = STATUS_NO_MEMORY;
  559. goto Cleanup;
  560. }
  561. if (!GetLongPathNameW(UnicodeInput.Buffer,
  562. FileBuffer,
  563. MAX_PATH)) {
  564. // duplicate UnicodeInput into identStruct.UnicodeFullyQualfiedLongFileName
  565. Status = RtlDuplicateUnicodeString(
  566. RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE |
  567. RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING,
  568. &UnicodeInput,
  569. pUnicodeResult);
  570. } else {
  571. // Conversion was possible, so just return an
  572. // allocated copy of what we were able to find.
  573. // This can happen when the file path doesn't exist.
  574. Status = RtlCreateUnicodeString(
  575. pUnicodeResult,
  576. FileBuffer);
  577. }
  578. if (NT_SUCCESS(Status))
  579. {
  580. goto Cleanup;
  581. }
  582. }
  583. Cleanup:
  584. if (FileBuffer != NULL)
  585. {
  586. RtlFreeHeap(RtlProcessHeap(), 0, FileBuffer);
  587. }
  588. if (FileBufferTwo != NULL)
  589. {
  590. RtlFreeHeap(RtlProcessHeap(), 0, FileBufferTwo);
  591. }
  592. return Status;
  593. }