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.

3406 lines
114 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. cmparse.c
  5. Abstract:
  6. This module contains parse routines for the configuration manager, particularly
  7. the registry.
  8. Author:
  9. Bryan M. Willman (bryanwi) 10-Sep-1991
  10. Revision History:
  11. --*/
  12. #include "cmp.h"
  13. #ifdef ALLOC_DATA_PRAGMA
  14. #pragma const_seg("PAGECONST")
  15. #endif
  16. const ULONG CmpCacheOnFlag = CM_CACHE_FAKE_KEY;
  17. extern PCMHIVE CmpMasterHive;
  18. extern BOOLEAN CmpNoMasterCreates;
  19. extern PCM_KEY_CONTROL_BLOCK CmpKeyControlBlockRoot;
  20. extern UNICODE_STRING CmSymbolicLinkValueName;
  21. #define CM_HASH_STACK_SIZE 30
  22. typedef struct _CM_HASH_ENTRY {
  23. ULONG ConvKey;
  24. UNICODE_STRING KeyName;
  25. } CM_HASH_ENTRY, *PCM_HASH_ENTRY;
  26. ULONG
  27. CmpComputeHashValue(
  28. IN PCM_HASH_ENTRY HashStack,
  29. IN OUT ULONG *TotalSubkeys,
  30. IN ULONG BaseConvKey,
  31. IN PUNICODE_STRING RemainingName
  32. );
  33. NTSTATUS
  34. CmpCacheLookup(
  35. IN PCM_HASH_ENTRY HashStack,
  36. IN ULONG TotalRemainingSubkeys,
  37. OUT ULONG *MatchRemainSubkeyLevel,
  38. IN OUT PCM_KEY_CONTROL_BLOCK *Kcb,
  39. OUT PUNICODE_STRING RemainingName,
  40. OUT PHHIVE *Hive,
  41. OUT HCELL_INDEX *Cell
  42. );
  43. VOID
  44. CmpCacheAdd(
  45. IN PCM_HASH_ENTRY LastHashEntry,
  46. IN ULONG Count
  47. );
  48. PCM_KEY_CONTROL_BLOCK
  49. CmpAddInfoAfterParseFailure(
  50. PHHIVE Hive,
  51. HCELL_INDEX Cell,
  52. PCM_KEY_NODE Node,
  53. PCM_KEY_CONTROL_BLOCK kcb,
  54. PUNICODE_STRING NodeName
  55. );
  56. //
  57. // Prototypes for procedures private to this file
  58. //
  59. BOOLEAN
  60. CmpGetSymbolicLink(
  61. IN PHHIVE Hive,
  62. IN OUT PUNICODE_STRING ObjectName,
  63. IN OUT PCM_KEY_CONTROL_BLOCK SymbolicKcb,
  64. IN PUNICODE_STRING RemainingName
  65. );
  66. NTSTATUS
  67. CmpDoOpen(
  68. IN PHHIVE Hive,
  69. IN HCELL_INDEX Cell,
  70. IN PCM_KEY_NODE Node,
  71. IN PACCESS_STATE AccessState,
  72. IN KPROCESSOR_MODE AccessMode,
  73. IN ULONG Attributes,
  74. IN PCM_PARSE_CONTEXT Context,
  75. IN BOOLEAN CompleteKeyCached,
  76. IN OUT PCM_KEY_CONTROL_BLOCK *CachedKcb,
  77. IN PUNICODE_STRING KeyName,
  78. OUT PVOID *Object
  79. );
  80. NTSTATUS
  81. CmpCreateLinkNode(
  82. IN PHHIVE Hive,
  83. IN HCELL_INDEX Cell,
  84. IN PACCESS_STATE AccessState,
  85. IN UNICODE_STRING Name,
  86. IN KPROCESSOR_MODE AccessMode,
  87. IN ULONG Attributes,
  88. IN PCM_PARSE_CONTEXT Context,
  89. IN PCM_KEY_CONTROL_BLOCK ParentKcb,
  90. OUT PVOID *Object
  91. );
  92. #ifdef CM_DYN_SYM_LINK
  93. BOOLEAN
  94. CmpCaptureProcessEnvironmentString(
  95. OUT PWSTR *ProcessEnvironment,
  96. OUT PULONG Length
  97. );
  98. PWSTR
  99. CmpExpandEnvVars(
  100. IN PWSTR StringToExpand,
  101. IN ULONG LengthToExpand,
  102. OUT PULONG ExpandedLength
  103. );
  104. BOOLEAN
  105. CmpGrowAndCopyString(
  106. IN OUT PWSTR *OldString,
  107. IN OUT PULONG OldStringSize,
  108. IN ULONG GrowIncrements
  109. );
  110. BOOLEAN
  111. CmpFindEnvVar(
  112. IN PWSTR ProcessEnv,
  113. IN ULONG ProcessEnvLength,
  114. IN PWSTR CurrentEnvVar,
  115. IN ULONG CurrentEnvLength,
  116. OUT PWSTR *CurrentEnvValue,
  117. OUT PULONG CurrentEnvValueLength
  118. );
  119. #endif
  120. #ifdef ALLOC_PRAGMA
  121. #pragma alloc_text(PAGE,CmpParseKey)
  122. #pragma alloc_text(PAGE,CmpGetNextName)
  123. #pragma alloc_text(PAGE,CmpDoOpen)
  124. #pragma alloc_text(PAGE,CmpCreateLinkNode)
  125. #pragma alloc_text(PAGE,CmpGetSymbolicLink)
  126. #pragma alloc_text(PAGE,CmpComputeHashValue)
  127. #pragma alloc_text(PAGE,CmpCacheLookup)
  128. #pragma alloc_text(PAGE,CmpAddInfoAfterParseFailure)
  129. #ifdef CM_DYN_SYM_LINK
  130. #pragma alloc_text(PAGE,CmpCaptureProcessEnvironmentString)
  131. #pragma alloc_text(PAGE,CmpExpandEnvVars)
  132. #pragma alloc_text(PAGE,CmpGrowAndCopyString)
  133. #pragma alloc_text(PAGE,CmpFindEnvVar)
  134. #endif //CM_DYN_SYM_LINK
  135. #endif
  136. /*
  137. VOID
  138. CmpStepThroughExit(
  139. IN OUT PHHIVE *Hive,
  140. IN OUT HCELL_INDEX *Cell,
  141. IN OUT PCM_KEY_NODE *pNode
  142. )
  143. */
  144. #define CmpStepThroughExit(h,c,n,ReleaseHive,ReleaseCell) \
  145. if ((n)->Flags & KEY_HIVE_EXIT) { \
  146. if( ReleaseCell != HCELL_NIL ) { \
  147. ASSERT( ReleaseHive != NULL ); \
  148. HvReleaseCell( ReleaseHive,ReleaseCell); \
  149. } \
  150. (h)=(n)->ChildHiveReference.KeyHive; \
  151. (c)=(n)->ChildHiveReference.KeyCell; \
  152. (n)=(PCM_KEY_NODE)HvGetCell((h),(c)); \
  153. if( (n) == NULL ) { \
  154. ReleaseHive = NULL; \
  155. ReleaseCell = HCELL_NIL; \
  156. } else { \
  157. ReleaseHive = (h); \
  158. ReleaseCell = (c); \
  159. } \
  160. }
  161. #define CmpReleasePreviousAndHookNew(NewHive,NewCell,ReleaseHive,ReleaseCell) \
  162. if( ReleaseCell != HCELL_NIL ) { \
  163. ASSERT( ReleaseHive != NULL ); \
  164. HvReleaseCell( ReleaseHive,ReleaseCell); \
  165. } \
  166. ReleaseHive = (NewHive); \
  167. ReleaseCell = (NewCell)
  168. #define CMP_PARSE_GOTO_NONE 0
  169. #define CMP_PARSE_GOTO_CREATE 1
  170. #define CMP_PARSE_GOTO_RETURN 2
  171. #define CMP_PARSE_GOTO_RETURN2 3
  172. #ifdef CMP_STATS
  173. extern BOOLEAN CmpNtFakeCreateStarted;
  174. extern ULONG CmpNtFakeCreate;
  175. #endif
  176. NTSTATUS
  177. CmpParseKey(
  178. IN PVOID ParseObject,
  179. IN PVOID ObjectType,
  180. IN OUT PACCESS_STATE AccessState,
  181. IN KPROCESSOR_MODE AccessMode,
  182. IN ULONG Attributes,
  183. IN OUT PUNICODE_STRING CompleteName,
  184. IN OUT PUNICODE_STRING RemainingName,
  185. IN OUT PVOID Context OPTIONAL,
  186. IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
  187. OUT PVOID *Object
  188. )
  189. /*++
  190. Routine Description:
  191. This routine interfaces to the NT Object Manager. It is invoked when
  192. the object system is given the name of an entity to create or open and
  193. a Key or KeyRoot is encountered in the path. In practice this means
  194. that this routine is called for all objects whose names are of the
  195. form \REGISTRY\...
  196. This routine will create a Key object, which is effectively an open
  197. instance to a registry key node, and return its address
  198. (for the success case.)
  199. Arguments:
  200. ParseObject - Pointer to a KeyRoot or Key, thus -> KEY_BODY.
  201. ObjectType - Type of the object being opened.
  202. AccessState - Running security access state information for operation.
  203. AccessMode - Access mode of the original caller.
  204. Attributes - Attributes to be applied to the object.
  205. CompleteName - Supplies complete name of the object.
  206. RemainingName - Remaining name of the object.
  207. Context - if create or hive root open, points to a CM_PARSE_CONTEXT
  208. structure,
  209. if open, is NULL.
  210. SecurityQos - Optional security quality of service indicator.
  211. Object - The address of a variable to receive the created key object, if
  212. any.
  213. Return Value:
  214. The function return value is one of the following:
  215. a) Success - This indicates that the function succeeded and the object
  216. parameter contains the address of the created key object.
  217. b) STATUS_REPARSE - This indicates that a symbolic link key was
  218. found, and the path should be reparsed.
  219. c) Error - This indicates that the file was not found or created and
  220. no file object was created.
  221. --*/
  222. {
  223. NTSTATUS status;
  224. BOOLEAN rc;
  225. PHHIVE Hive;
  226. PCM_KEY_NODE Node = NULL;
  227. HCELL_INDEX Cell;
  228. HCELL_INDEX ParentCell;
  229. HCELL_INDEX NextCell;
  230. PHCELL_INDEX Index;
  231. PCM_PARSE_CONTEXT lcontext;
  232. UNICODE_STRING Current;
  233. UNICODE_STRING NextName; // Component last returned by CmpGetNextName,
  234. // will always be behind Current.
  235. BOOLEAN Last; // TRUE if component NextName points to
  236. // is the last one in the path.
  237. ULONG TotalRemainingSubkeys;
  238. ULONG MatchRemainSubkeyLevel;
  239. ULONG TotalSubkeys=0;
  240. PCM_KEY_CONTROL_BLOCK kcb;
  241. PCM_KEY_HASH PCmpCacheEntry=NULL;
  242. PCM_KEY_CONTROL_BLOCK ParentKcb;
  243. UNICODE_STRING TmpNodeName;
  244. ULONG namelength;
  245. ULONG GoToValue = CMP_PARSE_GOTO_NONE;
  246. BOOLEAN CompleteKeyCached = FALSE;
  247. USHORT i,j;
  248. WCHAR *p1;
  249. BOOLEAN ExclusiveLock = FALSE;
  250. PHHIVE HiveToRelease = NULL;
  251. HCELL_INDEX CellToRelease = HCELL_NIL;
  252. PAGED_CODE();
  253. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpParseKey:\n\t"));
  254. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CompleteName = '%wZ'\n\t", CompleteName));
  255. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"RemainingName = '%wZ'\n", RemainingName));
  256. //
  257. // Strip off any trailing path separators
  258. //
  259. while ((RemainingName->Length > 0) &&
  260. (RemainingName->Buffer[(RemainingName->Length/sizeof(WCHAR)) - 1] == OBJ_NAME_PATH_SEPARATOR)) {
  261. RemainingName->Length -= sizeof(WCHAR);
  262. }
  263. Current = *RemainingName;
  264. if (ObjectType != CmpKeyObjectType) {
  265. return STATUS_OBJECT_TYPE_MISMATCH;
  266. }
  267. lcontext = (PCM_PARSE_CONTEXT)Context;
  268. //
  269. // PreCreate callback
  270. //
  271. if ( CmAreCallbacksRegistered() ) {
  272. if( ARGUMENT_PRESENT(lcontext) ) {
  273. //
  274. // NtCreateKey
  275. //
  276. REG_PRE_CREATE_KEY_INFORMATION PreCreateInfo;
  277. PreCreateInfo.CompleteName = CompleteName;
  278. status = CmpCallCallBacks(RegNtPreCreateKey,&PreCreateInfo);
  279. } else {
  280. //
  281. // NtOpenKey
  282. //
  283. REG_PRE_OPEN_KEY_INFORMATION PreOpenInfo;
  284. PreOpenInfo.CompleteName = CompleteName;
  285. status = CmpCallCallBacks(RegNtPreOpenKey,&PreOpenInfo);
  286. }
  287. if( !NT_SUCCESS(status) ) {
  288. return status;
  289. }
  290. }
  291. BEGIN_LOCK_CHECKPOINT;
  292. //
  293. // we now lock it shared as 85% of the create calls are in fact opens
  294. // the lock will be aquired exclusively in CmpDoCreate/CmpCreateLinkNode
  295. //
  296. // We only lock the registry here, in the parse routine to reduce contention
  297. // on the registry lock (NO reason to wait on OB)
  298. //
  299. CmpLockRegistry();
  300. //CmpLockRegistryExclusive();
  301. //
  302. // Check to make sure the passed in root key is not marked for deletion.
  303. //
  304. if (((PCM_KEY_BODY)ParseObject)->KeyControlBlock->Delete == TRUE) {
  305. CmpUnlockRegistry();
  306. return(STATUS_KEY_DELETED);
  307. }
  308. //
  309. // Fetch the starting Hive.Cell. Because of the way the parse
  310. // paths work, this will always be defined. (ObOpenObjectByName
  311. // had to bounce off of a KeyObject or KeyRootObject to get here)
  312. //
  313. kcb = ((PCM_KEY_BODY)ParseObject)->KeyControlBlock;
  314. Hive = kcb->KeyHive;
  315. Cell = kcb->KeyCell;
  316. //
  317. // give back the stack after we don't need it anymore.
  318. //
  319. {
  320. CM_HASH_ENTRY HashStack[CM_HASH_STACK_SIZE];
  321. //
  322. // Compute the hash values of each subkeys
  323. //
  324. TotalRemainingSubkeys = CmpComputeHashValue(HashStack,
  325. &TotalSubkeys,
  326. kcb->ConvKey,
  327. &Current);
  328. PERFINFO_REG_PARSE(kcb, RemainingName);
  329. // Look up from the cache. kcb will be changed if we find a partial or exact match
  330. // PCmpCacheEntry, the entry found, will be moved to the front of
  331. // the Cache.
  332. BEGIN_KCB_LOCK_GUARD;
  333. CmpLockKCBTreeExclusive();
  334. status = CmpCacheLookup(HashStack,
  335. TotalRemainingSubkeys,
  336. &MatchRemainSubkeyLevel,
  337. &kcb,
  338. &Current,
  339. &Hive,
  340. &Cell);
  341. //
  342. // The RefCount of kcb was increased in the CmpCacheLookup process,
  343. // It is to protect it from being kicked out of cache.
  344. // Make sure we dereference it after we are done.
  345. //
  346. CmpUnlockKCBTree();
  347. END_KCB_LOCK_GUARD;
  348. }
  349. //
  350. // First make sure it is OK to proceed.
  351. //
  352. if (!NT_SUCCESS (status)) {
  353. goto JustReturn;
  354. }
  355. ParentKcb = kcb;
  356. if(TotalRemainingSubkeys == 0) {
  357. //
  358. // We REALLY don't want to mess with the cache code bellow
  359. // in this case (this could only happen if we called with
  360. // the lpSubkey = NULL )
  361. //
  362. CompleteKeyCached = TRUE;
  363. goto Found;
  364. }
  365. //
  366. // First check if there are further information in the cached kcb.
  367. //
  368. // The additional information can be
  369. // 1. This cached key is a fake key (CM_KCB_KEY_NON_EXIST), then either let it be created
  370. // or return STATUS_OBJECT_NAME_NOT_FOUND.
  371. // 2. The cached key is not the destination and it has no subkey (CM_KCB_NO_SUBKEY).
  372. // 3. The cached key is not the destination and it has
  373. // the first four characters of its subkeys. If the flag is CM_KCB_SUBKEY_ONE, there is only one subkey
  374. // and the four char is embedded in the KCB. If the flag is CM_KCB_SUBKEY_INFO, then there is
  375. // an allocation for these info.
  376. //
  377. // We do need to lock KCB tree to protect the KCB being modified. Currently there is not lock contention problem
  378. // on KCBs, We can change KCB lock to a read-write lock if this becomes a problem.
  379. //
  380. //
  381. // we did it; we changed the lock to a read-write resource
  382. //
  383. BEGIN_KCB_LOCK_GUARD;
  384. CmpLockKCBTree();
  385. if( FALSE ) {
  386. //
  387. // if we are here, we are in the position where we need to modify the KCB; therefore we need to aquire
  388. // the lock exclusive; after aquiring we test if the kcb is still valid (was not deleted in the meanwhile)
  389. //
  390. NeedExclusiveLock:
  391. CmpUnlockKCBTree();
  392. CmpLockKCBTreeExclusive();
  393. ExclusiveLock = TRUE;
  394. }
  395. if( kcb->Delete ) {
  396. //
  397. // kcb has been deleted while playing with the lock
  398. //
  399. status = STATUS_OBJECT_NAME_NOT_FOUND;
  400. CmpUnlockKCBTree();
  401. goto JustReturn;
  402. }
  403. if (kcb->ExtFlags & CM_KCB_CACHE_MASK) {
  404. if (MatchRemainSubkeyLevel == TotalRemainingSubkeys) {
  405. //
  406. // We have found a cache for the complete path,
  407. //
  408. if (kcb->ExtFlags & CM_KCB_KEY_NON_EXIST) {
  409. //
  410. // This key does not exist.
  411. //
  412. if (ARGUMENT_PRESENT(lcontext)) {
  413. ULONG LevelToSkip = TotalRemainingSubkeys-1;
  414. ULONG i=0;
  415. //
  416. // we need to change the kcb; get the lock exclusive (if noty already held) and try again
  417. //
  418. if( ExclusiveLock == FALSE ) {
  419. goto NeedExclusiveLock;
  420. }
  421. //
  422. // The non-existing key is the destination key and lcontext is present.
  423. // delete this fake kcb and let the real one be created.
  424. //
  425. // Temporarily increase the RefCount of the ParentKcb so it's
  426. // not removed while removing the fake and creating the real KCB.
  427. //
  428. ParentKcb = kcb->ParentKcb;
  429. if (CmpReferenceKeyControlBlock(ParentKcb)) {
  430. kcb->Delete = TRUE;
  431. CmpRemoveKeyControlBlock(kcb);
  432. CmpDereferenceKeyControlBlockWithLock(kcb);
  433. //
  434. // Update Hive, Cell and Node
  435. //
  436. Hive = ParentKcb->KeyHive;
  437. Cell = ParentKcb->KeyCell;
  438. Node = (PCM_KEY_NODE)HvGetCell(Hive,Cell);
  439. if( Node == NULL ) {
  440. //
  441. // we couldn't map the bin contianing this cell
  442. //
  443. CmpUnlockKCBTree();
  444. status = STATUS_INSUFFICIENT_RESOURCES;
  445. goto FreeAndReturn;
  446. }
  447. CmpReleasePreviousAndHookNew(Hive,Cell,HiveToRelease,CellToRelease);
  448. //
  449. // Now get the child name to be created.
  450. //
  451. NextName = *RemainingName;
  452. if ((NextName.Buffer == NULL) || (NextName.Length == 0)) {
  453. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"Something wrong in finding the child name\n"));
  454. DbgBreakPoint();
  455. }
  456. //
  457. // Skip over leading path separators
  458. //
  459. while (*(NextName.Buffer) == OBJ_NAME_PATH_SEPARATOR) {
  460. NextName.Buffer++;
  461. NextName.Length -= sizeof(WCHAR);
  462. NextName.MaximumLength -= sizeof(WCHAR);
  463. }
  464. while (i < LevelToSkip) {
  465. if (*(NextName.Buffer) == OBJ_NAME_PATH_SEPARATOR) {
  466. i++;
  467. while (*(NextName.Buffer) == OBJ_NAME_PATH_SEPARATOR) {
  468. NextName.Buffer++;
  469. NextName.Length -= sizeof(WCHAR);
  470. NextName.MaximumLength -= sizeof(WCHAR);
  471. }
  472. } else {
  473. NextName.Buffer++;
  474. NextName.Length -= sizeof(WCHAR);
  475. NextName.MaximumLength -= sizeof(WCHAR);
  476. }
  477. }
  478. GoToValue = CMP_PARSE_GOTO_CREATE;
  479. } else {
  480. //
  481. // We have maxed the RefCount of ParentKcb; treate it as key cannot be created.
  482. // The ParentKcb will not be dereferenced at the end.
  483. //
  484. status = STATUS_INSUFFICIENT_RESOURCES;
  485. GoToValue = CMP_PARSE_GOTO_RETURN2;
  486. }
  487. } else {
  488. status = STATUS_OBJECT_NAME_NOT_FOUND;
  489. GoToValue = CMP_PARSE_GOTO_RETURN;
  490. }
  491. }
  492. } else if (kcb->ExtFlags & CM_KCB_KEY_NON_EXIST) {
  493. //
  494. // one subkey (not destination) in the path does not exist. no point to continue.
  495. //
  496. status = STATUS_OBJECT_NAME_NOT_FOUND;
  497. GoToValue = CMP_PARSE_GOTO_RETURN;
  498. } else if (kcb->ExtFlags & CM_KCB_NO_SUBKEY) {
  499. //
  500. // one parent in the path has no subkey. see if it is a create.
  501. //
  502. if (((TotalRemainingSubkeys - MatchRemainSubkeyLevel) == 1) && (ARGUMENT_PRESENT(lcontext))) {
  503. //
  504. // Now we are going to create this subkey.
  505. // The kcb cache will be updated in CmpDoCreate routine.
  506. //
  507. } else {
  508. status = STATUS_OBJECT_NAME_NOT_FOUND;
  509. GoToValue = CMP_PARSE_GOTO_RETURN;
  510. }
  511. } else {
  512. //
  513. // We have a partial match. Current is the remaining name to be parsed.
  514. // The Key has either one or a few subkeys and has index hint. check if it is the candidate.
  515. //
  516. BOOLEAN NoMatch = TRUE;
  517. ULONG NextHashKey;
  518. PULONG TempHashKey;
  519. ULONG HintCounts;
  520. ULONG CmpCount;
  521. //
  522. // When NoMatch is TRUE, we know for sure there is no subkey that can match.
  523. // When NoMatch is FALSE, it can we either we found a match or
  524. // there is not enough information. Either case, we need to continue
  525. // the parse.
  526. //
  527. TmpNodeName = Current;
  528. rc = CmpGetNextName(&TmpNodeName, &NextName, &Last);
  529. NextHashKey = CmpComputeHashKey(&NextName);
  530. if (kcb->ExtFlags & CM_KCB_SUBKEY_ONE) {
  531. HintCounts = 1;
  532. TempHashKey = &(kcb->HashKey);
  533. } else {
  534. //
  535. // More than one child, the hint info in not inside the kcb but pointed by kcb.
  536. //
  537. HintCounts = kcb->IndexHint->Count;
  538. TempHashKey = &(kcb->IndexHint->HashKey[0]);
  539. }
  540. for (CmpCount=0; CmpCount<HintCounts; CmpCount++) {
  541. if( TempHashKey[CmpCount] == 0) {
  542. //
  543. // No hint available; assume the subkey exist and go on with the parse
  544. //
  545. //DbgPrint("KCB cache hit [0]\n");
  546. NoMatch = FALSE;
  547. break;
  548. }
  549. if( NextHashKey == TempHashKey[CmpCount] ) {
  550. //
  551. // There is a match.
  552. //
  553. //DbgPrint("KCB cache hit [1]\n");
  554. NoMatch = FALSE;
  555. break;
  556. }
  557. }
  558. if (NoMatch) {
  559. if (((TotalRemainingSubkeys - MatchRemainSubkeyLevel) == 1) && (ARGUMENT_PRESENT(lcontext))) {
  560. //
  561. // No we are going to create this subkey.
  562. // The kcb cache will be updated in CmpDoCreate.
  563. //
  564. } else {
  565. status = STATUS_OBJECT_NAME_NOT_FOUND;
  566. GoToValue = CMP_PARSE_GOTO_RETURN;
  567. }
  568. }
  569. }
  570. }
  571. CmpUnlockKCBTree();
  572. END_KCB_LOCK_GUARD;
  573. if (GoToValue == CMP_PARSE_GOTO_CREATE) {
  574. goto CreateChild;
  575. } else if (GoToValue == CMP_PARSE_GOTO_RETURN) {
  576. goto FreeAndReturn;
  577. } else if (GoToValue == CMP_PARSE_GOTO_RETURN2) {
  578. goto JustReturn;
  579. }
  580. if (MatchRemainSubkeyLevel) {
  581. // Found something, update the information to start the search
  582. // from the new BaseName
  583. if (MatchRemainSubkeyLevel == TotalSubkeys) {
  584. // The complete key has been found in the cache,
  585. // go directly to the CmpDoOpen.
  586. //
  587. // Found the whole thing cached.
  588. //
  589. //
  590. CompleteKeyCached = TRUE;
  591. goto Found;
  592. }
  593. ASSERT( (Cell == kcb->KeyCell) && (Hive == kcb->KeyHive) );
  594. }
  595. //
  596. // Check if we hit a symbolic link case
  597. //
  598. if (kcb->Flags & KEY_SYM_LINK) {
  599. //
  600. // The given key was a symbolic link. Find the name of
  601. // its link, and return STATUS_REPARSE to the Object Manager.
  602. //
  603. rc = CmpGetNextName(&Current, &NextName, &Last);
  604. Current.Buffer = NextName.Buffer;
  605. Current.Length += NextName.Length;
  606. Current.MaximumLength += NextName.MaximumLength;
  607. if (CmpGetSymbolicLink(Hive,
  608. CompleteName,
  609. kcb,
  610. &Current)) {
  611. status = STATUS_REPARSE;
  612. } else {
  613. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpParseKey: couldn't find symbolic link name\n"));
  614. status = STATUS_OBJECT_NAME_NOT_FOUND;
  615. }
  616. goto FreeAndReturn;
  617. }
  618. Node = (PCM_KEY_NODE)HvGetCell(Hive,Cell);
  619. if( Node == NULL ) {
  620. //
  621. // we couldn't map the bin contianing this cell
  622. //
  623. status = STATUS_INSUFFICIENT_RESOURCES;
  624. goto FreeAndReturn;
  625. }
  626. CmpReleasePreviousAndHookNew(Hive,Cell,HiveToRelease,CellToRelease);
  627. //
  628. // Parse the path.
  629. //
  630. status = STATUS_SUCCESS;
  631. while (TRUE) {
  632. //
  633. // Parse out next component of name
  634. //
  635. rc = CmpGetNextName(&Current, &NextName, &Last);
  636. if ((NextName.Length > 0) && (rc == TRUE)) {
  637. //
  638. // As we iterate through, we will create a kcb for each subkey parsed.
  639. //
  640. // Always use the information in kcb to avoid
  641. // touching registry data.
  642. //
  643. #ifdef CMP_KCB_CACHE_VALIDATION
  644. {
  645. PCM_KEY_NODE TempNode;
  646. TempNode = (PCM_KEY_NODE)HvGetCell(kcb->KeyHive,kcb->KeyCell);
  647. if( TempNode == NULL ) {
  648. //
  649. // we couldn't map the bin contianing this cell
  650. //
  651. status = STATUS_INSUFFICIENT_RESOURCES;
  652. break;
  653. }
  654. ASSERT( TempNode->Flags == kcb->Flags );
  655. HvReleaseCell(kcb->KeyHive,kcb->KeyCell);
  656. }
  657. #endif
  658. if (!(kcb->Flags & KEY_SYM_LINK)) {
  659. //
  660. // Got a legal name component, see if we can find a sub key
  661. // that actually has such a name.
  662. //
  663. NextCell = CmpFindSubKeyByName(Hive,
  664. Node,
  665. &NextName);
  666. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpParseKey:\n\t"));
  667. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"NextName = '%wZ'\n\t", &NextName));
  668. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"NextCell = %08lx Last = %01lx\n", NextCell, Last));
  669. if (NextCell != HCELL_NIL) {
  670. Cell = NextCell;
  671. Node = (PCM_KEY_NODE)HvGetCell(Hive,Cell);
  672. if( Node == NULL ) {
  673. //
  674. // we couldn't map the bin contianing this cell
  675. //
  676. status = STATUS_INSUFFICIENT_RESOURCES;
  677. break;
  678. }
  679. CmpReleasePreviousAndHookNew(Hive,Cell,HiveToRelease,CellToRelease);
  680. if (Last == TRUE) {
  681. Found:
  682. //
  683. // We will open the key regardless of whether the
  684. // call was open or create, so step through exit
  685. // portholes here.
  686. //
  687. if (CompleteKeyCached == TRUE) {
  688. //
  689. // If the key found is already cached,
  690. // do not need to StepThroughExit
  691. // (no kcb is created using exit node).
  692. // This prevents us from touching the key node just for the Flags.
  693. //
  694. } else {
  695. CmpStepThroughExit(Hive, Cell, Node,HiveToRelease,CellToRelease);
  696. if( Node == NULL ) {
  697. //
  698. // we couldn't map view for this cell
  699. //
  700. status = STATUS_INSUFFICIENT_RESOURCES;
  701. break;
  702. }
  703. }
  704. //
  705. // We have found the entire path, so we want to open
  706. // it (for both Open and Create calls).
  707. // Hive,Cell -> the key we are supposed to open.
  708. //
  709. #ifdef CMP_STATS
  710. if(CmpNtFakeCreateStarted == TRUE) {
  711. CmpNtFakeCreate++;
  712. }
  713. #endif
  714. status = CmpDoOpen(Hive,
  715. Cell,
  716. Node,
  717. AccessState,
  718. AccessMode,
  719. Attributes,
  720. lcontext,
  721. CompleteKeyCached,
  722. &kcb,
  723. &NextName,
  724. Object);
  725. if (status == STATUS_REPARSE) {
  726. //
  727. // The given key was a symbolic link. Find the name of
  728. // its link, and return STATUS_REPARSE to the Object Manager.
  729. //
  730. if (!CmpGetSymbolicLink(Hive,
  731. CompleteName,
  732. kcb,
  733. NULL)) {
  734. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpParseKey: couldn't find symbolic link name\n"));
  735. status = STATUS_OBJECT_NAME_NOT_FOUND;
  736. }
  737. }
  738. break;
  739. }
  740. // else
  741. // Not at end, so we'll simply iterate and consume
  742. // the next component.
  743. //
  744. //
  745. // Step through exit portholes here.
  746. // This ensures that no KCB is created using
  747. // the Exit node.
  748. //
  749. CmpStepThroughExit(Hive, Cell, Node,HiveToRelease,CellToRelease);
  750. if( Node == NULL ) {
  751. //
  752. // we couldn't map view for this cell
  753. //
  754. status = STATUS_INSUFFICIENT_RESOURCES;
  755. break;
  756. }
  757. //
  758. // Create a kcb for each subkey parsed.
  759. //
  760. kcb = CmpCreateKeyControlBlock(Hive,
  761. Cell,
  762. Node,
  763. ParentKcb,
  764. FALSE,
  765. &NextName);
  766. if (kcb == NULL) {
  767. status = STATUS_INSUFFICIENT_RESOURCES;
  768. goto FreeAndReturn;
  769. //
  770. // Currently, the kcb has one extra reference conut to be decremented.
  771. // Remember it so we can dereference it properly.
  772. //
  773. }
  774. //
  775. // Now we have created a kcb for the next level,
  776. // the kcb in the previous level is no longer needed.
  777. // Dereference the parent kcb.
  778. //
  779. CmpDereferenceKeyControlBlock(ParentKcb);
  780. ParentKcb = kcb;
  781. } else {
  782. //
  783. // We did not find a key matching the name, but no
  784. // unexpected error occured
  785. //
  786. if ((Last == TRUE) && (ARGUMENT_PRESENT(lcontext))) {
  787. CreateChild:
  788. //
  789. // Only unfound component is last one, and operation
  790. // is a create, so perform the create.
  791. //
  792. //
  793. // There are two possibilities here. The normal one
  794. // is that we are simply creating a new node.
  795. //
  796. // The abnormal one is that we are creating a root
  797. // node that is linked to the main hive. In this
  798. // case, we must create the link. Once the link is
  799. // created, we can check to see if the root node
  800. // exists, then either create it or open it as
  801. // necessary.
  802. //
  803. // CmpCreateLinkNode creates the link, and calls
  804. // back to CmpDoCreate or CmpDoOpen to create or open
  805. // the root node as appropriate.
  806. //
  807. //
  808. // either one of this will drop the reglock and reaquire it
  809. // exclusive; we need not to hurt ourselves, so release
  810. // all cells here
  811. //
  812. CmpReleasePreviousAndHookNew(NULL,HCELL_NIL,HiveToRelease,CellToRelease);
  813. if (lcontext->CreateLink) {
  814. status = CmpCreateLinkNode(Hive,
  815. Cell,
  816. AccessState,
  817. NextName,
  818. AccessMode,
  819. Attributes,
  820. lcontext,
  821. ParentKcb,
  822. Object);
  823. } else {
  824. if ( (Hive == &(CmpMasterHive->Hive)) &&
  825. (CmpNoMasterCreates == TRUE) ) {
  826. //
  827. // attempting to create a cell in the master
  828. // hive, and not a link, so blow out of here,
  829. // since it wouldn't work anyway.
  830. //
  831. status = STATUS_INVALID_PARAMETER;
  832. break;
  833. }
  834. status = CmpDoCreate(Hive,
  835. Cell,
  836. AccessState,
  837. &NextName,
  838. AccessMode,
  839. lcontext,
  840. ParentKcb,
  841. Object);
  842. }
  843. if( status == STATUS_REPARSE ) {
  844. //
  845. // somebody else created the key in between;
  846. // let the Object Manager work for us !!!
  847. // now we have the lock exclusive, so nothing can happen in between
  848. // next iterarion will find the key very quick
  849. //
  850. break;
  851. }
  852. lcontext->Disposition = REG_CREATED_NEW_KEY;
  853. break;
  854. } else {
  855. //
  856. // Did not find a key to match the component, and
  857. // are not at the end of the path. Thus, open must
  858. // fail because the whole path dosn't exist, create must
  859. // fail because more than 1 component doesn't exist.
  860. //
  861. //
  862. // We have a lookup failure here, so having additional information
  863. // about this kcb may help us not to go through all the code just to fail again.
  864. //
  865. ParentKcb = CmpAddInfoAfterParseFailure(Hive,
  866. Cell,
  867. Node,
  868. kcb,
  869. &NextName
  870. );
  871. if( ParentKcb == NULL ) {
  872. //
  873. // resource problem
  874. //
  875. status = STATUS_INSUFFICIENT_RESOURCES;
  876. } else {
  877. status = STATUS_OBJECT_NAME_NOT_FOUND;
  878. }
  879. break;
  880. }
  881. }
  882. } else {
  883. //
  884. // The given key was a symbolic link. Find the name of
  885. // its link, and return STATUS_REPARSE to the Object Manager.
  886. //
  887. Current.Buffer = NextName.Buffer;
  888. Current.Length += NextName.Length;
  889. Current.MaximumLength += NextName.MaximumLength;
  890. if (CmpGetSymbolicLink(Hive,
  891. CompleteName,
  892. kcb,
  893. &Current)) {
  894. status = STATUS_REPARSE;
  895. break;
  896. } else {
  897. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpParseKey: couldn't find symbolic link name\n"));
  898. status = STATUS_OBJECT_NAME_NOT_FOUND;
  899. break;
  900. }
  901. }
  902. } else if (rc == TRUE && Last == TRUE) {
  903. //
  904. // We will open the \Registry root.
  905. // Or some strange remaining name that
  906. // messes up the lookup.
  907. //
  908. CmpStepThroughExit(Hive, Cell, Node,HiveToRelease,CellToRelease);
  909. if( Node == NULL ) {
  910. //
  911. // we couldn't map view for this cell
  912. //
  913. status = STATUS_INSUFFICIENT_RESOURCES;
  914. break;
  915. }
  916. //
  917. // We have found the entire path, so we want to open
  918. // it (for both Open and Create calls).
  919. // Hive,Cell -> the key we are supposed to open.
  920. //
  921. status = CmpDoOpen(Hive,
  922. Cell,
  923. Node,
  924. AccessState,
  925. AccessMode,
  926. Attributes,
  927. lcontext,
  928. TRUE,
  929. &kcb,
  930. &NextName,
  931. Object);
  932. break;
  933. } else {
  934. //
  935. // bogus path -> fail
  936. //
  937. status = STATUS_INVALID_PARAMETER;
  938. break;
  939. }
  940. } // while
  941. FreeAndReturn:
  942. //
  943. // Now we have to free the last kcb that still has one extra reference count to
  944. // protect it from being freed.
  945. //
  946. if( ParentKcb != NULL ) {
  947. CmpDereferenceKeyControlBlock(ParentKcb);
  948. }
  949. JustReturn:
  950. CmpReleasePreviousAndHookNew(NULL,HCELL_NIL,HiveToRelease,CellToRelease);
  951. CmpUnlockRegistry();
  952. END_LOCK_CHECKPOINT;
  953. //
  954. // PostCreate callback
  955. //
  956. if ( CmAreCallbacksRegistered() ) {
  957. if( ARGUMENT_PRESENT(lcontext) ) {
  958. //
  959. // NtCreateKey
  960. //
  961. REG_POST_CREATE_KEY_INFORMATION PostCreateInfo;
  962. PostCreateInfo.CompleteName = CompleteName;
  963. PostCreateInfo.Object = Object;
  964. PostCreateInfo.Status = status;
  965. CmpCallCallBacks(RegNtPostCreateKey,&PostCreateInfo);
  966. } else {
  967. //
  968. // NtOpenKey
  969. //
  970. REG_POST_OPEN_KEY_INFORMATION PostOpenInfo;
  971. PostOpenInfo.CompleteName = CompleteName;
  972. PostOpenInfo.Object = Object;
  973. PostOpenInfo.Status = status;
  974. CmpCallCallBacks(RegNtPostOpenKey,&PostOpenInfo);
  975. }
  976. }
  977. return status;
  978. }
  979. BOOLEAN
  980. CmpGetNextName(
  981. IN OUT PUNICODE_STRING RemainingName,
  982. OUT PUNICODE_STRING NextName,
  983. OUT PBOOLEAN Last
  984. )
  985. /*++
  986. Routine Description:
  987. This routine parses off the next component of a registry path, returning
  988. all of the interesting state about it, including whether it's legal.
  989. Arguments:
  990. Current - supplies pointer to variable which points to path to parse.
  991. on input - parsing starts from here
  992. on output - updated to reflect starting position for next call.
  993. NextName - supplies pointer to a unicode_string, which will be set up
  994. to point into the parse string.
  995. Last - supplies a pointer to a boolean - set to TRUE if this is the
  996. last component of the name being parse, FALSE otherwise.
  997. Return Value:
  998. TRUE if all is well.
  999. FALSE if illegal name (too long component, bad character, etc.)
  1000. (if false, all out parameter values are bogus.)
  1001. --*/
  1002. {
  1003. BOOLEAN rc = TRUE;
  1004. //
  1005. // Deal with NULL paths, and pointers to NULL paths
  1006. //
  1007. if ((RemainingName->Buffer == NULL) || (RemainingName->Length == 0)) {
  1008. *Last = TRUE;
  1009. NextName->Buffer = NULL;
  1010. NextName->Length = 0;
  1011. return TRUE;
  1012. }
  1013. if (*(RemainingName->Buffer) == UNICODE_NULL) {
  1014. *Last = TRUE;
  1015. NextName->Buffer = NULL;
  1016. NextName->Length = 0;
  1017. return TRUE;
  1018. }
  1019. //
  1020. // Skip over leading path separators
  1021. //
  1022. if (*(RemainingName->Buffer) == OBJ_NAME_PATH_SEPARATOR) {
  1023. RemainingName->Buffer++;
  1024. RemainingName->Length -= sizeof(WCHAR);
  1025. RemainingName->MaximumLength -= sizeof(WCHAR);
  1026. }
  1027. //
  1028. // Remember where the component starts, and scan to the end
  1029. //
  1030. NextName->Buffer = RemainingName->Buffer;
  1031. while (TRUE) {
  1032. if (RemainingName->Length == 0) {
  1033. break;
  1034. }
  1035. if (*RemainingName->Buffer == OBJ_NAME_PATH_SEPARATOR) {
  1036. break;
  1037. }
  1038. //
  1039. // NOT at end
  1040. // NOT another path sep
  1041. //
  1042. RemainingName->Buffer++;
  1043. RemainingName->Length -= sizeof(WCHAR);
  1044. RemainingName->MaximumLength -= sizeof(WCHAR);
  1045. }
  1046. //
  1047. // Compute component length, return error if it's illegal
  1048. //
  1049. NextName->Length = (USHORT)
  1050. ((PUCHAR)RemainingName->Buffer - (PUCHAR)(NextName->Buffer));
  1051. if (NextName->Length > REG_MAX_KEY_NAME_LENGTH)
  1052. {
  1053. rc = FALSE;
  1054. }
  1055. NextName->MaximumLength = NextName->Length;
  1056. //
  1057. // Set last, return success
  1058. //
  1059. *Last = (RemainingName->Length == 0);
  1060. return rc;
  1061. }
  1062. NTSTATUS
  1063. CmpDoOpen(
  1064. IN PHHIVE Hive,
  1065. IN HCELL_INDEX Cell,
  1066. IN PCM_KEY_NODE Node,
  1067. IN PACCESS_STATE AccessState,
  1068. IN KPROCESSOR_MODE AccessMode,
  1069. IN ULONG Attributes,
  1070. IN PCM_PARSE_CONTEXT Context,
  1071. IN BOOLEAN CompleteKeyCached,
  1072. IN OUT PCM_KEY_CONTROL_BLOCK *CachedKcb,
  1073. IN PUNICODE_STRING KeyName,
  1074. OUT PVOID *Object
  1075. )
  1076. /*++
  1077. Routine Description:
  1078. Open a registry key, create a keycontrol block.
  1079. Arguments:
  1080. Hive - supplies a pointer to the hive control structure for the hive
  1081. Cell - supplies index of node to delete
  1082. AccessState - Running security access state information for operation.
  1083. AccessMode - Access mode of the original caller.
  1084. Attributes - Attributes to be applied to the object.
  1085. Context - if create or hive root open, points to a CM_PARSE_CONTEXT
  1086. structure,
  1087. if open, is NULL.
  1088. CompleteKeyCached - BOOLEAN to indicate it the completekey is cached.
  1089. CachedKcb - If the completekey is cached, this is the kcb for the destination.
  1090. If not, this is the parent kcb.
  1091. KeyName - Relative name (to BaseName)
  1092. Object - The address of a variable to receive the created key object, if
  1093. any.
  1094. Return Value:
  1095. NTSTATUS
  1096. --*/
  1097. {
  1098. NTSTATUS status;
  1099. PCM_KEY_BODY pbody;
  1100. PCM_KEY_CONTROL_BLOCK kcb;
  1101. KPROCESSOR_MODE mode;
  1102. BOOLEAN BackupRestore;
  1103. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpDoOpen:\n"));
  1104. if (ARGUMENT_PRESENT(Context)) {
  1105. //
  1106. // It's a create of some sort
  1107. //
  1108. if (Context->CreateLink) {
  1109. //
  1110. // The node already exists as a regular key, so it cannot be
  1111. // turned into a link node.
  1112. //
  1113. return STATUS_ACCESS_DENIED;
  1114. } else if (Context->CreateOptions & REG_OPTION_CREATE_LINK) {
  1115. //
  1116. // Attempt to create a symbolic link has hit an existing key
  1117. // so return an error
  1118. //
  1119. return STATUS_OBJECT_NAME_COLLISION;
  1120. } else {
  1121. //
  1122. // Operation is an open, so set Disposition
  1123. //
  1124. Context->Disposition = REG_OPENED_EXISTING_KEY;
  1125. }
  1126. }
  1127. //
  1128. // Check for symbolic link and caller does not want to open a link
  1129. //
  1130. if (CompleteKeyCached) {
  1131. //
  1132. // The complete key is cached.
  1133. //
  1134. BEGIN_KCB_LOCK_GUARD;
  1135. CmpLockKCBTreeExclusive();
  1136. if ((*CachedKcb)->Flags & KEY_SYM_LINK && !(Attributes & OBJ_OPENLINK)) {
  1137. //
  1138. // If the key is a symbolic link, check if the link has been resolved.
  1139. // If the link is resolved, change the kcb to the real KCB.
  1140. // Otherwise, return for reparse.
  1141. //
  1142. if ((*CachedKcb)->ExtFlags & CM_KCB_SYM_LINK_FOUND) {
  1143. kcb = (*CachedKcb)->ValueCache.RealKcb;
  1144. if (kcb->Delete == TRUE) {
  1145. //
  1146. // The real key it pointes to had been deleted.
  1147. // We have no way of knowing if the key has been recreated.
  1148. // Just clean up the cache and do a reparse.
  1149. //
  1150. CmpCleanUpKcbValueCache(*CachedKcb);
  1151. CmpUnlockKCBTree();
  1152. return(STATUS_REPARSE);
  1153. }
  1154. if (!CmpReferenceKeyControlBlock(kcb)) {
  1155. CmpUnlockKCBTree();
  1156. return STATUS_INSUFFICIENT_RESOURCES;
  1157. }
  1158. } else {
  1159. CmpUnlockKCBTree();
  1160. return(STATUS_REPARSE);
  1161. }
  1162. } else {
  1163. //
  1164. // Not a symbolic link, increase the reference Count of Kcb.
  1165. //
  1166. kcb = *CachedKcb;
  1167. if (!CmpReferenceKeyControlBlock(kcb)) {
  1168. CmpUnlockKCBTree();
  1169. return STATUS_INSUFFICIENT_RESOURCES;
  1170. }
  1171. }
  1172. CmpUnlockKCBTree();
  1173. END_KCB_LOCK_GUARD;
  1174. } else {
  1175. //
  1176. // The key is not in cache, the CachedKcb is the parentkcb of this
  1177. // key to be opened.
  1178. //
  1179. if (Node->Flags & KEY_SYM_LINK && !(Attributes & OBJ_OPENLINK)) {
  1180. //
  1181. // Create a KCB for this symbolic key and put it in delay close.
  1182. //
  1183. kcb = CmpCreateKeyControlBlock(Hive, Cell, Node, *CachedKcb, FALSE, KeyName);
  1184. if (kcb == NULL) {
  1185. return STATUS_INSUFFICIENT_RESOURCES;
  1186. }
  1187. CmpDereferenceKeyControlBlock(kcb);
  1188. *CachedKcb = kcb;
  1189. return(STATUS_REPARSE);
  1190. }
  1191. //
  1192. // If key control block does not exist, and cannot be created, fail,
  1193. // else just increment the ref count (done for us by CreateKeyControlBlock)
  1194. //
  1195. kcb = CmpCreateKeyControlBlock(Hive, Cell, Node, *CachedKcb, FALSE, KeyName);
  1196. if (kcb == NULL) {
  1197. return STATUS_INSUFFICIENT_RESOURCES;
  1198. }
  1199. ASSERT(kcb->Delete == FALSE);
  1200. *CachedKcb = kcb;
  1201. }
  1202. #if DBG
  1203. if( kcb->ExtFlags & CM_KCB_KEY_NON_EXIST ) {
  1204. //
  1205. // we shouldn't fall into this
  1206. //
  1207. DbgBreakPoint();
  1208. return STATUS_OBJECT_NAME_NOT_FOUND;
  1209. }
  1210. #endif //DBG
  1211. //
  1212. // Allocate the object.
  1213. //
  1214. status = ObCreateObject(AccessMode,
  1215. CmpKeyObjectType,
  1216. NULL,
  1217. AccessMode,
  1218. NULL,
  1219. sizeof(CM_KEY_BODY),
  1220. 0,
  1221. 0,
  1222. Object);
  1223. if (NT_SUCCESS(status)) {
  1224. pbody = (PCM_KEY_BODY)(*Object);
  1225. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_POOL,"CmpDoOpen: object allocated at:%p\n", pbody));
  1226. //
  1227. // Check for predefined handle
  1228. //
  1229. pbody = (PCM_KEY_BODY)(*Object);
  1230. if (kcb->Flags & KEY_PREDEF_HANDLE) {
  1231. pbody->Type = kcb->ValueCache.Count;
  1232. pbody->KeyControlBlock = kcb;
  1233. return(STATUS_PREDEFINED_HANDLE);
  1234. } else {
  1235. //
  1236. // Fill in CM specific fields in the object
  1237. //
  1238. pbody->Type = KEY_BODY_TYPE;
  1239. pbody->KeyControlBlock = kcb;
  1240. pbody->NotifyBlock = NULL;
  1241. pbody->Process = PsGetCurrentProcess();
  1242. ENLIST_KEYBODY_IN_KEYBODY_LIST(pbody);
  1243. }
  1244. #ifdef CM_BREAK_ON_KEY_OPEN
  1245. if( kcb->Flags & KEY_BREAK_ON_OPEN ) {
  1246. DbgPrint("\n\n Current process is opening a key tagged as BREAK ON OPEN\n");
  1247. DbgPrint("\nPlease type the following in the debugger window: !reg kcb %p\n\n\n",kcb);
  1248. try {
  1249. DbgBreakPoint();
  1250. } except (EXCEPTION_EXECUTE_HANDLER) {
  1251. //
  1252. // no debugger enabled, just keep going
  1253. //
  1254. }
  1255. }
  1256. #endif //CM_BREAK_ON_KEY_OPEN
  1257. } else {
  1258. //
  1259. // Failed to create object, so undo key control block work
  1260. //
  1261. CmpDereferenceKeyControlBlock(kcb);
  1262. return status;
  1263. }
  1264. //
  1265. // Check to make sure the caller can access the key.
  1266. //
  1267. BackupRestore = FALSE;
  1268. if (ARGUMENT_PRESENT(Context)) {
  1269. if (Context->CreateOptions & REG_OPTION_BACKUP_RESTORE) {
  1270. BackupRestore = TRUE;
  1271. }
  1272. }
  1273. status = STATUS_SUCCESS;
  1274. if (BackupRestore == TRUE) {
  1275. //
  1276. // this is an open to support a backup or restore
  1277. // operation, do the special case work
  1278. //
  1279. AccessState->RemainingDesiredAccess = 0;
  1280. AccessState->PreviouslyGrantedAccess = 0;
  1281. mode = KeGetPreviousMode();
  1282. if (SeSinglePrivilegeCheck(SeBackupPrivilege, mode)) {
  1283. AccessState->PreviouslyGrantedAccess |=
  1284. KEY_READ | ACCESS_SYSTEM_SECURITY;
  1285. }
  1286. if (SeSinglePrivilegeCheck(SeRestorePrivilege, mode)) {
  1287. AccessState->PreviouslyGrantedAccess |=
  1288. KEY_WRITE | ACCESS_SYSTEM_SECURITY | WRITE_DAC | WRITE_OWNER;
  1289. }
  1290. if (AccessState->PreviouslyGrantedAccess == 0) {
  1291. //
  1292. // relevent privileges not asserted/possessed, so
  1293. // deref (which will cause CmpDeleteKeyObject to clean up)
  1294. // and return an error.
  1295. //
  1296. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpDoOpen for backup restore: access denied\n"));
  1297. #ifdef CM_CHECK_FOR_ORPHANED_KCBS
  1298. //DbgPrint("CmpDoOpen for backup restore: access denied , hive = %p\n",Hive);
  1299. #endif //CM_CHECK_FOR_ORPHANED_KCBS
  1300. ObDereferenceObject(*Object);
  1301. return STATUS_ACCESS_DENIED;
  1302. }
  1303. } else {
  1304. if (!ObCheckObjectAccess(*Object,
  1305. AccessState,
  1306. TRUE, // Type mutex already locked
  1307. AccessMode,
  1308. &status))
  1309. {
  1310. //
  1311. // Access denied, so deref object, will cause CmpDeleteKeyObject
  1312. // to be called, it will clean up.
  1313. //
  1314. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpDoOpen: access denied\n"));
  1315. #ifdef CM_CHECK_FOR_ORPHANED_KCBS
  1316. //DbgPrint("CmpDoOpen: access denied , hive = %p\n",Hive);
  1317. #endif //CM_CHECK_FOR_ORPHANED_KCBS
  1318. ObDereferenceObject(*Object);
  1319. }
  1320. }
  1321. return status;
  1322. }
  1323. #ifdef CM_CHECK_FOR_ORPHANED_KCBS
  1324. ULONG CmpCheckOrphanedKcbFix = 0;
  1325. #endif //CM_CHECK_FOR_ORPHANED_KCBS
  1326. NTSTATUS
  1327. CmpCreateLinkNode(
  1328. IN PHHIVE Hive,
  1329. IN HCELL_INDEX Cell,
  1330. IN PACCESS_STATE AccessState,
  1331. IN UNICODE_STRING Name,
  1332. IN KPROCESSOR_MODE AccessMode,
  1333. IN ULONG Attributes,
  1334. IN PCM_PARSE_CONTEXT Context,
  1335. IN PCM_KEY_CONTROL_BLOCK ParentKcb,
  1336. OUT PVOID *Object
  1337. )
  1338. /*++
  1339. Routine Description:
  1340. Perform the creation of a link node. Allocate all components,
  1341. and attach to parent key. Calls CmpDoCreate or CmpDoOpen to
  1342. create or open the root node of the hive as appropriate.
  1343. Note that you can only create link nodes in the master hive.
  1344. Arguments:
  1345. Hive - supplies a pointer to the hive control structure for the hive
  1346. Cell - supplies index of node to create child under
  1347. Name - supplies pointer to a UNICODE string which is the name of
  1348. the child to be created.
  1349. AccessMode - Access mode of the original caller.
  1350. Attributes - Attributes to be applied to the object.
  1351. Context - pointer to CM_PARSE_CONTEXT structure passed through
  1352. the object manager
  1353. BaseName - Name of object create is relative to
  1354. KeyName - Relative name (to BaseName)
  1355. Object - The address of a variable to receive the created key object, if
  1356. any.
  1357. Return Value:
  1358. NTSTATUS
  1359. --*/
  1360. {
  1361. NTSTATUS Status;
  1362. PCELL_DATA Parent;
  1363. PCELL_DATA Link;
  1364. PCELL_DATA CellData;
  1365. HCELL_INDEX LinkCell;
  1366. HCELL_INDEX KeyCell;
  1367. HCELL_INDEX ChildCell;
  1368. PCM_KEY_CONTROL_BLOCK kcb = ParentKcb;
  1369. PCM_KEY_BODY KeyBody;
  1370. LARGE_INTEGER systemtime;
  1371. PCM_KEY_NODE TempNode;
  1372. LARGE_INTEGER TimeStamp;
  1373. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  1374. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpCreateLinkNode:\n"));
  1375. if (Hive != &CmpMasterHive->Hive) {
  1376. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpCreateLinkNode: attempt to create link node in\n"));
  1377. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE," non-master hive %p\n", Hive));
  1378. return(STATUS_ACCESS_DENIED);
  1379. }
  1380. #if DBG
  1381. //
  1382. // debug only code
  1383. //
  1384. *Object = NULL;
  1385. #endif
  1386. //
  1387. // this is a create, so we need exclusive access on the registry
  1388. // first get the time stamp to see if somebody messed with this key
  1389. // this might be more easier if we decide to cache the LastWriteTime
  1390. // in the KCB ; now it IS !!!
  1391. //
  1392. if( CmIsKcbReadOnly(ParentKcb) ) {
  1393. //
  1394. // key is protected
  1395. //
  1396. return STATUS_ACCESS_DENIED;
  1397. }
  1398. TimeStamp = ParentKcb->KcbLastWriteTime;
  1399. CmpUnlockRegistry();
  1400. CmpLockRegistryExclusive();
  1401. #ifdef CHECK_REGISTRY_USECOUNT
  1402. CmpCheckRegistryUseCount();
  1403. #endif //CHECK_REGISTRY_USECOUNT
  1404. //
  1405. // make sure nothing changed in between:
  1406. // 1. ParentKcb is still valid
  1407. // 2. Child was not already added by somebody else
  1408. //
  1409. if( ParentKcb->Delete ) {
  1410. //
  1411. // key was deleted in between
  1412. //
  1413. return STATUS_OBJECT_NAME_NOT_FOUND;
  1414. }
  1415. if( TimeStamp.QuadPart != ParentKcb->KcbLastWriteTime.QuadPart ) {
  1416. //
  1417. // key was changed in between; possibly this key was already created ==> reparse
  1418. //
  1419. return STATUS_REPARSE;
  1420. }
  1421. //
  1422. // Allocate link node
  1423. //
  1424. // Link nodes are always in the master hive, so their storage type is
  1425. // mostly irrelevent.
  1426. //
  1427. LinkCell = HvAllocateCell(Hive, CmpHKeyNodeSize(Hive, &Name), Stable,HCELL_NIL);
  1428. if (LinkCell == HCELL_NIL) {
  1429. return STATUS_INSUFFICIENT_RESOURCES;
  1430. }
  1431. KeyCell = Context->ChildHive.KeyCell;
  1432. if (KeyCell != HCELL_NIL) {
  1433. //
  1434. // This hive already exists, so we just need to open the root node.
  1435. //
  1436. ChildCell=KeyCell;
  1437. //
  1438. // The root cell in the hive does not has the Name buffer
  1439. // space reseverd. This is why we need to pass in the Name for creating KCB
  1440. // instead of using the name in the keynode.
  1441. //
  1442. CellData = HvGetCell(Context->ChildHive.KeyHive, ChildCell);
  1443. if( CellData == NULL ) {
  1444. //
  1445. // we couldn't map the bin contianing this cell
  1446. //
  1447. HvFreeCell(Hive, LinkCell);
  1448. return STATUS_INSUFFICIENT_RESOURCES;
  1449. }
  1450. // release the cell right here as we are holding the reglock exclusive
  1451. HvReleaseCell(Context->ChildHive.KeyHive, ChildCell);
  1452. CellData->u.KeyNode.Parent = LinkCell;
  1453. CellData->u.KeyNode.Flags |= KEY_HIVE_ENTRY | KEY_NO_DELETE;
  1454. TempNode = (PCM_KEY_NODE)HvGetCell(Context->ChildHive.KeyHive,KeyCell);
  1455. if( TempNode == NULL ) {
  1456. //
  1457. // we couldn't map the bin contianing this cell
  1458. //
  1459. HvFreeCell(Hive, LinkCell);
  1460. return STATUS_INSUFFICIENT_RESOURCES;
  1461. }
  1462. // release the cell right here as we are holding the reglock exclusive
  1463. HvReleaseCell(Context->ChildHive.KeyHive,KeyCell);
  1464. Status = CmpDoOpen( Context->ChildHive.KeyHive,
  1465. KeyCell,
  1466. TempNode,
  1467. AccessState,
  1468. AccessMode,
  1469. Attributes,
  1470. NULL,
  1471. FALSE,
  1472. &kcb,
  1473. &Name,
  1474. Object );
  1475. } else {
  1476. //
  1477. // This is a newly created hive, so we must allocate and initialize
  1478. // the root node.
  1479. //
  1480. Status = CmpDoCreateChild( Context->ChildHive.KeyHive,
  1481. Cell,
  1482. NULL,
  1483. AccessState,
  1484. &Name,
  1485. AccessMode,
  1486. Context,
  1487. ParentKcb,
  1488. KEY_HIVE_ENTRY | KEY_NO_DELETE,
  1489. &ChildCell,
  1490. Object );
  1491. if (NT_SUCCESS(Status)) {
  1492. //
  1493. // Initialize hive root cell pointer.
  1494. //
  1495. Context->ChildHive.KeyHive->BaseBlock->RootCell = ChildCell;
  1496. }
  1497. }
  1498. if (NT_SUCCESS(Status)) {
  1499. #ifdef CM_CHECK_FOR_ORPHANED_KCBS
  1500. if(CmpCheckOrphanedKcbFix) {
  1501. DbgPrint("CmpCreateLinkNode: Force return with STATUS_NO_LOG_SPACE\n");
  1502. Status = STATUS_NO_LOG_SPACE;
  1503. goto Cleanup;
  1504. }
  1505. #endif //CM_CHECK_FOR_ORPHANED_KCBS
  1506. //
  1507. // Initialize parent and flags. Note that we do this whether the
  1508. // root has been created or opened, because we are not guaranteed
  1509. // that the link node is always the same cell in the master hive.
  1510. //
  1511. if (!HvMarkCellDirty(Context->ChildHive.KeyHive, ChildCell)) {
  1512. Status = STATUS_NO_LOG_SPACE;
  1513. goto Cleanup;
  1514. }
  1515. CellData = HvGetCell(Context->ChildHive.KeyHive, ChildCell);
  1516. if( CellData == NULL ) {
  1517. //
  1518. // we couldn't map the bin contianing this cell
  1519. //
  1520. HvFreeCell(Hive, LinkCell);
  1521. Status = STATUS_INSUFFICIENT_RESOURCES;
  1522. goto Cleanup;
  1523. }
  1524. // release the cell right here as we are holding the reglock exclusive
  1525. HvReleaseCell(Context->ChildHive.KeyHive, ChildCell);
  1526. CellData->u.KeyNode.Parent = LinkCell;
  1527. CellData->u.KeyNode.Flags |= KEY_HIVE_ENTRY | KEY_NO_DELETE;
  1528. //
  1529. // Initialize special link node flags and data
  1530. //
  1531. Link = HvGetCell(Hive, LinkCell);
  1532. if( Link == NULL ) {
  1533. //
  1534. // we couldn't map the bin contianing this cell
  1535. // this shouldn't happen as we just allocated this cell
  1536. // (i.e. it should be PINNED in memory at this point)
  1537. //
  1538. ASSERT( FALSE );
  1539. HvFreeCell(Hive, LinkCell);
  1540. Status = STATUS_INSUFFICIENT_RESOURCES;
  1541. goto Cleanup;
  1542. }
  1543. // release the cell right here as we are holding the reglock exclusive
  1544. HvReleaseCell(Hive,LinkCell);
  1545. Link->u.KeyNode.Signature = CM_LINK_NODE_SIGNATURE;
  1546. Link->u.KeyNode.Flags = KEY_HIVE_EXIT | KEY_NO_DELETE;
  1547. Link->u.KeyNode.Parent = Cell;
  1548. Link->u.KeyNode.NameLength = CmpCopyName(Hive, Link->u.KeyNode.Name, &Name);
  1549. if (Link->u.KeyNode.NameLength < Name.Length) {
  1550. Link->u.KeyNode.Flags |= KEY_COMP_NAME;
  1551. }
  1552. KeQuerySystemTime(&systemtime);
  1553. Link->u.KeyNode.LastWriteTime = systemtime;
  1554. //
  1555. // Zero out unused fields.
  1556. //
  1557. Link->u.KeyNode.SubKeyCounts[Stable] = 0;
  1558. Link->u.KeyNode.SubKeyCounts[Volatile] = 0;
  1559. Link->u.KeyNode.SubKeyLists[Stable] = HCELL_NIL;
  1560. Link->u.KeyNode.SubKeyLists[Volatile] = HCELL_NIL;
  1561. Link->u.KeyNode.ValueList.Count = 0;
  1562. Link->u.KeyNode.ValueList.List = HCELL_NIL;
  1563. Link->u.KeyNode.ClassLength = 0;
  1564. //
  1565. // Fill in the link node's pointer to the root node
  1566. //
  1567. Link->u.KeyNode.ChildHiveReference.KeyHive = Context->ChildHive.KeyHive;
  1568. Link->u.KeyNode.ChildHiveReference.KeyCell = ChildCell;
  1569. //
  1570. // get the parent first, we don't need to do unneccessary cleanup
  1571. //
  1572. Parent = HvGetCell(Hive, Cell);
  1573. if( Parent == NULL ) {
  1574. //
  1575. // we couldn't map the bin contianing this cell
  1576. //
  1577. HvFreeCell(Hive, LinkCell);
  1578. Status = STATUS_INSUFFICIENT_RESOURCES;
  1579. goto Cleanup;
  1580. }
  1581. // release the cell right here as we are holding the reglock exclusive
  1582. HvReleaseCell(Hive,Cell);
  1583. //
  1584. // Fill in the parent cell's child list
  1585. //
  1586. if (! CmpAddSubKey(Hive, Cell, LinkCell)) {
  1587. HvFreeCell(Hive, LinkCell);
  1588. Status = STATUS_INSUFFICIENT_RESOURCES;
  1589. goto Cleanup;
  1590. }
  1591. //
  1592. // If the parent has the subkey info or hint cached, free it.
  1593. //
  1594. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  1595. KeyBody = (PCM_KEY_BODY)(*Object);
  1596. CmpCleanUpSubKeyInfo (KeyBody->KeyControlBlock->ParentKcb);
  1597. //
  1598. // Update max keyname and class name length fields
  1599. //
  1600. //
  1601. // It seems to me that the original code is wrong.
  1602. // Isn't the definition of MaxNameLen just the length of the subkey?
  1603. //
  1604. // some sanity asserts
  1605. ASSERT( KeyBody->KeyControlBlock->ParentKcb->KeyCell == Cell );
  1606. ASSERT( KeyBody->KeyControlBlock->ParentKcb->KeyHive == Hive );
  1607. ASSERT( KeyBody->KeyControlBlock->ParentKcb->KcbMaxNameLen == Parent->u.KeyNode.MaxNameLen );
  1608. //
  1609. // update the LastWriteTime on both keynode and kcb;
  1610. //
  1611. KeQuerySystemTime(&systemtime);
  1612. Parent->u.KeyNode.LastWriteTime = systemtime;
  1613. KeyBody->KeyControlBlock->ParentKcb->KcbLastWriteTime = systemtime;
  1614. if (Parent->u.KeyNode.MaxNameLen < Name.Length) {
  1615. Parent->u.KeyNode.MaxNameLen = Name.Length;
  1616. KeyBody->KeyControlBlock->ParentKcb->KcbMaxNameLen = Name.Length;
  1617. }
  1618. if (Parent->u.KeyNode.MaxClassLen < Context->Class.Length) {
  1619. Parent->u.KeyNode.MaxClassLen = Context->Class.Length;
  1620. }
  1621. Cleanup:
  1622. if( !NT_SUCCESS(Status) ) {
  1623. ASSERT( (*Object) != NULL );
  1624. //
  1625. // mark the kcb as "no-delay-close" so it gets kicked out of cache when
  1626. // refcount goes down to 0
  1627. //
  1628. KeyBody = (PCM_KEY_BODY)(*Object);
  1629. ASSERT( KeyBody->KeyControlBlock );
  1630. ASSERT_KCB( KeyBody->KeyControlBlock );
  1631. KeyBody->KeyControlBlock->ExtFlags |= CM_KCB_NO_DELAY_CLOSE;
  1632. ObDereferenceObject(*Object);
  1633. }
  1634. } else {
  1635. HvFreeCell(Hive, LinkCell);
  1636. }
  1637. return(Status);
  1638. }
  1639. BOOLEAN
  1640. CmpGetSymbolicLink(
  1641. IN PHHIVE Hive,
  1642. IN OUT PUNICODE_STRING ObjectName,
  1643. IN OUT PCM_KEY_CONTROL_BLOCK SymbolicKcb,
  1644. IN PUNICODE_STRING RemainingName OPTIONAL
  1645. )
  1646. /*++
  1647. Routine Description:
  1648. This routine extracts the symbolic link name from a key, if it is
  1649. marked as a symbolic link.
  1650. Arguments:
  1651. Hive - Supplies the hive of the key.
  1652. ObjectName - Supplies the current ObjectName.
  1653. Returns the new ObjectName. If the new name is longer
  1654. than the maximum length of the current ObjectName, the
  1655. old buffer will be freed and a new buffer allocated.
  1656. RemainingName - Supplies the remaining path. If present, this will be
  1657. concatenated with the symbolic link to form the new objectname.
  1658. Return Value:
  1659. TRUE - symbolic link succesfully found
  1660. FALSE - Key is not a symbolic link, or an error occurred
  1661. --*/
  1662. {
  1663. NTSTATUS Status;
  1664. HCELL_INDEX LinkCell = HCELL_NIL;
  1665. PHCELL_INDEX Index;
  1666. PCM_KEY_VALUE LinkValue = NULL;
  1667. PWSTR LinkName;
  1668. BOOLEAN LinkNameAllocated = FALSE;
  1669. PWSTR NewBuffer;
  1670. ULONG Length;
  1671. ULONG ValueLength;
  1672. extern ULONG CmpHashTableSize;
  1673. extern PCM_KEY_HASH *CmpCacheTable;
  1674. PUNICODE_STRING ConstructedName;
  1675. ULONG ConvKey=0;
  1676. PCM_KEY_HASH KeyHash;
  1677. PCM_KEY_CONTROL_BLOCK RealKcb;
  1678. BOOLEAN KcbFound = FALSE;
  1679. ULONG Cnt;
  1680. WCHAR *Cp;
  1681. WCHAR *Cp2;
  1682. ULONG TotalLevels;
  1683. BOOLEAN FreeConstructedName = FALSE;
  1684. BOOLEAN Result = TRUE;
  1685. HCELL_INDEX CellToRelease = HCELL_NIL;
  1686. #ifdef CM_DYN_SYM_LINK
  1687. BOOLEAN DynamicLink = FALSE;
  1688. PWSTR ExpandedLinkName = NULL;
  1689. #endif //CM_DYN_SYM_LINK
  1690. BEGIN_KCB_LOCK_GUARD;
  1691. CmpLockKCBTree();
  1692. if (SymbolicKcb->ExtFlags & CM_KCB_SYM_LINK_FOUND) {
  1693. //
  1694. // First see of the real kcb for this symbolic name has been found
  1695. //
  1696. ConstructedName = CmpConstructName(SymbolicKcb->ValueCache.RealKcb);
  1697. if (ConstructedName) {
  1698. FreeConstructedName = TRUE;
  1699. LinkName = ConstructedName->Buffer;
  1700. ValueLength = ConstructedName->Length;
  1701. Length = (USHORT)ValueLength + sizeof(WCHAR);
  1702. }
  1703. }
  1704. CmpUnlockKCBTree();
  1705. END_KCB_LOCK_GUARD;
  1706. if (FreeConstructedName == FALSE) {
  1707. PCM_KEY_NODE Node;
  1708. //
  1709. // Find the SymbolicLinkValue value. This is the name of the symbolic link.
  1710. //
  1711. Node = (PCM_KEY_NODE)HvGetCell(SymbolicKcb->KeyHive,SymbolicKcb->KeyCell);
  1712. if( Node == NULL ) {
  1713. //
  1714. // we couldn't map the bin containing this cell
  1715. //
  1716. Result = FALSE;
  1717. goto Exit;
  1718. }
  1719. LinkCell = CmpFindValueByName(Hive,
  1720. Node,
  1721. &CmSymbolicLinkValueName);
  1722. // release the node here as we don't need it anymore
  1723. HvReleaseCell(SymbolicKcb->KeyHive,SymbolicKcb->KeyCell);
  1724. if (LinkCell == HCELL_NIL) {
  1725. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpGetSymbolicLink: couldn't open symbolic link\n"));
  1726. Result = FALSE;
  1727. goto Exit;
  1728. }
  1729. LinkValue = (PCM_KEY_VALUE)HvGetCell(Hive, LinkCell);
  1730. if( LinkValue == NULL ) {
  1731. //
  1732. // we couldn't map the bin containing this cell
  1733. //
  1734. Result = FALSE;
  1735. goto Exit;
  1736. }
  1737. #ifdef CM_DYN_SYM_LINK
  1738. if( LinkValue->Type == REG_DYN_LINK ) {
  1739. //
  1740. // we have found a dynamic link
  1741. //
  1742. DynamicLink = TRUE;
  1743. } else
  1744. #endif //CM_DYN_SYM_LINK
  1745. if (LinkValue->Type != REG_LINK) {
  1746. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpGetSymbolicLink: link value is wrong type: %08lx", LinkValue->Type));
  1747. Result = FALSE;
  1748. goto Exit;
  1749. }
  1750. if( CmpGetValueData(Hive,LinkValue,&ValueLength,&LinkName,&LinkNameAllocated,&CellToRelease) == FALSE ) {
  1751. //
  1752. // insufficient resources; return NULL
  1753. //
  1754. ASSERT( LinkNameAllocated == FALSE );
  1755. ASSERT( LinkName == NULL );
  1756. Result = FALSE;
  1757. goto Exit;
  1758. }
  1759. #ifdef CM_DYN_SYM_LINK
  1760. if( DynamicLink == TRUE ) {
  1761. ULONG DestLength;
  1762. ExpandedLinkName = CmpExpandEnvVars(LinkName,ValueLength,&DestLength);
  1763. if( ExpandedLinkName == NULL ) {
  1764. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"Dynamic link not resolved !\n"));
  1765. Result = FALSE;
  1766. goto Exit;
  1767. }
  1768. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"Dynamic link resolved to: (%.*S)\n",DestLength/sizeof(WCHAR),ExpandedLinkName));
  1769. //
  1770. // if we are here, we successfully resolved the link
  1771. //
  1772. LinkName = ExpandedLinkName;
  1773. ValueLength = DestLength;
  1774. }
  1775. #endif //CM_DYN_SYM_LINK
  1776. Length = (USHORT)ValueLength + sizeof(WCHAR);
  1777. #ifdef CM_DYN_SYM_LINK
  1778. if( DynamicLink == FALSE ) {
  1779. #endif //CM_DYN_SYM_LINK
  1780. //
  1781. // Now see if we have this kcb cached.
  1782. //
  1783. Cp = LinkName;
  1784. //
  1785. // first char SHOULD be OBJ_NAME_PATH_SEPARATOR, otherwise we could get into real trouble!!!
  1786. //
  1787. if( *Cp != OBJ_NAME_PATH_SEPARATOR ) {
  1788. Result = FALSE;
  1789. goto Exit;
  1790. }
  1791. TotalLevels = 0;
  1792. for (Cnt=0; Cnt<ValueLength; Cnt += sizeof(WCHAR)) {
  1793. if (*Cp != OBJ_NAME_PATH_SEPARATOR) {
  1794. ConvKey = 37 * ConvKey + (ULONG) RtlUpcaseUnicodeChar(*Cp);
  1795. } else {
  1796. TotalLevels++;
  1797. }
  1798. ++Cp;
  1799. }
  1800. BEGIN_KCB_LOCK_GUARD;
  1801. CmpLockKCBTreeExclusive();
  1802. KeyHash = GET_HASH_ENTRY(CmpCacheTable, ConvKey);
  1803. while (KeyHash) {
  1804. RealKcb = CONTAINING_RECORD(KeyHash, CM_KEY_CONTROL_BLOCK, KeyHash);
  1805. if ((ConvKey == KeyHash->ConvKey) && (TotalLevels == RealKcb->TotalLevels) && (!(RealKcb->ExtFlags & CM_KCB_KEY_NON_EXIST)) ) {
  1806. ConstructedName = CmpConstructName(RealKcb);
  1807. if (ConstructedName) {
  1808. FreeConstructedName = TRUE;
  1809. if (ConstructedName->Length == ValueLength) {
  1810. KcbFound = TRUE;
  1811. Cp = LinkName;
  1812. Cp2 = ConstructedName->Buffer;
  1813. for (Cnt=0; Cnt<ConstructedName->Length; Cnt += sizeof(WCHAR)) {
  1814. if (RtlUpcaseUnicodeChar(*Cp) != RtlUpcaseUnicodeChar(*Cp2)) {
  1815. KcbFound = FALSE;
  1816. break;
  1817. }
  1818. ++Cp;
  1819. ++Cp2;
  1820. }
  1821. if (KcbFound) {
  1822. //
  1823. // Now the RealKcb is also pointed to by its symbolic link Kcb,
  1824. // Increase the reference count.
  1825. // Need to dereference the realkcb when the symbolic kcb is removed.
  1826. // Do this in CmpCleanUpKcbCacheWithLock();
  1827. //
  1828. if (CmpReferenceKeyControlBlock(RealKcb)) {
  1829. //
  1830. // This symbolic kcb may have value lookup for the path
  1831. // Cleanup the value cache.
  1832. //
  1833. CmpCleanUpKcbValueCache(SymbolicKcb);
  1834. SymbolicKcb->ExtFlags |= CM_KCB_SYM_LINK_FOUND;
  1835. SymbolicKcb->ValueCache.RealKcb = RealKcb;
  1836. } else {
  1837. //
  1838. // We have maxed out the ref count on the real kcb.
  1839. // do not cache the symbolic link.
  1840. //
  1841. }
  1842. break;
  1843. }
  1844. }
  1845. } else {
  1846. break;
  1847. }
  1848. }
  1849. if (FreeConstructedName) {
  1850. ExFreePoolWithTag(ConstructedName, CM_NAME_TAG | PROTECTED_POOL);
  1851. FreeConstructedName = FALSE;
  1852. }
  1853. KeyHash = KeyHash->NextHash;
  1854. }
  1855. CmpUnlockKCBTree();
  1856. END_KCB_LOCK_GUARD;
  1857. #ifdef CM_DYN_SYM_LINK
  1858. }
  1859. #endif //CM_DYN_SYM_LINK
  1860. }
  1861. if (ARGUMENT_PRESENT(RemainingName)) {
  1862. Length += RemainingName->Length + sizeof(WCHAR);
  1863. }
  1864. //
  1865. // Overflow test: If Length overflows the USHRT_MAX value
  1866. // cleanup and return FALSE
  1867. //
  1868. if( Length>0xFFFF ) {
  1869. Result = FALSE;
  1870. goto Exit;
  1871. }
  1872. if (Length > ObjectName->MaximumLength) {
  1873. UNICODE_STRING NewObjectName;
  1874. //
  1875. // The new name is too long to fit in the existing ObjectName buffer,
  1876. // so allocate a new buffer.
  1877. //
  1878. NewBuffer = ExAllocatePool(PagedPool, Length);
  1879. if (NewBuffer == NULL) {
  1880. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpGetSymbolicLink: couldn't allocate new name buffer\n"));
  1881. Result = FALSE;
  1882. goto Exit;
  1883. }
  1884. NewObjectName.Buffer = NewBuffer;
  1885. NewObjectName.MaximumLength = (USHORT)Length;
  1886. NewObjectName.Length = (USHORT)ValueLength;
  1887. RtlCopyMemory(NewBuffer, LinkName, ValueLength);
  1888. #if DBG
  1889. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE,"CmpGetSymbolicLink: LinkName is %wZ\n", ObjectName));
  1890. if (ARGUMENT_PRESENT(RemainingName)) {
  1891. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE," RemainingName is %wZ\n", RemainingName));
  1892. } else {
  1893. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_PARSE," RemainingName is NULL\n"));
  1894. }
  1895. #endif
  1896. if (ARGUMENT_PRESENT(RemainingName)) {
  1897. NewBuffer[ ValueLength / sizeof(WCHAR) ] = OBJ_NAME_PATH_SEPARATOR;
  1898. NewObjectName.Length += sizeof(WCHAR);
  1899. Status = RtlAppendUnicodeStringToString(&NewObjectName, RemainingName);
  1900. ASSERT(NT_SUCCESS(Status));
  1901. }
  1902. ExFreePool(ObjectName->Buffer);
  1903. *ObjectName = NewObjectName;
  1904. } else {
  1905. //
  1906. // The new name will fit within the maximum length of the existing
  1907. // ObjectName, so do the expansion in-place. Note that the remaining
  1908. // name must be moved into its new position first since the symbolic
  1909. // link may or may not overlap it.
  1910. //
  1911. ObjectName->Length = (USHORT)ValueLength;
  1912. if (ARGUMENT_PRESENT(RemainingName)) {
  1913. RtlMoveMemory(&ObjectName->Buffer[(ValueLength / sizeof(WCHAR)) + 1],
  1914. RemainingName->Buffer,
  1915. RemainingName->Length);
  1916. ObjectName->Buffer[ValueLength / sizeof(WCHAR)] = OBJ_NAME_PATH_SEPARATOR;
  1917. ObjectName->Length += RemainingName->Length + sizeof(WCHAR);
  1918. }
  1919. RtlCopyMemory(ObjectName->Buffer, LinkName, ValueLength);
  1920. }
  1921. ObjectName->Buffer[ObjectName->Length / sizeof(WCHAR)] = UNICODE_NULL;
  1922. Exit:
  1923. if( LinkNameAllocated ) {
  1924. ExFreePool(LinkName);
  1925. }
  1926. if (FreeConstructedName) {
  1927. ExFreePoolWithTag(ConstructedName, CM_NAME_TAG | PROTECTED_POOL);
  1928. }
  1929. #ifdef CM_DYN_SYM_LINK
  1930. if( ExpandedLinkName ) {
  1931. ExFreePool(ExpandedLinkName);
  1932. }
  1933. #endif //CM_DYN_SYM_LINK
  1934. if( LinkValue != NULL ) {
  1935. ASSERT( LinkCell != HCELL_NIL );
  1936. HvReleaseCell(Hive,LinkCell);
  1937. }
  1938. if( CellToRelease != HCELL_NIL ) {
  1939. HvReleaseCell(Hive,CellToRelease);
  1940. }
  1941. return Result;
  1942. }
  1943. ULONG
  1944. CmpComputeHashValue(
  1945. IN PCM_HASH_ENTRY HashStack,
  1946. IN OUT ULONG *TotalSubkeys,
  1947. IN ULONG BaseConvKey,
  1948. IN PUNICODE_STRING RemainingName
  1949. )
  1950. /*++
  1951. Routine Description:
  1952. This routine parses the complete path of a request registry key and calculate
  1953. the hash value at each level.
  1954. Arguments:
  1955. HashStack - Array for filling the hash value of each level.
  1956. TotalSubkeys - a pointer to fill the total number of subkeys
  1957. BaseConvKey - Supplies the convkey for the base key.
  1958. RemainingName - supplies pointer to a unicode_string for RemainingName.
  1959. Return Value:
  1960. Number of Levels in RemainingName
  1961. --*/
  1962. {
  1963. ULONG TotalRemainingSubkeys=0;
  1964. ULONG TotalKeys=0;
  1965. ULONG ConvKey=BaseConvKey;
  1966. USHORT Cnt;
  1967. WCHAR *Cp;
  1968. WCHAR *Begin;
  1969. USHORT Length;
  1970. if (RemainingName->Length) {
  1971. Cp = RemainingName->Buffer;
  1972. Cnt = RemainingName->Length;
  1973. //Skip the leading OBJ_NAME_PATH_SEPARATOR
  1974. while (*Cp == OBJ_NAME_PATH_SEPARATOR) {
  1975. Cp++;
  1976. Cnt -= sizeof(WCHAR);
  1977. }
  1978. Begin = Cp;
  1979. Length = 0;
  1980. HashStack[TotalRemainingSubkeys].KeyName.Buffer = Cp;
  1981. while (Cnt) {
  1982. if (*Cp == OBJ_NAME_PATH_SEPARATOR) {
  1983. if (TotalRemainingSubkeys < CM_HASH_STACK_SIZE) {
  1984. HashStack[TotalRemainingSubkeys].ConvKey = ConvKey;
  1985. //
  1986. // Due to the changes in KCB structure, we now only have the subkey name
  1987. // in the kcb (not the full path). Change the name in the stack to store
  1988. // the parse element (each subkey) only.
  1989. //
  1990. HashStack[TotalRemainingSubkeys].KeyName.Length = Length;
  1991. Length = 0;
  1992. TotalRemainingSubkeys++;
  1993. }
  1994. TotalKeys++;
  1995. //
  1996. // Now skip over leading path separators
  1997. // Just in case someone has a RemainingName '..A\\\\B..'
  1998. //
  1999. //
  2000. // We are stripping all OBJ_NAME_PATH_SEPARATOR (The origainl code keep the first one).
  2001. // so the KeyName.Buffer is set properly.
  2002. //
  2003. while(*Cp == OBJ_NAME_PATH_SEPARATOR) {
  2004. Cp++;
  2005. Cnt -= sizeof(WCHAR);
  2006. }
  2007. if (TotalRemainingSubkeys < CM_HASH_STACK_SIZE) {
  2008. HashStack[TotalRemainingSubkeys].KeyName.Buffer = Cp;
  2009. }
  2010. } else {
  2011. ConvKey = 37 * ConvKey + (ULONG) RtlUpcaseUnicodeChar(*Cp);
  2012. //
  2013. // We are stripping all OBJ_NAME_PATH_SEPARATOR in the above code,
  2014. // we should only move to the next char in the else case.
  2015. //
  2016. Cp++;
  2017. Cnt -= sizeof(WCHAR);
  2018. Length += sizeof(WCHAR);
  2019. }
  2020. }
  2021. //
  2022. // Since we have stripped off all trailing path separators in CmpParseKey routine,
  2023. // the last char will not be OBJ_NAME_PATH_SEPARATOR.
  2024. //
  2025. if (TotalRemainingSubkeys < CM_HASH_STACK_SIZE) {
  2026. HashStack[TotalRemainingSubkeys].ConvKey = ConvKey;
  2027. HashStack[TotalRemainingSubkeys].KeyName.Length = Length;
  2028. TotalRemainingSubkeys++;
  2029. }
  2030. TotalKeys++;
  2031. (*TotalSubkeys) = TotalKeys;
  2032. }
  2033. return(TotalRemainingSubkeys);
  2034. }
  2035. NTSTATUS
  2036. CmpCacheLookup(
  2037. IN PCM_HASH_ENTRY HashStack,
  2038. IN ULONG TotalRemainingSubkeys,
  2039. OUT ULONG *MatchRemainSubkeyLevel,
  2040. IN OUT PCM_KEY_CONTROL_BLOCK *Kcb,
  2041. OUT PUNICODE_STRING RemainingName,
  2042. OUT PHHIVE *Hive,
  2043. OUT HCELL_INDEX *Cell
  2044. )
  2045. /*++
  2046. Routine Description:
  2047. This routine Search the cache to find the matching path in the Cache.
  2048. Arguments:
  2049. HashStack - Array that has the hash value of each level.
  2050. TotalRemainingSubkeys - Total Subkey counts from base.
  2051. MatchRemainSubkeyLevel - Number of Levels in RemaingName
  2052. that matches. (0 if not found)
  2053. kcb - Pointer to the kcb of the basename.
  2054. Will be changed to the kcb for the new basename.
  2055. RemainingName - Returns remaining name
  2056. Hive - Returns the hive of the cache entry found (if any)
  2057. Cell - Returns the cell of the cache entry found (if any)
  2058. Return Value:
  2059. Status
  2060. --*/
  2061. {
  2062. LONG i;
  2063. LONG j;
  2064. NTSTATUS status = STATUS_SUCCESS;
  2065. ULONG CurrentLevel;
  2066. PCM_KEY_HASH Current;
  2067. PCM_KEY_CONTROL_BLOCK BaseKcb;
  2068. PCM_KEY_CONTROL_BLOCK CurrentKcb;
  2069. PCM_KEY_CONTROL_BLOCK ParentKcb;
  2070. BOOLEAN Found = FALSE;
  2071. BaseKcb = *Kcb;
  2072. CurrentLevel = TotalRemainingSubkeys + BaseKcb->TotalLevels + 1;
  2073. for(i = TotalRemainingSubkeys-1; i>=0; i--) {
  2074. //
  2075. // Try to find the longest path in the cache.
  2076. //
  2077. // First, find the kcb that match the hash value.
  2078. //
  2079. CurrentLevel--;
  2080. Current = GET_HASH_ENTRY(CmpCacheTable, HashStack[i].ConvKey);
  2081. while (Current) {
  2082. ASSERT_KEY_HASH(Current);
  2083. //
  2084. // Check against both the ConvKey and total levels;
  2085. //
  2086. CurrentKcb = (CONTAINING_RECORD(Current, CM_KEY_CONTROL_BLOCK, KeyHash));
  2087. if (CurrentKcb->TotalLevels == CurrentLevel) {
  2088. //
  2089. // The total subkey levels match.
  2090. // Iterate through the kcb path and compare each subkey.
  2091. //
  2092. Found = TRUE;
  2093. ParentKcb = CurrentKcb;
  2094. for (j=i; j>=0; j--) {
  2095. if (HashStack[j].ConvKey == ParentKcb->ConvKey) {
  2096. //
  2097. // Convkey matches, compare the string
  2098. //
  2099. LONG Result;
  2100. UNICODE_STRING TmpNodeName;
  2101. if (ParentKcb->NameBlock->Compressed) {
  2102. Result = CmpCompareCompressedName(&(HashStack[j].KeyName),
  2103. ParentKcb->NameBlock->Name,
  2104. ParentKcb->NameBlock->NameLength,
  2105. CMP_DEST_UP // name block is always UPPERCASE!!!
  2106. );
  2107. } else {
  2108. TmpNodeName.Buffer = ParentKcb->NameBlock->Name;
  2109. TmpNodeName.Length = ParentKcb->NameBlock->NameLength;
  2110. TmpNodeName.MaximumLength = ParentKcb->NameBlock->NameLength;
  2111. //
  2112. // use the cmp compare variant as we know the destination is already uppercased.
  2113. //
  2114. Result = CmpCompareUnicodeString(&(HashStack[j].KeyName),
  2115. &TmpNodeName,
  2116. CMP_DEST_UP);
  2117. }
  2118. if (Result) {
  2119. Found = FALSE;
  2120. break;
  2121. }
  2122. ParentKcb = ParentKcb->ParentKcb;
  2123. } else {
  2124. Found = FALSE;
  2125. break;
  2126. }
  2127. }
  2128. if (Found) {
  2129. //
  2130. // All remaining key matches. Now compare the BaseKcb.
  2131. //
  2132. if (BaseKcb == ParentKcb) {
  2133. if (CurrentKcb->ParentKcb->Delete) {
  2134. //
  2135. // The parentkcb is marked deleted.
  2136. // So this must be a fake key created when the parent still existed.
  2137. // Otherwise it cannot be in the cache
  2138. //
  2139. ASSERT (CurrentKcb->ExtFlags & CM_KCB_KEY_NON_EXIST);
  2140. //
  2141. // It is possible that the parent key was deleted but now recreated.
  2142. // In that case this fake key is not longer valid for the ParentKcb is bad.
  2143. // We must now remove this fake key out of cache so, if this is a
  2144. // create operation, we do get hit this kcb in CmpCreateKeyControlBlock.
  2145. //
  2146. if (CurrentKcb->RefCount == 0) {
  2147. //
  2148. // No one is holding this fake kcb, just delete it.
  2149. //
  2150. CmpRemoveFromDelayedClose(CurrentKcb);
  2151. CmpCleanUpKcbCacheWithLock(CurrentKcb);
  2152. } else {
  2153. //
  2154. // Someone is still holding this fake kcb,
  2155. // Mark it as delete and remove it out of cache.
  2156. //
  2157. CurrentKcb->Delete = TRUE;
  2158. CmpRemoveKeyControlBlock(CurrentKcb);
  2159. }
  2160. Found = FALSE;
  2161. break;
  2162. } else if(CurrentKcb->Delete) {
  2163. //
  2164. // the key has been deleted, but still kept in the cache for
  2165. // this kcb does not belong here
  2166. //
  2167. CmpRemoveKeyControlBlock(CurrentKcb);
  2168. return STATUS_OBJECT_NAME_NOT_FOUND;
  2169. }
  2170. //
  2171. // We have a match, update the RemainingName.
  2172. //
  2173. //
  2174. // Skip the leading OBJ_NAME_PATH_SEPARATOR
  2175. //
  2176. while ((RemainingName->Length > 0) &&
  2177. (RemainingName->Buffer[0] == OBJ_NAME_PATH_SEPARATOR)) {
  2178. RemainingName->Buffer++;
  2179. RemainingName->Length -= sizeof(WCHAR);
  2180. }
  2181. //
  2182. // Skip all subkeys plus OBJ_NAME_PATH_SEPARATOR
  2183. //
  2184. for(j=0; j<=i; j++) {
  2185. RemainingName->Buffer += HashStack[j].KeyName.Length/sizeof(WCHAR) + 1;
  2186. RemainingName->Length -= HashStack[j].KeyName.Length + sizeof(WCHAR);
  2187. }
  2188. //
  2189. // Update the KCB, Hive and Cell.
  2190. //
  2191. *Kcb = CurrentKcb;
  2192. *Hive = CurrentKcb->KeyHive;
  2193. *Cell = CurrentKcb->KeyCell;
  2194. break;
  2195. } else {
  2196. Found = FALSE;
  2197. }
  2198. }
  2199. }
  2200. Current = Current->NextHash;
  2201. }
  2202. if (Found) {
  2203. break;
  2204. }
  2205. }
  2206. if((*Kcb)->Delete) {
  2207. //
  2208. // the key has been deleted, but still kept in the cache for
  2209. // this kcb does not belong here
  2210. //
  2211. return STATUS_OBJECT_NAME_NOT_FOUND;
  2212. }
  2213. //
  2214. // Now the kcb will be used in the parse routine.
  2215. // Increase its reference count.
  2216. // Make sure we remember to dereference it at the parse routine.
  2217. //
  2218. if (!CmpReferenceKeyControlBlock(*Kcb)) {
  2219. status = STATUS_INSUFFICIENT_RESOURCES;
  2220. }
  2221. *MatchRemainSubkeyLevel = i+1;
  2222. return status;
  2223. }
  2224. PCM_KEY_CONTROL_BLOCK
  2225. CmpAddInfoAfterParseFailure(
  2226. PHHIVE Hive,
  2227. HCELL_INDEX Cell,
  2228. PCM_KEY_NODE Node,
  2229. PCM_KEY_CONTROL_BLOCK kcb,
  2230. PUNICODE_STRING NodeName
  2231. )
  2232. /*++
  2233. Routine Description:
  2234. This routine builds up further information in the cache when parse
  2235. fails. The additional information can be
  2236. 1. The key is has no subkey (CM_KCB_NO_SUBKEY).
  2237. 2. The key has a few subkeys, then build the index hint in the cache.
  2238. 3. If lookup failed even we have index hint cached, then create a fake key so
  2239. we do not fail again. This is very usful for lookup failure under keys like
  2240. \registry\machine\software\classes\clsid, which have 1500+ subkeys and lots of
  2241. them have the smae first four chars.
  2242. NOTE. Currently we are not seeing too many fake keys being created.
  2243. We need to monitor this periodly and work out a way to work around if
  2244. we do create too many fake keys.
  2245. One solution is to use hash value for index hint (We can do it in the cache only
  2246. if we need to be backward comparible).
  2247. Arguments:
  2248. Hive - Supplies Hive that holds the key we are creating a KCB for.
  2249. Cell - Supplies Cell that contains the key we are creating a KCB for.
  2250. Node - Supplies pointer to key node.
  2251. KeyName - The KeyName.
  2252. Return Value:
  2253. The KCB that CmpParse need to dereference at the end.
  2254. If resources problem, it returns NULL, and the caller is responsible for cleanup
  2255. --*/
  2256. {
  2257. ULONG TotalSubKeyCounts;
  2258. BOOLEAN CreateFakeKcb = FALSE;
  2259. BOOLEAN HintCached;
  2260. PCM_KEY_CONTROL_BLOCK ParentKcb;
  2261. USHORT i,j,k;
  2262. HCELL_INDEX CellToRelease;
  2263. ULONG HashKey;
  2264. if (!UseFastIndex(Hive)) {
  2265. //
  2266. // Older version of hive, do not bother to cache hint.
  2267. //
  2268. return (kcb);
  2269. }
  2270. TotalSubKeyCounts = Node->SubKeyCounts[Stable] + Node->SubKeyCounts[Volatile];
  2271. if (TotalSubKeyCounts == 0) {
  2272. BEGIN_KCB_LOCK_GUARD;
  2273. CmpLockKCBTreeExclusive();
  2274. kcb->ExtFlags |= CM_KCB_NO_SUBKEY;
  2275. // clean up the invalid flag (if any)
  2276. kcb->ExtFlags &= ~CM_KCB_INVALID_CACHED_INFO;
  2277. CmpUnlockKCBTree();
  2278. END_KCB_LOCK_GUARD;
  2279. } else if (TotalSubKeyCounts == 1) {
  2280. BEGIN_KCB_LOCK_GUARD;
  2281. CmpLockKCBTreeExclusive();
  2282. if (!(kcb->ExtFlags & CM_KCB_SUBKEY_ONE)) {
  2283. //
  2284. // Build the subkey hint to avoid unnecessary lookups in the index leaf
  2285. //
  2286. PCM_KEY_INDEX Index;
  2287. HCELL_INDEX SubKeyCell;
  2288. PCM_KEY_NODE SubKeyNode;
  2289. UNICODE_STRING TmpStr;
  2290. if (Node->SubKeyCounts[Stable] == 1) {
  2291. CellToRelease = Node->SubKeyLists[Stable];
  2292. Index = (PCM_KEY_INDEX)HvGetCell(Hive, CellToRelease);
  2293. } else {
  2294. CellToRelease = Node->SubKeyLists[Volatile];
  2295. Index = (PCM_KEY_INDEX)HvGetCell(Hive, CellToRelease);
  2296. }
  2297. if( Index == NULL ) {
  2298. //
  2299. // we couldn't map the bin containing this cell
  2300. // return NULL; The caller must handle this gracefully!
  2301. //
  2302. CmpUnlockKCBTree();
  2303. return NULL;
  2304. }
  2305. if( Index->Signature == CM_KEY_INDEX_ROOT ) {
  2306. //
  2307. // don't cache root indexes; they are too big
  2308. //
  2309. HvReleaseCell(Hive,CellToRelease);
  2310. CmpUnlockKCBTree();
  2311. return NULL;
  2312. }
  2313. HashKey = 0;
  2314. if ( Index->Signature == CM_KEY_HASH_LEAF ) {
  2315. PCM_KEY_FAST_INDEX FastIndex;
  2316. FastIndex = (PCM_KEY_FAST_INDEX)Index;
  2317. //
  2318. // we already have the hash key handy; preserve it for the kcb hint
  2319. //
  2320. HashKey = FastIndex->List[0].HashKey;
  2321. } else if(Index->Signature == CM_KEY_FAST_LEAF) {
  2322. PCM_KEY_FAST_INDEX FastIndex;
  2323. FastIndex = (PCM_KEY_FAST_INDEX)Index;
  2324. SubKeyCell = FastIndex->List[0].Cell;
  2325. } else {
  2326. SubKeyCell = Index->List[0];
  2327. }
  2328. //DbgPrint("CmpAddInfoAfterParseFailure [0]\n");
  2329. if( HashKey != 0 ) {
  2330. kcb->HashKey = HashKey;
  2331. kcb->ExtFlags |= CM_KCB_SUBKEY_ONE;
  2332. // clean up the invalid flag (if any)
  2333. kcb->ExtFlags &= ~CM_KCB_INVALID_CACHED_INFO;
  2334. } else {
  2335. SubKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,SubKeyCell);
  2336. if( SubKeyNode != NULL ) {
  2337. if (SubKeyNode->Flags & KEY_COMP_NAME) {
  2338. kcb->HashKey = CmpComputeHashKeyForCompressedName(SubKeyNode->Name,SubKeyNode->NameLength);
  2339. } else {
  2340. TmpStr.Buffer = SubKeyNode->Name;
  2341. TmpStr.Length = SubKeyNode->NameLength;
  2342. kcb->HashKey = CmpComputeHashKey(&TmpStr);
  2343. }
  2344. HvReleaseCell(Hive,SubKeyCell);
  2345. kcb->ExtFlags |= CM_KCB_SUBKEY_ONE;
  2346. // clean up the invalid flag (if any)
  2347. kcb->ExtFlags &= ~CM_KCB_INVALID_CACHED_INFO;
  2348. } else {
  2349. //
  2350. // we couldn't map the bin containing this cell
  2351. // return NULL; The caller must handle this gracefully!
  2352. //
  2353. HvReleaseCell(Hive,CellToRelease);
  2354. CmpUnlockKCBTree();
  2355. return NULL;
  2356. }
  2357. }
  2358. HvReleaseCell(Hive,CellToRelease);
  2359. } else {
  2360. //
  2361. // The name hint does not prevent from this look up
  2362. // Create the fake Kcb.
  2363. //
  2364. CreateFakeKcb = TRUE;
  2365. }
  2366. CmpUnlockKCBTree();
  2367. END_KCB_LOCK_GUARD;
  2368. } else if (TotalSubKeyCounts < CM_MAX_CACHE_HINT_SIZE) {
  2369. BEGIN_KCB_LOCK_GUARD;
  2370. CmpLockKCBTreeExclusive();
  2371. if (!(kcb->ExtFlags & CM_KCB_SUBKEY_HINT)) {
  2372. //
  2373. // Build the index leaf info in the parent KCB
  2374. // How to sync the cache with the registry data is a problem to be resolved.
  2375. //
  2376. ULONG Size;
  2377. PCM_KEY_INDEX Index;
  2378. PCM_KEY_FAST_INDEX FastIndex;
  2379. HCELL_INDEX SubKeyCell;
  2380. PCM_KEY_NODE SubKeyNode;
  2381. ULONG HintCrt;
  2382. UNICODE_STRING TmpStr;
  2383. Size = sizeof(ULONG) * (Node->SubKeyCounts[Stable] + Node->SubKeyCounts[Volatile] + 1);
  2384. kcb->IndexHint = ExAllocatePoolWithTag(PagedPool,
  2385. Size,
  2386. CM_CACHE_INDEX_TAG | PROTECTED_POOL);
  2387. HintCached = TRUE;
  2388. if (kcb->IndexHint) {
  2389. kcb->IndexHint->Count = Node->SubKeyCounts[Stable] + Node->SubKeyCounts[Volatile];
  2390. HintCrt = 0;
  2391. //DbgPrint("CmpAddInfoAfterParseFailure [1]\n");
  2392. for (i = 0; i < Hive->StorageTypeCount; i++) {
  2393. if(Node->SubKeyCounts[i]) {
  2394. CellToRelease = Node->SubKeyLists[i];
  2395. Index = (PCM_KEY_INDEX)HvGetCell(Hive, CellToRelease);
  2396. if( Index == NULL ) {
  2397. //
  2398. // we couldn't map the bin containing this cell
  2399. // return NULL; The caller must handle this gracefully!
  2400. //
  2401. CmpUnlockKCBTree();
  2402. return NULL;
  2403. }
  2404. if( Index->Signature == CM_KEY_INDEX_ROOT ) {
  2405. HvReleaseCell(Hive,CellToRelease);
  2406. HintCached = FALSE;
  2407. break;
  2408. } else {
  2409. for (j=0; j<Node->SubKeyCounts[i]; j++) {
  2410. HashKey = 0;
  2411. if ( Index->Signature == CM_KEY_HASH_LEAF ) {
  2412. FastIndex = (PCM_KEY_FAST_INDEX)Index;
  2413. //
  2414. // preserve the hash key for the kcb hint
  2415. //
  2416. HashKey = FastIndex->List[j].HashKey;
  2417. } else if( Index->Signature == CM_KEY_FAST_LEAF ) {
  2418. FastIndex = (PCM_KEY_FAST_INDEX)Index;
  2419. SubKeyCell = FastIndex->List[j].Cell;
  2420. } else {
  2421. SubKeyCell = Index->List[j];
  2422. }
  2423. if( HashKey != 0 ) {
  2424. kcb->IndexHint->HashKey[HintCrt] = HashKey;
  2425. } else {
  2426. SubKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,SubKeyCell);
  2427. if( SubKeyNode == NULL ) {
  2428. //
  2429. // couldn't map view; bad luck; don't cache hint for this kcb
  2430. //
  2431. HintCached = FALSE;
  2432. break;
  2433. }
  2434. if (SubKeyNode->Flags & KEY_COMP_NAME) {
  2435. kcb->IndexHint->HashKey[HintCrt] = CmpComputeHashKeyForCompressedName(SubKeyNode->Name,SubKeyNode->NameLength);
  2436. } else {
  2437. TmpStr.Buffer = SubKeyNode->Name;
  2438. TmpStr.Length = SubKeyNode->NameLength;
  2439. kcb->IndexHint->HashKey[HintCrt] = CmpComputeHashKey(&TmpStr);
  2440. }
  2441. HvReleaseCell(Hive,SubKeyCell);
  2442. }
  2443. //
  2444. // advance to the new hint
  2445. //
  2446. HintCrt++;
  2447. }
  2448. }
  2449. HvReleaseCell(Hive,CellToRelease);
  2450. }
  2451. }
  2452. if (HintCached) {
  2453. kcb->ExtFlags |= CM_KCB_SUBKEY_HINT;
  2454. // clean up the invalid flag (if any)
  2455. kcb->ExtFlags &= ~CM_KCB_INVALID_CACHED_INFO;
  2456. } else {
  2457. //
  2458. // Do not have a FAST_LEAF, free the allocation.
  2459. //
  2460. ExFreePoolWithTag(kcb->IndexHint, CM_CACHE_INDEX_TAG | PROTECTED_POOL);
  2461. }
  2462. }
  2463. } else {
  2464. //
  2465. // The name hint does not prevent from this look up
  2466. // Create the fake Kcb.
  2467. //
  2468. CreateFakeKcb = TRUE;
  2469. }
  2470. CmpUnlockKCBTree();
  2471. END_KCB_LOCK_GUARD;
  2472. } else {
  2473. CreateFakeKcb = TRUE;
  2474. }
  2475. ParentKcb = kcb;
  2476. if (CreateFakeKcb && (CmpCacheOnFlag & CM_CACHE_FAKE_KEY)) {
  2477. //
  2478. // It has more than a few children but not the one we are interested.
  2479. // Create a kcb for this non-existing key so we do not try to find it
  2480. // again. Use the cell and node from the parent.
  2481. //
  2482. // Before we create a new one. Dereference the current kcb.
  2483. //
  2484. // CmpCacheOnFlag is for us to turn it on/off easily.
  2485. //
  2486. kcb = CmpCreateKeyControlBlock(Hive,
  2487. Cell,
  2488. Node,
  2489. ParentKcb,
  2490. TRUE,
  2491. NodeName);
  2492. if (kcb) {
  2493. CmpDereferenceKeyControlBlock(ParentKcb);
  2494. ParentKcb = kcb;
  2495. }
  2496. }
  2497. return (ParentKcb);
  2498. }
  2499. #ifdef CM_DYN_SYM_LINK
  2500. //
  2501. // this code is commented out of the current builds;
  2502. // there is a potential security breach in the way RtlAcquirePebLock()
  2503. // works by calling a user mode routine (stored in the PEB) to lock the PEB
  2504. // We need to find a way to work around that before enabling this code
  2505. //
  2506. //
  2507. // Commenting the body of this function, to make sure code will not go in without
  2508. // fixing the above problem
  2509. //
  2510. BOOLEAN
  2511. CmpCaptureProcessEnvironmentString(
  2512. OUT PWSTR *ProcessEnvironment,
  2513. OUT PULONG Length
  2514. )
  2515. /*++
  2516. Routine Description:
  2517. Captures the process environment; It first Probe the env, then captures its
  2518. address. Parse the whole env to the end and count it's length.
  2519. Then allocate a buffer for it and copy.
  2520. All of these are done in try/except to protect for bogus user-mode data.
  2521. We need to lock the teb while working on it.
  2522. Arguments:
  2523. ProcessEnvironment - to receive the captured stuff
  2524. Length - length of the above - in bytes
  2525. Return Value:
  2526. TRUE or FALSE
  2527. when TRUE, the caller is responsible of freeing ProcessEnvironment
  2528. --*/
  2529. {
  2530. /*
  2531. BOOLEAN Result = TRUE;
  2532. PPEB Peb;
  2533. PWSTR LocalEnv;
  2534. PWSTR p;
  2535. PAGED_CODE();
  2536. *ProcessEnvironment = NULL;
  2537. *Length = 0;
  2538. try {
  2539. //
  2540. // grab the peb lock and the peb
  2541. //
  2542. RtlAcquirePebLock();
  2543. Peb = PsGetCurrentProcess()->Peb;
  2544. //
  2545. // probe the env from peb
  2546. //
  2547. LocalEnv = (PWSTR)ProbeAndReadPointer((PVOID *)(&(Peb->ProcessParameters->Environment)));
  2548. //
  2549. // parse the env to find its length
  2550. //
  2551. //
  2552. // The environment variable block consists of zero or more null
  2553. // terminated UNICODE strings. Each string is of the form:
  2554. //
  2555. // name=value
  2556. //
  2557. // where the null termination is after the value.
  2558. //
  2559. p = LocalEnv;
  2560. if (p != NULL) while (*p) {
  2561. while (*p) {
  2562. p++;
  2563. *Length += sizeof(WCHAR);
  2564. }
  2565. //
  2566. // Skip over the terminating null character for this name=value
  2567. // pair in preparation for the next iteration of the loop.
  2568. //
  2569. p++;
  2570. *Length += sizeof(WCHAR);
  2571. }
  2572. //
  2573. // adjust the length to accomodate the last two UNICODE_NULL
  2574. //
  2575. *Length += 2*sizeof(WCHAR);
  2576. //
  2577. // allocate a buffer for the captured env and copy
  2578. //
  2579. *ProcessEnvironment = (PWSTR)ExAllocatePoolWithTag(PagedPool,*Length,CM_FIND_LEAK_TAG41);
  2580. if( *ProcessEnvironment != NULL ) {
  2581. RtlCopyMemory(*ProcessEnvironment,LocalEnv, *Length);
  2582. } else {
  2583. *Length = 0;
  2584. }
  2585. //
  2586. // release the peb lock
  2587. //
  2588. RtlReleasePebLock();
  2589. } except (EXCEPTION_EXECUTE_HANDLER) {
  2590. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"!!CmpCaptureProcessEnvironmentString: code:%08lx\n", GetExceptionCode()));
  2591. Result = FALSE;
  2592. if( *ProcessEnvironment != NULL) {
  2593. ExFreePool(*ProcessEnvironment);
  2594. *ProcessEnvironment = NULL;
  2595. }
  2596. *Length = 0;
  2597. //
  2598. // release the peb lock
  2599. //
  2600. RtlReleasePebLock();
  2601. }
  2602. return Result;
  2603. */
  2604. }
  2605. #define GROW_INCREMENT 64*sizeof(WCHAR) // grow 64 wide-chars at a time
  2606. PWSTR
  2607. CmpExpandEnvVars(
  2608. IN PWSTR StringToExpand,
  2609. IN ULONG LengthToExpand,
  2610. OUT PULONG ExpandedLength
  2611. )
  2612. /*++
  2613. Routine Description:
  2614. Replaces all env vars from StringToExpand with their values, from the process
  2615. environment. Allocates a new buffer for the result and returns it.
  2616. Arguments:
  2617. StringToExpand - to receive the captured stuff
  2618. LengthToExpand - length of the above - in bytes
  2619. ExpandedLength - the actual length of the expanded string
  2620. Return Value:
  2621. NULL - the string could not be expanded (or not all the env inside it could be resolved)
  2622. valid buffer - the expanded string, it is the caller's responsibility to free it.
  2623. --*/
  2624. {
  2625. PWSTR ProcessEnv;
  2626. ULONG ProcessEnvLength;
  2627. PWSTR ExpandedString;
  2628. ULONG ExpandedStringSize;
  2629. PWSTR CurrentEnvVar;
  2630. ULONG CurrentEnvLength;
  2631. PWSTR CurrentEnvValue;
  2632. ULONG CurrentEnvValueLength;
  2633. PAGED_CODE();
  2634. *ExpandedLength = 0;
  2635. if( !CmpCaptureProcessEnvironmentString(&ProcessEnv,&ProcessEnvLength) ) {
  2636. //
  2637. // could not secure the process env
  2638. //
  2639. ASSERT( (ProcessEnv == NULL) && (ProcessEnvLength == 0) );
  2640. return NULL;
  2641. }
  2642. //
  2643. // allocate a buffer twice as the unexpanded buffer; we shall grow it if it's not big enough
  2644. //
  2645. ExpandedStringSize = LengthToExpand * 2;
  2646. ExpandedString = (PWSTR)ExAllocatePoolWithTag(PagedPool,ExpandedStringSize,CM_FIND_LEAK_TAG42);
  2647. if( ExpandedString == NULL ) {
  2648. goto JustReturn;
  2649. }
  2650. //
  2651. // convert to number of WCHARs
  2652. //
  2653. LengthToExpand /= sizeof(WCHAR);
  2654. //
  2655. // iterate through the string to be expanded and copy everything that's not and env var
  2656. // expand the env vars and replace them with their value
  2657. //
  2658. while( LengthToExpand ) {
  2659. //
  2660. // find a % sign
  2661. //
  2662. while( LengthToExpand && (*StringToExpand != L'%') ) {
  2663. if( *ExpandedLength == ExpandedStringSize ) {
  2664. //
  2665. // we need to grow the expanded string
  2666. //
  2667. if( !CmpGrowAndCopyString(&ExpandedString,&ExpandedStringSize,GROW_INCREMENT) ) {
  2668. goto ErrorExit;
  2669. }
  2670. }
  2671. ExpandedString[(*ExpandedLength) / sizeof(WCHAR)] = *StringToExpand;
  2672. (*ExpandedLength) += sizeof(WCHAR);
  2673. LengthToExpand--;
  2674. StringToExpand++;
  2675. }
  2676. if( LengthToExpand == 0 ) {
  2677. if( *StringToExpand != L'%') {
  2678. //
  2679. // we have exited the loop because of the end of the string
  2680. //
  2681. goto JustReturn;
  2682. } else {
  2683. //
  2684. // we have found a mismatched %
  2685. //
  2686. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpExpandEnvVars : mismatched % sign\n"));
  2687. goto ErrorExit;
  2688. }
  2689. }
  2690. ASSERT( *StringToExpand == L'%' );
  2691. //
  2692. // skip it; then mark the beggining of an env var
  2693. //
  2694. StringToExpand++;
  2695. LengthToExpand--;
  2696. CurrentEnvVar = StringToExpand;
  2697. CurrentEnvLength = 0;
  2698. //
  2699. // find a % match sign
  2700. //
  2701. while( LengthToExpand && (*StringToExpand != L'%') ) {
  2702. LengthToExpand--;
  2703. StringToExpand++;
  2704. CurrentEnvLength += sizeof(WCHAR);
  2705. }
  2706. if( LengthToExpand == 0 ) {
  2707. if( (*StringToExpand == L'%') && (CurrentEnvLength != 0) ) {
  2708. //
  2709. // end of string and no empty env var; we'll return (exit the surrounding
  2710. // while loop) after expanding this string
  2711. //
  2712. } else {
  2713. //
  2714. // we didn't find a matching % sign, or we are in the %% case
  2715. //
  2716. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpExpandEnvVars : mismatched % sign\n"));
  2717. goto ErrorExit;
  2718. }
  2719. } else {
  2720. //
  2721. // skip this % sign
  2722. //
  2723. StringToExpand++;
  2724. LengthToExpand--;
  2725. }
  2726. //
  2727. // find the value for this env var
  2728. //
  2729. if( !CmpFindEnvVar(ProcessEnv,ProcessEnvLength,CurrentEnvVar,CurrentEnvLength,&CurrentEnvValue,&CurrentEnvValueLength) ) {
  2730. //
  2731. // could not resolve this env var
  2732. //
  2733. ASSERT( CurrentEnvValue == NULL );
  2734. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpExpandEnvVars : could not resolve (%.*S)\n",CurrentEnvLength/sizeof(WCHAR),CurrentEnvVar));
  2735. goto ErrorExit;
  2736. }
  2737. ASSERT( (CurrentEnvValueLength % sizeof(WCHAR)) == 0 );
  2738. //
  2739. // found it; strcat it at the end of the expanded string
  2740. //
  2741. if( (*ExpandedLength + CurrentEnvValueLength) >= ExpandedStringSize ) {
  2742. //
  2743. // we first need to grow the buffer
  2744. //
  2745. if( !CmpGrowAndCopyString(&ExpandedString,&ExpandedStringSize,CurrentEnvValueLength) ) {
  2746. goto ErrorExit;
  2747. }
  2748. }
  2749. ASSERT( (*ExpandedLength + CurrentEnvValueLength) < ExpandedStringSize );
  2750. RtlCopyMemory(((PUCHAR)ExpandedString) + (*ExpandedLength),CurrentEnvValue,CurrentEnvValueLength);
  2751. *ExpandedLength += CurrentEnvValueLength;
  2752. }
  2753. goto JustReturn;
  2754. ErrorExit:
  2755. if( ExpandedString != NULL ) {
  2756. ExFreePool(ExpandedString);
  2757. ExpandedString = NULL;
  2758. }
  2759. JustReturn:
  2760. ExFreePool(ProcessEnv);
  2761. return ExpandedString;
  2762. }
  2763. BOOLEAN
  2764. CmpGrowAndCopyString(
  2765. IN OUT PWSTR *OldString,
  2766. IN OUT PULONG OldStringSize,
  2767. IN ULONG GrowIncrements
  2768. )
  2769. {
  2770. PWSTR NewString;
  2771. PAGED_CODE();
  2772. ASSERT( (*OldStringSize % sizeof(WCHAR)) == 0 );
  2773. ASSERT( (GrowIncrements % sizeof(WCHAR)) == 0 );
  2774. NewString = (PWSTR)ExAllocatePoolWithTag(PagedPool,*OldStringSize + GrowIncrements,CM_FIND_LEAK_TAG42);
  2775. if( NewString == NULL ) {
  2776. return FALSE;
  2777. }
  2778. RtlCopyMemory(NewString,*OldString,*OldStringSize);
  2779. ExFreePool(*OldString);
  2780. *OldString = NewString;
  2781. *OldStringSize = *OldStringSize + GrowIncrements;
  2782. return TRUE;
  2783. }
  2784. BOOLEAN
  2785. CmpFindEnvVar(
  2786. IN PWSTR ProcessEnv,
  2787. IN ULONG ProcessEnvLength,
  2788. IN PWSTR CurrentEnvVar,
  2789. IN ULONG CurrentEnvLength,
  2790. OUT PWSTR *CurrentEnvValue,
  2791. OUT PULONG CurrentEnvValueLength
  2792. )
  2793. /*++
  2794. Routine Description:
  2795. finds a specified envvar in the env string;
  2796. if found returns a pointer to it in the env string, along
  2797. with its size
  2798. Arguments:
  2799. ProcessEnvironment - to receive the captured stuff
  2800. Length - length of the above - in bytes
  2801. Return Value:
  2802. TRUE or FALSE
  2803. when TRUE, the caller is responsible of freeing ProcessEnvironment
  2804. --*/
  2805. {
  2806. PWSTR p;
  2807. UNICODE_STRING CurrentName;
  2808. UNICODE_STRING CurrentValue;
  2809. UNICODE_STRING SearchedName;
  2810. PAGED_CODE();
  2811. *CurrentEnvValue = NULL;
  2812. if( ProcessEnv == NULL ) {
  2813. return FALSE;
  2814. }
  2815. p = ProcessEnv;
  2816. SearchedName.Buffer = CurrentEnvVar;
  2817. SearchedName.Length = (USHORT)CurrentEnvLength;
  2818. SearchedName.MaximumLength = (USHORT)CurrentEnvLength;
  2819. //
  2820. // The environment variable block consists of zero or more null
  2821. // terminated UNICODE strings. Each string is of the form:
  2822. //
  2823. // name=value
  2824. //
  2825. // where the null termination is after the value.
  2826. //
  2827. while (ProcessEnvLength) {
  2828. //
  2829. // Determine the size of the name and value portions of
  2830. // the current string of the environment variable block.
  2831. //
  2832. CurrentName.Buffer = p;
  2833. CurrentName.Length = 0;
  2834. CurrentName.MaximumLength = 0;
  2835. while (*p) {
  2836. //
  2837. // If we see an equal sign, then compute the size of
  2838. // the name portion and scan for the end of the value.
  2839. //
  2840. if (*p == L'=' && p != CurrentName.Buffer) {
  2841. CurrentName.Length = (USHORT)(p - CurrentName.Buffer)*sizeof(WCHAR);
  2842. CurrentName.MaximumLength = (USHORT)(CurrentName.Length+sizeof(WCHAR));
  2843. CurrentValue.Buffer = ++p;
  2844. ProcessEnvLength -= sizeof(WCHAR);
  2845. while(*p) {
  2846. p++;
  2847. ProcessEnvLength -= sizeof(WCHAR);
  2848. }
  2849. CurrentValue.Length = (USHORT)(p - CurrentValue.Buffer)*sizeof(WCHAR);
  2850. CurrentValue.MaximumLength = (USHORT)(CurrentValue.Length+sizeof(WCHAR));
  2851. //
  2852. // At this point we have the length of both the name
  2853. // and value portions, so exit the loop so we can
  2854. // do the compare.
  2855. //
  2856. break;
  2857. }
  2858. else {
  2859. ProcessEnvLength -= sizeof(WCHAR);
  2860. p++;
  2861. }
  2862. }
  2863. //
  2864. // Skip over the terminating null character for this name=value
  2865. // pair in preparation for the next iteration of the loop.
  2866. //
  2867. p++;
  2868. ProcessEnvLength -= sizeof(WCHAR);
  2869. //
  2870. // Compare the current name with the one requested, ignore
  2871. // case.
  2872. //
  2873. if (RtlEqualUnicodeString( &SearchedName, &CurrentName, TRUE )) {
  2874. //
  2875. // Names are equal. Always return the length of the
  2876. // value string, excluding the terminating null.
  2877. //
  2878. *CurrentEnvValue = CurrentValue.Buffer;
  2879. *CurrentEnvValueLength = CurrentValue.Length;
  2880. return TRUE;
  2881. }
  2882. }
  2883. return FALSE;
  2884. }
  2885. #endif //CM_DYN_SYM_LINK