Scenario
Let's say we've the next code:
[SecuritySafeCritical]
public void SomeMethod()
{
SomeCriticalClass critical = new SomeCriticalClass();
Action someDelegate = () =>
{
critical.Do();
}
someDelegate();
}
- The
SomeMethodsignature has[SecuritySafeCritical]attribute. SomeCriticalClassis some class that has the[SecurityCritical]attribute either in the class or methodDomethod-level.- We create an anonymous delegate auto-inferred to
Action.
Problem
Calling critical.Do() causes a MethodAccessExceptionFieldAccessException because a security transparent method (the anonymous method) is trying to access a security critical field (the critical SomeCriticalClass local variable).
Question
How you overcome this?
The easy way would be implementing an actual method marked with [SecuritySafeCritical] instead of using an anonymous delegate. But this moves us to pre-anonymous delegates and lambas era. I don't want this.
Other easy way would be just don't using security transparency. This isn't a solution.
Almost any available libraries both from Microsoft and open source community aren't designed with security transparency in mind. That is, any own custom code must interoperate with third-party libraries through [SecuritySafeCritical] or [SecurityCritical] methods/properties/delegates.
Actually I believe that security transparency is a good tool because it forces better and secure software designs, critical actions are very localized and the rest of the code works with minimal permissions.
Action someDelegate = critical.Do;couldn't you? - Jon Skeet