다음을 통해 공유


CA2138: 투명한 메서드는 SuppressUnmanagedCodeSecurity 특성을 가진 메서드를 호출해서는 안 됩니다.

TypeName

TransparentMethodsMustNotCallSuppressUnmanagedCodeSecurityMethods

CheckId

CA2138

범주

Microsoft.Security

변경 수준

주요 변경

원인

보안 투명 메서드는 SuppressUnmanagedCodeSecurityAttribute 특성으로 표시된 메서드를 호출합니다.

규칙 설명

이 규칙은 P/Invoke(플랫폼 호출) 호출을 통해 사용함으로써 네이티브 코드로 직접 호출하는 모든 투명 메서드에서 실행됩니다.SuppressUnmanagedCodeSecurityAttribute 특성으로 표시되는 P/Invoke 및 COM interop 메서드는 호출하는 메서드에 대해 LinkDemand가 실행되도록 합니다.보안 투명 코드가 LinkDemands를 충족할 수 없기 때문에 코드는 SuppressUnmanagedCodeSecurity 특성이 표시된 메서드 또는 SuppressUnmanagedCodeSecurity 특성이 표시된 클래스의 메서드를 호출할 수 없습니다.이 메서드는 실패하거나 요청이 완전 요청으로 변환됩니다.

이 규칙 위반으로 인해 수준 2 보안 투명성 모델에서 MethodAccessException이 발생하고 수준 1 투명성 모델에서 UnmanagedCode에 대한 완전 요청이 발생합니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 SuppressUnmanagedCodeSecurityAttribute 특성을 제거하고 SecurityCriticalAttribute 또는 SecuritySafeCriticalAttribute 특성으로 이 메서드를 표시하십시오.

경고를 표시하지 않는 경우

이 규칙에서는 경고를 표시해야 합니다.

예제

using System;
using System.Runtime.InteropServices;
using System.Security;


namespace TransparencyWarningsDemo
{

    public class CallSuppressUnmanagedCodeSecurityClass
    {
        [SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool Beep(uint dwFreq, uint dwDuration);

        public void CallNativeMethod()
        {
            // CA2138 violation - transparent method calling a method marked with SuppressUnmanagedCodeSecurity 
            // (this is also a CA2149 violation as well, since this is a P/Invoke and not an interface call).
            Beep(10000, 1);
        }
    }

}