You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CWE23_Relative_Path_Traversal__char_environment_ofstream_01.cpp 2.9 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE23_Relative_Path_Traversal__char_environment_ofstream_01.cpp
  3. Label Definition File: CWE23_Relative_Path_Traversal.label.xml
  4. Template File: sources-sink-01.tmpl.cpp
  5. */
  6. /*
  7. * @description
  8. * CWE: 23 Relative Path Traversal
  9. * BadSource: environment Read input from an environment variable
  10. * GoodSource: Use a fixed file name
  11. * Sink: ofstream
  12. * BadSink : Open the file named in data using ofstream::open()
  13. * Flow Variant: 01 Baseline
  14. *
  15. * */
  16. #include "std_testcase.h"
  17. #ifdef _WIN32
  18. #define BASEPATH "c:\\temp\\"
  19. #else
  20. #include <wchar.h>
  21. #define BASEPATH "/tmp/"
  22. #endif
  23. #define ENV_VARIABLE "ADD"
  24. #ifdef _WIN32
  25. #define GETENV getenv
  26. #else
  27. #define GETENV getenv
  28. #endif
  29. #include <fstream>
  30. using namespace std;
  31. namespace CWE23_Relative_Path_Traversal__char_environment_ofstream_01
  32. {
  33. #ifndef OMITBAD
  34. void bad()
  35. {
  36. char * data;
  37. char dataBuffer[FILENAME_MAX] = BASEPATH;
  38. data = dataBuffer;
  39. {
  40. /* Append input from an environment variable to data */
  41. size_t dataLen = strlen(data);
  42. char * environment = GETENV(ENV_VARIABLE);
  43. /* If there is data in the environment variable */
  44. if (environment != NULL)
  45. {
  46. /* POTENTIAL FLAW: Read data from an environment variable */
  47. strncat(data+dataLen, environment, FILENAME_MAX-dataLen-1);
  48. }
  49. }
  50. {
  51. ofstream outputFile;
  52. /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
  53. outputFile.open((char *)data);
  54. outputFile.close();
  55. }
  56. }
  57. #endif /* OMITBAD */
  58. #ifndef OMITGOOD
  59. /* goodG2B uses the GoodSource with the BadSink */
  60. static void goodG2B()
  61. {
  62. char * data;
  63. char dataBuffer[FILENAME_MAX] = BASEPATH;
  64. data = dataBuffer;
  65. /* FIX: Use a fixed file name */
  66. strcat(data, "file.txt");
  67. {
  68. ofstream outputFile;
  69. /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
  70. outputFile.open((char *)data);
  71. outputFile.close();
  72. }
  73. }
  74. void good()
  75. {
  76. goodG2B();
  77. }
  78. #endif /* OMITGOOD */
  79. } /* close namespace */
  80. /* Below is the main(). It is only used when building this testcase on
  81. its own for testing or for building a binary to use in testing binary
  82. analysis tools. It is not used when compiling all the testcases as one
  83. application, which is how source code analysis tools are tested. */
  84. #ifdef INCLUDEMAIN
  85. using namespace CWE23_Relative_Path_Traversal__char_environment_ofstream_01; /* so that we can use good and bad easily */
  86. int main(int argc, char * argv[])
  87. {
  88. /* seed randomness */
  89. srand( (unsigned)time(NULL) );
  90. #ifndef OMITGOOD
  91. printLine("Calling good()...");
  92. good();
  93. printLine("Finished good()");
  94. #endif /* OMITGOOD */
  95. #ifndef OMITBAD
  96. printLine("Calling bad()...");
  97. bad();
  98. printLine("Finished bad()");
  99. #endif /* OMITBAD */
  100. return 0;
  101. }
  102. #endif

No Description

Contributors (1)