diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a2a6dc0750..575dda1e99 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -249,6 +249,7 @@ "Null", "OperatorInvariants", "Operators", + "OutOfBounds", "Pointers", "Pointers1", "Pointers2", diff --git a/c/cert/src/rules/ARR30-C/DoNotFormOutOfBoundsPointersOrArraySubscripts.md b/c/cert/src/rules/ARR30-C/DoNotFormOutOfBoundsPointersOrArraySubscripts.md new file mode 100644 index 0000000000..221b008786 --- /dev/null +++ b/c/cert/src/rules/ARR30-C/DoNotFormOutOfBoundsPointersOrArraySubscripts.md @@ -0,0 +1,485 @@ +# ARR30-C: Do not form or use out-of-bounds pointers or array subscripts + +This query implements the CERT-C rule ARR30-C: + +> Do not form or use out-of-bounds pointers or array subscripts + + +## Description + +The C Standard identifies the following distinct situations in which undefined behavior (UB) can arise as a result of invalid pointer operations: + +
UB | Description | Example Code |
---|---|---|
46 | Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object. | Forming Out-of-Bounds Pointer , Null Pointer Arithmetic |
47 | Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that points just beyond the array object and is used as the operand of a unary \* operator that is evaluated. | Dereferencing Past the End Pointer , Using Past the End Index |
49 | An array subscript is out of range, even if an object is apparently accessible with the given subscript, for example, in the lvalue expression a\[1\]\[7\] given the declaration int a\[4\]\[5\] ). | Apparently Accessible Out-of-Range Index |
62 | An attempt is made to access, or generate a pointer to just past, a flexible array member of a structure when the referenced object provides no elements for that array. | Pointer Past Flexible Array Member |
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ARR30-C | High | Likely | High | P9 | L2 |
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 22.04 | array-index-rangearray-index-range-constantnull-dereferencingpointered-deallocation return-reference-local | Partially checked Can detect all accesses to invalid pointers as well as array index out-of-bounds accesses and prove their absence. This rule is only partially checked as invalid but unused pointers may not be reported. |
Axivion Bauhaus Suite | 7.2.0 | CertC-ARR30 | Can detect out-of-bound access to array / buffer |
CodeSonar | 7.3p0 | LANG.MEM.BO LANG.MEM.BU LANG.MEM.TBA LANG.MEM.TO LANG.MEM.TULANG.STRUCT.PARITH LANG.STRUCT.PBB LANG.STRUCT.PPE BADFUNC.BO.\* | 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. |
Compass/ROSE | Could be configured to catch violations of this rule. The way to catch the noncompliant code example is to first hunt for example code that follows this pattern: for (LPWSTR pwszTemp = pwszPath + 2; \*pwszTemp != L'\\\\'; \*pwszTemp++;) In particular, the iteration variable is a pointer, it gets incremented, and the loop condition does not set an upper bound on the pointer. Once this case is handled, ROSE can handle cases like the real noncompliant code example, which is effectively the same semantics, just different syntax | ||
Coverity | 2017.07 | OVERRUN NEGATIVE_RETURNS ARRAY_VS_SINGLETON BUFFER_SIZE | Can detect the access of memory past the end of a memory buffer/array Can detect when the loop bound may become negative Can detect the out-of-bound read/write to array allocated statically or dynamically Can detect buffer overflows |
Cppcheck | 1.66 | arrayIndexOutOfBounds, outOfBounds, negativeIndex, arrayIndexThenCheck, arrayIndexOutOfBoundsCond, possibleBufferAccessOutOfBounds | Context sensitive analysis of array index, pointers, etc. Array index out of bounds Buffer overflow when calling various functions memset,strcpy,.. Warns about condition (a\[i\] == 0 && i < unknown_value) and recommends that (i < unknown_value && a\[i\] == 0) is used instead Detects unsafe code when array is accessed before/after it is tested if the array index is out of bounds |
Helix QAC | 2023.1 | C2840 DF2820, DF2821, DF2822, DF2823, DF2840, DF2841, DF2842, DF2843, DF2930, DF2931, DF2932, DF2933, DF2935, DF2936, DF2937, DF2938, DF2950, DF2951, DF2952, DF2953 | |
Klocwork | 2023.1 | ABV.GENERAL ABV.GENERAL.MULTIDIMENSION NPD.FUNC.CALL.MIGHT ABV.ANY_SIZE_ARRAY ABV.STACK ABV.TAINTED ABV.UNICODE.BOUND_MAP ABV.UNICODE.FAILED_MAP ABV.UNICODE.NNTS_MAP ABV.UNICODE.SELF_MAP ABV.UNKNOWN_SIZE NNTS.MIGHT NNTS.MUST NNTS.TAINTED SV.TAINTED.INDEX_ACCESS SV.TAINTED.LOOP_BOUND | |
LDRA tool suite | 9.7.1 | 45 D, 47 S, 476 S, 489 S, 64 X, 66 X, 68 X, 69 X, 70 X, 71 X , 79 X | Partially implemented |
Parasoft C/C++test | 2022.2 | CERT_C-ARR30-a | Avoid accessing arrays out of bounds |
Parasoft Insure++ | Runtime analysis | ||
PC-lint Plus | 1.4 | 413, 415, 416, 613, 661, 662, 676 | Fully supported |
Polyspace Bug Finder | R2023a | CERT C: Rule ARR30-C | Checks for: Array access out of boundsrray access out of bounds, pointer access out of boundsointer access out of bounds, array access with tainted indexrray access with tainted index, use of tainted pointerse of tainted pointer, pointer dereference with tainted offsetointer dereference with tainted offset. Rule partially covered. |
PRQA QA-C | 9.7 | 2820, 2821, 2822, 2823, 2840, 2841, 2842, 2843, 2930, 2931, 2932, 2933, 2935, 2936, 2937, 2938, 2950, 2951, 2952, 2953 | Partially implemented |
PRQA QA-C++ | 4.4 | 2820, 2821, 2822, 2823, 2840, 2841, 2842, 2843, 2930, 2931, 2932, 2933, 2935, 2936, 2937, 2938, 2950, 2951, 2952, 2953 | Partially implemented |
PVS-Studio | 7.24 | V512 , V557 , V582 , V594 , V643 , V645 , V694, V1086 | |
RuleChecker | 22.04 | array-index-range-constantreturn-reference-local | Partially checked |
TrustInSoft Analyzer | 1.38 | index_in_address | Exhaustively verified (see one compliant and one non-compliant example ). |
Taxonomy | Taxonomy item | Relationship |
---|---|---|
ISO/IEC TR 24772:2013 | Arithmetic Wrap-Around Error \[FIF\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TR 24772:2013 | Unchecked Array Indexing \[XYZ\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TS 17961 | Forming or using out-of-bounds pointers or array subscripts \[invptr\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
CWE 2.11 | CWE-119 , Improper Restriction of Operations within the Bounds of a Memory Buffer | 2017-05-18: CERT: Rule subset of CWE |
CWE 2.11 | CWE-123 , Write-what-where Condition | 2017-05-18: CERT: Partial overlap |
CWE 2.11 | CWE-125 , Out-of-bounds Read | 2017-05-18: CERT: Partial overlap |
MISRA C:2012 | Rule 18.1 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
\[ Finlay 2003 \] | |
\[ Microsoft 2003 \] | |
\[ Pethia 2003 \] | |
\[ Seacord 2013b \] | Chapter 1, "Running with Scissors" |
\[ Viega 2005 \] | Section 5.2.13, "Unchecked Array Indexing" |
\[ xorl 2009 \] | "CVE-2008-1517: Apple Mac OS X (XNU) Missing Array Index Validation" |
fgets() | fgetws() | mbstowcs() 1 | wcstombs() 1 |
mbrtoc16() 2 | mbrtoc32() 2 | mbsrtowcs() 1 | wcsrtombs() 1 |
mbtowc() 2 | mbrtowc() 2 | mblen() | mbrlen() |
memchr() | wmemchr() | memset() | wmemset() |
strftime() | wcsftime() | strxfrm()1 | wcsxfrm()1 |
strncat()2 | wcsncat()2 | snprintf() | vsnprintf() |
swprintf() | vswprintf() | setvbuf() | tmpnam_s() |
snprintf_s() | sprintf_s() | vsnprintf_s() | vsprintf_s() |
gets_s() | getenv_s() | wctomb_s() | mbstowcs_s()3 |
wcstombs_s()3 | memcpy_s()3 | memmove_s()3 | strncpy_s()3 |
strncat_s()3 | strtok_s()2 | strerror_s() | strnlen_s() |
asctime_s() | ctime_s() | snwprintf_s() | swprintf_s() |
vsnwprintf_s() | vswprintf_s() | wcsncpy_s()3 | wmemcpy_s()3 |
wmemmove_s()3 | wcsncat_s()3 | wcstok_s()2 | wcsnlen_s() |
wcrtomb_s() | mbsrtowcs_s()3 | wcsrtombs_s()3 | memset_s()4 |
| wmemcpy() | memmove() | wmemmove() |
strncpy() | wcsncpy() | memcmp() | wmemcmp() |
strncmp() | wcsncmp() | strcpy_s() | wcscpy_s() |
strcat_s() | wcscat_s() |
bsearch() | bsearch_s() | qsort() | qsort_s() |
fread() | fwrite() | |
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ARR38-C | High | Likely | Medium | P18 | L1 |
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 22.04 | array_out_of_bounds | Supported Astrée reports all out-of-bound accesses within library analysis stubs. The user may provide additional stubs for arbitrary (library) functions. |
CodeSonar | 7.2p0 | LANG.MEM.BO LANG.MEM.BU BADFUNC.BO.\* | Buffer overrun Buffer underrun A collection of warning classes that report uses of library functions prone to internal buffer overflows |
Compass/ROSE | |||
Coverity | 2017.07 | BUFFER_SIZE BAD_SIZEOF BAD_ALLOC_STRLEN BAD_ALLOC_ARITHMETIC | Implemented |
Fortify SCA | 5.0 | Can detect violations of this rule with CERT C Rule Pack | |
Helix QAC | 2022.4 | C2840 DF2840, DF2841, DF2842, DF2843, DF2845, DF2846, DF2847, DF2848, DF2935, DF2936, DF2937, DF2938, DF4880, DF4881, DF4882, DF4883 | |
Klocwork | 2022.4 | ABV.GENERALABV.GENERAL.MULTIDIMENSION | |
LDRA tool suite | 9.7.1 | 64 X, 66 X, 68 X, 69 X, 70 X, 71 X, 79 X | Partially Implmented |
Parasoft C/C++test | 2022.2 | CERT_C-ARR38-a CERT_C-ARR38-b CERT_C-ARR38-c CERT_C-ARR38-d | Avoid overflow when reading from a buffer Avoid overflow when writing to a buffer Avoid buffer overflow due to defining incorrect format limits Avoid overflow due to reading a not zero terminated string |
Parasoft Insure++ | Runtime analysis | ||
PC-lint Plus | 1.4 | 419, 420 | Partially supported |
Polyspace Bug Finder | R2023a | CERT C: Rule ARR38-C | Checks for: Mismatch between data length and sizeismatch between data length and size, invalid use of standard library memory routinenvalid use of standard library memory routine, possible misuse of sizeofossible misuse of sizeof, buffer overflow from incorrect string format specifieruffer overflow from incorrect string format specifier, invalid use of standard library string routinenvalid use of standard library string routine, destination buffer overflow in string manipulationestination buffer overflow in string manipulation, destination buffer underflow in string manipulationestination buffer underflow in string manipulation. Rule partially covered. |
PRQA QA-C | 9.7 | 2840, 2841, 2842, 2843, 2845, 2846, 2847, 2848, 2935, 2936, 2937, 2938 | Fully implemented |
PRQA QA-C++ | 4.4 | 2840, 2841, 2842, 2843, 2845, 2846, 2847, 2848, 2935, 2936, 2937, 2938 | Fully implemented |
Splint | 3.1.1 | ||
TrustInSoft Analyzer | 1.38 | out of bounds read | Partially verified. |
Taxonomy | Taxonomy item | Relationship |
---|---|---|
C Secure Coding Standard | API00-C. Functions should validate their parameters | Prior to 2018-01-12: CERT: Unspecified Relationship |
C Secure Coding Standard | ARR01-C. Do not apply the sizeof operator to a pointer when taking the size of an array | Prior to 2018-01-12: CERT: Unspecified Relationship |
C Secure Coding Standard | INT30-C. Ensure that unsigned integer operations do not wrap | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TS 17961:2013 | Forming invalid pointers by library functions \[libptr\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TR 24772:2013 | Buffer Boundary Violation (Buffer Overflow) \[HCB\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TR 24772:2013 | Unchecked Array Copying \[XYW\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
CWE 2.11 | CWE-119 , Improper Restriction of Operations within the Bounds of a Memory Buffer | 2017-05-18: CERT: Rule subset of CWE |
CWE 2.11 | CWE-121 , Stack-based Buffer Overflow | 2017-05-18: CERT: Partial overlap |
CWE 2.11 | CWE-123 , Write-what-where Condition | 2017-05-18: CERT: Partial overlap |
CWE 2.11 | CWE-125 , Out-of-bounds Read | 2017-05-18: CERT: Partial overlap |
CWE 2.11 | CWE-805 , Buffer Access with Incorrect Length Value | 2017-05-18: CERT: Partial overlap |
CWE 3.1 | CWE-129 , Improper Validation of Array Index | 2017-10-30:MITRE:Unspecified Relationship 2018-10-18:CERT: Partial Overlap |
\[ Cassidy 2014 \] | Existential Type Crisis : Diagnosis of the OpenSSL Heartbleed Bug |
\[ IETF: RFC 6520 \] | |
\[ ISO/IEC TS 17961:2013 \] | |
\[ VU\#720951 \] |