The SAMATE Project Department of Homeland Security
Downloads:  Selected

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

Test Case IDCandidate91
Bad / GoodBadBad test case
AuthorN/A
Associated test caseN/A
ContributorFortify
LanguageC
Type of test caseSource Code
Input stringN/A
Expected OutputN/A
InstructionsN/A
Submission date2006-01-04
DescriptioniconA chroot() is performed without a chdir(). PLOVER:CP.UPATH.ELEMENT
Filename

There is no comments :: Submit a comment :: RSS

>./chroot-bad1.c
  1. /*PLOVER:CP.UPATH.ELEMENT*/
  2.  
  3. /*
  4. Description: A chroot() is performed without a chdir().
  5. Keywords: Size0 Complex0 Api Chroot
  6. */
  7.  
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10.  
  11. #define DIR     "/tmp"
  12. #define FILE    "/etc/passwd"
  13.  
  14.  
  15.  
  16. void test(char *str)
  17. {
  18.         int fd;
  19.         if(chroot(DIR) < 0)
  20.                 return;
  21.         fd = open(FILE, O_RDONLY);            /* BAD */
  22.         if(fd >= 0) {
  23.                 if(close(fd) < 0)
  24.                         ;
  25.         }
  26. }
  27.  
  28. int main(int argc, char **argv)
  29. {
  30.         char *userstr;
  31.         if(argc > 1) {
  32.                 userstr = argv[1];
  33.                 test(userstr);
  34.         }
  35.         return 0;
  36. }
  37.