diff --git a/c/cert/src/rules/INT30-C/UnsignedIntegerOperationsWrapAround.md b/c/cert/src/rules/INT30-C/UnsignedIntegerOperationsWrapAround.md new file mode 100644 index 0000000000..d57756b4b5 --- /dev/null +++ b/c/cert/src/rules/INT30-C/UnsignedIntegerOperationsWrapAround.md @@ -0,0 +1,249 @@ +# INT30-C: Ensure that unsigned integer operations do not wrap + +This query implements the CERT-C rule INT30-C: + +> Ensure that unsigned integer operations do not wrap + + +## Description + +The C Standard, 6.2.5, paragraph 9 \[[ISO/IEC 9899:2011](https://wiki.sei.cmu.edu/confluence/display/c/AA.+Bibliography#AA.Bibliography-ISO-IEC9899-2011)\], states + +> A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type. + + +This behavior is more informally called [unsigned integer wrapping](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-unsignedintegerwrapping). Unsigned integer operations can wrap if the resulting value cannot be represented by the underlying representation of the integer. The following table indicates which operators can result in wrapping: + +
Operator | Wrap | Operator | Wrap | Operator | Wrap | Operator | Wrap |
---|---|---|---|---|---|---|---|
+ | Yes | -= | Yes | << | Yes | < | No |
- | Yes | \*= | Yes | >> | No | > | No |
\* | Yes | /= | No | & | No | >= | No |
/ | No | %= | No | | | No | <= | No |
% | No | <<= | Yes | ^ | No | == | No |
++ | Yes | >>= | No | ~ | No | != | No |
-- | Yes | &= | No | ! | No | && | No |
= | No | |= | No | un + | No | || | No |
+= | Yes | ^= | No | un - | Yes | ?: | No |
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
INT30-C | High | Likely | High | P9 | L2 |
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 22.04 | integer-overflow | Fully checked |
Axivion Bauhaus Suite | 7.2.0 | CertC-INT30 | Implemented |
CodeSonar | 7.2p0 | ALLOC.SIZE.ADDOFLOW ALLOC.SIZE.IOFLOW ALLOC.SIZE.MULOFLOW ALLOC.SIZE.SUBUFLOW MISC.MEM.SIZE.ADDOFLOW MISC.MEM.SIZE.BAD MISC.MEM.SIZE.MULOFLOW MISC.MEM.SIZE.SUBUFLOW | Addition overflow of allocation size Integer overflow of allocation size Multiplication overflow of allocation size Subtraction underflow of allocation size Addition overflow of size Unreasonable size argument Multiplication overflow of size Subtraction underflow of size |
Compass/ROSE | Can detect violations of this rule by ensuring that operations are checked for overflow before being performed (Be mindful of exception INT30-EX2 because it excuses many operations from requiring validation , including all the operations that would validate a potentially dangerous operation. For instance, adding two unsigned int s together requires validation involving subtracting one of the numbers from UINT_MAX , which itself requires no validation because it cannot wrap.) | ||
Coverity | 2017.07 | INTEGER_OVERFLOW | Implemented |
Helix QAC | 2022.4 | C2910, C3383, C3384, C3385, C3386 C++2910 DF2911, DF2912, DF2913, | |
Klocwork | 2022.4 | NUM.OVERFLOW CWARN.NOEFFECT.OUTOFRANGE NUM.OVERFLOW.DF | |
LDRA tool suite | 9.7.1 | 493 S, 494 S | Partially implemented |
Parasoft C/C++test | 2022.2 | CERT_C-INT30-a CERT_C-INT30-b CERT_C-INT30-c | Avoid integer overflows Integer overflow or underflow in constant expression in '+', '-', '\*' operator Integer overflow or underflow in constant expression in '<<' operator |
Polyspace Bug Finder | R2022b | CERT C: Rule INT30-C | Checks for: Unsigned integer overflownsigned integer overflow, unsigned integer constant overflownsigned integer constant overflow. Rule partially covered. |
PRQA QA-C | 9.7 | 2910 \[C\], 2911 \[D\], 2912 \[A\], 2913 \[S\], 3383, 3384, 3385, 3386 | Partially implemented |
PRQA QA-C++ | 4.4 | 2910, 2911, 2912, 2913 | |
PVS-Studio | 7.23 | V658, V1012, V1028, V5005, V5011 | |
TrustInSoft Analyzer | 1.38 | unsigned overflow | Exhaustively verified. |
Taxonomy | Taxonomy item | Relationship |
---|---|---|
CERT C | INT02-C. Understand integer conversion rules | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT C | ARR30-C. Do not form or use out-of-bounds pointers or array subscripts | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT C | ARR36-C. Do not subtract or compare two pointers that do not refer to the same array | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT C | ARR37-C. Do not add or subtract an integer to a pointer to a non-array object | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT C | CON08-C. Do not assume that a group of calls to independently atomic methods is atomic | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TR 24772:2013 | Arithmetic Wrap-Around Error \[FIF\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
CWE 2.11 | CWE-190 , Integer Overflow or Wraparound | 2016-12-02: CERT: Rule subset of CWE |
CWE 2.11 | CWE-131 | 2017-05-16: CERT: Partial overlap |
CWE 2.11 | CWE-191 | 2017-05-18: CERT: Partial overlap |
CWE 2.11 | CWE-680 | 2017-05-18: CERT: Partial overlap |
\[ Bailey 2014 \] | Raising Lazarus - The 20 Year Old Bug that Went to Mars |
\[ Dowd 2006 \] | Chapter 6, "C Language Issues" ("Arithmetic Boundary Conditions," pp. 211–223) |
\[ ISO/IEC 9899:2011 \] | Subclause 6.2.5, "Types" |
\[ Seacord 2013b \] | Chapter 5, "Integer Security" |
\[ Viega 2005 \] | Section 5.2.7, "Integer Overflow" |
\[ VU\#551436 \] | |
\[ Warren 2002 \] | Chapter 2, "Basics" |
\[ Wojtczuk 2008 \] | |
\[ xorl 2009 \] | "CVE-2009-1385: Linux Kernel E1000 Integer Underflow" |
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
INT31-C | High | Probable | High | P6 | L2 |
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 22.04 | Supported via MISRA C:2012 Rules 10.1, 10.3, 10.4, 10.6 and 10.7 | |
CodeSonar | 7.2p0 | LANG.CAST.PC.AVLANG.CAST.PC.CONST2PTRLANG.CAST.PC.INT LANG.CAST.COERCELANG.CAST.VALUE ALLOC.SIZE.TRUNCMISC.MEM.SIZE.TRUNC LANG.MEM.TBA | Cast: arithmetic type/void pointer Conversion: integer constant to pointer Conversion: pointer/integer Coercion alters value Cast alters value Truncation of allocation size Truncation of size Tainted buffer access |
Compass/ROSE | Can detect violations of this rule. However, false warnings may be raised if limits.h is included | ||
Coverity \* | 2017.07 | NEGATIVE_RETURNS REVERSE_NEGATIVE MISRA_CAST | Can find array accesses, loop bounds, and other expressions that may contain dangerous implied integer conversions that would result in unexpected behavior Can find instances where a negativity check occurs after the negative value has been used for something else Can find instances where an integer expression is implicitly converted to a narrower integer type, where the signedness of an integer value is implicitly converted, or where the type of a complex expression is implicitly converted |
Cppcheck | 1.66 | memsetValueOutOfRange | The second argument to memset() cannot be represented as unsigned char |
Helix QAC | 2022.4 | C2850, C2855, C2890, C2895, C2900, C2905, C++2850, C++2855, C++2890, C++2895, C++2900, C++2905, C++3000, C++3010 DF2851, DF2852, DF2853, DF2856, DF2857, DF2858, DF2891, DF2892, DF2893, DF2896, DF2897, DF2898, DF2901, DF2902, DF2903, DF2906, DF2907, DF2908 | |
Klocwork | 2022.4 | PORTING.CAST.SIZE | |
LDRA tool suite | 9.7.1 | 93 S , 433 S , 434 S | Partially implemented |
Parasoft C/C++test | 2022.2 | CERT_C-INT31-a CERT_C-INT31-b CERT_C-INT31-c CERT_C-INT31-d CERT_C-INT31-e CERT_C-INT31-f CERT_C-INT31-g CERT_C-INT31-h CERT_C-INT31-i CERT_C-INT31-j CERT_C-INT31-k CERT_C-INT31-l CERT_C-INT31-m CERT_C-INT31-nCERT_C-INT31-o | An expression of essentially Boolean type should always be used where an operand is interpreted as a Boolean value An operand of essentially Boolean type should not be used where an operand is interpreted as a numeric value An operand of essentially character type should not be used where an operand is interpreted as a numeric value An operand of essentially enum type should not be used in an arithmetic operation Shift and bitwise operations should not be performed on operands of essentially signed or enum type An operand of essentially signed or enum type should not be used as the right hand operand to the bitwise shifting operator An operand of essentially unsigned type should not be used as the operand to the unary minus operator The value of an expression shall not be assigned to an object with a narrower essential type The value of an expression shall not be assigned to an object of a different essential type category Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category The second and third operands of the ternary operator shall have the same essential type category The value of a composite expression shall not be assigned to an object with wider essential type If a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential type If a composite expression is used as one (second or third) operand of a conditional operator then the other operand shall not have wider essential type Avoid integer overflows |
Polyspace Bug Finder | R2022b | CERT C: Rule INT31-C | Checks for: Integer conversion overflownteger conversion overflow, call to memset with unintended value all to memset with unintended value , sign change integer conversion overflowign change integer conversion overflow, tainted sign change conversionainted sign change conversion, unsigned integer conversion overflownsigned integer conversion overflow. Rule partially covered. |
PRQA QA-C | 9.7 | 2850, 2851, 2852, 2853, 2855, 2856, 2857, 2858, 2890, 2891, 2892, 2893, 2895, 2896, 2897, 2898 2900, 2901, 2902, 2903, 2905, 2906, 2907, 2908 | Partially implemented |
PRQA QA-C++ | 4.4 | 2850, 2851, 2852, 2853, 2855, 2856, 2857, 2858, 2890, 2891, 2892, 2893, 2895, 2896, 2897, 2898, 2900, 2901, 2902, 2903, 2905, 2906, 2907, 2908, 3000, 3010 | |
PVS-Studio | 7.23 | V562 , V569 , V642 , V676 , V716 , V721 , V724 , V732 , V739 , V784 , V793 , V1019 , V1029 , V1046 | |
RuleChecker | 22.04 | Supported via MISRA C:2012 Rules 10.1, 10.3, 10.4, 10.6 and 10.7 | |
TrustInSoft Analyzer | 1.38 | signed_downcast | Exhaustively verified. |
Taxonomy | Taxonomy item | Relationship |
---|---|---|
CERT C | DCL03-C. Use a static assertion to test the value of a constant expression | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT C | INT18-C. Evaluate integer expressions in a larger size before comparing or assigning to that size | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT C | FIO34-C. Distinguish between characters read from a file and EOF or WEOF | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT Oracle Secure Coding Standard for Java | NUM12-J. Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TR 24772:2013 | Numeric Conversion Errors \[FLC\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 10.1 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 10.3 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 10.4 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 10.6 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 10.7 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
CWE 2.11 | CWE-192 , Integer Coercion Error | 2017-07-17: CERT: Exact |
CWE 2.11 | CWE-197 , Numeric Truncation Error | 2017-06-14: CERT: Rule subset of CWE |
CWE 2.11 | CWE-681 , Incorrect Conversion between Numeric Types | 2017-07-17: CERT: Rule subset of CWE |
CWE 2.11 | CWE-704 | 2017-07-17: CERT: Rule subset of CWE |
\[ Dowd 2006 \] | Chapter 6, "C Language Issues" ("Type Conversions," pp. 223–270) |
\[ ISO/IEC 9899:2011 \] | 6.3.1.3, "Signed and Unsigned Integers" |
\[ Jones 2008 \] | Section 6.2.6.2, "Integer Types" |
\[ Seacord 2013b \] | Chapter 5, "Integer Security" |
\[ Viega 2005 \] | Section 5.2.9, "Truncation Error" Section 5.2.10, "Sign Extension Error" Section 5.2.11, "Signed to Unsigned Conversion Error" Section 5.2.12, "Unsigned to Signed Conversion Error" |
\[ Warren 2002 \] | Chapter 2, "Basics" |
\[ xorl 2009 \] | "CVE-2009-1376: Pidgin MSN SLP Integer Truncation" |
Operator | Overflow | Operator | Overflow | Operator | Overflow | Operator | Overflow |
---|---|---|---|---|---|---|---|
+ | Yes | -= | Yes | << | Yes | < | No |
- | Yes | \*= | Yes | >> | No | > | No |
\* | Yes | /= | Yes | & | No | >= | No |
/ | Yes | %= | Yes | | | No | <= | No |
% | Yes | <<= | Yes | ^ | No | == | No |
++ | Yes | >>= | No | ~ | No | != | No |
-- | Yes | &= | No | ! | No | && | No |
= | No | |= | No | unary + | No | || | No |
+= | Yes | ^= | No | unary - | Yes | ?: | No |
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
INT32-C | High | Likely | High | P9 | L2 |
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 22.04 | integer-overflow | Fully checked |
CodeSonar | 7.2p0 | ALLOC.SIZE.ADDOFLOW ALLOC.SIZE.IOFLOW ALLOC.SIZE.MULOFLOW ALLOC.SIZE.SUBUFLOW MISC.MEM.SIZE.ADDOFLOW MISC.MEM.SIZE.BAD MISC.MEM.SIZE.MULOFLOW MISC.MEM.SIZE.SUBUFLOW | Addition overflow of allocation size Integer overflow of allocation size Multiplication overflow of allocation size Subtraction underflow of allocation size Addition overflow of size Unreasonable size argument Multiplication overflow of size Subtraction underflow of size |
Coverity | 2017.07 | TAINTED_SCALAR BAD_SHIFT | Implemented |
Helix QAC | 2022.4 | C2800, C2860 C++2800, C++2860 DF2801, DF2802, DF2803, DF2861, DF2862, DF2863 | |
Klocwork | 2022.4 | NUM.OVERFLOW CWARN.NOEFFECT.OUTOFRANGE NUM.OVERFLOW.DF | |
LDRA tool suite | 9.7.1 | 493 S, 494 S | Partially implemented |
Parasoft C/C++test | 2022.2 | CERT_C-INT32-a CERT_C-INT32-b CERT_C-INT32-c | Avoid integer overflows Integer overflow or underflow in constant expression in '+', '-', '\*' operator Integer overflow or underflow in constant expression in '<<' operator |
Parasoft Insure++ | Runtime analysis | ||
Polyspace Bug Finder | R2022b | CERT C: Rule INT32-C | Checks for: Integer overflownteger overflow, tainted division operandainted division operand, tainted modulo operandainted modulo operand. Rule partially covered. |
PRQA QA-C | 9.7 | 2800, 2801, 2802, 2803, 2860, 2861, 2862, 2863 | Fully implemented |
PRQA QA-C++ | 4.4 | 2800, 2801, 2802, 2803, 2860, 2861, 2862, 2863 | |
PVS-Studio | 7.23 | V1026, V1070, V1081, V1083, V1085, V5010 | |
TrustInSoft Analyzer | 1.38 | signed_overflow | Exhaustively verified (see one compliant and one non-compliant example ). |
\[ Dowd 2006 \] | Chapter 6, "C Language Issues" ("Arithmetic Boundary Conditions," pp. 211–223) |
\[ ISO/IEC 9899:2011 \] | Subclause 6.5.5, "Multiplicative Operators" |
\[ Seacord 2013b \] | Chapter 5, "Integer Security" |
\[ Viega 2005 \] | Section 5.2.7, "Integer Overflow" |
\[ Warren 2002 \] | Chapter 2, "Basics" |
UB | Description |
45 | The value of the second operand of the / or % operator is zero (6.5.5). |
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
INT33-C | Low | Likely | Medium | P6 | L2 |
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 22.04 | int-division-by-zero int-modulo-by-zero | Fully checked |
Axivion Bauhaus Suite | 7.2.0 | CertC-INT33 | |
CodeSonar | 7.2p0 | LANG.ARITH.DIVZEROLANG.ARITH.FDIVZERO | Division by zero Float Division By Zero |
Compass/ROSE | Can detect some violations of this rule (In particular, it ensures that all operations involving division or modulo are preceded by a check ensuring that the second operand is nonzero.) | ||
Coverity | 2017.07 | DIVIDE_BY_ZERO | Fully implemented |
Cppcheck | 1.66 | zerodivzerodivcond | Context sensitive analysis of division by zero Not detected for division by struct member / array element / pointer data that is 0 Detected when there is unsafe division by variable before/after test if variable is zero |
Helix QAC | 2022.4 | C2830 C++2830 DF2831, DF2832, DF2833 | |
Klocwork | 2022.4 | DBZ.CONST DBZ.CONST.CALL DBZ.GENERAL DBZ.ITERATOR DBZ.ITERATOR.CALL | |
LDRA tool suite | 9.7.1 | 43 D, 127 D, 248 S, 629 S, 80 X | Partially implemented |
Parasoft C/C++test | 2022.2 | CERT_C-INT33-a | Avoid division by zero |
Parasoft Insure++ | Runtime analysis | ||
Polyspace Bug Finder | R2022b | CERT C: Rule INT33-C | Checks for: Integer division by zeronteger division by zero, tainted division operandainted division operand, tainted modulo operandainted modulo operand. Rule fully covered. |
PRQA QA-C | 9.7 | 2830 \[C\], 2831 \[D\], 2832 \[A\] 2833 \[S\] | Fully implemented |
PRQA QA-C++ | 4.4 | 2831, 2832, 2833 | |
SonarQube C/C++ Plugin | 3.11 | S3518 | |
PVS-Studio | 7.23 | V609 | |
TrustInSoft Analyzer | 1.38 | division_by_zero | Exhaustively verified (see one compliant and one non-compliant example ). |
Taxonomy | Taxonomy item | Relationship |
---|---|---|
CERT C | INT32-C. Ensure that operations on signed integers do not result in overflow | Prior to 2018-01-12: CERT: Unspecified Relationship |
CERT Oracle Secure Coding Standard for Java | NUM02-J. Ensure that division and remainder operations do not result in divide-by-zero errors | Prior to 2018-01-12: CERT: Unspecified Relationship |
ISO/IEC TS 17961 | Integer division errors \[diverr\] | Prior to 2018-01-12: CERT: Unspecified Relationship |
CWE 2.11 | CWE-369 , Divide By Zero | 2017-07-07: CERT: Exact |
\[ Seacord 2013b \] | Chapter 5, "Integer Security" |
\[ Warren 2002 \] | Chapter 2, "Basics" |
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
INT35-C | Low | Unlikely | Medium | P2 | L3 |
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 22.04 | Supported: Astrée reports overflows due to insufficient precision. | |
CodeSonar | 7.2p0 | LANG.ARITH.BIGSHIFT | Shift Amount Exceeds Bit Width |
Helix QAC | 2022.4 | C0582 C++3115 | |
Parasoft C/C++test | 2022.2 | CERT_C-INT35-a | Use correct integer precisions when checking the right hand operand of the shift operator |
Polyspace Bug Finder | R2022b | CERT C: Rule INT35-C | Checks for situations when integer precisions are exceeded (rule fully covered) |
PRQA QA-C | 9.7 | 0582 |
Taxonomy | Taxonomy item | Relationship |
---|---|---|
CWE 2.11 | CWE-681 , Incorrect Conversion between Numeric Types | 2017-10-30:MITRE: Unspecified Relationship 2018-10-18:CERT:Partial Overlap |
\[ Dowd 2006 \] | Chapter 6, "C Language Issues" |
\[ C99 Rationale 2003 \] | 6.5.7, "Bitwise Shift Operators" |