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.

4920 lines
140 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. cmapi.c
  5. Abstract:
  6. This module contains CM level entry points for the registry.
  7. Author:
  8. Bryan M. Willman (bryanwi) 30-Aug-1991
  9. Revision History:
  10. --*/
  11. #include "cmp.h"
  12. extern BOOLEAN CmpNoWrite;
  13. extern LIST_ENTRY CmpHiveListHead;
  14. extern BOOLEAN CmpProfileLoaded;
  15. extern BOOLEAN CmpWasSetupBoot;
  16. extern UNICODE_STRING CmSymbolicLinkValueName;
  17. extern ULONG CmpGlobalQuotaAllowed;
  18. extern ULONG CmpGlobalQuotaWarning;
  19. extern PCMHIVE CmpMasterHive;
  20. extern HIVE_LIST_ENTRY CmpMachineHiveList[];
  21. VOID
  22. CmpDereferenceNameControlBlockWithLock(
  23. PCM_NAME_CONTROL_BLOCK Ncb
  24. );
  25. //
  26. // procedures private to this file
  27. //
  28. NTSTATUS
  29. CmpSetValueKeyExisting(
  30. IN PHHIVE Hive,
  31. IN HCELL_INDEX OldChild,
  32. IN PCM_KEY_VALUE Value,
  33. IN ULONG Type,
  34. IN PVOID Data,
  35. IN ULONG DataSize,
  36. IN ULONG StorageType,
  37. IN ULONG TempData
  38. );
  39. NTSTATUS
  40. CmpSetValueKeyNew(
  41. IN PHHIVE Hive,
  42. IN PCM_KEY_NODE Parent,
  43. IN PUNICODE_STRING ValueName,
  44. IN ULONG Index,
  45. IN ULONG Type,
  46. IN PVOID Data,
  47. IN ULONG DataSize,
  48. IN ULONG StorageType,
  49. IN ULONG TempData
  50. );
  51. VOID
  52. CmpRemoveKeyHash(
  53. IN PCM_KEY_HASH KeyHash
  54. );
  55. PCM_KEY_CONTROL_BLOCK
  56. CmpInsertKeyHash(
  57. IN PCM_KEY_HASH KeyHash,
  58. IN BOOLEAN FakeKey
  59. );
  60. #if DBG
  61. ULONG
  62. CmpUnloadKeyWorker(
  63. PCM_KEY_CONTROL_BLOCK Current,
  64. PVOID Context1,
  65. PVOID Context2
  66. );
  67. #endif
  68. ULONG
  69. CmpCompressKeyWorker(
  70. PCM_KEY_CONTROL_BLOCK Current,
  71. PVOID Context1,
  72. PVOID Context2
  73. );
  74. NTSTATUS
  75. CmpDuplicateKey(
  76. PHHIVE Hive,
  77. HCELL_INDEX OldKeyCell,
  78. PHCELL_INDEX NewKeyCell
  79. );
  80. VOID
  81. CmpDestroyTemporaryHive(
  82. PCMHIVE CmHive
  83. );
  84. BOOLEAN
  85. CmpCompareNewValueDataAgainstKCBCache( PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  86. PUNICODE_STRING ValueName,
  87. ULONG Type,
  88. PVOID Data,
  89. ULONG DataSize
  90. );
  91. BOOLEAN
  92. CmpGetValueDataFromCache(
  93. IN PHHIVE Hive,
  94. IN PPCM_CACHED_VALUE ContainingList,
  95. IN PCELL_DATA ValueKey,
  96. IN BOOLEAN ValueCached,
  97. OUT PUCHAR *DataPointer,
  98. OUT PBOOLEAN Allocated,
  99. OUT PHCELL_INDEX CellToRelease
  100. );
  101. BOOLEAN
  102. CmpCompareNewValueDataAgainstKCBCache( PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  103. PUNICODE_STRING ValueName,
  104. ULONG Type,
  105. PVOID Data,
  106. ULONG DataSize
  107. );
  108. BOOLEAN
  109. CmpGetValueDataFromCache(
  110. IN PHHIVE Hive,
  111. IN PPCM_CACHED_VALUE ContainingList,
  112. IN PCELL_DATA ValueKey,
  113. IN BOOLEAN ValueCached,
  114. OUT PUCHAR *DataPointer,
  115. OUT PBOOLEAN Allocated,
  116. OUT PHCELL_INDEX CellToRelease
  117. );
  118. BOOLEAN
  119. CmpIsHiveAlreadyLoaded( IN HANDLE KeyHandle,
  120. IN POBJECT_ATTRIBUTES SourceFile
  121. );
  122. NTSTATUS
  123. static
  124. __forceinline
  125. CmpCheckReplaceHive( IN PHHIVE Hive,
  126. OUT PHCELL_INDEX Key
  127. );
  128. #ifdef ALLOC_PRAGMA
  129. #pragma alloc_text(PAGE,CmDeleteValueKey)
  130. #pragma alloc_text(PAGE,CmEnumerateKey)
  131. #pragma alloc_text(PAGE,CmEnumerateValueKey)
  132. #pragma alloc_text(PAGE,CmFlushKey)
  133. #pragma alloc_text(PAGE,CmQueryKey)
  134. #pragma alloc_text(PAGE,CmQueryValueKey)
  135. #pragma alloc_text(PAGE,CmQueryMultipleValueKey)
  136. #pragma alloc_text(PAGE,CmSetValueKey)
  137. #pragma alloc_text(PAGE,CmpSetValueKeyExisting)
  138. #pragma alloc_text(PAGE,CmpSetValueKeyNew)
  139. #pragma alloc_text(PAGE,CmSetLastWriteTimeKey)
  140. #pragma alloc_text(PAGE,CmSetKeyUserFlags)
  141. #pragma alloc_text(PAGE,CmLoadKey)
  142. #pragma alloc_text(PAGE,CmUnloadKey)
  143. #ifdef NT_UNLOAD_KEY_EX
  144. #pragma alloc_text(PAGE,CmUnloadKeyEx)
  145. #endif //NT_UNLOAD_KEY_EX
  146. #pragma alloc_text(PAGE,CmpDoFlushAll)
  147. #pragma alloc_text(PAGE,CmReplaceKey)
  148. #ifdef WRITE_PROTECTED_REGISTRY_POOL
  149. #pragma alloc_text(PAGE,CmpMarkAllBinsReadOnly)
  150. #endif //WRITE_PROTECTED_REGISTRY_POOL
  151. #ifdef NT_RENAME_KEY
  152. #pragma alloc_text(PAGE,CmRenameKey)
  153. #endif //NT_RENAME_KEY
  154. #pragma alloc_text(PAGE,CmLockKcbForWrite)
  155. #if DBG
  156. #pragma alloc_text(PAGE,CmpUnloadKeyWorker)
  157. #endif
  158. #pragma alloc_text(PAGE,CmMoveKey)
  159. #pragma alloc_text(PAGE,CmpDuplicateKey)
  160. #pragma alloc_text(PAGE,CmCompressKey)
  161. #pragma alloc_text(PAGE,CmpCompressKeyWorker)
  162. #pragma alloc_text(PAGE,CmpCompareNewValueDataAgainstKCBCache)
  163. #pragma alloc_text(PAGE,CmpIsHiveAlreadyLoaded)
  164. #pragma alloc_text(PAGE,CmpCheckReplaceHive)
  165. #endif
  166. NTSTATUS
  167. CmDeleteValueKey(
  168. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  169. IN UNICODE_STRING ValueName // RAW
  170. )
  171. /*++
  172. Routine Description:
  173. One of the value entries of a registry key may be removed with this call.
  174. The value entry with ValueName matching ValueName is removed from the key.
  175. If no such entry exists, an error is returned.
  176. Arguments:
  177. KeyControlBlock - pointer to kcb for key to operate on
  178. ValueName - The name of the value to be deleted. NULL is a legal name.
  179. Return Value:
  180. NTSTATUS - Result code from call, among the following:
  181. <TBS>
  182. --*/
  183. {
  184. NTSTATUS status;
  185. PCM_KEY_NODE pcell = NULL;
  186. PCHILD_LIST plist;
  187. PCM_KEY_VALUE Value = NULL;
  188. ULONG targetindex;
  189. HCELL_INDEX ChildCell;
  190. PHHIVE Hive;
  191. HCELL_INDEX Cell;
  192. ULONG realsize;
  193. LARGE_INTEGER systemtime;
  194. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmDeleteValueKey\n"));
  195. CmpLockRegistryExclusive();
  196. #ifdef CHECK_REGISTRY_USECOUNT
  197. CmpCheckRegistryUseCount();
  198. #endif //CHECK_REGISTRY_USECOUNT
  199. PERFINFO_REG_DELETE_VALUE(KeyControlBlock, &ValueName);
  200. try {
  201. //
  202. // no edits, not even this one, on keys marked for deletion
  203. //
  204. if (KeyControlBlock->Delete) {
  205. return STATUS_KEY_DELETED;
  206. }
  207. Hive = KeyControlBlock->KeyHive;
  208. Cell = KeyControlBlock->KeyCell;
  209. pcell = (PCM_KEY_NODE)HvGetCell(Hive, Cell);
  210. if( pcell == NULL ) {
  211. //
  212. // we couldn't map a view for the bin containing this cell
  213. //
  214. return STATUS_INSUFFICIENT_RESOURCES;
  215. }
  216. // Mark the hive as read only
  217. CmpMarkAllBinsReadOnly(Hive);
  218. status = STATUS_OBJECT_NAME_NOT_FOUND;
  219. plist = &(pcell->ValueList);
  220. ChildCell = HCELL_NIL;
  221. if (plist->Count != 0) {
  222. //
  223. // The parent has at least one value, map in the list of
  224. // values and call CmpFindChildInList
  225. //
  226. //
  227. // plist -> the CHILD_LIST structure
  228. // pchild -> the child node structure being examined
  229. //
  230. if( CmpFindNameInList(Hive,
  231. plist,
  232. &ValueName,
  233. &targetindex,
  234. &ChildCell) == FALSE ) {
  235. // Mark the hive as read only
  236. CmpMarkAllBinsReadOnly(Hive);
  237. return STATUS_INSUFFICIENT_RESOURCES;
  238. }
  239. if (ChildCell != HCELL_NIL) {
  240. //
  241. // 1. the desired target was found
  242. // 2. ChildCell is it's HCELL_INDEX
  243. // 3. targetaddress points to it
  244. // 4. targetindex is it's index
  245. //
  246. //
  247. // attempt to mark all relevent cells dirty
  248. //
  249. if (!(HvMarkCellDirty(Hive, Cell) &&
  250. HvMarkCellDirty(Hive, pcell->ValueList.List) &&
  251. HvMarkCellDirty(Hive, ChildCell)))
  252. {
  253. // Mark the hive as read only
  254. CmpMarkAllBinsReadOnly(Hive);
  255. return STATUS_NO_LOG_SPACE;
  256. }
  257. Value = (PCM_KEY_VALUE)HvGetCell(Hive,ChildCell);
  258. if( Value == NULL ) {
  259. //
  260. // could not map view inside
  261. // this is impossible as we just dirtied the view
  262. //
  263. ASSERT( FALSE );
  264. // Mark the hive as read only
  265. CmpMarkAllBinsReadOnly(Hive);
  266. return STATUS_INSUFFICIENT_RESOURCES;
  267. }
  268. if( !CmpMarkValueDataDirty(Hive,Value) ) {
  269. // Mark the hive as read only
  270. CmpMarkAllBinsReadOnly(Hive);
  271. return(STATUS_NO_LOG_SPACE);
  272. }
  273. // sanity
  274. ASSERT_CELL_DIRTY(Hive,pcell->ValueList.List);
  275. ASSERT_CELL_DIRTY(Hive,ChildCell);
  276. if( !NT_SUCCESS(CmpRemoveValueFromList(Hive,targetindex,plist)) ) {
  277. //
  278. // bail out !
  279. //
  280. return STATUS_INSUFFICIENT_RESOURCES;
  281. }
  282. if( CmpFreeValue(Hive, ChildCell) == FALSE ) {
  283. //
  284. // we couldn't map a view inside above call
  285. //
  286. return STATUS_INSUFFICIENT_RESOURCES;
  287. }
  288. KeQuerySystemTime(&systemtime);
  289. pcell->LastWriteTime = systemtime;
  290. // cache it in the kcb too.
  291. KeyControlBlock->KcbLastWriteTime = systemtime;
  292. // some sanity asserts
  293. ASSERT( pcell->MaxValueNameLen == KeyControlBlock->KcbMaxValueNameLen );
  294. ASSERT( pcell->MaxValueDataLen == KeyControlBlock->KcbMaxValueDataLen );
  295. ASSERT_CELL_DIRTY(Hive,Cell);
  296. if (pcell->ValueList.Count == 0) {
  297. pcell->MaxValueNameLen = 0;
  298. pcell->MaxValueDataLen = 0;
  299. // update the kcb cache too
  300. KeyControlBlock->KcbMaxValueNameLen = 0;
  301. KeyControlBlock->KcbMaxValueDataLen = 0;
  302. }
  303. //
  304. // We are changing the KCB cache. Since the registry is locked exclusively,
  305. // we do not need a KCB lock.
  306. //
  307. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  308. //
  309. // Invalidate and rebuild the cache
  310. //
  311. CmpCleanUpKcbValueCache(KeyControlBlock);
  312. CmpSetUpKcbValueCache(KeyControlBlock,plist->Count,plist->List);
  313. CmpReportNotify(
  314. KeyControlBlock,
  315. KeyControlBlock->KeyHive,
  316. KeyControlBlock->KeyCell,
  317. REG_NOTIFY_CHANGE_LAST_SET
  318. );
  319. status = STATUS_SUCCESS;
  320. } else {
  321. status = STATUS_OBJECT_NAME_NOT_FOUND;
  322. }
  323. }
  324. } finally {
  325. if(pcell != NULL){
  326. HvReleaseCell(Hive, Cell);
  327. }
  328. if(Value != NULL){
  329. ASSERT( ChildCell != HCELL_NIL );
  330. HvReleaseCell(Hive, ChildCell);
  331. }
  332. #ifdef CHECK_REGISTRY_USECOUNT
  333. CmpCheckRegistryUseCount();
  334. #endif //CHECK_REGISTRY_USECOUNT
  335. CmpUnlockRegistry();
  336. }
  337. // Mark the hive as read only
  338. CmpMarkAllBinsReadOnly(Hive);
  339. return status;
  340. }
  341. NTSTATUS
  342. CmEnumerateKey(
  343. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  344. IN ULONG Index,
  345. IN KEY_INFORMATION_CLASS KeyInformationClass,
  346. IN PVOID KeyInformation,
  347. IN ULONG Length,
  348. IN PULONG ResultLength
  349. )
  350. /*++
  351. Routine Description:
  352. Enumerate sub keys, return data on Index'th entry.
  353. CmEnumerateKey returns the name of the Index'th sub key of the open
  354. key specified. The value STATUS_NO_MORE_ENTRIES will be
  355. returned if value of Index is larger than the number of sub keys.
  356. Note that Index is simply a way to select among child keys. Two calls
  357. to CmEnumerateKey with the same Index are NOT guaranteed to return
  358. the same results.
  359. If KeyInformation is not long enough to hold all requested data,
  360. STATUS_BUFFER_OVERFLOW will be returned, and ResultLength will be
  361. set to the number of bytes actually required.
  362. Arguments:
  363. KeyControlBlock - pointer to the KCB that describes the key
  364. Index - Specifies the (0-based) number of the sub key to be returned.
  365. KeyInformationClass - Specifies the type of information returned in
  366. Buffer. One of the following types:
  367. KeyBasicInformation - return last write time, title index, and name.
  368. (see KEY_BASIC_INFORMATION structure)
  369. KeyNodeInformation - return last write time, title index, name, class.
  370. (see KEY_NODE_INFORMATION structure)
  371. KeyInformation -Supplies pointer to buffer to receive the data.
  372. Length - Length of KeyInformation in bytes.
  373. ResultLength - Number of bytes actually written into KeyInformation.
  374. Return Value:
  375. NTSTATUS - Result code from call, among the following:
  376. <TBS>
  377. --*/
  378. {
  379. NTSTATUS status;
  380. HCELL_INDEX childcell;
  381. PHHIVE Hive;
  382. HCELL_INDEX Cell;
  383. PCM_KEY_NODE Node;
  384. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmEnumerateKey\n"));
  385. CmpLockRegistry();
  386. PERFINFO_REG_ENUM_KEY(KeyControlBlock, Index);
  387. if (KeyControlBlock->Delete) {
  388. CmpUnlockRegistry();
  389. return STATUS_KEY_DELETED;
  390. }
  391. Hive = KeyControlBlock->KeyHive;
  392. Cell = KeyControlBlock->KeyCell;
  393. // Mark the hive as read only
  394. CmpMarkAllBinsReadOnly(Hive);
  395. //
  396. // fetch the child of interest
  397. //
  398. Node = (PCM_KEY_NODE)HvGetCell(Hive, Cell);
  399. if( Node == NULL ) {
  400. //
  401. // we couldn't map a view for the bin containing this cell
  402. //
  403. CmpUnlockRegistry();
  404. CmpMarkAllBinsReadOnly(Hive);
  405. return STATUS_INSUFFICIENT_RESOURCES;
  406. }
  407. childcell = CmpFindSubKeyByNumber(Hive, Node, Index);
  408. // release this cell here as we don't need this Node anymore
  409. HvReleaseCell(Hive, Cell);
  410. if (childcell == HCELL_NIL) {
  411. //
  412. // no such child, clean up and return error
  413. //
  414. // we cannot return STATUS_INSUFFICIENT_RESOURCES because of Iop
  415. // subsystem which treats INSUFFICIENT RESOURCES as no fatal error
  416. //
  417. CmpUnlockRegistry();
  418. // Mark the hive as read only
  419. CmpMarkAllBinsReadOnly(Hive);
  420. return STATUS_NO_MORE_ENTRIES;
  421. }
  422. Node = (PCM_KEY_NODE)HvGetCell(Hive,childcell);
  423. if( Node == NULL ) {
  424. //
  425. // we couldn't map a view for the bin containing this cell
  426. //
  427. CmpMarkAllBinsReadOnly(Hive);
  428. CmpUnlockRegistry();
  429. return STATUS_INSUFFICIENT_RESOURCES;
  430. }
  431. try {
  432. //
  433. // call a worker to perform data transfer
  434. //
  435. status = CmpQueryKeyData(Hive,
  436. Node,
  437. KeyInformationClass,
  438. KeyInformation,
  439. Length,
  440. ResultLength
  441. #if defined(CMP_STATS) || defined(CMP_KCB_CACHE_VALIDATION)
  442. ,
  443. NULL
  444. #endif
  445. );
  446. } except (EXCEPTION_EXECUTE_HANDLER) {
  447. HvReleaseCell(Hive, childcell);
  448. CmpUnlockRegistry();
  449. status = GetExceptionCode();
  450. // Mark the hive as read only
  451. CmpMarkAllBinsReadOnly(Hive);
  452. return status;
  453. }
  454. HvReleaseCell(Hive, childcell);
  455. CmpUnlockRegistry();
  456. // Mark the hive as read only
  457. CmpMarkAllBinsReadOnly(Hive);
  458. return status;
  459. }
  460. NTSTATUS
  461. CmEnumerateValueKey(
  462. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  463. IN ULONG Index,
  464. IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
  465. IN PVOID KeyValueInformation,
  466. IN ULONG Length,
  467. IN PULONG ResultLength
  468. )
  469. /*++
  470. Routine Description:
  471. The value entries of an open key may be enumerated.
  472. CmEnumerateValueKey returns the name of the Index'th value
  473. entry of the open key specified by KeyHandle. The value
  474. STATUS_NO_MORE_ENTRIES will be returned if value of Index is
  475. larger than the number of sub keys.
  476. Note that Index is simply a way to select among value
  477. entries. Two calls to NtEnumerateValueKey with the same Index
  478. are NOT guaranteed to return the same results.
  479. If KeyValueInformation is not long enough to hold all requested data,
  480. STATUS_BUFFER_OVERFLOW will be returned, and ResultLength will be
  481. set to the number of bytes actually required.
  482. Arguments:
  483. KeyControlBlock - pointer to the KCB that describes the key
  484. Index - Specifies the (0-based) number of the sub key to be returned.
  485. KeyValueInformationClass - Specifies the type of information returned
  486. in Buffer. One of the following types:
  487. KeyValueBasicInformation - return time of last write,
  488. title index, and name. (See KEY_VALUE_BASIC_INFORMATION)
  489. KeyValueFullInformation - return time of last write,
  490. title index, name, class. (See KEY_VALUE_FULL_INFORMATION)
  491. KeyValueInformation -Supplies pointer to buffer to receive the data.
  492. Length - Length of KeyValueInformation in bytes.
  493. ResultLength - Number of bytes actually written into KeyValueInformation.
  494. Return Value:
  495. NTSTATUS - Result code from call, among the following:
  496. <TBS>
  497. --*/
  498. {
  499. NTSTATUS status;
  500. PHHIVE Hive;
  501. PCM_KEY_NODE Node;
  502. PCELL_DATA ChildList;
  503. PCM_KEY_VALUE ValueData;
  504. BOOLEAN IndexCached;
  505. BOOLEAN ValueCached;
  506. PPCM_CACHED_VALUE ContainingList;
  507. HCELL_INDEX ValueDataCellToRelease = HCELL_NIL;
  508. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmEnumerateValueKey\n"));
  509. //
  510. // lock the parent cell
  511. //
  512. CmpLockRegistry();
  513. PERFINFO_REG_ENUM_VALUE(KeyControlBlock, Index);
  514. if (KeyControlBlock->Delete) {
  515. CmpUnlockRegistry();
  516. return STATUS_KEY_DELETED;
  517. }
  518. Hive = KeyControlBlock->KeyHive;
  519. Node = (PCM_KEY_NODE)HvGetCell(Hive, KeyControlBlock->KeyCell);
  520. if( Node == NULL ) {
  521. //
  522. // we couldn't map a view for the bin containing this cell
  523. //
  524. CmpUnlockRegistry();
  525. return STATUS_INSUFFICIENT_RESOURCES;
  526. }
  527. //
  528. // fetch the child of interest
  529. //
  530. //
  531. // Do it using the cache
  532. //
  533. if (Index >= KeyControlBlock->ValueCache.Count) {
  534. //
  535. // No such child, clean up and return error.
  536. //
  537. HvReleaseCell(Hive, KeyControlBlock->KeyCell);
  538. CmpUnlockRegistry();
  539. return(STATUS_NO_MORE_ENTRIES);
  540. }
  541. // Mark the hive as read only
  542. CmpMarkAllBinsReadOnly(Hive);
  543. BEGIN_KCB_LOCK_GUARD;
  544. CmpLockKCBTreeExclusive();
  545. if (KeyControlBlock->ExtFlags & CM_KCB_SYM_LINK_FOUND) {
  546. //
  547. // The value list is now set to the KCB for symbolic link,
  548. // Clean it up and set the value right before we do the query.
  549. //
  550. CmpCleanUpKcbValueCache(KeyControlBlock);
  551. CmpSetUpKcbValueCache(KeyControlBlock,Node->ValueList.Count,Node->ValueList.List);
  552. }
  553. ChildList = CmpGetValueListFromCache(Hive, &(KeyControlBlock->ValueCache), &IndexCached);
  554. if( ChildList == NULL ) {
  555. //
  556. // couldn't map view; treat it as insufficient resources
  557. //
  558. HvReleaseCell(Hive, KeyControlBlock->KeyCell);
  559. // Mark the hive as read only
  560. CmpMarkAllBinsReadOnly(Hive);
  561. CmpUnlockKCBTree();
  562. CmpUnlockRegistry();
  563. return(STATUS_INSUFFICIENT_RESOURCES);
  564. }
  565. ValueData = CmpGetValueKeyFromCache(Hive, ChildList, Index, &ContainingList, IndexCached, &ValueCached,&ValueDataCellToRelease);
  566. if( ValueData == NULL ) {
  567. //
  568. // couldn't map view; treat it as insufficient resources
  569. //
  570. HvReleaseCell(Hive, KeyControlBlock->KeyCell);
  571. if( ValueDataCellToRelease != HCELL_NIL ) {
  572. HvReleaseCell(Hive,ValueDataCellToRelease);
  573. }
  574. // Mark the hive as read only
  575. CmpMarkAllBinsReadOnly(Hive);
  576. CmpUnlockKCBTree();
  577. CmpUnlockRegistry();
  578. return(STATUS_INSUFFICIENT_RESOURCES);
  579. }
  580. END_KCB_LOCK_GUARD;
  581. // Trying to catch the BAD guy who writes over our pool.
  582. CmpMakeValueCacheReadWrite(ValueCached,CMP_GET_CACHED_ADDRESS(KeyControlBlock->ValueCache.ValueList));
  583. try {
  584. //
  585. // call a worker to perform data transfer; we are touching user-mode address; do it in a try/except
  586. //
  587. status = CmpQueryKeyValueData(Hive,
  588. ContainingList,
  589. ValueData,
  590. ValueCached,
  591. KeyValueInformationClass,
  592. KeyValueInformation,
  593. Length,
  594. ResultLength);
  595. } except (EXCEPTION_EXECUTE_HANDLER) {
  596. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"CmEnumerateValueKey: code:%08lx\n", GetExceptionCode()));
  597. status = GetExceptionCode();
  598. }
  599. // Trying to catch the BAD guy who writes over our pool.
  600. CmpMakeValueCacheReadOnly(ValueCached,CMP_GET_CACHED_ADDRESS(KeyControlBlock->ValueCache.ValueList));
  601. HvReleaseCell(Hive, KeyControlBlock->KeyCell);
  602. if( ValueDataCellToRelease != HCELL_NIL ) {
  603. HvReleaseCell(Hive,ValueDataCellToRelease);
  604. }
  605. CmpUnlockKCBTree();
  606. CmpUnlockRegistry();
  607. // Mark the hive as read only
  608. CmpMarkAllBinsReadOnly(Hive);
  609. return status;
  610. }
  611. NTSTATUS
  612. CmFlushKey(
  613. IN PHHIVE Hive,
  614. IN HCELL_INDEX Cell
  615. )
  616. /*++
  617. Routine Description:
  618. Forces changes made to a key to disk.
  619. CmFlushKey will not return to its caller until any changed data
  620. associated with the key has been written out.
  621. WARNING: CmFlushKey will flush the entire registry tree, and thus will
  622. burn cycles and I/O.
  623. Arguments:
  624. Hive - supplies a pointer to the hive control structure for the hive
  625. Cell - supplies index of node to whose sub keys are to be found
  626. Return Value:
  627. NTSTATUS - Result code from call, among the following:
  628. <TBS>
  629. --*/
  630. {
  631. PCMHIVE CmHive;
  632. NTSTATUS status = STATUS_SUCCESS;
  633. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmFlushKey\n"));
  634. //
  635. // If writes are not working, lie and say we succeeded, will
  636. // clean up in a short time. Only early system init code
  637. // will ever know the difference.
  638. //
  639. if (CmpNoWrite) {
  640. return STATUS_SUCCESS;
  641. }
  642. // Mark the hive as read only
  643. CmpMarkAllBinsReadOnly(Hive);
  644. CmHive = CONTAINING_RECORD(Hive, CMHIVE, Hive);
  645. //
  646. // Don't flush the master hive. If somebody asks for a flushkey on
  647. // the master hive, do a CmpDoFlushAll instead. CmpDoFlushAll flushes
  648. // every hive except the master hive, which is what they REALLY want.
  649. //
  650. if (CmHive == CmpMasterHive) {
  651. CmpDoFlushAll(FALSE);
  652. } else {
  653. DCmCheckRegistry(CONTAINING_RECORD(Hive, CMHIVE, Hive));
  654. CmLockHive (CmHive);
  655. CmLockHiveViews (CmHive);
  656. if( HvHiveWillShrink( &(CmHive->Hive) ) ) {
  657. //
  658. // we may end up here is when the hive shrinks and we need
  659. // exclusive access over the registry, as we are going to CcPurge !
  660. //
  661. CmUnlockHiveViews (CmHive);
  662. CmUnlockHive (CmHive);
  663. CmpUnlockRegistry();
  664. CmpLockRegistryExclusive();
  665. #ifdef CHECK_REGISTRY_USECOUNT
  666. CmpCheckRegistryUseCount();
  667. #endif //CHECK_REGISTRY_USECOUNT
  668. CmLockHive (CmHive);
  669. if( CmHive->UseCount != 0) {
  670. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  671. CmpFixHiveUsageCount(CmHive);
  672. ASSERT( CmHive->UseCount == 0 );
  673. }
  674. } else {
  675. //
  676. // release the views
  677. //
  678. CmUnlockHiveViews (CmHive);
  679. }
  680. if (! HvSyncHive(Hive)) {
  681. status = STATUS_REGISTRY_IO_FAILED;
  682. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmFlushKey: HvSyncHive failed\n"));
  683. }
  684. CmUnlockHive (CmHive);
  685. }
  686. // Mark the hive as read only
  687. CmpMarkAllBinsReadOnly(Hive);
  688. return status;
  689. }
  690. NTSTATUS
  691. CmQueryKey(
  692. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  693. IN KEY_INFORMATION_CLASS KeyInformationClass,
  694. IN PVOID KeyInformation,
  695. IN ULONG Length,
  696. IN PULONG ResultLength
  697. )
  698. /*++
  699. Routine Description:
  700. Data about the class of a key, and the numbers and sizes of its
  701. children and value entries may be queried with CmQueryKey.
  702. NOTE: The returned lengths are guaranteed to be at least as
  703. long as the described values, but may be longer in
  704. some circumstances.
  705. Arguments:
  706. KeyControlBlock - pointer to the KCB that describes the key
  707. KeyInformationClass - Specifies the type of information
  708. returned in Buffer. One of the following types:
  709. KeyBasicInformation - return last write time, title index, and name.
  710. (See KEY_BASIC_INFORMATION)
  711. KeyNodeInformation - return last write time, title index, name, class.
  712. (See KEY_NODE_INFORMATION)
  713. KeyFullInformation - return all data except for name and security.
  714. (See KEY_FULL_INFORMATION)
  715. KeyInformation -Supplies pointer to buffer to receive the data.
  716. Length - Length of KeyInformation in bytes.
  717. ResultLength - Number of bytes actually written into KeyInformation.
  718. Return Value:
  719. NTSTATUS - Result code from call, among the following:
  720. <TBS>
  721. --*/
  722. {
  723. NTSTATUS status;
  724. PCM_KEY_NODE Node = NULL;
  725. PUNICODE_STRING Name = NULL;
  726. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmQueryKey\n"));
  727. CmpLockRegistry();
  728. PERFINFO_REG_QUERY_KEY(KeyControlBlock);
  729. // Mark the hive as read only
  730. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  731. try {
  732. //
  733. // request for the FULL path of the key
  734. //
  735. if( KeyInformationClass == KeyNameInformation ) {
  736. if (KeyControlBlock->Delete ) {
  737. //
  738. // special case: return key deleted status, but still fill the full name of the key.
  739. //
  740. status = STATUS_KEY_DELETED;
  741. } else {
  742. status = STATUS_SUCCESS;
  743. }
  744. if( KeyControlBlock->NameBlock ) {
  745. Name = CmpConstructName(KeyControlBlock);
  746. if (Name == NULL) {
  747. status = STATUS_INSUFFICIENT_RESOURCES;
  748. } else {
  749. ULONG requiredlength;
  750. ULONG minimumlength;
  751. USHORT NameLength;
  752. LONG leftlength;
  753. PKEY_INFORMATION pbuffer = (PKEY_INFORMATION)KeyInformation;
  754. NameLength = Name->Length;
  755. requiredlength = FIELD_OFFSET(KEY_NAME_INFORMATION, Name) + NameLength;
  756. minimumlength = FIELD_OFFSET(KEY_NAME_INFORMATION, Name);
  757. *ResultLength = requiredlength;
  758. if (Length < minimumlength) {
  759. status = STATUS_BUFFER_TOO_SMALL;
  760. } else {
  761. //
  762. // Fill in the length of the name
  763. //
  764. pbuffer->KeyNameInformation.NameLength = NameLength;
  765. //
  766. // Now copy the full name into the user buffer, if enough space
  767. //
  768. leftlength = Length - minimumlength;
  769. requiredlength = NameLength;
  770. if (leftlength < (LONG)requiredlength) {
  771. requiredlength = leftlength;
  772. status = STATUS_BUFFER_OVERFLOW;
  773. }
  774. //
  775. // If not enough space, copy how much we can and return overflow
  776. //
  777. RtlCopyMemory(
  778. &(pbuffer->KeyNameInformation.Name[0]),
  779. Name->Buffer,
  780. requiredlength
  781. );
  782. }
  783. }
  784. }
  785. } else if(KeyControlBlock->Delete ) {
  786. //
  787. // key already deleted
  788. //
  789. status = STATUS_KEY_DELETED;
  790. } else if( KeyInformationClass == KeyFlagsInformation ) {
  791. //
  792. // we only want to get the user defined flags;
  793. //
  794. PKEY_INFORMATION pbuffer = (PKEY_INFORMATION)KeyInformation;
  795. ULONG requiredlength;
  796. requiredlength = sizeof(KEY_FLAGS_INFORMATION);
  797. *ResultLength = requiredlength;
  798. if (Length < requiredlength) {
  799. status = STATUS_BUFFER_TOO_SMALL;
  800. } else {
  801. pbuffer->KeyFlagsInformation.UserFlags = (ULONG)((USHORT)KeyControlBlock->Flags >> KEY_USER_FLAGS_SHIFT);
  802. status = STATUS_SUCCESS;
  803. }
  804. } else {
  805. //
  806. // call a worker to perform data transfer
  807. //
  808. if( KeyInformationClass == KeyCachedInformation ) {
  809. //
  810. // call the fast version
  811. //
  812. status = CmpQueryKeyDataFromCache( KeyControlBlock,
  813. KeyInformationClass,
  814. KeyInformation,
  815. Length,
  816. ResultLength );
  817. } else {
  818. //
  819. // old'n plain slow version
  820. //
  821. Node = (PCM_KEY_NODE)HvGetCell(KeyControlBlock->KeyHive, KeyControlBlock->KeyCell);
  822. if( Node == NULL ) {
  823. //
  824. // we couldn't map a view for the bin containing this cell
  825. //
  826. status = STATUS_INSUFFICIENT_RESOURCES;
  827. } else {
  828. status = CmpQueryKeyData(KeyControlBlock->KeyHive,
  829. Node,
  830. KeyInformationClass,
  831. KeyInformation,
  832. Length,
  833. ResultLength
  834. #if defined(CMP_STATS) || defined(CMP_KCB_CACHE_VALIDATION)
  835. ,
  836. KeyControlBlock
  837. #endif
  838. );
  839. }
  840. }
  841. }
  842. } finally {
  843. if( Node != NULL ) {
  844. HvReleaseCell(KeyControlBlock->KeyHive, KeyControlBlock->KeyCell);
  845. }
  846. if( Name != NULL ) {
  847. ExFreePoolWithTag(Name, CM_NAME_TAG | PROTECTED_POOL);
  848. }
  849. CmpUnlockRegistry();
  850. }
  851. // Mark the hive as read only
  852. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  853. return status;
  854. }
  855. NTSTATUS
  856. CmQueryValueKey(
  857. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  858. IN UNICODE_STRING ValueName,
  859. IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
  860. IN PVOID KeyValueInformation,
  861. IN ULONG Length,
  862. IN PULONG ResultLength
  863. )
  864. /*++
  865. Routine Description:
  866. The ValueName, TitleIndex, Type, and Data for any one of a key's
  867. value entries may be queried with CmQueryValueKey.
  868. If KeyValueInformation is not long enough to hold all requested data,
  869. STATUS_BUFFER_OVERFLOW will be returned, and ResultLength will be
  870. set to the number of bytes actually required.
  871. Arguments:
  872. KeyControlBlock - pointer to the KCB that describes the key
  873. ValueName - The name of the value entry to return data for.
  874. KeyValueInformationClass - Specifies the type of information
  875. returned in KeyValueInformation. One of the following types:
  876. KeyValueBasicInformation - return time of last write, title
  877. index, and name. (See KEY_VALUE_BASIC_INFORMATION)
  878. KeyValueFullInformation - return time of last write, title
  879. index, name, class. (See KEY_VALUE_FULL_INFORMATION)
  880. KeyValueInformation -Supplies pointer to buffer to receive the data.
  881. Length - Length of KeyValueInformation in bytes.
  882. ResultLength - Number of bytes actually written into KeyValueInformation.
  883. Return Value:
  884. NTSTATUS - Result code from call, among the following:
  885. <TBS>
  886. --*/
  887. {
  888. NTSTATUS status;
  889. HCELL_INDEX childcell;
  890. PHCELL_INDEX childindex;
  891. HCELL_INDEX Cell;
  892. PCM_KEY_VALUE ValueData;
  893. ULONG Index;
  894. BOOLEAN ValueCached;
  895. PPCM_CACHED_VALUE ContainingList;
  896. HCELL_INDEX ValueDataCellToRelease = HCELL_NIL;
  897. PAGED_CODE();
  898. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmQueryValueKey\n"));
  899. CmpLockRegistry();
  900. PERFINFO_REG_QUERY_VALUE(KeyControlBlock, &ValueName);
  901. if (KeyControlBlock->Delete) {
  902. CmpUnlockRegistry();
  903. return STATUS_KEY_DELETED;
  904. }
  905. // Mark the hive as read only
  906. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  907. BEGIN_KCB_LOCK_GUARD;
  908. CmpLockKCBTreeExclusive();
  909. if (KeyControlBlock->ExtFlags & CM_KCB_SYM_LINK_FOUND) {
  910. //
  911. // The value list is now set to the KCB for symbolic link,
  912. // Clean it up and set the value right before we do the query.
  913. //
  914. CmpCleanUpKcbValueCache(KeyControlBlock);
  915. {
  916. PCM_KEY_NODE Node = (PCM_KEY_NODE)HvGetCell(KeyControlBlock->KeyHive, KeyControlBlock->KeyCell);
  917. if( Node == NULL ) {
  918. //
  919. // we couldn't map a view for the bin containing this cell
  920. //
  921. CmpUnlockKCBTree();
  922. CmpUnlockRegistry();
  923. // Mark the hive as read only
  924. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  925. return STATUS_INSUFFICIENT_RESOURCES;
  926. }
  927. CmpSetUpKcbValueCache(KeyControlBlock,Node->ValueList.Count,Node->ValueList.List);
  928. HvReleaseCell(KeyControlBlock->KeyHive, KeyControlBlock->KeyCell);
  929. }
  930. }
  931. //
  932. // Find the data
  933. //
  934. ValueData = CmpFindValueByNameFromCache(KeyControlBlock->KeyHive,
  935. &(KeyControlBlock->ValueCache),
  936. &ValueName,
  937. &ContainingList,
  938. &Index,
  939. &ValueCached,
  940. &ValueDataCellToRelease
  941. );
  942. END_KCB_LOCK_GUARD;
  943. if (ValueData) {
  944. // Trying to catch the BAD guy who writes over our pool.
  945. CmpMakeValueCacheReadWrite(ValueCached,CMP_GET_CACHED_ADDRESS(KeyControlBlock->ValueCache.ValueList));
  946. try {
  947. //
  948. // call a worker to perform data transfer; we are touching user-mode address; do it in a try/except
  949. //
  950. status = CmpQueryKeyValueData(KeyControlBlock->KeyHive,
  951. ContainingList,
  952. ValueData,
  953. ValueCached,
  954. KeyValueInformationClass,
  955. KeyValueInformation,
  956. Length,
  957. ResultLength);
  958. } except (EXCEPTION_EXECUTE_HANDLER) {
  959. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"CmQueryValueKey: code:%08lx\n", GetExceptionCode()));
  960. status = GetExceptionCode();
  961. }
  962. // Trying to catch the BAD guy who writes over our pool.
  963. CmpMakeValueCacheReadOnly(ValueCached,CMP_GET_CACHED_ADDRESS(KeyControlBlock->ValueCache.ValueList));
  964. } else {
  965. status = STATUS_OBJECT_NAME_NOT_FOUND;
  966. }
  967. if(ValueDataCellToRelease != HCELL_NIL) {
  968. HvReleaseCell(KeyControlBlock->KeyHive,ValueDataCellToRelease);
  969. }
  970. CmpUnlockKCBTree();
  971. CmpUnlockRegistry();
  972. // Mark the hive as read only
  973. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  974. return status;
  975. }
  976. NTSTATUS
  977. CmQueryMultipleValueKey(
  978. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  979. IN PKEY_VALUE_ENTRY ValueEntries,
  980. IN ULONG EntryCount,
  981. IN PVOID ValueBuffer,
  982. IN OUT PULONG BufferLength,
  983. IN OPTIONAL PULONG ResultLength
  984. )
  985. /*++
  986. Routine Description:
  987. Multiple values of any key may be queried atomically with
  988. this api.
  989. Arguments:
  990. KeyControlBlock - Supplies the key to be queried.
  991. ValueEntries - Returns an array of KEY_VALUE_ENTRY structures, one for each value.
  992. EntryCount - Supplies the number of entries in the ValueNames and ValueEntries arrays
  993. ValueBuffer - Returns the value data for each value.
  994. BufferLength - Supplies the length of the ValueBuffer array in bytes.
  995. Returns the length of the ValueBuffer array that was filled in.
  996. ResultLength - if present, Returns the length in bytes of the ValueBuffer
  997. array required to return the requested values of this key.
  998. Return Value:
  999. NTSTATUS
  1000. --*/
  1001. {
  1002. PHHIVE Hive;
  1003. NTSTATUS Status;
  1004. ULONG i;
  1005. UNICODE_STRING CurrentName;
  1006. HCELL_INDEX ValueCell = HCELL_NIL;
  1007. PCM_KEY_VALUE ValueNode;
  1008. ULONG RequiredLength = 0;
  1009. ULONG UsedLength = 0;
  1010. ULONG DataLength;
  1011. BOOLEAN BufferFull = FALSE;
  1012. BOOLEAN Small;
  1013. PUCHAR Data;
  1014. KPROCESSOR_MODE PreviousMode;
  1015. PCM_KEY_NODE Node;
  1016. PAGED_CODE();
  1017. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmQueryMultipleValueKey\n"));
  1018. CmpLockRegistry();
  1019. if (KeyControlBlock->Delete) {
  1020. CmpUnlockRegistry();
  1021. return STATUS_KEY_DELETED;
  1022. }
  1023. Hive = KeyControlBlock->KeyHive;
  1024. Status = STATUS_SUCCESS;
  1025. Node = (PCM_KEY_NODE)HvGetCell(Hive, KeyControlBlock->KeyCell);
  1026. if( Node == NULL ) {
  1027. //
  1028. // we couldn't map a view for the bin containing this cell
  1029. //
  1030. CmpUnlockRegistry();
  1031. return STATUS_INSUFFICIENT_RESOURCES;
  1032. }
  1033. // Mark the hive as read only
  1034. CmpMarkAllBinsReadOnly(Hive);
  1035. PreviousMode = KeGetPreviousMode();
  1036. try {
  1037. for (i=0; i < EntryCount; i++) {
  1038. //
  1039. // find the data
  1040. //
  1041. if (PreviousMode == UserMode) {
  1042. CurrentName = ProbeAndReadUnicodeString(ValueEntries[i].ValueName);
  1043. ProbeForRead(CurrentName.Buffer,CurrentName.Length,sizeof(WCHAR));
  1044. } else {
  1045. CurrentName = *(ValueEntries[i].ValueName);
  1046. }
  1047. PERFINFO_REG_QUERY_MULTIVALUE(KeyControlBlock, &CurrentName);
  1048. ValueCell = CmpFindValueByName(Hive,
  1049. Node,
  1050. &CurrentName);
  1051. if (ValueCell != HCELL_NIL) {
  1052. ValueNode = (PCM_KEY_VALUE)HvGetCell(Hive, ValueCell);
  1053. if( ValueNode == NULL ) {
  1054. //
  1055. // we couldn't map a view for the bin containing this cell
  1056. //
  1057. Status = STATUS_INSUFFICIENT_RESOURCES;
  1058. break;
  1059. }
  1060. Small = CmpIsHKeyValueSmall(DataLength, ValueNode->DataLength);
  1061. //
  1062. // Round up UsedLength and RequiredLength to a ULONG boundary
  1063. //
  1064. UsedLength = (UsedLength + sizeof(ULONG)-1) & ~(sizeof(ULONG)-1);
  1065. RequiredLength = (RequiredLength + sizeof(ULONG)-1) & ~(sizeof(ULONG)-1);
  1066. //
  1067. // If there is enough room for this data value in the buffer,
  1068. // fill it in now. Otherwise, mark the buffer as full. We must
  1069. // keep iterating through the values in order to determine the
  1070. // RequiredLength.
  1071. //
  1072. if ((UsedLength + DataLength <= *BufferLength) &&
  1073. (!BufferFull)) {
  1074. PCELL_DATA Buffer;
  1075. BOOLEAN BufferAllocated;
  1076. HCELL_INDEX CellToRelease;
  1077. //
  1078. // get the data from source, regardless of the size
  1079. //
  1080. if( CmpGetValueData(Hive,ValueNode,&DataLength,&Buffer,&BufferAllocated,&CellToRelease) == FALSE ) {
  1081. //
  1082. // insufficient resources; return NULL
  1083. //
  1084. ASSERT( BufferAllocated == FALSE );
  1085. ASSERT( Buffer == NULL );
  1086. Status = STATUS_INSUFFICIENT_RESOURCES;
  1087. break;
  1088. }
  1089. RtlCopyMemory((PUCHAR)ValueBuffer + UsedLength,
  1090. Buffer,
  1091. DataLength);
  1092. //
  1093. // cleanup the temporary buffer
  1094. //
  1095. if( BufferAllocated == TRUE ) {
  1096. ExFreePool( Buffer );
  1097. }
  1098. //
  1099. // release the buffer in case we are using hive storage
  1100. //
  1101. if( CellToRelease != HCELL_NIL ) {
  1102. HvReleaseCell(Hive,CellToRelease);
  1103. }
  1104. ValueEntries[i].Type = ValueNode->Type;
  1105. ValueEntries[i].DataLength = DataLength;
  1106. ValueEntries[i].DataOffset = UsedLength;
  1107. UsedLength += DataLength;
  1108. } else {
  1109. BufferFull = TRUE;
  1110. Status = STATUS_BUFFER_OVERFLOW;
  1111. }
  1112. RequiredLength += DataLength;
  1113. HvReleaseCell(Hive, ValueCell);
  1114. ValueCell = HCELL_NIL;
  1115. } else {
  1116. Status = STATUS_OBJECT_NAME_NOT_FOUND;
  1117. break;
  1118. }
  1119. }
  1120. if (NT_SUCCESS(Status) ||
  1121. (Status == STATUS_BUFFER_OVERFLOW)) {
  1122. *BufferLength = UsedLength;
  1123. if (ARGUMENT_PRESENT(ResultLength)) {
  1124. *ResultLength = RequiredLength;
  1125. }
  1126. }
  1127. } finally {
  1128. if( ValueCell != HCELL_NIL) {
  1129. HvReleaseCell(Hive, ValueCell);
  1130. }
  1131. HvReleaseCell(Hive, KeyControlBlock->KeyCell);
  1132. CmpUnlockRegistry();
  1133. }
  1134. // Mark the hive as read only
  1135. CmpMarkAllBinsReadOnly(Hive);
  1136. return Status;
  1137. }
  1138. NTSTATUS
  1139. CmSetValueKey(
  1140. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  1141. IN PUNICODE_STRING ValueName,
  1142. IN ULONG Type,
  1143. IN PVOID Data,
  1144. IN ULONG DataSize
  1145. )
  1146. /*++
  1147. Routine Description:
  1148. A value entry may be created or replaced with CmSetValueKey.
  1149. If a value entry with a Value ID (i.e. name) matching the
  1150. one specified by ValueName exists, it is deleted and replaced
  1151. with the one specified. If no such value entry exists, a new
  1152. one is created. NULL is a legal Value ID. While Value IDs must
  1153. be unique within any given key, the same Value ID may appear
  1154. in many different keys.
  1155. Arguments:
  1156. KeyControlBlock - pointer to kcb for the key to operate on
  1157. ValueName - The unique (relative to the containing key) name
  1158. of the value entry. May be NULL.
  1159. Type - The integer type number of the value entry.
  1160. Data - Pointer to buffer with actual data for the value entry.
  1161. DataSize - Size of Data buffer.
  1162. Return Value:
  1163. NTSTATUS - Result code from call, among the following:
  1164. <TBS>
  1165. --*/
  1166. {
  1167. NTSTATUS status;
  1168. PCM_KEY_NODE parent = NULL;
  1169. HCELL_INDEX oldchild;
  1170. ULONG count;
  1171. PHHIVE Hive;
  1172. HCELL_INDEX Cell;
  1173. ULONG StorageType;
  1174. ULONG TempData;
  1175. BOOLEAN found;
  1176. PCM_KEY_VALUE Value = NULL;
  1177. LARGE_INTEGER systemtime;
  1178. ULONG mustChange=FALSE;
  1179. ULONG ChildIndex;
  1180. HCELL_INDEX ParentToRelease = HCELL_NIL;
  1181. HCELL_INDEX ChildToRelease = HCELL_NIL;
  1182. PERFINFO_REG_SET_VALUE_DECL();
  1183. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmSetValueKey\n"));
  1184. CmpLockRegistry();
  1185. ASSERT(sizeof(ULONG) == CM_KEY_VALUE_SMALL);
  1186. PERFINFO_REG_SET_VALUE(KeyControlBlock);
  1187. // Mark the hive as read only
  1188. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  1189. while (TRUE) {
  1190. //
  1191. // Check that we are not being asked to add a value to a key
  1192. // that has been deleted
  1193. //
  1194. if (KeyControlBlock->Delete == TRUE) {
  1195. status = STATUS_KEY_DELETED;
  1196. goto Exit;
  1197. }
  1198. //
  1199. // Check to see if this is a symbolic link node. If so caller
  1200. // is only allowed to create/change the SymbolicLinkValue
  1201. // value name
  1202. //
  1203. #ifdef CMP_KCB_CACHE_VALIDATION
  1204. {
  1205. PCM_KEY_NODE Node;
  1206. Node = (PCM_KEY_NODE)HvGetCell(KeyControlBlock->KeyHive, KeyControlBlock->KeyCell);
  1207. if( Node == NULL ) {
  1208. //
  1209. // we couldn't map a view for the bin containing this cell
  1210. //
  1211. status = STATUS_INSUFFICIENT_RESOURCES;
  1212. goto Exit;
  1213. }
  1214. ASSERT( Node->Flags == KeyControlBlock->Flags );
  1215. HvReleaseCell(KeyControlBlock->KeyHive, KeyControlBlock->KeyCell);
  1216. }
  1217. #endif
  1218. if (KeyControlBlock->Flags & KEY_SYM_LINK &&
  1219. (( (Type != REG_LINK)
  1220. #ifdef CM_DYN_SYM_LINK
  1221. && (Type != REG_DYN_LINK)
  1222. #endif //CM_DYN_SYM_LINK
  1223. ) ||
  1224. ValueName == NULL ||
  1225. !RtlEqualUnicodeString(&CmSymbolicLinkValueName, ValueName, TRUE)))
  1226. {
  1227. //
  1228. // Disallow attempts to manipulate any value names under a symbolic link
  1229. // except for the "SymbolicLinkValue" value name or type other than REG_LINK
  1230. //
  1231. // Mark the hive as read only
  1232. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  1233. status = STATUS_ACCESS_DENIED;
  1234. goto Exit;
  1235. }
  1236. if( mustChange == FALSE ) {
  1237. //
  1238. // first iteration; look inside the kcb cache
  1239. //
  1240. if( CmpCompareNewValueDataAgainstKCBCache(KeyControlBlock,ValueName,Type,Data,DataSize) == TRUE ) {
  1241. //
  1242. // the value is in the cache and is the same; make this call a noop
  1243. //
  1244. status = STATUS_SUCCESS;
  1245. goto Exit;
  1246. }
  1247. //
  1248. // To Get here, we must either be changing a value, or setting a new one
  1249. //
  1250. mustChange=TRUE;
  1251. } else {
  1252. //
  1253. // second iteration; look inside the hive
  1254. //
  1255. //
  1256. // get reference to parent key,
  1257. //
  1258. Hive = KeyControlBlock->KeyHive;
  1259. Cell = KeyControlBlock->KeyCell;
  1260. if( ParentToRelease != HCELL_NIL ) {
  1261. HvReleaseCell(Hive,ParentToRelease);
  1262. ParentToRelease = HCELL_NIL;
  1263. }
  1264. parent = (PCM_KEY_NODE)HvGetCell(Hive, Cell);
  1265. if( parent == NULL ) {
  1266. //
  1267. // we couldn't map a view for the bin containing this cell
  1268. //
  1269. status = STATUS_INSUFFICIENT_RESOURCES;
  1270. goto Exit;
  1271. }
  1272. ParentToRelease = Cell;
  1273. //
  1274. // try to find an existing value entry by the same name
  1275. //
  1276. count = parent->ValueList.Count;
  1277. found = FALSE;
  1278. if (count > 0) {
  1279. if( CmpFindNameInList(Hive,
  1280. &parent->ValueList,
  1281. ValueName,
  1282. &ChildIndex,
  1283. &oldchild) == FALSE ) {
  1284. //
  1285. // we couldn't map a view for the bin containing this cell
  1286. //
  1287. status = STATUS_INSUFFICIENT_RESOURCES;
  1288. goto Exit;
  1289. }
  1290. if (oldchild != HCELL_NIL) {
  1291. if( ChildToRelease != HCELL_NIL ) {
  1292. HvReleaseCell(Hive,ChildToRelease);
  1293. ChildToRelease = HCELL_NIL;
  1294. }
  1295. Value = (PCM_KEY_VALUE)HvGetCell(Hive,oldchild);
  1296. if( Value == NULL ) {
  1297. //
  1298. // could no map view
  1299. //
  1300. status = STATUS_INSUFFICIENT_RESOURCES;
  1301. goto Exit;
  1302. }
  1303. ChildToRelease = oldchild;
  1304. found = TRUE;
  1305. }
  1306. } else {
  1307. //
  1308. // empty list; add it first
  1309. //
  1310. ChildIndex = 0;
  1311. }
  1312. //
  1313. // Performance Hack:
  1314. // If a Set is asking us to set a key to the current value (IE does this a lot)
  1315. // drop it (and, therefore, the last modified time) on the floor, but return success
  1316. // this stops the page from being dirtied, and us having to flush the registry.
  1317. //
  1318. //
  1319. break;
  1320. }
  1321. //
  1322. // We're going through these gyrations so that if someone does come in and try and delete the
  1323. // key we're setting we're safe. Once we know we have to change the key, take the
  1324. // Exclusive (write) lock then restart
  1325. //
  1326. //
  1327. CmpUnlockRegistry();
  1328. CmpLockRegistryExclusive();
  1329. #ifdef CHECK_REGISTRY_USECOUNT
  1330. CmpCheckRegistryUseCount();
  1331. #endif //CHECK_REGISTRY_USECOUNT
  1332. }// while
  1333. ASSERT( mustChange == TRUE );
  1334. // It's a different or new value, mark it dirty, since we'll
  1335. // at least set its time stamp
  1336. if (! HvMarkCellDirty(Hive, Cell)) {
  1337. status = STATUS_NO_LOG_SPACE;
  1338. goto Exit;
  1339. }
  1340. StorageType = HvGetCellType(Cell);
  1341. //
  1342. // stash small data if relevent
  1343. //
  1344. TempData = 0;
  1345. if ((DataSize <= CM_KEY_VALUE_SMALL) &&
  1346. (DataSize > 0))
  1347. {
  1348. try {
  1349. RtlMoveMemory( // yes, move memory, could be 1 byte
  1350. &TempData, // at the end of a page.
  1351. Data,
  1352. DataSize
  1353. );
  1354. } except (EXCEPTION_EXECUTE_HANDLER) {
  1355. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"!!CmSetValueKey: code:%08lx\n", GetExceptionCode()));
  1356. status = GetExceptionCode();
  1357. goto Exit;
  1358. }
  1359. }
  1360. if (found) {
  1361. //
  1362. // ----- Existing Value Entry Path -----
  1363. //
  1364. //
  1365. // An existing value entry of the specified name exists,
  1366. // set our data into it.
  1367. //
  1368. status = CmpSetValueKeyExisting(Hive,
  1369. oldchild,
  1370. Value,
  1371. Type,
  1372. Data,
  1373. DataSize,
  1374. StorageType,
  1375. TempData);
  1376. PERFINFO_REG_SET_VALUE_EXIST();
  1377. } else {
  1378. //
  1379. // ----- New Value Entry Path -----
  1380. //
  1381. //
  1382. // Either there are no existing value entries, or the one
  1383. // specified is not in the list. In either case, create and
  1384. // fill a new one, and add it to the list
  1385. //
  1386. status = CmpSetValueKeyNew(Hive,
  1387. parent,
  1388. ValueName,
  1389. ChildIndex,
  1390. Type,
  1391. Data,
  1392. DataSize,
  1393. StorageType,
  1394. TempData);
  1395. PERFINFO_REG_SET_VALUE_NEW();
  1396. }
  1397. if (NT_SUCCESS(status)) {
  1398. // sanity assert
  1399. ASSERT( parent->MaxValueNameLen == KeyControlBlock->KcbMaxValueNameLen );
  1400. if (parent->MaxValueNameLen < ValueName->Length) {
  1401. parent->MaxValueNameLen = ValueName->Length;
  1402. // update the kcb cache too
  1403. KeyControlBlock->KcbMaxValueNameLen = ValueName->Length;
  1404. }
  1405. //sanity assert
  1406. ASSERT( parent->MaxValueDataLen == KeyControlBlock->KcbMaxValueDataLen );
  1407. if (parent->MaxValueDataLen < DataSize) {
  1408. parent->MaxValueDataLen = DataSize;
  1409. // update the kcb cache too
  1410. KeyControlBlock->KcbMaxValueDataLen = parent->MaxValueDataLen;
  1411. }
  1412. KeQuerySystemTime(&systemtime);
  1413. parent->LastWriteTime = systemtime;
  1414. // update the kcb cache too.
  1415. KeyControlBlock->KcbLastWriteTime = systemtime;
  1416. //
  1417. // Update the cache, no need for KCB lock as the registry is locked exclusively.
  1418. //
  1419. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  1420. if( found && (CMP_IS_CELL_CACHED(KeyControlBlock->ValueCache.ValueList)) ) {
  1421. //
  1422. // invalidate only the entry we changed.
  1423. //
  1424. PULONG_PTR CachedList = (PULONG_PTR) CMP_GET_CACHED_CELLDATA(KeyControlBlock->ValueCache.ValueList);
  1425. if (CMP_IS_CELL_CACHED(CachedList[ChildIndex])) {
  1426. ExFreePool((PVOID) CMP_GET_CACHED_ADDRESS(CachedList[ChildIndex]));
  1427. }
  1428. CachedList[ChildIndex] = oldchild;
  1429. } else {
  1430. //
  1431. // rebuild ALL KCB cache
  1432. //
  1433. CmpCleanUpKcbValueCache(KeyControlBlock);
  1434. CmpSetUpKcbValueCache(KeyControlBlock,parent->ValueList.Count,parent->ValueList.List);
  1435. }
  1436. CmpReportNotify(KeyControlBlock,
  1437. KeyControlBlock->KeyHive,
  1438. KeyControlBlock->KeyCell,
  1439. REG_NOTIFY_CHANGE_LAST_SET);
  1440. }
  1441. Exit:
  1442. PERFINFO_REG_SET_VALUE_DONE(ValueName);
  1443. if( ParentToRelease != HCELL_NIL ) {
  1444. HvReleaseCell(Hive,ParentToRelease);
  1445. }
  1446. if( ChildToRelease != HCELL_NIL ) {
  1447. HvReleaseCell(Hive,ChildToRelease);
  1448. }
  1449. CmpUnlockRegistry();
  1450. // Mark the hive as read only
  1451. CmpMarkAllBinsReadOnly(KeyControlBlock->KeyHive);
  1452. return status;
  1453. }
  1454. NTSTATUS
  1455. CmpSetValueKeyExisting(
  1456. IN PHHIVE Hive,
  1457. IN HCELL_INDEX OldChild,
  1458. IN PCM_KEY_VALUE Value,
  1459. IN ULONG Type,
  1460. IN PVOID Data,
  1461. IN ULONG DataSize,
  1462. IN ULONG StorageType,
  1463. IN ULONG TempData
  1464. )
  1465. /*++
  1466. Routine Description:
  1467. Helper for CmSetValueKey, implements the case where the value entry
  1468. being set already exists.
  1469. Arguments:
  1470. Hive - hive of interest
  1471. OldChild - hcell_index of the value entry body to which we are to
  1472. set new data
  1473. Type - The integer type number of the value entry.
  1474. Data - Pointer to buffer with actual data for the value entry.
  1475. DataSize - Size of Data buffer.
  1476. StorageType - stable or volatile
  1477. TempData - small values are passed here
  1478. Return Value:
  1479. STATUS_SUCCESS if it worked, appropriate status code if it did not
  1480. Note:
  1481. For new hives format, we have the following cases:
  1482. New Data Old Data
  1483. -------- --------
  1484. 1. small small
  1485. 2. small normal
  1486. 3. small bigdata
  1487. 4. normal small
  1488. 5. normal normal
  1489. 6. normal bigdata
  1490. 7. bigdata small
  1491. 8. bigdata normal
  1492. 9. bigdata bigdata
  1493. --*/
  1494. {
  1495. HCELL_INDEX DataCell;
  1496. HCELL_INDEX OldDataCell;
  1497. PCELL_DATA pdata;
  1498. HCELL_INDEX NewCell;
  1499. ULONG OldRealSize;
  1500. USHORT OldSizeType; // 0 - small
  1501. USHORT NewSizeType; // 1 - normal
  1502. // 2 - bigdata
  1503. HANDLE hSecure = 0;
  1504. NTSTATUS status = STATUS_SUCCESS;
  1505. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  1506. //
  1507. // value entry by the specified name already exists
  1508. // oldchild is hcell_index of its value entry body
  1509. // which we will always edit, so mark it dirty
  1510. //
  1511. if (! HvMarkCellDirty(Hive, OldChild)) {
  1512. return STATUS_NO_LOG_SPACE;
  1513. }
  1514. if(CmpIsHKeyValueSmall(OldRealSize, Value->DataLength) == TRUE ) {
  1515. //
  1516. // old data was small
  1517. //
  1518. OldSizeType = 0;
  1519. } else if( CmpIsHKeyValueBig(Hive,OldRealSize) == TRUE ) {
  1520. //
  1521. // old data was big
  1522. //
  1523. OldSizeType = 2;
  1524. } else {
  1525. //
  1526. // old data was normal
  1527. //
  1528. OldSizeType = 1;
  1529. }
  1530. if( DataSize <= CM_KEY_VALUE_SMALL ) {
  1531. //
  1532. // new data is small
  1533. //
  1534. NewSizeType = 0;
  1535. } else if( CmpIsHKeyValueBig(Hive,DataSize) == TRUE ) {
  1536. //
  1537. // new data is big
  1538. //
  1539. NewSizeType = 2;
  1540. } else {
  1541. //
  1542. // new data is normal
  1543. //
  1544. NewSizeType = 1;
  1545. }
  1546. //
  1547. // this will handle all cases and will make sure data is marked dirty
  1548. //
  1549. if( !CmpMarkValueDataDirty(Hive,Value) ) {
  1550. return STATUS_NO_LOG_SPACE;
  1551. }
  1552. //
  1553. // cases 1,2,3
  1554. //
  1555. if( NewSizeType == 0 ) {
  1556. if( ((OldSizeType == 1) && (OldRealSize > 0) ) ||
  1557. (OldSizeType == 2)
  1558. ) {
  1559. CmpFreeValueData(Hive,Value->Data,OldRealSize);
  1560. }
  1561. //
  1562. // write our new small data into value entry body
  1563. //
  1564. Value->DataLength = DataSize + CM_KEY_VALUE_SPECIAL_SIZE;
  1565. Value->Data = TempData;
  1566. Value->Type = Type;
  1567. return STATUS_SUCCESS;
  1568. }
  1569. //
  1570. // secure the user buffer so we don't get inconsistencies.
  1571. // ONLY if we are called with a user mode buffer !!!
  1572. //
  1573. if ( (ULONG_PTR)Data <= (ULONG_PTR)MM_HIGHEST_USER_ADDRESS ) {
  1574. hSecure = MmSecureVirtualMemory(Data,DataSize, PAGE_READONLY);
  1575. if (hSecure == 0) {
  1576. return STATUS_INVALID_PARAMETER;
  1577. }
  1578. }
  1579. //
  1580. // store it to be freed if the allocation succeeds
  1581. //
  1582. OldDataCell = Value->Data;
  1583. //
  1584. // cases 4,5,6
  1585. //
  1586. if( NewSizeType == 1 ){
  1587. if( (OldSizeType == 1) && (OldRealSize > 0)) {
  1588. //
  1589. // we already have a cell; see if we can reuse it !
  1590. //
  1591. DataCell = Value->Data;
  1592. ASSERT(DataCell != HCELL_NIL);
  1593. pdata = HvGetCell(Hive, DataCell);
  1594. if( pdata == NULL ) {
  1595. //
  1596. // we couldn't map a view for the bin containing this cell
  1597. //
  1598. status = STATUS_INSUFFICIENT_RESOURCES;
  1599. goto Exit;
  1600. }
  1601. // release it right here, as the registry is locked exclusively, so we don't care
  1602. HvReleaseCell(Hive, DataCell);
  1603. ASSERT(HvGetCellSize(Hive, pdata) > 0);
  1604. if (DataSize <= (ULONG)(HvGetCellSize(Hive, pdata))) {
  1605. //
  1606. // The existing data cell is big enough to hold the new data.
  1607. //
  1608. //
  1609. // we'll keep this cell
  1610. //
  1611. NewCell = DataCell;
  1612. } else {
  1613. //
  1614. // grow the existing cell
  1615. //
  1616. NewCell = HvReallocateCell(Hive,DataCell,DataSize);
  1617. if (NewCell == HCELL_NIL) {
  1618. status = STATUS_INSUFFICIENT_RESOURCES;
  1619. goto Exit;
  1620. }
  1621. }
  1622. } else {
  1623. //
  1624. // allocate a new cell
  1625. //
  1626. NewCell = HvAllocateCell(Hive, DataSize, StorageType,(HvGetCellType(OldChild)==StorageType)?OldChild:HCELL_NIL);
  1627. if (NewCell == HCELL_NIL) {
  1628. status = STATUS_INSUFFICIENT_RESOURCES;
  1629. goto Exit;
  1630. }
  1631. }
  1632. //
  1633. // now we have a cell that can accomodate the data
  1634. //
  1635. pdata = HvGetCell(Hive, NewCell);
  1636. if( pdata == NULL ) {
  1637. //
  1638. // we couldn't map a view for the bin containing this cell
  1639. //
  1640. // this shouldn't happen as we just allocated/ reallocated/ marked dirty this cell
  1641. //
  1642. ASSERT( FALSE );
  1643. status = STATUS_INSUFFICIENT_RESOURCES;
  1644. goto Exit;
  1645. }
  1646. // release it right here, as the registry is locked exclusively, so we don't care
  1647. HvReleaseCell(Hive, NewCell);
  1648. //
  1649. // copy the actual data
  1650. //
  1651. RtlCopyMemory(pdata,Data,DataSize);
  1652. Value->Data = NewCell;
  1653. Value->DataLength = DataSize;
  1654. Value->Type = Type;
  1655. // sanity
  1656. ASSERT_CELL_DIRTY(Hive,NewCell);
  1657. if( OldSizeType == 2 ) {
  1658. //
  1659. // old data was big; free it
  1660. //
  1661. ASSERT( OldDataCell != NewCell );
  1662. CmpFreeValueData(Hive,OldDataCell,OldRealSize);
  1663. }
  1664. status = STATUS_SUCCESS;
  1665. goto Exit;
  1666. }
  1667. //
  1668. // cases 7,8,9
  1669. //
  1670. if( NewSizeType == 2 ) {
  1671. if( OldSizeType == 2 ) {
  1672. //
  1673. // data was previously big; grow it!
  1674. //
  1675. status =CmpSetValueDataExisting(Hive,Data,DataSize,StorageType,OldDataCell);
  1676. if( !NT_SUCCESS(status) ) {
  1677. goto Exit;
  1678. }
  1679. NewCell = OldDataCell;
  1680. } else {
  1681. //
  1682. // data was small or normal.
  1683. // allocate and copy to a new big data cell;
  1684. // then free the old cell
  1685. //
  1686. status = CmpSetValueDataNew(Hive,Data,DataSize,StorageType,OldChild,&NewCell);
  1687. if( !NT_SUCCESS(status) ) {
  1688. //
  1689. // We have bombed out loading user data, clean up and exit.
  1690. //
  1691. goto Exit;
  1692. }
  1693. if( (OldSizeType != 0) && (OldRealSize != 0) ) {
  1694. //
  1695. // there is something to free
  1696. //
  1697. HvFreeCell(Hive, Value->Data);
  1698. }
  1699. }
  1700. Value->DataLength = DataSize;
  1701. Value->Data = NewCell;
  1702. Value->Type = Type;
  1703. // sanity
  1704. ASSERT_CELL_DIRTY(Hive,NewCell);
  1705. status = STATUS_SUCCESS;
  1706. goto Exit;
  1707. }
  1708. //
  1709. // we shouldn't go here
  1710. //
  1711. ASSERT( FALSE );
  1712. Exit:
  1713. if( hSecure) {
  1714. MmUnsecureVirtualMemory(hSecure);
  1715. }
  1716. return status;
  1717. }
  1718. NTSTATUS
  1719. CmpSetValueKeyNew(
  1720. IN PHHIVE Hive,
  1721. IN PCM_KEY_NODE Parent,
  1722. IN PUNICODE_STRING ValueName,
  1723. IN ULONG Index,
  1724. IN ULONG Type,
  1725. IN PVOID Data,
  1726. IN ULONG DataSize,
  1727. IN ULONG StorageType,
  1728. IN ULONG TempData
  1729. )
  1730. /*++
  1731. Routine Description:
  1732. Helper for CmSetValueKey, implements the case where the value entry
  1733. being set does not exist. Will create new value entry and data,
  1734. place in list (which may be created)
  1735. Arguments:
  1736. Hive - hive of interest
  1737. Parent - pointer to key node value entry is for
  1738. ValueName - The unique (relative to the containing key) name
  1739. of the value entry. May be NULL.
  1740. Index - where in the list should this value be inserted
  1741. Type - The integer type number of the value entry.
  1742. Data - Pointer to buffer with actual data for the value entry.
  1743. DataSize - Size of Data buffer.
  1744. StorageType - stable or volatile
  1745. TempData - small data values passed here
  1746. Return Value:
  1747. STATUS_SUCCESS if it worked, appropriate status code if it did not
  1748. --*/
  1749. {
  1750. PCELL_DATA pvalue;
  1751. HCELL_INDEX ValueCell;
  1752. NTSTATUS Status;
  1753. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  1754. //
  1755. // Either Count == 0 (no list) or our entry is simply not in
  1756. // the list. Create a new value entry body, and data. Add to list.
  1757. // (May create the list.)
  1758. //
  1759. if (Parent->ValueList.Count != 0) {
  1760. ASSERT(Parent->ValueList.List != HCELL_NIL);
  1761. if (! HvMarkCellDirty(Hive, Parent->ValueList.List)) {
  1762. return STATUS_NO_LOG_SPACE;
  1763. }
  1764. }
  1765. //
  1766. // allocate the body of the value entry, and the data
  1767. //
  1768. ValueCell = HvAllocateCell(
  1769. Hive,
  1770. CmpHKeyValueSize(Hive, ValueName),
  1771. StorageType,
  1772. HCELL_NIL
  1773. );
  1774. if (ValueCell == HCELL_NIL) {
  1775. return STATUS_INSUFFICIENT_RESOURCES;
  1776. }
  1777. //
  1778. // map in the body, and fill in its fixed portion
  1779. //
  1780. pvalue = HvGetCell(Hive, ValueCell);
  1781. if( pvalue == NULL ) {
  1782. //
  1783. // we couldn't map a view for the bin containing this cell
  1784. //
  1785. //
  1786. // normally this shouldn't happen as we just allocated ValueCell
  1787. // i.e. the bin containing ValueCell should be mapped in memory at this point.
  1788. //
  1789. ASSERT( FALSE );
  1790. HvFreeCell(Hive, ValueCell);
  1791. return STATUS_INSUFFICIENT_RESOURCES;
  1792. }
  1793. // release it right here, as the registry is locked exclusively, so we don't care
  1794. HvReleaseCell(Hive, ValueCell);
  1795. // sanity
  1796. ASSERT_CELL_DIRTY(Hive,ValueCell);
  1797. pvalue->u.KeyValue.Signature = CM_KEY_VALUE_SIGNATURE;
  1798. //
  1799. // fill in the variable portions of the new value entry, name and
  1800. // and data are copied from caller space, could fault.
  1801. //
  1802. try {
  1803. //
  1804. // fill in the name
  1805. //
  1806. pvalue->u.KeyValue.NameLength = CmpCopyName(Hive,
  1807. pvalue->u.KeyValue.Name,
  1808. ValueName);
  1809. } except (EXCEPTION_EXECUTE_HANDLER) {
  1810. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"!!CmSetValueKey: code:%08lx\n", GetExceptionCode()));
  1811. //
  1812. // We have bombed out loading user data, clean up and exit.
  1813. //
  1814. HvFreeCell(Hive, ValueCell);
  1815. return GetExceptionCode();
  1816. }
  1817. if (pvalue->u.KeyValue.NameLength < ValueName->Length) {
  1818. pvalue->u.KeyValue.Flags = VALUE_COMP_NAME;
  1819. } else {
  1820. pvalue->u.KeyValue.Flags = 0;
  1821. }
  1822. //
  1823. // fill in the data
  1824. //
  1825. if (DataSize > CM_KEY_VALUE_SMALL) {
  1826. Status = CmpSetValueDataNew(Hive,Data,DataSize,StorageType,ValueCell,&(pvalue->u.KeyValue.Data));
  1827. if( !NT_SUCCESS(Status) ) {
  1828. //
  1829. // We have bombed out loading user data, clean up and exit.
  1830. //
  1831. HvFreeCell(Hive, ValueCell);
  1832. return Status;
  1833. }
  1834. pvalue->u.KeyValue.DataLength = DataSize;
  1835. // sanity
  1836. ASSERT_CELL_DIRTY(Hive,pvalue->u.KeyValue.Data);
  1837. } else {
  1838. pvalue->u.KeyValue.DataLength = DataSize + CM_KEY_VALUE_SPECIAL_SIZE;
  1839. pvalue->u.KeyValue.Data = TempData;
  1840. }
  1841. pvalue->u.KeyValue.Type = Type;
  1842. if( !NT_SUCCESS(CmpAddValueToList(Hive,ValueCell,Index,StorageType,&(Parent->ValueList)) ) ) {
  1843. // out of space, free all allocated stuff
  1844. // this will free embeded cigdata cell info too (if any)
  1845. CmpFreeValue(Hive,ValueCell);
  1846. return STATUS_INSUFFICIENT_RESOURCES;
  1847. }
  1848. return STATUS_SUCCESS;
  1849. }
  1850. NTSTATUS
  1851. CmSetLastWriteTimeKey(
  1852. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  1853. IN PLARGE_INTEGER LastWriteTime
  1854. )
  1855. /*++
  1856. Routine Description:
  1857. The LastWriteTime associated with a key node can be set with
  1858. CmSetLastWriteTimeKey
  1859. Arguments:
  1860. KeyControlBlock - pointer to kcb for the key to operate on
  1861. LastWriteTime - new time for key
  1862. Return Value:
  1863. NTSTATUS - Result code from call, among the following:
  1864. <TBS>
  1865. --*/
  1866. {
  1867. PCM_KEY_NODE parent;
  1868. PHHIVE Hive;
  1869. HCELL_INDEX Cell;
  1870. NTSTATUS status = STATUS_SUCCESS;
  1871. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmSetLastWriteTimeKey\n"));
  1872. CmpLockRegistryExclusive();
  1873. //
  1874. // Check that we are not being asked to modify a key
  1875. // that has been deleted
  1876. //
  1877. if (KeyControlBlock->Delete == TRUE) {
  1878. status = STATUS_KEY_DELETED;
  1879. goto Exit;
  1880. }
  1881. Hive = KeyControlBlock->KeyHive;
  1882. Cell = KeyControlBlock->KeyCell;
  1883. parent = (PCM_KEY_NODE)HvGetCell(Hive, Cell);
  1884. if( parent == NULL ) {
  1885. //
  1886. // we couldn't map a view for the bin containing this cell
  1887. //
  1888. status = STATUS_INSUFFICIENT_RESOURCES;
  1889. goto Exit;
  1890. }
  1891. // release the cell right here, as the registry is locked exclusively, so we don't care
  1892. HvReleaseCell(Hive, Cell);
  1893. if (! HvMarkCellDirty(Hive, Cell)) {
  1894. status = STATUS_NO_LOG_SPACE;
  1895. goto Exit;
  1896. }
  1897. parent->LastWriteTime = *LastWriteTime;
  1898. // update the kcb cache too.
  1899. KeyControlBlock->KcbLastWriteTime = *LastWriteTime;
  1900. Exit:
  1901. CmpUnlockRegistry();
  1902. return status;
  1903. }
  1904. NTSTATUS
  1905. CmSetKeyUserFlags(
  1906. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  1907. IN ULONG UserFlags
  1908. )
  1909. /*++
  1910. Routine Description:
  1911. Sets the user defined flags for the key; At this point there are only
  1912. 4 bits reserved for user defined flags. kcb and knode must be kept in
  1913. sync.
  1914. Arguments:
  1915. KeyControlBlock - pointer to kcb for the key to operate on
  1916. UserFlags - user defined flags to be set on this key.
  1917. Return Value:
  1918. NTSTATUS - Result code from call, among the following:
  1919. <TBS>
  1920. --*/
  1921. {
  1922. PCM_KEY_NODE Node;
  1923. PHHIVE Hive;
  1924. HCELL_INDEX Cell;
  1925. LARGE_INTEGER LastWriteTime;
  1926. NTSTATUS status = STATUS_SUCCESS;
  1927. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmSetKeyUserFlags\n"));
  1928. CmpLockRegistryExclusive();
  1929. //
  1930. // Check that we are not being asked to modify a key
  1931. // that has been deleted
  1932. //
  1933. if (KeyControlBlock->Delete == TRUE) {
  1934. status = STATUS_KEY_DELETED;
  1935. goto Exit;
  1936. }
  1937. if( UserFlags & (~((ULONG)KEY_USER_FLAGS_VALID_MASK)) ) {
  1938. //
  1939. // number of user defined flags exceeded; punt
  1940. //
  1941. status = STATUS_INVALID_PARAMETER;
  1942. goto Exit;
  1943. }
  1944. Hive = KeyControlBlock->KeyHive;
  1945. Cell = KeyControlBlock->KeyCell;
  1946. Node = (PCM_KEY_NODE)HvGetCell(Hive, Cell);
  1947. if( Node == NULL ) {
  1948. //
  1949. // we couldn't map a view for the bin containing this cell
  1950. //
  1951. status = STATUS_INSUFFICIENT_RESOURCES;
  1952. goto Exit;
  1953. }
  1954. // release the cell right here, as the registry is locked exclusively, so we don't care
  1955. HvReleaseCell(Hive, Cell);
  1956. if (! HvMarkCellDirty(Hive, Cell)) {
  1957. status = STATUS_NO_LOG_SPACE;
  1958. goto Exit;
  1959. }
  1960. //
  1961. // shift/(pack) the user defined flags and
  1962. // update knode and kcb cache
  1963. //
  1964. // first, erase the old flags
  1965. Node->Flags &= KEY_USER_FLAGS_CLEAR_MASK;
  1966. Node->Flags |= (USHORT)(UserFlags<<KEY_USER_FLAGS_SHIFT);
  1967. // update the kcb cache
  1968. KeyControlBlock->Flags = Node->Flags;
  1969. //
  1970. // we need to update the LstWriteTime as well
  1971. //
  1972. KeQuerySystemTime(&LastWriteTime);
  1973. Node->LastWriteTime = LastWriteTime;
  1974. // update the kcb cache too.
  1975. KeyControlBlock->KcbLastWriteTime = LastWriteTime;
  1976. Exit:
  1977. CmpUnlockRegistry();
  1978. return status;
  1979. }
  1980. BOOLEAN
  1981. CmpIsHiveAlreadyLoaded( IN HANDLE KeyHandle,
  1982. IN POBJECT_ATTRIBUTES SourceFile
  1983. )
  1984. /*++
  1985. Routine Description:
  1986. Checks if the SourceFile is already loaded in the same spot as KeyHandle.
  1987. Arguments:
  1988. KeyHandle - should be the root of a hive. We'll query the name of the primary file
  1989. and compare it against the name of SourceFile
  1990. SourceFile - specifies a file. while file could be remote,
  1991. that is strongly discouraged.
  1992. Return Value:
  1993. TRUE/FALSE
  1994. --*/
  1995. {
  1996. NTSTATUS status;
  1997. PCM_KEY_BODY KeyBody;
  1998. BOOLEAN Result = FALSE; // pesimistic
  1999. PCMHIVE CmHive;
  2000. PAGED_CODE();
  2001. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  2002. status = ObReferenceObjectByHandle(KeyHandle,
  2003. 0,
  2004. CmpKeyObjectType,
  2005. KernelMode,
  2006. (PVOID *)(&KeyBody),
  2007. NULL);
  2008. if(!NT_SUCCESS(status)) {
  2009. return FALSE;
  2010. }
  2011. CmHive = (PCMHIVE)CONTAINING_RECORD(KeyBody->KeyControlBlock->KeyHive, CMHIVE, Hive);
  2012. //
  2013. // should be the root of a hive
  2014. //
  2015. if( !(KeyBody->KeyControlBlock->Flags & KEY_HIVE_ENTRY) || // not root of a hive
  2016. (CmHive->FileUserName.Buffer == NULL)// no name captured
  2017. ) {
  2018. goto ExitCleanup;
  2019. }
  2020. if( RtlCompareUnicodeString(&(CmHive->FileUserName),
  2021. SourceFile->ObjectName,
  2022. TRUE) == 0 ) {
  2023. //
  2024. // same file; same spot
  2025. //
  2026. Result = TRUE;
  2027. //
  2028. // unfreeze the hive;hive will become just a regular hive from now on
  2029. // it is safe to do this because we hold an extra refcount on the root of the hive
  2030. // as we have specifically opened the root to check if it's already loaded
  2031. //
  2032. if( IsHiveFrozen(CmHive) ) {
  2033. CmHive->Frozen = FALSE;
  2034. if( CmHive->UnloadWorkItem != NULL ) {
  2035. ExFreePool( CmHive->UnloadWorkItem );
  2036. CmHive->UnloadWorkItem = NULL;
  2037. }
  2038. if( CmHive->RootKcb ) {
  2039. CmpDereferenceKeyControlBlockWithLock(CmHive->RootKcb);
  2040. CmHive->RootKcb = NULL;
  2041. }
  2042. }
  2043. }
  2044. ExitCleanup:
  2045. ObDereferenceObject((PVOID)KeyBody);
  2046. return Result;
  2047. }
  2048. NTSTATUS
  2049. CmLoadKey(
  2050. IN POBJECT_ATTRIBUTES TargetKey,
  2051. IN POBJECT_ATTRIBUTES SourceFile,
  2052. IN ULONG Flags
  2053. )
  2054. /*++
  2055. Routine Description:
  2056. A hive (file in the format created by NtSaveKey) may be linked
  2057. into the active registry with this call. UNLIKE NtRestoreKey,
  2058. the file specified to NtLoadKey will become the actual backing
  2059. store of part of the registry (that is, it will NOT be copied.)
  2060. The file may have an associated .log file.
  2061. If the hive file is marked as needing a .log file, and one is
  2062. not present, the call will fail.
  2063. The name specified by SourceFile must be such that ".log" can
  2064. be appended to it to generate the name of the log file. Thus,
  2065. on FAT file systems, the hive file may not have an extension.
  2066. This call is used by logon to make the user's profile available
  2067. in the registry. It is not intended for use doing backup,
  2068. restore, etc. Use NtRestoreKey for that.
  2069. N.B. This routine assumes that the object attributes for the file
  2070. to be opened have been captured into kernel space so that
  2071. they can safely be passed to the worker thread to open the file
  2072. and do the actual I/O.
  2073. Arguments:
  2074. TargetKey - specifies the path to a key to link the hive to.
  2075. path must be of the form "\registry\user\<username>"
  2076. SourceFile - specifies a file. while file could be remote,
  2077. that is strongly discouraged.
  2078. Flags - specifies any flags that should be used for the load operation.
  2079. The only valid flag is REG_NO_LAZY_FLUSH.
  2080. Return Value:
  2081. NTSTATUS - values TBS.
  2082. --*/
  2083. {
  2084. PCMHIVE NewHive;
  2085. NTSTATUS Status;
  2086. BOOLEAN Allocate;
  2087. BOOLEAN RegistryLockAquired;
  2088. SECURITY_QUALITY_OF_SERVICE ServiceQos;
  2089. SECURITY_CLIENT_CONTEXT ClientSecurityContext;
  2090. HANDLE KeyHandle;
  2091. //
  2092. // Obtain the security context here so we can use it
  2093. // later to impersonate the user, which we will do
  2094. // if we cannot access the file as SYSTEM. This
  2095. // usually occurs if the file is on a remote machine.
  2096. //
  2097. ServiceQos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
  2098. ServiceQos.ImpersonationLevel = SecurityImpersonation;
  2099. ServiceQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  2100. ServiceQos.EffectiveOnly = TRUE;
  2101. Status = SeCreateClientSecurity(CONTAINING_RECORD(KeGetCurrentThread(),ETHREAD,Tcb),
  2102. &ServiceQos,
  2103. FALSE,
  2104. &ClientSecurityContext);
  2105. if (!NT_SUCCESS(Status)) {
  2106. return(Status);
  2107. }
  2108. //
  2109. // we open the root of the hive here. if it already exists,this will prevent it from going
  2110. // away from under us while we are doing the "already loaded" check (due to delay unload logic)
  2111. //
  2112. Status = ObOpenObjectByName(TargetKey,
  2113. CmpKeyObjectType,
  2114. KernelMode,
  2115. NULL,
  2116. KEY_READ,
  2117. NULL,
  2118. &KeyHandle);
  2119. if(!NT_SUCCESS(Status)) {
  2120. KeyHandle = NULL;
  2121. }
  2122. //
  2123. // Do not lock the registry; Instead set the RegistryLockAquired member
  2124. // of REGISTRY_COMMAND so CmpWorker can lock it after opening the hive files
  2125. //
  2126. //CmpLockRegistryExclusive();
  2127. //
  2128. RegistryLockAquired = FALSE;
  2129. Allocate = TRUE;
  2130. Status = CmpCmdHiveOpen( SourceFile, // FileAttributes
  2131. &ClientSecurityContext, // ImpersonationContext
  2132. &Allocate, // Allocate
  2133. &RegistryLockAquired, // RegistryLockAquired
  2134. &NewHive, // NewHive
  2135. CM_CHECK_REGISTRY_CHECK_CLEAN //CheckFlags
  2136. );
  2137. SeDeleteClientSecurity( &ClientSecurityContext );
  2138. if (!NT_SUCCESS(Status)) {
  2139. if( KeyHandle != NULL ) {
  2140. //
  2141. // lock the registry exclusive while we are checking attempt to load same file into the same spot
  2142. //
  2143. if( !RegistryLockAquired ) {
  2144. CmpLockRegistryExclusive();
  2145. RegistryLockAquired = TRUE;
  2146. }
  2147. //
  2148. // check if the same file is loaded in the same spot
  2149. //
  2150. if( CmpIsHiveAlreadyLoaded(KeyHandle,SourceFile) ) {
  2151. Status = STATUS_SUCCESS;
  2152. }
  2153. }
  2154. if( RegistryLockAquired ) {
  2155. // if CmpWorker has locked the registry, unlock it now.
  2156. CmpUnlockRegistry();
  2157. }
  2158. if( KeyHandle != NULL ) {
  2159. ZwClose(KeyHandle);
  2160. }
  2161. return(Status);
  2162. } else {
  2163. //
  2164. // if we got here, CmpWorker should have locked the registry exclusive.
  2165. //
  2166. ASSERT( RegistryLockAquired );
  2167. }
  2168. //
  2169. // if this is a NO_LAZY_FLUSH hive, set the appropriate bit.
  2170. //
  2171. if (Flags & REG_NO_LAZY_FLUSH) {
  2172. NewHive->Hive.HiveFlags |= HIVE_NOLAZYFLUSH;
  2173. }
  2174. //
  2175. // We now have a succesfully loaded and initialized CmHive, so we
  2176. // just need to link that into the appropriate spot in the master hive.
  2177. //
  2178. Status = CmpLinkHiveToMaster(TargetKey->ObjectName,
  2179. TargetKey->RootDirectory,
  2180. NewHive,
  2181. Allocate,
  2182. TargetKey->SecurityDescriptor);
  2183. if (NT_SUCCESS(Status)) {
  2184. //
  2185. // add new hive to hivelist
  2186. //
  2187. CmpAddToHiveFileList(NewHive);
  2188. //
  2189. // flush the hive right here if just created; this is to avoid situations where
  2190. // the lazy flusher doesn't get a chance to flush the hive, or it can't (because
  2191. // the hive is a no_lazy_flush hive and it is never explicitly flushed)
  2192. //
  2193. if( Allocate == TRUE ) {
  2194. HvSyncHive(&(NewHive->Hive));
  2195. }
  2196. } else {
  2197. LOCK_HIVE_LIST();
  2198. CmpRemoveEntryList(&(NewHive->HiveList));
  2199. UNLOCK_HIVE_LIST();
  2200. CmpCheckForOrphanedKcbs((PHHIVE)NewHive);
  2201. CmpDestroyHiveViewList(NewHive);
  2202. CmpDestroySecurityCache (NewHive);
  2203. CmpDropFileObjectForHive(NewHive);
  2204. HvFreeHive((PHHIVE)NewHive);
  2205. //
  2206. // Close the hive files
  2207. //
  2208. CmpCmdHiveClose(NewHive);
  2209. //
  2210. // free the cm level structure
  2211. //
  2212. ASSERT( NewHive->HiveLock );
  2213. ExFreePool(NewHive->HiveLock);
  2214. ASSERT( NewHive->ViewLock );
  2215. ExFreePool(NewHive->ViewLock);
  2216. CmpFree(NewHive, sizeof(CMHIVE));
  2217. }
  2218. //
  2219. // We've given user chance to log on, so turn on quota
  2220. //
  2221. if ((CmpProfileLoaded == FALSE) &&
  2222. (CmpWasSetupBoot == FALSE)) {
  2223. CmpProfileLoaded = TRUE;
  2224. CmpSetGlobalQuotaAllowed();
  2225. }
  2226. #ifdef CHECK_REGISTRY_USECOUNT
  2227. CmpCheckRegistryUseCount();
  2228. #endif //CHECK_REGISTRY_USECOUNT
  2229. CmpUnlockRegistry();
  2230. if( KeyHandle != NULL ) {
  2231. ZwClose(KeyHandle);
  2232. }
  2233. return(Status);
  2234. }
  2235. #if DBG
  2236. ULONG
  2237. CmpUnloadKeyWorker(
  2238. PCM_KEY_CONTROL_BLOCK Current,
  2239. PVOID Context1,
  2240. PVOID Context2
  2241. )
  2242. {
  2243. PUNICODE_STRING ConstructedName;
  2244. if (Current->KeyHive == Context1) {
  2245. ConstructedName = CmpConstructName(Current);
  2246. if (ConstructedName) {
  2247. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_TRACE_LEVEL,"%wZ\n", ConstructedName));
  2248. ExFreePoolWithTag(ConstructedName, CM_NAME_TAG | PROTECTED_POOL);
  2249. }
  2250. }
  2251. return KCB_WORKER_CONTINUE; // always keep searching
  2252. }
  2253. #endif
  2254. NTSTATUS
  2255. CmUnloadKey(
  2256. IN PHHIVE Hive,
  2257. IN HCELL_INDEX Cell,
  2258. IN PCM_KEY_CONTROL_BLOCK Kcb
  2259. )
  2260. /*++
  2261. Routine Description:
  2262. Unlinks a hive from its location in the registry, closes its file
  2263. handles, and deallocates all its memory.
  2264. There must be no key control blocks currently referencing the hive
  2265. to be unloaded.
  2266. Arguments:
  2267. Hive - Supplies a pointer to the hive control structure for the
  2268. hive to be unloaded
  2269. Cell - supplies the HCELL_INDEX for the root cell of the hive.
  2270. Kcb - Supplies the key control block
  2271. Return Value:
  2272. NTSTATUS
  2273. --*/
  2274. {
  2275. PCMHIVE CmHive;
  2276. BOOLEAN Success;
  2277. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmUnloadKey\n"));
  2278. //
  2279. // Make sure the cell passed in is the root cell of the hive.
  2280. //
  2281. if (Cell != Hive->BaseBlock->RootCell) {
  2282. return(STATUS_INVALID_PARAMETER);
  2283. }
  2284. //
  2285. // Make sure there are no open references to key control blocks
  2286. // for this hive. If there are none, then we can unload the hive.
  2287. //
  2288. CmHive = CONTAINING_RECORD(Hive, CMHIVE, Hive);
  2289. if(Kcb->RefCount != 1) {
  2290. Success = (CmpSearchForOpenSubKeys(Kcb,SearchIfExist) == 0);
  2291. Success = Success && (Kcb->RefCount == 1);
  2292. if( Success == FALSE) {
  2293. #if DBG
  2294. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_TRACE_LEVEL,"List of keys open against hive unload was attempted on:\n"));
  2295. CmpSearchKeyControlBlockTree(
  2296. CmpUnloadKeyWorker,
  2297. Hive,
  2298. NULL
  2299. );
  2300. #endif
  2301. return STATUS_CANNOT_DELETE;
  2302. }
  2303. }
  2304. ASSERT( Kcb->RefCount == 1 );
  2305. //
  2306. // Flush any dirty data to disk. If this fails, too bad.
  2307. //
  2308. CmFlushKey(Hive, Cell);
  2309. //
  2310. // Remove the hive from the HiveFileList
  2311. //
  2312. CmpRemoveFromHiveFileList((PCMHIVE)Hive);
  2313. //
  2314. // Unlink from master hive, remove from list
  2315. //
  2316. Success = CmpDestroyHive(Hive, Cell);
  2317. if (Success) {
  2318. //
  2319. // signal the user event (if any), then do the cleanup (i.e. deref the event
  2320. // and the artificial refcount we set on the root kcb)
  2321. //
  2322. if( CmHive->UnloadEvent != NULL ) {
  2323. KeSetEvent(CmHive->UnloadEvent,0,FALSE);
  2324. ObDereferenceObject(CmHive->UnloadEvent);
  2325. }
  2326. CmpDestroyHiveViewList(CmHive);
  2327. CmpDestroySecurityCache (CmHive);
  2328. CmpDropFileObjectForHive(CmHive);
  2329. HvFreeHive(Hive);
  2330. //
  2331. // Close the hive files
  2332. //
  2333. CmpCmdHiveClose(CmHive);
  2334. //
  2335. // free the cm level structure
  2336. //
  2337. ASSERT( CmHive->HiveLock );
  2338. ExFreePool(CmHive->HiveLock);
  2339. ASSERT( CmHive->ViewLock );
  2340. ExFreePool(CmHive->ViewLock);
  2341. CmpFree(CmHive, sizeof(CMHIVE));
  2342. return(STATUS_SUCCESS);
  2343. } else {
  2344. return(STATUS_INSUFFICIENT_RESOURCES);
  2345. }
  2346. }
  2347. #ifdef NT_UNLOAD_KEY_EX
  2348. NTSTATUS
  2349. CmUnloadKeyEx(
  2350. IN PCM_KEY_CONTROL_BLOCK kcb,
  2351. IN PKEVENT UserEvent
  2352. )
  2353. /*++
  2354. Routine Description:
  2355. First tries to unlink the hive, by calling the sync version
  2356. If the hive cannot be unloaded (there are open handles inside it),
  2357. reference the root of the hive (i.e. kcb) and freeze the hive.
  2358. Arguments:
  2359. Kcb - Supplies the key control block
  2360. UserEvent - the event to be signaled after the hive was unloaded
  2361. (only if late - unload is needed)
  2362. Return Value:
  2363. STATUS_PENDING - the hive was frozen and it'll be unloaded later
  2364. STATUS_SUCCESS - the hive was successfully sync-unloaded (no need
  2365. to signal for UserEvent)
  2366. <other> - an error occured, operation failed
  2367. --*/
  2368. {
  2369. PCMHIVE CmHive;
  2370. HCELL_INDEX Cell;
  2371. NTSTATUS Status;
  2372. PAGED_CODE();
  2373. Cell = kcb->KeyCell;
  2374. CmHive = (PCMHIVE)CONTAINING_RECORD(kcb->KeyHive, CMHIVE, Hive);
  2375. if( IsHiveFrozen(CmHive) ) {
  2376. //
  2377. // don't let them hurt themselves by calling it twice
  2378. //
  2379. return STATUS_TOO_LATE;
  2380. }
  2381. //
  2382. // first, try out he sync routine; this may or may not unload the hive,
  2383. // but at least will kick kcbs with refcount = 0 out of cache
  2384. //
  2385. Status = CmUnloadKey(&(CmHive->Hive),Cell,kcb);
  2386. if( Status != STATUS_CANNOT_DELETE ) {
  2387. //
  2388. // the hive was either unloaded, or some bad thing happened
  2389. //
  2390. return Status;
  2391. }
  2392. ASSERT( kcb->RefCount > 1 );
  2393. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  2394. //
  2395. // Prepare for late-unloading:
  2396. // 1. reference the kcb, to make sure it won't go away without us noticing
  2397. // (we have the registry locked in exclusive mode, so we don't need to lock the kcbtree
  2398. //
  2399. if (!CmpReferenceKeyControlBlock(kcb)) {
  2400. return STATUS_INSUFFICIENT_RESOURCES;
  2401. }
  2402. //
  2403. // parse the kcb tree and mark all open kcbs inside this hive and "no delay close"
  2404. //
  2405. CmpSearchForOpenSubKeys(kcb,SearchAndTagNoDelayClose);
  2406. kcb->ExtFlags |= CM_KCB_NO_DELAY_CLOSE;
  2407. //
  2408. // 2. Freeze the hive
  2409. //
  2410. CmHive->RootKcb = kcb;
  2411. CmHive->Frozen = TRUE;
  2412. CmHive->UnloadEvent = UserEvent;
  2413. return STATUS_PENDING;
  2414. }
  2415. #endif //NT_UNLOAD_KEY_EX
  2416. // define in cmworker.c
  2417. extern BOOLEAN CmpForceForceFlush;
  2418. BOOLEAN
  2419. CmpDoFlushAll(
  2420. BOOLEAN ForceFlush
  2421. )
  2422. /*++
  2423. Routine Description:
  2424. Flush all hives.
  2425. Runs in the context of the CmpWorkerThread.
  2426. Runs down list of Hives and applies HvSyncHive to them.
  2427. NOTE: Hives which are marked as HV_NOLAZYFLUSH are *NOT* flushed
  2428. by this call. You must call HvSyncHive explicitly to flush
  2429. a hive marked as HV_NOLAZYFLUSH.
  2430. Arguments:
  2431. ForceFlush - used as a contingency plan when a prior exception left
  2432. some hive in a used state. When set to TRUE, assumes the
  2433. registry is locked exclusive. It also repairs the broken
  2434. hives.
  2435. - When FALSE saves only the hives with UseCount == 0.
  2436. Return Value:
  2437. NONE
  2438. Notes:
  2439. If any of the hives is about to shrink CmpForceForceFlush is set to TRUE,
  2440. otherwise, it is set to FALSE
  2441. --*/
  2442. {
  2443. NTSTATUS Status;
  2444. PLIST_ENTRY p;
  2445. PCMHIVE h;
  2446. BOOLEAN Result = TRUE;
  2447. /*
  2448. ULONG rc;
  2449. */
  2450. extern PCMHIVE CmpMasterHive;
  2451. //
  2452. // If writes are not working, lie and say we succeeded, will
  2453. // clean up in a short time. Only early system init code
  2454. // will ever know the difference.
  2455. //
  2456. if (CmpNoWrite) {
  2457. return TRUE;
  2458. }
  2459. CmpForceForceFlush = FALSE;
  2460. //
  2461. // traverse list of hives, sync each one
  2462. //
  2463. LOCK_HIVE_LIST();
  2464. p = CmpHiveListHead.Flink;
  2465. while (p != &CmpHiveListHead) {
  2466. h = CONTAINING_RECORD(p, CMHIVE, HiveList);
  2467. if (!(h->Hive.HiveFlags & HIVE_NOLAZYFLUSH)) {
  2468. //
  2469. //Lock the hive before we flush it.
  2470. //-- since we now allow multiple readers
  2471. // during a flush (a flush is considered a read)
  2472. // we have to force a serialization on the vector table
  2473. //
  2474. CmLockHive (h);
  2475. if( (ForceFlush == TRUE) && (h->UseCount != 0) ) {
  2476. //
  2477. // hive was left in an instable state by a prior exception raised
  2478. // somewhere inside a CM function.
  2479. //
  2480. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  2481. CmpFixHiveUsageCount(h);
  2482. ASSERT( h->UseCount == 0 );
  2483. }
  2484. if( (ForceFlush == TRUE) || (!HvHiveWillShrink((PHHIVE)h)) ) {
  2485. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_IO,"CmpDoFlushAll hive = %p ForceFlush = %lu IsHiveShrinking = %lu BaseLength = %lx StableLength = %lx\n",
  2486. h,(ULONG)ForceFlush,(ULONG)HvHiveWillShrink((PHHIVE)h),((PHHIVE)h)->BaseBlock->Length,((PHHIVE)h)->Storage[Stable].Length));
  2487. Status = HvSyncHive((PHHIVE)h);
  2488. if( !NT_SUCCESS( Status ) ) {
  2489. Result = FALSE;
  2490. }
  2491. } else {
  2492. CmKdPrintEx((DPFLTR_CONFIG_ID,DPFLTR_TRACE_LEVEL,"CmpDoFlushAll: Fail to flush hive %p because is shrinking\n",h));
  2493. Result = FALSE;
  2494. //
  2495. // another unsuccessful attempt to save this hive, because we needed the reglock exclisive
  2496. //
  2497. CmpForceForceFlush = TRUE;
  2498. }
  2499. CmUnlockHive (h);
  2500. //
  2501. // WARNNOTE - the above means that a lazy flush or
  2502. // or shutdown flush did not work. we don't
  2503. // know why. there is noone to report an error
  2504. // to, so continue on and hope for the best.
  2505. // (in theory, worst that can happen is user changes
  2506. // are lost.)
  2507. //
  2508. }
  2509. p = p->Flink;
  2510. }
  2511. UNLOCK_HIVE_LIST();
  2512. return Result;
  2513. }
  2514. NTSTATUS
  2515. CmReplaceKey(
  2516. IN PHHIVE Hive,
  2517. IN HCELL_INDEX Cell,
  2518. IN PUNICODE_STRING NewHiveName,
  2519. IN PUNICODE_STRING OldFileName
  2520. )
  2521. /*++
  2522. Routine Description:
  2523. Renames the hive file for a running system and replaces it with a new
  2524. file. The new file is not actually used until the next boot.
  2525. Arguments:
  2526. Hive - Supplies a hive control structure for the hive to be replaced.
  2527. Cell - Supplies the HCELL_INDEX of the root cell of the hive to be
  2528. replaced.
  2529. NewHiveName - Supplies the name of the file which is to be installed
  2530. as the new hive.
  2531. OldFileName - Supplies the name of the file which the existing hive
  2532. file is to be renamed to.
  2533. Return Value:
  2534. NTSTATUS
  2535. --*/
  2536. {
  2537. CHAR ObjectInfoBuffer[512];
  2538. NTSTATUS Status;
  2539. NTSTATUS Status2;
  2540. OBJECT_ATTRIBUTES Attributes;
  2541. PCMHIVE NewHive;
  2542. PCMHIVE CmHive;
  2543. POBJECT_NAME_INFORMATION NameInfo;
  2544. ULONG OldQuotaAllowed;
  2545. ULONG OldQuotaWarning;
  2546. BOOLEAN Allocate;
  2547. BOOLEAN RegistryLockAquired;
  2548. UNREFERENCED_PARAMETER (Cell);
  2549. CmpLockRegistryExclusive();
  2550. #ifdef CHECK_REGISTRY_USECOUNT
  2551. CmpCheckRegistryUseCount();
  2552. #endif //CHECK_REGISTRY_USECOUNT
  2553. if (Hive->HiveFlags & HIVE_HAS_BEEN_REPLACED) {
  2554. CmpUnlockRegistry();
  2555. return STATUS_FILE_RENAMED;
  2556. }
  2557. //
  2558. // temporarily disable registry quota as we will be giving this memory back immediately!
  2559. //
  2560. OldQuotaAllowed = CmpGlobalQuotaAllowed;
  2561. OldQuotaWarning = CmpGlobalQuotaWarning;
  2562. CmpGlobalQuotaAllowed = CM_WRAP_LIMIT;
  2563. CmpGlobalQuotaWarning = CM_WRAP_LIMIT;
  2564. //
  2565. // First open the new hive file and check to make sure it is valid.
  2566. //
  2567. InitializeObjectAttributes(&Attributes,
  2568. NewHiveName,
  2569. OBJ_CASE_INSENSITIVE,
  2570. NULL,
  2571. NULL);
  2572. Allocate = FALSE;
  2573. RegistryLockAquired = TRUE;
  2574. Status = CmpCmdHiveOpen( &Attributes, // FileAttributes
  2575. NULL, // ImpersonationContext
  2576. &Allocate, // Allocate
  2577. &RegistryLockAquired, // RegistryLockAquired
  2578. &NewHive, // NewHive
  2579. CM_CHECK_REGISTRY_CHECK_CLEAN // CheckFlags
  2580. );
  2581. if (!NT_SUCCESS(Status)) {
  2582. goto ErrorExit;
  2583. }
  2584. ASSERT(Allocate == FALSE);
  2585. if( Hive == (PHHIVE)(CmpMachineHiveList[SYSTEM_HIVE_INDEX].CmHive) ) {
  2586. //
  2587. // Somebody attempts to replace the system hive: do the WPA test
  2588. //
  2589. HCELL_INDEX Src,Dest;
  2590. Status = CmpCheckReplaceHive(Hive,&Src);
  2591. if( !NT_SUCCESS(Status) ) {
  2592. goto ErrorCleanup;
  2593. }
  2594. Status = CmpCheckReplaceHive((PHHIVE)NewHive,&Dest);
  2595. if( !NT_SUCCESS(Status) ) {
  2596. goto ErrorCleanup;
  2597. }
  2598. ASSERT( Src != HCELL_NIL );
  2599. ASSERT( Dest != HCELL_NIL );
  2600. //
  2601. // now stuff the current WPA subtree into the new hive
  2602. //
  2603. if( !CmpSyncTrees(Hive, Src, (PHHIVE)NewHive, Dest, FALSE ) ) {
  2604. Status = STATUS_REGISTRY_CORRUPT;
  2605. goto ErrorCleanup;
  2606. }
  2607. //
  2608. // commit the changes we've made in the destination hive
  2609. //
  2610. if( !HvSyncHive((PHHIVE)NewHive) ) {
  2611. Status = STATUS_REGISTRY_CORRUPT;
  2612. goto ErrorCleanup;
  2613. }
  2614. }
  2615. //
  2616. // The new hive exists, and is consistent, and we have it open.
  2617. // Now rename the current hive file.
  2618. //
  2619. CmHive = (PCMHIVE)CONTAINING_RECORD(Hive, CMHIVE, Hive);
  2620. Status = CmpCmdRenameHive( CmHive, // CmHive
  2621. (POBJECT_NAME_INFORMATION)ObjectInfoBuffer, // OldName
  2622. OldFileName, // NewName
  2623. sizeof(ObjectInfoBuffer) // NameInfoLength
  2624. );
  2625. if (!NT_SUCCESS(Status)) {
  2626. //
  2627. // rename failed, close the files associated with the new hive
  2628. //
  2629. goto ErrorCleanup;
  2630. }
  2631. //
  2632. // The existing hive was successfully renamed, so try to rename the
  2633. // new file to what the old hive file was named. (which was returned
  2634. // into ObjectInfoBuffer by the worker thread)
  2635. //
  2636. Hive->HiveFlags |= HIVE_HAS_BEEN_REPLACED;
  2637. NameInfo = (POBJECT_NAME_INFORMATION)ObjectInfoBuffer;
  2638. Status = CmpCmdRenameHive( NewHive, // CmHive
  2639. NULL, // OldName
  2640. &NameInfo->Name,// NewName
  2641. 0 // NameInfoLength
  2642. );
  2643. if (!NT_SUCCESS(Status)) {
  2644. //
  2645. // We are in trouble now. We have renamed the existing hive file,
  2646. // but we couldn't rename the new hive file! Try to rename the
  2647. // existing hive file back to where it was.
  2648. //
  2649. CmHive = (PCMHIVE)CONTAINING_RECORD(Hive, CMHIVE, Hive);
  2650. Status2 = CmpCmdRenameHive( CmHive, // CmHive
  2651. NULL, // OldName
  2652. &NameInfo->Name, // NewName
  2653. 0 // NameInfoLength
  2654. );
  2655. if (!NT_SUCCESS(Status2)) {
  2656. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmReplaceKey: renamed existing hive file, but couldn't\n"));
  2657. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," rename new hive file (%08lx) ",Status));
  2658. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," or replace old hive file (%08lx)!\n",Status2));
  2659. //
  2660. // WARNNOTE:
  2661. // To get into this state, the user must have relevent
  2662. // privileges, deliberately mess with system in an attempt
  2663. // to defeat it, AND get it done in a narrow timing window.
  2664. //
  2665. // Further, if it's a user profile, the system will
  2666. // still come up.
  2667. //
  2668. // Therefore, return an error code and go on.
  2669. //
  2670. Status = STATUS_REGISTRY_CORRUPT;
  2671. }
  2672. } else {
  2673. //
  2674. // flush file buffers (we are particulary interested in ValidDataLength to be updated on-disk)
  2675. //
  2676. IO_STATUS_BLOCK IoStatus;
  2677. Status = ZwFlushBuffersFile(NewHive->FileHandles[HFILE_TYPE_PRIMARY],&IoStatus);
  2678. if (!NT_SUCCESS(Status)) {
  2679. //
  2680. // failed to set ValidDataLength, close the files associated with the new hive
  2681. //
  2682. //
  2683. // We are in trouble now. We have renamed the existing hive file,
  2684. // but we couldn't rename the new hive file! Try to rename the
  2685. // existing hive file back to where it was.
  2686. //
  2687. CmHive = (PCMHIVE)CONTAINING_RECORD(Hive, CMHIVE, Hive);
  2688. Status2 = CmpCmdRenameHive( CmHive, // CmHive
  2689. NULL, // OldName
  2690. &NameInfo->Name, // NewName
  2691. 0 // NameInfoLength
  2692. );
  2693. if (!NT_SUCCESS(Status2)) {
  2694. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK,"CmReplaceKey: renamed existing hive file, but couldn't\n"));
  2695. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," rename new hive file (%08lx) ",Status));
  2696. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_BUGCHECK," or replace old hive file (%08lx)!\n",Status2));
  2697. //
  2698. // WARNNOTE:
  2699. // To get into this state, the user must have relevent
  2700. // privileges, deliberately mess with system in an attempt
  2701. // to defeat it, AND get it done in a narrow timing window.
  2702. //
  2703. // Further, if it's a user profile, the system will
  2704. // still come up.
  2705. //
  2706. // Therefore, return an error code and go on.
  2707. //
  2708. Status = STATUS_REGISTRY_CORRUPT;
  2709. }
  2710. }
  2711. }
  2712. //
  2713. // All of the renaming is done. However, we are holding an in-memory
  2714. // image of the new hive. Release it, since it will not actually
  2715. // be used until next boot.
  2716. //
  2717. // Do not close the open file handles to the new hive, we need to
  2718. // keep it locked exclusively until the system is rebooted to prevent
  2719. // people from mucking with it.
  2720. //
  2721. ErrorCleanup:
  2722. LOCK_HIVE_LIST();
  2723. CmpRemoveEntryList(&(NewHive->HiveList));
  2724. UNLOCK_HIVE_LIST();
  2725. CmpDestroyHiveViewList(NewHive);
  2726. CmpDestroySecurityCache(NewHive);
  2727. CmpDropFileObjectForHive(NewHive);
  2728. HvFreeHive((PHHIVE)NewHive);
  2729. //
  2730. // only close handles on error
  2731. //
  2732. if( !NT_SUCCESS(Status) ) {
  2733. CmpCmdHiveClose(NewHive);
  2734. }
  2735. ASSERT( NewHive->HiveLock );
  2736. ExFreePool(NewHive->HiveLock);
  2737. ASSERT( NewHive->ViewLock );
  2738. ExFreePool(NewHive->ViewLock);
  2739. CmpFree(NewHive, sizeof(CMHIVE));
  2740. ErrorExit:
  2741. //
  2742. // Set global quota back to what it was.
  2743. //
  2744. CmpGlobalQuotaAllowed = OldQuotaAllowed;
  2745. CmpGlobalQuotaWarning = OldQuotaWarning;
  2746. #ifdef CHECK_REGISTRY_USECOUNT
  2747. CmpCheckRegistryUseCount();
  2748. #endif //CHECK_REGISTRY_USECOUNT
  2749. CmpUnlockRegistry();
  2750. return(Status);
  2751. }
  2752. #ifdef NT_RENAME_KEY
  2753. ULONG
  2754. CmpComputeKcbConvKey(
  2755. PCM_KEY_CONTROL_BLOCK KeyControlBlock
  2756. );
  2757. NTSTATUS
  2758. CmRenameKey(
  2759. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  2760. IN UNICODE_STRING NewKeyName // RAW
  2761. )
  2762. /*++
  2763. Routine Description:
  2764. Changes the name of the key to the given one.
  2765. What needs to be done:
  2766. 1. Allocate a cell big enough to accomodate new knode
  2767. 2. make a duplicate of the index in subkeylist of kcb's parent
  2768. 3. replace parent's subkeylist with the duplicate
  2769. 4. add new subkey to parent
  2770. 5. remove old subkey
  2771. 6. free storage.
  2772. Arguments:
  2773. KeyControlBlock - pointer to kcb for key to operate on
  2774. NewKeyName - The new name to be given to this key
  2775. Return Value:
  2776. NTSTATUS - Result code from call, among the following:
  2777. <TBS>
  2778. Comments:
  2779. What do we do with symbolic links?
  2780. --*/
  2781. {
  2782. NTSTATUS Status;
  2783. PHHIVE Hive;
  2784. HCELL_INDEX Cell;
  2785. PCM_KEY_NODE Node;
  2786. PCM_KEY_NODE ParentNode;
  2787. ULONG NodeSize;
  2788. HCELL_INDEX NewKeyCell = HCELL_NIL;
  2789. HSTORAGE_TYPE StorageType;
  2790. HCELL_INDEX OldSubKeyList = HCELL_NIL;
  2791. PCM_KEY_NODE NewKeyNode;
  2792. PCM_KEY_INDEX Index;
  2793. ULONG i;
  2794. LARGE_INTEGER TimeStamp;
  2795. ULONG NameLength;
  2796. PCM_NAME_CONTROL_BLOCK OldNcb;
  2797. ULONG ConvKey;
  2798. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmRenameKey\n"));
  2799. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  2800. //
  2801. // no edits, on keys marked for deletion
  2802. //
  2803. if (KeyControlBlock->Delete) {
  2804. return STATUS_KEY_DELETED;
  2805. }
  2806. //
  2807. // see if the newName is not already a subkey of parentKcb
  2808. //
  2809. Hive = KeyControlBlock->KeyHive;
  2810. Cell = KeyControlBlock->KeyCell;
  2811. StorageType = HvGetCellType(Cell);
  2812. //
  2813. // OBS. we could have worked with the kcb tree instead, but if this is not
  2814. // going to work, we are in trouble anyway, so it's better to find out soon
  2815. //
  2816. Node = (PCM_KEY_NODE)HvGetCell(Hive,Cell);
  2817. if( Node == NULL ) {
  2818. //
  2819. // cannot map view
  2820. //
  2821. return STATUS_INSUFFICIENT_RESOURCES;
  2822. }
  2823. // release the cell right here, as the registry is locked exclusively, so we don't care
  2824. HvReleaseCell(Hive, Cell);
  2825. //
  2826. // cannot rename the root of a hive; or anything in the master hive !!!
  2827. //
  2828. if((Hive == &CmpMasterHive->Hive) || (KeyControlBlock->ParentKcb == NULL) || (KeyControlBlock->ParentKcb->KeyHive == &CmpMasterHive->Hive) ) {
  2829. return STATUS_ACCESS_DENIED;
  2830. }
  2831. ParentNode = (PCM_KEY_NODE)HvGetCell(Hive,Node->Parent);
  2832. if( ParentNode == NULL ) {
  2833. //
  2834. // cannot map view
  2835. //
  2836. return STATUS_INSUFFICIENT_RESOURCES;
  2837. }
  2838. // release the cell right here, as the registry is locked exclusively, so we don't care
  2839. HvReleaseCell(Hive, Node->Parent);
  2840. try {
  2841. if( CmpFindSubKeyByName(Hive,ParentNode,&NewKeyName) != HCELL_NIL ) {
  2842. //
  2843. // a subkey with this name already exists
  2844. //
  2845. return STATUS_CANNOT_DELETE;
  2846. }
  2847. //
  2848. // since we are in try-except, compute the new node size
  2849. //
  2850. NodeSize = CmpHKeyNodeSize(Hive, &NewKeyName);
  2851. } except (EXCEPTION_EXECUTE_HANDLER) {
  2852. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"!!CmRenameKey: code:%08lx\n", GetExceptionCode()));
  2853. return GetExceptionCode();
  2854. }
  2855. //
  2856. // 1. Allocate the new knode cell and copy the data from the old one, updating
  2857. // the name.
  2858. //
  2859. // mark the parent dirty, as we will modify its SubkeyLists
  2860. //
  2861. if(!HvMarkCellDirty(Hive, Node->Parent)) {
  2862. return STATUS_NO_LOG_SPACE;
  2863. }
  2864. //
  2865. // mark the index dirty as we are going to free it on success
  2866. //
  2867. if ( !CmpMarkIndexDirty(Hive, Node->Parent, Cell) ) {
  2868. return STATUS_NO_LOG_SPACE;
  2869. }
  2870. //
  2871. // mark key_node as dirty as we are going to free it if we succeed
  2872. //
  2873. if(!HvMarkCellDirty(Hive, Cell)) {
  2874. return STATUS_NO_LOG_SPACE;
  2875. }
  2876. OldSubKeyList = ParentNode->SubKeyLists[StorageType];
  2877. ASSERT( OldSubKeyList != HCELL_NIL );
  2878. if(!HvMarkCellDirty(Hive, OldSubKeyList)) {
  2879. return STATUS_NO_LOG_SPACE;
  2880. }
  2881. Index = (PCM_KEY_INDEX)HvGetCell(Hive,OldSubKeyList);
  2882. if( Index == NULL ) {
  2883. //
  2884. // this is a bad joke; we just marked this dirty
  2885. //
  2886. ASSERT( FALSE );
  2887. Status = STATUS_INSUFFICIENT_RESOURCES;
  2888. goto ErrorExit;
  2889. }
  2890. // release the cell right here, as the registry is locked exclusively, so we don't care
  2891. HvReleaseCell(Hive, OldSubKeyList);
  2892. //
  2893. // mark all the index cells dirty
  2894. //
  2895. if( Index->Signature == CM_KEY_INDEX_ROOT ) {
  2896. //
  2897. // it's a root
  2898. //
  2899. for(i=0;i<Index->Count;i++) {
  2900. // common sense
  2901. ASSERT( (Index->List[i] != 0) && (Index->List[i] != HCELL_NIL) );
  2902. if(!HvMarkCellDirty(Hive, Index->List[i])) {
  2903. return STATUS_NO_LOG_SPACE;
  2904. }
  2905. }
  2906. }
  2907. NewKeyCell = HvAllocateCell(
  2908. Hive,
  2909. NodeSize,
  2910. StorageType,
  2911. Cell // in the same vicinity
  2912. );
  2913. if( NewKeyCell == HCELL_NIL ) {
  2914. return STATUS_INSUFFICIENT_RESOURCES;
  2915. }
  2916. NewKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,NewKeyCell);
  2917. if( NewKeyNode == NULL ) {
  2918. //
  2919. // cannot map view; this shouldn't happen as we just allocated
  2920. // this cell (i.e. it should be dirty/pinned into memory)
  2921. //
  2922. ASSERT( FALSE );
  2923. Status = STATUS_INSUFFICIENT_RESOURCES;
  2924. goto ErrorExit;
  2925. }
  2926. // release the cell right here, as the registry is locked exclusively, so we don't care
  2927. HvReleaseCell(Hive, NewKeyCell);
  2928. //
  2929. // copy old keynode info onto the new cell and update the name
  2930. //
  2931. // first everything BUT the name
  2932. RtlCopyMemory(NewKeyNode,Node,FIELD_OFFSET(CM_KEY_NODE, Name));
  2933. // second, the new name
  2934. try {
  2935. NewKeyNode->NameLength = CmpCopyName( Hive,
  2936. NewKeyNode->Name,
  2937. &NewKeyName);
  2938. NameLength = NewKeyName.Length;
  2939. if (NewKeyNode->NameLength < NameLength ) {
  2940. NewKeyNode->Flags |= KEY_COMP_NAME;
  2941. } else {
  2942. NewKeyNode->Flags &= ~KEY_COMP_NAME;
  2943. }
  2944. } except (EXCEPTION_EXECUTE_HANDLER) {
  2945. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"!!CmRenameKey: code:%08lx\n", GetExceptionCode()));
  2946. Status = GetExceptionCode();
  2947. goto ErrorExit;
  2948. }
  2949. // third, the timestamp
  2950. KeQuerySystemTime(&TimeStamp);
  2951. NewKeyNode->LastWriteTime = TimeStamp;
  2952. //
  2953. // at this point we have the new key_node all built up.
  2954. //
  2955. //
  2956. // 2.3. Make a duplicate of the parent's subkeylist and replace the original
  2957. //
  2958. ParentNode->SubKeyLists[StorageType] = CmpDuplicateIndex(Hive,OldSubKeyList,StorageType);
  2959. if( ParentNode->SubKeyLists[StorageType] == HCELL_NIL ) {
  2960. Status = STATUS_INSUFFICIENT_RESOURCES;
  2961. goto ErrorExit;
  2962. }
  2963. //
  2964. // 4. Add new subkey to the parent. This will take care of index
  2965. // grow and rebalance problems.
  2966. // Note: the index is at this point a duplicate, so if we fail, we still have the
  2967. // original one handy to recover
  2968. //
  2969. if( !CmpAddSubKey(Hive,Node->Parent,NewKeyCell) ) {
  2970. Status = STATUS_INSUFFICIENT_RESOURCES;
  2971. goto ErrorExit;
  2972. }
  2973. //
  2974. // 5. remove old subkey;
  2975. //
  2976. if( !CmpRemoveSubKey(Hive,Node->Parent,Cell) ) {
  2977. Status = STATUS_INSUFFICIENT_RESOURCES;
  2978. goto ErrorExit;
  2979. }
  2980. //
  2981. // 5'. update the parent on each and every son.
  2982. //
  2983. if( !CmpUpdateParentForEachSon(Hive,NewKeyCell) ) {
  2984. Status = STATUS_INSUFFICIENT_RESOURCES;
  2985. goto ErrorExit;
  2986. }
  2987. //
  2988. // update the NCB in the kcb; at the end of this function, the kcbs underneath this
  2989. // will eventually get rehashed
  2990. //
  2991. OldNcb = KeyControlBlock->NameBlock;
  2992. try {
  2993. KeyControlBlock->NameBlock = CmpGetNameControlBlock (&NewKeyName);
  2994. } except (EXCEPTION_EXECUTE_HANDLER) {
  2995. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"!!CmRenameKey: code:%08lx\n", GetExceptionCode()));
  2996. Status = GetExceptionCode();
  2997. goto ErrorExit;
  2998. }
  2999. //
  3000. // 6. At this point we have it all done. We just need to free the old index and key_cell
  3001. //
  3002. //
  3003. // free old index
  3004. //
  3005. Index = (PCM_KEY_INDEX)HvGetCell(Hive,OldSubKeyList);
  3006. if( Index == NULL ) {
  3007. //
  3008. // this is a bad joke; we just marked this dirty
  3009. //
  3010. ASSERT( FALSE );
  3011. Status = STATUS_INSUFFICIENT_RESOURCES;
  3012. goto ErrorExit;
  3013. }
  3014. // release the cell right here, as the registry is locked exclusively, so we don't care
  3015. HvReleaseCell(Hive, OldSubKeyList);
  3016. if( Index->Signature == CM_KEY_INDEX_ROOT ) {
  3017. //
  3018. // it's a root
  3019. //
  3020. for(i=0;i<Index->Count;i++) {
  3021. // common sense
  3022. ASSERT( (Index->List[i] != 0) && (Index->List[i] != HCELL_NIL) );
  3023. HvFreeCell(Hive, Index->List[i]);
  3024. }
  3025. } else {
  3026. //
  3027. // should be a leaf
  3028. //
  3029. ASSERT((Index->Signature == CM_KEY_INDEX_LEAF) ||
  3030. (Index->Signature == CM_KEY_FAST_LEAF) ||
  3031. (Index->Signature == CM_KEY_HASH_LEAF)
  3032. );
  3033. ASSERT(Index->Count != 0);
  3034. }
  3035. HvFreeCell(Hive, OldSubKeyList);
  3036. //
  3037. // free old cell
  3038. //
  3039. HvFreeCell(Hive,Cell);
  3040. //
  3041. // update the node KeyCell for this kcb and the timestamp on the kcb;
  3042. //
  3043. KeyControlBlock->KeyCell = NewKeyCell;
  3044. KeyControlBlock->KcbLastWriteTime = TimeStamp;
  3045. //
  3046. // and one last "little" thing: update parent's maxnamelen and reset parents cache
  3047. //
  3048. CmpCleanUpSubKeyInfo (KeyControlBlock->ParentKcb);
  3049. if (ParentNode->MaxNameLen < NameLength) {
  3050. ParentNode->MaxNameLen = NameLength;
  3051. KeyControlBlock->ParentKcb->KcbMaxNameLen = (USHORT)NameLength;
  3052. }
  3053. //
  3054. // rehash this kcb
  3055. //
  3056. ConvKey = CmpComputeKcbConvKey(KeyControlBlock);
  3057. if( ConvKey != KeyControlBlock->ConvKey ) {
  3058. //
  3059. // rehash the kcb by removing it from hash, and then inserting it
  3060. // again with th new ConvKey
  3061. //
  3062. CmpRemoveKeyHash(&(KeyControlBlock->KeyHash));
  3063. KeyControlBlock->ConvKey = ConvKey;
  3064. CmpInsertKeyHash(&(KeyControlBlock->KeyHash),FALSE);
  3065. }
  3066. //
  3067. // Aditional work: take care of the kcb subtree; this cannot fail, punt
  3068. //
  3069. CmpSearchForOpenSubKeys(KeyControlBlock,SearchAndRehash);
  3070. //
  3071. // last, dereference the OldNcb for this kcb
  3072. //
  3073. ASSERT( OldNcb != NULL );
  3074. CmpDereferenceNameControlBlockWithLock(OldNcb);
  3075. return STATUS_SUCCESS;
  3076. ErrorExit:
  3077. if( OldSubKeyList != HCELL_NIL ) {
  3078. //
  3079. // we have attempted (maybe even succedded) to duplicate parent's index)
  3080. //
  3081. if( ParentNode->SubKeyLists[StorageType] != HCELL_NIL ) {
  3082. //
  3083. // we need to free this as it is a duplicate
  3084. //
  3085. Index = (PCM_KEY_INDEX)HvGetCell(Hive,ParentNode->SubKeyLists[StorageType]);
  3086. if( Index == NULL ) {
  3087. //
  3088. // could not map view;this shouldn't happen as we just allocated this cell
  3089. //
  3090. ASSERT( FALSE );
  3091. } else {
  3092. // release the cell right here, as the registry is locked exclusively, so we don't care
  3093. HvReleaseCell(Hive, ParentNode->SubKeyLists[StorageType]);
  3094. if( Index->Signature == CM_KEY_INDEX_ROOT ) {
  3095. //
  3096. // it's a root
  3097. //
  3098. for(i=0;i<Index->Count;i++) {
  3099. // common sense
  3100. ASSERT( (Index->List[i] != 0) && (Index->List[i] != HCELL_NIL) );
  3101. HvFreeCell(Hive, Index->List[i]);
  3102. }
  3103. } else {
  3104. //
  3105. // should be a leaf
  3106. //
  3107. ASSERT((Index->Signature == CM_KEY_INDEX_LEAF) ||
  3108. (Index->Signature == CM_KEY_FAST_LEAF) ||
  3109. (Index->Signature == CM_KEY_HASH_LEAF)
  3110. );
  3111. ASSERT(Index->Count != 0);
  3112. }
  3113. HvFreeCell(Hive, ParentNode->SubKeyLists[StorageType]);
  3114. }
  3115. }
  3116. //
  3117. // restore the parent's index
  3118. //
  3119. ParentNode->SubKeyLists[StorageType] = OldSubKeyList;
  3120. }
  3121. ASSERT( NewKeyCell != HCELL_NIL );
  3122. HvFreeCell(Hive,NewKeyCell);
  3123. if( OldNcb != NULL ) {
  3124. KeyControlBlock->NameBlock = OldNcb;
  3125. }
  3126. return Status;
  3127. }
  3128. #endif
  3129. NTSTATUS
  3130. CmMoveKey(
  3131. IN PCM_KEY_CONTROL_BLOCK KeyControlBlock
  3132. )
  3133. /*++
  3134. Routine Description:
  3135. Moves all the cells related to this kcb above the specified fileoffset.
  3136. What needs to be done:
  3137. 1. mark all data that we are going to touch dirty
  3138. 2. Duplicate the key_node (and values and all cells involved)
  3139. 3. Update the parent for all children
  3140. 4. replace the new Key_cell in the parent's subkeylist
  3141. 5. Update the kcb and the kcb cache
  3142. 6. remove old subkey
  3143. WARNING:
  3144. after 3 we cannot fail anymore. if we do, we'll leak cells.
  3145. Arguments:
  3146. KeyControlBlock - pointer to kcb for key to operate on
  3147. Return Value:
  3148. NTSTATUS - Result code from call, among the following:
  3149. <TBS>
  3150. --*/
  3151. {
  3152. NTSTATUS Status;
  3153. PHHIVE Hive;
  3154. HCELL_INDEX OldKeyCell;
  3155. HCELL_INDEX NewKeyCell = HCELL_NIL;
  3156. HCELL_INDEX ParentKeyCell;
  3157. HSTORAGE_TYPE StorageType;
  3158. PCM_KEY_NODE OldKeyNode;
  3159. PCM_KEY_NODE ParentKeyNode;
  3160. PCM_KEY_NODE NewKeyNode;
  3161. PCM_KEY_INDEX ParentIndex;
  3162. PCM_KEY_INDEX OldIndex;
  3163. ULONG i,j;
  3164. HCELL_INDEX LeafCell;
  3165. PCM_KEY_INDEX Leaf;
  3166. PCM_KEY_FAST_INDEX FastIndex;
  3167. PHCELL_INDEX ParentIndexLocation = NULL;
  3168. PAGED_CODE();
  3169. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmMoveKey\n"));
  3170. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  3171. //
  3172. // no edits, on keys marked for deletion
  3173. //
  3174. if (KeyControlBlock->Delete) {
  3175. return STATUS_KEY_DELETED;
  3176. }
  3177. //
  3178. // see if the newName is not already a subkey of parentKcb
  3179. //
  3180. Hive = KeyControlBlock->KeyHive;
  3181. OldKeyCell = KeyControlBlock->KeyCell;
  3182. StorageType = HvGetCellType(OldKeyCell);
  3183. if( StorageType != Stable ) {
  3184. //
  3185. // nop the volatiles
  3186. //
  3187. return STATUS_SUCCESS;
  3188. }
  3189. if( OldKeyCell == Hive->BaseBlock->RootCell ) {
  3190. //
  3191. // this works only for stable keys.
  3192. //
  3193. return STATUS_INVALID_PARAMETER;
  3194. }
  3195. //
  3196. // 1. mark all data that we are going to touch dirty
  3197. //
  3198. // parent's index, as we will replace the key node cell in it
  3199. // we only search in the Stable storage. It is supposed to be there
  3200. //
  3201. OldKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,OldKeyCell);
  3202. if( OldKeyNode == NULL ) {
  3203. //
  3204. // cannot map view
  3205. //
  3206. return STATUS_INSUFFICIENT_RESOURCES;
  3207. }
  3208. if (! CmpMarkKeyDirty(Hive, OldKeyCell
  3209. #if DBG
  3210. ,FALSE
  3211. #endif //DBG
  3212. )) {
  3213. HvReleaseCell(Hive, OldKeyCell);
  3214. return STATUS_NO_LOG_SPACE;
  3215. }
  3216. // release the cell right here, as the registry is locked exclusively, and the key_cell is marked as dirty
  3217. HvReleaseCell(Hive, OldKeyCell);
  3218. if( OldKeyNode->Flags & KEY_SYM_LINK ) {
  3219. //
  3220. // we do not compact links
  3221. //
  3222. return STATUS_INVALID_PARAMETER;
  3223. }
  3224. if( OldKeyNode->SubKeyLists[Stable] != HCELL_NIL ) {
  3225. //
  3226. // mark the index dirty
  3227. //
  3228. OldIndex = (PCM_KEY_INDEX)HvGetCell(Hive, OldKeyNode->SubKeyLists[Stable]);
  3229. if( OldIndex == NULL ) {
  3230. //
  3231. // we couldn't map the bin containing this cell
  3232. //
  3233. return STATUS_INSUFFICIENT_RESOURCES;
  3234. }
  3235. HvReleaseCell(Hive, OldKeyNode->SubKeyLists[Stable]);
  3236. if( !HvMarkCellDirty(Hive, OldKeyNode->SubKeyLists[Stable]) ) {
  3237. return STATUS_NO_LOG_SPACE;
  3238. }
  3239. if(OldIndex->Signature == CM_KEY_INDEX_ROOT) {
  3240. for (i = 0; i < OldIndex->Count; i++) {
  3241. if( !HvMarkCellDirty(Hive, OldIndex->List[i]) ) {
  3242. return STATUS_NO_LOG_SPACE;
  3243. }
  3244. }
  3245. }
  3246. }
  3247. ParentKeyCell = OldKeyNode->Parent;
  3248. //
  3249. // now in the parent's spot
  3250. //
  3251. ParentKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,ParentKeyCell);
  3252. if( ParentKeyNode == NULL ) {
  3253. //
  3254. // cannot map view
  3255. //
  3256. return STATUS_INSUFFICIENT_RESOURCES;
  3257. }
  3258. if( !HvMarkCellDirty(Hive, ParentKeyCell) ) {
  3259. HvReleaseCell(Hive, ParentKeyCell);
  3260. return STATUS_NO_LOG_SPACE;
  3261. }
  3262. // release the cell right here, as the registry is locked exclusively, so we don't care
  3263. // Key_cell is marked dirty to keep the parent knode mapped
  3264. HvReleaseCell(Hive, ParentKeyCell);
  3265. ParentIndex = (PCM_KEY_INDEX)HvGetCell(Hive, ParentKeyNode->SubKeyLists[Stable]);
  3266. if( ParentIndex == NULL ) {
  3267. //
  3268. // we couldn't map the bin containing this cell
  3269. //
  3270. return STATUS_INSUFFICIENT_RESOURCES;
  3271. }
  3272. HvReleaseCell(Hive, ParentKeyNode->SubKeyLists[Stable]);
  3273. if(ParentIndex->Signature == CM_KEY_INDEX_ROOT) {
  3274. //
  3275. // step through root, till we find the right leaf
  3276. //
  3277. for (i = 0; i < ParentIndex->Count; i++) {
  3278. LeafCell = ParentIndex->List[i];
  3279. Leaf = (PCM_KEY_INDEX)HvGetCell(Hive, LeafCell);
  3280. if( Leaf == NULL ) {
  3281. //
  3282. // we couldn't map the bin containing this cell
  3283. //
  3284. return STATUS_INSUFFICIENT_RESOURCES;
  3285. }
  3286. HvReleaseCell(Hive, LeafCell);
  3287. if ( (Leaf->Signature == CM_KEY_FAST_LEAF) ||
  3288. (Leaf->Signature == CM_KEY_HASH_LEAF)
  3289. ) {
  3290. FastIndex = (PCM_KEY_FAST_INDEX)Leaf;
  3291. for(j=0;j<FastIndex->Count;j++) {
  3292. if( FastIndex->List[j].Cell == OldKeyCell ) {
  3293. //
  3294. // found it! remember the locations we want to update later and break the loop
  3295. //
  3296. if( !HvMarkCellDirty(Hive, LeafCell) ) {
  3297. return STATUS_NO_LOG_SPACE;
  3298. }
  3299. ParentIndexLocation = &(FastIndex->List[j].Cell);
  3300. break;
  3301. }
  3302. }
  3303. if( ParentIndexLocation != NULL ) {
  3304. break;
  3305. }
  3306. } else {
  3307. for(j=0;j<Leaf->Count;j++) {
  3308. if( Leaf->List[j] == OldKeyCell ) {
  3309. //
  3310. // found it! remember the locations we want to update later and break the loop
  3311. //
  3312. if( !HvMarkCellDirty(Hive, LeafCell) ) {
  3313. return STATUS_NO_LOG_SPACE;
  3314. }
  3315. ParentIndexLocation = &(Leaf->List[j]);
  3316. break;
  3317. }
  3318. }
  3319. if( ParentIndexLocation != NULL ) {
  3320. break;
  3321. }
  3322. }
  3323. }
  3324. } else if ( (ParentIndex->Signature == CM_KEY_FAST_LEAF) ||
  3325. (ParentIndex->Signature == CM_KEY_HASH_LEAF)
  3326. ) {
  3327. FastIndex = (PCM_KEY_FAST_INDEX)ParentIndex;
  3328. for(j=0;j<FastIndex->Count;j++) {
  3329. if( FastIndex->List[j].Cell == OldKeyCell ) {
  3330. //
  3331. // found it! remember the locations we want to update later and break the loop
  3332. //
  3333. if( !HvMarkCellDirty(Hive, ParentKeyNode->SubKeyLists[Stable]) ) {
  3334. return STATUS_NO_LOG_SPACE;
  3335. }
  3336. ParentIndexLocation = &(FastIndex->List[j].Cell);
  3337. break;
  3338. }
  3339. }
  3340. } else {
  3341. for(j=0;j<ParentIndex->Count;j++) {
  3342. if( ParentIndex->List[j] == OldKeyCell ) {
  3343. //
  3344. // found it! remember the locations we want to update later and break the loop
  3345. //
  3346. if( !HvMarkCellDirty(Hive, ParentKeyNode->SubKeyLists[Stable]) ) {
  3347. return STATUS_NO_LOG_SPACE;
  3348. }
  3349. ParentIndexLocation = &(ParentIndex->List[j]);
  3350. break;
  3351. }
  3352. }
  3353. }
  3354. // we should've find it !!!
  3355. ASSERT( ParentIndexLocation != NULL );
  3356. //
  3357. // 2. Duplicate the key_node (and values and all cells involved)
  3358. //
  3359. Status = CmpDuplicateKey(Hive,OldKeyCell,&NewKeyCell);
  3360. if( !NT_SUCCESS(Status) ) {
  3361. return Status;
  3362. }
  3363. // sanity
  3364. ASSERT( (NewKeyCell != HCELL_NIL) && (StorageType == (HSTORAGE_TYPE)HvGetCellType(NewKeyCell)));
  3365. //
  3366. // 3. update the parent on each and every son.
  3367. //
  3368. if( !CmpUpdateParentForEachSon(Hive,NewKeyCell) ) {
  3369. Status = STATUS_INSUFFICIENT_RESOURCES;
  3370. goto ErrorExit;
  3371. }
  3372. //
  3373. // 4. replace the new Key_cell in the parent's subkeylist
  3374. // From now on, WE CANNOT fails. we have everything marked dirty
  3375. // we just update some fields. no resources required !
  3376. // If we fail to free some cells, too bad, we'll leak some cells.
  3377. //
  3378. *ParentIndexLocation = NewKeyCell;
  3379. //
  3380. // 5. Update the kcb and the kcb cache
  3381. //
  3382. CmpCleanUpSubKeyInfo(KeyControlBlock->ParentKcb);
  3383. KeyControlBlock->KeyCell = NewKeyCell;
  3384. CmpRebuildKcbCache(KeyControlBlock);
  3385. //
  3386. // 6. remove old subkey
  3387. //
  3388. // First the Index; it's already marked dirty (i.e. PINNED)
  3389. //
  3390. if( OldKeyNode->SubKeyLists[Stable] != HCELL_NIL ) {
  3391. OldIndex = (PCM_KEY_INDEX)HvGetCell(Hive, OldKeyNode->SubKeyLists[Stable]);
  3392. ASSERT( OldIndex != NULL );
  3393. HvReleaseCell(Hive, OldKeyNode->SubKeyLists[Stable]);
  3394. if(OldIndex->Signature == CM_KEY_INDEX_ROOT) {
  3395. for (i = 0; i < OldIndex->Count; i++) {
  3396. HvFreeCell(Hive, OldIndex->List[i]);
  3397. }
  3398. }
  3399. HvFreeCell(Hive,OldKeyNode->SubKeyLists[Stable]);
  3400. }
  3401. OldKeyNode->SubKeyCounts[Stable] = 0;
  3402. OldKeyNode->SubKeyCounts[Volatile] = 0;
  3403. CmpFreeKeyByCell(Hive,OldKeyCell,FALSE);
  3404. return STATUS_SUCCESS;
  3405. ErrorExit:
  3406. //
  3407. // we need to free the new knode allocated
  3408. //
  3409. NewKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,NewKeyCell);
  3410. // must be dirty
  3411. ASSERT( NewKeyNode != NULL );
  3412. HvReleaseCell(Hive, NewKeyCell);
  3413. if( NewKeyNode->SubKeyLists[Stable] != HCELL_NIL ) {
  3414. OldIndex = (PCM_KEY_INDEX)HvGetCell(Hive, NewKeyNode->SubKeyLists[Stable]);
  3415. ASSERT( OldIndex != NULL );
  3416. HvReleaseCell(Hive, NewKeyNode->SubKeyLists[Stable]);
  3417. if(OldIndex->Signature == CM_KEY_INDEX_ROOT) {
  3418. for (i = 0; i < OldIndex->Count; i++) {
  3419. HvFreeCell(Hive, OldIndex->List[i]);
  3420. }
  3421. }
  3422. HvFreeCell(Hive,NewKeyNode->SubKeyLists[Stable]);
  3423. }
  3424. NewKeyNode->SubKeyCounts[Stable] = 0;
  3425. NewKeyNode->SubKeyCounts[Volatile] = 0;
  3426. CmpFreeKeyByCell(Hive,NewKeyCell,FALSE);
  3427. return Status;
  3428. }
  3429. NTSTATUS
  3430. CmpDuplicateKey(
  3431. PHHIVE Hive,
  3432. HCELL_INDEX OldKeyCell,
  3433. PHCELL_INDEX NewKeyCell
  3434. )
  3435. /*++
  3436. Routine Description:
  3437. Makes an exact clone of OldKeyCell key_node in the
  3438. space above AboveFileOffset.
  3439. Operates on Stable storage ONLY!!!
  3440. Arguments:
  3441. Return Value:
  3442. NTSTATUS - Result code from call, among the following:
  3443. <TBS>
  3444. --*/
  3445. {
  3446. PCM_KEY_NODE OldKeyNode;
  3447. PCM_KEY_NODE NewKeyNode;
  3448. ULONG i;
  3449. PRELEASE_CELL_ROUTINE TargetReleaseCellRoutine;
  3450. PAGED_CODE();
  3451. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  3452. ASSERT( HvGetCellType(OldKeyCell) == Stable );
  3453. OldKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,OldKeyCell);
  3454. if( OldKeyNode == NULL ) {
  3455. //
  3456. // cannot map view
  3457. //
  3458. return STATUS_INSUFFICIENT_RESOURCES;
  3459. }
  3460. //
  3461. // since the registry is locked exclusively here, we don't need to lock/release cells
  3462. // while copying the trees; So, we just set the release routines to NULL and restore after
  3463. // the copy is complete; this saves some pain
  3464. //
  3465. TargetReleaseCellRoutine = Hive->ReleaseCellRoutine;
  3466. Hive->ReleaseCellRoutine = NULL;
  3467. *NewKeyCell = CmpCopyKeyPartial(Hive,OldKeyCell,Hive,OldKeyNode->Parent,TRUE);
  3468. Hive->ReleaseCellRoutine = TargetReleaseCellRoutine;
  3469. if( *NewKeyCell == HCELL_NIL ) {
  3470. HvReleaseCell(Hive, OldKeyCell);
  3471. return STATUS_INSUFFICIENT_RESOURCES;
  3472. }
  3473. NewKeyNode = (PCM_KEY_NODE)HvGetCell(Hive,*NewKeyCell);
  3474. if( NewKeyNode == NULL ) {
  3475. //
  3476. // cannot map view
  3477. //
  3478. HvReleaseCell(Hive, OldKeyCell);
  3479. CmpFreeKeyByCell(Hive,*NewKeyCell,FALSE);
  3480. return STATUS_INSUFFICIENT_RESOURCES;
  3481. }
  3482. //
  3483. // now we have the key_cell duplicated. Values and security has also been taken care of
  3484. // Go ahead and duplicate the Index.
  3485. //
  3486. if( OldKeyNode->SubKeyLists[Stable] != HCELL_NIL ) {
  3487. NewKeyNode->SubKeyLists[Stable] = CmpDuplicateIndex(Hive,OldKeyNode->SubKeyLists[Stable],Stable);
  3488. if( NewKeyNode->SubKeyLists[Stable] == HCELL_NIL ) {
  3489. HvReleaseCell(Hive, OldKeyCell);
  3490. CmpFreeKeyByCell(Hive,*NewKeyCell,FALSE);
  3491. HvReleaseCell(Hive, *NewKeyCell);
  3492. return STATUS_INSUFFICIENT_RESOURCES;
  3493. }
  3494. } else {
  3495. ASSERT( OldKeyNode->SubKeyCounts[Stable] == 0 );
  3496. NewKeyNode->SubKeyLists[Stable] = HCELL_NIL;
  3497. }
  3498. NewKeyNode->SubKeyCounts[Stable] = OldKeyNode->SubKeyCounts[Stable];
  3499. NewKeyNode->SubKeyLists[Volatile] = OldKeyNode->SubKeyLists[Volatile];
  3500. NewKeyNode->SubKeyCounts[Volatile] = OldKeyNode->SubKeyCounts[Volatile];
  3501. HvReleaseCell(Hive, *NewKeyCell);
  3502. HvReleaseCell(Hive, OldKeyCell);
  3503. return STATUS_SUCCESS;
  3504. }
  3505. #ifdef WRITE_PROTECTED_REGISTRY_POOL
  3506. VOID
  3507. CmpMarkAllBinsReadOnly(
  3508. PHHIVE Hive
  3509. )
  3510. /*++
  3511. Routine Description:
  3512. Marks the memory allocated for all the stable bins in this hive as read only.
  3513. Arguments:
  3514. Hive - supplies a pointer to the hive control structure for the
  3515. hive of interest
  3516. Return Value:
  3517. NONE (It should work!)
  3518. --*/
  3519. {
  3520. PHMAP_ENTRY t;
  3521. PHBIN Bin;
  3522. HCELL_INDEX p;
  3523. ULONG Length;
  3524. //
  3525. // we are only interested in the stable storage
  3526. //
  3527. Length = Hive->Storage[Stable].Length;
  3528. p = 0;
  3529. //
  3530. // for each bin in the space
  3531. //
  3532. while (p < Length) {
  3533. t = HvpGetCellMap(Hive, p);
  3534. VALIDATE_CELL_MAP(__LINE__,t,Hive,p);
  3535. Bin = (PHBIN)HBIN_BASE(t->BinAddress);
  3536. if (t->BinAddress & HMAP_NEWALLOC) {
  3537. //
  3538. // Mark it as read Only
  3539. //
  3540. HvpChangeBinAllocation(Bin,TRUE);
  3541. }
  3542. // next one, please
  3543. p = (ULONG)p + Bin->Size;
  3544. }
  3545. }
  3546. #endif //WRITE_PROTECTED_REGISTRY_POOL
  3547. ULONG
  3548. CmpCompressKeyWorker(
  3549. PCM_KEY_CONTROL_BLOCK Current,
  3550. PVOID Context1,
  3551. PVOID Context2
  3552. )
  3553. {
  3554. PLIST_ENTRY pListHead;
  3555. PCM_KCB_REMAP_BLOCK kcbRemapBlock;
  3556. //PLIST_ENTRY AnchorAddr;
  3557. if (Current->KeyHive == Context1) {
  3558. pListHead = (PLIST_ENTRY)Context2;
  3559. ASSERT( pListHead );
  3560. /*
  3561. //
  3562. // check if we didn't already recorded this kcb
  3563. //
  3564. AnchorAddr = pListHead;
  3565. kcbRemapBlock = (PCM_KCB_REMAP_BLOCK)(pListHead->Flink);
  3566. while ( kcbRemapBlock != (PCM_KCB_REMAP_BLOCK)AnchorAddr ) {
  3567. kcbRemapBlock = CONTAINING_RECORD(
  3568. kcbRemapBlock,
  3569. CM_KCB_REMAP_BLOCK,
  3570. RemapList
  3571. );
  3572. if( kcbRemapBlock->KeyControlBlock == Current ) {
  3573. //
  3574. // we already have this kcb
  3575. //
  3576. return KCB_WORKER_CONTINUE;
  3577. }
  3578. //
  3579. // skip to the next element
  3580. //
  3581. kcbRemapBlock = (PCM_KCB_REMAP_BLOCK)(kcbRemapBlock->RemapList.Flink);
  3582. }
  3583. */
  3584. kcbRemapBlock = (PCM_KCB_REMAP_BLOCK)ExAllocatePool(PagedPool, sizeof(CM_KCB_REMAP_BLOCK));
  3585. if( kcbRemapBlock == NULL ) {
  3586. return KCB_WORKER_ERROR;
  3587. }
  3588. kcbRemapBlock->KeyControlBlock = Current;
  3589. kcbRemapBlock->NewCellIndex = HCELL_NIL;
  3590. kcbRemapBlock->OldCellIndex = Current->KeyCell;
  3591. kcbRemapBlock->ValueCount = 0;
  3592. kcbRemapBlock->ValueList = HCELL_NIL;
  3593. InsertTailList(pListHead,&(kcbRemapBlock->RemapList));
  3594. }
  3595. return KCB_WORKER_CONTINUE; // always keep searching
  3596. }
  3597. NTSTATUS
  3598. CmCompressKey(
  3599. IN PHHIVE Hive
  3600. )
  3601. /*++
  3602. Routine Description:
  3603. Compresses the kcb, by means of simulating an "in-place" SaveKey
  3604. What needs to be done:
  3605. 1. iterate through the kcb tree and make a list of all the kcbs
  3606. that need to be changed (their keycell will change during the process)
  3607. 2. iterate through the cache and compute an array of security cells.
  3608. We'll need it to map security cells into the new hive.
  3609. 3. Save the hive into a temporary hive, preserving
  3610. the volatile info in keynodes and updating the cell mappings.
  3611. 4. Update the cache by adding volatile security cells from the old hive.
  3612. 5. Dump temporary (compressed) hive over to the old file.
  3613. 6. Switch hive data from the compressed one to the existing one and update
  3614. the kcb KeyCell and security mapping
  3615. 7. Invalidate the map and drop paged bins.
  3616. 8. Free storage for the new hive (OK if we fail)
  3617. Arguments:
  3618. Hive - Hive to operate on
  3619. Return Value:
  3620. NTSTATUS - Result code from call, among the following:
  3621. <TBS>
  3622. --*/
  3623. {
  3624. NTSTATUS Status = STATUS_SUCCESS;
  3625. HCELL_INDEX KeyCell;
  3626. PCMHIVE CmHive;
  3627. PCM_KCB_REMAP_BLOCK RemapBlock;
  3628. PCMHIVE NewHive = NULL;
  3629. HCELL_INDEX LinkCell;
  3630. PCM_KEY_NODE LinkNode;
  3631. PCM_KNODE_REMAP_BLOCK KnodeRemapBlock;
  3632. ULONG OldLength;
  3633. PAGED_CODE();
  3634. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_CM,"CmCompressKey\n"));
  3635. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  3636. if( HvAutoCompressCheck(Hive) == FALSE ) {
  3637. return STATUS_SUCCESS;
  3638. }
  3639. KeyCell = Hive->BaseBlock->RootCell;
  3640. CmHive = CONTAINING_RECORD(Hive, CMHIVE, Hive);
  3641. //
  3642. // Make sure the cell passed in is the root cell of the hive.
  3643. //
  3644. if ( CmHive == CmpMasterHive ) {
  3645. return STATUS_INVALID_PARAMETER;
  3646. }
  3647. //
  3648. // 0. Get the cells we need to relink the compressed hive
  3649. //
  3650. LinkNode = (PCM_KEY_NODE)HvGetCell(Hive,KeyCell);
  3651. if( LinkNode == NULL ) {
  3652. return STATUS_INSUFFICIENT_RESOURCES;
  3653. }
  3654. LinkCell = LinkNode->Parent;
  3655. HvReleaseCell(Hive,KeyCell);
  3656. LinkNode = (PCM_KEY_NODE)HvGetCell((PHHIVE)CmpMasterHive,LinkCell);
  3657. // master storage is paged pool
  3658. ASSERT(LinkNode != NULL);
  3659. HvReleaseCell((PHHIVE)CmpMasterHive,LinkCell);
  3660. OldLength = Hive->BaseBlock->Length;
  3661. //
  3662. // 1. iterate through the kcb tree and make a list of all the kcbs
  3663. // that need to be changed (their keycell will change during the process)
  3664. //
  3665. ASSERT( IsListEmpty(&(CmHive->KcbConvertListHead)) );
  3666. //
  3667. // this will kick all kcb with refcount == 0 out of cache, so we can use
  3668. // CmpSearchKeyControlBlockTree for recording referenced kcbs
  3669. //
  3670. CmpCleanUpKCBCacheTable();
  3671. //CmpSearchForOpenSubKeys(KeyControlBlock,SearchIfExist);
  3672. if( !CmpSearchKeyControlBlockTree(CmpCompressKeyWorker,(PVOID)Hive,(PVOID)(&(CmHive->KcbConvertListHead))) ) {
  3673. Status = STATUS_INSUFFICIENT_RESOURCES;
  3674. goto Exit;
  3675. }
  3676. //
  3677. // 2. iterate through the cache and compute an array of security cells.
  3678. // We'll need it to map security cells into the new hive.
  3679. //
  3680. if( !CmpBuildSecurityCellMappingArray(CmHive) ) {
  3681. Status = STATUS_INSUFFICIENT_RESOURCES;
  3682. goto Exit;
  3683. }
  3684. //
  3685. // 3. Save the hive into a temporary hive , preserving
  3686. // the volatile info in keynodes and updating the cell mappings.
  3687. //
  3688. Status = CmpShiftHiveFreeBins(CmHive,&NewHive);
  3689. if( !NT_SUCCESS(Status) ) {
  3690. goto Exit;
  3691. }
  3692. //
  3693. // 5. Dump temporary (compressed) hive over to the old file.
  3694. //
  3695. Status = CmpOverwriteHive(CmHive,NewHive,LinkCell);
  3696. if (!NT_SUCCESS(Status)) {
  3697. goto Exit;
  3698. }
  3699. //
  3700. // From this point on, we WILL NOT FAIL!
  3701. //
  3702. //
  3703. // get the root node and link it into the master storage
  3704. //
  3705. LinkNode->ChildHiveReference.KeyCell = NewHive->Hive.BaseBlock->RootCell;
  3706. //
  3707. // 6. Switch hive data from the compressed one to the existing one and update
  3708. // the kcb KeyCell and security mapping
  3709. // This should better NOT fail!!! If it does, we are doomed, as we have partial
  3710. // data => bugcheck
  3711. //
  3712. CmpSwitchStorageAndRebuildMappings(CmHive,NewHive);
  3713. //
  3714. // 7. Invalidate the map and drop paged bins. If system hive, check for the hysteresis callback.
  3715. //
  3716. HvpDropAllPagedBins(&(CmHive->Hive));
  3717. if( OldLength < CmHive->Hive.BaseBlock->Length ) {
  3718. CmpUpdateSystemHiveHysteresis(&(CmHive->Hive),CmHive->Hive.BaseBlock->Length,OldLength);
  3719. }
  3720. Exit:
  3721. //
  3722. // 8. Free storage for the new hive (OK if we fail)
  3723. //
  3724. if( NewHive != NULL ) {
  3725. CmpDestroyTemporaryHive(NewHive);
  3726. }
  3727. if( CmHive->CellRemapArray != NULL ) {
  3728. ExFreePool(CmHive->CellRemapArray);
  3729. CmHive->CellRemapArray = NULL;
  3730. }
  3731. //
  3732. // remove all remap blocks and free them
  3733. //
  3734. while (IsListEmpty(&(CmHive->KcbConvertListHead)) == FALSE) {
  3735. RemapBlock = (PCM_KCB_REMAP_BLOCK)RemoveHeadList(&(CmHive->KcbConvertListHead));
  3736. RemapBlock = CONTAINING_RECORD(
  3737. RemapBlock,
  3738. CM_KCB_REMAP_BLOCK,
  3739. RemapList
  3740. );
  3741. ExFreePool(RemapBlock);
  3742. }
  3743. while (IsListEmpty(&(CmHive->KnodeConvertListHead)) == FALSE) {
  3744. KnodeRemapBlock = (PCM_KNODE_REMAP_BLOCK)RemoveHeadList(&(CmHive->KnodeConvertListHead));
  3745. KnodeRemapBlock = CONTAINING_RECORD(
  3746. KnodeRemapBlock,
  3747. CM_KNODE_REMAP_BLOCK,
  3748. RemapList
  3749. );
  3750. ExFreePool(KnodeRemapBlock);
  3751. }
  3752. return Status;
  3753. }
  3754. NTSTATUS
  3755. CmLockKcbForWrite(PCM_KEY_CONTROL_BLOCK KeyControlBlock)
  3756. /*++
  3757. Routine Description:
  3758. Tags the kcb as being read-only and no-delay-close
  3759. Arguments:
  3760. KeyControlBlock
  3761. Return Value:
  3762. TBS
  3763. --*/
  3764. {
  3765. PAGED_CODE();
  3766. CmpLockKCBTreeExclusive();
  3767. ASSERT_KCB(KeyControlBlock);
  3768. if( KeyControlBlock->Delete ) {
  3769. CmpUnlockKCBTree();
  3770. return STATUS_KEY_DELETED;
  3771. }
  3772. //
  3773. // sanity check in case we are called twice
  3774. //
  3775. ASSERT( ((KeyControlBlock->ExtFlags&CM_KCB_READ_ONLY_KEY) && (KeyControlBlock->ExtFlags&CM_KCB_NO_DELAY_CLOSE)) ||
  3776. (!(KeyControlBlock->ExtFlags&CM_KCB_READ_ONLY_KEY))
  3777. );
  3778. //
  3779. // tag the kcb as read-only; also make it no-delay close so it can revert to the normal state after all handles are closed.
  3780. //
  3781. KeyControlBlock->ExtFlags |= (CM_KCB_READ_ONLY_KEY|CM_KCB_NO_DELAY_CLOSE);
  3782. //
  3783. // add an artificial refcount on this kcb. This will keep the kcb (and the read only flag set in memory for as long as the system is up)
  3784. //
  3785. InterlockedIncrement( (PLONG)&KeyControlBlock->RefCount );
  3786. CmpUnlockKCBTree();
  3787. return STATUS_SUCCESS;
  3788. }
  3789. BOOLEAN
  3790. CmpCompareNewValueDataAgainstKCBCache( PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  3791. PUNICODE_STRING ValueName,
  3792. ULONG Type,
  3793. PVOID Data,
  3794. ULONG DataSize
  3795. )
  3796. /*++
  3797. Routine Description:
  3798. Most of the SetValue calls are noops (i.e. they are setting the same
  3799. value name to the same value data). By comparing against the data already
  3800. in the kcb cache (i.e. faulted in) we can save page faults.
  3801. Arguments:
  3802. KeyControlBlock - pointer to kcb for the key to operate on
  3803. ValueName - The unique (relative to the containing key) name
  3804. of the value entry. May be NULL.
  3805. Type - The integer type number of the value entry.
  3806. Data - Pointer to buffer with actual data for the value entry.
  3807. DataSize - Size of Data buffer.
  3808. Return Value:
  3809. TRUE - same value with the same data exist in the cache.
  3810. --*/
  3811. {
  3812. PCM_KEY_VALUE Value;
  3813. ULONG Index;
  3814. BOOLEAN ValueCached;
  3815. PPCM_CACHED_VALUE ContainingList;
  3816. HCELL_INDEX ValueDataCellToRelease = HCELL_NIL;
  3817. BOOLEAN Result = FALSE;
  3818. PUCHAR datapointer;
  3819. BOOLEAN BufferAllocated = FALSE;
  3820. HCELL_INDEX CellToRelease = HCELL_NIL;
  3821. ULONG compareSize;
  3822. ULONG realsize;
  3823. BOOLEAN small;
  3824. PAGED_CODE();
  3825. BEGIN_KCB_LOCK_GUARD;
  3826. CmpLockKCBTreeExclusive();
  3827. if( KeyControlBlock->Flags & KEY_SYM_LINK ) {
  3828. //
  3829. // need to rebuild the value cache, so we could runt the same code
  3830. //
  3831. PCM_KEY_NODE Node = (PCM_KEY_NODE)HvGetCell(KeyControlBlock->KeyHive,KeyControlBlock->KeyCell);
  3832. if( Node == NULL ) {
  3833. //
  3834. // we couldn't map the bin containing this cell
  3835. //
  3836. goto Exit;
  3837. }
  3838. CmpCleanUpKcbValueCache(KeyControlBlock);
  3839. CmpSetUpKcbValueCache(KeyControlBlock,Node->ValueList.Count,Node->ValueList.List);
  3840. HvReleaseCell(KeyControlBlock->KeyHive,KeyControlBlock->KeyCell);
  3841. }
  3842. Value = CmpFindValueByNameFromCache(KeyControlBlock->KeyHive,
  3843. &(KeyControlBlock->ValueCache),
  3844. ValueName,
  3845. &ContainingList,
  3846. &Index,
  3847. &ValueCached,
  3848. &ValueDataCellToRelease
  3849. );
  3850. if(Value) {
  3851. if( (Type == Value->Type) && (DataSize == (Value->DataLength & ~CM_KEY_VALUE_SPECIAL_SIZE)) ) {
  3852. small = CmpIsHKeyValueSmall(realsize, Value->DataLength);
  3853. if (small == TRUE) {
  3854. datapointer = (PUCHAR)(&(Value->Data));
  3855. } else if( CmpGetValueDataFromCache(KeyControlBlock->KeyHive, ContainingList,(PCELL_DATA)Value,
  3856. ValueCached,&datapointer,&BufferAllocated,&CellToRelease) == FALSE ){
  3857. //
  3858. // we couldn't map view for cell; treat it as insufficient resources problem
  3859. //
  3860. ASSERT( datapointer == NULL );
  3861. ASSERT( BufferAllocated == FALSE );
  3862. goto Exit;
  3863. }
  3864. //
  3865. // compare data
  3866. //
  3867. if (DataSize > 0) {
  3868. try {
  3869. compareSize = (ULONG)RtlCompareMemory ((PVOID)datapointer,Data,(DataSize & ~CM_KEY_VALUE_SPECIAL_SIZE));
  3870. } except (EXCEPTION_EXECUTE_HANDLER) {
  3871. goto Exit;
  3872. }
  3873. } else {
  3874. compareSize = 0;
  3875. }
  3876. if (compareSize == DataSize) {
  3877. Result = TRUE;
  3878. }
  3879. }
  3880. }
  3881. Exit:
  3882. CmpUnlockKCBTree();
  3883. END_KCB_LOCK_GUARD;
  3884. if(ValueDataCellToRelease != HCELL_NIL) {
  3885. HvReleaseCell(KeyControlBlock->KeyHive,ValueDataCellToRelease);
  3886. }
  3887. if( BufferAllocated == TRUE ) {
  3888. ExFreePool(datapointer);
  3889. }
  3890. if(CellToRelease != HCELL_NIL) {
  3891. HvReleaseCell(KeyControlBlock->KeyHive,CellToRelease);
  3892. }
  3893. return Result;
  3894. }
  3895. NTSTATUS
  3896. static
  3897. __forceinline
  3898. CmpCheckReplaceHive( IN PHHIVE Hive,
  3899. OUT PHCELL_INDEX Key
  3900. )
  3901. {
  3902. HCELL_INDEX RootCell;
  3903. UNICODE_STRING Name;
  3904. NTSTATUS Status = STATUS_SUCCESS;
  3905. PRELEASE_CELL_ROUTINE TargetReleaseCellRoutine;
  3906. WCHAR Buffer[4];
  3907. PAGED_CODE();
  3908. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  3909. //
  3910. // disable refcounting
  3911. //
  3912. TargetReleaseCellRoutine = Hive->ReleaseCellRoutine;
  3913. Hive->ReleaseCellRoutine = NULL;
  3914. Buffer[3] = 0;
  3915. *Key = HCELL_NIL;
  3916. Buffer[1] = (WCHAR)'P';
  3917. RootCell = Hive->BaseBlock->RootCell;
  3918. Buffer[2] = (WCHAR)'A';
  3919. if( RootCell == HCELL_NIL ) {
  3920. //
  3921. // could not find root cell. Bogus.
  3922. //
  3923. Status = STATUS_REGISTRY_CORRUPT;
  3924. goto Exit;
  3925. }
  3926. Buffer[0] = (WCHAR)'W';
  3927. RtlInitUnicodeString(&Name, Buffer);
  3928. RootCell = CmpFindSubKeyByName(Hive,
  3929. (PCM_KEY_NODE)HvGetCell(Hive,RootCell),
  3930. &Name);
  3931. if( RootCell != HCELL_NIL ) {
  3932. //
  3933. // found it.
  3934. //
  3935. *Key = RootCell;
  3936. } else {
  3937. //
  3938. // WPA key should be present; it's created by GUI mode.
  3939. //
  3940. Status = STATUS_REGISTRY_CORRUPT;
  3941. goto Exit;
  3942. }
  3943. Exit:
  3944. Hive->ReleaseCellRoutine = TargetReleaseCellRoutine;
  3945. return Status;
  3946. }