Description
Integer coercion refers to a set of flaws pertaining to the type casting, extension, or truncation of primitive data types.
Several flaws fall under the category of integer coercion errors. For the most part, these errors in and of themselves result only in availability and data integrity issues. However, in some circumstances, they may result in other, more complicated security related flaws, such as buffer overflow conditions.
Potential Impact
Availability
DoS: Resource Consumption (CPU), DoS: Resource Consumption (Memory), DoS: Crash, Exit, or Restart
Integrity, Confidentiality, Availability
Execute Unauthorized Code or Commands
Integrity, Other
Other
Demonstrative Examples
DataPacket *packet;int numHeaders;PacketHeader *headers;
sock=AcceptSocketConnection();ReadPacket(packet, sock);numHeaders =packet->headers;
if (numHeaders > 100) {ExitError("too many headers!");}headers = malloc(numHeaders * sizeof(PacketHeader);ParsePacketHeaders(packet, headers);int GetUntrustedInt () {return(0x0000FFFF);}
void main (int argc, char **argv) {
char path[256];char *input;int i;short s;unsigned int sz;
i = GetUntrustedInt();s = i;/* s is -1 so it passes the safety check - CWE-697 */if (s > 256) {DiePainfully("go away!\n");}
/* s is sign-extended and saved in sz */sz = s;
/* output: i=65535, s=-1, sz=4294967295 - your mileage may vary */printf("i=%d, s=%d, sz=%u\n", i, s, sz);
input = GetUserInput("Enter pathname:");
/* strncpy interprets s as unsigned int, so it's treated as MAX_INT(CWE-195), enabling buffer overflow (CWE-119) */strncpy(path, input, s);path[255] = '\0'; /* don't want CWE-170 */printf("Path is: %s\n", path);
}Mitigations & Prevention
A language which throws exceptions on ambiguous data casts might be chosen.
Design objects and program flow such that multiple or complex casts are unnecessary
Ensure that any data type casting that you must used is entirely understood in order to reduce the plausibility of error in use.
Detection Methods
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-2022-2639 | Chain: integer coercion error (CWE-192) prevents a return value from indicating an error, leading to out-of-bounds write (CWE-787) |
Related Weaknesses
Taxonomy Mappings
- CLASP: — Integer coercion error
- CERT C Secure Coding: INT02-C — Understand integer conversion rules
- CERT C Secure Coding: INT05-C — Do not use input functions to convert character data if they cannot handle all possible inputs
- CERT C Secure Coding: INT31-C — Ensure that integer conversions do not result in lost or misinterpreted data
Frequently Asked Questions
What is CWE-192?
CWE-192 (Integer Coercion Error) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. Integer coercion refers to a set of flaws pertaining to the type casting, extension, or truncation of primitive data types.
How can CWE-192 be exploited?
Attackers can exploit CWE-192 (Integer Coercion Error) to dos: resource consumption (cpu), dos: resource consumption (memory), dos: crash, exit, or restart. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-192?
Key mitigations include: A language which throws exceptions on ambiguous data casts might be chosen.
What is the severity of CWE-192?
CWE-192 is classified as a Variant-level weakness (Low-Medium abstraction). It has been observed in 1 real-world CVEs.