Weblog
Identity sneak preview
My security bible for .NET has always been .NET Framework Security. It's not a new book but quite frankly this is the book I still respect the most on .NET Security. However there is a big omission from the book, as it almost doesn't cover IIdentity and IPrincipal interfaces and related classes. While the basic for Identity/Principal are simple some aspects are more complex (e.g. WindowsIdentity, WindowsImpersonationContext ...) and can even be misleading. Thanksfully there is now an alternative and anyone interested in Identity and Principal should have a good look at the upcoming book by Keith Brown,
A .NET Developer's Guide to Windows Security. A draft is available to read online.
WindowsIdentity implements the IIdentity interface (the simple part) but also adds some cool stuff like the ability to impersonate another user. This is really cool but, unless you have Windows 2003, it requires you to P/Invoke to get a user token - see Windows sample on MSDN.
Windows and UNIX have many differences in handling identities. Thanksfully the framework has a good abstraction for identities and principals, which should hide most differences for day to day usages. So the big questions becomes how do we get the cool stuff to work under Linux ?
Actually we can get a very nice mapping with WindowsIdentity even if there are some fundamental differences between the operating systems (so don't expect perfect portability here). Here's an example of what we can do...
First the sample will show you the current OS identity running the assembly. I say OS identity because this may be different than Thread.CurrentPrincipal.Identity something important to keep in mind when designing .NET applications dealing with identities.
The second part checks if you're running on a Unix system and, if so, if you are the super-user (root, which always has uid 0). The root user is important for this sample because it has the privilege to impersonate any user on the system. As the root we create a file (root.txt) and display it's content.
We now try to impersonate another user on the system. Note that root doesn't need the user password to impersonate the user - so, unlike Windows, no P/Invoke will be required here. As the new user we will try to display the file content, then try to delete the file.
Finally we will revert to the original process identity and, if the root.txt file still exists, display the file before deleting it.
Executing the sample
WARNING: Don't try this (yet) at home - that's what a sneak preview is for. While mcs, or even csc, can compile the sample code, neither Mono nor the MS runtime can execute it correctly right now (well MS will probably never run this under Linux). I'll update this entry when my patches are committed into CVS.
Ok, maybe the sample didn't work as you expected, like:
- How could the user read the root file ?
- Because by default the files are created as readable for everyone -
umask - How could the user delete the root file ?
- Just checking the file attributes isn't enough. If the user has write privileges in the directory he can delete the file (e.g. if the user is the directory's owner). Just be sure to run the sample if a directory where the impersonated user can't write to it (i.e. like
/home/). - I got exceptions running the code ?
- Some are normal. Remember that this will only works on POSIX compliant systems.
Ok, while this sample isn't portable (i.e. it won't work on Windows) it does have a big advantage over Windows - we didn't have to P/Invoke our way into any OS calls. Which means that the WindowsIdentity class work even better on Linux than on Windows :-).
4/5/2004 11:33:18 | Comments
The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.
