1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
//------------------------------------------------------------------------------
// <copyright file="SecurityUtils.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
*/
#if Microsoft_NAMESPACE
namespace System.Windows.Forms
#elif DRAWING_NAMESPACE
namespace System.Drawing
#elif Microsoft_PUBLIC_GRAPHICS_LIBRARY
namespace System.Internal
#elif SYSTEM_NAMESPACE
namespace System
#elif SYSTEM_WEB
namespace System.Web
#elif SYSTEM_DATA_LINQ
namespace System.Data.Linq
#else
namespace System.Windows.Forms
#endif
{
using System;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Security;
using System.Security.Permissions;
/// <devdoc>
/// Useful methods to securely call 'dangerous' managed APIs (especially reflection).
/// See http://wiki/default.aspx/Microsoft.Projects.DotNetClient.SecurityConcernsAroundReflection
/// for more information specifically about why we need to be careful about reflection invocations.
/// </devdoc>
internal static class SecurityUtils {
private static volatile ReflectionPermission memberAccessPermission = null;
private static volatile ReflectionPermission restrictedMemberAccessPermission = null;
private static ReflectionPermission MemberAccessPermission
{
get {
if (memberAccessPermission == null) {
memberAccessPermission = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
}
return memberAccessPermission;
}
}
private static ReflectionPermission RestrictedMemberAccessPermission {
get {
if (restrictedMemberAccessPermission == null) {
restrictedMemberAccessPermission = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess);
}
return restrictedMemberAccessPermission;
}
}
private static void DemandReflectionAccess(Type type) {
#if MONO_FEATURE_CAS
try {
MemberAccessPermission.Demand();
}
catch (SecurityException) {
DemandGrantSet(type.Assembly);
}
#endif
}
[SecuritySafeCritical]
private static void DemandGrantSet(Assembly assembly) {
#if MONO_FEATURE_CAS
PermissionSet targetGrantSet = assembly.PermissionSet;
targetGrantSet.AddPermission(RestrictedMemberAccessPermission);
targetGrantSet.Demand();
#endif
}
private static bool HasReflectionPermission(Type type) {
try {
DemandReflectionAccess(type);
return true;
}
catch (SecurityException) {
}
return false;
}
/// <devdoc>
/// This helper method provides safe access to Activator.CreateInstance.
/// NOTE: This overload will work only with public .ctors.
/// </devdoc>
internal static object SecureCreateInstance(Type type) {
return SecureCreateInstance(type, null, false);
}
/// <devdoc>
/// This helper method provides safe access to Activator.CreateInstance.
/// Set allowNonPublic to true if you want non public ctors to be used.
/// </devdoc>
internal static object SecureCreateInstance(Type type, object[] args, bool allowNonPublic) {
if (type == null) {
throw new ArgumentNullException("type");
}
BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
// if it's an internal type, we demand reflection permission.
if (!type.IsVisible) {
DemandReflectionAccess(type);
}
else if (allowNonPublic && !HasReflectionPermission(type)) {
// Someone is trying to instantiate a public type in *our* assembly, but does not
// have full reflection permission. We shouldn't pass BindingFlags.NonPublic in this case.
// The reason we don't directly demand the permission here is because we don't know whether
// a public or non-public .ctor will be invoked. We want to allow the public .ctor case to
// succeed.
allowNonPublic = false;
}
if (allowNonPublic) {
flags |= BindingFlags.NonPublic;
}
return Activator.CreateInstance(type, flags, null, args, null);
}
#if (!Microsoft_NAMESPACE)
/// <devdoc>
/// This helper method provides safe access to Activator.CreateInstance.
/// NOTE: This overload will work only with public .ctors.
/// </devdoc>
internal static object SecureCreateInstance(Type type, object[] args) {
return SecureCreateInstance(type, args, false);
}
/// <devdoc>
/// Helper method to safely invoke a .ctor. You should prefer SecureCreateInstance to this.
/// Set allowNonPublic to true if you want non public ctors to be used.
/// </devdoc>
internal static object SecureConstructorInvoke(Type type, Type[] argTypes, object[] args, bool allowNonPublic) {
return SecureConstructorInvoke(type, argTypes, args, allowNonPublic, BindingFlags.Default);
}
/// <devdoc>
/// Helper method to safely invoke a .ctor. You should prefer SecureCreateInstance to this.
/// Set allowNonPublic to true if you want non public ctors to be used.
/// The 'extraFlags' parameter is used to pass in any other flags you need,
/// besides Public, NonPublic and Instance.
/// </devdoc>
internal static object SecureConstructorInvoke(Type type, Type[] argTypes, object[] args,
bool allowNonPublic, BindingFlags extraFlags) {
if (type == null) {
throw new ArgumentNullException("type");
}
// if it's an internal type, we demand reflection permission.
if (!type.IsVisible) {
DemandReflectionAccess(type);
}
else if (allowNonPublic && !HasReflectionPermission(type)) {
// Someone is trying to invoke a ctor on a public type, but does not
// have full reflection permission. We shouldn't pass BindingFlags.NonPublic in this case.
allowNonPublic = false;
}
BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | extraFlags;
if (!allowNonPublic) {
flags &= ~BindingFlags.NonPublic;
}
ConstructorInfo ctor = type.GetConstructor(flags, null, argTypes, null);
if (ctor != null) {
return ctor.Invoke(args);
}
return null;
}
private static bool GenericArgumentsAreVisible(MethodInfo method) {
if (method.IsGenericMethod) {
Type[] parameterTypes = method.GetGenericArguments();
foreach (Type type in parameterTypes) {
if (!type.IsVisible) {
return false;
}
}
}
return true;
}
/// <devdoc>
/// This helper method provides safe access to FieldInfo's GetValue method.
/// </devdoc>
internal static object FieldInfoGetValue(FieldInfo field, object target) {
Type type = field.DeclaringType;
if (type == null) {
// Type is null for Global fields.
if (!field.IsPublic) {
DemandGrantSet(field.Module.Assembly);
}
} else if (!(type != null && type.IsVisible && field.IsPublic)) {
DemandReflectionAccess(type);
}
return field.GetValue(target);
}
/// <devdoc>
/// This helper method provides safe access to MethodInfo's Invoke method.
/// </devdoc>
internal static object MethodInfoInvoke(MethodInfo method, object target, object[] args) {
Type type = method.DeclaringType;
if (type == null) {
// Type is null for Global methods. In this case we would need to demand grant set on
// the containing assembly for internal methods.
if (!(method.IsPublic && GenericArgumentsAreVisible(method))) {
DemandGrantSet(method.Module.Assembly);
}
} else if (!(type.IsVisible && method.IsPublic && GenericArgumentsAreVisible(method))) {
// this demand is required for internal types in system.dll and its friend assemblies.
DemandReflectionAccess(type);
}
return method.Invoke(target, args);
}
/// <devdoc>
/// This helper method provides safe access to ConstructorInfo's Invoke method.
/// Constructors can't be generic, so we don't check if argument types are visible
/// </devdoc>
internal static object ConstructorInfoInvoke(ConstructorInfo ctor, object[] args) {
Type type = ctor.DeclaringType;
if ((type != null) && !(type.IsVisible && ctor.IsPublic)) {
DemandReflectionAccess(type);
}
return ctor.Invoke(args);
}
/// <devdoc>
/// This helper method provides safe access to Array.CreateInstance.
/// </devdoc>
internal static object ArrayCreateInstance(Type type, int length) {
if (!type.IsVisible) {
DemandReflectionAccess(type);
}
return Array.CreateInstance(type, length);
}
#endif
}
}
|