Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/10/11 14:28:48 (14 years ago)
Author:
ascheibe
Message:

#831

  • completed SandboxManager
  • added SandboxApplicationManager for Hive
  • DefaultApplicationManager is now a subclass of the SandboxApplicationManager
  • ApplicationManager now instantiates the LightweightApplicationManager only if it is accessed to avoid calling InitializeLifetimeService(..) in a sandbox
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Sandboxing/SandboxManager.cs

    r5523 r6174  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Diagnostics;
    25 using System.Linq;
    2623using System.Reflection;
    2724using System.Security;
    2825using System.Security.Permissions;
    29 using System.Security.Policy;
    3026using HeuristicLab.PluginInfrastructure.Manager;
    3127
    3228namespace HeuristicLab.PluginInfrastructure.Sandboxing {
    33   public class SandboxManager {
     29  public static class SandboxManager {
    3430
    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);
    3738
    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();
    8040      setup.PrivateBinPath = applicationBase;
    8141      setup.ApplicationBase = applicationBase;
    8242      setup.ConfigurationFile = configFilePath;
    8343
    84       AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
    8544      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
    8848      PluginManager pm = new PluginManager(applicationBase);
    8949      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
    9352      return applicationDomain;
    9453    }
    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    }
    9698  }
    9799}
Note: See TracChangeset for help on using the changeset viewer.