<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=TBeattie</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=TBeattie"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/TBeattie"/>
		<updated>2026-04-28T00:39:35Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15520</id>
		<title>Reviewing Code for Buffer Overruns and Overflows</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15520"/>
				<updated>2007-01-17T23:30:05Z</updated>
		
		<summary type="html">&lt;p&gt;TBeattie: /* Good Patterns &amp;amp; procedures to prevent buffer overflows: */ fix the formatting of the example code&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
==The buffer ==&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
==How to locate the potentially vulnerable code==&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays''':&lt;br /&gt;
  int x[20];&lt;br /&gt;
  int y[20][5];&lt;br /&gt;
  int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
  printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
  %x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
'''Over flows:'''&lt;br /&gt;
&lt;br /&gt;
  strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Vulnerable Patterns for buffer overflows==&lt;br /&gt;
&lt;br /&gt;
===‘Vanilla’ buffer overflow: ===&lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  void copyData(char *userId) {  &lt;br /&gt;
     char  smallBuffer['''10''']; // size of 10  &lt;br /&gt;
     '''strcpy'''(smallBuffer, userId);&lt;br /&gt;
  }  &lt;br /&gt;
  int main(int argc, char *argv[]) {  &lt;br /&gt;
  char *userId = &amp;quot;'''01234567890'''&amp;quot;; // Payload of 11&lt;br /&gt;
  copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Format String: ===&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs to output information, print error messages, or process strings.&lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
  %x        hexadecimal (unsigned int)&lt;br /&gt;
  %s        string ((const) (unsigned) char *)&lt;br /&gt;
  %n        number of bytes written so far, (* int)&lt;br /&gt;
  %d        decimal (int)&lt;br /&gt;
  %u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printed as a string.&lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not meant to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
===Crashing an application: ===&lt;br /&gt;
&lt;br /&gt;
  printf (User_Input);&lt;br /&gt;
&lt;br /&gt;
If we supply %x (hex unsigned int) as the input, the '''printf''' function shall expect to find an integer relating to that format string, but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
===Walking the stack: ===&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Worse again is using the '''%n''' directive in '''printf()'''. This directive takes an '''int*''' and '''writes''' the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the '''printf()''' family of functions,  '''printf(),fprintf(), sprintf(), snprintf().''' Also '''syslog()''' (writes system log information) and setproctitle(''const char *fmt'', ''...''); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Integer overflows: ===&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
            int val;&lt;br /&gt;
            val = 0x7fffffff; 	/* 2147483647*/&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
            return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111; this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
        }&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the '''''if '''''condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
The line '''myArray[v2]  = v1''' assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Good Patterns &amp;amp; procedures to prevent buffer overflows: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 void copyData(char *userId) {&lt;br /&gt;
    char  smallBuffer[10]; // size of 10&lt;br /&gt;
    strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char *argv[]) {&lt;br /&gt;
    char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
    copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
C library functions such as '''strcpy (), strcat (), sprintf ()''' and '''vsprintf ()''' operate on null terminated strings and perform no bounds checking. '''gets ()''' is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The '''scanf ()''' family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the maximum string length.&lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
==.NET &amp;amp; Java ==&lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is ''managed''. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked, buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>TBeattie</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15519</id>
		<title>Talk:Reviewing Code for Buffer Overruns and Overflows</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15519"/>
				<updated>2007-01-17T23:25:44Z</updated>
		
		<summary type="html">&lt;p&gt;TBeattie: Please remove statement about %n&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Under &amp;quot;Walking the stack&amp;quot;, the statement &amp;quot;the %n directive in printf()... takes an int* and writes the number of bytes so far to that location&amp;quot; is incorrect.  &amp;quot;%n&amp;quot; is defined for the sscanf() function, but not for printf()... unless somebody knows of a non-standard implementation of C which does behave in this way, in which case that implementation should be identified.&lt;/div&gt;</summary>
		<author><name>TBeattie</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15518</id>
		<title>Reviewing Code for Buffer Overruns and Overflows</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15518"/>
				<updated>2007-01-17T23:19:05Z</updated>
		
		<summary type="html">&lt;p&gt;TBeattie: /* Crashing an application: */ fix the example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
==The buffer ==&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
==How to locate the potentially vulnerable code==&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays''':&lt;br /&gt;
  int x[20];&lt;br /&gt;
  int y[20][5];&lt;br /&gt;
  int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
  printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
  %x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
'''Over flows:'''&lt;br /&gt;
&lt;br /&gt;
  strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Vulnerable Patterns for buffer overflows==&lt;br /&gt;
&lt;br /&gt;
===‘Vanilla’ buffer overflow: ===&lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  void copyData(char *userId) {  &lt;br /&gt;
     char  smallBuffer['''10''']; // size of 10  &lt;br /&gt;
     '''strcpy'''(smallBuffer, userId);&lt;br /&gt;
  }  &lt;br /&gt;
  int main(int argc, char *argv[]) {  &lt;br /&gt;
  char *userId = &amp;quot;'''01234567890'''&amp;quot;; // Payload of 11&lt;br /&gt;
  copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Format String: ===&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs to output information, print error messages, or process strings.&lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
  %x        hexadecimal (unsigned int)&lt;br /&gt;
  %s        string ((const) (unsigned) char *)&lt;br /&gt;
  %n        number of bytes written so far, (* int)&lt;br /&gt;
  %d        decimal (int)&lt;br /&gt;
  %u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printed as a string.&lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not meant to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
===Crashing an application: ===&lt;br /&gt;
&lt;br /&gt;
  printf (User_Input);&lt;br /&gt;
&lt;br /&gt;
If we supply %x (hex unsigned int) as the input, the '''printf''' function shall expect to find an integer relating to that format string, but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
===Walking the stack: ===&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Worse again is using the '''%n''' directive in '''printf()'''. This directive takes an '''int*''' and '''writes''' the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the '''printf()''' family of functions,  '''printf(),fprintf(), sprintf(), snprintf().''' Also '''syslog()''' (writes system log information) and setproctitle(''const char *fmt'', ''...''); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Integer overflows: ===&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
            int val;&lt;br /&gt;
            val = 0x7fffffff; 	/* 2147483647*/&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
            return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111; this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
        }&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the '''''if '''''condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
The line '''myArray[v2]  = v1''' assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Good Patterns &amp;amp; procedures to prevent buffer overflows: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
C library functions such as '''strcpy (), strcat (), sprintf ()''' and '''vsprintf ()''' operate on null terminated strings and perform no bounds checking. '''gets ()''' is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The '''scanf ()''' family of functions also may result in buffer overflows.&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==.NET &amp;amp; Java ==&lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is ''managed''. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked, buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>TBeattie</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15517</id>
		<title>Reviewing Code for Buffer Overruns and Overflows</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_Buffer_Overruns_and_Overflows&amp;diff=15517"/>
				<updated>2007-01-17T23:16:59Z</updated>
		
		<summary type="html">&lt;p&gt;TBeattie: /* The Format String: */ typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
==The buffer ==&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
==How to locate the potentially vulnerable code==&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays''':&lt;br /&gt;
  int x[20];&lt;br /&gt;
  int y[20][5];&lt;br /&gt;
  int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
  printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
  %x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
'''Over flows:'''&lt;br /&gt;
&lt;br /&gt;
  strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Vulnerable Patterns for buffer overflows==&lt;br /&gt;
&lt;br /&gt;
===‘Vanilla’ buffer overflow: ===&lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  void copyData(char *userId) {  &lt;br /&gt;
     char  smallBuffer['''10''']; // size of 10  &lt;br /&gt;
     '''strcpy'''(smallBuffer, userId);&lt;br /&gt;
  }  &lt;br /&gt;
  int main(int argc, char *argv[]) {  &lt;br /&gt;
  char *userId = &amp;quot;'''01234567890'''&amp;quot;; // Payload of 11&lt;br /&gt;
  copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Format String: ===&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs to output information, print error messages, or process strings.&lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
  %x        hexadecimal (unsigned int)&lt;br /&gt;
  %s        string ((const) (unsigned) char *)&lt;br /&gt;
  %n        number of bytes written so far, (* int)&lt;br /&gt;
  %d        decimal (int)&lt;br /&gt;
  %u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printed as a string.&lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not meant to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
===Crashing an application: ===&lt;br /&gt;
&lt;br /&gt;
  printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
If we supply %x (hex unsigned int) as the input, the '''printf''' function shall expect to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
===Walking the stack: ===&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Worse again is using the '''%n''' directive in '''printf()'''. This directive takes an '''int*''' and '''writes''' the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the '''printf()''' family of functions,  '''printf(),fprintf(), sprintf(), snprintf().''' Also '''syslog()''' (writes system log information) and setproctitle(''const char *fmt'', ''...''); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Integer overflows: ===&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
            int val;&lt;br /&gt;
            val = 0x7fffffff; 	/* 2147483647*/&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
            return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111; this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
        }&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the '''''if '''''condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
The line '''myArray[v2]  = v1''' assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Good Patterns &amp;amp; procedures to prevent buffer overflows: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
C library functions such as '''strcpy (), strcat (), sprintf ()''' and '''vsprintf ()''' operate on null terminated strings and perform no bounds checking. '''gets ()''' is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The '''scanf ()''' family of functions also may result in buffer overflows.&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==.NET &amp;amp; Java ==&lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is ''managed''. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked, buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>TBeattie</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Code_Review_Introduction&amp;diff=15516</id>
		<title>Code Review Introduction</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Code_Review_Introduction&amp;diff=15516"/>
				<updated>2007-01-17T23:07:20Z</updated>
		
		<summary type="html">&lt;p&gt;TBeattie: /* Introduction */ typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
==Preface==&lt;br /&gt;
&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and is now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do; to bring security in web application development into the mainstream, to make it a selling point. 30% to 35% of Microsoft’s budget for “Longhorn” is earmarked for security, a sign of the times. &amp;lt;u&amp;gt;http://news.bbc.co.uk/2/hi/business/4516269.stm&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a puristic point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purist in the real world.&lt;br /&gt;
&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
  ''“We never get hacked (that I know of), we don’t need security” ''&lt;br /&gt;
&lt;br /&gt;
  ''“We never get hacked, we got a firewall”.''&lt;br /&gt;
&lt;br /&gt;
  ''Question: “How much does security cost”? Answer: “How much shall no security cost”?''&lt;br /&gt;
&lt;br /&gt;
  ''&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
&lt;br /&gt;
  ''	- I love proverbs as you can see.''&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
&lt;br /&gt;
==The Basics: What we know we don’t know and what we know we know. ==&lt;br /&gt;
&lt;br /&gt;
===What is Secure Code Review? ===&lt;br /&gt;
&lt;br /&gt;
Secure code review is the process of auditing code for an application on a line by line basis for its security quality. Code review is a way of ensuring that the application is developed in an appropriate fashion so as to be “self defending” in its given environment.&lt;br /&gt;
&lt;br /&gt;
Secure Code review is a method of assuring secure application developers are following secure development techniques. A general rule of thumb is that a pen test should not discover any additional application vulnerabilities relating to the developed code after the application has undergone a proper secure code review. &lt;br /&gt;
&lt;br /&gt;
Secure code review is a manual process. It is labour intensive and not very scalable but it is accurate if performed by humans (and not tools, so far).&lt;br /&gt;
&lt;br /&gt;
Tools can be used to perform this task but they always need human verification. Tools do not understand context, which is the keystone of secure code review. Tools are good at assessing large amounts of code and pointing out possible issues but a person needs to verify every single result and also figure out the false positives and worse again the false negatives.&lt;br /&gt;
&lt;br /&gt;
There are many source code review tool vendors. None have created a “silver bullet” at a reasonable cost. Vendor tools that are effective cost upwards around $60,000 USD per developer seat.&lt;br /&gt;
&lt;br /&gt;
Code review can be broken down into a number of discrete phases.&lt;br /&gt;
&lt;br /&gt;
# Discovery (Pre Transaction analysis).&lt;br /&gt;
# Transactional analysis.&lt;br /&gt;
# Post transaction analysis. &lt;br /&gt;
# Procedure peer review.&lt;br /&gt;
# Reporting &amp;amp; Presentation.&lt;br /&gt;
&lt;br /&gt;
===Laying the ground work ===&lt;br /&gt;
&lt;br /&gt;
(Ideally the reviewer should be involved in the design phase of the application, but this is not always possible so we assume the reviewer was not.)&lt;br /&gt;
&lt;br /&gt;
There are two scenarios to look at.&lt;br /&gt;
&lt;br /&gt;
# The security consultant was involved since project inception and has guided and helped integrate security into the SDLC.&lt;br /&gt;
# The security consultant is brought into the project near project end and is presented with a mountain of code, has no insight into the design, functionality or business requirements.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
So we’ve got 100K lines of code for secure code inspection, how do we handle this?&lt;br /&gt;
&lt;br /&gt;
The most important step is collaboration with developers. Obtaining information from the developers is the most time saving method for performing an accurate code review in a timely manner.&lt;br /&gt;
&lt;br /&gt;
Performing code review can feel like an audit, and everybody hates being audited. The way to approach this is to create an atmosphere of collaboration between the reviewer, the development team &amp;amp; vested interests. Portraying an image of an advisor and not a policeman is very important if you wish to get full co-operation from the development team.&lt;br /&gt;
&lt;br /&gt;
“''Help me help you''” is the approach and ideal that needs to be communicated.&lt;br /&gt;
&lt;br /&gt;
===Discovery: Gathering the information ===&lt;br /&gt;
&lt;br /&gt;
As mentioned above talking to developers is arguably the most accurate and definitely the quickest way of gaining insight into the application. &lt;br /&gt;
&lt;br /&gt;
A culture of collaboration between the security analyst and the development team is important to establish.&lt;br /&gt;
&lt;br /&gt;
Other artefacts required would be design documents, business requirements, functional specifications and any other relating information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Before we start:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The reviewer(s) need to be familiar with:&lt;br /&gt;
# '''Code''': The language used, the features and issues of that language from a security perspective. The issues one needs to look out for and best practices from a security and performance perspective.&lt;br /&gt;
# '''Context''': They need to be familiar with the application being reviewed. All security is in context of what we are trying to secure. Recommending military standard security mechanisms on an application that vends apples would be over-kill, and out of context. What type of data is being manipulated or processed and what would the damage to the company be if this data was compromised. Context is the “''Holy Grail''” of secure code inspection and risk assessment…we’ll see more later.&lt;br /&gt;
# '''Audience''': From 2 (above) we need to know the users of the application, is it externally facing or internal to “Trusted” users.  Does this application talk to other entities (machines/services). Do humans use this application?&lt;br /&gt;
# '''Importance''': The availability of the application is also important. Shall the enterprise be affected in any great way if the application is “bounced” or shut down for a significant or insignificant amount of time?&lt;br /&gt;
&lt;br /&gt;
===Context, Context, Context ===&lt;br /&gt;
&lt;br /&gt;
The context in which the application is intended to operate is a very important issue in establishing potential risk. &lt;br /&gt;
&lt;br /&gt;
Defining context should provide us with the following information:&lt;br /&gt;
*Establish the importance of application to enterprise.&lt;br /&gt;
*Establish the boundaries of the application context.&lt;br /&gt;
*Establish the trust relationships between entities.&lt;br /&gt;
*Establish potential threats and possible countermeasures.&lt;br /&gt;
&lt;br /&gt;
So we can establish something akin to a threat model. Take into account where our application sits, what its expected to do and who uses it.&lt;br /&gt;
&lt;br /&gt;
Simple questions like:&lt;br /&gt;
&lt;br /&gt;
“'''''What type/how sensitive is the data/Asset contained in the application?'''''”:&lt;br /&gt;
&lt;br /&gt;
This is a keystone to security and assessing possible risk to the application. How desirable is the information? What effect would it have on the enterprise if the information were compromised in any way?&lt;br /&gt;
&lt;br /&gt;
“'''''Is the application internal or external facing?'''''”, “'''''Who uses the application, are they trusted users?'''”''&lt;br /&gt;
&lt;br /&gt;
This is a bit of a false sense of security as attacks take place by internal/trusted users more often than is acknowledged. It does give us context that the application ''should'' be limited to a finite number of identified users but its not a guarantee that these users shall all behave properly!!”&lt;br /&gt;
&lt;br /&gt;
''“'''Where does the application host sit'''?”''&lt;br /&gt;
&lt;br /&gt;
Users should not be allowed past the DMZ into the LAN without being authenticated. Internal users also need to be authenticated. No authentication = no accountability and a weak audit trail.&lt;br /&gt;
&lt;br /&gt;
If there are internal and external users, what are the differences from a security standpoint? How do we identify one from another. How does authorisation work?&lt;br /&gt;
&lt;br /&gt;
“'''''How important is this application to the enterprise?'''''”.&lt;br /&gt;
&lt;br /&gt;
Is the application of minor significance or a Tier A / Mission critical application, which the enterprise would fail without. Any good web application development policy would have additional requirements for different applications of differing importance to the enterprise. It would be the analyst’s job to ensure the policy was followed from a code perspective also.&lt;br /&gt;
&lt;br /&gt;
A useful approach is to present the team with a checklist, which asks the relevant, questions pertaining to any web application.:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Checklist ===&lt;br /&gt;
&lt;br /&gt;
Defining a generic checklist which can be filled out by the development team is of high value is the checklist asks the correct questions in order to give us context. The checklist should cover the “Usual Suspects” in application security such as:&lt;br /&gt;
&lt;br /&gt;
* Authentication&lt;br /&gt;
* Authorization&lt;br /&gt;
* Data Validation (a''nother “holy grail”'')&lt;br /&gt;
* Session management&lt;br /&gt;
* Logging&lt;br /&gt;
* Error handling&lt;br /&gt;
* Cryptography&lt;br /&gt;
* Topology (where is this app in the network context).&lt;br /&gt;
&lt;br /&gt;
An example can be found:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;u&amp;gt;http://www.owasp.org/docroot/owasp/misc/designreviewchecklist.doc&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The checklist is a good barometer for the level of security the developers have attempted or thought of.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>TBeattie</name></author>	</entry>

	</feed>