<?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>Safer Code - Secure Coding In C \ C++ And More.. &#187; Security</title>
	<atom:link href="http://www.safercode.com/blog/category/security/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Tue, 05 Jan 2010 07:59:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Large Arrays In C</title>
		<link>http://www.safercode.com/blog/2009/08/24/large-arrays-in-c.html</link>
		<comments>http://www.safercode.com/blog/2009/08/24/large-arrays-in-c.html#comments</comments>
		<pubDate>Sun, 23 Aug 2009 20:09:19 +0000</pubDate>
		<dc:creator>Shantanu Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[BSS]]></category>
		<category><![CDATA[C CPP]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[Large Arrays]]></category>
		<category><![CDATA[Programming Pearls]]></category>
		<category><![CDATA[Stack Overflo]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/2009/08/24/large-arrays-in-c.html</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Most C programmers, even beginners, would claim that arrays are easy, except those abundant off-by-one errors and they are right. Arrays are easy indeed. However, here are a few points to consider when writing a program that needs a rather large array (By the way, the thought for this article came into mind while helping [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Most C programmers, even beginners, would claim that arrays are easy, except those abundant off-by-one errors and they are right. Arrays are easy indeed. However, here are a few points to consider when writing a program that needs a rather large array (By the way, the thought for this article came into mind while helping a colleague to resolve an error a couple of days ago). When creating a large array (e.g. something like int a[1000][1000], which BTW takes up around 3.8 MB on most machines), you might not have any issue at all or might see either a compile time error or worse, runtime errors.<br />Why do these errors happen? Depending on your machine&#8217;s limitations you are most probably consuming the total available stack (if you used a local variable) or data memory. Depending on your compiler, these might be flagged at compile time or surface when you try to run your program. Or it might be that your compiler isn&#8217;t able to work with large arrays. What you can do to alleviate these:
<ul>
<li>The first and best way is to minimize your array size (This makes for an amazing example here from the bookd &#8220;Programming Peals&#8221;: http://www.cs.bell-labs.com/cm/cs/pearls/cto.html ) </li>
<li>Use &#8220;huge&#8221; memory model.</li>
<li>Use a global variable (or a static variable if you are particular about its visibility to the rest of the program). The stack is generally quite limited as compared to the data memory available to a program. So, a variable with static storage would ensure that you use memory from the data segment.</li>
<li>Don&#8217;t declare it as an array at all and instead use a pointer and dynamically allocate the memory required for it. You might have to use special allocation calls instead of normal malloc/calloc to get this working though (e.g. using farmalloc)</li>
<li>Create a section in assembly with the &#8220;Area&#8221; directive (or whatever it is for your particular assembler) and reserve space for your array there and refer to that array as an extern variable.</li>
</ul>
<p>The first approach is something that you should always look for. But if you can&#8217;t mimize your need any further, choose one out of the rest two. But few things to be kept in mind here that these options can still fail, e.g., when you don&#8217;t have enough RAM/heap memory remaining at runtime (of course, you would have planned for a graceful exit though in such case instead of the random crash that would have occured otherwise). But still these could be useful to you in case you don&#8217;t have any real RAM limitations but just that your compiler isn&#8217;t able to work with large objects.
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2009/08/24/large-arrays-in-c.html">Large Arrays In C</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F08%2F24%2Flarge-arrays-in-c.html&amp;title=Large%20Arrays%20In%20C&amp;bodytext=Most%20C%20programmers%2C%20even%20beginners%2C%20would%20claim%20that%20arrays%20are%20easy%2C%20except%20those%20abundant%20off-by-one%20errors%20and%20they%20are%20right.%20Arrays%20are%20easy%20indeed.%20However%2C%20here%20are%20a%20few%20points%20to%20consider%20when%20writing%20a%20program%20that%20needs%20a%20rather%20large%20arra" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F08%2F24%2Flarge-arrays-in-c.html&amp;title=Large%20Arrays%20In%20C&amp;notes=Most%20C%20programmers%2C%20even%20beginners%2C%20would%20claim%20that%20arrays%20are%20easy%2C%20except%20those%20abundant%20off-by-one%20errors%20and%20they%20are%20right.%20Arrays%20are%20easy%20indeed.%20However%2C%20here%20are%20a%20few%20points%20to%20consider%20when%20writing%20a%20program%20that%20needs%20a%20rather%20large%20arra" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F08%2F24%2Flarge-arrays-in-c.html&amp;t=Large%20Arrays%20In%20C" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F08%2F24%2Flarge-arrays-in-c.html&amp;title=Large%20Arrays%20In%20C" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F08%2F24%2Flarge-arrays-in-c.html&amp;title=Large%20Arrays%20In%20C" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F08%2F24%2Flarge-arrays-in-c.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/bss" title="BSS" rel="tag nofollow">BSS</a>, <a href="http://www.safercode.com/blog/tag/c-cpp" title="C CPP" rel="tag nofollow">C CPP</a>, <a href="http://www.safercode.com/blog/tag/heap" title="heap" rel="tag nofollow">heap</a>, <a href="http://www.safercode.com/blog/tag/large-arrays" title="Large Arrays" rel="tag nofollow">Large Arrays</a>, <a href="http://www.safercode.com/blog/tag/programming-pearls" title="Programming Pearls" rel="tag nofollow">Programming Pearls</a>, <a href="http://www.safercode.com/blog/tag/stack-overflo" title="Stack Overflo" rel="tag nofollow">Stack Overflo</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html" title="int main() vs void main() (October 14, 2008)">int main() vs void main()</a> (22)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2009/08/24/large-arrays-in-c.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>&#8220;De-Bugging&#8221; Code before Check-in</title>
		<link>http://www.safercode.com/blog/2009/06/10/de-bugging-code-before-check-in.html</link>
		<comments>http://www.safercode.com/blog/2009/06/10/de-bugging-code-before-check-in.html#comments</comments>
		<pubDate>Tue, 09 Jun 2009 19:30:58 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[safety and security]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=41</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Even an expert programmer cannot claim of writing bug free code. Bugs are here to stay during a software development life cycle. But what every programmer needs to do is to test his code before the code goes into the main repository. So, programmers have different techniques to do this. Running Test cases, getting code [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Even an expert programmer cannot claim of writing bug free code. Bugs are here to stay during a software development life cycle. But what every programmer needs to do is to test his code before the code goes into the main repository. So, programmers have different techniques to do this. Running Test cases, getting code reviewed, code walk through, running manual tests, ad-hoc tests are various things performed by people and Bang!!! code goes into the repository. Let&#8217;s consider the following psuedo-code:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span><span style="color: #339933;">*</span> someString <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>someString <span style="color: #339933;">!=</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// handle error condition</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-41"></span><br />
Now, If you try to test the above code using the above mentioned testing techniques, you can never be sure that you have covered 100% code in developer testing. The code definitely seems to be fine handling the error condition if the memory allocation fails. But have you actually tested the error condition handling? Generally, all the automated test cases or running the code will make the memory allocation successful and the error condition will never get executed. Even the code reviewers will pass the code without any objection as the code is handling memory allocation failure. But you haven&#8217;t made sure that error handling is safe or not. The Bug might be in error handling scenario. </p>
<p>So, One of the best way to &#8220;de-Bug&#8221; or test your code is always to step through your code. Think that you were stepping through your code using a debugger. Now, put a breakpoint at malloc and set someString to NULL right after memory allocation is done. This will enable your error condition code to be executed and you can claim 100% coverage of testing your code before checking it in the repository. </p>
<p>The next word of advise is that not only error conditions, one should check all the possible execution paths in the code. Consider the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> someInt <span style="color: #339933;">=</span> <span style="color: #0000dd;">30</span><span style="color: #339933;">;</span>
<span style="color: #993333;">char</span> someChar <span style="color: #339933;">=</span> <span style="color: #ff0000;">'c'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> someInt<span style="color: #339933;">=</span> <span style="color: #0000dd;">32</span> <span style="color: #339933;">&amp;&amp;</span> someChar<span style="color: #339933;">==</span><span style="color: #ff0000;">'c'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// handle error condition</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you notice, I have made a mistake in the code by typing &#8216;=&#8217; in place of &#8216;==&#8217; in the first part of &#8216;if&#8217; statement. So, even if you run test cases or use debugger, the code will easily execute &#8216;if&#8217; statement in one shot and will evaulate to true as first and second condition will always evaluate to &#8216;TRUE&#8217;. and hence, leaving a hole in the code. The error condition will never occur in this case. This can only be caught if while using a debugger, put a breakpoint inside &#8216;if&#8217; statement, and then verify both the conditions on either side of &#8216;&#038;&#038;&#8217; operator. This will lead you to find the anomaly in the code and hence, you&#8217;ll be able to kill a potential showstopper bug instantly. </p>
<p>In a nutshell, I wish to emphasize on a point is that debuggers are not meant only to be used when the defects are raised in defect trackign systems, they should be used during developmental testing of the code. I agree that this consumes a hell lot of extra time in development but isn&#8217;t it beneficial to be safe in the beginning rather than wasting time on these small issues after the software is released and have already translated into bigger crash bugs.</p>
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2009/06/10/de-bugging-code-before-check-in.html">&#8220;De-Bugging&#8221; Code before Check-in</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F06%2F10%2Fde-bugging-code-before-check-in.html&amp;title=%22De-Bugging%22%20Code%20before%20Check-in&amp;bodytext=Even%20an%20expert%20programmer%20cannot%20claim%20of%20writing%20bug%20free%20code.%20Bugs%20are%20here%20to%20stay%20during%20a%20software%20development%20life%20cycle.%20But%20what%20every%20programmer%20needs%20to%20do%20is%20to%20test%20his%20code%20before%20the%20code%20goes%20into%20the%20main%20repository.%20So%2C%20programmers%20" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F06%2F10%2Fde-bugging-code-before-check-in.html&amp;title=%22De-Bugging%22%20Code%20before%20Check-in&amp;notes=Even%20an%20expert%20programmer%20cannot%20claim%20of%20writing%20bug%20free%20code.%20Bugs%20are%20here%20to%20stay%20during%20a%20software%20development%20life%20cycle.%20But%20what%20every%20programmer%20needs%20to%20do%20is%20to%20test%20his%20code%20before%20the%20code%20goes%20into%20the%20main%20repository.%20So%2C%20programmers%20" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F06%2F10%2Fde-bugging-code-before-check-in.html&amp;t=%22De-Bugging%22%20Code%20before%20Check-in" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F06%2F10%2Fde-bugging-code-before-check-in.html&amp;title=%22De-Bugging%22%20Code%20before%20Check-in" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F06%2F10%2Fde-bugging-code-before-check-in.html&amp;title=%22De-Bugging%22%20Code%20before%20Check-in" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F06%2F10%2Fde-bugging-code-before-check-in.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/bugs" title="bugs" rel="tag nofollow">bugs</a>, <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/crash" title="crash" rel="tag nofollow">crash</a>, <a href="http://www.safercode.com/blog/tag/debugging" title="debugging" rel="tag nofollow">debugging</a>, <a href="http://www.safercode.com/blog/tag/efficiency" title="Efficiency" rel="tag nofollow">Efficiency</a>, <a href="http://www.safercode.com/blog/tag/languages" title="Languages" rel="tag nofollow">Languages</a>, <a href="http://www.safercode.com/blog/tag/programming" title="programming" rel="tag nofollow">programming</a>, <a href="http://www.safercode.com/blog/tag/safety-and-security" title="safety and security" rel="tag nofollow">safety and security</a>, <a href="http://www.safercode.com/blog/tag/security" title="Security" rel="tag nofollow">Security</a>, <a href="http://www.safercode.com/blog/tag/testing" title="testing" rel="tag nofollow">testing</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/10/14/and-so-it-begins.html" title="And So It Begins&#8230; (October 14, 2008)">And So It Begins&#8230;</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html" title="Improper Variable Initialization (January 13, 2009)">Improper Variable Initialization</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html" title="Predicting the rand() and using Cryptographic Random Numbers (February 10, 2009)">Predicting the rand() and using Cryptographic Random Numbers</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html" title="int main() vs void main() (October 14, 2008)">int main() vs void main()</a> (22)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/18/all-input-is-evil.html" title="All Input is Evil (November 18, 2008)">All Input is Evil</a> (3)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2009/06/10/de-bugging-code-before-check-in.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lint your code: Find probable mistakes much before testing</title>
		<link>http://www.safercode.com/blog/2009/03/23/lint-your-code-find-probable-mistakes-much-before-testing.html</link>
		<comments>http://www.safercode.com/blog/2009/03/23/lint-your-code-find-probable-mistakes-much-before-testing.html#comments</comments>
		<pubDate>Mon, 23 Mar 2009 18:15:16 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[buffer overflow]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Flex Lint]]></category>
		<category><![CDATA[LINT]]></category>
		<category><![CDATA[MISRA]]></category>
		<category><![CDATA[PC Lint]]></category>
		<category><![CDATA[Safety]]></category>
		<category><![CDATA[warnings]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=37</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Every programmer, no matter how great he is, makes mistakes sometime or the other while coding. Although every compiler tries its best to put across every possible error during compilation,many mistakes skip the wrath of compiler. Some are seemingly very innocent and very tough to be caught even during code review, sometimes even get through [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Every programmer, no matter how great he is, makes mistakes sometime or the other while coding. Although every compiler tries its best to put across every possible error during compilation,many mistakes skip the wrath of compiler. Some are seemingly very innocent and very tough to be caught even during code review, sometimes even get through the cycle of testing. The real face of these mistakes show up always on the customer side by crashing the system.</p>
<p>Consider the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> multiply<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> result <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	result <span style="color: #339933;">=</span> m <span style="color: #339933;">*</span> n<span style="color: #339933;">;</span>	
	<span style="color: #b1b100;">return</span> 	result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> func<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> m <span style="color: #339933;">=</span> <span style="color: #0000dd;">32767</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> n <span style="color: #339933;">=</span> <span style="color: #0000dd;">32767</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> result <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	result <span style="color: #339933;">=</span> multiply<span style="color: #009900;">&#40;</span> m<span style="color: #339933;">,</span> n <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-37"></span><br />
In this example, if you notice, the result always overrun the maximum value of an integer (int being of 16 bits). Now, for any compiler, this code seems to be perfect. But if you lint this code, the lint tool will definitely raise a warning about this potential bug. This bug if overlooked, can cause havoc in any system in crucial scenarios. </p>
<p>Similarly, there are many more example like these which can be caught while linting the code. Quite a few significant but obvious problems like buffer overrun, array index out of bounds, uninitialized variables causing junk in junk out can be caught using any of the good lint tools. This process of linting makes the code safe, secure and strong enough to withstand any kind of malicious input injections or buffer overrun attacks. Ofcourse, the complex scenarios can get skipped by some of the tools but still, it definitely is a better steo to catch the bug early. Quite a few tools are available in the market but i&#8217;ll recommend a tools can PC-Lint(Windows)/FlexLint(Linux). This tool is pretty good as it catches almost every obvious flaw which gets skipped by the developers or code reviewers eyes. It follows the guidelines given in MISRA (Motor Industry Software Reliability Assocation)standard and strictly adhers to that.</p>
<p>These linting tools generally have their properietary algorithms but in general, they all follow the same approach of static analysis of source code. Following are examples of some of the problems which these tools are capable of finding during the lint process.</p>
<li>Accidental assignment (= compared with ==)</li>
<li>Bad pointer arithmetic</li>
<li>Accidental booleans</li>
<li>Bad use of macros</li>
<li>Use of undefined external methods (ST20 compiler assumes int func(void))</li>
<li>Uninitialised variables</li>
<li>Unsafe array usage</li>
<li>Signed/unsigned data type mix-ups</li>
<li>Bad use of casts</li>
<li>Memory leaks (over-use of CMM and API heap). Too much reliance on dynamic memory allocation.</li>
<p>Linting your code during development is very important as it can make your code much safer. It definitely does add to the build time and it might take few extra seconds to get the final object file, but isn&#8217;t it worth the hassle if you are saved from deadly bugs?
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2009/03/23/lint-your-code-find-probable-mistakes-much-before-testing.html">Lint your code: Find probable mistakes much before testing</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F23%2Flint-your-code-find-probable-mistakes-much-before-testing.html&amp;title=Lint%20your%20code%3A%20Find%20probable%20mistakes%20much%20before%20testing&amp;bodytext=Every%20programmer%2C%20no%20matter%20how%20great%20he%20is%2C%20makes%20mistakes%20sometime%20or%20the%20other%20while%20coding.%20Although%20every%20compiler%20tries%20its%20best%20to%20put%20across%20every%20possible%20error%20during%20compilation%2Cmany%20mistakes%20skip%20the%20wrath%20of%20compiler.%20Some%20are%20seemingly%20" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F23%2Flint-your-code-find-probable-mistakes-much-before-testing.html&amp;title=Lint%20your%20code%3A%20Find%20probable%20mistakes%20much%20before%20testing&amp;notes=Every%20programmer%2C%20no%20matter%20how%20great%20he%20is%2C%20makes%20mistakes%20sometime%20or%20the%20other%20while%20coding.%20Although%20every%20compiler%20tries%20its%20best%20to%20put%20across%20every%20possible%20error%20during%20compilation%2Cmany%20mistakes%20skip%20the%20wrath%20of%20compiler.%20Some%20are%20seemingly%20" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F23%2Flint-your-code-find-probable-mistakes-much-before-testing.html&amp;t=Lint%20your%20code%3A%20Find%20probable%20mistakes%20much%20before%20testing" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F23%2Flint-your-code-find-probable-mistakes-much-before-testing.html&amp;title=Lint%20your%20code%3A%20Find%20probable%20mistakes%20much%20before%20testing" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F23%2Flint-your-code-find-probable-mistakes-much-before-testing.html&amp;title=Lint%20your%20code%3A%20Find%20probable%20mistakes%20much%20before%20testing" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F23%2Flint-your-code-find-probable-mistakes-much-before-testing.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/buffer-overflow" title="buffer overflow" rel="tag nofollow">buffer overflow</a>, <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/flex-lint" title="Flex Lint" rel="tag nofollow">Flex Lint</a>, <a href="http://www.safercode.com/blog/tag/lint" title="LINT" rel="tag nofollow">LINT</a>, <a href="http://www.safercode.com/blog/tag/misra" title="MISRA" rel="tag nofollow">MISRA</a>, <a href="http://www.safercode.com/blog/tag/pc-lint" title="PC Lint" rel="tag nofollow">PC Lint</a>, <a href="http://www.safercode.com/blog/tag/safety" title="Safety" rel="tag nofollow">Safety</a>, <a href="http://www.safercode.com/blog/tag/warnings" title="warnings" rel="tag nofollow">warnings</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html" title="Validating Untrusted String Inputs (November 11, 2008)">Validating Untrusted String Inputs</a> (1)</li>
	<li><a href="http://www.safercode.com/blog/2008/12/02/unsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html" title="Unsafe Functions In C And Their Safer Replacements: Strings Part II (December 2, 2008)">Unsafe Functions In C And Their Safer Replacements: Strings Part II</a> (8)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/04/unsafe-functions-in-c-and-their-safer-replacements-strings-part-i.html" title="Unsafe Functions In C And Their Safer Replacements: Strings Part I (November 4, 2008)">Unsafe Functions In C And Their Safer Replacements: Strings Part I</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html" title="int main() vs void main() (October 14, 2008)">int main() vs void main()</a> (22)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2009/03/23/lint-your-code-find-probable-mistakes-much-before-testing.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Predicting the rand() and using Cryptographic Random Numbers</title>
		<link>http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html</link>
		<comments>http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html#comments</comments>
		<pubDate>Tue, 10 Feb 2009 16:08:19 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Concepts]]></category>
		<category><![CDATA[CryptGenRandom]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[rand]]></category>
		<category><![CDATA[Random Numbers]]></category>
		<category><![CDATA[security holes]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=31</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Everyone must have used rand() sometime or the other while writing C code. The problem with rand() in most of the platforms is that it is easy to predict the output. Being based on unsigned int, it is just a simple function using a seed which is always the last randomly generated some number. This [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Everyone must have used rand() sometime or the other while writing C code. The problem with rand() in most of the platforms is that it is easy to predict the output. Being based on <em>unsigned int</em>, it is just a simple function using a seed which is always the last randomly generated some number. This seed is not very tough to guess for an advanced hacker. once this seed is guessed,, any password or information based on random number generation can be easilt cracked and maligned.</p>
<p>following code is abridged code of rand() function implementation referenced from the book <em>The C programming Language written by Brian Kernighan and Dennis Ritchie </em></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #993333;">int</span> next <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> rand<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    next <span style="color: #339933;">=</span> next <span style="color: #339933;">*</span> <span style="color: #0000dd;">1103515245</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">12345</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>next <span style="color: #339933;">/</span> <span style="color: #0000dd;">65536</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000dd;">32768</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This type of function is generally called <em>linear congruential function</em>. As you can notice yourself, that these type of linear congruential functions are very much predictable and are not recommended for security sensitive applications. If you look at the above given code, it is obvious that if the underlying environment does not change, then the random number generation can easily be guessed as it will generate same random number on running the application again and again.</p>
<p>
<span id="more-31"></span></p>
<p>Any type of good random number generator should adhere to three basic properties:</p>
<ol>
<li>Generate evenly distributed numbers</li>
<li>The generated number should be unpredictable</li>
<li>It should use a long cycle of range of numbers</li>
</ol>
<p>Linear congruential functions just suffice the first property but fail miserably in the other two, thus, making themselves unusable for secure random number generation.In the early era of internet, people have used the rand() function, and hackers have exploited it at leisure.</p>
<p>To Solve this problem, most of the platforms support Cryptographic Random Numbers these days. For example, CryptGenRandom is provided by almost all the Windows platforms. These type of Cryptographic Random numbers are completely random as the seed used in them is also unknown to the programmer. It is completely random because it uses the system properties(system entropy) to generate the seed, which is then hashed used a hashing algorithm like SHA-1/MD4/MD5 etc. This implementation is completely platform dependent. For example: quite a few parameters used by Windows platforms are like</p>
<ol>
<li>Current Process ID</li>
<li>Current Thread ID</li>
<li>GetLocalTime()</li>
<li>CPU counters</li>
<li>Allocated Process Memmory at initial time</li>
<li>Page Fault count</li>
<li>etc etc etc&#8230;.</li>
</ol>
<p>Due to these large number of parameters and then, applying the hashing algorithms, the seed becomes almost random and unpredictable, thus, making it next to impossible to be decrypted.</p>
<p>Crypt Random Numbers and algorithms are part of FIPS (Federal Information Processing Standard) standard. The above mentioned function is a part of FIPS 186-2 standard.</p>
<p>Talking about Cryptographic Random numbers is a fairly vast topic and I would like to continue in my next post.
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html">Predicting the rand() and using Cryptographic Random Numbers</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F02%2F10%2Fpredicting-the-rand-and-using-cryptographic-random-numbers.html&amp;title=Predicting%20the%20rand%28%29%20and%20using%20Cryptographic%20Random%20Numbers&amp;bodytext=Everyone%20must%20have%20used%20rand%28%29%20sometime%20or%20the%20other%20while%20writing%20C%20code.%20The%20problem%20with%20rand%28%29%20in%20most%20of%20the%20platforms%20is%20that%20it%20is%20easy%20to%20predict%20the%20output.%20Being%20based%20on%20unsigned%20int%2C%20it%20is%20just%20a%20simple%20function%20using%20a%20seed%20which%20is%20alwa" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F02%2F10%2Fpredicting-the-rand-and-using-cryptographic-random-numbers.html&amp;title=Predicting%20the%20rand%28%29%20and%20using%20Cryptographic%20Random%20Numbers&amp;notes=Everyone%20must%20have%20used%20rand%28%29%20sometime%20or%20the%20other%20while%20writing%20C%20code.%20The%20problem%20with%20rand%28%29%20in%20most%20of%20the%20platforms%20is%20that%20it%20is%20easy%20to%20predict%20the%20output.%20Being%20based%20on%20unsigned%20int%2C%20it%20is%20just%20a%20simple%20function%20using%20a%20seed%20which%20is%20alwa" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F02%2F10%2Fpredicting-the-rand-and-using-cryptographic-random-numbers.html&amp;t=Predicting%20the%20rand%28%29%20and%20using%20Cryptographic%20Random%20Numbers" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F02%2F10%2Fpredicting-the-rand-and-using-cryptographic-random-numbers.html&amp;title=Predicting%20the%20rand%28%29%20and%20using%20Cryptographic%20Random%20Numbers" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F02%2F10%2Fpredicting-the-rand-and-using-cryptographic-random-numbers.html&amp;title=Predicting%20the%20rand%28%29%20and%20using%20Cryptographic%20Random%20Numbers" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F02%2F10%2Fpredicting-the-rand-and-using-cryptographic-random-numbers.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/concepts" title="Concepts" rel="tag nofollow">Concepts</a>, <a href="http://www.safercode.com/blog/tag/cryptgenrandom" title="CryptGenRandom" rel="tag nofollow">CryptGenRandom</a>, <a href="http://www.safercode.com/blog/tag/cryptography" title="Cryptography" rel="tag nofollow">Cryptography</a>, <a href="http://www.safercode.com/blog/tag/efficiency" title="Efficiency" rel="tag nofollow">Efficiency</a>, <a href="http://www.safercode.com/blog/tag/rand" title="rand" rel="tag nofollow">rand</a>, <a href="http://www.safercode.com/blog/tag/random-numbers" title="Random Numbers" rel="tag nofollow">Random Numbers</a>, <a href="http://www.safercode.com/blog/tag/security" title="Security" rel="tag nofollow">Security</a>, <a href="http://www.safercode.com/blog/tag/security-holes" title="security holes" rel="tag nofollow">security holes</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/10/14/and-so-it-begins.html" title="And So It Begins&#8230; (October 14, 2008)">And So It Begins&#8230;</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/18/all-input-is-evil.html" title="All Input is Evil (November 18, 2008)">All Input is Evil</a> (3)</li>
	<li><a href="http://www.safercode.com/blog/2008/12/16/using-enum-pattern-in-java-15.html" title="Using Enum Pattern in Java < 1.5 (December 16, 2008)">Using Enum Pattern in Java < 1.5</a> (1)</li>
	<li><a href="http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html" title="Improper Variable Initialization (January 13, 2009)">Improper Variable Initialization</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2009/06/10/de-bugging-code-before-check-in.html" title="&#8220;De-Bugging&#8221; Code before Check-in (June 10, 2009)">&#8220;De-Bugging&#8221; Code before Check-in</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Improper Variable Initialization</title>
		<link>http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html</link>
		<comments>http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html#comments</comments>
		<pubDate>Tue, 13 Jan 2009 05:21:08 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[buffer overflow]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[initialization]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[variable declaration]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=28</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Except for few good C programmers, others generally tend to ignore variable initialization or I should rather say “proper variable initialization”. Generally seen, the variable declaration itself is not done with a good thinking. Improper local variable initialization might not be good for the working of the program but improper global variable initialization might get [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Except for few good C programmers, others generally tend to ignore variable initialization or I should rather say “proper variable initialization”. Generally seen, the variable declaration itself is not done with a good thinking. Improper local variable initialization might not be good for the working of the program but improper global variable initialization might get your software or system hacked.</p>
<p>The uninitialized variable or a wrongly initialized variable might lead a program to change its normal course of flow from the intended one. For example: If a variable “index” is being used for array navigation and is left uninitialized, it might contain a garbage value which can lead to array index out of bounds error. or if the variable “index” is initialized wrongly to –1, it might lead to serious flaw in code flow. Even if an integer value is being initialized to ‘0’, it might lead to a security check bypass because for some programs, even a ‘0’ is considered a valid value.</p>
<p>Lets take an example of a code piece.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"> <span style="color: #993333;">int</span> isMachineRunning <span style="color: #339933;">=</span> GetMachineStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #993333;">int</span> state <span style="color: #339933;">=</span> GetUserState<span style="color: #009900;">&#40;</span>isMachineRunning<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #993333;">int</span> userid <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	userid <span style="color: #339933;">=</span> ExtractUserID<span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* do stuff */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>uid <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	DoAdminThings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-28"></span><br />
Now, in the above example, userId is initialized to &#8216;0&#8242;. and adminUserID is also equal to &#8216;0&#8242;. Consider that the GetUserState() function somehow failed to get the state of user then, the If condition check might fail resulting in failure to obtain a valid user id. This, In turn, will still lead to admin access as we have wrongly initialized the userid variable to &#8216;0&#8242; which is equal to admin user id. Let&#8217;s consider another example:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span> str<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
strcat<span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This might seem innocent enough, but str was not initialized, so it contains random memory. As a result, str[0] might not contain the null terminator, so the copy might start at an offset other than 0. The consequences can vary, depending on the underlying memory. If a null terminator is found before str[8], then some bytes of random garbage will be printed before the &#8220;hello world&#8221; string. The memory might contain sensitive information from previous uses, such as a password stored in a buffer. In this example, it might not be a big deal, but consider what could happen if large amounts of memory are printed out before the null terminator is found. If a null terminator isn&#8217;t found before str[8], then a buffer overflow could occur, since strcat will first look for the null terminator, then copy 12 bytes starting with that location. Alternately, a buffer over-read might occur if a null terminator isn&#8217;t found before the end of the memory segment is reached, leading to a segmentation fault and crash. I hope that the above mentioned examples are goos enough to emphasize on the correct initialization of variables and yes, that each and every needs to be initialized.</p>
<p>The four mandatory steps to follow for correct variable declaration is :</p>
<ol>
<li>Explicitly initialize all variable or data stores with the correct and expected values either at the first usage or during declaration as a must rule.</li>
<li>Properly do input validation to make sure that the variable usage in the first statement itself is initialized to expected value.</li>
<li>Avoid race conditions during initialization routine.</li>
<li>Definitely run some static analysis tool on your code to make sure that it raises all sorts of warnings or errors to warn you before you publish your code.</li>
</ol>
<p>Once you follow the above mentioned checklist, I am sure that you’ll face least of problems or issues with variable initializations.
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html">Improper Variable Initialization</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F01%2F13%2Fimproper-variable-initialization.html&amp;title=Improper%20Variable%20Initialization&amp;bodytext=Except%20for%20few%20good%20C%20programmers%2C%20others%20generally%20tend%20to%20ignore%20variable%20initialization%20or%20I%20should%20rather%20say%20%E2%80%9Cproper%20variable%20initialization%E2%80%9D.%20Generally%20seen%2C%20the%20variable%20declaration%20itself%20is%20not%20done%20with%20a%20good%20thinking.%20Improper%20local%20v" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F01%2F13%2Fimproper-variable-initialization.html&amp;title=Improper%20Variable%20Initialization&amp;notes=Except%20for%20few%20good%20C%20programmers%2C%20others%20generally%20tend%20to%20ignore%20variable%20initialization%20or%20I%20should%20rather%20say%20%E2%80%9Cproper%20variable%20initialization%E2%80%9D.%20Generally%20seen%2C%20the%20variable%20declaration%20itself%20is%20not%20done%20with%20a%20good%20thinking.%20Improper%20local%20v" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F01%2F13%2Fimproper-variable-initialization.html&amp;t=Improper%20Variable%20Initialization" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F01%2F13%2Fimproper-variable-initialization.html&amp;title=Improper%20Variable%20Initialization" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F01%2F13%2Fimproper-variable-initialization.html&amp;title=Improper%20Variable%20Initialization" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F01%2F13%2Fimproper-variable-initialization.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/buffer-overflow" title="buffer overflow" rel="tag nofollow">buffer overflow</a>, <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/efficiency" title="Efficiency" rel="tag nofollow">Efficiency</a>, <a href="http://www.safercode.com/blog/tag/initialization" title="initialization" rel="tag nofollow">initialization</a>, <a href="http://www.safercode.com/blog/tag/languages" title="Languages" rel="tag nofollow">Languages</a>, <a href="http://www.safercode.com/blog/tag/security" title="Security" rel="tag nofollow">Security</a>, <a href="http://www.safercode.com/blog/tag/variable-declaration" title="variable declaration" rel="tag nofollow">variable declaration</a>, <a href="http://www.safercode.com/blog/tag/variables" title="variables" rel="tag nofollow">variables</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/10/14/and-so-it-begins.html" title="And So It Begins&#8230; (October 14, 2008)">And So It Begins&#8230;</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2009/06/10/de-bugging-code-before-check-in.html" title="&#8220;De-Bugging&#8221; Code before Check-in (June 10, 2009)">&#8220;De-Bugging&#8221; Code before Check-in</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html" title="Predicting the rand() and using Cryptographic Random Numbers (February 10, 2009)">Predicting the rand() and using Cryptographic Random Numbers</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html" title="Validating Untrusted String Inputs (November 11, 2008)">Validating Untrusted String Inputs</a> (1)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Enum Pattern in Java &lt; 1.5</title>
		<link>http://www.safercode.com/blog/2008/12/16/using-enum-pattern-in-java-15.html</link>
		<comments>http://www.safercode.com/blog/2008/12/16/using-enum-pattern-in-java-15.html#comments</comments>
		<pubDate>Tue, 16 Dec 2008 15:56:03 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[Concepts]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[efficient code]]></category>
		<category><![CDATA[input validation]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[Safety]]></category>
		<category><![CDATA[untrusted inputs]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=27</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Alright!!! Let&#8217;s get started. This is one of many subjects which always overwhelms me. Why so? Ofcourse, the reasons can not be explained here but then, the reason should be the least of your worries.
Okay, if you know enough about this, then please post your knowledge tips as comments because your comments might help towards [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Alright!!! Let&#8217;s get started. This is one of many subjects which always overwhelms me. Why so? Ofcourse, the reasons can not be explained here but then, the reason should be the least of your worries.</p>
<p>Okay, if you know enough about this, then please post your knowledge tips as comments because your comments might help towards my unexplained reasons.</p>
<p>You may find similar information on other websites but then, it&#8217;s a wild world and I am not intending to infringe any copyrights.</p>
<p>Now to begin with, let&#8217;s first understand how to evaluate the performance of java code and protect the java code from tainted objects. We&#8217;ve already talked about Tainted Object Propagation in my previous post in context with databases. now, it is in context with application code.</p>
<p>I&#8217;ll explain this with an example of enum pattern.</p>
<p>We can have enums in Java in two ways. <span id="more-27"></span><br />
1) Either we have &#8220;public static final&#8221; constants declared.</p>
<div>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UsingConstants <span style="color: #009900;">&#123;</span>  
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> CONST_1 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1234</span> <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> CONST_2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> CONST_3 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> value <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> UsingConstants<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       value <span style="color: #339933;">=</span> param <span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>2) Implement the enum pattern.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UsingEnumPattern <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> UsingEnumPattern CONST_1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UsingEnumPattern<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1234</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> UsingEnumPattern CONST_2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UsingEnumPattern<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> UsingEnumPattern CONST_3 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UsingEnumPattern<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> value <span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> UsingEnumPattern<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       value <span style="color: #339933;">=</span> param <span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> getValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To evaluate the performance and understand the details of these two different implementations, have a look at the user code given below:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserCode <span style="color: #009900;">&#123;</span>  
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> initializeSomethingUsingConstants<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> param<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> initializeSomethingUsingEnumPattern<span style="color: #009900;">&#40;</span>UsingEnumPattern param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        UsingEnumPattern i <span style="color: #339933;">=</span> UsingEnumPattern.<span style="color: #006633;">CONST_1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> UsingEnumPattern.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> callMethods<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
        initializeSomethingUsingConstants<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1111</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// I can pass any integer here</span>
        initializeSomethingUsingEnumPattern<span style="color: #009900;">&#40;</span>UsingEnumPattern.<span style="color: #006633;">CONST_1</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// only defined enums can be passed.</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, We&#8217;ll deduce the following two things from this example:</p>
<p>a) The first example, UsingConstants, has defnitely got faster execution time as it has got only few bytecode instructions to execute.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>   aload_0
    <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>   invokespecial   #<span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method java/lang/Object.&quot;&amp;lt;init&amp;gt;&quot;:()V</span>
    <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>   aload_0
    <span style="color: #cc66cc;">5</span><span style="color: #339933;">:</span>   iload_1
    <span style="color: #cc66cc;">6</span><span style="color: #339933;">:</span>   putfield        #<span style="color: #cc66cc;">19</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Field value:I</span>
       <span style="color: #cc66cc;">9</span><span style="color: #339933;">:</span>   <span style="color: #000000; font-weight: bold;">return</span></pre></div></div>

<p>   where as the second example, UsingEnumPattern has about 16 instructions to execute. 16 to create statics and 6 for initialization.</p>
<div>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
      <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>   <span style="color: #000000; font-weight: bold;">new</span>     #<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//class com/nds/epg/network/IPConfigErrorCodeA</span>
      <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>   dup
      <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>   sipush  <span style="color: #cc66cc;">1234</span>
      <span style="color: #cc66cc;">7</span><span style="color: #339933;">:</span>   invokespecial   #<span style="color: #cc66cc;">14</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method &quot;&amp;lt;init&amp;gt;&quot;:(I)V</span>
      <span style="color: #cc66cc;">10</span><span style="color: #339933;">:</span>  putstatic       #<span style="color: #cc66cc;">18</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Field CONST_1:LUsingEnumPattern;</span>
      <span style="color: #cc66cc;">13</span><span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">new</span>     #<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//class UsingEnumPattern</span>
      <span style="color: #cc66cc;">16</span><span style="color: #339933;">:</span>  dup
      <span style="color: #cc66cc;">17</span><span style="color: #339933;">:</span>  iconst_1
      <span style="color: #cc66cc;">18</span><span style="color: #339933;">:</span>  invokespecial   #<span style="color: #cc66cc;">14</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method &quot;&amp;lt;init&amp;gt;&quot;:(I)V</span>
      <span style="color: #cc66cc;">21</span><span style="color: #339933;">:</span>  putstatic       #<span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Field CONST_2:LUsingEnumPattern;</span>
      <span style="color: #cc66cc;">24</span><span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">new</span>     #<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//class UsingEnumPattern</span>
      <span style="color: #cc66cc;">27</span><span style="color: #339933;">:</span>  dup
      <span style="color: #cc66cc;">28</span><span style="color: #339933;">:</span>  iconst_1
      <span style="color: #cc66cc;">29</span><span style="color: #339933;">:</span>  invokespecial   #<span style="color: #cc66cc;">14</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method &quot;&amp;lt;init&amp;gt;&quot;:(I)V</span>
      <span style="color: #cc66cc;">32</span><span style="color: #339933;">:</span>  putstatic       #<span style="color: #cc66cc;">22</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Field CONST_3:LUsingEnumPattern;</span>
      <span style="color: #cc66cc;">35</span><span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">return</span>
&nbsp;
      <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>   aload_0
      <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>   invokespecial   #<span style="color: #cc66cc;">26</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method java/lang/Object.&quot;&amp;lt;init&amp;gt;&quot;:()V</span>
      <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>   aload_0
      <span style="color: #cc66cc;">5</span><span style="color: #339933;">:</span>   iload_1
      <span style="color: #cc66cc;">6</span><span style="color: #339933;">:</span>   putfield        #<span style="color: #cc66cc;">28</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Field value:I</span>
      <span style="color: #cc66cc;">9</span><span style="color: #339933;">:</span>   <span style="color: #000000; font-weight: bold;">return</span></pre></div></div>

</div>
<p>and as per user code, constant evaluation will bve faster as it will using the &#8217;sipush&#8217; instruction as compared to &#8216;getstatic&#8217; instruction in case of enum pattern.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">   <span style="color: #666666; font-style: italic;">/*
   public void initializeSomethingUsingConstants();
      0:   sipush  1234
      3:   istore_1
      4:   return
&nbsp;
   public void initializeSomethingUsingEnumPattern();
      0:   getstatic       #18; //Field UsingEnumPattern.CONST_1:LUsingEnumPattern;
      3:   astore_1
      4:   return
*/</span></pre></div></div>

<p>So, to summarize, if you are very much worried just about performance then definitely, using constants makes it a perfect sense.</p>
<p>b) Now, lets look at the problem of using constants. get a bit paranoid and think that people are just waiting to break your code. it is easily possible as while using constants, any integer value can be passed to the user method instead of the constant values, which is enough to break your code. Now, to protect this, you&#8217;ll have to write conditions for every possible value the program expects and handles the correct error conditions for every incorrect value. Now, won&#8217;t this increase your code size resulting in a lot of execution overhead.</p>
<p>Whereas, if you use second example, incorrect values cannot be passed at all and your code will be safe from attackers. and you need not write any extra guard conditions resulting in keeping your code size to minimum.</p>
<p>Alright!! By now, most of you would be thinking that how this post relate to bytecode reverse engineering but then, I just needed a starting point to explain a problem before getting into injecting malicious content in your bytecode to break what you consider secure.</p>
<p>In my next post, i&#8217;ll evaluate these bytecodes more and then, later on, we&#8217;ll talk about contaminating bytecodes to make the code malfunction. All my posts will consider the aspect of code performance vs. code safety amd I am not sure whether these thoughts of mine will make any sense but I do need a place to vent out. <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2008/12/16/using-enum-pattern-in-java-15.html">Using Enum Pattern in Java < 1.5</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F16%2Fusing-enum-pattern-in-java-15.html&amp;title=Using%20Enum%20Pattern%20in%20Java%20%3C%201.5&amp;bodytext=Alright%21%21%21%20Let%27s%20get%20started.%20This%20is%20one%20of%20many%20subjects%20which%20always%20overwhelms%20me.%20Why%20so%3F%20Ofcourse%2C%20the%20reasons%20can%20not%20be%20explained%20here%20but%20then%2C%20the%20reason%20should%20be%20the%20least%20of%20your%20worries.%0D%0A%0D%0AOkay%2C%20if%20you%20know%20enough%20about%20this%2C%20then%20plea" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F16%2Fusing-enum-pattern-in-java-15.html&amp;title=Using%20Enum%20Pattern%20in%20Java%20%3C%201.5&amp;notes=Alright%21%21%21%20Let%27s%20get%20started.%20This%20is%20one%20of%20many%20subjects%20which%20always%20overwhelms%20me.%20Why%20so%3F%20Ofcourse%2C%20the%20reasons%20can%20not%20be%20explained%20here%20but%20then%2C%20the%20reason%20should%20be%20the%20least%20of%20your%20worries.%0D%0A%0D%0AOkay%2C%20if%20you%20know%20enough%20about%20this%2C%20then%20plea" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F16%2Fusing-enum-pattern-in-java-15.html&amp;t=Using%20Enum%20Pattern%20in%20Java%20%3C%201.5" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F16%2Fusing-enum-pattern-in-java-15.html&amp;title=Using%20Enum%20Pattern%20in%20Java%20%3C%201.5" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F16%2Fusing-enum-pattern-in-java-15.html&amp;title=Using%20Enum%20Pattern%20in%20Java%20%3C%201.5" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F16%2Fusing-enum-pattern-in-java-15.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/bytecode" title="bytecode" rel="tag nofollow">bytecode</a>, <a href="http://www.safercode.com/blog/tag/concepts" title="Concepts" rel="tag nofollow">Concepts</a>, <a href="http://www.safercode.com/blog/tag/efficiency" title="Efficiency" rel="tag nofollow">Efficiency</a>, <a href="http://www.safercode.com/blog/tag/efficient-code" title="efficient code" rel="tag nofollow">efficient code</a>, <a href="http://www.safercode.com/blog/tag/input-validation" title="input validation" rel="tag nofollow">input validation</a>, <a href="http://www.safercode.com/blog/tag/java" title="Java" rel="tag nofollow">Java</a>, <a href="http://www.safercode.com/blog/tag/pattern" title="pattern" rel="tag nofollow">pattern</a>, <a href="http://www.safercode.com/blog/tag/reverse-engineering" title="reverse engineering" rel="tag nofollow">reverse engineering</a>, <a href="http://www.safercode.com/blog/tag/safety" title="Safety" rel="tag nofollow">Safety</a>, <a href="http://www.safercode.com/blog/tag/security" title="Security" rel="tag nofollow">Security</a>, <a href="http://www.safercode.com/blog/tag/untrusted-inputs" title="untrusted inputs" rel="tag nofollow">untrusted inputs</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/10/14/and-so-it-begins.html" title="And So It Begins&#8230; (October 14, 2008)">And So It Begins&#8230;</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/18/all-input-is-evil.html" title="All Input is Evil (November 18, 2008)">All Input is Evil</a> (3)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html" title="Predicting the rand() and using Cryptographic Random Numbers (February 10, 2009)">Predicting the rand() and using Cryptographic Random Numbers</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/12/08/tainted-object-propagation.html" title="Tainted Object Propagation (December 8, 2008)">Tainted Object Propagation</a> (5)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2008/12/16/using-enum-pattern-in-java-15.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tainted Object Propagation</title>
		<link>http://www.safercode.com/blog/2008/12/08/tainted-object-propagation.html</link>
		<comments>http://www.safercode.com/blog/2008/12/08/tainted-object-propagation.html#comments</comments>
		<pubDate>Mon, 08 Dec 2008 16:24:59 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[database input]]></category>
		<category><![CDATA[Queries]]></category>
		<category><![CDATA[SQL Injection]]></category>
		<category><![CDATA[Taint]]></category>
		<category><![CDATA[Tainted Object]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=26</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Basically, Tainted Object Propagation is the term defined for using incorrect or invalid inputs to get more than required information from the system and in some cases, taking control of the system. Although this technique is much widely used to misuse web applications and database oriented applications, but this holds true for any API publisher [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Basically, <b><u>Tainted Object Propagation</u></b> is the term defined for using incorrect or invalid inputs to get more than required information from the system and in some cases, taking control of the system. Although this technique is much widely used to misuse web applications and database oriented applications, but this holds true for any API publisher who exposes his API&#8217;s to third party application writers.</p>
<p>Again, just like previous post, Let&#8217;s start with an example. </p>
<p>Consider that a web page or an application takes an input &#8220;userName&#8221; and the application executes the following query to find that particular user.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">HttpServletRequest request <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> userName <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Connection</span> con <span style="color: #339933;">=</span> ...
<span style="color: #003399;">String</span> query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM Users &quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; WHERE name = ’&quot;</span> <span style="color: #339933;">+</span> userName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;’&quot;</span><span style="color: #339933;">;</span>
con.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, this is the usual code written by programmers to get the particular from the database. Now, if an attacker gets the control of the userName field, he can set it to <b><i> &#8216;OR 1=1; </i></b> This query allows the user to circumvent user name check and returns all the users from the database. In this case, the input variable &#8220;userName&#8221; is considered as <b><u>Tainted Object</u></b>.</p>
<p><span id="more-26"></span><br />
Lets take another example,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;total_price&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;25.00&quot;</span><span style="color: #339933;">&gt;</span></pre></div></div>

<p>A web form contains hidden fields to pass some information to the server which is not visible to the user. Now, HTTP pages are stateless. Unlike regular fields, hidden fields cannot be modified directly by typing values into an HTML form. However, since the hidden field is part of the page source, saving the HTML page, editing the hidden field value, and reloading the page will cause the Web application to receive the newly updated value of the hidden field.</p>
<p>Following is the classification of various attacks:</p>
<li>Inject malicious data intoWeb applications. Common methods used include:
<ul><b>Parameter tampering:</b> pass specially crafted malicious values in fields of HTML forms.</ul>
<ul><b>URL manipulation:</b> use specially crafted parameters to be submitted to the Web application as part of the URL.</ul>
<ul><b>Hidden field manipulation:</b> set hidden fields of HTML forms in Web pages to malicious values.</ul>
<ul><b>HTTP header tampering:</b> manipulate parts of HTTP requests sent to the application.</ul>
<ul><b>Cookie poisoning:</b> place malicious data in cookies, small files sent to Web-based applications.  </ul>
</li>
<li>Manipulate applications using malicious data. Common methods used include:
<ul><b>SQL injection:</b> pass input containing SQL commands to a database server for execution.</ul>
<ul><b>Cross-site scripting:</b> exploit applications that output unchecked input verbatim to trick the user into executing malicious scripts.</ul>
<ul><b>HTTP response splitting:</b> exploit applications that output input verbatim to perform Web page defacements or Web cache poisoning attacks.</ul>
<ul><b>Path traversal:</b> exploit unchecked user input to control which files are accessed on the server.</ul>
<ul><b>Command injection:</b> exploit user input to execute shell commands.</ul>
</li>
<p>Now, I&#8217;ll describe how to <b>&#8220;UnTaint&#8221;</b> your objects.</p>
<p>In order to track tainted inputs, we must specify following three things:</p>
<li>
<b>Source:</b> This is the originator of the tainted object. For example: the input fields or variables.</li>
<li><b>Derivatives:</b> Derivatives are the strings formed using &#8220;Source&#8221; strings to execute some instructions or perform some actions in the code. Derivative strings should also be marked as tainted.</li>
<li><b>Sinks:</b> A sink is a method that consumes input or derivative of user input. This includes methods that execute some form of code (such as a script or SQL query), or methods that output data (such presenting a new HTML page). Tainted strings must be prevented from being used as parameters to sinks.</li>
</li>
<p>To track the taintedness of strings, we associated a taint flag with every string. This taint flag is set when a string is returned by a source method. We propagate this taint flag to strings that are derived from tainted strings through operations such as concatenation, case conversion etc.</p>
<p>To &#8220;Untaint&#8221;, we need to have mechanism in place which will subject every input for taint verification and then, untaint it. For example: a tainted string that is passed through a regular expression match, or been tested for the presence of a particular character is not tainted anymore. Note that, here we need to trust the programmer to have performed a meaningful check that accounts for all cases that might be exploitable in an attack. It is entirely possible that the programmer wrote a faulty input validation routine that lets through user-input strings with malicious content in them. <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>If we find any tainted object using these algorithms, then two things can be done. Either <i>raise a TaintException</i> or <i>Abandon that particular taint session</i>. The weakest option is to let tainted data be used as an argument to a sink, but make a full log of the arguments, the sink, and the path the tainted data took from source to sink. This seems insecure, but is useful when auditing, doing penetration testing, debugging, or if used in a honeypot.</p>
<p>The best way to find most of the taint problems and the code vulnerablity is use of static analyzer tools. These tools identify the possibility of any input field being maliciously utilized and raise a warning to fix that error. The algorithms by which most of these static analyzer tools work use the above mentioned methodology. Some of the java static analyzer tools work on the bytecode level to prevent bytecode contamination also. </p>
<p>But even after this, you may find some taint issues coming your way. In this case, no one can defeat the strictest of code reviews. More strict the code review and reviewer, less the code is prone to attackers. </p>
<p>There is a lot of information available on the net about Taint object problem. I have used the resources available on the net itself to make myself aware of the gory details and if you are interested, search details on the net <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . By going through the details, even you should be able to build you own static analyzer tool. </p>
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2008/12/08/tainted-object-propagation.html">Tainted Object Propagation</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F08%2Ftainted-object-propagation.html&amp;title=Tainted%20Object%20Propagation&amp;bodytext=Basically%2C%20Tainted%20Object%20Propagation%20is%20the%20term%20defined%20for%20using%20incorrect%20or%20invalid%20inputs%20to%20get%20more%20than%20required%20information%20from%20the%20system%20and%20in%20some%20cases%2C%20taking%20control%20of%20the%20system.%20Although%20this%20technique%20is%20much%20widely%20used%20to%20misu" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F08%2Ftainted-object-propagation.html&amp;title=Tainted%20Object%20Propagation&amp;notes=Basically%2C%20Tainted%20Object%20Propagation%20is%20the%20term%20defined%20for%20using%20incorrect%20or%20invalid%20inputs%20to%20get%20more%20than%20required%20information%20from%20the%20system%20and%20in%20some%20cases%2C%20taking%20control%20of%20the%20system.%20Although%20this%20technique%20is%20much%20widely%20used%20to%20misu" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F08%2Ftainted-object-propagation.html&amp;t=Tainted%20Object%20Propagation" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F08%2Ftainted-object-propagation.html&amp;title=Tainted%20Object%20Propagation" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F08%2Ftainted-object-propagation.html&amp;title=Tainted%20Object%20Propagation" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F08%2Ftainted-object-propagation.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/database" title="Database" rel="tag nofollow">Database</a>, <a href="http://www.safercode.com/blog/tag/database-input" title="database input" rel="tag nofollow">database input</a>, <a href="http://www.safercode.com/blog/tag/java" title="Java" rel="tag nofollow">Java</a>, <a href="http://www.safercode.com/blog/tag/queries" title="Queries" rel="tag nofollow">Queries</a>, <a href="http://www.safercode.com/blog/tag/security" title="Security" rel="tag nofollow">Security</a>, <a href="http://www.safercode.com/blog/tag/sql-injection" title="SQL Injection" rel="tag nofollow">SQL Injection</a>, <a href="http://www.safercode.com/blog/tag/taint" title="Taint" rel="tag nofollow">Taint</a>, <a href="http://www.safercode.com/blog/tag/tainted-object" title="Tainted Object" rel="tag nofollow">Tainted Object</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/12/16/using-enum-pattern-in-java-15.html" title="Using Enum Pattern in Java < 1.5 (December 16, 2008)">Using Enum Pattern in Java < 1.5</a> (1)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/14/and-so-it-begins.html" title="And So It Begins&#8230; (October 14, 2008)">And So It Begins&#8230;</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html" title="Predicting the rand() and using Cryptographic Random Numbers (February 10, 2009)">Predicting the rand() and using Cryptographic Random Numbers</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html" title="int main() vs void main() (October 14, 2008)">int main() vs void main()</a> (22)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2008/12/08/tainted-object-propagation.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Unsafe Functions In C And Their Safer Replacements: Strings Part II</title>
		<link>http://www.safercode.com/blog/2008/12/02/unsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html</link>
		<comments>http://www.safercode.com/blog/2008/12/02/unsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html#comments</comments>
		<pubDate>Tue, 02 Dec 2008 15:00:22 +0000</pubDate>
		<dc:creator>Shantanu Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[buffer overflow]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Computer Security]]></category>
		<category><![CDATA[strcat]]></category>
		<category><![CDATA[strcpy]]></category>
		<category><![CDATA[Strings]]></category>
		<category><![CDATA[strlcat]]></category>
		<category><![CDATA[strlcpy]]></category>
		<category><![CDATA[Ulrich Drepper]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=25</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Last time, we advised you to use ditch the unsafe functions like strcpy and strcat, and use their safer replacements (strlcpy, strlcat) instead. However, there is a small problem with this that you might discover that your compiler (especially gcc) does not have these functions in their implementation of the c library (libc). Why is [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p><a title="Unsafe Functions In C And Their Safer Replacements: Part I" href="http://www.safercode.com/blog/2008/11/04/unsafe-functions-in-c-and-their-safer-replacements-strings-part-i.html">Last time</a>, we advised you to use ditch the unsafe functions like strcpy and strcat, and use their safer replacements (strlcpy, strlcat) instead. However, there is a small problem with this that you might discover that your compiler (especially gcc) does not have these functions in their implementation of the c library (libc). Why is this so? Read <a title="Thread about patch to incorporate strlcpy and strlcat in libc" href="http://sources.redhat.com/ml/libc-alpha/2000-08/msg00052.html" rel="external nofollow">this thread</a> about the original patch that was submitted to add these functions to libc. Essentially, it was rejected on the basis that these functions hide problems instead of solving them and would actually lead to hard to detect bugs that creep in because of unsolicited truncation caused by these functions. A sample implementation of strlcpy looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;sys/types.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
 * Copy src to string dst of size siz.  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz == 0).
 * Returns strlen(src); if retval &gt;= siz, truncation occurred.
 */</span>
size_t
strlcpy<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>dst<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>src<span style="color: #339933;">,</span> size_t siz<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>d <span style="color: #339933;">=</span> dst<span style="color: #339933;">;</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>s <span style="color: #339933;">=</span> src<span style="color: #339933;">;</span>
	size_t n <span style="color: #339933;">=</span> siz<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Copy as many bytes as will fit */</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>n <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>d<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>s<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Not enough room in dst, add NUL and traverse rest of src */</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>siz <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">*</span>d <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>		<span style="color: #808080; font-style: italic;">/* NUL-terminate dst */</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>s <span style="color: #339933;">-</span> src <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #808080; font-style: italic;">/* count does not include NUL */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, there are supporters for both the sides. Solaris and BSD accepted these functions while GNU/Linux refuses to do so till date. The stand that I’d like to take here is that this is at least a step towards the right direction because a truncation bug is much better than an attacker taking over the control of your network just by pushing out a long string onto the stack. However, we could indeed augment the implementation above to communicate back to the caller somehow that truncation occurred. Whichever side you choose, be aware that these problems are very real and don’t just keep on using the older unsafe versions (Even in words of Ulrich Drepper, the opposer of strlcpy: “ The users of these functions[strcpy, strcat] should be severly punished”).</p>
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2008/12/02/unsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html">Unsafe Functions In C And Their Safer Replacements: Strings Part II</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F02%2Funsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html&amp;title=Unsafe%20Functions%20In%20C%20And%20Their%20Safer%20Replacements%3A%20Strings%20Part%20II&amp;bodytext=Last%20time%2C%20we%20advised%20you%20to%20use%20ditch%20the%20unsafe%20functions%20like%20strcpy%20and%20strcat%2C%20and%20use%20their%20safer%20replacements%20%28strlcpy%2C%20strlcat%29%20instead.%20However%2C%20there%20is%20a%20small%20problem%20with%20this%20that%20you%20might%20discover%20that%20your%20compiler%20%28especially%20gcc%29%20d" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F02%2Funsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html&amp;title=Unsafe%20Functions%20In%20C%20And%20Their%20Safer%20Replacements%3A%20Strings%20Part%20II&amp;notes=Last%20time%2C%20we%20advised%20you%20to%20use%20ditch%20the%20unsafe%20functions%20like%20strcpy%20and%20strcat%2C%20and%20use%20their%20safer%20replacements%20%28strlcpy%2C%20strlcat%29%20instead.%20However%2C%20there%20is%20a%20small%20problem%20with%20this%20that%20you%20might%20discover%20that%20your%20compiler%20%28especially%20gcc%29%20d" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F02%2Funsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html&amp;t=Unsafe%20Functions%20In%20C%20And%20Their%20Safer%20Replacements%3A%20Strings%20Part%20II" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F02%2Funsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html&amp;title=Unsafe%20Functions%20In%20C%20And%20Their%20Safer%20Replacements%3A%20Strings%20Part%20II" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F02%2Funsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html&amp;title=Unsafe%20Functions%20In%20C%20And%20Their%20Safer%20Replacements%3A%20Strings%20Part%20II" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F12%2F02%2Funsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/buffer-overflow" title="buffer overflow" rel="tag nofollow">buffer overflow</a>, <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/computer-security" title="Computer Security" rel="tag nofollow">Computer Security</a>, <a href="http://www.safercode.com/blog/tag/strcat" title="strcat" rel="tag nofollow">strcat</a>, <a href="http://www.safercode.com/blog/tag/strcpy" title="strcpy" rel="tag nofollow">strcpy</a>, <a href="http://www.safercode.com/blog/tag/strings" title="Strings" rel="tag nofollow">Strings</a>, <a href="http://www.safercode.com/blog/tag/strlcat" title="strlcat" rel="tag nofollow">strlcat</a>, <a href="http://www.safercode.com/blog/tag/strlcpy" title="strlcpy" rel="tag nofollow">strlcpy</a>, <a href="http://www.safercode.com/blog/tag/ulrich-drepper" title="Ulrich Drepper" rel="tag nofollow">Ulrich Drepper</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/11/04/unsafe-functions-in-c-and-their-safer-replacements-strings-part-i.html" title="Unsafe Functions In C And Their Safer Replacements: Strings Part I (November 4, 2008)">Unsafe Functions In C And Their Safer Replacements: Strings Part I</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html" title="Validating Untrusted String Inputs (November 11, 2008)">Validating Untrusted String Inputs</a> (1)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2009/03/23/lint-your-code-find-probable-mistakes-much-before-testing.html" title="Lint your code: Find probable mistakes much before testing (March 23, 2009)">Lint your code: Find probable mistakes much before testing</a> (3)</li>
	<li><a href="http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html" title="Improper Variable Initialization (January 13, 2009)">Improper Variable Initialization</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2008/12/02/unsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>All Input is Evil</title>
		<link>http://www.safercode.com/blog/2008/11/18/all-input-is-evil.html</link>
		<comments>http://www.safercode.com/blog/2008/11/18/all-input-is-evil.html#comments</comments>
		<pubDate>Tue, 18 Nov 2008 16:00:47 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Concepts]]></category>
		<category><![CDATA[input validation]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Safety]]></category>
		<category><![CDATA[security holes]]></category>
		<category><![CDATA[untrusted inputs]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=21</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->In my previous posts, I have been emphasizing on validating Integer and String inputs by putting various checks in place. But now, I’ll suggest you to consider any type of input to your application or software as “Evil”. Consider the following two rules for any input data:

All input is evil until proven otherwise. 
Data must [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>In my previous posts, I have been emphasizing on validating Integer and String inputs by putting various checks in place. But now, I’ll suggest you to consider any type of input to your application or software as “<strong>Evil”</strong>. Consider the following two rules for any input data:</p>
<ol>
<li><em>All input is evil until proven otherwise. </em></li>
<li><em>Data must be validated as it crosses the boundary between untrusted and trusted environments.</em></li>
</ol>
<p>Till now, I explained how to validate Integer and String data, but today, I’ll explain what is to be validated in the input data. First things first, Look for valid data and reject everything else. You should deny all access until you are sure that the input in the request is valid. You should look for valid data and not look for invalid data for two reasons:</p>
<ol>
<li><em>There might be more than one valid way to represent the data.</em>
<ul>
<li>
<ul>For example: a word “Rose” can be represented in many ways like “ROSE”, “rose”, “R%6fse”, “RoSE” et cetera. All the mentioned words are the variations of single word “Rose” and they are valid variations. But, This can definitely be a problem for an application.</ul>
</li>
</ul>
</li>
<li><em>You might miss an invalid data pattern.</em></li>
</ol>
<p>Consider the following code: <span id="more-21"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">bool IsBadExtn<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>szFileName<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/* some code */</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>szFileName<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		size_t cFileName <span style="color: #339933;">=</span> strlen<span style="color: #009900;">&#40;</span>szFileName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>cFileName <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #993333;">char</span><span style="color: #339933;">*</span> szBadExtn<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;.exe&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.com&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.bat&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.cmd&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/** Line 9 **/</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">/* code to check validate the input &quot;szFileName&quot; against these file extensions which are not allowed */</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, The above mentioned code is not good enough as It does not catches all the vulnerabilities. This program is trying to block all those types of file extensions which might be harmful for the system if executed. But, The validation done is not good enough as even “.pl”, “.js”, “.vbs”, “.wsh” etc can also be harmful. So, To protect your system, allow only file those types which are expected. For example: Line 9 can be replaced by</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span><span style="color: #339933;">*</span> szGoodExtn<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;.gif&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.bmp&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.png&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.txt&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is a positive aspect of data validation. This is needed as we don’t know what are various possibilities that a program can be exploited with. So, we block all access other than the expected input.</p>
<p>In a nutshell, There is no hard and fast rule whether to do a negative validation or a positive validation. It’s purely up to you depending on the application requirements. But, If you take care of the above mentioned rules, There are very rare chances of  tainted data entering into your system.
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2008/11/18/all-input-is-evil.html">All Input is Evil</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F18%2Fall-input-is-evil.html&amp;title=All%20Input%20is%20Evil&amp;bodytext=In%20my%20previous%20posts%2C%20I%20have%20been%20emphasizing%20on%20validating%20Integer%20and%20String%20inputs%20by%20putting%20various%20checks%20in%20place.%20But%20now%2C%20I%E2%80%99ll%20suggest%20you%20to%20consider%20any%20type%20of%20input%20to%20your%20application%20or%20software%20as%20%E2%80%9CEvil%E2%80%9D.%20Consider%20the%20following%20" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F18%2Fall-input-is-evil.html&amp;title=All%20Input%20is%20Evil&amp;notes=In%20my%20previous%20posts%2C%20I%20have%20been%20emphasizing%20on%20validating%20Integer%20and%20String%20inputs%20by%20putting%20various%20checks%20in%20place.%20But%20now%2C%20I%E2%80%99ll%20suggest%20you%20to%20consider%20any%20type%20of%20input%20to%20your%20application%20or%20software%20as%20%E2%80%9CEvil%E2%80%9D.%20Consider%20the%20following%20" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F18%2Fall-input-is-evil.html&amp;t=All%20Input%20is%20Evil" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F18%2Fall-input-is-evil.html&amp;title=All%20Input%20is%20Evil" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F18%2Fall-input-is-evil.html&amp;title=All%20Input%20is%20Evil" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F18%2Fall-input-is-evil.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/concepts" title="Concepts" rel="tag nofollow">Concepts</a>, <a href="http://www.safercode.com/blog/tag/input-validation" title="input validation" rel="tag nofollow">input validation</a>, <a href="http://www.safercode.com/blog/tag/programming" title="programming" rel="tag nofollow">programming</a>, <a href="http://www.safercode.com/blog/tag/safety" title="Safety" rel="tag nofollow">Safety</a>, <a href="http://www.safercode.com/blog/tag/security" title="Security" rel="tag nofollow">Security</a>, <a href="http://www.safercode.com/blog/tag/security-holes" title="security holes" rel="tag nofollow">security holes</a>, <a href="http://www.safercode.com/blog/tag/untrusted-inputs" title="untrusted inputs" rel="tag nofollow">untrusted inputs</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2008/12/16/using-enum-pattern-in-java-15.html" title="Using Enum Pattern in Java < 1.5 (December 16, 2008)">Using Enum Pattern in Java < 1.5</a> (1)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/14/and-so-it-begins.html" title="And So It Begins&#8230; (October 14, 2008)">And So It Begins&#8230;</a> (0)</li>
	<li><a href="http://www.safercode.com/blog/2009/02/10/predicting-the-rand-and-using-cryptographic-random-numbers.html" title="Predicting the rand() and using Cryptographic Random Numbers (February 10, 2009)">Predicting the rand() and using Cryptographic Random Numbers</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html" title="int main() vs void main() (October 14, 2008)">int main() vs void main()</a> (22)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2008/11/18/all-input-is-evil.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Validating Untrusted String Inputs</title>
		<link>http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html</link>
		<comments>http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html#comments</comments>
		<pubDate>Tue, 11 Nov 2008 16:00:00 +0000</pubDate>
		<dc:creator>Amit Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[buffer overflow]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Cross Site Scriping]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[string input]]></category>
		<category><![CDATA[Strings]]></category>
		<category><![CDATA[untrusted inputs]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/?p=20</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Alright!! In my last post about untrusted inputs, we talked about validating the data of the &#8220;integer&#8221; input parameters, checking the out parameters et cetera.This time, we&#8217;ll talk about other types of inputs. If you have written a program to take in multiple lines of strings as an input from the user, you need to [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Alright!! In my last post about untrusted inputs, we talked about validating the data of the &#8220;integer&#8221; input parameters, checking the out parameters et cetera.This time, we&#8217;ll talk about other types of inputs. If you have written a program to take in multiple lines of strings as an input from the user, you need to make sure that the input is not tainted. It is clean and as per your expectations. For example: If your program requires an answer for a question which can be subjective, then you need to provide a string buffer good enough to get a complete answer but not large enough to crash your system or make it run out of memory. Or you need to protect your system from getting any malicious scripts being inserted.Strings are a very risky area for inputs as there is pre-defined rule for this type of validation. So, following are the points to ponder to make your code safe and secure.</p>
<ol>
<li>Firstly, do use regular expressions to validate the string input. For example, <strong>^[A-Za-z0-9]+$</strong> specifies that the string must be at least one character long and that it can only include upper-case letters, lower-case letters, and the digits 0 through 9 (in any order). You can use regular expressions to limit which characters are allowed and to be more specific (for example, you can often limit even further what the first character can be).If you use regular expressions, be sure to indicate that you want to match the beginning (usually symbolized by ^) and end (usually symbolized by $) of the data in your match. If you forget to include ^ or $, an attacker could include legal text inside their attack to bypass your check.</li>
<li>Now, if your program needs more variety of input and the above point doesn&#8217;t fulfil the requirements then you need to make a bit more complicated regular expressions. If the data is a filename (or will be used to create one), be very restrictive. Ideally, don&#8217;t let users choose filenames, and if that won&#8217;t work, limit the characters to small patterns such as <strong>^[A-Za-z0-9][A-Za-z0-9._\-]*$</strong>. You should consider omitting from the legal patterns characters like &#8220;/&#8221;, control characters (especially newline), and a leading &#8220;.&#8221;Similarly, you need to take care for email strings, locale specific strings. UTF-8 encoding characters et cetera. In most of the programs, complex regular expressions are good enough to validate a string. But in certain cases, a malicious input containing some script code can spoil the fun.</li>
<li>If your program faces HTML tags or script related instructions in the input, the input should be rejected immidiately or your program might get infected with self executing malicious code. This technique is generally used in Cross Site scripting attack. (XSS attack). These problems are especially a problem for Web applications. Now, you need to again take care not to validate any input which looks like an HTML tag. The easiest way is to use above mentioned regular expressions which won&#8217;t allow the entry of &#8216;&lt;&#8217; or &#8216;&gt;&#8217; character. But if you must support some of the HTML tags like <strong>&lt;a href=&gt;</strong> etc, please validate them exclusively by filtering the whole string using a regex like <strong>^(http|ftp|https)://[-A-Za-z0-9._/]+$. </strong>A pattern that allows some more complex patterns is: <strong>^(http|ftp|https)://[-A-Za-z0-9._]+(\/([A-Za-z0-9\-\_\.\!\~\*\'\(\)\%\?]+))*/?$ </strong></li>
<li>For more complex strings, like reading a data file, regular expressions, again, prove useful but the ideal way is to break the file into multiple chunks rather than reading it in one complete string.</li>
</ol>
<p>To Keep your String input kept in well defined range or buffer, make sure that your program terminates it with a NULL character. This will ensure that even if a large buffer is inserted using the input, the sting will get truncated as soon as the buffer gets full and it will be protected from buffer overflow.
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html">Validating Untrusted String Inputs</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F11%2Fvalidating-untrusted-string-inputs.html&amp;title=Validating%20Untrusted%20String%20Inputs&amp;bodytext=Alright%21%21%20In%20my%20last%20post%20about%20untrusted%20inputs%2C%20we%20talked%20about%20validating%20the%20data%20of%20the%20%22integer%22%20input%20parameters%2C%20checking%20the%20out%20parameters%20et%20cetera.This%20time%2C%20we%27ll%20talk%20about%20other%20types%20of%20inputs.%20If%20you%20have%20written%20a%20program%20to%20take%20in" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F11%2Fvalidating-untrusted-string-inputs.html&amp;title=Validating%20Untrusted%20String%20Inputs&amp;notes=Alright%21%21%20In%20my%20last%20post%20about%20untrusted%20inputs%2C%20we%20talked%20about%20validating%20the%20data%20of%20the%20%22integer%22%20input%20parameters%2C%20checking%20the%20out%20parameters%20et%20cetera.This%20time%2C%20we%27ll%20talk%20about%20other%20types%20of%20inputs.%20If%20you%20have%20written%20a%20program%20to%20take%20in" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F11%2Fvalidating-untrusted-string-inputs.html&amp;t=Validating%20Untrusted%20String%20Inputs" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F11%2Fvalidating-untrusted-string-inputs.html&amp;title=Validating%20Untrusted%20String%20Inputs" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F11%2Fvalidating-untrusted-string-inputs.html&amp;title=Validating%20Untrusted%20String%20Inputs" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2008%2F11%2F11%2Fvalidating-untrusted-string-inputs.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/buffer-overflow" title="buffer overflow" rel="tag nofollow">buffer overflow</a>, <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/cross-site-scriping" title="Cross Site Scriping" rel="tag nofollow">Cross Site Scriping</a>, <a href="http://www.safercode.com/blog/tag/memory" title="memory" rel="tag nofollow">memory</a>, <a href="http://www.safercode.com/blog/tag/regular-expressions" title="regular expressions" rel="tag nofollow">regular expressions</a>, <a href="http://www.safercode.com/blog/tag/string-input" title="string input" rel="tag nofollow">string input</a>, <a href="http://www.safercode.com/blog/tag/strings" title="Strings" rel="tag nofollow">Strings</a>, <a href="http://www.safercode.com/blog/tag/untrusted-inputs" title="untrusted inputs" rel="tag nofollow">untrusted inputs</a>, <a href="http://www.safercode.com/blog/tag/validation" title="validation" rel="tag nofollow">validation</a>, <a href="http://www.safercode.com/blog/tag/xss" title="XSS" rel="tag nofollow">XSS</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2008/11/04/unsafe-functions-in-c-and-their-safer-replacements-strings-part-i.html" title="Unsafe Functions In C And Their Safer Replacements: Strings Part I (November 4, 2008)">Unsafe Functions In C And Their Safer Replacements: Strings Part I</a> (7)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2008/12/02/unsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html" title="Unsafe Functions In C And Their Safer Replacements: Strings Part II (December 2, 2008)">Unsafe Functions In C And Their Safer Replacements: Strings Part II</a> (8)</li>
	<li><a href="http://www.safercode.com/blog/2009/03/23/lint-your-code-find-probable-mistakes-much-before-testing.html" title="Lint your code: Find probable mistakes much before testing (March 23, 2009)">Lint your code: Find probable mistakes much before testing</a> (3)</li>
	<li><a href="http://www.safercode.com/blog/2009/01/13/improper-variable-initialization.html" title="Improper Variable Initialization (January 13, 2009)">Improper Variable Initialization</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
