Don't use CoSetProxyBlanket and CoInitializeSecurity

Rule ND3100 Security

Once managed code is JIT'ed and executed, it is too late to call CoInitializeSecurity() or CoSetProxyBlanket(): the CLR has already initialized the COM security environment for the whole process.

Flagged Code
[DllImport("ole32.dll")]   // flagged: P/Invoke declared and called from managed code
static extern int CoInitializeSecurity(IntPtr pSecDesc, int cAuthSvc, IntPtr asAuthSvc,
   IntPtr pReserved1, uint dwAuthnLevel, uint dwImpLevel, IntPtr pAuthList,
   uint dwCapabilities, IntPtr pReserved3);

static void Main() {
   CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, 6, 3,
                        IntPtr.Zero, 0, IntPtr.Zero);
}
Fixed Code
// Unmanaged C++ host shim, run before the CLR is loaded: managed code declares no P/Invoke
int wmain() {
   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
   CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
                        RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE, nullptr);
   return RunManagedEntryPoint();
}

How to fix

Don't call them from managed code. Write an unmanaged C++ "shim" calling them before loading the CLR within the process, as explained here: https://stackoverflow.com/a/48545055/27194

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Don't use CoSetProxyBlanket and CoInitializeSecurity</Name>
// <Id>ND3100:DontUseCoSetProxyBlanketAndCoInitializeSecurity</Id>
warnif count > 0 

from m in Application.Methods
where (m.HasAttribute ("System.Runtime.InteropServices.DllImportAttribute".AllowNoMatch())) 
  && m.SimpleNameEqualsAny("CoSetProxyBlanket","CoInitializeSecurity")

select new { 
  m,
  Debt = 1.ToHours().ToDebt(),
  AnnualInterest = 2.ToHours().ToAnnualInterest()
}

//<Expl>
// Don't call the *P/Invoke {0}* from managed code.
//</Expl>

//<Desc>
// Once managed code is JIT'ed and executed, it is too late to call
// *CoInitializeSecurity()* or *CoSetProxyBlanket()*: the CLR has already
// initialized the COM security environment for the whole process.
//
// **Flagged Code**
// ```csharp
// [DllImport("ole32.dll")]   // flagged: P/Invoke declared and called from managed code
// static extern int CoInitializeSecurity(IntPtr pSecDesc, int cAuthSvc, IntPtr asAuthSvc,
//    IntPtr pReserved1, uint dwAuthnLevel, uint dwImpLevel, IntPtr pAuthList,
//    uint dwCapabilities, IntPtr pReserved3);
//
// static void Main() {
//    CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, 6, 3,
//                         IntPtr.Zero, 0, IntPtr.Zero);
// }
// ```
//
// **Fixed Code**
// ```cpp
// // Unmanaged C++ host shim, run before the CLR is loaded: managed code declares no P/Invoke
// int wmain() {
//    CoInitializeEx(nullptr, COINIT_MULTITHREADED);
//    CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
//                         RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE, nullptr);
//    return RunManagedEntryPoint();
// }
// ```
//</Desc>

//<HowToFix>
// Don't call them from managed code. Write an unmanaged C++ "shim" calling
// them before loading the CLR within the process, as explained here:
// https://stackoverflow.com/a/48545055/27194
//</HowToFix>

Security: all 39 items