C++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122<P><FONT face="Verdana, Geneva, Arial, Sans-serif">I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time.&nbsp; I've just recently had the cycles to investigate the warning&nbsp; The warning message is as follows</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">Warning&nbsp;CA2122 : Microsoft.Security : MyClass.Method():Void calls into Marshal.GetExceptionPointers():IntPtr which has a LinkDemand. By making this call, Marshal.GetExceptionPointers():IntPtr is indirectly exposed to user code. Review the following call stack that might expose a way to circumvent security protection: <BR>-&gt;System.Runtime.InteropServices.Marshal.GetExceptionPointers : IntPtr<BR>-&gt;MyClass.Method : Void<BR>...</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">MyClass is a managed class where Method calls a native static (for readable illustration purposes only) function; where the code is as follows:</FONT></P> <DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 10pt; BACKGROUND: cornsilk; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; COLOR: black; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: Courier New"> <P style="MARGIN: 0px; FONT-FAMILY: courier new"><SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> MethodImplementation() <SPAN style="COLOR: blue">throw</SPAN>(std::runtime_error &amp;)</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">{</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">throw</SPAN> std::runtime_error(<SPAN style="COLOR: maroon">"a message"</SPAN>);</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">}</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new"><SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new"><SPAN style="COLOR: blue">void</SPAN> MyClass::Method()</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">{</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">try</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; {</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">return</SPAN> MethodImplementation();</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; }</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">catch</SPAN>(std::runtime_error &amp;)</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; {</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; }</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">}</P></DIV> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">My concern isn't that FxCop/Code-Analysis is pumping out this message, it's the LinkDemand that catch(std::runtime_error) is forcing upon this method.</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">Assume for a moment that the method <EM>MethodImplementation</EM> is not within my control (cannot modify it)&nbsp;and it can obviously throw an std::runtime_error reference.&nbsp; While I can add a LinkDemand attribute to the <EM>Method</EM> method, strong-name its assembly, and either throw&nbsp;that assenbly&nbsp;in the GAC or manually give it FullTrust and the implications of this LinkDemand go away; but I don't think I should need to.&nbsp; There's are some architectural and policy&nbsp;issues to simply going FullTrust to swallow the "inherited" security demands of&nbsp;the&nbsp;<EM>Method</EM>&nbsp;method (like APTC); but this should all be an implementation detail, and the abstraction of C++ interop has leaked through to my implementation.</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">Any thoughts?</FONT></P>© 2009 Microsoft Corporation. All rights reserved.Thu, 19 Jun 2008 00:15:52 Ze5f93636-4ac4-42fd-ba6b-039dfbdbe016http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#e5f93636-4ac4-42fd-ba6b-039dfbdbe016http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#e5f93636-4ac4-42fd-ba6b-039dfbdbe016Peter Ritchiehttp://social.msdn.microsoft.com/Profile/en-US/?user=Peter%20RitchieC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122<P><FONT face="Verdana, Geneva, Arial, Sans-serif">I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time.&nbsp; I've just recently had the cycles to investigate the warning&nbsp; The warning message is as follows</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">Warning&nbsp;CA2122 : Microsoft.Security : MyClass.Method():Void calls into Marshal.GetExceptionPointers():IntPtr which has a LinkDemand. By making this call, Marshal.GetExceptionPointers():IntPtr is indirectly exposed to user code. Review the following call stack that might expose a way to circumvent security protection: <BR>-&gt;System.Runtime.InteropServices.Marshal.GetExceptionPointers : IntPtr<BR>-&gt;MyClass.Method : Void<BR>...</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">MyClass is a managed class where Method calls a native static (for readable illustration purposes only) function; where the code is as follows:</FONT></P> <DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 10pt; BACKGROUND: cornsilk; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; COLOR: black; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: Courier New"> <P style="MARGIN: 0px; FONT-FAMILY: courier new"><SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> MethodImplementation() <SPAN style="COLOR: blue">throw</SPAN>(std::runtime_error &amp;)</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">{</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">throw</SPAN> std::runtime_error(<SPAN style="COLOR: maroon">"a message"</SPAN>);</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">}</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new"><SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new"><SPAN style="COLOR: blue">void</SPAN> MyClass::Method()</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">{</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">try</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; {</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">return</SPAN> MethodImplementation();</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; }</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">catch</SPAN>(std::runtime_error &amp;)</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; {</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">//...</SPAN></P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">&nbsp;&nbsp;&nbsp; }</P> <P style="MARGIN: 0px; FONT-FAMILY: courier new">}</P></DIV> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">My concern isn't that FxCop/Code-Analysis is pumping out this message, it's the LinkDemand that catch(std::runtime_error) is forcing upon this method.</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">Assume for a moment that the method <EM>MethodImplementation</EM> is not within my control (cannot modify it)&nbsp;and it can obviously throw an std::runtime_error reference.&nbsp; While I can add a LinkDemand attribute to the <EM>Method</EM> method, strong-name its assembly, and either throw&nbsp;that assenbly&nbsp;in the GAC or manually give it FullTrust and the implications of this LinkDemand go away; but I don't think I should need to.&nbsp; There's are some architectural and policy&nbsp;issues to simply going FullTrust to swallow the "inherited" security demands of&nbsp;the&nbsp;<EM>Method</EM>&nbsp;method (like APTC); but this should all be an implementation detail, and the abstraction of C++ interop has leaked through to my implementation.</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif">Any thoughts?</FONT></P>Tue, 12 Sep 2006 15:52:11 Z2006-11-30T19:56:17Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#b343aef4-ec8d-4564-a76b-e65a347bf3behttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#b343aef4-ec8d-4564-a76b-e65a347bf3beAyman Shoukry - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Ayman%20Shoukry%20-%20MSFTC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122<p>Peter, is that still an open issue?</p> <address>Thanks,</address> <address>Ayman Shoukry</address> <address>VC++ Team</address>Mon, 16 Oct 2006 19:07:47 Z2006-10-16T19:07:47Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#6197b213-5eb1-42eb-aab5-3311c3422df7http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#6197b213-5eb1-42eb-aab5-3311c3422df7Peter Ritchiehttp://social.msdn.microsoft.com/Profile/en-US/?user=Peter%20RitchieC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122Hi Ayman. Last I heard, the Code Analysis team is still discussing this.Mon, 16 Oct 2006 19:19:32 Z2006-10-16T19:19:32Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#b529e0a2-653e-48f3-9bf9-10b8db512783http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#b529e0a2-653e-48f3-9bf9-10b8db512783Ayman Shoukry - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Ayman%20Shoukry%20-%20MSFTC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122<p>Thanks Peter!</p> <p>Just keep us updated once you have more details. If you need help from the my side (VC++ team), let me know.</p> <address>Thanks,</address> <address>Ayman Shoukry</address> <address>VC++ Team</address>Mon, 16 Oct 2006 20:38:20 Z2006-10-16T20:38:20Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#7e399c67-ae64-4e7d-8fbf-609e37086efdhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#7e399c67-ae64-4e7d-8fbf-609e37086efdBrian Kramerhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20KramerC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122Bump to top.Sun, 05 Nov 2006 03:37:24 Z2006-11-05T03:37:24Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#2f42e3c9-6986-4a87-a1dc-4efc3d442ca0http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#2f42e3c9-6986-4a87-a1dc-4efc3d442ca0Peter Ritchiehttp://social.msdn.microsoft.com/Profile/en-US/?user=Peter%20RitchieC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122<div class=quote><table width="85%"><tr><td class=txt4> <strong>Brian Kramer wrote:</strong></td></tr><tr><td class=quoteTable><table width="100%"><tr><td width="100%" valign=top class=txt4>Bump to top.</td></tr></table></td></tr></table></div>Hi Brian.  Are you looking for a resolution to this issue?  I've sort of relegated it to my &quot;suppressible&quot; list due to it's cross-team context.  I don't expect to see any new changes/fixes until well after Orcas.Sun, 05 Nov 2006 19:13:47 Z2006-11-05T19:13:47Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#f86b94fb-5bc5-42e9-888c-c7163c2fb3eehttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#f86b94fb-5bc5-42e9-888c-c7163c2fb3eeBrian Kramerhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20KramerC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122Hey Peter.  I'm just maintaining its visibility.  Looks like this is one of those threads that can't get resolved.Sun, 05 Nov 2006 20:17:35 Z2006-11-05T20:17:35Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#c1ca13b6-40bc-42d8-b23b-79ffd06af3e7http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#c1ca13b6-40bc-42d8-b23b-79ffd06af3e7Peter Ritchiehttp://social.msdn.microsoft.com/Profile/en-US/?user=Peter%20RitchieC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122Ayman is pretty good at keeping track of these things; but, there hasn't been a decision made on it that I've been made aware of (otherwise I would have added a comment of some sort).  I feel like it's a couple of issues myself.  The more important issue is that the C/C++ compiler is generating code that requires a LinkDemand even though it's within an UnmanagedCode permission context.  Getting rid of that (by pushing the LinkDemand into a system DLL and wrapping it with an assert) would get around the warning.  The other issue is the warning, some feel the warning about a call to a method with a LinkDemand for UnmanagedCode shouldn't be raised when the assembly has a SecurityPermission attribute for UnmanagedCode.  In this case the assembly has [assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)] and there is still a warning that GetExceptionPointers has a LinkDemand for UnmanagedCode...  I've sent a fresh ping about the issue and I'll post any pertinent (disclosable) information.Mon, 06 Nov 2006 01:59:21 Z2006-11-30T19:56:17Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#1b62ae4a-bc3f-4d30-8d42-34056248794dhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e5f93636-4ac4-42fd-ba6b-039dfbdbe016#1b62ae4a-bc3f-4d30-8d42-34056248794dAyman Shoukry - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Ayman%20Shoukry%20-%20MSFTC++/CLI C++ Interop and FxCop/Code-Analysis warning CA2122<p>Hi Peter,</p> <p>Hopefully, you already got some feedback. Please unmark the previous post if the issue is still open or you can just add any updates an then unmark the thread.</p> <address>Thanks,</address> <address>Ayman Shoukry</address> <address>VC++ Team</address>Thu, 30 Nov 2006 19:56:12 Z2006-11-30T19:56:12Z