I have a C# application the performs some runtime compilation of source files containing calculations into dynamic assemblies. Obviously this presents a serious security issue.
From the following 'formula', the code below would be generated, and a dynamic assembly created:
Formula:
Int32 _index = value.LastIndexOf('.');
String _retVal = value.Substring(_index + 1);
return _retVal;
Code Generated:
using System;
namespace Dynamics
{
public class Evaluator
{
public Object Evaluate(String value)
{
// Begin external code
Int32 _index = value.LastIndexOf('.');
String _retVal = value.Substring(_index + 1);
return _retVal;
// End external code
}
}
}
The dynamic assembly is then loaded and the Evaluate method executed via Reflection. This works Great.
The problem is that the potential for malicious code injection is huge, so I want to run the Evaluate method in a 'Sandboxed' thread (without any unmanaged API calls). For testing purposes I'm using the built in Anonymous Windows user, and have come up with the following code:
Thread tSandbox = new Thread(
new ParameterizedThreadStart(this.DoSandboxedEvaluation));
WindowsIdentity tIdentity = WindowsIdentity.GetAnonymous();
WindowsPrincipal tPrincipal = new WindowsPrincipal(i);
This gives me the Anonymous users' Identity and Principal. How can this be applied to thread tSandbox so the code on that thread runs in the specified Security Context, without using Unmanaged API calls?
Thanks!