Tuesday, December 11, 2012

Buffer overflow & Memory leak


Buffer overflow


A buffer overflow occurs when a program or process tries to store more data in a buffer(temporary data storage area) than it was intended to hold. 


A buffer overflow occurs when data written to a buffer, due to insufficient bounds checking, corrupts data values in memory addresses adjacent to the allocated buffer. Most commonly this occurs when copying strings of characters from one buffer to another.


Basic example

In the following example, a program has defined two data items which are adjacent in memory: an 8-byte-long string buffer, A, and a two-byte integer, B. Initially, A contains nothing but zero bytes, and B contains the number 1979. Characters are one byte wide.
variable nameAB
value[null string]1979
hex value000000000000000007BB
Now, the program attempts to store the null-terminated string "excessive" in the A buffer. "excessive" is 9 characters long, and A can take 8 characters. By failing to check the length of the string, it overwrites the value of B:
variable nameAB
value'e''x''c''e''s''s''i''v'25856
hex65786365737369766500
Although the programmer did not intend to change B at all, B's value has now been replaced by a number formed from part of the character string. In this example, on a big-endian system that uses ASCII, "e" followed by a zero byte would become the number 25856. If B was the only other variable data item defined by the program, writing an even longer string that went past the end of B could cause an error such as a segmentation fault, terminating the process.


Memory leak

memory leak, in computer science (or leakage, in this context), occurs when a computer program acquires memory but fails to release it back to the operating system.[1] In object-oriented programming, a memory leak may happen when an object is stored in memory but cannot be accessed by the running code.[2] A memory leak has symptoms similar to a number of other problems (see below) and generally can only be diagnosed by a programmer with access to the program source code.

No comments:

Post a Comment