A security software engineer is performing a code audit on an enterprise data processing service written in C. The service reads user-supplied binary data streams containing a length header field () and a payload array (). During review, the engineer identifies the following code segment used to process incoming data chunks:
c
unsigned short length = get_user_header_length();
char *buffer = (char *)malloc(length + 1);
if (buffer == NULL) return -1;
memcpy(buffer, user_data, length);
buffer[length] = '\0';
Based on this code snippet, which of the following vulnerabilities and security risks are present in this implementation? (Select TWO)
- Integer overflow during size calculation that can lead to an undersized memory allocationAnswer
- BStored SQL injection flaw caused by raw user data being passed into database parameters
- Heap-based buffer overflow during copying due to mismatched memory buffer sizingAnswer
- DMissing authentication control allowing unauthorized access to administrative privileges
Answer
The implementation contains an integer overflow vulnerability in the allocation size arithmetic and a subsequent heap-based buffer overflow during the memory copy operation.
An integer overflow occurs when wraps to 0 for a maximum 16-bit unsigned short value (). This causes `malloc(0)` to allocate insufficient heap memory, while `memcpy` attempts to copy bytes into that buffer, resulting in a heap-based buffer overflow.
Step-by-Step Solution
Key Concept
Integer Overflow and Heap Buffer Overflow