...
In this example, taken from dowd Dowd, buf_ptr
is used to insert new integers into buf
, which is an array of 1024 integers. If there is data to be inserted into buf
(which is indicated by havedata()
) and buf_ptr
has not been incremented past buf + sizeof(buf)
, then an integer is inserted into buf
via buf_ptr
. However, the sizeof
operator returns the total number of bytes in buf
, which, assuming four-byte integers, is 4096 bytes. This value is then scaled to the size of an integer and added to buf
. As a result, it is possible to write integers past the end of buf
and cause a buffer overflow.
Code Block | ||
---|---|---|
| ||
int buf[1024]; int *buf_ptr = buf; while (havedata() && buf_ptr < buf + sizeof(buf)) { *buf_ptr++ = parseint(getdata()); } |
Compliant Code Example
1)
Code Block | ||
---|---|---|
| ||
int buf[BUF_LEN];
int *buf_ptr = buf;
while (havedata() && buf_ptr < buf[BUF_LEN-1])
{
*buf_ptr = parseint(getdata());
buf_ptr++;
}
|
2)To correct this example, the size of buf
can be directly added to buf
and used as an upper bound. The integer literal is scaled appropriately
Code Block | ||
---|---|---|
| ||
int buf[1024]; int *b = buf; while (havedata() && b < buf+sizeof(buf1024)) { *b++ = parseint(getdata()); } |
These corrected versions:
...
Risk Analysis
Failure to notice a coding error of this variety would easily become a buffer overflow vulnerability. In a worst case scenario this could lead to arbitrary code execution and thus hold severe risk.
Reference
...
understand and properly use pointer arithmetic can allow an attacker to execute arbitrary code.
Reference
Wiki Markup |
---|
\[[Dowd|AA. C References#Dowd 06]\] Chapter 6, "C Language Issues" (Vulnerabilities) |