...
Code Block | ||||
---|---|---|---|---|
| ||||
#define __STDC_WANT_LIB_EXT1__ 1 #include <string.h> enum { STR_SIZE = 32 }; size_t func(const char *source) { char ac_str[STR_SIZE]; size_t ret = 0; if (source) { errno_t err = strncpy_s( ac_str, sizeof(ac_str), source, strlenstrnlen(source, sizeof(c_str)) ); if (err != 0) { /* Handle error */ } else { ret = strnlen(c_s(astr, sizeof(ac_str)); } } else { /* Handle null pointer */ } return ret; } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <string.h> enum { STR_SIZE = 32 }; size_t func(const char *source) { char c_str[STR_SIZE]; size_t ret = 0; if (source) { if (strlenstrnlen(source, sizeof(c_str)) < sizeof(c_str)) { strcpy(c_str, source); ret = strlen(c_str); } else { /* Handle string-too-large */ } } else { /* Handle null pointer */ } return ret; } |
Risk Assessment
Failure to properly null-terminate a character sequence that is passed to a library function that expects a string can result in buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process. Null-termination errors can also result in unintended information disclosure.
...
Rule
...
Severity
...
Likelihood
...
Remediation Cost
...
Priority
...
Level
...
STR32-C
...
High
...
Probable
...
Medium
...
P12
...
L1
Note that this code is not bulletproof. It gracefully handles the case where source
is NULL, when it is a valid string, and when source
is not null-terminated, but at least the first 32 bytes are valid. However, in cases where source
is not NULL, but points to invalid memory, or any of the first 32 bytes are invalid memory, the first call to strnlen()
will access this invalid memory, and the resulting behavior is undefined. Unfortunately, standard C provides no way to prevent or even detect this condition without some external knowledge about the memory source
points to.
Risk Assessment
Failure to properly null-terminate a character sequence that is passed to a library function that expects a string can result in buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process. Null-termination errors can also result in unintended information disclosure.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
STR32-C | High | Probable | Medium | P12 | L1 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| Supported Astrée supports the implementation of library stubs to fully verify this guideline. | |||||||
Axivion Bauhaus Suite |
| CertC-STR32 | Partially implemented: can detect some violation of the rule | ||||||
CodeSonar |
| MISC.MEM.NTERM.CSTRING | Unterminated C String | ||||||
Compass/ROSE | Can detect some violations of this rule | ||||||||
Coverity |
| STRING_NULL | Fully implemented | ||||||
Helix QAC |
| DF2835, DF2836, DF2839 | |||||||
Klocwork |
| NNTS.MIGHT | |||||||
LDRA tool suite |
| 404 S, 600 S | Partially implemented | ||||||
Parasoft C/C++test |
| CERT_C-STR32-a | Avoid overflow due to reading a not zero terminated string | ||||||
Polyspace Bug Finder |
| Checks for:
Rule partially covered. |
Automated Detection
Tool | Version | Checker | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Astrée | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | Astrée_V | Astrée_V | Supported Astrée supports the implementation of library stubs to fully verify this guideline. | Axivion Bauhaus Suite | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | Axivion Bauhaus Suite_V | Axivion Bauhaus Suite_V | CertC-STR32 | Partially implemented: can detect some violation of the rule | CodeSonar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | CodeSonar_V | CodeSonar_V | MISC.MEM.NTERM.CSTRING | Unterminated C String | Compass/ROSE | Can detect some violations of this rule | Coverity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | Coverity_V | Coverity_V | STRING_NULL | Fully implemented | Helix QAC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | Helix QAC_V | Helix QAC_V | C2835, C2836, C2839 C++2835, C++2836, C++2839 | Klocwork | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | Klocwork_V | Klocwork_V | NNTS.MIGHT | LDRA tool suite | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | LDRA_V | LDRA_V | 404 S, 600 S | Partially implemented | Parasoft C/C++test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | Parasoft_V | Parasoft_V | CERT_C-STR32-a | Avoid overflow due to reading a not zero terminated string | Polyspace Bug Finder | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | Polyspace Bug Finder_V | Polyspace Bug Finder_V | Checks for:
Rule partially covered. | PRQA QA-C | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include Page | PRQA QA-C_v | PRQA QA-C_v | 2835, 2836, 2839 | PRQA QA-C++ | Include Page | | cplusplus:PRQA QA-C++_V | cplusplus:PRQA QA-C++_V | 0145|||||||||||||||||||||||||||||||||||||||||||||||||||||
PVS-Studio |
| V692 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TrustInSoft Analyzer |
| match format and arguments | Partially verified. |
...