NNTS.MUSTBuffer overflow from non null-terminated stringIn C and C++, a C string, or a null-terminated string, is a character sequence terminated with a null character (\0). The length of a C string is found by searching for the null character. The NNTS family of checkers looks for code that uses string manipulation functions with character arrays that are not or may not be null terminated. The NNTS.MUST checker looks for code using string manipulation functions in which a character array is not null terminated. Vulnerability and riskThe null termination has historically created security problems. For example:
Mitigation and preventionTo avoid the problem:
Vulnerable code example1 #include <stdio.h> 2 3 int main() 4 { 5 char buf[8]; 6 char tgt[1024]; 7 const char * src = "abcdef"; 8 9 strncpy(buf, src, 3); 10 strcpy(tgt, buf); 11 return 0; 12 } Klocwork produces a buffer overflow report for array 'tgt' at line 10: buffer overflow of 'tgt', due to non-null terminated string 'buf'. A similar error is reported for array 'buf' also at line 10. In the example, the array bounds violation is reported twice because there is a reading of 'buf' and writing of 'tgt'. Both read and write occur beyond the buffers' boundaries (equal to or greater than 3), due to the fact that 'buf' was not null terminated (which in turn occurred because of the improper use of strcpy). This code will result in a buffer overflow, which can cause various significant security problems. Related checkersExternal guidance
ExtensionThis checker can be extended. See Tuning C/C++ analysis for more information. |