Description
A product calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value.
Potential Impact
Availability
DoS: Crash, Exit, or Restart, DoS: Resource Consumption (CPU), DoS: Resource Consumption (Memory), DoS: Instability
Integrity
Modify Memory
Confidentiality, Availability, Access Control
Execute Unauthorized Code or Commands, Bypass Protection Mechanism
Demonstrative Examples
int i;unsigned int numWidgets;Widget **WidgetList;
numWidgets = GetUntrustedSizeValue();if ((numWidgets == 0) || (numWidgets > MAX_NUM_WIDGETS)) {ExitError("Incorrect number of widgets requested!");}WidgetList = (Widget **)malloc(numWidgets * sizeof(Widget *));printf("WidgetList ptr=%p\n", WidgetList);for(i=0; i<numWidgets; i++) {WidgetList[i] = InitializeWidget();}WidgetList[numWidgets] = NULL;showWidgets(WidgetList);char firstname[20];char lastname[20];char fullname[40];fullname[0] = '\0';strncat(fullname, firstname, 20);strncat(fullname, lastname, 20);char firstname[20];char lastname[20];char fullname[40];fullname[0] = '\0';strncat(fullname, firstname, sizeof(fullname)-strlen(fullname)-1);strncat(fullname, lastname, sizeof(fullname)-strlen(fullname)-1);#define PATH_SIZE 60
char filename[PATH_SIZE];
for(i=0; i<=PATH_SIZE; i++) {
char c = fgetc(stdin);
if (c == EOF) {
filename[i] = '\0';
}
else {
filename[i] = c;
}
}for(i=0; i<PATH_SIZE; i++) {...int setFilename(char *filename) {char name[20];sprintf(name, "%16s.dat", filename);int success = saveFormattedFilenameToDB(name);return success;}Mitigations & Prevention
When copying character arrays or using character manipulation methods, the correct size parameter must be used to account for the null terminator that needs to be added at the end of the array. Some examples of functions susceptible to this weakness in C include strcpy(), strncpy(), strcat(), strncat(), printf(), sprintf(), scanf() and sscanf().
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-2003-0252 | Off-by-one error allows remote attackers to cause a denial of service and possibly execute arbitrary code via requests that do not contain newlines. |
| CVE-2001-1391 | Off-by-one vulnerability in driver allows users to modify kernel memory. |
| CVE-2002-0083 | Off-by-one error allows local users or remote malicious servers to gain privileges. |
| CVE-2002-0653 | Off-by-one buffer overflow in function usd by server allows local users to execute arbitrary code as the server user via .htaccess files with long entries. |
| CVE-2002-0844 | Off-by-one buffer overflow in version control system allows local users to execute arbitrary code. |
| CVE-1999-1568 | Off-by-one error in FTP server allows a remote attacker to cause a denial of service (crash) via a long PORT command. |
| CVE-2004-0346 | Off-by-one buffer overflow in FTP server allows local users to gain privileges via a 1024 byte RETR command. |
| CVE-2004-0005 | Multiple buffer overflows in chat client allow remote attackers to cause a denial of service and possibly execute arbitrary code. |
| CVE-2003-0356 | Multiple off-by-one vulnerabilities in product allow remote attackers to cause a denial of service and possibly execute arbitrary code. |
| CVE-2001-1496 | Off-by-one buffer overflow in server allows remote attackers to cause a denial of service and possibly execute arbitrary code. |
| CVE-2004-0342 | This is an interesting example that might not be an off-by-one. |
| CVE-2001-0609 | An off-by-one enables a terminating null to be overwritten, which causes 2 strings to be merged and enable a format string. |
| CVE-2002-1745 | Off-by-one error allows source code disclosure of files with 4 letter extensions that match an accepted 3-letter extension. |
| CVE-2002-1816 | Off-by-one buffer overflow. |
| CVE-2002-1721 | Off-by-one error causes an snprintf call to overwrite a critical internal variable with a null value. |
Showing 15 of 18 observed examples.
Related Weaknesses
Taxonomy Mappings
- PLOVER: — Off-by-one Error
- CERT C Secure Coding: STR31-C — Guarantee that storage for strings has sufficient space for character data and the null terminator
Frequently Asked Questions
What is CWE-193?
CWE-193 (Off-by-one Error) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. A product calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value.
How can CWE-193 be exploited?
Attackers can exploit CWE-193 (Off-by-one Error) to dos: crash, exit, or restart, dos: resource consumption (cpu), dos: resource consumption (memory), dos: instability. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-193?
Key mitigations include: When copying character arrays or using character manipulation methods, the correct size parameter must be used to account for the null terminator that needs to be added at the end of the array. Some e
What is the severity of CWE-193?
CWE-193 is classified as a Base-level weakness (Medium abstraction). It has been observed in 18 real-world CVEs.