CA2116:APTCA 方法应只调用 APTCA 方法

类型名

AptcaMethodsShouldOnlyCallAptcaMethods

CheckId

CA2116

类别

Microsoft.Security

是否重大更改

原因

具有 System.Security.AllowPartiallyTrustedCallersAttribute 特性的程序集中的某方法调用了没有该特性的程序集中的方法。

规则说明

默认情况下,具有强名称的程序集中的公共方法或受保护方法受针对完全信任的 链接需求 的隐式保护;只有完全受信任的调用方才可以访问具有强名称的程序集。 用 AllowPartiallyTrustedCallersAttribute (APTCA) 特性标记的具有强名称的程序集没有此保护。 该特性禁用链接要求,使未获得完全信任的调用方可以访问具有强名称的程序集,例如可通过 Intranet 或 Internet 执行代码。

在完全受信任的程序集具有 APTCA 特性时,如果该程序集执行不允许部分受信任调用方的另一个程序集中的代码,则可能存在安全漏洞。 如果两个方法 M1 和 M2 满足以下条件,则恶意调用方可以使用 M1 方法绕过用于保护 M2 的隐式的完全信任链接要求:

  • M1 是在具有 APTCA 特性的完全受信任程序集中声明的公共方法。

  • M1 调用 M1 的程序集之外的 M2 方法。

  • M2 的程序集没有 APTCA 特性,因此不应由部分受信任的调用方执行或代表部分受信任的调用方执行。

部分受信任的调用方 X 可以调用 M1 方法,从而可以使 M1 调用 M2。 因为 M2 没有 APTCA 特性,所以其直接调用方 (M1) 必须满足针对完全信任的链接要求;M1 具有完全信任,因此满足此检查。 因为 X 不满足防止不受信任的调用方调用 M2 的链接要求,所以存在安全风险。 因此,具有 APTCA 特性的方法不得调用没有该特性的方法。

如何解决冲突

如果 APCTA 特性是必需的,请使用需求保护调入完全信任程序集的方法。 您要求的确切权限将取决于由方法公开的功能。 如果可能,请使用对完全信任的要求保护该方法以确保基础功能未对部分受信任的调用方公开。 如果不可能,请选择能够有效保护已公开功能的一组权限。 有关需求的更多信息,请参见需求

何时禁止显示警告

若要安全地禁止显示此规则发出的警告,必须确保由方法公开的功能不会直接或间接允许调用方访问能够以破坏方式使用的敏感信息、操作或资源。

示例

下面的示例使用两个程序集和一个测试应用程序来阐释该规则检测到的安全漏洞。 第一个程序集没有 APTCA 特性,部分受信任的调用方(由前面讨论中的 M2 表示)应无法访问它。

using System;
using System.Security;
using System.Security.Permissions;
using System.Reflection;

// This code is compiled into a strong-named
// assembly that requires full trust and does 
// not allow partially trusted callers. 

namespace AptcaTestLibrary
{
   public class ClassRequiringFullTrust
   {
      public static void DoWork()
      {
        Console.WriteLine("ClassRequiringFullTrust.DoWork was called.");
      }
   }
}

第二个程序集是完全受信任的,允许部分受信任的调用方(由前面讨论中的 M1 表示)访问。

using System;
using System.Security;
using System.Security.Permissions;
using System.Reflection;

// This assembly executes with full trust and 
// allows partially trusted callers. 

[assembly:AllowPartiallyTrustedCallers]  

namespace AptcaTestLibrary
{
   public class AccessAClassRequiringFullTrust
   {
      public static void Access()
      {    
         // This security check fails if the caller 
         // does not have full trust. 
         NamedPermissionSet pset= new NamedPermissionSet("FullTrust");

         // This try-catch block shows the caller's permissions.
         // Correct code would either not catch the exception,
         // or would rethrow it.
         try 
         {
            pset.Demand();
         }
         catch (SecurityException e)
         {
            Console.WriteLine("Demand for full trust:{0}", e.Message);
         }
         // Call the type that requires full trust.
         // Violates rule AptcaMethodsShouldOnlyCallAptcaMethods.
         ClassRequiringFullTrust.DoWork();
     }
   }
}

测试应用程序(由前面讨论中的 X 表示)是部分受信任的。

using System;
using AptcaTestLibrary;

// If this test is run from the local computer, it gets full trust by default.
// Remove full trust.
[assembly:System.Security.Permissions.PermissionSetAttribute(
   System.Security.Permissions.SecurityAction.RequestRefuse, Name="FullTrust")]

namespace TestSecLibrary
{
   class TestApctaMethodRule
   {
      public static void Main()
      {
          // Indirectly calls DoWork in the full-trust class.
          ClassRequiringFullTrust testClass = new ClassRequiringFullTrust();
          testClass.Access();
      }
   }
}

该示例产生下面的输出。

      

相关规则

CA2117:APTCA 类型应只扩展 APTCA 基类型

请参见

概念

代码安全维护指南

部分受信任的代码可调用的 .NET Framework 程序集

通过部分受信任的代码使用库

需求

链接需求

其他资源

数据访问和建模