AppDomain.DoCallBack requires ReflectionPermission?

677 views Asked by At

I have this class, instance of which I create in an AppDomain with no permissions but SecurityPermissionFlag.Execute:

class IsolationEntryPoint : MarshalByRefObject
{
    // main is the original AppDomain with all the permissions
    public void Enter(AppDomain main)
    {
        // these work correctly
        Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("Host: " + main.FriendlyName);

        // the exception is thrown here
        main.DoCallBack(this.MyCallBack);
    }

    public void MyCallBack()
    {
        Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
    }
}

The odd thing is that I get SecurityException in the DoCallback line saying:

Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

MSDNsays this about permission requirements of AppDomain.DoCallBack:

ReflectionPermission when invoked late-bound through mechanisms such as Type.InvokeMember.

The call is not using anything like Type.InvokeMember, why am I getting the exception?

EDIT:

For clarity, here is the code I use to create the AppDomain with the isolation object:

    [STAThread]
    static void Main(string[] args)
    {

        var setup = new AppDomainSetup();
        setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

        var evidence = new Evidence();

        var permissions = new PermissionSet(PermissionState.None);
        permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

        var domain = AppDomain.CreateDomain(
            "isolationDomain",
            evidence,
            setup,
            permissions);

        var handle = Activator.CreateInstanceFrom(
            domain, typeof(IsolationEntryPoint).Assembly.ManifestModule.FullyQualifiedName,
            typeof(IsolationEntryPoint).FullName);

        var instance = (IsolationEntryPoint)handle.Unwrap();

        instance.Enter(AppDomain.CurrentDomain);
    }

These two pieces code are my full application, there is nothing else (so the exception should be easy to reproduce).

Thanks for your help

2

There are 2 answers

2
Steffen Opel On BEST ANSWER

The solution is actually quite simple: You missed to add the public access modifier to class IsolationEntryPoint, i.e after changing the class signature like so your sample runs just fine:

public class IsolationEntryPoint : MarshalByRefObject
{
    // [...]
}
3
Soundararajan On

I tried the below and it seems to work.

class Program
{

    static void Main(string[] args)
    {
        SecurityPermission t = new SecurityPermission(SecurityPermissionFlag.Execution);
        t.Demand();
        IsolationEntryPoint x = new IsolationEntryPoint();
        x.Enter(AppDomain.CurrentDomain);
    }
}


class IsolationEntryPoint : MarshalByRefObject
{
    // main is the original AppDomain with all the permissions 
    public void Enter(AppDomain main)
    {
        // these work correctly 
        Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("Host: " + main.FriendlyName);

        // the exception is thrown here 
        main.DoCallBack(this.MyCallBack);
    }

    public void MyCallBack()
    {
        Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
    }
}