Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

CA2145: Transparent methods should not be decorated with the SuppressUnmanagedCodeSecurityAttribute

TypeName

TransparentMethodsShouldNotUseSuppressUnmanagedCodeSecurity

CheckId

CA2145

Category

Microsoft.Security

Breaking Change

Breaking

A transparent method, a method that is marked with the SecuritySafeCriticalAttribute method, or a type that contains a method is marked with the SuppressUnmanagedCodeSecurityAttribute attribute.

Methods decorated with the SuppressUnmanagedCodeSecurityAttribute attribute have an implicit LinkDemand placed upon any method that calls it. This LinkDemand requires that the calling code be security critical. Marking the method that uses SuppressUnmanagedCodeSecurity with the SecurityCriticalAttribute attribute makes this requirement more obvious for callers of the method.

To fix a violation of this rule, mark the method or type with the SecurityCriticalAttribute attribute.

Do not suppress a warning from this rule.

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);
    }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.