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

CWE36_Absolute_Path_Traversal__char_connect_socket_fopen_51a.cpp 4.9 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE36_Absolute_Path_Traversal__char_connect_socket_fopen_51a.cpp
  3. Label Definition File: CWE36_Absolute_Path_Traversal.label.xml
  4. Template File: sources-sink-51a.tmpl.cpp
  5. */
  6. /*
  7. * @description
  8. * CWE: 36 Absolute Path Traversal
  9. * BadSource: connect_socket Read data using a connect socket (client side)
  10. * GoodSource: Full path and file name
  11. * Sink: fopen
  12. * BadSink : Open the file named in data using fopen()
  13. * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files
  14. *
  15. * */
  16. #include "std_testcase.h"
  17. #ifndef _WIN32
  18. #include <wchar.h>
  19. #endif
  20. #ifdef _WIN32
  21. #include <winsock2.h>
  22. #include <windows.h>
  23. #include <direct.h>
  24. #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  25. #define CLOSE_SOCKET closesocket
  26. #else /* NOT _WIN32 */
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <unistd.h>
  32. #define INVALID_SOCKET -1
  33. #define SOCKET_ERROR -1
  34. #define CLOSE_SOCKET close
  35. #define SOCKET int
  36. #endif
  37. #define TCP_PORT 27015
  38. #define IP_ADDRESS "127.0.0.1"
  39. #ifdef _WIN32
  40. #define FOPEN fopen
  41. #else
  42. #define FOPEN fopen
  43. #endif
  44. namespace CWE36_Absolute_Path_Traversal__char_connect_socket_fopen_51
  45. {
  46. #ifndef OMITBAD
  47. /* bad function declaration */
  48. void badSink(char * data);
  49. void bad()
  50. {
  51. char * data;
  52. char dataBuffer[FILENAME_MAX] = "";
  53. data = dataBuffer;
  54. {
  55. #ifdef _WIN32
  56. WSADATA wsaData;
  57. int wsaDataInit = 0;
  58. #endif
  59. int recvResult;
  60. struct sockaddr_in service;
  61. char *replace;
  62. SOCKET connectSocket = INVALID_SOCKET;
  63. size_t dataLen = strlen(data);
  64. do
  65. {
  66. #ifdef _WIN32
  67. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  68. {
  69. break;
  70. }
  71. wsaDataInit = 1;
  72. #endif
  73. /* POTENTIAL FLAW: Read data using a connect socket */
  74. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  75. if (connectSocket == INVALID_SOCKET)
  76. {
  77. break;
  78. }
  79. memset(&service, 0, sizeof(service));
  80. service.sin_family = AF_INET;
  81. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  82. service.sin_port = htons(TCP_PORT);
  83. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  84. {
  85. break;
  86. }
  87. /* Abort on error or the connection was closed, make sure to recv one
  88. * less char than is in the recv_buf in order to append a terminator */
  89. /* Abort on error or the connection was closed */
  90. recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(char) * (FILENAME_MAX - dataLen - 1), 0);
  91. if (recvResult == SOCKET_ERROR || recvResult == 0)
  92. {
  93. break;
  94. }
  95. /* Append null terminator */
  96. data[dataLen + recvResult / sizeof(char)] = '\0';
  97. /* Eliminate CRLF */
  98. replace = strchr(data, '\r');
  99. if (replace)
  100. {
  101. *replace = '\0';
  102. }
  103. replace = strchr(data, '\n');
  104. if (replace)
  105. {
  106. *replace = '\0';
  107. }
  108. }
  109. while (0);
  110. if (connectSocket != INVALID_SOCKET)
  111. {
  112. CLOSE_SOCKET(connectSocket);
  113. }
  114. #ifdef _WIN32
  115. if (wsaDataInit)
  116. {
  117. WSACleanup();
  118. }
  119. #endif
  120. }
  121. badSink(data);
  122. }
  123. #endif /* OMITBAD */
  124. #ifndef OMITGOOD
  125. /* good function declarations */
  126. void goodG2BSink(char * data);
  127. /* goodG2B uses the GoodSource with the BadSink */
  128. static void goodG2B()
  129. {
  130. char * data;
  131. char dataBuffer[FILENAME_MAX] = "";
  132. data = dataBuffer;
  133. #ifdef _WIN32
  134. /* FIX: Use a fixed, full path and file name */
  135. strcat(data, "c:\\temp\\file.txt");
  136. #else
  137. /* FIX: Use a fixed, full path and file name */
  138. strcat(data, "/tmp/file.txt");
  139. #endif
  140. goodG2BSink(data);
  141. }
  142. void good()
  143. {
  144. goodG2B();
  145. }
  146. #endif /* OMITGOOD */
  147. } /* close namespace */
  148. /* Below is the main(). It is only used when building this testcase on
  149. its own for testing or for building a binary to use in testing binary
  150. analysis tools. It is not used when compiling all the testcases as one
  151. application, which is how source code analysis tools are tested. */
  152. #ifdef INCLUDEMAIN
  153. using namespace CWE36_Absolute_Path_Traversal__char_connect_socket_fopen_51; /* so that we can use good and bad easily */
  154. int main(int argc, char * argv[])
  155. {
  156. /* seed randomness */
  157. srand( (unsigned)time(NULL) );
  158. #ifndef OMITGOOD
  159. printLine("Calling good()...");
  160. good();
  161. printLine("Finished good()");
  162. #endif /* OMITGOOD */
  163. #ifndef OMITBAD
  164. printLine("Calling bad()...");
  165. bad();
  166. printLine("Finished bad()");
  167. #endif /* OMITBAD */
  168. return 0;
  169. }
  170. #endif

No Description

Contributors (1)