Ejemplo n.º 1
0
		protected void Schedule()
		{
			if (!this.isScheduled)
			{
				this.isScheduled = true;
				if (PartialTrustHelpers.ShouldFlowSecurityContext)
				{
					this.context = PartialTrustHelpers.CaptureSecurityContextNoIdentityFlow();
				}
				if (this.context == null)
				{
					this.ScheduleCallback(ActionItem.CallbackHelper.InvokeWithoutContextCallback);
					return;
				}
				else
				{
					this.ScheduleCallback(ActionItem.CallbackHelper.InvokeWithContextCallback);
					return;
				}
			}
			else
			{
				throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.ActionItemIsAlreadyScheduled));
			}
		}
Ejemplo n.º 2
0
		internal ExecutionContext (ExecutionContext ec)
		{
			if (ec._sc != null)
				_sc = new SecurityContext (ec._sc);
			_suppressFlow = ec._suppressFlow;
			_capture = true;
		}
Ejemplo n.º 3
0
 internal void Setup(SecurityContextDisableFlow flags)
 {
     this.useEC = false;
     this._sc = Thread.CurrentThread.ExecutionContext.SecurityContext;
     this._sc._disableFlow = flags;
     this._thread = Thread.CurrentThread;
 }
Ejemplo n.º 4
0
		// copy constructor
		internal SecurityContext (SecurityContext sc)
		{
			_capture = true;
			_winid = sc._winid;
			if (sc._stack != null)
				_stack = sc._stack.CreateCopy ();
		}
Ejemplo n.º 5
0
		internal ExecutionContext (ExecutionContext ec)
		{
#if !NET_2_1 || MONOTOUCH
			if (ec._sc != null)
				_sc = new SecurityContext (ec._sc);
#endif
			_suppressFlow = ec._suppressFlow;
			_capture = true;
		}
Ejemplo n.º 6
0
		// copy constructor
		internal SecurityContext (SecurityContext sc)
		{
			_capture = true;
#if !MOBILE
			_winid = sc._winid;
			if (sc._stack != null)
				_stack = sc._stack.CreateCopy ();
#endif
		}
		internal ExecutionContext (ExecutionContext ec)
		{
#if !MOONLIGHT
			if (ec._sc != null)
				_sc = new SecurityContext (ec._sc);
#endif
			_suppressFlow = ec._suppressFlow;
			_capture = true;
		}
Ejemplo n.º 8
0
		private ExecutionContext (ExecutionContext ec)
		{
#if !MOBILE
			if (ec._sc != null)
				_sc = new SecurityContext (ec._sc);
#endif
			if (ec._lcc != null)
				_lcc = (LogicalCallContext) ec._lcc.Clone ();

			_suppressFlow = ec._suppressFlow;
			_capture = true;
		}
Ejemplo n.º 9
0
		// static methods

		static public SecurityContext Capture ()
		{
			SecurityContext sc = Thread.CurrentThread.ExecutionContext.SecurityContext;
			if (sc.FlowSuppressed)
				return null;

			SecurityContext capture = new SecurityContext ();
			capture._capture = true;
			capture._winid = WindowsIdentity.GetCurrentToken ();
			capture._stack = CompressedStack.Capture ();
			return capture;
		}
Ejemplo n.º 10
0
        internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext)
        {
            StackCrawlMark           lookForMyCaller = StackCrawlMark.LookForMyCaller;
            ExecutionContextSwitcher switcher        = new ExecutionContextSwitcher {
                thread = System.Threading.Thread.CurrentThread,
                prevEC = System.Threading.Thread.CurrentThread.GetExecutionContextNoCreate(),
                currEC = executionContext
            };

            System.Threading.Thread.CurrentThread.SetExecutionContext(executionContext);
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                if (executionContext == null)
                {
                    return(switcher);
                }
                System.Security.SecurityContext securityContext = executionContext.SecurityContext;
                if (securityContext != null)
                {
                    System.Security.SecurityContext prevSecurityContext = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null;
                    switcher.scsw = System.Security.SecurityContext.SetSecurityContext(securityContext, prevSecurityContext, ref lookForMyCaller);
                }
                else if (!System.Security.SecurityContext.CurrentlyInDefaultFTSecurityContext(switcher.prevEC))
                {
                    System.Security.SecurityContext context3 = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null;
                    switcher.scsw = System.Security.SecurityContext.SetSecurityContext(System.Security.SecurityContext.FullTrustSecurityContext, context3, ref lookForMyCaller);
                }
                System.Threading.SynchronizationContext synchronizationContext = executionContext.SynchronizationContext;
                if (synchronizationContext != null)
                {
                    System.Threading.SynchronizationContext prevSyncContext = (switcher.prevEC != null) ? switcher.prevEC.SynchronizationContext : null;
                    switcher.sysw = System.Threading.SynchronizationContext.SetSynchronizationContext(synchronizationContext, prevSyncContext);
                }
                System.Threading.HostExecutionContext hostExecutionContext = executionContext.HostExecutionContext;
                if (hostExecutionContext != null)
                {
                    switcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostExecutionContext);
                }
            }
            catch
            {
                switcher.UndoNoThrow();
                throw;
            }
            return(switcher);
        }
Ejemplo n.º 11
0
 private void Dispose(bool disposing)
 {
     if (!this.disposed)
     {
         if (disposing)
         {
             this.DisposeCommands();
             this.localPipeline         = null;
             this.externalSuccessOutput = null;
             this.externalErrorOutput   = null;
             this.executionScope        = null;
             this.SecurityContext.Dispose();
             this.SecurityContext = null;
             this.logBuffer       = null;
         }
         this.disposed = true;
     }
 }
Ejemplo n.º 12
0
        protected void Schedule()
        {
            if (isScheduled)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(CommonResources.ActionItemIsAlreadyScheduled));
            }

            this.isScheduled = true;
            if (PartialTrustHelpers.ShouldFlowSecurityContext)
            {
                this.context = PartialTrustHelpers.CaptureSecurityContextNoIdentityFlow();
            }
            if (this.context != null)
            {
                ScheduleCallback(CallbackHelper.InvokeWithContextCallback);
            }
            else
            {
                ScheduleCallback(CallbackHelper.InvokeWithoutContextCallback);
            }
        }
Ejemplo n.º 13
0
        internal static ExecutionContext Capture(ref StackCrawlMark stackMark, CaptureOptions options)
        {
            if (IsFlowSuppressed())
            {
                return(null);
            }
            bool             flag  = CaptureOptions.None != (options & CaptureOptions.IgnoreSyncCtx);
            bool             flag2 = CaptureOptions.None != (options & CaptureOptions.OptimizeDefaultCase);
            ExecutionContext executionContextNoCreate = System.Threading.Thread.CurrentThread.GetExecutionContextNoCreate();

            System.Security.SecurityContext         context2 = System.Security.SecurityContext.Capture(executionContextNoCreate, ref stackMark);
            System.Threading.HostExecutionContext   context3 = HostExecutionContextManager.CaptureHostExecutionContext();
            System.Threading.SynchronizationContext context4 = null;
            if ((executionContextNoCreate != null) && !flag)
            {
                context4 = (executionContextNoCreate._syncContext == null) ? null : executionContextNoCreate._syncContext.CreateCopy();
            }
            System.Runtime.Remoting.Messaging.LogicalCallContext context5 = null;
            if (((executionContextNoCreate != null) && (executionContextNoCreate._logicalCallContext != null)) && executionContextNoCreate.LogicalCallContext.HasInfo)
            {
                context5 = (System.Runtime.Remoting.Messaging.LogicalCallContext)executionContextNoCreate.LogicalCallContext.Clone();
            }
            if (((flag2 && (context2 == null)) && ((context3 == null) && (context4 == null))) && ((context5 == null) || !context5.HasInfo))
            {
                return(s_dummyDefaultEC);
            }
            ExecutionContext context6 = new ExecutionContext {
                SecurityContext = context2
            };

            if (context6.SecurityContext != null)
            {
                context6.SecurityContext.ExecutionContext = context6;
            }
            context6._hostExecutionContext = context3;
            context6._syncContext          = context4;
            context6.LogicalCallContext    = context5;
            context6.isNewCapture          = true;
            return(context6);
        }
Ejemplo n.º 14
0
        protected void Schedule()
        {
            if (isScheduled)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.ActionItemIsAlreadyScheduled));
            }

            this.isScheduled = true;
#if FEATURE_COMPRESSEDSTACK
            if (PartialTrustHelpers.ShouldFlowSecurityContext)
            {
                this.context = PartialTrustHelpers.CaptureSecurityContextNoIdentityFlow();
            }
            if (this.context != null)
            {
                ScheduleCallback(CallbackHelper.InvokeWithContextCallback);
            }
            else
#endif
            {
                ScheduleCallback(CallbackHelper.InvokeWithoutContextCallback);
            }
        }
Ejemplo n.º 15
0
 public bool IsSame(SecurityContext.Reader sc) { return m_sc == sc.m_sc; }
Ejemplo n.º 16
0
 public bool IsSame(SecurityContext sc) { return m_sc == sc; }
Ejemplo n.º 17
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        internal SecurityContext CreateMutableCopy()
        {
            Contract.Assert(!this.isNewCapture);

            SecurityContext sc = new SecurityContext();
            sc._disableFlow = this._disableFlow;

#if !FEATURE_PAL && FEATURE_IMPERSONATION
            if (this.WindowsIdentity != null)
                sc._windowsIdentity = new WindowsIdentity(this.WindowsIdentity.AccessToken);
#endif //!FEATURE_PAL && FEATURE_IMPERSONATION

            // 
            if (this._compressedStack != null)
                sc._compressedStack = this._compressedStack.CreateCopy();

            return sc;
        }
Ejemplo n.º 18
0
        [System.Security.SecurityCritical]  // auto-generated
        static internal SecurityContext CreateFullTrustSecurityContext()
        {
            SecurityContext sc = new SecurityContext();
            sc.isNewCapture = true;
        
    #if !FEATURE_PAL && FEATURE_IMPERSONATION
            if (IsWindowsIdentityFlowSuppressed())
            {
                sc._disableFlow = SecurityContextDisableFlow.WI;
            }
    #endif // !FEATURE_PAL && FEATURE_IMPERSONATION
        
#if FEATURE_COMPRESSEDSTACK
            // Force create CompressedStack
            sc.CompressedStack = new CompressedStack(null);
#endif
            return sc;
        }
Ejemplo n.º 19
0
 internal SecurityContextRunData(SecurityContext securityContext, ContextCallback cb, Object state)
 {
     this.sc = securityContext;
     this.callBack = cb;
     this.state = state;
     this.scsw = new SecurityContextSwitcher();
 }
Ejemplo n.º 20
0
        [HandleProcessCorruptedStateExceptions] // 
#endif // FEATURE_CORRUPTING_EXCEPTIONS
        internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext.Reader prevSecurityContext, bool modifyCurrentExecutionContext, ref StackCrawlMark stackMark)
        {
            // Save the flow state at capture and reset it in the SC.
            SecurityContextDisableFlow _capturedFlowState = sc._disableFlow;
            sc._disableFlow = SecurityContextDisableFlow.Nothing;
            
            //Set up the switcher object
            SecurityContextSwitcher scsw = new SecurityContextSwitcher();
            scsw.currSC = sc;   
            scsw.prevSC = prevSecurityContext;

            if (modifyCurrentExecutionContext)
            {
                // save the current Execution Context
                ExecutionContext currEC = Thread.CurrentThread.GetMutableExecutionContext();
                scsw.currEC = currEC;
                currEC.SecurityContext = sc;
            }

            if (sc != null)
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
#if !FEATURE_PAL && FEATURE_IMPERSONATION
                    scsw.wic = null;
                    if (!_LegacyImpersonationPolicy)
                    {
                        if (sc.WindowsIdentity != null)
                        {
                            scsw.wic = sc.WindowsIdentity.Impersonate(ref stackMark);
                        }
                        else if ( ((_capturedFlowState & SecurityContextDisableFlow.WI) == 0) 
                            && prevSecurityContext.WindowsIdentity != null)
                        {
                            // revert impersonation if there was no WI flow supression at capture and we're currently impersonating
                            scsw.wic = WindowsIdentity.SafeRevertToSelf(ref stackMark); 
                        }
                    }
#endif
                    scsw.cssw = CompressedStack.SetCompressedStack(sc.CompressedStack, prevSecurityContext.CompressedStack);
                }
                catch 
                {
                    scsw.UndoNoThrow();
                    throw;
                }      
            }
            return scsw;
        }
Ejemplo n.º 21
0
 public static bool IsWindowsIdentityFlowSuppressed()
 {
     return(_LegacyImpersonationPolicy || SecurityContext.IsFlowSuppressed(SecurityContextDisableFlow.WI));
 }
Ejemplo n.º 22
0
 internal void Setup(SecurityContextDisableFlow flags)
 {
     useEC = false;
     Thread currentThread = Thread.CurrentThread;
     _sc = currentThread.GetMutableExecutionContext().SecurityContext;
     _sc._disableFlow = flags;
     _thread = currentThread;
 }
Ejemplo n.º 23
0
		static public void Run (SecurityContext securityContext, ContextCallback callback, object state)
		{
			if (securityContext == null) {
				throw new InvalidOperationException (Locale.GetText (
					"Null SecurityContext"));
			}

			SecurityContext sc = Thread.CurrentThread.ExecutionContext.SecurityContext;
			IPrincipal original = Thread.CurrentPrincipal;
			try {
				if (sc.IdentityToken != IntPtr.Zero) {
					Thread.CurrentPrincipal = new WindowsPrincipal (new WindowsIdentity (sc.IdentityToken));
				}

				// FIXME: is the security manager isn't active then we may not have
				// a compressed stack (bug #78652)
				if (securityContext.CompressedStack != null)
					CompressedStack.Run (securityContext.CompressedStack, callback, state);
				else
					callback (state);
			}
			finally {
				if ((original != null) && (sc.IdentityToken != IntPtr.Zero))
					Thread.CurrentPrincipal = original;
			}
		}
Ejemplo n.º 24
0
 static public void Run(SecurityContext securityContext, ContextCallback callback, object state)
 {
     callback(state);
 }
Ejemplo n.º 25
0
        [System.Security.SecurityCritical]  // auto-generated 
        static internal SecurityContext Capture(ExecutionContext currThreadEC, ref StackCrawlMark stackMark)
        { 
          // check to see if Flow is suppressed
        if (IsFlowSuppressed()) return null;

        // If we're in FT right now, return null 
        if (CurrentlyInDefaultFTSecurityContext(currThreadEC))
            return null; 
 
        SecurityContext sc = new SecurityContext();
        sc.isNewCapture = true; 

#if !FEATURE_PAL && FEATURE_IMPERSONATION
            // Force create WindowsIdentity
        if (!IsWindowsIdentityFlowSuppressed()) 
        {
            WindowsIdentity currentIdentity = GetCurrentWI(currThreadEC); 
            if (currentIdentity != null) 
                sc._windowsIdentity = new WindowsIdentity(currentIdentity.TokenHandle);
        } 
        else
        {
            sc._disableFlow = SecurityContextDisableFlow.WI;
        } 
#endif // !FEATURE_PAL && FEATURE_IMPERSONATION
 
        // Force create CompressedStack 
        sc.CompressedStack = CompressedStack.GetCompressedStack(ref stackMark);
        return sc; 
    }
Ejemplo n.º 26
0
 [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
 internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext prevSecurityContext) 
 {
     StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
     return SetSecurityContext(sc, prevSecurityContext, ref stackMark);
 } 
Ejemplo n.º 27
0
        protected void ScheduleWithContext(SecurityContext context)
        {
            if (context == null)
            {
                throw Fx.Exception.ArgumentNull("context");
            }
            if (isScheduled)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.ActionItemIsAlreadyScheduled));
            }

            this.isScheduled = true;
            this.context = context.CreateCopy();
            ScheduleCallback(CallbackHelper.InvokeWithContextCallback);
        }
Ejemplo n.º 28
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public static void Run(SecurityContext securityContext, ContextCallback callback, Object state)
        {
            if (securityContext == null )
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullContext"));
            }
            Contract.EndContractBlock();

            StackCrawlMark stackMark = StackCrawlMark.LookForMe;

            if (!securityContext.isNewCapture)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
            }

            securityContext.isNewCapture = false;
#if !MOBILE
            ExecutionContext.Reader ec = Thread.CurrentThread.GetExecutionContextReader();
            
            // Optimization: do the callback directly if both the current and target contexts are equal to the
            // default full-trust security context
            if ( SecurityContext.CurrentlyInDefaultFTSecurityContext(ec)
                && securityContext.IsDefaultFTSecurityContext())
            {
                callback(state);
                
                if (GetCurrentWI(Thread.CurrentThread.GetExecutionContextReader()) != null) 
                {
                    // If we enter here it means the callback did an impersonation
                    // that we need to revert.
                    // We don't need to revert any other security state since it is stack-based 
                    // and automatically goes away when the callback returns.
                    WindowsIdentity.SafeRevertToSelf(ref stackMark);
                    // Ensure we have reverted to the state we entered in.
                    Contract.Assert(GetCurrentWI(Thread.CurrentThread.GetExecutionContextReader()) == null);
                }
            }
            else
#endif
            {
                RunInternal(securityContext, callback, state);
            }

        }
Ejemplo n.º 29
0
 public static void Run(SecurityContext securityContext, ContextCallback callback, object state)
 {
     throw new NotSupportedException();
 }
Ejemplo n.º 30
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static void RunInternal(SecurityContext securityContext, ContextCallback callBack, Object state)
        {
            if (cleanupCode == null)
            {
                tryCode = new RuntimeHelpers.TryCode(runTryCode);
                cleanupCode = new RuntimeHelpers.CleanupCode(runFinallyCode);
            }
            SecurityContextRunData runData = new SecurityContextRunData(securityContext, callBack, state);
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, runData);

        }
Ejemplo n.º 31
0
 public static bool IsFlowSuppressed()
 {
     return(SecurityContext.IsFlowSuppressed(SecurityContextDisableFlow.All));
 }
Ejemplo n.º 32
0
 [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
 internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext.Reader prevSecurityContext, bool modifyCurrentExecutionContext)
 {
     StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
     return SetSecurityContext(sc, prevSecurityContext, modifyCurrentExecutionContext, ref stackMark);
 }
Ejemplo n.º 33
0
 public bool IsSame(SecurityContext sc)
 {
     return(m_sc == sc);
 }
Ejemplo n.º 34
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public SecurityContext CreateCopy()
        {
            if (!isNewCapture)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
            }                                

            SecurityContext sc = new SecurityContext();
            sc.isNewCapture = true;
            sc._disableFlow = _disableFlow;

#if !FEATURE_PAL && FEATURE_IMPERSONATION
            if (WindowsIdentity != null)
                sc._windowsIdentity = new WindowsIdentity(WindowsIdentity.AccessToken);
#endif //!FEATURE_PAL && FEATURE_IMPERSONATION

            if (_compressedStack != null)
                sc._compressedStack = _compressedStack.CreateCopy();

            return sc;
        }
Ejemplo n.º 35
0
 public Reader(SecurityContext sc)
 {
     m_sc = sc;
 }
Ejemplo n.º 36
0
        [System.Security.SecurityCritical]  // auto-generated
        static private SecurityContext CaptureCore(ExecutionContext.Reader currThreadEC, ref StackCrawlMark stackMark)
        {
            SecurityContext sc = new SecurityContext();
            sc.isNewCapture = true;

    #if !FEATURE_PAL && FEATURE_IMPERSONATION
                // Force create WindowsIdentity
            if (!IsWindowsIdentityFlowSuppressed())
            {
                WindowsIdentity currentIdentity = GetCurrentWI(currThreadEC);
                if (currentIdentity != null)
                    sc._windowsIdentity = new WindowsIdentity(currentIdentity.AccessToken);
            }
            else
            {
                sc._disableFlow = SecurityContextDisableFlow.WI;
            }
    #endif // !FEATURE_PAL && FEATURE_IMPERSONATION

            // Force create CompressedStack
            sc.CompressedStack = CompressedStack.GetCompressedStack(ref stackMark);
            return sc;
        }
Ejemplo n.º 37
0
 public Reader(SecurityContext sc) { m_sc = sc; }
Ejemplo n.º 38
0
        [HandleProcessCorruptedStateExceptions] // 
#endif // FEATURE_CORRUPTING_EXCEPTIONS
        public void Undo()
        {        
            if (currSC == null) 
            {
                return; // mutiple Undo()s called on this switcher object
            }  

            if (currEC != null)
            {
                Contract.Assert(currEC == Thread.CurrentThread.GetMutableExecutionContext(), "SecurityContextSwitcher used from another thread");
                Contract.Assert(currSC == currEC.SecurityContext, "SecurityContextSwitcher context mismatch");
            
                // restore the saved security context 
                currEC.SecurityContext = prevSC.DangerousGetRawSecurityContext();
            }
            else
            {
                // caller must have already restored the ExecutionContext
                Contract.Assert(Thread.CurrentThread.GetExecutionContextReader().SecurityContext.IsSame(prevSC));
            }

            currSC = null; // this will prevent the switcher object being used again        

            bool bNoException = true;
#if !FEATURE_PAL && FEATURE_IMPERSONATION
            try 
            {
                if (wic != null)
                    bNoException &= wic.UndoNoThrow();
            }
            catch
            {
                // Failfast since we can't continue safely...
                bNoException &= cssw.UndoNoThrow();
                System.Environment.FailFast(Environment.GetResourceString("ExecutionContext_UndoFailed"));
                
            }
#endif
            bNoException &= cssw.UndoNoThrow();


            if (!bNoException)
            {
                // Failfast since we can't continue safely...
                System.Environment.FailFast(Environment.GetResourceString("ExecutionContext_UndoFailed"));                
            }

        }
Ejemplo n.º 39
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext.Reader prevSecurityContext, bool modifyCurrentExecutionContext)
        {
            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;

            return(SetSecurityContext(sc, prevSecurityContext, modifyCurrentExecutionContext, ref stackMark));
        }