310
only limits the impact to the operating system; the rest of the application may still be subject to
compromise. Be careful to avoid CWE-243 and other weaknesses related to jails.
Effectiveness = Limited
The effectiveness of this mitigation depends on the prevention capabilities of the specific
sandbox or jail being used and might only help to reduce the scope of an attack, such as
restricting the attacker to certain system calls or limiting the portion of the file system that can be
accessed.
Demonstrative Examples
Example 1:
The following code asks the user to enter their last name and then attempts to store the value
entered in the last_name array.
Example Language: C (Bad)
char last_name[20];
printf ("Enter your last name: ");
scanf ("%s", last_name);
The problem with the code above is that it does not restrict or limit the size of the name entered by
the user. If the user enters "Very_very_long_last_name" which is 24 characters long, then a buffer
overflow will occur since the array can only hold 20 characters total.
Example 2:
The following code attempts to create a local copy of a buffer to perform some manipulations to the
data.
Example Language: C (Bad)
void manipulate_string(char * string){
char buf[24];
strcpy(buf, string);
...
}
However, the programmer does not ensure that the size of the data pointed to by string will fit in the
local buffer and copies the data with the potentially dangerous strcpy() function. This may result in
a buffer overflow condition if an attacker can influence the contents of the string parameter.
Example 3:
The code below calls the gets() function to read in data from the command line.
Example Language: C (Bad)
char buf[24];
printf("Please enter your name and press <Enter>\n");
gets(buf);
...
}
However, gets() is inherently unsafe, because it copies all input from STDIN to the buffer without
checking size. This allows the user to provide a string that is larger than the buffer size, resulting in
an overflow condition.
Example 4:
In the following example, a server accepts connections from a client and processes the client
request. After accepting a client connection, the program will obtain client information using the
CWE Version 4.14
CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')
CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')