

The history of passing a null pointer as the key name to RegOpenKeyEx
source link: https://devblogs.microsoft.com/oldnewthing/20210723-00/?p=105479
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The history of passing a null pointer as the key name to RegOpenKeyEx

Raymond
July 23rd, 2021
For decades, the documentation for the RegOpenKeyEx
function said
The lpSubKey parameter can be NULL only if hKey is one of the predefined keys.
This statement was true when it was written.
In 1992.
For Windows NT 3.5, the behavior of the RegOpenKeyEx
function was revised so that passing NULL as the lpSubKey is equivalent to passing an empty string.
Nobody updated the documentation to reflect this.
As a result, from 1994 to 2021, the documentation for RegOpenKeyEx
called out a special case that was no longer a special case.
Here’s what changed:
NULL
Refreshes hive, returns same key
Other key
NULL
ERROR_BADKEY
Returns unique handle to same key
Any key
non-NULL
Returns unique handle to subkey
The ERROR_BADKEY
case was removed. Instead, what you get is a unique handle to the same underlying key.
Note that the RegOpenKey
function behaves differently when you pass NULL
or an empty string as the subkey name:
NULL
or empty string
Refreshes hive, returns same key
Other key
NULL
or empty string
Returns same key
Any key
non-empty string
Returns unique handle to subkey
The fact that it returns the same key back when the subkey is NULL
or empty makes the function difficult to use because the handle might need to be closed, or might not, depending on whether the subkey is a non-empty string.
Some of the outcomes are labeled Refreshes hive. What does that mean?
If you pass a predefined key to RegCloseKey
or trigger one of the outcomes marked Refreshes hive, then the registry key associated with the root of the predefined key is closed, and when all of the outstanding subkeys from that hive are closed, the hive is unloaded. Meanwhile, any future references to the predefined key will go back and reload the hive.
Ironically, one of the things that counts as a reference to a predefined key is closing it! This means that if you call RegCloseKey
twice on a predefined key, the first time will close the hive reference from the root key, and then the second time will reload the hive, only to close it imediately.
Basically, you shouldn’t try to close a predefined key. It just creates a lot of work for no net effect.
One rare case where there is a net effect is where you are closing HKEY_CURRENT_USER
or HKEY_CLASSES_ROOT
from a service that impersonates. Recall that these keys are problematic when impersonating because they load the registry hive associated with the user being impersonated, making the hive available to all threads (not just the one doing the impersonation), and it remains available even after the impersonation reverts.
A totally hacky way to clear out the hive left over from impersonation is to close it explicitly, but now you’re using a global solution for a local problem. The predefined keys are applicable to the entire process, but you are trying to clean up your thread. If two threads are impersonating, they will step all over each other. You really should be using RegOpenCurrentUser
or using RegOpenUserClassesRoot
to access the registry hive that corresponds to the user being impersonated.
Recommend
-
216
Java: What to Know About Passing by Value Of course, in Java,...
-
68
-
11
Open sourcing fred in the name of freedom Over the past couple of days, I've asked for direction from my readers. I wanted to know what I should work on next. The response was clear: do f...
-
8
What are the Best Startup Name Changes in History? The tech industry has a rich history of startups that started with a pretty awkward name, and rebranded over time to the big brands we have come to know. Some of those changes are p...
-
6
Article io_uring [5.11] Exploring null pointer de-reference in io_uring_create Palash Oswal 17 Apr 2021 • 6 min read ...
-
13
How the K-line got its name: history of IRC daemon configurationMost people who use IRC know that to K-line someone means to ban them from an IRC server. Since the origins of this terminology are now rather obscure, I thought it migh...
-
13
Why do smart pointers null out the wrapped pointer before destroying it?
-
8
Rahul Ramteke Posted on Apr 21
-
9
Why am I getting a null pointer crash when trying to call a method on my C++/WinRT object?
-
3
XWorm Trojan 2.1 - Null Pointer Derefernce DoS ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK