Windows code security questions

  1. What’s the difference between code-based security and role-based security? Which one is better? Code security is the approach of using permissions and permission sets for a given code to run. The admin, for example, can disable running executables off the Internet or restrict access to corporate database to only few applications. Role-based security most of the time involves the code running with the privileges of the current user. This way the code cannot supposedly do more harm than mess up a single user account. There’s no better, or 100% thumbs-up approach, depending on the nature of deployment, both code-based and role-based security could be implemented to an extent.
  2. How can you work with permissions from your .NET application? You can request permission to do something and you can demand certain permissions from other apps. You can also refuse permissions so that your app is not inadvertently used to destroy some data.
  3. How can C# app request minimum permissions?

    using System.Security.Permissions;
    [assembly:FileDialogPermissionAttribute(SecurityAction.RequestMinimum, Unrestricted=true)]

  4. What’s a code group? A code group is a set of assemblies that share a security context.
  5. What’s the difference between authentication and authorization? Authentication happens first. You verify user’s identity based on credentials. Authorization is making sure the user only gets access to the resources he has credentials for.
  6. What are the authentication modes in ASP.NET? None, Windows, Forms and Passport.
  7. Are the actual permissions for the application defined at run-time or compile-time? The CLR computes actual permissions at runtime based on code group membership and the calling chain of the code.
This entry was posted in .NET, Windows. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments on Windows code security questions

  1. Kathy
    Posted 5/23/2004 at 8:33 pm | Permalink

    Can I write IL programs directly?
    Yes. Peter Drayton posted this simple example to the DOTNET mailing list:

    .assembly MyAssembly {}
    .class MyApp {
    .method static void Main() {
    .entrypoint
    ldstr “Hello, IL!”
    call void System.Console::WriteLine(class System.Object)
    ret
    }
    }

    Just put this into a file called hello.il, and then run ilasm hello.il. An exe assembly will be generated.

    9.5 Can I do things in IL that I can’t do in C#?
    Yes. A couple of simple examples are that you can throw exceptions that are not derived from System.Exception, and you can have non-zero-based arrays.

    10. Implications for COM
    10.1 Is COM dead?
    This subject causes a lot of controversy, as you’ll see if you read the mailing list archives. Take a look at the following two threads:

    http://discuss.develop.com/archives/wa.exe?A2=ind0007&L=DOTNET&D=0&P=68241
    http://discuss.develop.com/archives/wa.exe?A2=ind0007&L=DOTNET&P=R60761

    FWIW my view is as follows: COM is many things, and it’s different things to different people. But to me, COM is fundamentally about how little blobs of code find other little blobs of code, and how they communicate with each other when they find each other. COM specifies precisely how this location and communication takes place. In a ‘pure’ .NET world, consisting entirely of .NET objects, little blobs of code still find each other and talk to each other, but they don’t use COM to do so. They use a model which is similar to COM in some ways - for example, type information is stored in a tabular form packaged with the component, which is quite similar to packaging a type library with a COM component. But it’s not COM.

    So, does this matter? Well, I don’t really care about most of the COM stuff going away - I don’t care that finding components doesn’t involve a trip to the registry, or that I don’t use IDL to define my interfaces. But there is one thing that I wouldn’t like to go away - I wouldn’t like to lose the idea of interface-based development. COM’s greatest strength, in my opinion, is its insistence on a cast-iron separation between interface and implementation. Unfortunately, the .NET framework seems to make no such insistence - it lets you do interface-based development, but it doesn’t insist. Some people would argue that having a choice can never be a bad thing, and maybe they’re right, but I can’t help feeling that maybe it’s a backward step.

    10.2 Is DCOM dead?
    Pretty much, for .NET developers. The .NET Framework has a new remoting model which is not based on DCOM. Of course DCOM will still be used in interop scenarios.

    10.3 Is MTS/COM+ dead?
    No. The approach for the first .NET release is to provide access to the existing COM+ services (through an interop layer) rather than replace the services with native .NET ones. Various tools and attributes are provided to try to make this as painless as possible. The PDC release of the .NET SDK includes interop support for core services (JIT activation, transactions) but not some of the higher level services (e.g. COM+ Events, Queued components).

    Over time it is expected that interop will become more seamless - this may mean that some services become a core part of the CLR, and/or it may mean that some services will be rewritten as managed code which runs on top of the CLR.

    For more on this topic, search for postings by Joe Long in the archives - Joe is the MS group manager for COM+. Start with this message:

    http://discuss.develop.com/archives/wa.exe?A2=ind0007&L=DOTNET&P=R68370

    10.4 Can I use COM components from .NET programs?
    Yes. COM components are accessed from the .NET runtime via a Runtime Callable Wrapper (RCW). This wrapper turns the COM interfaces exposed by the COM component into .NET-compatible interfaces. For oleautomation interfaces, the RCW can be generated automatically from a type library. For non-oleautomation interfaces, it may be necessary to develop a custom RCW which manually maps the types exposed by the COM interface to .NET-compatible types.

    Here’s a simple example for those familiar with ATL. First, create an ATL component which implements the following IDL:

    import “oaidl.idl”;
    import “ocidl.idl”;

    [
    object,
    uuid(EA013F93-487A-4403-86EC-FD9FEE5E6206),
    helpstring("ICppName Interface"),
    pointer_default(unique),
    oleautomation
    ]

    interface ICppName : IUnknown
    {
    [helpstring("method SetName")] HRESULT SetName([in] BSTR name);
    [helpstring("method GetName")] HRESULT GetName([out,retval] BSTR *pName );
    };

    [
    uuid(F5E4C61D-D93A-4295-A4B4-2453D4A4484D),
    version(1.0),
    helpstring("cppcomserver 1.0 Type Library")
    ]
    library CPPCOMSERVERLib
    {
    importlib(”stdole32.tlb”);
    importlib(”stdole2.tlb”);
    [
    uuid(600CE6D9-5ED7-4B4D-BB49-E8D5D5096F70),
    helpstring("CppName Class")
    ]
    coclass CppName
    {
    [default] interface ICppName;
    };
    };

    When you’ve built the component, you should get a typelibrary. Run the TLBIMP utility on the typelibary, like this:

    tlbimp cppcomserver.tlb

    If successful, you will get a message like this:

    Typelib imported successfully to CPPCOMSERVERLib.dll

    You now need a .NET client - let’s use C#. Create a .cs file containing the following code:

    using System;
    using CPPCOMSERVERLib;

    public class MainApp
    {
    static public void Main()
    {
    CppName cppname = new CppName();
    cppname.SetName( “bob” );
    Console.WriteLine( “Name is ” + cppname.GetName() );
    }
    }

    Note that we are using the type library name as a namespace, and the COM class name as the class. Alternatively we could have used CPPCOMSERVERLib.CppName for the class name and gone without the using CPPCOMSERVERLib statement.

    Compile the C# code like this:

    csc /r:cppcomserverlib.dll csharpcomclient.cs

    Note that the compiler is being told to reference the DLL we previously generated from the typelibrary using TLBIMP.

    You should now be able to run csharpcomclient.exe, and get the following output on the console:

    Name is bob

  2. Posted 5/23/2004 at 8:34 pm | Permalink

    Can I write IL programs directly?
    Yes. Peter Drayton posted this simple example to the DOTNET mailing list:

    .assembly MyAssembly {}
    .class MyApp {
    .method static void Main() {
    .entrypoint
    ldstr “Hello, IL!”
    call void System.Console::WriteLine(class System.Object)
    ret
    }
    }

    Just put this into a file called hello.il, and then run ilasm hello.il. An exe assembly will be generated.

    9.5 Can I do things in IL that I can’t do in C#?
    Yes. A couple of simple examples are that you can throw exceptions that are not derived from System.Exception, and you can have non-zero-based arrays.

    10. Implications for COM
    10.1 Is COM dead?
    This subject causes a lot of controversy, as you’ll see if you read the mailing list archives. Take a look at the following two threads:

    http://discuss.develop.com/archives/wa.exe?A2=ind0007&L=DOTNET&D=0&P=68241
    http://discuss.develop.com/archives/wa.exe?A2=ind0007&L=DOTNET&P=R60761

    FWIW my view is as follows: COM is many things, and it’s different things to different people. But to me, COM is fundamentally about how little blobs of code find other little blobs of code, and how they communicate with each other when they find each other. COM specifies precisely how this location and communication takes place. In a ‘pure’ .NET world, consisting entirely of .NET objects, little blobs of code still find each other and talk to each other, but they don’t use COM to do so. They use a model which is similar to COM in some ways - for example, type information is stored in a tabular form packaged with the component, which is quite similar to packaging a type library with a COM component. But it’s not COM.

    So, does this matter? Well, I don’t really care about most of the COM stuff going away - I don’t care that finding components doesn’t involve a trip to the registry, or that I don’t use IDL to define my interfaces. But there is one thing that I wouldn’t like to go away - I wouldn’t like to lose the idea of interface-based development. COM’s greatest strength, in my opinion, is its insistence on a cast-iron separation between interface and implementation. Unfortunately, the .NET framework seems to make no such insistence - it lets you do interface-based development, but it doesn’t insist. Some people would argue that having a choice can never be a bad thing, and maybe they’re right, but I can’t help feeling that maybe it’s a backward step.

    10.2 Is DCOM dead?
    Pretty much, for .NET developers. The .NET Framework has a new remoting model which is not based on DCOM. Of course DCOM will still be used in interop scenarios.

    10.3 Is MTS/COM+ dead?
    No. The approach for the first .NET release is to provide access to the existing COM+ services (through an interop layer) rather than replace the services with native .NET ones. Various tools and attributes are provided to try to make this as painless as possible. The PDC release of the .NET SDK includes interop support for core services (JIT activation, transactions) but not some of the higher level services (e.g. COM+ Events, Queued components).

    Over time it is expected that interop will become more seamless - this may mean that some services become a core part of the CLR, and/or it may mean that some services will be rewritten as managed code which runs on top of the CLR.

    For more on this topic, search for postings by Joe Long in the archives - Joe is the MS group manager for COM+. Start with this message:

    http://discuss.develop.com/archives/wa.exe?A2=ind0007&L=DOTNET&P=R68370

    10.4 Can I use COM components from .NET programs?
    Yes. COM components are accessed from the .NET runtime via a Runtime Callable Wrapper (RCW). This wrapper turns the COM interfaces exposed by the COM component into .NET-compatible interfaces. For oleautomation interfaces, the RCW can be generated automatically from a type library. For non-oleautomation interfaces, it may be necessary to develop a custom RCW which manually maps the types exposed by the COM interface to .NET-compatible types.

    Here’s a simple example for those familiar with ATL. First, create an ATL component which implements the following IDL:

    import “oaidl.idl”;
    import “ocidl.idl”;

    [
    object,
    uuid(EA013F93-487A-4403-86EC-FD9FEE5E6206),
    helpstring("ICppName Interface"),
    pointer_default(unique),
    oleautomation
    ]

    interface ICppName : IUnknown
    {
    [helpstring("method SetName")] HRESULT SetName([in] BSTR name);
    [helpstring("method GetName")] HRESULT GetName([out,retval] BSTR *pName );
    };

    [
    uuid(F5E4C61D-D93A-4295-A4B4-2453D4A4484D),
    version(1.0),
    helpstring("cppcomserver 1.0 Type Library")
    ]
    library CPPCOMSERVERLib
    {
    importlib(”stdole32.tlb”);
    importlib(”stdole2.tlb”);
    [
    uuid(600CE6D9-5ED7-4B4D-BB49-E8D5D5096F70),
    helpstring("CppName Class")
    ]
    coclass CppName
    {
    [default] interface ICppName;
    };
    };

    When you’ve built the component, you should get a typelibrary. Run the TLBIMP utility on the typelibary, like this:

    tlbimp cppcomserver.tlb

    If successful, you will get a message like this:

    Typelib imported successfully to CPPCOMSERVERLib.dll

    You now need a .NET client - let’s use C#. Create a .cs file containing the following code:

    using System;
    using CPPCOMSERVERLib;

    public class MainApp
    {
    static public void Main()
    {
    CppName cppname = new CppName();
    cppname.SetName( “bob” );
    Console.WriteLine( “Name is ” + cppname.GetName() );
    }
    }

    Note that we are using the type library name as a namespace, and the COM class name as the class. Alternatively we could have used CPPCOMSERVERLib.CppName for the class name and gone without the using CPPCOMSERVERLib statement.

    Compile the C# code like this:

    csc /r:cppcomserverlib.dll csharpcomclient.cs

    Note that the compiler is being told to reference the DLL we previously generated from the typelibrary using TLBIMP.

    You should now be able to run csharpcomclient.exe, and get the following output on the console:

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*