Groups | Blog | Home
all groups > dotnet security > july 2006 >

dotnet security : Newbie Question - Thanks in Advance...


David White
7/19/2006 10:37:18 AM
I have written a C# 2.0 application which writes to (creates subkeys and data
values) in the registry under HLM\SOFTWARE. This application works fine when
logged in as admin. But it fails when logged into another (non-admin) account.
Of course, this is what I expect.

However, I am looking for a way, upon program start, for the system to look at
my program, see what permissions it requires, and terminate (with some warning)
if the current user context does not fulfill one or more security requirement.

It seems like this should be easy to do. But no matter what, the program runs
until a call is made to modify the registry and then throws a security
exception. I really don't want my program to run at all unless it will be able
to succeed in diddling the registry.

I have tried the following in the main executable's assemblyinfo file (note,
however, that the method which modifies the registry is in another assembly):

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
All = "HKEY_LOCAL_MACHINE\\SOFTWARE")]

The above is almost verbatim from the docs for RegistryKey.CreateSubKey().
Since this is an attribute of the main program's assembly, I'd think the
runtime should see this, check it against the current context, and see that the
non-admin user will NOT be granted permission. With this knowledge, I'd think
the runtime would not permit the executable to run.

But I guess that something could be done dynamically which changes things so it
will succeed. So maybe the runtime cannot do this automatically and I have to
do something myself.

David White
7/19/2006 10:42:31 AM
I should have also mentioned that I also tried annotating my program's main()
with the following:

[RegistryPermission(SecurityAction.Demand, Unrestricted = true)]

The program doesn't get stopped here either. It runs until the attempt is made
to actually modify the registry and then fails if the user is non-admin. Thanks.

[quoted text, click to view]
Joe Kaplan (MVP - ADSI)
7/19/2006 1:59:12 PM
This looks like a confusion between Windows security and code access
security. You are being denied access to the registry based on the user the
logged in, not based on the identity of the code that is trying to do the
registry operation. The permissions that you are trying to use are for code
access security. In your case, your code likely has full trust since it is
run locally, so those demands would actually work.

The is compounded by a design "variance" in MS's implementation of the
registry classes, where they throw a SecurityException when they should
throw the standard UnauthorizedAccessException. This makes the problem look
like CAS, when it is Windows related.

To actually know whether Windows will allow the requested registry mods, you
need to call the Windows AccessCheck function via p/invoke. There isn't an
easy way to do this in .NET besides simply catching the exception that is
thrown when you try it.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
[quoted text, click to view]

David White
7/19/2006 3:04:28 PM
Thanks for all the responses. I would guess that an administrator could give
other users the ability to changes this part of the registry (not sure how,
maybe the local policy editor). So rather than test for the user being in the
admin role, I will follow Joe's advice. Cheers!

[quoted text, click to view]
Joe Kaplan (MVP - ADSI)
7/19/2006 7:51:21 PM
My approach is probably more technically correct, but much more painful to
implement. The administrator check is a good bet if you don't want to do
the heavy lifting and don't have reason to expect the ACLs on those objects
would be different from their defaults.

Up to you...

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
[quoted text, click to view]
Dominick Baier
7/19/2006 8:35:18 PM
as Joe said, you ca try to access the resource and catch the exception...

or you can check if the current user is an administrator with this code:

if (new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrators))
{}



[quoted text, click to view]

Dominick Baier
7/19/2006 10:12:16 PM
Both the local security policy and regedit allow modifying ACLs on registry
keys.

dominick

[quoted text, click to view]

AddThis Social Bookmark Button