|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /* TEMPLATE GENERATED TESTCASE FILE
- Filename: CWE23_Relative_Path_Traversal__char_environment_ofstream_01.cpp
- Label Definition File: CWE23_Relative_Path_Traversal.label.xml
- Template File: sources-sink-01.tmpl.cpp
- */
- /*
- * @description
- * CWE: 23 Relative Path Traversal
- * BadSource: environment Read input from an environment variable
- * GoodSource: Use a fixed file name
- * Sink: ofstream
- * BadSink : Open the file named in data using ofstream::open()
- * Flow Variant: 01 Baseline
- *
- * */
-
- #include "std_testcase.h"
-
- #ifdef _WIN32
- #define BASEPATH "c:\\temp\\"
- #else
- #include <wchar.h>
- #define BASEPATH "/tmp/"
- #endif
-
- #define ENV_VARIABLE "ADD"
-
- #ifdef _WIN32
- #define GETENV getenv
- #else
- #define GETENV getenv
- #endif
-
- #include <fstream>
- using namespace std;
-
- namespace CWE23_Relative_Path_Traversal__char_environment_ofstream_01
- {
-
- #ifndef OMITBAD
-
- void bad()
- {
- char * data;
- char dataBuffer[FILENAME_MAX] = BASEPATH;
- data = dataBuffer;
- {
- /* Append input from an environment variable to data */
- size_t dataLen = strlen(data);
- char * environment = GETENV(ENV_VARIABLE);
- /* If there is data in the environment variable */
- if (environment != NULL)
- {
- /* POTENTIAL FLAW: Read data from an environment variable */
- strncat(data+dataLen, environment, FILENAME_MAX-dataLen-1);
- }
- }
- {
- ofstream outputFile;
- /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
- outputFile.open((char *)data);
- outputFile.close();
- }
- }
-
- #endif /* OMITBAD */
-
- #ifndef OMITGOOD
-
- /* goodG2B uses the GoodSource with the BadSink */
- static void goodG2B()
- {
- char * data;
- char dataBuffer[FILENAME_MAX] = BASEPATH;
- data = dataBuffer;
- /* FIX: Use a fixed file name */
- strcat(data, "file.txt");
- {
- ofstream outputFile;
- /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
- outputFile.open((char *)data);
- outputFile.close();
- }
- }
-
- void good()
- {
- goodG2B();
- }
-
- #endif /* OMITGOOD */
-
- } /* close namespace */
-
- /* Below is the main(). It is only used when building this testcase on
- its own for testing or for building a binary to use in testing binary
- analysis tools. It is not used when compiling all the testcases as one
- application, which is how source code analysis tools are tested. */
-
- #ifdef INCLUDEMAIN
-
- using namespace CWE23_Relative_Path_Traversal__char_environment_ofstream_01; /* so that we can use good and bad easily */
-
- int main(int argc, char * argv[])
- {
- /* seed randomness */
- srand( (unsigned)time(NULL) );
- #ifndef OMITGOOD
- printLine("Calling good()...");
- good();
- printLine("Finished good()");
- #endif /* OMITGOOD */
- #ifndef OMITBAD
- printLine("Calling bad()...");
- bad();
- printLine("Finished bad()");
- #endif /* OMITBAD */
- return 0;
- }
-
- #endif
|