- Timestamp:
- 05/10/11 14:28:48 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Sandboxing/SandboxManager.cs
r5523 r6174 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Diagnostics;25 using System.Linq;26 23 using System.Reflection; 27 24 using System.Security; 28 25 using System.Security.Permissions; 29 using System.Security.Policy;30 26 using HeuristicLab.PluginInfrastructure.Manager; 31 27 32 28 namespace HeuristicLab.PluginInfrastructure.Sandboxing { 33 public class SandboxManager {29 public static class SandboxManager { 34 30 35 // static class 36 private SandboxManager() { } 31 /// <summary> 32 /// Creates an privileged sandbox, meaning that the executed code is fully trusted and permissions are not restricted. 33 /// This method is a fall back for trusted users in HeuristicLab Hive. 34 /// </summary> 35 public static AppDomain CreateAndInitPrivilegedSandbox(string appDomainName, string applicationBase, string configFilePath) { 36 PermissionSet pSet; 37 pSet = new PermissionSet(PermissionState.Unrestricted); 37 38 38 private static StrongName CreateStrongName(Assembly assembly) { 39 if (assembly == null) 40 throw new ArgumentNullException("assembly"); 41 42 AssemblyName assemblyName = assembly.GetName(); 43 Trace.Assert(assemblyName != null, "Could not get assembly name"); 44 45 // get the public key blob 46 byte[] publicKey = assemblyName.GetPublicKey(); 47 if (publicKey == null || publicKey.Length == 0) 48 throw new InvalidOperationException("Assembly is not strongly named"); 49 50 StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey); 51 52 // and create the StrongName 53 return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version); 54 } 55 56 #region ISandboxManager Members 57 public static AppDomain CreateAndInitSandbox(string appDomainName, string applicationBase, string configFilePath) { 58 PermissionSet pset; 59 60 #region permission set for sandbox 61 // Uncomment code for sandboxed appdomain 62 //pset = new PermissionSet(PermissionState.None); 63 //pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); 64 //pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)); 65 //FileIOPermission fPerm = new FileIOPermission(PermissionState.None); 66 67 //foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) { 68 // fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, plugin.Files.ToArray()); 69 //} 70 71 //pset.AddPermission(fPerm); 72 #endregion 73 74 #region permission set of unrestricted appdomain 75 // unrestricted appdomain 76 pset = new PermissionSet(PermissionState.Unrestricted); 77 #endregion 78 79 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation; 39 AppDomainSetup setup = new AppDomainSetup(); 80 40 setup.PrivateBinPath = applicationBase; 81 41 setup.ApplicationBase = applicationBase; 82 42 setup.ConfigurationFile = configFilePath; 83 43 84 AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));85 44 Type applicationManagerType = typeof(DefaultApplicationManager); 86 DefaultApplicationManager applicationManager = 87 (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null); 45 AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, null, setup, pSet, null); 46 DefaultApplicationManager applicationManager = (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null); 47 88 48 PluginManager pm = new PluginManager(applicationBase); 89 49 pm.DiscoverAndCheckPlugins(); 90 ApplicationDescription[] apps = pm.Applications.Cast<ApplicationDescription>().ToArray(); 91 PluginDescription[] plugins = pm.Plugins.Cast<PluginDescription>().ToArray(); 92 applicationManager.PrepareApplicationDomain(apps, plugins); 50 applicationManager.PrepareApplicationDomain(pm.Applications, pm.Plugins); 51 93 52 return applicationDomain; 94 53 } 95 #endregion 54 55 /// <summary> 56 /// Creates a sandbox with restricted permissions. 57 /// Code that is executed in such an AppDomain is partially-trusted and is not allowed to call or override 58 /// methods that require full trust. 59 /// </summary> 60 public static AppDomain CreateAndInitSandbox(string appDomainName, string applicationBase, string configFilePath) { 61 PermissionSet pSet; 62 63 pSet = new PermissionSet(PermissionState.None); 64 pSet.AddPermission(new SecurityPermission(PermissionState.None)); 65 pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); 66 pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Infrastructure)); 67 pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)); 68 pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.SerializationFormatter)); 69 pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.ControlAppDomain)); 70 //needed for HeuristicLab.Persistence, see DynamicMethod Constructor (String, Type, array<Type []()>[], Type, Boolean) 71 pSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.ControlEvidence)); 72 73 ReflectionPermission refPerm = new ReflectionPermission(PermissionState.Unrestricted); 74 pSet.AddPermission(refPerm); 75 76 FileIOPermission ioPerm = new FileIOPermission(PermissionState.None); 77 //allow path discovery for system drive, needed by HeuristicLab.Persistence: Serializer.BuildTypeCache() -> Assembly.CodeBase 78 ioPerm.AddPathList(FileIOPermissionAccess.PathDiscovery, Environment.SystemDirectory.Substring(0, 3)); 79 //allow full access to the appdomain's base directory 80 ioPerm.AddPathList(FileIOPermissionAccess.AllAccess, applicationBase); 81 pSet.AddPermission(ioPerm); 82 83 AppDomainSetup setup = new AppDomainSetup(); 84 setup.PrivateBinPath = applicationBase; 85 setup.ApplicationBase = applicationBase; 86 setup.ConfigurationFile = configFilePath; 87 88 Type applicationManagerType = typeof(SandboxApplicationManager); 89 AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, null, setup, pSet, null); 90 SandboxApplicationManager applicationManager = (SandboxApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null); 91 92 PluginManager pm = new PluginManager(applicationBase); 93 pm.DiscoverAndCheckPlugins(); 94 applicationManager.PrepareApplicationDomain(pm.Applications, pm.Plugins); 95 96 return applicationDomain; 97 } 96 98 } 97 99 }
Note: See TracChangeset
for help on using the changeset viewer.