Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3a90ca5

Browse filesBrowse files
authored
Merge branch 'main' into lcartey/a2-10-5-templates
2 parents c6d26d3 + 4de3634 commit 3a90ca5
Copy full SHA for 3a90ca5

File tree

123 files changed

+6235
-115
lines changed
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner

123 files changed

+6235
-115
lines changed

‎.github/touch

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

‎.github/workflows/bump-version.yml

Copy file name to clipboard
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: 📦 Bump Workflow
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
new_version:
7+
description: |
8+
The version to update to (eg: 2.6.0 or 2.6.0-dev do not include `v`).
9+
required: true
10+
jobs:
11+
12+
apply-version-bump:
13+
runs-on: ubuntu-latest
14+
name: Apply Version Bump
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Apply Bump
20+
shell: bash
21+
run: |
22+
bash ./scripts/bump_version.sh ${{ github.event.inputs.new_version }}
23+
24+
- name: Create Pull Request
25+
uses: peter-evans/create-pull-request@v4
26+
with:
27+
title: "Release Engineering: Version bump to ${{ github.event.inputs.new_version }}."
28+
body: "This PR updates codeql-coding-standards to version ${{ github.event.inputs.new_version }}."
29+
commit-message: "Version bump to ${{ github.event.inputs.new_version }}."
30+
team-reviewers: github/codeql-coding-standards
31+
delete-branch: true
32+
branch: "automation/version-bump-${{ github.event.inputs.new_version }}"

‎.vscode/tasks.json

Copy file name to clipboardExpand all lines: .vscode/tasks.json
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@
197197
"Concurrency1",
198198
"Concurrency2",
199199
"Concurrency3",
200+
"Concurrency4",
201+
"Concurrency5",
200202
"Conditionals",
201203
"Const",
202204
"DeadCode",
203205
"Declarations",
204206
"Declarations1",
207+
"Declarations2",
205208
"Exceptions1",
206209
"Exceptions2",
207210
"Expressions",
@@ -210,6 +213,7 @@
210213
"IO1",
211214
"IO2",
212215
"IO3",
216+
"IO4",
213217
"Includes",
214218
"Initialization",
215219
"IntegerConversion",

‎c/cert/src/qlpack.yml

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: cert-c-coding-standards
2-
version: 2.8.0-dev
2+
version: 2.9.0-dev
33
suites: codeql-suites
44
libraryPathDependencies: common-c-coding-standards
+192Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
## Description
9+
10+
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).)
11+
12+
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.
13+
14+
## Noncompliant Code Example
15+
16+
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`.
17+
18+
```cpp
19+
enum { INTBUFSIZE = 80 };
20+
21+
extern int getdata(void);
22+
int buf[INTBUFSIZE];
23+
24+
void func(void) {
25+
int *buf_ptr = buf;
26+
27+
while (buf_ptr < (buf + sizeof(buf))) {
28+
*buf_ptr++ = getdata();
29+
}
30+
}
31+
```
32+
33+
## Compliant Solution
34+
35+
This compliant solution uses an unscaled integer to obtain a pointer to the end of the array:
36+
37+
```cpp
38+
enum { INTBUFSIZE = 80 };
39+
40+
extern int getdata(void);
41+
int buf[INTBUFSIZE];
42+
43+
void func(void) {
44+
int *buf_ptr = buf;
45+
46+
while (buf_ptr < (buf + INTBUFSIZE)) {
47+
*buf_ptr++ = getdata();
48+
}
49+
}
50+
```
51+
52+
## Noncompliant Code Example
53+
54+
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`.
55+
56+
```cpp
57+
#include <string.h>
58+
#include <stdlib.h>
59+
#include <stddef.h>
60+
61+
struct big {
62+
unsigned long long ull_a;
63+
unsigned long long ull_b;
64+
unsigned long long ull_c;
65+
int si_e;
66+
int si_f;
67+
};
68+
69+
void func(void) {
70+
size_t skip = offsetof(struct big, ull_b);
71+
struct big *s = (struct big *)malloc(sizeof(struct big));
72+
if (s == NULL) {
73+
/* Handle malloc() error */
74+
}
75+
76+
memset(s + skip, 0, sizeof(struct big) - skip);
77+
/* ... */
78+
free(s);
79+
s = NULL;
80+
}
81+
```
82+
83+
## Compliant Solution
84+
85+
This compliant solution uses an `unsigned char *` to calculate the offset instead of using a `struct big *`, which would result in scaled arithmetic:
86+
87+
```cpp
88+
#include <string.h>
89+
#include <stdlib.h>
90+
#include <stddef.h>
91+
92+
struct big {
93+
unsigned long long ull_a;
94+
unsigned long long ull_b;
95+
unsigned long long ull_c;
96+
int si_d;
97+
int si_e;
98+
};
99+
100+
void func(void) {
101+
size_t skip = offsetof(struct big, ull_b);
102+
unsigned char *ptr = (unsigned char *)malloc(
103+
sizeof(struct big)
104+
);
105+
if (ptr == NULL) {
106+
/* Handle malloc() error */
107+
}
108+
109+
memset(ptr + skip, 0, sizeof(struct big) - skip);
110+
/* ... */
111+
free(ptr);
112+
ptr = NULL;
113+
}
114+
```
115+
116+
## Noncompliant Code Example
117+
118+
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`:
119+
120+
```cpp
121+
#include <wchar.h>
122+
#include <stdio.h>
123+
124+
enum { WCHAR_BUF = 128 };
125+
126+
void func(void) {
127+
wchar_t error_msg[WCHAR_BUF];
128+
129+
wcscpy(error_msg, L"Error: ");
130+
fgetws(error_msg + wcslen(error_msg) * sizeof(wchar_t),
131+
WCHAR_BUF - 7, stdin);
132+
/* ... */
133+
}
134+
```
135+
136+
## Compliant Solution
137+
138+
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:
139+
140+
```cpp
141+
#include <wchar.h>
142+
#include <stdio.h>
143+
144+
enum { WCHAR_BUF = 128 };
145+
const wchar_t ERROR_PREFIX[7] = L"Error: ";
146+
147+
void func(void) {
148+
const size_t prefix_len = wcslen(ERROR_PREFIX);
149+
wchar_t error_msg[WCHAR_BUF];
150+
151+
wcscpy(error_msg, ERROR_PREFIX);
152+
fgetws(error_msg + prefix_len,
153+
WCHAR_BUF - prefix_len, stdin);
154+
/* ... */
155+
}
156+
```
157+
158+
## Risk Assessment
159+
160+
Failure to understand and properly use pointer arithmetic can allow an attacker to execute arbitrary code.
161+
162+
<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>
163+
164+
165+
## Automated Detection
166+
167+
<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>
168+
169+
170+
## Related Vulnerabilities
171+
172+
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).
173+
174+
## Related Guidelines
175+
176+
[Key here](https://wiki.sei.cmu.edu/confluence/display/c/How+this+Coding+Standard+is+Organized#HowthisCodingStandardisOrganized-RelatedGuidelines) (explains table format and definitions)
177+
178+
<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>
179+
180+
181+
## Bibliography
182+
183+
<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>
184+
185+
186+
## Implementation notes
187+
188+
None
189+
190+
## References
191+
192+
* CERT-C: [ARR39-C: Do not add or subtract a scaled integer to a pointer](https://wiki.sei.cmu.edu/confluence/display/c)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.