I’ve set up a little example where I loaded an assembly into a new AppDomain without any Permission. This works fine, the assembly can’t access the file system and can’t listen to sockets.
But there is another thing i want to prevent: Thread creation. Why? Cause theoreticly this assembly can create a thread which creates even more threads and flood my memory.
I thought of the (in my opinion) best way: Limiting the memory of an AppDomain. Is this possible? And if not, what can i do to avoid thread creation?
Used this code to create the thread
Thread t = new Thread(this.DoWork);
t.Start();
And this code for the AppDomain
PermissionSet set = new PermissionSet(PermissionState.None);
set.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
set.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read |
FileIOPermissionAccess.PathDiscovery,
this.path));
AppDomainSetup info = new AppDomainSetup { ApplicationBase = this.path };
this.domain = AppDomain.CreateDomain("Sandbox", null, info, set, null);
(Ok, i gave access to the file system in the folder where i want to load the assembly, this is just because StrongName fullTrustAssembly = typeof(SecureInstance).Assembly.Evidence.GetHostEvidence<StrongName>(); don’t work for me either.
Hope s/o can help. (:
Seems like there’s no easy answer for this. What you can do is use the .NET Profiling API to try and monitor memory usage in your AppDomain. You can find out more about it here, but you’ll need to do some digging: http://msdn.microsoft.com/en-us/library/bb384493.aspx
Anyway, isn’t it better to run whatever you want to run in a separate process with a lower priority, so if it goes all wild with memory allocations, it doesn’t affect your process, the OS kills it and it doesn’t affect your main process?