CS.SV.TRANSP.SUCSA

透過的なメソッドは SuppressUnmanagedCodeSecurityAttribute で修飾してはなりません。

SuppressUnmanagedCodeSecurityAttribute 属性で修飾されたメソッドには、それを呼び出すメソッドに配置された暗黙の LinkDemand があります。この LinkDemand では、呼び出しているコードがセキュリティクリティカルである必要があります。SuppressUnmanagedCodeSecurity を使用するメソッドを SecurityCriticalAttribute 属性でマークすると、メソッドの呼び出し元にとって、この要件がより明確になります。

軽減と防止

この規則の違反を修正するには、メソッドまたは型を SecurityCriticalAttribute 属性でマークします。

脆弱コード例

コピー
  using System;
  using System.Runtime.InteropServices;
  using System.Security;
  
  namespace TransparencyWarningsDemo
  {
  
      public class SafeNativeMethods
     {
         // CA2145 violation - transparent method marked SuppressUnmanagedCodeSecurity.  This should be fixed by 
         // marking this method SecurityCritical.
         [DllImport("kernel32.dll", SetLastError = true)]
         [SuppressUnmanagedCodeSecurity]
         [return: MarshalAs(UnmanagedType.Bool)]
         internal static extern bool Beep(uint dwFreq, uint dwDuration);
     }
 }