CWE-806: Buffer Access Using Size of Source Buffer

Description

The product uses the size of a source buffer when reading from or writing to a destination buffer, which may cause it to access memory that is outside of the bounds of the buffer.

Submission Date :

Jan. 15, 2010, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

When the size of the destination is smaller than the size of the source, a buffer overflow could occur.

Example Vulnerable Codes

Example - 1

In the following example, the source character string is copied to the dest character string using the method strncpy.


...char source[21] = "the character string";char dest[12];strncpy(dest, source, sizeof(source)-1);...

However, in the call to strncpy the source character string is used within the sizeof call to determine the number of characters to copy. This will create a buffer overflow as the size of the source character string is greater than the dest character string. The dest character string should be used within the sizeof call to ensure that the correct number of characters are copied, as shown below.


...char source[21] = "the character string";char dest[12];strncpy(dest, source, sizeof(dest)-1);...

Example - 2

In this example, the method outputFilenameToLog outputs a filename to a log file. The method arguments include a pointer to a character string containing the file name and an integer for the number of characters in the string. The filename is copied to a buffer where the buffer size is set to a maximum size for inputs to the log file. The method then calls another method to save the contents of the buffer to the log file.


// // saves the file name to a log file// 

// // buffer with size set to maximum size for input to log file// 
// // copy filename to buffer// 
// // save to log file// 
int success;char buf[LOG_INPUT_SIZE];strncpy(buf, filename, length);success = saveToLogFile(buf);return success;#define LOG_INPUT_SIZE 40int outputFilenameToLog(char *filename, int length) {}

However, in this case the string copy method, strncpy, mistakenly uses the length method argument to determine the number of characters to copy rather than using the size of the local character string, buf. This can lead to a buffer overflow if the number of characters contained in character string pointed to by filename is larger then the number of characters allowed for the local character string. The string copy method should use the buf character string within a sizeof call to ensure that only characters up to the size of the buf array are copied to avoid a buffer overflow, as shown below.


// // copy filename to buffer// 
...strncpy(buf, filename, sizeof(buf)-1);...

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.

© cvefeed.io
Latest DB Update: Nov. 21, 2024 22:34