index
int64
0
101k
filename
stringlengths
50
161
class
int64
0
195
good
stringlengths
103
8.98k
bad
stringlengths
141
8.19k
29,645
testcases/CWE126_Buffer_Overread/s02/CWE126_Buffer_Overread__malloc_char_loop_53c.c
138
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__malloc_char_loop_53c.c Label Definition File: CWE126_Buffer_Overread__malloc.label.xml Template File: sources-sink-53c.tmpl.c */ /* * @description * CWE: 126 Buffer Over-read * BadSource: Use a small buffer * GoodSource: Use a large buffer * Sink: loop * BadSink : Copy data to string using a loop * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #include <wchar.h> /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITGOOD /* good function declaration */ void CWE126_Buffer_Overread__malloc_char_loop_53d_goodG2BSink(char * data); /* goodG2B uses the GoodSource with the BadSink */ void CWE126_Buffer_Overread__malloc_char_loop_53c_goodG2BSink(char * data) { CWE126_Buffer_Overread__malloc_char_loop_53d_goodG2BSink(data); } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__malloc_char_loop_53c.c Label Definition File: CWE126_Buffer_Overread__malloc.label.xml Template File: sources-sink-53c.tmpl.c */ /* * @description * CWE: 126 Buffer Over-read * BadSource: Use a small buffer * GoodSource: Use a large buffer * Sink: loop * BadSink : Copy data to string using a loop * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #include <wchar.h> /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITBAD /* bad function declaration */ void CWE126_Buffer_Overread__malloc_char_loop_53d_badSink(char * data); void CWE126_Buffer_Overread__malloc_char_loop_53c_badSink(char * data) { CWE126_Buffer_Overread__malloc_char_loop_53d_badSink(data); } #endif /* OMITBAD */
17,546
testcases/CWE121_Stack_Based_Buffer_Overflow/s04/CWE121_Stack_Based_Buffer_Overflow__CWE805_int64_t_declare_memmove_02.c
153
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE805_int64_t_declare_memmove_02.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.label.xml Template File: sources-sink-02.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Set data pointer to the bad buffer * GoodSource: Set data pointer to the good buffer * Sink: memmove * BadSink : Copy int64_t array to data using memmove * Flow Variant: 02 Control flow: if(1) and if(0) * * */ #include "std_testcase.h" return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE805_int64_t_declare_memmove_02.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.label.xml Template File: sources-sink-02.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Set data pointer to the bad buffer * GoodSource: Set data pointer to the good buffer * Sink: memmove * BadSink : Copy int64_t array to data using memmove * Flow Variant: 02 Control flow: if(1) and if(0) * * */ #include "std_testcase.h" #ifndef OMITBAD void CWE121_Stack_Based_Buffer_Overflow__CWE805_int64_t_declare_memmove_02_bad() { int64_t * data; int64_t dataBadBuffer[50]; int64_t dataGoodBuffer[100]; if(1) { /* FLAW: Set a pointer to a "small" buffer. This buffer will be used in the sinks as a destination * buffer in various memory copying functions using a "large" source buffer. */ data = dataBadBuffer; } { int64_t source[100] = {0}; /* fill with 0's */ /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */ memmove(data, source, 100*sizeof(int64_t)); printLongLongLine(data[0]); } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE121_Stack_Based_Buffer_Overflow__CWE805_int64_t_declare_memmove_02_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
1,722
testcases/CWE78_OS_Command_Injection/s05/CWE78_OS_Command_Injection__wchar_t_connect_socket_w32_spawnlp_72a.cpp
168
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_connect_socket_w32_spawnlp_72a.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-72a.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Fixed string * Sinks: w32_spawnlp * BadSink : execute command with wspawnlp * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files * * */ #include "std_testcase.h" #include <vector> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else /* NOT _WIN32 */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define IP_ADDRESS "127.0.0.1" using namespace std; namespace CWE78_OS_Command_Injection__wchar_t_connect_socket_w32_spawnlp_72 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_connect_socket_w32_spawnlp_72a.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-72a.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Fixed string * Sinks: w32_spawnlp * BadSink : execute command with wspawnlp * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files * * */ #include "std_testcase.h" #include <vector> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else /* NOT _WIN32 */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define IP_ADDRESS "127.0.0.1" using namespace std; namespace CWE78_OS_Command_Injection__wchar_t_connect_socket_w32_spawnlp_72 { #ifndef OMITBAD /* bad function declaration */ void badSink(vector<wchar_t *> dataVector); void bad() { wchar_t * data; vector<wchar_t *> dataVector; wchar_t dataBuffer[100] = COMMAND_ARG2; data = dataBuffer; { #ifdef _WIN32 WSADATA wsaData; int wsaDataInit = 0; #endif int recvResult; struct sockaddr_in service; wchar_t *replace; SOCKET connectSocket = INVALID_SOCKET; size_t dataLen = wcslen(data); do { #ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) { break; } wsaDataInit = 1; #endif /* POTENTIAL FLAW: Read data using a connect socket */ connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (connectSocket == INVALID_SOCKET) { break; } memset(&service, 0, sizeof(service)); service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(IP_ADDRESS); service.sin_port = htons(TCP_PORT); if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR) { break; } /* Abort on error or the connection was closed, make sure to recv one * less char than is in the recv_buf in order to append a terminator */ /* Abort on error or the connection was closed */ recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0); if (recvResult == SOCKET_ERROR || recvResult == 0) { break; } /* Append null terminator */ data[dataLen + recvResult / sizeof(wchar_t)] = L'\0'; /* Eliminate CRLF */ replace = wcschr(data, L'\r'); if (replace) { *replace = L'\0'; } replace = wcschr(data, L'\n'); if (replace) { *replace = L'\0'; } } while (0); if (connectSocket != INVALID_SOCKET) { CLOSE_SOCKET(connectSocket); } #ifdef _WIN32 if (wsaDataInit) { WSACleanup(); } #endif } /* Put data in a vector */ dataVector.insert(dataVector.end(), 1, data); dataVector.insert(dataVector.end(), 1, data); dataVector.insert(dataVector.end(), 1, data); badSink(dataVector); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
75,231
testcases/CWE122_Heap_Based_Buffer_Overflow/s06/CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memcpy_63b.c
125
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memcpy_63b.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-63b.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sinks: memcpy * BadSink : Copy string to data using memcpy() * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memcpy_63b_goodG2BSink(char * * dataPtr) { char * data = *dataPtr; { char source[10+1] = SRC_STRING; /* Copy length + 1 to include NUL terminator from source */ /* POTENTIAL FLAW: data may not have enough space to hold source */ memcpy(data, source, (strlen(source) + 1) * sizeof(char)); printLine(data); free(data); } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memcpy_63b.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-63b.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sinks: memcpy * BadSink : Copy string to data using memcpy() * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" #ifndef OMITBAD void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memcpy_63b_badSink(char * * dataPtr) { char * data = *dataPtr; { char source[10+1] = SRC_STRING; /* Copy length + 1 to include NUL terminator from source */ /* POTENTIAL FLAW: data may not have enough space to hold source */ memcpy(data, source, (strlen(source) + 1) * sizeof(char)); printLine(data); free(data); } } #endif /* OMITBAD */
67,923
testcases/CWE122_Heap_Based_Buffer_Overflow/s01/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_fscanf_81_bad.cpp
141
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_fscanf_81_bad.cpp Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129.label.xml Template File: sources-sinks-81_bad.tmpl.cpp */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Larger than zero but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking the upper bound * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_fscanf_81_bad.cpp Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129.label.xml Template File: sources-sinks-81_bad.tmpl.cpp */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Larger than zero but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking the upper bound * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */ #ifndef OMITBAD #include "std_testcase.h" #include "CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_fscanf_81.h" namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_fscanf_81 { void CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_fscanf_81_bad::action(int data) const { { int i; int * buffer = new int[10]; /* initialize buffer */ for (i = 0; i < 10; i++) { buffer[i] = 0; } /* POTENTIAL FLAW: Attempt to write to an index of the array that is above the upper bound * This code does check to see if the array index is negative */ if (data >= 0) { buffer[data] = 1; /* Print the array values */ for(i = 0; i < 10; i++) { printIntLine(buffer[i]); } } else { printLine("ERROR: Array index is negative."); } delete[] buffer; } } } #endif /* OMITBAD */
46,523
testcases/CWE124_Buffer_Underwrite/s03/CWE124_Buffer_Underwrite__new_char_cpy_82_bad.cpp
40
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE124_Buffer_Underwrite__new_char_cpy_82_bad.cpp Label Definition File: CWE124_Buffer_Underwrite__new.label.xml Template File: sources-sink-82_bad.tmpl.cpp */ /* * @description * CWE: 124 Buffer Underwrite * BadSource: Set data pointer to before the allocated memory buffer * GoodSource: Set data pointer to the allocated memory buffer * Sinks: cpy * BadSink : Copy string to data using strcpy * Flow Variant: 82 Data flow: data passed in a parameter to a virtual method called via a pointer * * */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE124_Buffer_Underwrite__new_char_cpy_82_bad.cpp Label Definition File: CWE124_Buffer_Underwrite__new.label.xml Template File: sources-sink-82_bad.tmpl.cpp */ /* * @description * CWE: 124 Buffer Underwrite * BadSource: Set data pointer to before the allocated memory buffer * GoodSource: Set data pointer to the allocated memory buffer * Sinks: cpy * BadSink : Copy string to data using strcpy * Flow Variant: 82 Data flow: data passed in a parameter to a virtual method called via a pointer * * */ #ifndef OMITBAD #include "std_testcase.h" #include "CWE124_Buffer_Underwrite__new_char_cpy_82.h" namespace CWE124_Buffer_Underwrite__new_char_cpy_82 { void CWE124_Buffer_Underwrite__new_char_cpy_82_bad::action(char * data) { { char source[100]; memset(source, 'C', 100-1); /* fill with 'C's */ source[100-1] = '\0'; /* null terminate */ /* POTENTIAL FLAW: Possibly copying data to memory before the destination buffer */ strcpy(data, source); printLine(data); /* INCIDENTAL CWE-401: Memory Leak - data may not point to location * returned by new [] so can't safely call delete [] on it */ } } } #endif /* OMITBAD */
29,020
testcases/CWE126_Buffer_Overread/s02/CWE126_Buffer_Overread__new_wchar_t_memcpy_01.cpp
138
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__new_wchar_t_memcpy_01.cpp Label Definition File: CWE126_Buffer_Overread__new.label.xml Template File: sources-sink-01.tmpl.cpp */ /* * @description * CWE: 126 Buffer Over-read * BadSource: Use a small buffer * GoodSource: Use a large buffer * Sink: memcpy * BadSink : Copy data to string using memcpy * Flow Variant: 01 Baseline * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE126_Buffer_Overread__new_wchar_t_memcpy_01 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__new_wchar_t_memcpy_01.cpp Label Definition File: CWE126_Buffer_Overread__new.label.xml Template File: sources-sink-01.tmpl.cpp */ /* * @description * CWE: 126 Buffer Over-read * BadSource: Use a small buffer * GoodSource: Use a large buffer * Sink: memcpy * BadSink : Copy data to string using memcpy * Flow Variant: 01 Baseline * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE126_Buffer_Overread__new_wchar_t_memcpy_01 { #ifndef OMITBAD void bad() { wchar_t * data; data = NULL; /* FLAW: Use a small buffer */ data = new wchar_t[50]; wmemset(data, L'A', 50-1); /* fill with 'A's */ data[50-1] = L'\0'; /* null terminate */ { wchar_t dest[100]; wmemset(dest, L'C', 100-1); dest[100-1] = L'\0'; /* null terminate */ /* POTENTIAL FLAW: using memcpy with the length of the dest where data * could be smaller than dest causing buffer overread */ memcpy(dest, data, wcslen(dest)*sizeof(wchar_t)); dest[100-1] = L'\0'; printWLine(dest); delete [] data; } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
59,601
testcases/CWE789_Uncontrolled_Mem_Alloc/s02/CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_rand_72a.cpp
183
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_rand_72a.cpp Label Definition File: CWE789_Uncontrolled_Mem_Alloc__new.label.xml Template File: sources-sinks-72a.tmpl.cpp */ /* * @description * CWE: 789 Uncontrolled Memory Allocation * BadSource: rand Set data to result of rand(), which may be zero * GoodSource: Small number greater than zero * Sinks: * GoodSink: Allocate memory with new [] and check the size of the memory to be allocated * BadSink : Allocate memory with new [], but incorrectly check the size of the memory to be allocated * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files * * */ #include "std_testcase.h" #include <vector> #ifndef _WIN32 #include <wchar.h> #endif using namespace std; namespace CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_rand_72 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_rand_72a.cpp Label Definition File: CWE789_Uncontrolled_Mem_Alloc__new.label.xml Template File: sources-sinks-72a.tmpl.cpp */ /* * @description * CWE: 789 Uncontrolled Memory Allocation * BadSource: rand Set data to result of rand(), which may be zero * GoodSource: Small number greater than zero * Sinks: * GoodSink: Allocate memory with new [] and check the size of the memory to be allocated * BadSink : Allocate memory with new [], but incorrectly check the size of the memory to be allocated * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files * * */ #include "std_testcase.h" #include <vector> #ifndef _WIN32 #include <wchar.h> #endif using namespace std; namespace CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_rand_72 { #ifndef OMITBAD /* bad function declaration */ void badSink(vector<size_t> dataVector); void bad() { size_t data; vector<size_t> dataVector; /* Initialize data */ data = 0; /* POTENTIAL FLAW: Set data to a random value */ data = rand(); /* Put data in a vector */ dataVector.insert(dataVector.end(), 1, data); dataVector.insert(dataVector.end(), 1, data); dataVector.insert(dataVector.end(), 1, data); badSink(dataVector); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
61,112
testcases/CWE127_Buffer_Underread/s01/CWE127_Buffer_Underread__CWE839_fgets_45.c
19
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE127_Buffer_Underread__CWE839_fgets_45.c Label Definition File: CWE127_Buffer_Underread__CWE839.label.xml Template File: sources-sinks-45.tmpl.c */ /* * @description * CWE: 127 Buffer Underread * BadSource: fgets Read data from the console using fgets() * GoodSource: Non-negative but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking to see if the value is negative * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file * * */ #include "std_testcase.h" #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) static int CWE127_Buffer_Underread__CWE839_fgets_45_badData; static int CWE127_Buffer_Underread__CWE839_fgets_45_goodG2BData; static int CWE127_Buffer_Underread__CWE839_fgets_45_goodB2GData; return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE127_Buffer_Underread__CWE839_fgets_45.c Label Definition File: CWE127_Buffer_Underread__CWE839.label.xml Template File: sources-sinks-45.tmpl.c */ /* * @description * CWE: 127 Buffer Underread * BadSource: fgets Read data from the console using fgets() * GoodSource: Non-negative but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking to see if the value is negative * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file * * */ #include "std_testcase.h" #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) static int CWE127_Buffer_Underread__CWE839_fgets_45_badData; static int CWE127_Buffer_Underread__CWE839_fgets_45_goodG2BData; static int CWE127_Buffer_Underread__CWE839_fgets_45_goodB2GData; #ifndef OMITBAD static void badSink() { int data = CWE127_Buffer_Underread__CWE839_fgets_45_badData; { int buffer[10] = { 0 }; /* POTENTIAL FLAW: Attempt to access a negative index of the array * This check does not check to see if the array index is negative */ if (data < 10) { printIntLine(buffer[data]); } else { printLine("ERROR: Array index is too big."); } } } void CWE127_Buffer_Underread__CWE839_fgets_45_bad() { int data; /* Initialize data */ data = -1; { char inputBuffer[CHAR_ARRAY_SIZE] = ""; /* POTENTIAL FLAW: Read data from the console using fgets() */ if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL) { /* Convert to int */ data = atoi(inputBuffer); } else { printLine("fgets() failed."); } } CWE127_Buffer_Underread__CWE839_fgets_45_badData = data; badSink(); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE127_Buffer_Underread__CWE839_fgets_45_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
35,975
testcases/CWE114_Process_Control/CWE114_Process_Control__w32_char_relativePath_67b.c
92
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE114_Process_Control__w32_char_relativePath_67b.c Label Definition File: CWE114_Process_Control__w32.label.xml Template File: sources-sink-67b.tmpl.c */ /* * @description * CWE: 114 Process Control * BadSource: relativePath Hard code the relative pathname to the library * GoodSource: Hard code the full pathname to the library * Sinks: * BadSink : Load a dynamic link library * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files * * */ #include "std_testcase.h" #include <wchar.h> #include <windows.h> typedef struct _CWE114_Process_Control__w32_char_relativePath_67_structType { char * structFirst; } CWE114_Process_Control__w32_char_relativePath_67_structType; #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE114_Process_Control__w32_char_relativePath_67b_goodG2BSink(CWE114_Process_Control__w32_char_relativePath_67_structType myStruct) { char * data = myStruct.structFirst; { HMODULE hModule; /* POTENTIAL FLAW: If the path to the library is not specified, an attacker may be able to * replace his own file with the intended library */ hModule = LoadLibraryA(data); if (hModule != NULL) { FreeLibrary(hModule); printLine("Library loaded and freed successfully"); } else { printLine("Unable to load library"); } } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE114_Process_Control__w32_char_relativePath_67b.c Label Definition File: CWE114_Process_Control__w32.label.xml Template File: sources-sink-67b.tmpl.c */ /* * @description * CWE: 114 Process Control * BadSource: relativePath Hard code the relative pathname to the library * GoodSource: Hard code the full pathname to the library * Sinks: * BadSink : Load a dynamic link library * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files * * */ #include "std_testcase.h" #include <wchar.h> #include <windows.h> typedef struct _CWE114_Process_Control__w32_char_relativePath_67_structType { char * structFirst; } CWE114_Process_Control__w32_char_relativePath_67_structType; #ifndef OMITBAD void CWE114_Process_Control__w32_char_relativePath_67b_badSink(CWE114_Process_Control__w32_char_relativePath_67_structType myStruct) { char * data = myStruct.structFirst; { HMODULE hModule; /* POTENTIAL FLAW: If the path to the library is not specified, an attacker may be able to * replace his own file with the intended library */ hModule = LoadLibraryA(data); if (hModule != NULL) { FreeLibrary(hModule); printLine("Library loaded and freed successfully"); } else { printLine("Unable to load library"); } } } #endif /* OMITBAD */
33,115
testcases/CWE401_Memory_Leak/s02/CWE401_Memory_Leak__new_int64_t_63a.cpp
161
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE401_Memory_Leak__new_int64_t_63a.cpp Label Definition File: CWE401_Memory_Leak__new.label.xml Template File: sources-sinks-63a.tmpl.cpp */ /* * @description * CWE: 401 Memory Leak * BadSource: Allocate data using new * GoodSource: Allocate data on the stack * Sinks: * GoodSink: call delete on data * BadSink : no deallocation of data * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif namespace CWE401_Memory_Leak__new_int64_t_63 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE401_Memory_Leak__new_int64_t_63a.cpp Label Definition File: CWE401_Memory_Leak__new.label.xml Template File: sources-sinks-63a.tmpl.cpp */ /* * @description * CWE: 401 Memory Leak * BadSource: Allocate data using new * GoodSource: Allocate data on the stack * Sinks: * GoodSink: call delete on data * BadSink : no deallocation of data * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif namespace CWE401_Memory_Leak__new_int64_t_63 { #ifndef OMITBAD /* bad function declaration */ void badSink(int64_t * * dataPtr); void bad() { int64_t * data; data = NULL; /* POTENTIAL FLAW: Allocate memory on the heap */ data = new int64_t; /* Initialize and make use of data */ *data = 5LL; printLongLongLine(*data); badSink(&data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
56,875
testcases/CWE197_Numeric_Truncation_Error/s01/CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54b.c
149
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54b.c Label Definition File: CWE197_Numeric_Truncation_Error__int.label.xml Template File: sources-sink-54b.tmpl.c */ /* * @description * CWE: 197 Numeric Truncation Error * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Less than CHAR_MAX * Sink: to_short * BadSink : Convert data to a short * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else /* NOT _WIN32 */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define IP_ADDRESS "127.0.0.1" #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITGOOD /* good function declaration */ void CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54c_goodG2BSink(int data); /* goodG2B uses the GoodSource with the BadSink */ void CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54b_goodG2BSink(int data) { CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54c_goodG2BSink(data); } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54b.c Label Definition File: CWE197_Numeric_Truncation_Error__int.label.xml Template File: sources-sink-54b.tmpl.c */ /* * @description * CWE: 197 Numeric Truncation Error * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Less than CHAR_MAX * Sink: to_short * BadSink : Convert data to a short * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else /* NOT _WIN32 */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define IP_ADDRESS "127.0.0.1" #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITBAD /* bad function declaration */ void CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54c_badSink(int data); void CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54b_badSink(int data) { CWE197_Numeric_Truncation_Error__int_connect_socket_to_short_54c_badSink(data); } #endif /* OMITBAD */
25,453
testcases/CWE606_Unchecked_Loop_Condition/CWE606_Unchecked_Loop_Condition__wchar_t_console_82_bad.cpp
169
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE606_Unchecked_Loop_Condition__wchar_t_console_82_bad.cpp Label Definition File: CWE606_Unchecked_Loop_Condition.label.xml Template File: sources-sinks-82_bad.tmpl.cpp */ /* * @description * CWE: 606 Unchecked Input For Loop Condition * BadSource: console Read input from the console * GoodSource: Input a number less than MAX_LOOP * Sinks: * GoodSink: Use data as the for loop variant after checking to see if it is less than MAX_LOOP * BadSink : Use data as the for loop variant without checking its size * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer * * */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE606_Unchecked_Loop_Condition__wchar_t_console_82_bad.cpp Label Definition File: CWE606_Unchecked_Loop_Condition.label.xml Template File: sources-sinks-82_bad.tmpl.cpp */ /* * @description * CWE: 606 Unchecked Input For Loop Condition * BadSource: console Read input from the console * GoodSource: Input a number less than MAX_LOOP * Sinks: * GoodSink: Use data as the for loop variant after checking to see if it is less than MAX_LOOP * BadSink : Use data as the for loop variant without checking its size * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer * * */ #ifndef OMITBAD #include "std_testcase.h" #include "CWE606_Unchecked_Loop_Condition__wchar_t_console_82.h" namespace CWE606_Unchecked_Loop_Condition__wchar_t_console_82 { void CWE606_Unchecked_Loop_Condition__wchar_t_console_82_bad::action(wchar_t * data) { { int i, n, intVariable; if (swscanf(data, L"%d", &n) == 1) { /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */ intVariable = 0; for (i = 0; i < n; i++) { /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */ intVariable++; /* avoid a dead/empty code block issue */ } printIntLine(intVariable); } } } } #endif /* OMITBAD */
39,240
testcases/CWE134_Uncontrolled_Format_String/s04/CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84_goodB2G.cpp
119
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84_goodB2G.cpp Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml Template File: sources-vasinks-84_goodB2G.tmpl.cpp */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: console Read input from the console * GoodSource: Copy a fixed string into data * Sinks: vprintf * GoodSink: vwprintf with a format string * BadSink : vwprintf without a format string * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use * * */ #ifndef OMITGOOD #include <stdarg.h> #include "std_testcase.h" #include "CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84.h" namespace CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84 { CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84_goodB2G::CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84_goodB2G(wchar_t * dataCopy) { data = dataCopy; { /* Read input from the console */ size_t dataLen = wcslen(data); /* if there is room in data, read into it from the console */ if (100-dataLen > 1) { /* POTENTIAL FLAW: Read data from the console */ if (fgetws(data+dataLen, (int)(100-dataLen), stdin) != NULL) { /* The next few lines remove the carriage return from the string that is * inserted by fgetws() */ dataLen = wcslen(data); if (dataLen > 0 && data[dataLen-1] == L'\n') { data[dataLen-1] = L'\0'; } } else { printLine("fgetws() failed"); /* Restore NUL terminator if fgetws fails */ data[dataLen] = L'\0'; } } } } static void goodB2GVaSink(wchar_t * data, ...) { { va_list args; va_start(args, data); /* FIX: Specify the format disallowing a format string vulnerability */ vwprintf(L"%s", args); va_end(args); } } CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84_goodB2G::~CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84_goodB2G() { goodB2GVaSink(data, data); } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_84_goodB2G.cpp Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml Template File: sources-vasinks-84_goodB2G.tmpl.cpp */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: console Read input from the console * GoodSource: Copy a fixed string into data * Sinks: vprintf * GoodSink: vwprintf with a format string * BadSink : vwprintf without a format string * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use * * */
77,622
testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_83_bad.cpp
180
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE415_Double_Free__malloc_free_struct_83_bad.cpp Label Definition File: CWE415_Double_Free__malloc_free.label.xml Template File: sources-sinks-83_bad.tmpl.cpp */ /* * @description * CWE: 415 Double Free * BadSource: Allocate data using malloc() and Deallocate data using free() * GoodSource: Allocate data using malloc() * Sinks: * GoodSink: do nothing * BadSink : Deallocate data using free() * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack * * */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE415_Double_Free__malloc_free_struct_83_bad.cpp Label Definition File: CWE415_Double_Free__malloc_free.label.xml Template File: sources-sinks-83_bad.tmpl.cpp */ /* * @description * CWE: 415 Double Free * BadSource: Allocate data using malloc() and Deallocate data using free() * GoodSource: Allocate data using malloc() * Sinks: * GoodSink: do nothing * BadSink : Deallocate data using free() * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack * * */ #ifndef OMITBAD #include "std_testcase.h" #include "CWE415_Double_Free__malloc_free_struct_83.h" namespace CWE415_Double_Free__malloc_free_struct_83 { CWE415_Double_Free__malloc_free_struct_83_bad::CWE415_Double_Free__malloc_free_struct_83_bad(twoIntsStruct * dataCopy) { data = dataCopy; data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct)); if (data == NULL) {exit(-1);} /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */ free(data); } CWE415_Double_Free__malloc_free_struct_83_bad::~CWE415_Double_Free__malloc_free_struct_83_bad() { /* POTENTIAL FLAW: Possibly freeing memory twice */ free(data); } } #endif /* OMITBAD */
83,983
testcases/CWE191_Integer_Underflow/s01/CWE191_Integer_Underflow__int64_t_fscanf_sub_68b.c
150
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int64_t_fscanf_sub_68b.c Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-68b.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: sub * GoodSink: Ensure there will not be an underflow before subtracting 1 from data * BadSink : Subtract 1 from data, which can cause an Underflow * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files * * */ #include "std_testcase.h" extern int64_t CWE191_Integer_Underflow__int64_t_fscanf_sub_68_badData; extern int64_t CWE191_Integer_Underflow__int64_t_fscanf_sub_68_goodG2BData; extern int64_t CWE191_Integer_Underflow__int64_t_fscanf_sub_68_goodB2GData; #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE191_Integer_Underflow__int64_t_fscanf_sub_68b_goodG2BSink() { int64_t data = CWE191_Integer_Underflow__int64_t_fscanf_sub_68_goodG2BData; { /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */ int64_t result = data - 1; printLongLongLine(result); } } /* goodB2G uses the BadSource with the GoodSink */ void CWE191_Integer_Underflow__int64_t_fscanf_sub_68b_goodB2GSink() { int64_t data = CWE191_Integer_Underflow__int64_t_fscanf_sub_68_goodB2GData; /* FIX: Add a check to prevent an underflow from occurring */ if (data > LLONG_MIN) { int64_t result = data - 1; printLongLongLine(result); } else { printLine("data value is too large to perform subtraction."); } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int64_t_fscanf_sub_68b.c Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-68b.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: sub * GoodSink: Ensure there will not be an underflow before subtracting 1 from data * BadSink : Subtract 1 from data, which can cause an Underflow * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files * * */ #include "std_testcase.h" extern int64_t CWE191_Integer_Underflow__int64_t_fscanf_sub_68_badData; extern int64_t CWE191_Integer_Underflow__int64_t_fscanf_sub_68_goodG2BData; extern int64_t CWE191_Integer_Underflow__int64_t_fscanf_sub_68_goodB2GData; #ifndef OMITBAD void CWE191_Integer_Underflow__int64_t_fscanf_sub_68b_badSink() { int64_t data = CWE191_Integer_Underflow__int64_t_fscanf_sub_68_badData; { /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */ int64_t result = data - 1; printLongLongLine(result); } } #endif /* OMITBAD */
92,200
testcases/CWE762_Mismatched_Memory_Management_Routines/s03/CWE762_Mismatched_Memory_Management_Routines__delete_int_malloc_14.cpp
178
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_int_malloc_14.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete.label.xml Template File: sources-sinks-14.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: malloc Allocate data using malloc() * GoodSource: Allocate data using new * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5) * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_int_malloc_14 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_int_malloc_14.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete.label.xml Template File: sources-sinks-14.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: malloc Allocate data using malloc() * GoodSource: Allocate data using new * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5) * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_int_malloc_14 { #ifndef OMITBAD void bad() { int * data; /* Initialize data*/ data = NULL; if(globalFive==5) { /* POTENTIAL FLAW: Allocate memory with a function that requires free() to free the memory */ data = (int *)malloc(100*sizeof(int)); if (data == NULL) {exit(-1);} } if(globalFive==5) { /* POTENTIAL FLAW: Deallocate memory using delete - the source memory allocation function may * require a call to free() to deallocate the memory */ delete data; } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
8,818
testcases/CWE78_OS_Command_Injection/s06/CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45.c
136
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45.c Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-45.tmpl.c */ /* * @description * CWE: 78 OS Command Injection * BadSource: console Read input from the console * GoodSource: Fixed string * Sinks: w32_execvp * BadSink : execute command with wexecvp * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #include <process.h> #define EXECVP _wexecvp static wchar_t * CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_badData; static wchar_t * CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_goodG2BData; return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45.c Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-45.tmpl.c */ /* * @description * CWE: 78 OS Command Injection * BadSource: console Read input from the console * GoodSource: Fixed string * Sinks: w32_execvp * BadSink : execute command with wexecvp * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #include <process.h> #define EXECVP _wexecvp static wchar_t * CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_badData; static wchar_t * CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_goodG2BData; #ifndef OMITBAD static void badSink() { wchar_t * data = CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_badData; { wchar_t *args[] = {COMMAND_INT_PATH, COMMAND_ARG1, COMMAND_ARG3, NULL}; /* wexecvp - searches for the location of the command among * the directories specified by the PATH environment variable */ /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */ EXECVP(COMMAND_INT, args); } } void CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_bad() { wchar_t * data; wchar_t dataBuffer[100] = COMMAND_ARG2; data = dataBuffer; { /* Read input from the console */ size_t dataLen = wcslen(data); /* if there is room in data, read into it from the console */ if (100-dataLen > 1) { /* POTENTIAL FLAW: Read data from the console */ if (fgetws(data+dataLen, (int)(100-dataLen), stdin) != NULL) { /* The next few lines remove the carriage return from the string that is * inserted by fgetws() */ dataLen = wcslen(data); if (dataLen > 0 && data[dataLen-1] == L'\n') { data[dataLen-1] = L'\0'; } } else { printLine("fgetws() failed"); /* Restore NUL terminator if fgetws fails */ data[dataLen] = L'\0'; } } } CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_badData = data; badSink(); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE78_OS_Command_Injection__wchar_t_console_w32_execvp_45_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
8,033
testcases/CWE78_OS_Command_Injection/s07/CWE78_OS_Command_Injection__wchar_t_environment_w32spawnl_73a.cpp
42
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_environment_w32spawnl_73a.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-73a.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: environment Read input from an environment variable * GoodSource: Fixed string * Sinks: w32spawnl * BadSink : execute command with wspawnl * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files * * */ #include "std_testcase.h" #include <list> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #define ENV_VARIABLE L"ADD" #ifdef _WIN32 #define GETENV _wgetenv #else #define GETENV getenv #endif using namespace std; namespace CWE78_OS_Command_Injection__wchar_t_environment_w32spawnl_73 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_environment_w32spawnl_73a.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-73a.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: environment Read input from an environment variable * GoodSource: Fixed string * Sinks: w32spawnl * BadSink : execute command with wspawnl * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files * * */ #include "std_testcase.h" #include <list> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #define ENV_VARIABLE L"ADD" #ifdef _WIN32 #define GETENV _wgetenv #else #define GETENV getenv #endif using namespace std; namespace CWE78_OS_Command_Injection__wchar_t_environment_w32spawnl_73 { #ifndef OMITBAD /* bad function declaration */ void badSink(list<wchar_t *> dataList); void bad() { wchar_t * data; list<wchar_t *> dataList; wchar_t dataBuffer[100] = COMMAND_ARG2; data = dataBuffer; { /* Append input from an environment variable to data */ size_t dataLen = wcslen(data); wchar_t * environment = GETENV(ENV_VARIABLE); /* If there is data in the environment variable */ if (environment != NULL) { /* POTENTIAL FLAW: Read data from an environment variable */ wcsncat(data+dataLen, environment, 100-dataLen-1); } } /* Put data in a list */ dataList.push_back(data); dataList.push_back(data); dataList.push_back(data); badSink(dataList); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
90,550
testcases/CWE762_Mismatched_Memory_Management_Routines/s02/CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82a.cpp
70
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82a.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete_array.label.xml Template File: sources-sinks-82a.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: calloc Allocate data using calloc() * GoodSource: Allocate data using new [] * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete [] * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer * * */ #include "std_testcase.h" #include "CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82a.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete_array.label.xml Template File: sources-sinks-82a.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: calloc Allocate data using calloc() * GoodSource: Allocate data using new [] * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete [] * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer * * */ #include "std_testcase.h" #include "CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82 { #ifndef OMITBAD void bad() { long * data; /* Initialize data*/ data = NULL; /* POTENTIAL FLAW: Allocate memory with a function that requires free() to free the memory */ data = (long *)calloc(100, sizeof(long)); if (data == NULL) {exit(-1);} CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82_base* baseObject = new CWE762_Mismatched_Memory_Management_Routines__delete_array_long_calloc_82_bad; baseObject->action(data); delete baseObject; } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
64,423
testcases/CWE427_Uncontrolled_Search_Path_Element/CWE427_Uncontrolled_Search_Path_Element__wchar_t_connect_socket_03.c
182
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE427_Uncontrolled_Search_Path_Element__wchar_t_connect_socket_03.c Label Definition File: CWE427_Uncontrolled_Search_Path_Element.label.xml Template File: sources-sink-03.tmpl.c */ /* * @description * CWE: 427 Uncontrolled Search Path Element * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Use a hardcoded path * Sink: * BadSink : Set the environment variable * Flow Variant: 03 Control flow: if(5==5) and if(5!=5) * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define NEW_PATH L"%SystemRoot%\\system32" #define PUTENV _wputenv #else #define NEW_PATH L"/bin" #define PUTENV putenv #endif #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else /* NOT _WIN32 */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define IP_ADDRESS "127.0.0.1" return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE427_Uncontrolled_Search_Path_Element__wchar_t_connect_socket_03.c Label Definition File: CWE427_Uncontrolled_Search_Path_Element.label.xml Template File: sources-sink-03.tmpl.c */ /* * @description * CWE: 427 Uncontrolled Search Path Element * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Use a hardcoded path * Sink: * BadSink : Set the environment variable * Flow Variant: 03 Control flow: if(5==5) and if(5!=5) * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define NEW_PATH L"%SystemRoot%\\system32" #define PUTENV _wputenv #else #define NEW_PATH L"/bin" #define PUTENV putenv #endif #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else /* NOT _WIN32 */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define IP_ADDRESS "127.0.0.1" #ifndef OMITBAD void CWE427_Uncontrolled_Search_Path_Element__wchar_t_connect_socket_03_bad() { wchar_t * data; wchar_t dataBuffer[250] = L"PATH="; data = dataBuffer; if(5==5) { { #ifdef _WIN32 WSADATA wsaData; int wsaDataInit = 0; #endif int recvResult; struct sockaddr_in service; wchar_t *replace; SOCKET connectSocket = INVALID_SOCKET; size_t dataLen = wcslen(data); do { #ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) { break; } wsaDataInit = 1; #endif /* POTENTIAL FLAW: Read data using a connect socket */ connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (connectSocket == INVALID_SOCKET) { break; } memset(&service, 0, sizeof(service)); service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(IP_ADDRESS); service.sin_port = htons(TCP_PORT); if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR) { break; } /* Abort on error or the connection was closed, make sure to recv one * less char than is in the recv_buf in order to append a terminator */ /* Abort on error or the connection was closed */ recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (250 - dataLen - 1), 0); if (recvResult == SOCKET_ERROR || recvResult == 0) { break; } /* Append null terminator */ data[dataLen + recvResult / sizeof(wchar_t)] = L'\0'; /* Eliminate CRLF */ replace = wcschr(data, L'\r'); if (replace) { *replace = L'\0'; } replace = wcschr(data, L'\n'); if (replace) { *replace = L'\0'; } } while (0); if (connectSocket != INVALID_SOCKET) { CLOSE_SOCKET(connectSocket); } #ifdef _WIN32 if (wsaDataInit) { WSACleanup(); } #endif } } /* POTENTIAL FLAW: Set a new environment variable with a path that is possibly insecure */ PUTENV(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE427_Uncontrolled_Search_Path_Element__wchar_t_connect_socket_03_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
64,459
testcases/CWE427_Uncontrolled_Search_Path_Element/CWE427_Uncontrolled_Search_Path_Element__char_environment_64b.c
182
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE427_Uncontrolled_Search_Path_Element__char_environment_64b.c Label Definition File: CWE427_Uncontrolled_Search_Path_Element.label.xml Template File: sources-sink-64b.tmpl.c */ /* * @description * CWE: 427 Uncontrolled Search Path Element * BadSource: environment Read input from an environment variable * GoodSource: Use a hardcoded path * Sinks: * BadSink : Set the environment variable * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define NEW_PATH "%SystemRoot%\\system32" #define PUTENV _putenv #else #define NEW_PATH "/bin" #define PUTENV putenv #endif #define ENV_VARIABLE "ADD" #ifdef _WIN32 #define GETENV getenv #else #define GETENV getenv #endif #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE427_Uncontrolled_Search_Path_Element__char_environment_64b_goodG2BSink(void * dataVoidPtr) { /* cast void pointer to a pointer of the appropriate type */ char * * dataPtr = (char * *)dataVoidPtr; /* dereference dataPtr into data */ char * data = (*dataPtr); /* POTENTIAL FLAW: Set a new environment variable with a path that is possibly insecure */ PUTENV(data); } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE427_Uncontrolled_Search_Path_Element__char_environment_64b.c Label Definition File: CWE427_Uncontrolled_Search_Path_Element.label.xml Template File: sources-sink-64b.tmpl.c */ /* * @description * CWE: 427 Uncontrolled Search Path Element * BadSource: environment Read input from an environment variable * GoodSource: Use a hardcoded path * Sinks: * BadSink : Set the environment variable * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define NEW_PATH "%SystemRoot%\\system32" #define PUTENV _putenv #else #define NEW_PATH "/bin" #define PUTENV putenv #endif #define ENV_VARIABLE "ADD" #ifdef _WIN32 #define GETENV getenv #else #define GETENV getenv #endif #ifndef OMITBAD void CWE427_Uncontrolled_Search_Path_Element__char_environment_64b_badSink(void * dataVoidPtr) { /* cast void pointer to a pointer of the appropriate type */ char * * dataPtr = (char * *)dataVoidPtr; /* dereference dataPtr into data */ char * data = (*dataPtr); /* POTENTIAL FLAW: Set a new environment variable with a path that is possibly insecure */ PUTENV(data); } #endif /* OMITBAD */
38,951
testcases/CWE134_Uncontrolled_Format_String/s04/CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67b.c
119
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67b.c Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml Template File: sources-vasinks-67b.tmpl.c */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: console Read input from the console * GoodSource: Copy a fixed string into data * Sinks: vprintf * GoodSink: vwprintf with a format string * BadSink : vwprintf without a format string * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files * * */ #include <stdarg.h> #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif typedef struct _CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67_structType { wchar_t * structFirst; } CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67_structType; #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ static void goodG2BVaSink(wchar_t * data, ...) { { va_list args; va_start(args, data); /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */ vwprintf(data, args); va_end(args); } } void CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67b_goodG2BSink(CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67_structType myStruct) { wchar_t * data = myStruct.structFirst; goodG2BVaSink(data, data); } /* goodB2G uses the BadSource with the GoodSink */ static void goodB2GVaSink(wchar_t * data, ...) { { va_list args; va_start(args, data); /* FIX: Specify the format disallowing a format string vulnerability */ vwprintf(L"%s", args); va_end(args); } } void CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67b_goodB2GSink(CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67_structType myStruct) { wchar_t * data = myStruct.structFirst; goodB2GVaSink(data, data); } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67b.c Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml Template File: sources-vasinks-67b.tmpl.c */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: console Read input from the console * GoodSource: Copy a fixed string into data * Sinks: vprintf * GoodSink: vwprintf with a format string * BadSink : vwprintf without a format string * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files * * */ #include <stdarg.h> #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif typedef struct _CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67_structType { wchar_t * structFirst; } CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67_structType; #ifndef OMITBAD static void badVaSink(wchar_t * data, ...) { { va_list args; va_start(args, data); /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */ vwprintf(data, args); va_end(args); } } void CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67b_badSink(CWE134_Uncontrolled_Format_String__wchar_t_console_vprintf_67_structType myStruct) { wchar_t * data = myStruct.structFirst; badVaSink(data, data); } #endif /* OMITBAD */
52,584
testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_81a.cpp
171
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE190_Integer_Overflow__int_fscanf_multiply_81a.cpp Label Definition File: CWE190_Integer_Overflow__int.label.xml Template File: sources-sinks-81a.tmpl.cpp */ /* * @description * CWE: 190 Integer Overflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (two) * Sinks: multiply * GoodSink: Ensure there will not be an overflow before multiplying data by 2 * BadSink : If data is positive, multiply by 2, which can cause an overflow * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */ #include "std_testcase.h" #include "CWE190_Integer_Overflow__int_fscanf_multiply_81.h" namespace CWE190_Integer_Overflow__int_fscanf_multiply_81 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE190_Integer_Overflow__int_fscanf_multiply_81a.cpp Label Definition File: CWE190_Integer_Overflow__int.label.xml Template File: sources-sinks-81a.tmpl.cpp */ /* * @description * CWE: 190 Integer Overflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (two) * Sinks: multiply * GoodSink: Ensure there will not be an overflow before multiplying data by 2 * BadSink : If data is positive, multiply by 2, which can cause an overflow * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */ #include "std_testcase.h" #include "CWE190_Integer_Overflow__int_fscanf_multiply_81.h" namespace CWE190_Integer_Overflow__int_fscanf_multiply_81 { #ifndef OMITBAD void bad() { int data; /* Initialize data */ data = 0; /* POTENTIAL FLAW: Read data from the console using fscanf() */ fscanf(stdin, "%d", &data); const CWE190_Integer_Overflow__int_fscanf_multiply_81_base& baseObject = CWE190_Integer_Overflow__int_fscanf_multiply_81_bad(); baseObject.action(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
3,025
testcases/CWE78_OS_Command_Injection/s01/CWE78_OS_Command_Injection__char_connect_socket_execlp_73b.cpp
69
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__char_connect_socket_execlp_73b.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-73b.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Fixed string * Sinks: execlp * BadSink : execute command with execlp * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files * * */ #include "std_testcase.h" #include <list> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH "%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT "cmd.exe" #define COMMAND_ARG1 "/c" #define COMMAND_ARG2 "dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH "/bin/sh" #define COMMAND_INT "sh" #define COMMAND_ARG1 "-c" #define COMMAND_ARG2 "ls " #define COMMAND_ARG3 data #endif #ifdef _WIN32 #include <process.h> #define EXECLP _execlp #else /* NOT _WIN32 */ #define EXECLP execlp #endif using namespace std; namespace CWE78_OS_Command_Injection__char_connect_socket_execlp_73 { #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void goodG2BSink(list<char *> dataList) { char * data = dataList.back(); /* execlp - searches for the location of the command among * the directories specified by the PATH environment variable */ /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */ EXECLP(COMMAND_INT, COMMAND_INT, COMMAND_ARG1, COMMAND_ARG3, NULL); } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__char_connect_socket_execlp_73b.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-73b.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: connect_socket Read data using a connect socket (client side) * GoodSource: Fixed string * Sinks: execlp * BadSink : execute command with execlp * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files * * */ #include "std_testcase.h" #include <list> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH "%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT "cmd.exe" #define COMMAND_ARG1 "/c" #define COMMAND_ARG2 "dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH "/bin/sh" #define COMMAND_INT "sh" #define COMMAND_ARG1 "-c" #define COMMAND_ARG2 "ls " #define COMMAND_ARG3 data #endif #ifdef _WIN32 #include <process.h> #define EXECLP _execlp #else /* NOT _WIN32 */ #define EXECLP execlp #endif using namespace std; namespace CWE78_OS_Command_Injection__char_connect_socket_execlp_73 { #ifndef OMITBAD void badSink(list<char *> dataList) { /* copy data out of dataList */ char * data = dataList.back(); /* execlp - searches for the location of the command among * the directories specified by the PATH environment variable */ /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */ EXECLP(COMMAND_INT, COMMAND_INT, COMMAND_ARG1, COMMAND_ARG3, NULL); } #endif /* OMITBAD */ } /* close namespace */
18,419
testcases/CWE121_Stack_Based_Buffer_Overflow/s08/CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_52a.cpp
57
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_52a.cpp Label Definition File: CWE121_Stack_Based_Buffer_Overflow__placement_new.label.xml Template File: sources-sinks-52a.tmpl.cpp */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Initialize data to a small buffer * GoodSource: Initialize data to a buffer large enough to hold a TwoIntsClass * Sinks: * GoodSink: Allocate a new class using placement new and a buffer that is large enough to hold the class * BadSink : Allocate a new class using placement new and a buffer that is too small * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" namespace CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_52 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_52a.cpp Label Definition File: CWE121_Stack_Based_Buffer_Overflow__placement_new.label.xml Template File: sources-sinks-52a.tmpl.cpp */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Initialize data to a small buffer * GoodSource: Initialize data to a buffer large enough to hold a TwoIntsClass * Sinks: * GoodSink: Allocate a new class using placement new and a buffer that is large enough to hold the class * BadSink : Allocate a new class using placement new and a buffer that is too small * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" namespace CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_52 { #ifndef OMITBAD /* bad function declaration */ void badSink_b(char * data); void bad() { char * data; char dataBadBuffer[sizeof(OneIntClass)]; char dataGoodBuffer[sizeof(TwoIntsClass)]; /* POTENTIAL FLAW: Initialize data to a buffer smaller than the sizeof(TwoIntsClass) */ data = dataBadBuffer; badSink_b(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
73,961
testcases/CWE122_Heap_Based_Buffer_Overflow/s07/CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53b.c
81
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53b.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-53b.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sink: memmove * BadSink : Copy string to data using memmove() * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITGOOD /* good function declaration */ void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53c_goodG2BSink(char * data); /* goodG2B uses the GoodSource with the BadSink */ void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53b_goodG2BSink(char * data) { CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53c_goodG2BSink(data); } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53b.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-53b.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sink: memmove * BadSink : Copy string to data using memmove() * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITBAD /* bad function declaration */ void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53c_badSink(char * data); void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53b_badSink(char * data) { CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_memmove_53c_badSink(data); } #endif /* OMITBAD */
99,161
testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_52c.c
103
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE369_Divide_by_Zero__float_zero_52c.c Label Definition File: CWE369_Divide_by_Zero__float.label.xml Template File: sources-sinks-52c.tmpl.c */ /* * @description * CWE: 369 Divide by Zero * BadSource: zero Fixed value of zero * GoodSource: A hardcoded non-zero number (two) * Sinks: * GoodSink: Check value of or near zero before dividing * BadSink : Divide a constant by data * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #include <math.h> #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE369_Divide_by_Zero__float_zero_52c_goodG2BSink(float data) { { /* POTENTIAL FLAW: Possibly divide by zero */ int result = (int)(100.0 / data); printIntLine(result); } } /* goodB2G uses the BadSource with the GoodSink */ void CWE369_Divide_by_Zero__float_zero_52c_goodB2GSink(float data) { /* FIX: Check for value of or near zero before dividing */ if(fabs(data) > 0.000001) { int result = (int)(100.0 / data); printIntLine(result); } else { printLine("This would result in a divide by zero"); } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE369_Divide_by_Zero__float_zero_52c.c Label Definition File: CWE369_Divide_by_Zero__float.label.xml Template File: sources-sinks-52c.tmpl.c */ /* * @description * CWE: 369 Divide by Zero * BadSource: zero Fixed value of zero * GoodSource: A hardcoded non-zero number (two) * Sinks: * GoodSink: Check value of or near zero before dividing * BadSink : Divide a constant by data * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #include <math.h> #ifndef OMITBAD void CWE369_Divide_by_Zero__float_zero_52c_badSink(float data) { { /* POTENTIAL FLAW: Possibly divide by zero */ int result = (int)(100.0 / data); printIntLine(result); } } #endif /* OMITBAD */
27,995
testcases/CWE126_Buffer_Overread/s01/CWE126_Buffer_Overread__CWE129_listen_socket_14.c
76
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__CWE129_listen_socket_14.c Label Definition File: CWE126_Buffer_Overread__CWE129.label.xml Template File: sources-sinks-14.tmpl.c */ /* * @description * CWE: 126 Buffer Overread * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Larger than zero but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking the upper bound * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5) * * */ #include "std_testcase.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__CWE129_listen_socket_14.c Label Definition File: CWE126_Buffer_Overread__CWE129.label.xml Template File: sources-sinks-14.tmpl.c */ /* * @description * CWE: 126 Buffer Overread * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Larger than zero but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking the upper bound * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5) * * */ #include "std_testcase.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) #ifndef OMITBAD void CWE126_Buffer_Overread__CWE129_listen_socket_14_bad() { int data; /* Initialize data */ data = -1; if(globalFive==5) { { #ifdef _WIN32 WSADATA wsaData; int wsaDataInit = 0; #endif int recvResult; struct sockaddr_in service; SOCKET listenSocket = INVALID_SOCKET; SOCKET acceptSocket = INVALID_SOCKET; char inputBuffer[CHAR_ARRAY_SIZE]; do { #ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) { break; } wsaDataInit = 1; #endif /* POTENTIAL FLAW: Read data using a listen socket */ listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listenSocket == INVALID_SOCKET) { break; } memset(&service, 0, sizeof(service)); service.sin_family = AF_INET; service.sin_addr.s_addr = INADDR_ANY; service.sin_port = htons(TCP_PORT); if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR) { break; } if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR) { break; } acceptSocket = accept(listenSocket, NULL, NULL); if (acceptSocket == SOCKET_ERROR) { break; } /* Abort on error or the connection was closed */ recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0); if (recvResult == SOCKET_ERROR || recvResult == 0) { break; } /* NUL-terminate the string */ inputBuffer[recvResult] = '\0'; /* Convert to int */ data = atoi(inputBuffer); } while (0); if (listenSocket != INVALID_SOCKET) { CLOSE_SOCKET(listenSocket); } if (acceptSocket != INVALID_SOCKET) { CLOSE_SOCKET(acceptSocket); } #ifdef _WIN32 if (wsaDataInit) { WSACleanup(); } #endif } } if(globalFive==5) { { int buffer[10] = { 0 }; /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound * This check does not check the upper bounds of the array index */ if (data >= 0) { printIntLine(buffer[data]); } else { printLine("ERROR: Array index is negative"); } } } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE126_Buffer_Overread__CWE129_listen_socket_14_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
75,906
testcases/CWE122_Heap_Based_Buffer_Overflow/s06/CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53c.c
125
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53c.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-53c.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sink: cpy * BadSink : Copy string to data using strcpy() * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITGOOD /* good function declaration */ void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53d_goodG2BSink(char * data); /* goodG2B uses the GoodSource with the BadSink */ void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53c_goodG2BSink(char * data) { CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53d_goodG2BSink(data); } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53c.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-53c.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sink: cpy * BadSink : Copy string to data using strcpy() * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITBAD /* bad function declaration */ void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53d_badSink(char * data); void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53c_badSink(char * data) { CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_53d_badSink(data); } #endif /* OMITBAD */
37,972
testcases/CWE134_Uncontrolled_Format_String/s01/CWE134_Uncontrolled_Format_String__char_console_vfprintf_07.c
8
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__char_console_vfprintf_07.c Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml Template File: sources-vasinks-07.tmpl.c */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: console Read input from the console * GoodSource: Copy a fixed string into data * Sinks: vfprintf * GoodSink: vfprintf with a format string * BadSink : vfprintf without a format string * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5) * * */ #include <stdarg.h> #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* The variable below is not declared "const", but is never assigned any other value so a tool should be able to identify that reads of this will always give its initialized value. */ static int staticFive = 5; return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__char_console_vfprintf_07.c Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml Template File: sources-vasinks-07.tmpl.c */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: console Read input from the console * GoodSource: Copy a fixed string into data * Sinks: vfprintf * GoodSink: vfprintf with a format string * BadSink : vfprintf without a format string * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5) * * */ #include <stdarg.h> #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* The variable below is not declared "const", but is never assigned any other value so a tool should be able to identify that reads of this will always give its initialized value. */ static int staticFive = 5; #ifndef OMITBAD static void badVaSinkB(char * data, ...) { { va_list args; va_start(args, data); /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */ vfprintf(stdout, data, args); va_end(args); } } void CWE134_Uncontrolled_Format_String__char_console_vfprintf_07_bad() { char * data; char dataBuffer[100] = ""; data = dataBuffer; if(staticFive==5) { { /* Read input from the console */ size_t dataLen = strlen(data); /* if there is room in data, read into it from the console */ if (100-dataLen > 1) { /* POTENTIAL FLAW: Read data from the console */ if (fgets(data+dataLen, (int)(100-dataLen), stdin) != NULL) { /* The next few lines remove the carriage return from the string that is * inserted by fgets() */ dataLen = strlen(data); if (dataLen > 0 && data[dataLen-1] == '\n') { data[dataLen-1] = '\0'; } } else { printLine("fgets() failed"); /* Restore NUL terminator if fgets fails */ data[dataLen] = '\0'; } } } } if(staticFive==5) { badVaSinkB(data, data); } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE134_Uncontrolled_Format_String__char_console_vfprintf_07_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
85,415
testcases/CWE191_Integer_Underflow/s04/CWE191_Integer_Underflow__int64_t_fscanf_postdec_54e.c
95
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int64_t_fscanf_postdec_54e.c Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-54e.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: decrement * GoodSink: Ensure there will not be an underflow before decrementing data * BadSink : Decrement data, which can cause an Underflow * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE191_Integer_Underflow__int64_t_fscanf_postdec_54e_goodG2BSink(int64_t data) { { /* POTENTIAL FLAW: Decrementing data could cause an underflow */ data--; int64_t result = data; printLongLongLine(result); } } /* goodB2G uses the BadSource with the GoodSink */ void CWE191_Integer_Underflow__int64_t_fscanf_postdec_54e_goodB2GSink(int64_t data) { /* FIX: Add a check to prevent an underflow from occurring */ if (data > LLONG_MIN) { data--; int64_t result = data; printLongLongLine(result); } else { printLine("data value is too large to perform arithmetic safely."); } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int64_t_fscanf_postdec_54e.c Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-54e.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: decrement * GoodSink: Ensure there will not be an underflow before decrementing data * BadSink : Decrement data, which can cause an Underflow * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" #ifndef OMITBAD void CWE191_Integer_Underflow__int64_t_fscanf_postdec_54e_badSink(int64_t data) { { /* POTENTIAL FLAW: Decrementing data could cause an underflow */ data--; int64_t result = data; printLongLongLine(result); } } #endif /* OMITBAD */
22,476
testcases/CWE121_Stack_Based_Buffer_Overflow/s06/CWE121_Stack_Based_Buffer_Overflow__CWE806_char_alloca_memmove_15.c
96
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE806_char_alloca_memmove_15.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE806.label.xml Template File: sources-sink-15.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Initialize data as a large string * GoodSource: Initialize data as a small string * Sink: memmove * BadSink : Copy data to string using memmove * Flow Variant: 15 Control flow: switch(6) * * */ #include "std_testcase.h" #include <wchar.h> return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE806_char_alloca_memmove_15.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE806.label.xml Template File: sources-sink-15.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Initialize data as a large string * GoodSource: Initialize data as a small string * Sink: memmove * BadSink : Copy data to string using memmove * Flow Variant: 15 Control flow: switch(6) * * */ #include "std_testcase.h" #include <wchar.h> #ifndef OMITBAD void CWE121_Stack_Based_Buffer_Overflow__CWE806_char_alloca_memmove_15_bad() { char * data; char * dataBuffer = (char *)ALLOCA(100*sizeof(char)); data = dataBuffer; switch(6) { case 6: /* FLAW: Initialize data as a large buffer that is larger than the small buffer used in the sink */ memset(data, 'A', 100-1); /* fill with 'A's */ data[100-1] = '\0'; /* null terminate */ break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ printLine("Benign, fixed string"); break; } { char dest[50] = ""; /* POTENTIAL FLAW: Possible buffer overflow if data is larger than dest */ memmove(dest, data, strlen(data)*sizeof(char)); dest[50-1] = '\0'; /* Ensure the destination buffer is null terminated */ printLine(data); } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE121_Stack_Based_Buffer_Overflow__CWE806_char_alloca_memmove_15_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
88,667
testcases/CWE762_Mismatched_Memory_Management_Routines/s01/CWE762_Mismatched_Memory_Management_Routines__delete_array_class_realloc_54e.cpp
43
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_array_class_realloc_54e.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete_array.label.xml Template File: sources-sinks-54e.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: realloc Allocate data using realloc() * GoodSource: Allocate data using new [] * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete [] * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_array_class_realloc_54 { #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void goodG2BSink_e(TwoIntsClass * data) { /* POTENTIAL FLAW: Deallocate memory using delete [] - the source memory allocation function may * require a call to free() to deallocate the memory */ delete [] data; } /* goodB2G uses the BadSource with the GoodSink */ void goodB2GSink_e(TwoIntsClass * data) { /* FIX: Free memory using free() */ free(data); } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_array_class_realloc_54e.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete_array.label.xml Template File: sources-sinks-54e.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: realloc Allocate data using realloc() * GoodSource: Allocate data using new [] * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete [] * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_array_class_realloc_54 { #ifndef OMITBAD void badSink_e(TwoIntsClass * data) { /* POTENTIAL FLAW: Deallocate memory using delete [] - the source memory allocation function may * require a call to free() to deallocate the memory */ delete [] data; } #endif /* OMITBAD */ } /* close namespace */
100,082
testcases/CWE369_Divide_by_Zero/s02/CWE369_Divide_by_Zero__int_zero_modulo_81_bad.cpp
87
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE369_Divide_by_Zero__int_zero_modulo_81_bad.cpp Label Definition File: CWE369_Divide_by_Zero__int.label.xml Template File: sources-sinks-81_bad.tmpl.cpp */ /* * @description * CWE: 369 Divide by Zero * BadSource: zero Fixed value of zero * GoodSource: Non-zero * Sinks: modulo * GoodSink: Check for zero before modulo * BadSink : Modulo a constant with data * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE369_Divide_by_Zero__int_zero_modulo_81_bad.cpp Label Definition File: CWE369_Divide_by_Zero__int.label.xml Template File: sources-sinks-81_bad.tmpl.cpp */ /* * @description * CWE: 369 Divide by Zero * BadSource: zero Fixed value of zero * GoodSource: Non-zero * Sinks: modulo * GoodSink: Check for zero before modulo * BadSink : Modulo a constant with data * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */ #ifndef OMITBAD #include "std_testcase.h" #include "CWE369_Divide_by_Zero__int_zero_modulo_81.h" namespace CWE369_Divide_by_Zero__int_zero_modulo_81 { void CWE369_Divide_by_Zero__int_zero_modulo_81_bad::action(int data) const { /* POTENTIAL FLAW: Possibly divide by zero */ printIntLine(100 % data); } } #endif /* OMITBAD */
50,142
testcases/CWE190_Integer_Overflow/s01/CWE190_Integer_Overflow__char_rand_add_62b.cpp
55
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE190_Integer_Overflow__char_rand_add_62b.cpp Label Definition File: CWE190_Integer_Overflow.label.xml Template File: sources-sinks-62b.tmpl.cpp */ /* * @description * CWE: 190 Integer Overflow * BadSource: rand Set data to result of rand() * GoodSource: Set data to a small, non-zero number (two) * Sinks: add * GoodSink: Ensure there will not be an overflow before adding 1 to data * BadSink : Add 1 to data, which can cause an overflow * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files * * */ #include "std_testcase.h" namespace CWE190_Integer_Overflow__char_rand_add_62 { #ifndef OMITGOOD /* goodG2B() uses the GoodSource with the BadSink */ void goodG2BSource(char &data) { /* FIX: Use a small, non-zero value that will not cause an overflow in the sinks */ data = 2; } /* goodB2G() uses the BadSource with the GoodSink */ void goodB2GSource(char &data) { /* POTENTIAL FLAW: Use a random value */ data = (char)RAND32(); } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE190_Integer_Overflow__char_rand_add_62b.cpp Label Definition File: CWE190_Integer_Overflow.label.xml Template File: sources-sinks-62b.tmpl.cpp */ /* * @description * CWE: 190 Integer Overflow * BadSource: rand Set data to result of rand() * GoodSource: Set data to a small, non-zero number (two) * Sinks: add * GoodSink: Ensure there will not be an overflow before adding 1 to data * BadSink : Add 1 to data, which can cause an overflow * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files * * */ #include "std_testcase.h" namespace CWE190_Integer_Overflow__char_rand_add_62 { #ifndef OMITBAD void badSource(char &data) { /* POTENTIAL FLAW: Use a random value */ data = (char)RAND32(); } #endif /* OMITBAD */ } /* close namespace */
48,026
testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_62b.cpp
4
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE416_Use_After_Free__malloc_free_int64_t_62b.cpp Label Definition File: CWE416_Use_After_Free__malloc_free.label.xml Template File: sources-sinks-62b.tmpl.cpp */ /* * @description * CWE: 416 Use After Free * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free() * GoodSource: Allocate data using malloc() and initialize memory block * Sinks: * GoodSink: Do nothing * BadSink : Use data * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE416_Use_After_Free__malloc_free_int64_t_62 { #ifndef OMITGOOD /* goodG2B() uses the GoodSource with the BadSink */ void goodG2BSource(int64_t * &data) { data = (int64_t *)malloc(100*sizeof(int64_t)); if (data == NULL) {exit(-1);} { size_t i; for(i = 0; i < 100; i++) { data[i] = 5LL; } } /* FIX: Do not free data in the source */ } /* goodB2G() uses the BadSource with the GoodSink */ void goodB2GSource(int64_t * &data) { data = (int64_t *)malloc(100*sizeof(int64_t)); if (data == NULL) {exit(-1);} { size_t i; for(i = 0; i < 100; i++) { data[i] = 5LL; } } /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */ free(data); } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE416_Use_After_Free__malloc_free_int64_t_62b.cpp Label Definition File: CWE416_Use_After_Free__malloc_free.label.xml Template File: sources-sinks-62b.tmpl.cpp */ /* * @description * CWE: 416 Use After Free * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free() * GoodSource: Allocate data using malloc() and initialize memory block * Sinks: * GoodSink: Do nothing * BadSink : Use data * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE416_Use_After_Free__malloc_free_int64_t_62 { #ifndef OMITBAD void badSource(int64_t * &data) { data = (int64_t *)malloc(100*sizeof(int64_t)); if (data == NULL) {exit(-1);} { size_t i; for(i = 0; i < 100; i++) { data[i] = 5LL; } } /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */ free(data); } #endif /* OMITBAD */ } /* close namespace */
4,103
testcases/CWE78_OS_Command_Injection/s08/CWE78_OS_Command_Injection__wchar_t_listen_socket_w32_execvp_09.c
157
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_listen_socket_w32_execvp_09.c Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-09.tmpl.c */ /* * @description * CWE: 78 OS Command Injection * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Fixed string * Sink: w32_execvp * BadSink : execute command with wexecvp * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE) * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 #include <process.h> #define EXECVP _wexecvp return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__wchar_t_listen_socket_w32_execvp_09.c Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-09.tmpl.c */ /* * @description * CWE: 78 OS Command Injection * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Fixed string * Sink: w32_execvp * BadSink : execute command with wexecvp * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE) * * */ #include "std_testcase.h" #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH L"%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT L"cmd.exe" #define COMMAND_ARG1 L"/c" #define COMMAND_ARG2 L"dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH L"/bin/sh" #define COMMAND_INT L"sh" #define COMMAND_ARG1 L"-c" #define COMMAND_ARG2 L"ls " #define COMMAND_ARG3 data #endif #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 #include <process.h> #define EXECVP _wexecvp #ifndef OMITBAD void CWE78_OS_Command_Injection__wchar_t_listen_socket_w32_execvp_09_bad() { wchar_t * data; wchar_t dataBuffer[100] = COMMAND_ARG2; data = dataBuffer; if(GLOBAL_CONST_TRUE) { { #ifdef _WIN32 WSADATA wsaData; int wsaDataInit = 0; #endif int recvResult; struct sockaddr_in service; wchar_t *replace; SOCKET listenSocket = INVALID_SOCKET; SOCKET acceptSocket = INVALID_SOCKET; size_t dataLen = wcslen(data); do { #ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) { break; } wsaDataInit = 1; #endif /* POTENTIAL FLAW: Read data using a listen socket */ listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listenSocket == INVALID_SOCKET) { break; } memset(&service, 0, sizeof(service)); service.sin_family = AF_INET; service.sin_addr.s_addr = INADDR_ANY; service.sin_port = htons(TCP_PORT); if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR) { break; } if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR) { break; } acceptSocket = accept(listenSocket, NULL, NULL); if (acceptSocket == SOCKET_ERROR) { break; } /* Abort on error or the connection was closed */ recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0); if (recvResult == SOCKET_ERROR || recvResult == 0) { break; } /* Append null terminator */ data[dataLen + recvResult / sizeof(wchar_t)] = L'\0'; /* Eliminate CRLF */ replace = wcschr(data, L'\r'); if (replace) { *replace = L'\0'; } replace = wcschr(data, L'\n'); if (replace) { *replace = L'\0'; } } while (0); if (listenSocket != INVALID_SOCKET) { CLOSE_SOCKET(listenSocket); } if (acceptSocket != INVALID_SOCKET) { CLOSE_SOCKET(acceptSocket); } #ifdef _WIN32 if (wsaDataInit) { WSACleanup(); } #endif } } { wchar_t *args[] = {COMMAND_INT_PATH, COMMAND_ARG1, COMMAND_ARG3, NULL}; /* wexecvp - searches for the location of the command among * the directories specified by the PATH environment variable */ /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */ EXECVP(COMMAND_INT, args); } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE78_OS_Command_Injection__wchar_t_listen_socket_w32_execvp_09_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
90,146
testcases/CWE762_Mismatched_Memory_Management_Routines/s04/CWE762_Mismatched_Memory_Management_Routines__delete_wchar_t_realloc_53d.cpp
53
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_wchar_t_realloc_53d.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete.label.xml Template File: sources-sinks-53d.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: realloc Allocate data using realloc() * GoodSource: Allocate data using new * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_wchar_t_realloc_53 { #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void goodG2BSink_d(wchar_t * data) { /* POTENTIAL FLAW: Deallocate memory using delete - the source memory allocation function may * require a call to free() to deallocate the memory */ delete data; } /* goodB2G uses the BadSource with the GoodSink */ void goodB2GSink_d(wchar_t * data) { /* FIX: Deallocate the memory using free() */ free(data); } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_wchar_t_realloc_53d.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete.label.xml Template File: sources-sinks-53d.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: realloc Allocate data using realloc() * GoodSource: Allocate data using new * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_wchar_t_realloc_53 { #ifndef OMITBAD void badSink_d(wchar_t * data) { /* POTENTIAL FLAW: Deallocate memory using delete - the source memory allocation function may * require a call to free() to deallocate the memory */ delete data; } #endif /* OMITBAD */ } /* close namespace */
67,830
testcases/CWE122_Heap_Based_Buffer_Overflow/s01/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_char_memmove_64b.cpp
141
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_char_memmove_64b.cpp Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193.label.xml Template File: sources-sink-64b.tmpl.cpp */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sinks: memmove * BadSink : Copy string to data using memmove() * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_char_memmove_64 { #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void goodG2BSink(void * dataVoidPtr) { /* cast void pointer to a pointer of the appropriate type */ char * * dataPtr = (char * *)dataVoidPtr; /* dereference dataPtr into data */ char * data = (*dataPtr); { char source[10+1] = SRC_STRING; /* Copy length + 1 to include NUL terminator from source */ /* POTENTIAL FLAW: data may not have enough space to hold source */ memmove(data, source, (strlen(source) + 1) * sizeof(char)); printLine(data); delete [] data; } } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_char_memmove_64b.cpp Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193.label.xml Template File: sources-sink-64b.tmpl.cpp */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sinks: memmove * BadSink : Copy string to data using memmove() * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_char_memmove_64 { #ifndef OMITBAD void badSink(void * dataVoidPtr) { /* cast void pointer to a pointer of the appropriate type */ char * * dataPtr = (char * *)dataVoidPtr; /* dereference dataPtr into data */ char * data = (*dataPtr); { char source[10+1] = SRC_STRING; /* Copy length + 1 to include NUL terminator from source */ /* POTENTIAL FLAW: data may not have enough space to hold source */ memmove(data, source, (strlen(source) + 1) * sizeof(char)); printLine(data); delete [] data; } } #endif /* OMITBAD */ } /* close namespace */
92,962
testcases/CWE762_Mismatched_Memory_Management_Routines/s06/CWE762_Mismatched_Memory_Management_Routines__new_delete_array_long_62b.cpp
133
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__new_delete_array_long_62b.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__new_delete_array.label.xml Template File: sources-sinks-62b.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: Allocate data using new * GoodSource: Allocate data using new [] * Sinks: * GoodSink: Deallocate data using delete * BadSink : Deallocate data using delete [] * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files * * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__new_delete_array_long_62 { #ifndef OMITGOOD /* goodG2B() uses the GoodSource with the BadSink */ void goodG2BSource(long * &data) { /* FIX: Allocate memory from the heap using new [] */ data = new long[100]; } /* goodB2G() uses the BadSource with the GoodSink */ void goodB2GSource(long * &data) { /* POTENTIAL FLAW: Allocate memory with a function that requires delete to free the memory */ data = new long; } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__new_delete_array_long_62b.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__new_delete_array.label.xml Template File: sources-sinks-62b.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: Allocate data using new * GoodSource: Allocate data using new [] * Sinks: * GoodSink: Deallocate data using delete * BadSink : Deallocate data using delete [] * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files * * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__new_delete_array_long_62 { #ifndef OMITBAD void badSource(long * &data) { /* POTENTIAL FLAW: Allocate memory with a function that requires delete to free the memory */ data = new long; } #endif /* OMITBAD */ } /* close namespace */
58,304
testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_pointer_18.c
86
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE457_Use_of_Uninitialized_Variable__double_pointer_18.c Label Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml Template File: sources-sinks-18.tmpl.c */ /* * @description * CWE: 457 Use of Uninitialized Variable * BadSource: no_init Don't initialize data * GoodSource: Initialize data * Sinks: use * GoodSink: Initialize then use data * BadSink : Use data * Flow Variant: 18 Control flow: goto statements * * */ #include "std_testcase.h" #include <wchar.h> return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE457_Use_of_Uninitialized_Variable__double_pointer_18.c Label Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml Template File: sources-sinks-18.tmpl.c */ /* * @description * CWE: 457 Use of Uninitialized Variable * BadSource: no_init Don't initialize data * GoodSource: Initialize data * Sinks: use * GoodSink: Initialize then use data * BadSink : Use data * Flow Variant: 18 Control flow: goto statements * * */ #include "std_testcase.h" #include <wchar.h> #ifndef OMITBAD void CWE457_Use_of_Uninitialized_Variable__double_pointer_18_bad() { double * data; goto source; source: /* POTENTIAL FLAW: Don't initialize data */ ; /* empty statement needed for some flow variants */ goto sink; sink: /* POTENTIAL FLAW: Use data without initializing it */ printDoubleLine(*data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE457_Use_of_Uninitialized_Variable__double_pointer_18_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
91,894
testcases/CWE762_Mismatched_Memory_Management_Routines/s03/CWE762_Mismatched_Memory_Management_Routines__delete_class_realloc_12.cpp
178
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_class_realloc_12.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete.label.xml Template File: sources-sinks-12.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: realloc Allocate data using realloc() * GoodSource: Allocate data using new * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse()) * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_class_realloc_12 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__delete_class_realloc_12.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__delete.label.xml Template File: sources-sinks-12.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: realloc Allocate data using realloc() * GoodSource: Allocate data using new * Sinks: * GoodSink: Deallocate data using free() * BadSink : Deallocate data using delete * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse()) * */ #include "std_testcase.h" namespace CWE762_Mismatched_Memory_Management_Routines__delete_class_realloc_12 { #ifndef OMITBAD void bad() { TwoIntsClass * data; /* Initialize data*/ data = NULL; if(globalReturnsTrueOrFalse()) { data = NULL; /* POTENTIAL FLAW: Allocate memory with a function that requires free() to free the memory */ data = (TwoIntsClass *)realloc(data, 100*sizeof(TwoIntsClass)); if (data == NULL) {exit(-1);} } else { /* FIX: Allocate memory from the heap using new */ data = new TwoIntsClass; } if(globalReturnsTrueOrFalse()) { /* POTENTIAL FLAW: Deallocate memory using delete - the source memory allocation function may * require a call to free() to deallocate the memory */ delete data; } else { /* FIX: Deallocate the memory using free() */ free(data); } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
15,275
testcases/CWE121_Stack_Based_Buffer_Overflow/s05/CWE121_Stack_Based_Buffer_Overflow__CWE805_struct_declare_memmove_32.c
7
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE805_struct_declare_memmove_32.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.label.xml Template File: sources-sink-32.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Set data pointer to the bad buffer * GoodSource: Set data pointer to the good buffer * Sink: memmove * BadSink : Copy twoIntsStruct array to data using memmove * Flow Variant: 32 Data flow using two pointers to the same value within the same function * * */ #include "std_testcase.h" return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE805_struct_declare_memmove_32.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.label.xml Template File: sources-sink-32.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Set data pointer to the bad buffer * GoodSource: Set data pointer to the good buffer * Sink: memmove * BadSink : Copy twoIntsStruct array to data using memmove * Flow Variant: 32 Data flow using two pointers to the same value within the same function * * */ #include "std_testcase.h" #ifndef OMITBAD void CWE121_Stack_Based_Buffer_Overflow__CWE805_struct_declare_memmove_32_bad() { twoIntsStruct * data; twoIntsStruct * *dataPtr1 = &data; twoIntsStruct * *dataPtr2 = &data; twoIntsStruct dataBadBuffer[50]; twoIntsStruct dataGoodBuffer[100]; { twoIntsStruct * data = *dataPtr1; /* FLAW: Set a pointer to a "small" buffer. This buffer will be used in the sinks as a destination * buffer in various memory copying functions using a "large" source buffer. */ data = dataBadBuffer; *dataPtr1 = data; } { twoIntsStruct * data = *dataPtr2; { twoIntsStruct source[100]; { size_t i; /* Initialize array */ for (i = 0; i < 100; i++) { source[i].intOne = 0; source[i].intTwo = 0; } } /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */ memmove(data, source, 100*sizeof(twoIntsStruct)); printStructLine(&data[0]); } } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE121_Stack_Based_Buffer_Overflow__CWE805_struct_declare_memmove_32_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
28,111
testcases/CWE126_Buffer_Overread/s01/CWE126_Buffer_Overread__char_declare_loop_81_bad.cpp
76
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__char_declare_loop_81_bad.cpp Label Definition File: CWE126_Buffer_Overread.stack.label.xml Template File: sources-sink-81_bad.tmpl.cpp */ /* * @description * CWE: 126 Buffer Over-read * BadSource: Set data pointer to a small buffer * GoodSource: Set data pointer to a large buffer * Sinks: loop * BadSink : Copy data to string using a loop * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__char_declare_loop_81_bad.cpp Label Definition File: CWE126_Buffer_Overread.stack.label.xml Template File: sources-sink-81_bad.tmpl.cpp */ /* * @description * CWE: 126 Buffer Over-read * BadSource: Set data pointer to a small buffer * GoodSource: Set data pointer to a large buffer * Sinks: loop * BadSink : Copy data to string using a loop * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */ #ifndef OMITBAD #include "std_testcase.h" #include "CWE126_Buffer_Overread__char_declare_loop_81.h" namespace CWE126_Buffer_Overread__char_declare_loop_81 { void CWE126_Buffer_Overread__char_declare_loop_81_bad::action(char * data) const { { size_t i, destLen; char dest[100]; memset(dest, 'C', 100-1); dest[100-1] = '\0'; /* null terminate */ destLen = strlen(dest); /* POTENTIAL FLAW: using length of the dest where data * could be smaller than dest causing buffer overread */ for (i = 0; i < destLen; i++) { dest[i] = data[i]; } dest[100-1] = '\0'; printLine(dest); } } } #endif /* OMITBAD */
75,826
testcases/CWE122_Heap_Based_Buffer_Overflow/s06/CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34.c
125
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-34.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sinks: cpy * BadSink : Copy string to data using strcpy() * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function) * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" typedef union { char * unionFirst; char * unionSecond; } CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34_unionType; return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34.c Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml Template File: sources-sink-34.tmpl.c */ /* * @description * CWE: 122 Heap Based Buffer Overflow * BadSource: Allocate memory for a string, but do not allocate space for NULL terminator * GoodSource: Allocate enough memory for a string and the NULL terminator * Sinks: cpy * BadSink : Copy string to data using strcpy() * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function) * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" typedef union { char * unionFirst; char * unionSecond; } CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34_unionType; #ifndef OMITBAD void CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34_bad() { char * data; CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34_unionType myUnion; data = NULL; /* FLAW: Did not leave space for a null terminator */ data = (char *)malloc(10*sizeof(char)); if (data == NULL) {exit(-1);} myUnion.unionFirst = data; { char * data = myUnion.unionSecond; { char source[10+1] = SRC_STRING; /* POTENTIAL FLAW: data may not have enough space to hold source */ strcpy(data, source); printLine(data); free(data); } } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_cpy_34_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
100,816
testcases/CWE321_Hard_Coded_Cryptographic_Key/CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67b.c
144
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67b.c Label Definition File: CWE321_Hard_Coded_Cryptographic_Key__w32.label.xml Template File: sources-sink-67b.tmpl.c */ /* * @description * CWE: 321 Use of Hard-coded Cryptographic Key * BadSource: Copy a hardcoded value into cryptoKey * GoodSource: Read cryptoKey from the console * Sinks: * BadSink : Hash cryptoKey and use the value to encrypt a string * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files * * */ #include "std_testcase.h" #define CRYPTO_KEY L"Hardcoded" #include <windows.h> #include <wincrypt.h> /* Link with the Advapi32.lib file for Crypt* functions */ #pragma comment (lib, "Advapi32") typedef struct _CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67_structType { wchar_t * structFirst; } CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67_structType; #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67b_goodG2BSink(CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67_structType myStruct) { wchar_t * cryptoKey = myStruct.structFirst; { HCRYPTPROV hCryptProv; HCRYPTKEY hKey; HCRYPTHASH hHash; wchar_t toBeEncrypted[] = L"String to be encrypted"; DWORD encryptedLen = wcslen(toBeEncrypted)*sizeof(wchar_t); BYTE encrypted[200]; /* buffer should be larger than toBeEncrypted to have room for IV and padding */ /* Copy plaintext (without NUL terminator) into byte buffer */ memcpy(encrypted, toBeEncrypted, encryptedLen); /* Try to get a context with and without a new key set */ if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_AES, 0)) { if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET)) { printLine("Error in acquiring cryptographic context"); exit(1); } } /* Create Hash handle */ if(!CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash)) { printLine("Error in creating hash"); exit(1); } /* Hash the cryptoKey */ if(!CryptHashData(hHash, (BYTE *) cryptoKey, wcslen(cryptoKey)*sizeof(wchar_t), 0)) { printLine("Error in hashing cryptoKey"); exit(1); } /* Derive an AES key from the Hashed cryptoKey */ if(!CryptDeriveKey(hCryptProv, CALG_AES_256, hHash, 0, &hKey)) { printLine("Error in CryptDeriveKey"); exit(1); } /* POTENTIAL FLAW: Possibly using a hardcoded crypto key */ /* Use the derived key to encrypt something */ if(!CryptEncrypt(hKey, (HCRYPTHASH)NULL, 1, 0, encrypted, &encryptedLen, sizeof(encrypted))) { printLine("Error in CryptEncrypt"); exit(1); } /* use encrypted block */ printBytesLine(encrypted, encryptedLen); if (hKey) { CryptDestroyKey(hKey); } if (hHash) { CryptDestroyHash(hHash); } if (hCryptProv) { CryptReleaseContext(hCryptProv, 0); } } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67b.c Label Definition File: CWE321_Hard_Coded_Cryptographic_Key__w32.label.xml Template File: sources-sink-67b.tmpl.c */ /* * @description * CWE: 321 Use of Hard-coded Cryptographic Key * BadSource: Copy a hardcoded value into cryptoKey * GoodSource: Read cryptoKey from the console * Sinks: * BadSink : Hash cryptoKey and use the value to encrypt a string * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files * * */ #include "std_testcase.h" #define CRYPTO_KEY L"Hardcoded" #include <windows.h> #include <wincrypt.h> /* Link with the Advapi32.lib file for Crypt* functions */ #pragma comment (lib, "Advapi32") typedef struct _CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67_structType { wchar_t * structFirst; } CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67_structType; #ifndef OMITBAD void CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67b_badSink(CWE321_Hard_Coded_Cryptographic_Key__w32_wchar_t_67_structType myStruct) { wchar_t * cryptoKey = myStruct.structFirst; { HCRYPTPROV hCryptProv; HCRYPTKEY hKey; HCRYPTHASH hHash; wchar_t toBeEncrypted[] = L"String to be encrypted"; DWORD encryptedLen = wcslen(toBeEncrypted)*sizeof(wchar_t); BYTE encrypted[200]; /* buffer should be larger than toBeEncrypted to have room for IV and padding */ /* Copy plaintext (without NUL terminator) into byte buffer */ memcpy(encrypted, toBeEncrypted, encryptedLen); /* Try to get a context with and without a new key set */ if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_AES, 0)) { if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET)) { printLine("Error in acquiring cryptographic context"); exit(1); } } /* Create Hash handle */ if(!CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash)) { printLine("Error in creating hash"); exit(1); } /* Hash the cryptoKey */ if(!CryptHashData(hHash, (BYTE *) cryptoKey, wcslen(cryptoKey)*sizeof(wchar_t), 0)) { printLine("Error in hashing cryptoKey"); exit(1); } /* Derive an AES key from the Hashed cryptoKey */ if(!CryptDeriveKey(hCryptProv, CALG_AES_256, hHash, 0, &hKey)) { printLine("Error in CryptDeriveKey"); exit(1); } /* POTENTIAL FLAW: Possibly using a hardcoded crypto key */ /* Use the derived key to encrypt something */ if(!CryptEncrypt(hKey, (HCRYPTHASH)NULL, 1, 0, encrypted, &encryptedLen, sizeof(encrypted))) { printLine("Error in CryptEncrypt"); exit(1); } /* use encrypted block */ printBytesLine(encrypted, encryptedLen); if (hKey) { CryptDestroyKey(hKey); } if (hHash) { CryptDestroyHash(hHash); } if (hCryptProv) { CryptReleaseContext(hCryptProv, 0); } } } #endif /* OMITBAD */
84,584
testcases/CWE191_Integer_Underflow/s01/CWE191_Integer_Underflow__int64_t_min_multiply_82a.cpp
150
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int64_t_min_multiply_82a.cpp Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-82a.tmpl.cpp */ /* * @description * CWE: 191 Integer Underflow * BadSource: min Set data to the min value for int64_t * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: multiply * GoodSink: Ensure there will not be an underflow before multiplying data by 2 * BadSink : If data is negative, multiply by 2, which can cause an underflow * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer * * */ #include "std_testcase.h" #include "CWE191_Integer_Underflow__int64_t_min_multiply_82.h" namespace CWE191_Integer_Underflow__int64_t_min_multiply_82 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int64_t_min_multiply_82a.cpp Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-82a.tmpl.cpp */ /* * @description * CWE: 191 Integer Underflow * BadSource: min Set data to the min value for int64_t * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: multiply * GoodSink: Ensure there will not be an underflow before multiplying data by 2 * BadSink : If data is negative, multiply by 2, which can cause an underflow * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer * * */ #include "std_testcase.h" #include "CWE191_Integer_Underflow__int64_t_min_multiply_82.h" namespace CWE191_Integer_Underflow__int64_t_min_multiply_82 { #ifndef OMITBAD void bad() { int64_t data; data = 0LL; /* POTENTIAL FLAW: Use the minimum size of the data type */ data = LLONG_MIN; CWE191_Integer_Underflow__int64_t_min_multiply_82_base* baseObject = new CWE191_Integer_Underflow__int64_t_min_multiply_82_bad; baseObject->action(data); delete baseObject; } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
46,713
testcases/CWE124_Buffer_Underwrite/s03/CWE124_Buffer_Underwrite__new_char_memcpy_54a.cpp
40
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE124_Buffer_Underwrite__new_char_memcpy_54a.cpp Label Definition File: CWE124_Buffer_Underwrite__new.label.xml Template File: sources-sink-54a.tmpl.cpp */ /* * @description * CWE: 124 Buffer Underwrite * BadSource: Set data pointer to before the allocated memory buffer * GoodSource: Set data pointer to the allocated memory buffer * Sink: memcpy * BadSink : Copy string to data using memcpy * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE124_Buffer_Underwrite__new_char_memcpy_54 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE124_Buffer_Underwrite__new_char_memcpy_54a.cpp Label Definition File: CWE124_Buffer_Underwrite__new.label.xml Template File: sources-sink-54a.tmpl.cpp */ /* * @description * CWE: 124 Buffer Underwrite * BadSource: Set data pointer to before the allocated memory buffer * GoodSource: Set data pointer to the allocated memory buffer * Sink: memcpy * BadSink : Copy string to data using memcpy * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE124_Buffer_Underwrite__new_char_memcpy_54 { #ifndef OMITBAD /* bad function declaration */ void badSink_b(char * data); void bad() { char * data; data = NULL; { char * dataBuffer = new char[100]; memset(dataBuffer, 'A', 100-1); dataBuffer[100-1] = '\0'; /* FLAW: Set data pointer to before the allocated memory buffer */ data = dataBuffer - 8; } badSink_b(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
28,194
testcases/CWE126_Buffer_Overread/s01/CWE126_Buffer_Overread__CWE129_rand_52c.c
76
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__CWE129_rand_52c.c Label Definition File: CWE126_Buffer_Overread__CWE129.label.xml Template File: sources-sinks-52c.tmpl.c */ /* * @description * CWE: 126 Buffer Overread * BadSource: rand Set data to result of rand(), which may be zero * GoodSource: Larger than zero but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking the upper bound * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE126_Buffer_Overread__CWE129_rand_52c_goodG2BSink(int data) { { int buffer[10] = { 0 }; /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound * This check does not check the upper bounds of the array index */ if (data >= 0) { printIntLine(buffer[data]); } else { printLine("ERROR: Array index is negative"); } } } /* goodB2G uses the BadSource with the GoodSink */ void CWE126_Buffer_Overread__CWE129_rand_52c_goodB2GSink(int data) { { int buffer[10] = { 0 }; /* FIX: Properly validate the array index and prevent a buffer overread */ if (data >= 0 && data < (10)) { printIntLine(buffer[data]); } else { printLine("ERROR: Array index is out-of-bounds"); } } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE126_Buffer_Overread__CWE129_rand_52c.c Label Definition File: CWE126_Buffer_Overread__CWE129.label.xml Template File: sources-sinks-52c.tmpl.c */ /* * @description * CWE: 126 Buffer Overread * BadSource: rand Set data to result of rand(), which may be zero * GoodSource: Larger than zero but less than 10 * Sinks: * GoodSink: Ensure the array index is valid * BadSink : Improperly check the array index by not checking the upper bound * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #ifndef OMITBAD void CWE126_Buffer_Overread__CWE129_rand_52c_badSink(int data) { { int buffer[10] = { 0 }; /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound * This check does not check the upper bounds of the array index */ if (data >= 0) { printIntLine(buffer[data]); } else { printLine("ERROR: Array index is negative"); } } } #endif /* OMITBAD */
80,713
testcases/CWE23_Relative_Path_Traversal/s04/CWE23_Relative_Path_Traversal__wchar_t_file_ofstream_52c.cpp
83
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE23_Relative_Path_Traversal__wchar_t_file_ofstream_52c.cpp Label Definition File: CWE23_Relative_Path_Traversal.label.xml Template File: sources-sink-52c.tmpl.cpp */ /* * @description * CWE: 23 Relative Path Traversal * BadSource: file Read input from a file * GoodSource: Use a fixed file name * Sink: ofstream * BadSink : Open the file named in data using ofstream::open() * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #ifdef _WIN32 #define BASEPATH L"c:\\temp\\" #else #include <wchar.h> #define BASEPATH L"/tmp/" #endif #ifdef _WIN32 #define FILENAME "C:\\temp\\file.txt" #else #define FILENAME "/tmp/file.txt" #endif #include <fstream> using namespace std; namespace CWE23_Relative_Path_Traversal__wchar_t_file_ofstream_52 { /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void goodG2BSink_c(wchar_t * data) { { ofstream outputFile; /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */ outputFile.open((char *)data); outputFile.close(); } } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE23_Relative_Path_Traversal__wchar_t_file_ofstream_52c.cpp Label Definition File: CWE23_Relative_Path_Traversal.label.xml Template File: sources-sink-52c.tmpl.cpp */ /* * @description * CWE: 23 Relative Path Traversal * BadSource: file Read input from a file * GoodSource: Use a fixed file name * Sink: ofstream * BadSink : Open the file named in data using ofstream::open() * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #ifdef _WIN32 #define BASEPATH L"c:\\temp\\" #else #include <wchar.h> #define BASEPATH L"/tmp/" #endif #ifdef _WIN32 #define FILENAME "C:\\temp\\file.txt" #else #define FILENAME "/tmp/file.txt" #endif #include <fstream> using namespace std; namespace CWE23_Relative_Path_Traversal__wchar_t_file_ofstream_52 { /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */ #ifndef OMITBAD void badSink_c(wchar_t * data) { { ofstream outputFile; /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */ outputFile.open((char *)data); outputFile.close(); } } #endif /* OMITBAD */ } /* close namespace */
52,438
testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_53a.c
171
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE190_Integer_Overflow__int_fscanf_multiply_53a.c Label Definition File: CWE190_Integer_Overflow__int.label.xml Template File: sources-sinks-53a.tmpl.c */ /* * @description * CWE: 190 Integer Overflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (two) * Sinks: multiply * GoodSink: Ensure there will not be an overflow before multiplying data by 2 * BadSink : If data is positive, multiply by 2, which can cause an overflow * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE190_Integer_Overflow__int_fscanf_multiply_53a.c Label Definition File: CWE190_Integer_Overflow__int.label.xml Template File: sources-sinks-53a.tmpl.c */ /* * @description * CWE: 190 Integer Overflow * BadSource: fscanf Read data from the console using fscanf() * GoodSource: Set data to a small, non-zero number (two) * Sinks: multiply * GoodSink: Ensure there will not be an overflow before multiplying data by 2 * BadSink : If data is positive, multiply by 2, which can cause an overflow * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #ifndef OMITBAD /* bad function declaration */ void CWE190_Integer_Overflow__int_fscanf_multiply_53b_badSink(int data); void CWE190_Integer_Overflow__int_fscanf_multiply_53_bad() { int data; /* Initialize data */ data = 0; /* POTENTIAL FLAW: Read data from the console using fscanf() */ fscanf(stdin, "%d", &data); CWE190_Integer_Overflow__int_fscanf_multiply_53b_badSink(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE190_Integer_Overflow__int_fscanf_multiply_53_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
41,709
testcases/CWE134_Uncontrolled_Format_String/s06/CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81a.cpp
145
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81a.cpp Label Definition File: CWE134_Uncontrolled_Format_String.label.xml Template File: sources-sinks-81a.tmpl.cpp */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Copy a fixed string into data * Sinks: printf * GoodSink: wprintf with "%s" as the first argument and data as the second * BadSink : wprintf with only data as an argument * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */ #include "std_testcase.h" #include "CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 namespace CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81a.cpp Label Definition File: CWE134_Uncontrolled_Format_String.label.xml Template File: sources-sinks-81a.tmpl.cpp */ /* * @description * CWE: 134 Uncontrolled Format String * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Copy a fixed string into data * Sinks: printf * GoodSink: wprintf with "%s" as the first argument and data as the second * BadSink : wprintf with only data as an argument * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference * * */ #include "std_testcase.h" #include "CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 namespace CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81 { #ifndef OMITBAD void bad() { wchar_t * data; wchar_t dataBuffer[100] = L""; data = dataBuffer; { #ifdef _WIN32 WSADATA wsaData; int wsaDataInit = 0; #endif int recvResult; struct sockaddr_in service; wchar_t *replace; SOCKET listenSocket = INVALID_SOCKET; SOCKET acceptSocket = INVALID_SOCKET; size_t dataLen = wcslen(data); do { #ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) { break; } wsaDataInit = 1; #endif /* POTENTIAL FLAW: Read data using a listen socket */ listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listenSocket == INVALID_SOCKET) { break; } memset(&service, 0, sizeof(service)); service.sin_family = AF_INET; service.sin_addr.s_addr = INADDR_ANY; service.sin_port = htons(TCP_PORT); if (::bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR) { break; } if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR) { break; } acceptSocket = accept(listenSocket, NULL, NULL); if (acceptSocket == SOCKET_ERROR) { break; } /* Abort on error or the connection was closed */ recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0); if (recvResult == SOCKET_ERROR || recvResult == 0) { break; } /* Append null terminator */ data[dataLen + recvResult / sizeof(wchar_t)] = L'\0'; /* Eliminate CRLF */ replace = wcschr(data, L'\r'); if (replace) { *replace = L'\0'; } replace = wcschr(data, L'\n'); if (replace) { *replace = L'\0'; } } while (0); if (listenSocket != INVALID_SOCKET) { CLOSE_SOCKET(listenSocket); } if (acceptSocket != INVALID_SOCKET) { CLOSE_SOCKET(acceptSocket); } #ifdef _WIN32 if (wsaDataInit) { WSACleanup(); } #endif } const CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81_base& baseObject = CWE134_Uncontrolled_Format_String__wchar_t_listen_socket_printf_81_bad(); baseObject.action(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
85,914
testcases/CWE191_Integer_Underflow/s02/CWE191_Integer_Underflow__int_listen_socket_multiply_45.c
132
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int_listen_socket_multiply_45.c Label Definition File: CWE191_Integer_Underflow__int.label.xml Template File: sources-sinks-45.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: multiply * GoodSink: Ensure there will not be an underflow before multiplying data by 2 * BadSink : If data is negative, multiply by 2, which can cause an underflow * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file * * */ #include "std_testcase.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) static int CWE191_Integer_Underflow__int_listen_socket_multiply_45_badData; static int CWE191_Integer_Underflow__int_listen_socket_multiply_45_goodG2BData; static int CWE191_Integer_Underflow__int_listen_socket_multiply_45_goodB2GData; return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__int_listen_socket_multiply_45.c Label Definition File: CWE191_Integer_Underflow__int.label.xml Template File: sources-sinks-45.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: multiply * GoodSink: Ensure there will not be an underflow before multiplying data by 2 * BadSink : If data is negative, multiply by 2, which can cause an underflow * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file * * */ #include "std_testcase.h" #ifdef _WIN32 #include <winsock2.h> #include <windows.h> #include <direct.h> #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */ #define CLOSE_SOCKET closesocket #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define CLOSE_SOCKET close #define SOCKET int #endif #define TCP_PORT 27015 #define LISTEN_BACKLOG 5 #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) static int CWE191_Integer_Underflow__int_listen_socket_multiply_45_badData; static int CWE191_Integer_Underflow__int_listen_socket_multiply_45_goodG2BData; static int CWE191_Integer_Underflow__int_listen_socket_multiply_45_goodB2GData; #ifndef OMITBAD static void badSink() { int data = CWE191_Integer_Underflow__int_listen_socket_multiply_45_badData; if(data < 0) /* ensure we won't have an overflow */ { /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */ int result = data * 2; printIntLine(result); } } void CWE191_Integer_Underflow__int_listen_socket_multiply_45_bad() { int data; /* Initialize data */ data = 0; { #ifdef _WIN32 WSADATA wsaData; int wsaDataInit = 0; #endif int recvResult; struct sockaddr_in service; SOCKET listenSocket = INVALID_SOCKET; SOCKET acceptSocket = INVALID_SOCKET; char inputBuffer[CHAR_ARRAY_SIZE]; do { #ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) { break; } wsaDataInit = 1; #endif /* POTENTIAL FLAW: Read data using a listen socket */ listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listenSocket == INVALID_SOCKET) { break; } memset(&service, 0, sizeof(service)); service.sin_family = AF_INET; service.sin_addr.s_addr = INADDR_ANY; service.sin_port = htons(TCP_PORT); if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR) { break; } if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR) { break; } acceptSocket = accept(listenSocket, NULL, NULL); if (acceptSocket == SOCKET_ERROR) { break; } /* Abort on error or the connection was closed */ recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0); if (recvResult == SOCKET_ERROR || recvResult == 0) { break; } /* NUL-terminate the string */ inputBuffer[recvResult] = '\0'; /* Convert to int */ data = atoi(inputBuffer); } while (0); if (listenSocket != INVALID_SOCKET) { CLOSE_SOCKET(listenSocket); } if (acceptSocket != INVALID_SOCKET) { CLOSE_SOCKET(acceptSocket); } #ifdef _WIN32 if (wsaDataInit) { WSACleanup(); } #endif } CWE191_Integer_Underflow__int_listen_socket_multiply_45_badData = data; badSink(); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE191_Integer_Underflow__int_listen_socket_multiply_45_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
94,524
testcases/CWE511_Logic_Time_Bomb/CWE511_Logic_Time_Bomb__time_02.c
186
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE511_Logic_Time_Bomb__time_02.c Label Definition File: CWE511_Logic_Time_Bomb.label.xml Template File: point-flaw-02.tmpl.c */ /* * @description * CWE: 511 Logic Time Bomb * Sinks: time * GoodSink: After a certain date, do something harmless * BadSink : After a certain date, do something bad * Flow Variant: 02 Control flow: if(1) and if(0) * * */ #include "std_testcase.h" #ifdef _WIN32 #define UNLINK _unlink #else #include <unistd.h> #define UNLINK unlink #endif #include <time.h> #define TIME_CHECK ((time_t)1199163600) /* Jan 1, 2008 12:00:00 AM */ return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE511_Logic_Time_Bomb__time_02.c Label Definition File: CWE511_Logic_Time_Bomb.label.xml Template File: point-flaw-02.tmpl.c */ /* * @description * CWE: 511 Logic Time Bomb * Sinks: time * GoodSink: After a certain date, do something harmless * BadSink : After a certain date, do something bad * Flow Variant: 02 Control flow: if(1) and if(0) * * */ #include "std_testcase.h" #ifdef _WIN32 #define UNLINK _unlink #else #include <unistd.h> #define UNLINK unlink #endif #include <time.h> #define TIME_CHECK ((time_t)1199163600) /* Jan 1, 2008 12:00:00 AM */ #ifndef OMITBAD void CWE511_Logic_Time_Bomb__time_02_bad() { if(1) { { time_t currentTime; /* FLAW: After a certain date, delete a file */ time(&currentTime); if (currentTime > TIME_CHECK) { UNLINK("important_file.txt"); } } } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE511_Logic_Time_Bomb__time_02_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
88,529
testcases/CWE762_Mismatched_Memory_Management_Routines/s05/CWE762_Mismatched_Memory_Management_Routines__new_array_free_long_72b.cpp
62
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__new_array_free_long_72b.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__new_array_free.label.xml Template File: sources-sinks-72b.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: Allocate data using new [] * GoodSource: Allocate data using malloc() * Sinks: * GoodSink: Deallocate data using delete [] * BadSink : Deallocate data using free() * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files * * */ #include "std_testcase.h" #include <vector> using namespace std; namespace CWE762_Mismatched_Memory_Management_Routines__new_array_free_long_72 { #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void goodG2BSink(vector<long *> dataVector) { long * data = dataVector[2]; /* POTENTIAL FLAW: Deallocate memory using free() - the source memory allocation function may * require a call to delete [] to deallocate the memory */ free(data); } /* goodB2G uses the BadSource with the GoodSink */ void goodB2GSink(vector<long *> dataVector) { long * data = dataVector[2]; /* FIX: Deallocate the memory using delete [] */ delete [] data; } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE762_Mismatched_Memory_Management_Routines__new_array_free_long_72b.cpp Label Definition File: CWE762_Mismatched_Memory_Management_Routines__new_array_free.label.xml Template File: sources-sinks-72b.tmpl.cpp */ /* * @description * CWE: 762 Mismatched Memory Management Routines * BadSource: Allocate data using new [] * GoodSource: Allocate data using malloc() * Sinks: * GoodSink: Deallocate data using delete [] * BadSink : Deallocate data using free() * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files * * */ #include "std_testcase.h" #include <vector> using namespace std; namespace CWE762_Mismatched_Memory_Management_Routines__new_array_free_long_72 { #ifndef OMITBAD void badSink(vector<long *> dataVector) { /* copy data out of dataVector */ long * data = dataVector[2]; /* POTENTIAL FLAW: Deallocate memory using free() - the source memory allocation function may * require a call to delete [] to deallocate the memory */ free(data); } #endif /* OMITBAD */ } /* close namespace */
13,416
testcases/CWE590_Free_Memory_Not_on_Heap/s03/CWE590_Free_Memory_Not_on_Heap__delete_int64_t_placement_new_53a.cpp
147
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE590_Free_Memory_Not_on_Heap__delete_int64_t_placement_new_53a.cpp Label Definition File: CWE590_Free_Memory_Not_on_Heap__delete.pointer.label.xml Template File: sources-sink-53a.tmpl.cpp */ /* * @description * CWE: 590 Free Memory Not on Heap * BadSource: placement_new Data buffer is declared on the stack * GoodSource: Allocate memory on the heap * Sink: * BadSink : Print then free data * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE590_Free_Memory_Not_on_Heap__delete_int64_t_placement_new_53 { return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE590_Free_Memory_Not_on_Heap__delete_int64_t_placement_new_53a.cpp Label Definition File: CWE590_Free_Memory_Not_on_Heap__delete.pointer.label.xml Template File: sources-sink-53a.tmpl.cpp */ /* * @description * CWE: 590 Free Memory Not on Heap * BadSource: placement_new Data buffer is declared on the stack * GoodSource: Allocate memory on the heap * Sink: * BadSink : Print then free data * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files * * */ #include "std_testcase.h" #include <wchar.h> namespace CWE590_Free_Memory_Not_on_Heap__delete_int64_t_placement_new_53 { #ifndef OMITBAD /* bad function declaration */ void badSink_b(int64_t * data); void bad() { int64_t * data; data = NULL; /* Initialize data */ { /* FLAW: data is allocated on the stack and deallocated in the BadSink */ char buffer[sizeof(int64_t)]; int64_t * dataBuffer = new(buffer) int64_t; *dataBuffer = 5LL; data = dataBuffer; } badSink_b(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
64,788
testcases/CWE195_Signed_to_Unsigned_Conversion_Error/s01/CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memmove_51a.c
100
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memmove_51a.c Label Definition File: CWE195_Signed_to_Unsigned_Conversion_Error.label.xml Template File: sources-sink-51a.tmpl.c */ /* * @description * CWE: 195 Signed to Unsigned Conversion Error * BadSource: fgets Read data from the console using fgets() * GoodSource: Positive integer * Sink: memmove * BadSink : Copy strings using memmove() with the length of data * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files * * */ #include "std_testcase.h" #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memmove_51a.c Label Definition File: CWE195_Signed_to_Unsigned_Conversion_Error.label.xml Template File: sources-sink-51a.tmpl.c */ /* * @description * CWE: 195 Signed to Unsigned Conversion Error * BadSource: fgets Read data from the console using fgets() * GoodSource: Positive integer * Sink: memmove * BadSink : Copy strings using memmove() with the length of data * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files * * */ #include "std_testcase.h" #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2) #ifndef OMITBAD /* bad function declaration */ void CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memmove_51b_badSink(int data); void CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memmove_51_bad() { int data; /* Initialize data */ data = -1; { char inputBuffer[CHAR_ARRAY_SIZE] = ""; /* POTENTIAL FLAW: Read data from the console using fgets() */ if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL) { /* Convert to int */ data = atoi(inputBuffer); } else { printLine("fgets() failed."); } } CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memmove_51b_badSink(data); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memmove_51_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
16,492
testcases/CWE121_Stack_Based_Buffer_Overflow/s01/CWE121_Stack_Based_Buffer_Overflow__CWE193_char_alloca_cpy_66a.c
154
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE193_char_alloca_cpy_66a.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml Template File: sources-sink-66a.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Point data to a buffer that does not have space for a NULL terminator * GoodSource: Point data to a buffer that includes space for a NULL terminator * Sinks: cpy * BadSink : Copy string to data using strcpy() * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE121_Stack_Based_Buffer_Overflow__CWE193_char_alloca_cpy_66a.c Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml Template File: sources-sink-66a.tmpl.c */ /* * @description * CWE: 121 Stack Based Buffer Overflow * BadSource: Point data to a buffer that does not have space for a NULL terminator * GoodSource: Point data to a buffer that includes space for a NULL terminator * Sinks: cpy * BadSink : Copy string to data using strcpy() * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files * * */ #include "std_testcase.h" #ifndef _WIN32 #include <wchar.h> #endif /* MAINTENANCE NOTE: The length of this string should equal the 10 */ #define SRC_STRING "AAAAAAAAAA" #ifndef OMITBAD /* bad function declaration */ void CWE121_Stack_Based_Buffer_Overflow__CWE193_char_alloca_cpy_66b_badSink(char * dataArray[]); void CWE121_Stack_Based_Buffer_Overflow__CWE193_char_alloca_cpy_66_bad() { char * data; char * dataArray[5]; char * dataBadBuffer = (char *)ALLOCA((10)*sizeof(char)); char * dataGoodBuffer = (char *)ALLOCA((10+1)*sizeof(char)); /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing * string copies in the sinks */ data = dataBadBuffer; data[0] = '\0'; /* null terminate */ /* put data in array */ dataArray[2] = data; CWE121_Stack_Based_Buffer_Overflow__CWE193_char_alloca_cpy_66b_badSink(dataArray); } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE121_Stack_Based_Buffer_Overflow__CWE193_char_alloca_cpy_66_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
65,931
testcases/CWE195_Signed_to_Unsigned_Conversion_Error/s02/CWE195_Signed_to_Unsigned_Conversion_Error__negative_memmove_31.c
193
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE195_Signed_to_Unsigned_Conversion_Error__negative_memmove_31.c Label Definition File: CWE195_Signed_to_Unsigned_Conversion_Error.label.xml Template File: sources-sink-31.tmpl.c */ /* * @description * CWE: 195 Signed to Unsigned Conversion Error * BadSource: negative Set data to a fixed negative number * GoodSource: Positive integer * Sinks: memmove * BadSink : Copy strings using memmove() with the length of data * Flow Variant: 31 Data flow using a copy of data within the same function * * */ #include "std_testcase.h" return 0; } #endif
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE195_Signed_to_Unsigned_Conversion_Error__negative_memmove_31.c Label Definition File: CWE195_Signed_to_Unsigned_Conversion_Error.label.xml Template File: sources-sink-31.tmpl.c */ /* * @description * CWE: 195 Signed to Unsigned Conversion Error * BadSource: negative Set data to a fixed negative number * GoodSource: Positive integer * Sinks: memmove * BadSink : Copy strings using memmove() with the length of data * Flow Variant: 31 Data flow using a copy of data within the same function * * */ #include "std_testcase.h" #ifndef OMITBAD void CWE195_Signed_to_Unsigned_Conversion_Error__negative_memmove_31_bad() { int data; /* Initialize data */ data = -1; /* FLAW: Use a negative number */ data = -1; { int dataCopy = data; int data = dataCopy; { char source[100]; char dest[100] = ""; memset(source, 'A', 100-1); source[100-1] = '\0'; if (data < 100) { /* POTENTIAL FLAW: data is interpreted as an unsigned int - if its value is negative, * the sign conversion could result in a very large number */ memmove(dest, source, data); dest[data] = '\0'; /* NULL terminate */ } printLine(dest); } } } #endif /* OMITBAD */ #ifndef OMITBAD printLine("Calling bad()..."); CWE195_Signed_to_Unsigned_Conversion_Error__negative_memmove_31_bad(); printLine("Finished bad()"); #endif /* OMITBAD */ return 0; } #endif
3,612
testcases/CWE78_OS_Command_Injection/s04/CWE78_OS_Command_Injection__char_listen_socket_w32_execvp_73b.cpp
52
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__char_listen_socket_w32_execvp_73b.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-73b.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Fixed string * Sinks: w32_execvp * BadSink : execute command with execvp * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files * * */ #include "std_testcase.h" #include <list> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH "%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT "cmd.exe" #define COMMAND_ARG1 "/c" #define COMMAND_ARG2 "dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH "/bin/sh" #define COMMAND_INT "sh" #define COMMAND_ARG1 "-c" #define COMMAND_ARG2 "ls " #define COMMAND_ARG3 data #endif #include <process.h> #define EXECVP _execvp using namespace std; namespace CWE78_OS_Command_Injection__char_listen_socket_w32_execvp_73 { #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void goodG2BSink(list<char *> dataList) { char * data = dataList.back(); { char *args[] = {COMMAND_INT_PATH, COMMAND_ARG1, COMMAND_ARG3, NULL}; /* execvp - searches for the location of the command among * the directories specified by the PATH environment variable */ /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */ EXECVP(COMMAND_INT, args); } } #endif /* OMITGOOD */ } /* close namespace */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE78_OS_Command_Injection__char_listen_socket_w32_execvp_73b.cpp Label Definition File: CWE78_OS_Command_Injection.strings.label.xml Template File: sources-sink-73b.tmpl.cpp */ /* * @description * CWE: 78 OS Command Injection * BadSource: listen_socket Read data using a listen socket (server side) * GoodSource: Fixed string * Sinks: w32_execvp * BadSink : execute command with execvp * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files * * */ #include "std_testcase.h" #include <list> #include <wchar.h> #ifdef _WIN32 #define COMMAND_INT_PATH "%WINDIR%\\system32\\cmd.exe" #define COMMAND_INT "cmd.exe" #define COMMAND_ARG1 "/c" #define COMMAND_ARG2 "dir " #define COMMAND_ARG3 data #else /* NOT _WIN32 */ #include <unistd.h> #define COMMAND_INT_PATH "/bin/sh" #define COMMAND_INT "sh" #define COMMAND_ARG1 "-c" #define COMMAND_ARG2 "ls " #define COMMAND_ARG3 data #endif #include <process.h> #define EXECVP _execvp using namespace std; namespace CWE78_OS_Command_Injection__char_listen_socket_w32_execvp_73 { #ifndef OMITBAD void badSink(list<char *> dataList) { /* copy data out of dataList */ char * data = dataList.back(); { char *args[] = {COMMAND_INT_PATH, COMMAND_ARG1, COMMAND_ARG3, NULL}; /* execvp - searches for the location of the command among * the directories specified by the PATH environment variable */ /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */ EXECVP(COMMAND_INT, args); } } #endif /* OMITBAD */ } /* close namespace */
85,174
testcases/CWE191_Integer_Underflow/s04/CWE191_Integer_Underflow__unsigned_int_min_postdec_52c.c
95
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__unsigned_int_min_postdec_52c.c Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-52c.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: min Set data to the min value for unsigned int * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: decrement * GoodSink: Ensure there will not be an underflow before decrementing data * BadSink : Decrement data, which can cause an Underflow * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #ifndef OMITGOOD /* goodG2B uses the GoodSource with the BadSink */ void CWE191_Integer_Underflow__unsigned_int_min_postdec_52c_goodG2BSink(unsigned int data) { { /* POTENTIAL FLAW: Decrementing data could cause an underflow */ data--; unsigned int result = data; printUnsignedLine(result); } } /* goodB2G uses the BadSource with the GoodSink */ void CWE191_Integer_Underflow__unsigned_int_min_postdec_52c_goodB2GSink(unsigned int data) { /* FIX: Add a check to prevent an underflow from occurring */ if (data > 0) { data--; unsigned int result = data; printUnsignedLine(result); } else { printLine("data value is too large to perform arithmetic safely."); } } #endif /* OMITGOOD */
/* TEMPLATE GENERATED TESTCASE FILE Filename: CWE191_Integer_Underflow__unsigned_int_min_postdec_52c.c Label Definition File: CWE191_Integer_Underflow.label.xml Template File: sources-sinks-52c.tmpl.c */ /* * @description * CWE: 191 Integer Underflow * BadSource: min Set data to the min value for unsigned int * GoodSource: Set data to a small, non-zero number (negative two) * Sinks: decrement * GoodSink: Ensure there will not be an underflow before decrementing data * BadSink : Decrement data, which can cause an Underflow * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files * * */ #include "std_testcase.h" #ifndef OMITBAD void CWE191_Integer_Underflow__unsigned_int_min_postdec_52c_badSink(unsigned int data) { { /* POTENTIAL FLAW: Decrementing data could cause an underflow */ data--; unsigned int result = data; printUnsignedLine(result); } } #endif /* OMITBAD */

Dataset Card for the Juliet Test Suite 1.3

Dataset Summary

This Datasets contains all test cases from the NIST's Juliet test suite for the C and C++ programming languages. The dataset contains a benign and a defective implementation of each sample, which have been extracting by means of the OMITGOOD and OMITBAD preprocessor macros of the Juliet test suite.

Supported Tasks and Leaderboards

Software defect prediction, code clone detection.

Languages

The C and C++ programming languages.

Dataset Structure

Data Instances

Data Fields

index name type description
0 index int The index of each sample in the dataset.
1 filename str The path to the test case including the file name.
2 class int The class of the defect, i.e., the collection by CWE number from which the sample was taken.
3 good str The code of the benign implementation.
4 bad str The code of the defective implementation.

Data Splits

type size
train 80706 cases
test 20177 cases

Dataset Creation

Curation Rationale

Source Data

https://samate.nist.gov/SARD/test-suites/112

Initial Data Collection and Normalization

Who are the source language producers?

Annotations

Annotation process

Who are the annotators?

Personal and Sensitive Information

Considerations for Using the Data

Social Impact of Dataset

Discussion of Biases

Other Known Limitations

The Juliet test suite is a synthetic dataset, meaning that all samples have been manually crafted. Therefore they are not entirely representative of actual software defects found in the wild. A classifier trained on these samples may suffer from decreased predictive performance, leading to gross misclassifactions. Critical software defects may therefore be overlooked, when such model is applied in a realistic environment.

Additional Information

Dataset Curators

https://github.com/lorenz9314/

Licensing Information

Citation Information

Contributions

Downloads last month
7
Edit dataset card