...
Code Block | ||||
---|---|---|---|---|
| ||||
int buf[INTBUFSIZE];
int *buf_ptr = buf;
while (havedata() && buf_ptr < &buf[INTBUFSIZE]) {
*buf_ptr++ = parseint(getdata());
}
|
...
Noncompliant Code Example
The following This noncompliant code example is based on a flaw in the OpenBSD operating system. An integer, skip
, is added as an offset to a pointer of type struct big
. The adjusted pointer is then used as a destination address in a call to memset()
. However, when skip
is added to the struct big
pointer, it is automatically scaled by the size of struct big
, which is 32 bytes (assuming 4-byte integers, 8-byte long long
integers, and no structure padding). This scaling results in the call to memset()
writing to unintended memory.
...
Failure to understand and properly use pointer arithmetic can allow an attacker to execute arbitrary code.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP08-C |
High | Probable | High | P6 | L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| Supported: Astrée reports potential runtime errors resulting from invalid pointer arithmetics. | |||||||
CodeSonar |
| LANG.STRUCT.PARITH | Pointer arithmetic | ||||||
Helix QAC |
| C0488, C2930, C2931, C2932, C2933 | |||||||
Klocwork |
| ABV.ITERATOR ABV.GENERAL ABV.GENERAL.MULTIDIMENSION | |||||||
LDRA tool suite |
| 45 D | Partially implemented |
2930
2814
0488
Partially Implemented
Parasoft C/C++test |
| CERT_C-EXP08-a | Pointer arithmetic should not be used | ||||||
Parasoft Insure++ | Runtime analysis | ||||||||
PC-lint Plus |
| 416 | Partially supported | ||||||
Polyspace Bug Finder |
| Checks for:
Rec. fully supported. | |||||||
PVS-Studio |
| V503, V520, V574, V600, V613, V619, V620, V643, V650, V687, V769, V1004 |
How long is 4 yards plus 3 feet? It is obvious from elementary arithmetic that any answer involving 7 is wrong, as the student did not take the units into account. The right method is to convert both numbers to reflect the same units.
...
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
SEI CERT C++ |
Coding Standard | VOID EXP08-CPP. Ensure pointer arithmetic is used correctly |
ISO/IEC TR 24772:2013 | Pointer Casting and Pointer Type Changes [HFC] Pointer Arithmetic [RVG] |
ISO/IEC TS 17961 | Forming or using out-of-bounds pointers or array subscripts [invptr] |
MISRA C:2012 | Rule 18.1 (required) Rule 18.2 (required) Rule 18.3 (required) Rule 18.4 (advisory) |
MITRE CWE | CWE-468, Incorrect pointer scaling |
Bibliography
[Dowd 2006] | Chapter 6, "C Language Issues" |
[Murenin 2007] |
...
...