The SAMATE Project Department of Homeland Security
Downloads:  Selected

Back to the previous page...Back to the previous page

Test Case IDCandidate1592
Bad / GoodBadBad test case
AuthorFortify Software
Associated test case1593
ContributorJeff Meister
LanguageC
Type of test caseSource Code
Input stringN/A
Expected OutputN/A
InstructionsN/A
Submission date2006-06-22
DescriptioniconA strncpy generates a string that may be missing a NUL termination. When it is copied with strcpy a stack buffer can be overrun.
Filename
Flaw
  • (?) CWE-170: Improper Null Termination at line 46,48

There is 1 comment :: Submit a comment :: RSS

See the comments

Comment #1 :: Weakness Name Change
CWE has deprecated “Miscalculated Null Termination”; due to it was a duplicate of “Improper Null Termination”. Accordingly, the original weakness name of this test case is changed to new name.
Posted by Michael Koo :: 2009-01-02 11:21:46

>./nonul1-bad.c
  1. /*
  2. Description: A strncpy generates a string that may be missing a NUL termination.  When it is copied with strcpy a stack buffer can be overrun.
  3. Keywords: Port C Size0 Complex1 BufferOverflow Stack Strcpy NoNul
  4. ValidArg: "a"*30
  5. InvalidArg: "a"*100
  6. Copyright 2005 Fortify Software.
  7. Permission is hereby granted, without written agreement or royalty fee, to
  8. use, copy, modify, and distribute this software and its documentation for
  9. any purpose, provided that the above copyright notice and the following
  10. three paragraphs appear in all copies of this software.
  11. IN NO EVENT SHALL FORTIFY SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT,
  12. INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  13. USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF FORTIFY SOFTWARE HAS
  14. BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMANGE.
  15. FORTIFY SOFTWARE SPECIFICALLY DISCLAIMS ANY WARRANTIES INCLUDING, BUT NOT
  16. LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  18. THE SOFTWARE IS PROVIDED ON AN "AS-IS" BASIS AND FORTIFY SOFTWARE HAS NO
  19. OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  20. MODIFICATIONS.
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. /*
  27. * we pick a round buffer size in hopes that the compiler lays these
  28. * out next to each other without padding.  Other layouts may
  29. * inadvertantly NUL terminate the buffer with stack garbage.
  30. */
  31. #define MAXSIZE    32
  32.  
  33. void
  34. test(char *str)
  35. {
  36.         char buf3[MAXSIZE];
  37.         char buf2[MAXSIZE];
  38.         char buf1[MAXSIZE];
  39.  
  40.         /* strncpy does not NUL terminate if buffer isnt large enough */
  41.         strncpy(buf1, str, sizeof buf1);        /* BAD */
  42.         strncpy(buf2, "This is a Test string", sizeof buf2);
  43.         strcpy(buf3, buf1);                         /* BAD */
  44.         printf("result: %s\n", buf3);
  45. }
  46.  
  47. int
  48. main(int argc, char **argv)
  49. {
  50.         char *userstr;
  51.  
  52.         if(argc > 1) {
  53.                 userstr = argv[1];
  54.                 test(userstr);
  55.         }
  56.         return 0;
  57. }
  58.  
  59.