views:

463

answers:

3

I am currently developing an application in where a user will dynamically choose dlls and the application will execute some of the methods in that dll. (if you follow the first link, you will see that I am developing a sort of Robocode game application using the .Net Framework).

For example, when the battle starts, the run method from the dll is executed.

Since whatever was specified in the run method will be executed, there are quite a bit of Security Constraints that have to be applied.

Like for example, if the user who programmed the dll, instead of using only the methods that are applicable from the interface (methods that the robot uses to walk and fire etc...), invokes methods that will retrieve files and maybe even delete files from the hard disk...and when another user loads that dll into his computer, those methods will be invoked on his pc and his files will be modified by this malicious code.

Thus, I need to somehow make this application run from a sort of Sandboxed Environment so that whatever methods are invoked, it will not affect the hard disk of the computer that the dll is opened on.

Any ideas on how I should start in doing this ?

Here is an example on how I am loading these dlls and invoking their methods:

for (int i = 0; i < robotList.Count; i++)
{
    IRunnable o = robotList[i];
    new Thread(delegate()
    {
        o.run();    
    }).Start();
}
+2  A: 

What you're looking to do is basically running Assemblies in a separate Application Domain. Check out this page on MSDN for a good starting point. It's actually quite east to do:

http://msdn.microsoft.com/en-us/library/ms173139(VS.80).aspx

BFree
Gonna check it out; cheers
Andreas Grech
+5  A: 

Normally, you might just live with

AppDomain newDomain = AppDomain.CreateDomain(name);
Assembly asm = newDomain.Load(System.IO.File.ReadAllBytes(name));

But one interesting point is, the AppDomain.Load method will load the assembly to the new app domain, as well as to the current domain.

A more elegant solution is to use System.AddIn namespace in 3.5. - http://msdn.microsoft.com/en-us/magazine/cc163476.aspx

Then, you can actually specify the trust level for your addin, using the AddinSecurityLevel like

//Activate the selected AddInToken in a new
//application domain with the Internet trust level.
Calculator CalcAddIn = selectedToken.Activate<Calculator>(AddInSecurityLevel.Internet);

See http://msdn.microsoft.com/en-us/library/bb355219.aspx for details.

amazedsaint
A: 

It's pretty complicated. See this and my implementation

Pavel Savara