Files
reactos/sdk
Hermès Bélusca-Maïto 396019984c [NTOS:CONFIG] Fix CmpRemoveFromHiveFileList() management (#9564)
Supplement to commit f09f448d95 (r57263), CORE-6491


## Problem description:

The support for unloading registry hives using the `NtUnloadKey2()` and
`CmUnloadKey()` functions has been introduced in commit 3507ef2ac6 (r68295),
CORE-3094.
It also brought the first usage of the `CmpRemoveFromHiveFileList()` routine,
previously implemented in commit f09f448d95 (r57263), in order to remove the
registry value associated to a registry hive, listed under the registry key
`HKLM\SYSTEM\CurrentControlSet\Control\hivelist`, added by a previous
`CmpAddToHiveFileList()` invocation when the hive was initially mounted.

The problem was that in `CmUnloadKey()`, the `CmpRemoveFromHiveFileList()`
routine was invoked *AFTER* a successful call to `CmpUnlinkFromMasterHive()`.
Because of this, the routine was looking at an alread invalidated `LinkData`
hive node. In doing so, the `LinkName` it deduced, and the hive path it
constructed to lookup into the `hivelist` registry key, could become invalid:
either from the name data being read, or, because the `KeyNode.Flags` value
became invalid, invalidating the `KEY_COMP_NAME` flag for example, thus
affecting the readability of the key name.

In ReactOS, the problem almost never showed up, except during its 1st-stage
installation, where the target `SOFTWARE` hive, mounted as:
`\Registry\Machine\USetup_Software`, remained listed in the `hivelist`
registry key, while the hive itself was already perfectly unloaded.
As it turned out, the `SOFTWARE` mount link key was already invalidated,
and the `USetup_Software` name was read incorrectly, and the code was
generating an invalid hive path string to be looked up.

Note that invoking `CmpRemoveFromHiveFileList()` *AFTER* a successful
`CmpUnlinkFromMasterHive()`, is an action similar to what is done in
Windows 2003 SP1 and later; however, this would have required reconstructing
the hive's path just before unlinking it. (On Windows 2003 RTM and before,
`CmpRemoveFromHiveFileList()` is invoked *BEFORE* `CmpUnlinkFromMasterHive()`
in symmetry to what is done in the hive linking process. This has been
changed on SP1+ so as to keep the hive linking information in the registry
up until the hive unlinking has been successfully done.)


## Problem resolution:

To resolve this issue, instead of reconstructing the hive path just before
unlinking the hive, as done in Windows 2003 SP1 and SP2 (and removing it from
the hive list later), we adopt an approach _similar_ to that of Vista+, by
caching the hive path string initially constructed by `CmpAddToHiveFileList()`
in the `CMHIVE` structure into a new `HiveRootPath` member.

The `CMHIVE::HiveRootPath` member is absent in Windows 2003,
https://www.vergiliusproject.com/kernels/x86/server-2003/sp2/_CMHIVE
and is introduced with Windows Vista RTM,
https://www.vergiliusproject.com/kernels/x86/windows-vista/rtm/_CMHIVE
2026-09-22 15:22:54 +02:00
..