<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>素包子 &#187; prefast</title>
	<atom:link href="http://baoz.net/tag/prefast/feed/" rel="self" type="application/rss+xml" />
	<link>http://baoz.net</link>
	<description>己所不欲，勿劝他人</description>
	<lastBuildDate>Wed, 08 Sep 2010 12:33:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Static Analysis Tools and the SDL (Part One)</title>
		<link>http://baoz.net/static-analysis-tools-and-the-sdl-part-one/</link>
		<comments>http://baoz.net/static-analysis-tools-and-the-sdl-part-one/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 05:41:59 +0000</pubDate>
		<dc:creator>baoz</dc:creator>
				<category><![CDATA[技术点滴]]></category>
		<category><![CDATA[prefast]]></category>
		<category><![CDATA[SDL]]></category>
		<category><![CDATA[static analysis]]></category>

		<guid isPermaLink="false">http://baoz.net/?p=2818</guid>
		<description><![CDATA[<p>howard，多本产品安全书籍的作者，发了个讲静态分析的文章，相当值得一读。静态分析几乎是SDL过程中最困难的一个过程，合适的工具很难找，自己开发风险又太高。从找工具，到测试，到推广，到落实，每一步都非常坎坷……但是一旦走出去，对相关产品质量的影响是相当深远的。</p>
<p><span id="more-2818"></span></p>
<p>Hi, Michael here.</p>
<p>This is part one of a two part series of posts by myself and Bryan Sullivan; I will cover the static analysis tools we use at Microsoft (and make available publicly) for analyzing unmanaged (ie; Native) C and C++ code, and Bryan will cover managed code static analysis in a later post.</p>
<p>I’m a huge fan of static analysis tools; actually, I’m a fan of <em>any</em> tooling that beneficially automates any portion of the software development process. Software development is a complex business, and anything you can do to make the process more repeatable, predictable and reduces ‘friction’ is a big win in my book.</p>
<p>There are many benefits to using static analysis tools. The most important reasons include:</p>
<ul>
<li>Static analysis tools can scale: they can review a great deal of code very quickly; this is something humans cannot do very well.</li>
<li>Static analysis tools don’t get tired. A static analysis tool running for four straight hours at 2:00 in the morning is just as effective as if it runs during business hours. You can’t say the same thing about human reviewers!</li>
<li>Static analysis tools help developers learn about security vulnerabilities. Over the years, I’ve met a small number of developers who had bugs flagged in their code by static analysis tools, and they never knew what the bugs were until the tool posted a sign saying, “Security bug, right here!”</li>
</ul>
<p>Before I dive into static analysis tools in detail, it’s worthwhile explaining what ‘static analysis’ is. Static analysis is a method of analyzing program code without actually running the code. Generally, the tool will build an internal model of the code and analyze potential program flow through the code making assumptions about the data. For example, the following code may or may not be a real vulnerability:</p>
<p><span style="font-family: Courier New;">char foo[4];<br />
</span><span style="font-family: Courier New;">foo[i] = 0;</span></p>
<p>because it depends on the value of ‘i’; if ‘i’ is in the range 0..3 and can only ever be in the range 0..3 then there is no security vulnerability, so the static analysis tool has to determine if this condition is possible. Clearly, it’s simple to determine that the following code is safe, because the index is constrained right next to the code that writes to the array:</p>
<p><span style="font-family: Courier New;">char foo[4];<br />
</span><span style="font-family: Courier New;">if (i&gt;=0 &amp;&amp; i&lt;=3) foo[i] = 0;</span></p>
<p>But things get more complex if the index is validated in remote parts of the code. It is this level of analysis that determines if a tool is noisy: a tool that flags too many issues (false positives) because it missed a validity check will rapidly annoy a developer.</p>
<p>I want to point out that static analysis is not grep, static analysis tends to be more robust. That does not mean grep is not useful, for example, if you have a set of banned functionality such as banning MD4 and MD5 (as the SDL does, along with other crypto algorithms) then grep’ing for MD4 and MD5 is totally valid, probably low noise and requires next to zero engineering effort.</p>
<p>I also want to point out that the SDL focuses on using static analysis tools to find security vulnerabilities. Under the SDL umbrella, we would not require development teams use static analysis tools that didn’t find security issues. A tool that does not find security bugs is not a useless tool; we just would not make it an SDL requirement.</p>
<p>It’s important to point out that static analysis tools work in tandem with human code reviewing experts. Tools tend to find a lot of bugs quickly, but expert code reviewers are better at finding a smaller number of hard-to-find security bugs. I <a href="http://www.computer.org/portal/site/security/menuitem.6f7b2414551cb84651286b108bcd45f3/index.jsp?&amp;pName=security_level1_article&amp;TheCat=1001&amp;path=security/2006/v4n4&amp;file=basic.xml&amp;">wrote an article</a> for IEEE Security &amp; Privacy a few years back describing the methods I use to review code for security bugs.</p>
<p>Static analysis tools have been used for many years at Microsoft. We started in earnest with a tool named PREfix when we acquired Intrinsa. PREfix is aimed at finding general code quality bugs in C and C++ and has proven very effective over the years. The main downside to PREfix is it is big, and generally is run centrally rather than each developers’ desktop. So PREfix begat PREfast, a smaller desktop version of PREfix. PREfast has the advantage of being relatively quick to run (it <span style="text-decoration: underline;">only</span> doubles compile times!) but it suffers from only being intra-procedural; in other words, its view of your code is very small, while PREfix is inter-procedural and can evaluate conditions in far-flung regions of your code. If you need to know why that’s important, refer to the example code above!</p>
<p>PREfix and PREfast both support the Standard Annotation Language (SAL) which I have addressed a <a href="http://blogs.msdn.com/sdl/archive/2009/06/11/a-declspec-sal-to-attribute-sal-rosetta-stone.aspx">couple of times in the past</a>. SAL allows you to describe function contract semantics to help tools like PREfix and PREfast find more security bugs. SAL is used throughout Visual C++.</p>
<p>PREfast is available in Visual C++ today as the /analyze option, it’s also freely available in the Windows Device Driver Kit (as prefast.exe) and Software Development Kit (as /analyze).</p>
<h3>What You Should Do</h3>
<p>If you write native C or C++ code, you should:</p>
<ul>
<li>Compile at least once a day with /analyze</li>
<li>Use SAL to annotate your function prototypes, this will help the static analysis functionality in the compiler find many more bugs.</li>
</ul>
<p>The following warnings should be analyzed, as they are probably security issues:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/aa935142.aspx">6029</a> <a href="http://msdn.microsoft.com/en-us/library/a5b9aa09(VS.80).aspx">6053</a> <a href="http://msdn.microsoft.com/en-us/library/ms182080.aspx">6054</a> <a href="http://msdn.microsoft.com/en-us/library/t9a67d2b.aspx">6057</a> <a href="http://msdn.microsoft.com/en-us/library/2b5wde95.aspx">6059</a> <a href="http://msdn.microsoft.com/en-us/library/wkw5tfd8.aspx">6063</a> <a href="http://msdn.microsoft.com/en-us/library/b760t248.aspx">6067</a> <a href="http://msdn.microsoft.com/en-us/library/ms182081.aspx">6200</a> <a href="http://msdn.microsoft.com/en-us/library/11ckc29k.aspx">6201</a> <a href="http://msdn.microsoft.com/en-us/library/6cb2bae4.aspx">6202</a> <a href="http://msdn.microsoft.com/en-us/library/77w7wbyc.aspx">6203</a> <a href="http://msdn.microsoft.com/en-us/library/7exfe3st.aspx">6204</a> <a href="http://msdn.microsoft.com/en-us/library/e7ca7stt.aspx">6209</a> <a href="http://msdn.microsoft.com/en-us/library/c5se1z6d.aspx">6248</a> <a href="http://msdn.microsoft.com/en-us/library/4b4tecce.aspx">6277</a> <a href="http://msdn.microsoft.com/en-us/library/x8726e9z.aspx">6298</a> <a href="http://msdn.microsoft.com/en-us/library/aeh4k13s.aspx">6305</a> <a href="http://msdn.microsoft.com/en-us/library/kkedhy7c.aspx">6308</a> <a href="http://msdn.microsoft.com/en-us/library/ms182086.aspx">6383</a></p>
<p>Finally, for extra credit, look for the following warnings that are generated by the compiler and not by the static analysis tools:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/axhfhh6x.aspx">4700</a> and <a href="http://msdn.microsoft.com/en-us/library/1wea5zwe.aspx">4701</a></p>
<p>Both of these relate to uninitialized data and to enable these warnings either compile with warning level 4 (/W4), or if you’re not daring enough, use /W3 augmented with the following:</p>
<p>/W3 /WX /we4701/we4700</p>
<h3>The SDL Optimization Model and Static Analysis</h3>
<p>If you’re following the <a href="http://msdn.microsoft.com/en-us/security/dd221356.aspx">SDL Optimization model</a>, use of static analysis tools is deemed a requirement for the ‘Advanced’ maturity level.</p>
<h3>Summary</h3>
<p>In summary, the SDL mandates static analysis tools for C and C++ code. If you are currently not using static analysis tools in your development environment, you should. If you’ve never run static analysis tools then the chances are good you’ll find some ‘interesting’ bugs!</p>
]]></description>
		<wfw:commentRss>http://baoz.net/static-analysis-tools-and-the-sdl-part-one/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What PREfast Can Detect</title>
		<link>http://baoz.net/what-prefast-can-detect/</link>
		<comments>http://baoz.net/what-prefast-can-detect/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 09:17:25 +0000</pubDate>
		<dc:creator>baoz</dc:creator>
				<category><![CDATA[技术点滴]]></category>
		<category><![CDATA[prefast]]></category>
		<category><![CDATA[SDL]]></category>
		<category><![CDATA[安全开发生命周期]]></category>

		<guid isPermaLink="false">http://baoz.net/?p=1348</guid>
		<description><![CDATA[<p class="BodyTextLink" style="margin: 0cm 0cm 4pt;"><span lang="EN-US"><span style="font-size: x-small; font-family: Arial;">PRE<span class="Italic"><em>f</em></span>ast can detect several significant categories of potential errors in your code as soon as you can compile it, including the following:</span></span></p>
<p class="BodyTextLink" style="margin: 0cm 0cm 4pt;"><span lang="EN-US"><span style="font-size: x-small; font-family: Arial;"><span id="more-1348"></span></span></span></p>
<p class="BulletList" style="margin: 0cm 0cm 4pt 18pt;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: x-small;">·</span><span style="font: 7pt &quot;Times New Roman&quot;;">         </span></span></span><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US"><span style="font-size: x-small; font-family: Arial;">Memory</span></span></strong><span lang="EN-US"><br />
<span style="font-size: x-small; font-family: Arial;">Potential memory leaks, dereferenced NULL pointers, access to uninitialized memory, excessive use of the kernel-mode stack, and improper use of pool tags.</span></span></p>
<p class="BulletList" style="margin: 0cm 0cm 4pt 18pt;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: x-small;">·</span><span style="font: 7pt &quot;Times New Roman&quot;;">         </span></span></span><span style="font-size: x-small;"><span style="font-family: Arial;"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US">Resources<br />
</span></strong><span lang="EN-US">Failure to release resources such as locks, resources that a function holds when it should not, and resources that a function incorrectly fails to hold when it should.</span></span></span></p>
<p class="BulletList" style="margin: 0cm 0cm 4pt 18pt;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: x-small;">·</span><span style="font: 7pt &quot;Times New Roman&quot;;">         </span></span></span><span style="font-size: x-small;"><span style="font-family: Arial;"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US">Function usage<br />
</span></strong><span lang="EN-US">Potentially incorrect usage of certain functions, function arguments that appear to be incorrect, possible argument type mismatches for functions that do not strictly check types, possible use of certain obsolete functions, and function calls at a potentially incorrect interrupt request (IRQL).</span></span></span></p>
<p class="BulletList" style="margin: 0cm 0cm 4pt 18pt;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: x-small;">·</span><span style="font: 7pt &quot;Times New Roman&quot;;">         </span></span></span><span style="font-size: x-small;"><span style="font-family: Arial;"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US">Floating-point state<br />
</span></strong><span lang="EN-US">Failure to protect floating-point hardware state in a driver and attempting to restore floating-point state after saving it at a different IRQL.</span></span></span></p>
<p class="BulletList" style="margin: 0cm 0cm 4pt 18pt;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: x-small;">·</span><span style="font: 7pt &quot;Times New Roman&quot;;">         </span></span></span><span style="font-size: x-small;"><span style="font-family: Arial;"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US">Precedence rules<br />
</span></strong><span lang="EN-US">Code that might not behave as the programmer intended because of the precedence rules of C.</span></span></span></p>
<p class="BulletList" style="margin: 0cm 0cm 4pt 18pt;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: x-small;">·</span><span style="font: 7pt &quot;Times New Roman&quot;;">         </span></span></span><span style="font-size: x-small;"><span style="font-family: Arial;"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US">Kernel-mode coding practices<br />
</span></strong><span lang="EN-US">Coding practices that can cause errors, such as modifying an opaque memory descriptor list (MDL) structure, failing to examine the value of a variable set by a called function, using C runtime library string manipulation functions rather than the safe string functions that are defined in Ntstrsafe.h, and some misuses of pageable code segments.</span></span></span></p>
<p class="BulletList" style="margin: 0cm 0cm 4pt 18pt;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;" lang="EN-US"><span style="mso-list: Ignore;"><span style="font-size: x-small;">·</span><span style="font: 7pt &quot;Times New Roman&quot;;">         </span></span></span><span style="font-size: x-small;"><span style="font-family: Arial;"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US">Driver-specific coding practices<br />
</span></strong><span lang="EN-US">Specific operations that are often a source of errors in kernel-mode drivers, such as copying a whole I/O request packet (IRP) without modifying members or saving a pointer to a string or structure argument instead of copying an argument in a <span class="Bold"><strong>DriverEntry</strong></span> routine.</span></span></span></p>
<p class="Le" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: x-small; font-family: Arial;"> </span></span></p>
<div style="border-right: medium none; padding-right: 0cm; border-top: windowtext 1pt solid; padding-left: 0cm; padding-bottom: 1pt; border-left: medium none; padding-top: 1pt; border-bottom: windowtext 1pt solid; mso-border-top-alt: solid windowtext .75pt; mso-border-bottom-alt: solid windowtext .75pt; mso-element: para-border-div;">
<p class="Note" style="margin: 8pt 0cm;"><span style="font-size: x-small;"><span style="font-family: Arial;"><span class="Bold"><span lang="EN-US"><strong>Important <span style="mso-spacerun: yes;"> </span></strong></span></span><span lang="EN-US">PRE<span class="Italic"><em>f</em></span>ast is highly effective at detecting many errors that are difficult to find by other means, and it usually reports errors in a way that makes them easier to fix. This helps to free your test resources to concentrate on finding and fixing deeper, more significant bugs. However, PRE<span class="Italic"><em>f</em></span>ast does not find every possible error or even all possible instances of the errors it was designed to detect, so passing PRE<em style="mso-bidi-font-style: normal;">f</em>ast does not necessarily mean that your code is free of errors. Be sure to thoroughly test your code with all available tools, including Driver Verifier and Static Driver Verifier. For availability of these tools, see “Resources” at the end of this paper. </span></span></span></p>
</div>
]]></description>
		<wfw:commentRss>http://baoz.net/what-prefast-can-detect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
