Skip to content

Commit

Permalink
Merge pull request #623 from stevencohn/622-fix-registry-access-in-setup
Browse files Browse the repository at this point in the history
detect CLSID and properly delete trusted protocol
  • Loading branch information
stevencohn authored Sep 22, 2022
2 parents 9fb4ef7 + 7b18cae commit 1b0eb2a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 19 deletions.
2 changes: 1 addition & 1 deletion OneMoreSetupActions/Actions/ActiveSetupAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override int Install()

using (var key = Registry.LocalMachine.OpenSubKey(
$@"Software\Microsoft\Active Setup\Installed Components\{RegistryHelper.OneNoteID}",
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.Rights))
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.WriteRights))
{
var version = (string)key.GetValue("Version");
if (!string.IsNullOrEmpty(version))
Expand Down
4 changes: 2 additions & 2 deletions OneMoreSetupActions/Actions/ProtocolHandlerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override int Install()
var hive = Registry.LocalMachine;

var parent = hive.OpenSubKey(path,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.Rights);
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.WriteRights);

if (parent == null)
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public override int Install()
logger.WriteLine($@"step {stepper.Step()}: opening HKLM:\{path}\{cmdpath}");

var key = parent.OpenSubKey(cmdpath,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.Rights);
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.WriteRights);

if (key == null)
{
Expand Down
70 changes: 58 additions & 12 deletions OneMoreSetupActions/Actions/RegistryWowAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public override int Install()
return SUCCESS;
}
}
else
{
logger.WriteLine("WOW cloning not required");
}

return SUCCESS;
}
Expand All @@ -56,14 +60,16 @@ public override int Install()
*/
private int RegisterWow()
{
logger.WriteLine("cloning CLSID");
using (var source = Registry.ClassesRoot.OpenSubKey($@"CLSID\{RegistryHelper.OneNoteID}"))
logger.WriteLine($"step {stepper.Step()}: cloning CLSID");
using (var source = Registry.ClassesRoot.OpenSubKey(
$@"CLSID\{RegistryHelper.OneNoteID}",
RegistryKeyPermissionCheck.ReadSubTree, RegistryHelper.ReadRights))
{
if (source != null)
{
using (var target = Registry.ClassesRoot.OpenSubKey(@"WOW6432Node\CLSID", true))
{
logger.WriteLine($"copying from {source.Name} to {target.Name}");
logger.WriteLine($"step {stepper.Step()}: copying from {source.Name} to {target.Name}");
source.CopyTo(target);
}
}
Expand All @@ -85,13 +91,14 @@ public override int Uninstall()
return UnregisterWow();
}

logger.WriteLine("WOW cloning not required, no cleanup necessary");
return SUCCESS;
}


private int UnregisterWow()
{
logger.WriteLine("deleting CLSID clone");
logger.WriteLine($"step {stepper.Step()}: deleting CLSID clone");
using (var key = Registry.ClassesRoot.OpenSubKey(@"WOW6432Node\CLSID", true))
{
if (key != null)
Expand All @@ -112,38 +119,77 @@ private int UnregisterWow()

// Determines if 32-bit OneNote is installed.
// When this is true, the guid will exist under ClassesRoot\CLSID however the path
// will be blank and instead the the path is under ClassesRoot\Wow6432Node\CLSID\{guid}
// will be blank and instead the the path is under ClassesRoot\WOW6432Node\CLSID\{guid}
private bool CloningRequired()
{
string clsid = null;
using (var key = Registry.ClassesRoot.OpenSubKey(@"\OneNote.Application\CLSID"))

/*
* This makes no sense at all but if we don't attempt the 1st chance code then
* the 2nd chance code below doesn't work...
*/

using (var basekey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32))
{
if (key != null)
using (var key = basekey.OpenSubKey(@"OneNote.Application\CLSID"))
{
if (key != null)
{
clsid = key.GetValue(string.Empty) as string; // default value
logger.WriteLine($"read CLSID ({clsid})");
}
else
{
logger.WriteLine($"1st chance, could not read CLSID or key missing");
}
}
}

if (clsid == null)
{
using (var key = Registry.ClassesRoot.OpenSubKey(@"OneNote.Application\CLSID"))
{
clsid = (string)key.GetValue(string.Empty); // default value
if (key != null)
{
clsid = (string)key.GetValue(string.Empty); // default value
logger.WriteLine($"2nd chance, read CLSID ({clsid})");
}
else
{
logger.WriteLine($"2nd chance, could not read CLSID or key missing");
}
}
}

string path = null;
if (!string.IsNullOrEmpty(clsid))
{
using (var key = Registry.ClassesRoot
.OpenSubKey($@"CLSID\{clsid}\Localserver32"))
using (var key = Registry.ClassesRoot.OpenSubKey($@"CLSID\{clsid}\LocalServer32"))
{
if (key != null)
{
path = (string)key.GetValue(string.Empty); // default value
logger.WriteLine($"read path from LocalServer32 ({path})");
}
else
{
logger.WriteLine($"could not read LocalServer32 or key missing");
}
}

if (path == null)
{
using (var key = Registry.ClassesRoot
.OpenSubKey($@"WOW6432Node\CLSID\{clsid}\Localserver32"))
using (var key = Registry.ClassesRoot.OpenSubKey($@"WOW6432Node\CLSID\{clsid}\LocalServer32"))
{
if (key != null)
{
path = (string)key.GetValue(string.Empty); // default value
logger.WriteLine($@"read path from WOW6432Node\..\LocalServer32 ({path})");
}
else
{
logger.WriteLine($@"could not WOW6432Node\..\LocalServer32 or key missing");
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions OneMoreSetupActions/Actions/TrustedProtocolAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public override int Install()
try
{
using (var polKey = hive.OpenSubKey(policiesPath,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.Rights))
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryHelper.WriteRights))
{
key = polKey.CreateSubKey(policyPath, false);
if (key == null)
Expand Down Expand Up @@ -138,7 +138,9 @@ public override int Uninstall()
logger.WriteLine("TrustedProtocolAction.Uninstall ---");

var sid = RegistryHelper.GetUserSid("unregistering trusted protocol");
using (var hive = Registry.Users.OpenSubKey(sid))
using (var hive = Registry.Users.OpenSubKey(sid,
RegistryKeyPermissionCheck.ReadWriteSubTree,
RegistryHelper.DeleteRights))
{
var version = GetVersion("Excel", 16);
var path = $@"Software\Policies\Microsoft\Office\{version}\Common\Security\" +
Expand Down
11 changes: 10 additions & 1 deletion OneMoreSetupActions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ static void Main(string[] args)
stepper = new Stepper();

logger.WriteLine();
logger.WriteLine(new string('-', 70));
if (args[0] == "--install" || args[0] == "--uninstall")
{
logger.WriteLine(new string('=', 70));
logger.WriteLine($"starting action: {args[0]}");
}
else
{
logger.WriteLine(new string('-', 50));
logger.WriteLine($"reentry action: {args[0]}");
}

if (args.Any(a => a == "--x64" || a == "--x86"))
{
Expand Down
13 changes: 12 additions & 1 deletion OneMoreSetupActions/RegistryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ internal static class RegistryHelper
{
public const string OneNoteID = "{88AB88AB-CDFB-4C68-9C3A-F10B75A5BC61}";

public static readonly RegistryRights Rights =
public static readonly RegistryRights DeleteRights =
RegistryRights.EnumerateSubKeys |
RegistryRights.Delete |
RegistryRights.QueryValues |
RegistryRights.ReadKey;

public static readonly RegistryRights ReadRights =
RegistryRights.EnumerateSubKeys |
RegistryRights.QueryValues |
RegistryRights.ReadKey;

public static readonly RegistryRights WriteRights =
RegistryRights.CreateSubKey |
RegistryRights.EnumerateSubKeys |
RegistryRights.QueryValues |
Expand Down

0 comments on commit 1b0eb2a

Please sign in to comment.