Buffer and Stack Overflow Protection
Prevent the use of known dangerous functions and APIs in effort to protect against memory-corruption vulnerabilities within firmware. (e.g. Use of unsafe C functions - strcat, strcpy, sprintf, scanf. Memory-corruption vulnerabilities, such as buffer overflows, can consist of overflowing the stack (Stack overflow or overflowing the heap (Heap overflow. For simplicity purposes, this document does not distinguish between these two types of vulnerabilities. In the event a buffer overflow has been detected and exploited by an attacker, the instruction pointer register is overwritten to execute the arbitrary malicious code provided by the attacker.
Finding Vulnerable C functions in source code. Example: Utilize the “find” command below within a “C” repository to find vulnerable C functions such as "strncpy" and "strlen" in source code.
An OR grep expression could be utilized with the following expression:
Below, example output of flawfinder is shown run against C source code.
Usage of deprecated functions, Noncompliant Code Example:This noncompliant code example assumes that gets() will not read more than BUFSIZ - 1 characters from stdin. This is an invalid assumption, and the resulting operation can cause a buffer overflow. Note further that BUFSIZ is a macro integer constant, defined in stdio.h, representing a suggested argument to setvbuf() and not the maximum size of such an input buffer.
The gets() function reads characters from the stdin into a destination array until end-of-file is encountered or a newline character is read. Any newline character is discarded, and a null character is written immediately after the last character read into the array.
Compliant Example: The fgets() function reads, at most, one less than a specified number of characters from a stream into an array. This solution is compliant because the number of bytes copied from stdin to buf cannot exceed the allocated memory:
strncat() is a variation on the original strcat() library function. Both are used to append one NULL terminated C string to another. The danger with the original strcat() was that the caller might provide more data than can fit into the receiving buffer, thereby overrunning it. The most common result of this is a segmentation violation. A worse result is the silent and undetected corruption of whatever followed the receiving buffer in memory.
strncat() adds an additional parameter allowing the user to specify the maximum number of bytes to copy. This is NOT the amount of data to copy. It is NOT the size of the source data. It is a limit to the amount of data to copy and is usually set to the size of the receiving buffer.
Compliant Example usage of “strncat”:
NonCompliant Example usage of “strncat”:
Considerations:
What kind of buffer and where it resides: physical, logical, virtual memory?
What data will remain when the buffer is freed or left around to LRU out?
What strategy will be followed to ensure old buffers do not leak data (example: clear buffer after use)?
Initialize buffers to known value on allocation.
Consider where variables are stored: stack, static or allocated structure.
Dispose and securely wipe sensitive information stored in buffers or temporary files during runtime after they are no longer needed (e.g. Wipe buffers from locations where personally identifiable information(PII) is stored before releasing the buffers).
Explicitly initialize variables.
Ensure secure compiler flags or switches are utilized upon each firmware build. (e.g. For GCC -fPIE, -fstack-protector-all, -Wl,-z,noexecstack, -Wl,-z,noexecheap etc.. See additional references section for more details.)
Use safe equivalent functions for known vulnerable functions such as (non-exhaustive list below):
gets() -> fgets()
strcpy() -> strncpy()
strcat() -> strncat()
sprintf() -> snprintf()
Those functions that do not have safe equivalents should be rewritten with safe checks implemented.
If FreeRTOS OS is utilized, consider setting "configCHECK_FOR_STACK_OVERFLOW" to "1" with a hook function during the development and testing phases but removing for production builds.
Additional References
OSS (Open Source Software) Static Analysis Tools
Use of flawfinder and PMD for C
Consider Codechecker and Infer for C, C++, and iOS using Clang Static Analysis
Last updated