
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Making a Security Assert Safe
|
979
The code listing for SecureFunction is shown here:
public static void SecureFunction( )
{
// Set up a permission to be able to access nonpublic members
// via reflection.
ReflectionPermission perm =
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
// Demand the right to do this and cause a stackwalk.
perm.Demand( );
// Perform the action here...
}
Discussion
In the demonstration function CallSecureFunctionSafelyAndEfficiently, the func-
tion you are calling (
SecureFunction) performs a Demand on a ReflectionPermission to
ensure that the code can access nonpublic members of classes via reflection. Nor-
mally, this would result in a stackwalk for every call to
SecureFunction. The Demand in
CallSecureFunctionSafelyAndEfficiently is there only to protect against the usage of
the
Assert in the first place. To make this more efficient, you can use Assert to state
that all functions issuing
Demands that are called from this one do not have to stack-
walk any further. The
Assert says stop checking for this permission in the call stack.
In order to do this, you need the permission to call
Assert.
The problem comes in with this
Assert as it opens up a potential luring attack where
SecureFunction is called via CallSecureFunctionSafelyAndEfficiently,