Skip to content

Commit 2fc17ae

Browse files
author
Nikita Kraiouchkine
committed
Move rules to OutOfBounds and implement ARR39-C
1 parent 3387b68 commit 2fc17ae

File tree

9 files changed

+551
-2
lines changed

9 files changed

+551
-2
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# ARR39-C: Do not add or subtract a scaled integer to a pointer
2+
3+
This query implements the CERT-C rule ARR39-C:
4+
5+
> Do not add or subtract a scaled integer to a pointer
6+
7+
8+
9+
## Description
10+
11+
Pointer arithmetic is appropriate only when the pointer argument refers to an array (see [ARR37-C. Do not add or subtract an integer to a pointer to a non-array object](https://wiki.sei.cmu.edu/confluence/display/c/ARR37-C.+Do+not+add+or+subtract+an+integer+to+a+pointer+to+a+non-array+object)), including an array of bytes. When performing pointer arithmetic, the size of the value to add to or subtract from a pointer is automatically scaled to the size of the type of the referenced array object. Adding or subtracting a scaled integer value to or from a pointer is invalid because it may yield a pointer that does not point to an element within or one past the end of the array. (See [ARR30-C. Do not form or use out-of-bounds pointers or array subscripts](https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts).)
12+
13+
Adding a pointer to an array of a type other than character to the result of the `sizeof` operator or `offsetof` macro, which returns a size and an offset, respectively, violates this rule. However, adding an array pointer to the number of array elements, for example, by using the `arr[sizeof(arr)/sizeof(arr[0])])` idiom, is allowed provided that `arr` refers to an array and not a pointer.
14+
15+
## Noncompliant Code Example
16+
17+
In this noncompliant code example, `sizeof(buf)` is added to the array `buf`. This example is noncompliant because `sizeof(buf)` is scaled by `int` and then scaled again when added to `buf`.
18+
19+
```cpp
20+
enum { INTBUFSIZE = 80 };
21+
22+
extern int getdata(void);
23+
int buf[INTBUFSIZE];
24+
25+
void func(void) {
26+
int *buf_ptr = buf;
27+
28+
while (buf_ptr < (buf + sizeof(buf))) {
29+
*buf_ptr++ = getdata();
30+
}
31+
}
32+
```
33+
34+
## Compliant Solution
35+
36+
This compliant solution uses an unscaled integer to obtain a pointer to the end of the array:
37+
38+
```cpp
39+
enum { INTBUFSIZE = 80 };
40+
41+
extern int getdata(void);
42+
int buf[INTBUFSIZE];
43+
44+
void func(void) {
45+
int *buf_ptr = buf;
46+
47+
while (buf_ptr < (buf + INTBUFSIZE)) {
48+
*buf_ptr++ = getdata();
49+
}
50+
}
51+
```
52+
53+
## Noncompliant Code Example
54+
55+
In this noncompliant code example, `skip` is added to the pointer `s`. However, `skip` represents the byte offset of `ull_b` in `struct big`. When added to `s`, `skip` is scaled by the size of `struct big`.
56+
57+
```cpp
58+
#include <string.h>
59+
#include <stdlib.h>
60+
#include <stddef.h>
61+
62+
struct big {
63+
unsigned long long ull_a;
64+
unsigned long long ull_b;
65+
unsigned long long ull_c;
66+
int si_e;
67+
int si_f;
68+
};
69+
70+
void func(void) {
71+
size_t skip = offsetof(struct big, ull_b);
72+
struct big *s = (struct big *)malloc(sizeof(struct big));
73+
if (s == NULL) {
74+
/* Handle malloc() error */
75+
}
76+
77+
memset(s + skip, 0, sizeof(struct big) - skip);
78+
/* ... */
79+
free(s);
80+
s = NULL;
81+
}
82+
```
83+
84+
## Compliant Solution
85+
86+
This compliant solution uses an `unsigned char *` to calculate the offset instead of using a `struct big *`, which would result in scaled arithmetic:
87+
88+
```cpp
89+
#include <string.h>
90+
#include <stdlib.h>
91+
#include <stddef.h>
92+
93+
struct big {
94+
unsigned long long ull_a;
95+
unsigned long long ull_b;
96+
unsigned long long ull_c;
97+
int si_d;
98+
int si_e;
99+
};
100+
101+
void func(void) {
102+
size_t skip = offsetof(struct big, ull_b);
103+
unsigned char *ptr = (unsigned char *)malloc(
104+
sizeof(struct big)
105+
);
106+
if (ptr == NULL) {
107+
/* Handle malloc() error */
108+
}
109+
110+
memset(ptr + skip, 0, sizeof(struct big) - skip);
111+
/* ... */
112+
free(ptr);
113+
ptr = NULL;
114+
}
115+
```
116+
117+
## Noncompliant Code Example
118+
119+
In this noncompliant code example, `wcslen(error_msg) * sizeof(wchar_t)` bytes are scaled by the size of `wchar_t` when added to `error_msg`:
120+
121+
```cpp
122+
#include <wchar.h>
123+
#include <stdio.h>
124+
125+
enum { WCHAR_BUF = 128 };
126+
127+
void func(void) {
128+
wchar_t error_msg[WCHAR_BUF];
129+
130+
wcscpy(error_msg, L"Error: ");
131+
fgetws(error_msg + wcslen(error_msg) * sizeof(wchar_t),
132+
WCHAR_BUF - 7, stdin);
133+
/* ... */
134+
}
135+
```
136+
137+
## Compliant Solution
138+
139+
This compliant solution does not scale the length of the string; `wcslen()` returns the number of characters and the addition to `error_msg` is scaled:
140+
141+
```cpp
142+
#include <wchar.h>
143+
#include <stdio.h>
144+
145+
enum { WCHAR_BUF = 128 };
146+
const wchar_t ERROR_PREFIX[7] = L"Error: ";
147+
148+
void func(void) {
149+
const size_t prefix_len = wcslen(ERROR_PREFIX);
150+
wchar_t error_msg[WCHAR_BUF];
151+
152+
wcscpy(error_msg, ERROR_PREFIX);
153+
fgetws(error_msg + prefix_len,
154+
WCHAR_BUF - prefix_len, stdin);
155+
/* ... */
156+
}
157+
```
158+
159+
## Risk Assessment
160+
161+
Failure to understand and properly use pointer arithmetic can allow an attacker to execute arbitrary code.
162+
163+
<table> <tbody> <tr> <th> Rule </th> <th> Severity </th> <th> Likelihood </th> <th> Remediation Cost </th> <th> Priority </th> <th> Level </th> </tr> <tr> <td> ARR39-C </td> <td> High </td> <td> Probable </td> <td> High </td> <td> <strong>P6</strong> </td> <td> <strong>L2</strong> </td> </tr> </tbody> </table>
164+
165+
166+
## Automated Detection
167+
168+
<table> <tbody> <tr> <th> Tool </th> <th> Version </th> <th> Checker </th> <th> Description </th> </tr> <tr> <td> <a> Astrée </a> </td> <td> 22.04 </td> <td> <strong>scaled-pointer-arithmetic </strong> </td> <td> Partially checked Besides direct rule violations, Astrée reports all (resulting) out-of-bound array accesses. </td> </tr> <tr> <td> <a> Axivion Bauhaus Suite </a> </td> <td> 7.2.0 </td> <td> <strong>CertC-ARR39</strong> </td> <td> Fully implemented </td> </tr> <tr> <td> <a> CodeSonar </a> </td> <td> 7.0p0 </td> <td> <strong>LANG.MEM.BO</strong> <strong>LANG.MEM.BU</strong> <strong>LANG.MEM.TBA</strong> <strong>LANG.MEM.TO</strong> <strong>LANG.MEM.TULANG.STRUCT.PARITH</strong> <strong>LANG.STRUCT.PBB</strong> <strong>LANG.STRUCT.PPE</strong> <strong>BADFUNC.BO.\*</strong> </td> <td> Buffer overrun Buffer underrun Tainted buffer access Type overrun Type underrun Pointer Arithmetic Pointer before beginning of object Pointer past end of object A collection of warning classes that report uses of library functions prone to internal buffer overflows. </td> </tr> <tr> <td> <a> Coverity </a> </td> <td> 2017.07 </td> <td> <strong>BAD_SIZEOF</strong> </td> <td> Partially implemented </td> </tr> <tr> <td> <a> Helix QAC </a> </td> <td> 2022.2 </td> <td> <strong>C4955, C4956, C4957</strong> <strong>C++4955, C++4956, C++4957</strong> </td> <td> </td> </tr> <tr> <td> <a> Klocwork </a> </td> <td> 2022.2 </td> <td> <strong>MISRA.PTR.ARITH.2012</strong> </td> <td> </td> </tr> <tr> <td> <a> LDRA tool suite </a> </td> <td> 9.7.1 </td> <td> <strong>47 S, 489 S, 567 S,64 X, 66 X, 68 X,69 X, 70 X, 71 X</strong> </td> <td> Partially implemented </td> </tr> <tr> <td> <a> Parasoft C/C++test </a> </td> <td> 2022.1 </td> <td> <strong>CERT_C-ARR39-a</strong> <strong>CERT_C-ARR39-b</strong> <strong>CERT_C-ARR39-c</strong> </td> <td> Avoid accessing arrays out of bounds Pointer arithmetic should not be used Do not add or subtract a scaled integer to a pointer </td> </tr> <tr> <td> Polyspace Bug Finder </td> <td> R2022a </td> <td> <a> CERT C: Rule ARR39-C </a> </td> <td> Checks for: Incorrect pointer scalingncorrect pointer scaling, pointer access out of boundsointer access out of bounds, possible misuse of sizeofossible misuse of sizeof. Rule partially covered. </td> </tr> <tr> <td> <a> PRQA QA-C </a> </td> <td> 9.7 </td> <td> <strong> 4955, 4956, 4957</strong> </td> <td> </td> </tr> <tr> <td> <a> PRQA QA-C++ </a> </td> <td> 4.4 </td> <td> <strong>4955, 4956, 4957</strong> </td> <td> </td> </tr> <tr> <td> <a> RuleChecker </a> </td> <td> 22.04 </td> <td> <strong>scaled-pointer-arithmetic</strong> </td> <td> Partially checked </td> </tr> <tr> <td> <a> TrustInSoft Analyzer </a> </td> <td> 1.38 </td> <td> <strong>index_in_address</strong> </td> <td> Exhaustively detects undefined behavior (see <a> one compliant and one non-compliant example </a> ). </td> </tr> </tbody> </table>
169+
170+
171+
## Related Vulnerabilities
172+
173+
Search for [vulnerabilities](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-vulnerability) resulting from the violation of this rule on the [CERT website](https://www.kb.cert.org/vulnotes/bymetric?searchview&query=FIELD+KEYWORDS+contains+EXP41-C).
174+
175+
## Related Guidelines
176+
177+
[Key here](https://wiki.sei.cmu.edu/confluence/display/c/How+this+Coding+Standard+is+Organized#HowthisCodingStandardisOrganized-RelatedGuidelines) (explains table format and definitions)
178+
179+
<table> <tbody> <tr> <th> Taxonomy </th> <th> Taxonomy item </th> <th> Relationship </th> </tr> <tr> <td> <a> CERT C Secure Coding Standard </a> </td> <td> <a> ARR30-C. Do not form or use out-of-bounds pointers or array subscripts </a> </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> CERT C Secure Coding Standard </a> </td> <td> <a> ARR37-C. Do not add or subtract an integer to a pointer to a non-array object </a> </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> ISO/IEC TR 24772:2013 </a> </td> <td> Pointer Casting and Pointer Type Changes \[HFC\] </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> ISO/IEC TR 24772:2013 </a> </td> <td> Pointer Arithmetic \[RVG\] </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> MISRA C:2012 </a> </td> <td> Rule 18.1 (required) </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> MISRA C:2012 </a> </td> <td> Rule 18.2 (required) </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> MISRA C:2012 </a> </td> <td> Rule 18.3 (required) </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> MISRA C:2012 </a> </td> <td> Rule 18.4 (advisory) </td> <td> Prior to 2018-01-12: CERT: Unspecified Relationship </td> </tr> <tr> <td> <a> CWE 2.11 </a> </td> <td> <a> CWE-468 </a> , Incorrect Pointer Scaling </td> <td> 2017-07-07: CERT: Exact </td> </tr> </tbody> </table>
180+
181+
182+
## Bibliography
183+
184+
<table> <tbody> <tr> <td> \[ <a> Dowd 2006 </a> \] </td> <td> Chapter 6, "C Language Issues" </td> </tr> <tr> <td> \[ <a> Murenin 07 </a> \] </td> <td> </td> </tr> </tbody> </table>
185+
186+
187+
## Implementation notes
188+
189+
None
190+
191+
## References
192+
193+
* CERT-C: [ARR39-C: Do not add or subtract a scaled integer to a pointer](https://wiki.sei.cmu.edu/confluence/display/c)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @id c/cert/do-not-add-or-subtract-a-scaled-integer-to-a-pointer
3+
* @name ARR39-C: Do not add or subtract a scaled integer to a pointer
4+
* @description Adding or subtracting a scaled integer value to or from a pointer may yield an
5+
* out-of-bounds pointer.
6+
* @kind path-problem
7+
* @precision high
8+
* @problem.severity error
9+
* @tags external/cert/id/arr39-c
10+
* correctness
11+
* external/cert/obligation/rule
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.cert
16+
import semmle.code.cpp.dataflow.TaintTracking
17+
import DataFlow::PathGraph
18+
19+
/**
20+
* An expression which performs pointer arithmetic
21+
*/
22+
abstract class PointerArithmeticExpr extends Expr {
23+
abstract Expr getPointer();
24+
25+
abstract Expr getOperand();
26+
}
27+
28+
class SimplePointerArithmeticExpr extends PointerArithmeticExpr, PointerArithmeticOperation {
29+
override Expr getPointer() { result = this.getLeftOperand() }
30+
31+
override Expr getOperand() { result = this.getRightOperand() }
32+
}
33+
34+
class AssignPointerArithmeticExpr extends PointerArithmeticExpr, AssignOperation {
35+
AssignPointerArithmeticExpr() {
36+
this instanceof AssignPointerAddExpr or
37+
this instanceof AssignPointerSubExpr
38+
}
39+
40+
override Expr getPointer() { result = this.getLValue() }
41+
42+
override Expr getOperand() { result = this.getRValue() }
43+
}
44+
45+
class ArrayPointerArithmeticExpr extends PointerArithmeticExpr, ArrayExpr {
46+
override Expr getPointer() { result = this.getArrayBase() }
47+
48+
override Expr getOperand() { result = this.getArrayOffset() }
49+
}
50+
51+
class OffsetOfExpr extends Expr {
52+
OffsetOfExpr() {
53+
this instanceof BuiltInOperationBuiltInOffsetOf
54+
or
55+
exists(MacroInvocation mi | mi.getMacroName() = "offsetof" and mi.getExpr() = this)
56+
}
57+
}
58+
59+
/**
60+
* An array expression conforming to the "arr[ sizeof(arr)/sizeof(arr[ 0 ]) ]" idiom
61+
*/
62+
class ArrayCountOfExpr extends ArrayExpr {
63+
ArrayCountOfExpr() {
64+
exists(DivExpr div, Variable arr, VariableAccess left, ArrayExpr right |
65+
div = this.getArrayOffset() and
66+
arr = this.getArrayBase().(VariableAccess).getTarget() and
67+
// holds if the dividend is sizeof(arr)
68+
left = div.getLeftOperand().(SizeofExprOperator).getExprOperand() and
69+
left.getTarget() = this.getArrayBase().(VariableAccess).getTarget() and
70+
// holds if the divisor is sizeof(arr[0])
71+
right = div.getRightOperand().(SizeofExprOperator).getExprOperand() and
72+
right.getArrayBase().(VariableAccess).getTarget() = arr and
73+
right.getArrayOffset().(Literal).getValue() = "0"
74+
)
75+
}
76+
}
77+
78+
class ScaledIntegerExpr extends Expr {
79+
ScaledIntegerExpr() {
80+
not this.getParent*() instanceof ArrayCountOfExpr and
81+
(
82+
this.(SizeofExprOperator).getExprOperand().getType().getSize() > 1
83+
or
84+
this.(SizeofTypeOperator).getTypeOperand().getSize() > 1
85+
or
86+
this instanceof OffsetOfExpr
87+
)
88+
}
89+
}
90+
91+
class ScaledIntegerPointerArithmeticConfig extends DataFlow::Configuration {
92+
ScaledIntegerPointerArithmeticConfig() { this = "ScaledIntegerPointerArithmeticConfig" }
93+
94+
override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof ScaledIntegerExpr }
95+
96+
override predicate isSink(DataFlow::Node sink) {
97+
exists(PointerArithmeticExpr pa |
98+
// exclude pointers to 1-byte types as they do not scale
99+
pa.getPointer().getFullyConverted().getType().(DerivedType).getBaseType().getSize() != 1 and
100+
pa.getOperand().getAChild*() = sink.asExpr()
101+
)
102+
}
103+
}
104+
105+
from ScaledIntegerPointerArithmeticConfig config, DataFlow::PathNode src, DataFlow::PathNode sink
106+
where
107+
not isExcluded(sink.getNode().asExpr(),
108+
Pointers2Package::doNotAddOrSubtractAScaledIntegerToAPointerQuery()) and
109+
config.hasFlowPath(src, sink)
110+
select sink, src, sink, "Scaled integer used in pointer arithmetic."
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
edges
2+
| test.c:7:13:7:14 | p1 | test.c:9:9:9:10 | p1 |
3+
| test.c:16:19:16:41 | ... - ... | test.c:18:26:18:31 | offset |
4+
| test.c:16:19:16:41 | ... - ... | test.c:29:6:29:11 | offset |
5+
| test.c:17:17:17:26 | sizeof(<expr>) | test.c:23:9:23:12 | size |
6+
| test.c:29:6:29:11 | offset | test.c:7:13:7:14 | p1 |
7+
nodes
8+
| test.c:7:13:7:14 | p1 | semmle.label | p1 |
9+
| test.c:9:9:9:10 | p1 | semmle.label | p1 |
10+
| test.c:16:19:16:41 | ... - ... | semmle.label | ... - ... |
11+
| test.c:17:17:17:26 | sizeof(<expr>) | semmle.label | sizeof(<expr>) |
12+
| test.c:18:26:18:31 | offset | semmle.label | offset |
13+
| test.c:23:9:23:12 | size | semmle.label | size |
14+
| test.c:25:9:25:18 | sizeof(<expr>) | semmle.label | sizeof(<expr>) |
15+
| test.c:27:17:27:26 | sizeof(<expr>) | semmle.label | sizeof(<expr>) |
16+
| test.c:29:6:29:11 | offset | semmle.label | offset |
17+
subpaths
18+
#select
19+
| test.c:9:9:9:10 | p1 | test.c:16:19:16:41 | ... - ... | test.c:9:9:9:10 | p1 | Scaled integer used in pointer arithmetic. |
20+
| test.c:18:26:18:31 | offset | test.c:16:19:16:41 | ... - ... | test.c:18:26:18:31 | offset | Scaled integer used in pointer arithmetic. |
21+
| test.c:23:9:23:12 | size | test.c:17:17:17:26 | sizeof(<expr>) | test.c:23:9:23:12 | size | Scaled integer used in pointer arithmetic. |
22+
| test.c:25:9:25:18 | sizeof(<expr>) | test.c:25:9:25:18 | sizeof(<expr>) | test.c:25:9:25:18 | sizeof(<expr>) | Scaled integer used in pointer arithmetic. |
23+
| test.c:27:17:27:26 | sizeof(<expr>) | test.c:27:17:27:26 | sizeof(<expr>) | test.c:27:17:27:26 | sizeof(<expr>) | Scaled integer used in pointer arithmetic. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/ARR39-C/DoNotAddOrSubtractAScaledIntegerToAPointer.ql

c/cert/test/rules/ARR39-C/test.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stddef.h>
2+
3+
struct s1 {
4+
int v1, v2, v3;
5+
};
6+
7+
void f2(int p1, int p2) {
8+
int *v1;
9+
v1 += p1; // NON_COMPLIANT
10+
v1 += p2; // COMPLIANT
11+
}
12+
13+
void f1() {
14+
int v1[10];
15+
struct s1 *v2;
16+
size_t offset = offsetof(struct s1, v2);
17+
size_t size = sizeof(v1);
18+
int *v3 = (int *)(v2 + offset); // NON_COMPLIANT
19+
char *v4 = (char *)v2 + offset; // COMPLIANT
20+
v3 = (int *)(((char *)v2) + offset); // COMPLIANT
21+
v2++; // COMPLIANT
22+
v2 += 10; // COMPLIANT
23+
v3 += size; // NON_COMPLIANT
24+
v3++; // COMPLIANT
25+
v3 += sizeof(v1); // NON_COMPLIANT
26+
(void)v1[sizeof(v1) / sizeof(v1[0])]; // COMPLIANT
27+
(void)v1[10 / sizeof(v1)]; // NON_COMPLIANT
28+
v4 += offset; // COMPLIANT
29+
f2(offset, 2);
30+
}

0 commit comments

Comments
 (0)