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.

CWE127_Buffer_Underread__CWE839_connect_socket_01.c 7.2 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE127_Buffer_Underread__CWE839_connect_socket_01.c
  3. Label Definition File: CWE127_Buffer_Underread__CWE839.label.xml
  4. Template File: sources-sinks-01.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 127 Buffer Underread
  9. * BadSource: connect_socket Read data using a connect socket (client side)
  10. * GoodSource: Non-negative but less than 10
  11. * Sinks:
  12. * GoodSink: Ensure the array index is valid
  13. * BadSink : Improperly check the array index by not checking to see if the value is negative
  14. * Flow Variant: 01 Baseline
  15. *
  16. * */
  17. #include "std_testcase.h"
  18. #ifdef _WIN32
  19. #include <winsock2.h>
  20. #include <windows.h>
  21. #include <direct.h>
  22. #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  23. #define CLOSE_SOCKET closesocket
  24. #else /* NOT _WIN32 */
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <unistd.h>
  30. #define INVALID_SOCKET -1
  31. #define SOCKET_ERROR -1
  32. #define CLOSE_SOCKET close
  33. #define SOCKET int
  34. #endif
  35. #define TCP_PORT 27015
  36. #define IP_ADDRESS "127.0.0.1"
  37. #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  38. #ifndef OMITBAD
  39. void CWE127_Buffer_Underread__CWE839_connect_socket_01_bad()
  40. {
  41. int data;
  42. /* Initialize data */
  43. data = -1;
  44. {
  45. #ifdef _WIN32
  46. WSADATA wsaData;
  47. int wsaDataInit = 0;
  48. #endif
  49. int recvResult;
  50. struct sockaddr_in service;
  51. SOCKET connectSocket = INVALID_SOCKET;
  52. char inputBuffer[CHAR_ARRAY_SIZE];
  53. do
  54. {
  55. #ifdef _WIN32
  56. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  57. {
  58. break;
  59. }
  60. wsaDataInit = 1;
  61. #endif
  62. /* POTENTIAL FLAW: Read data using a connect socket */
  63. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  64. if (connectSocket == INVALID_SOCKET)
  65. {
  66. break;
  67. }
  68. memset(&service, 0, sizeof(service));
  69. service.sin_family = AF_INET;
  70. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  71. service.sin_port = htons(TCP_PORT);
  72. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  73. {
  74. break;
  75. }
  76. /* Abort on error or the connection was closed, make sure to recv one
  77. * less char than is in the recv_buf in order to append a terminator */
  78. recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);
  79. if (recvResult == SOCKET_ERROR || recvResult == 0)
  80. {
  81. break;
  82. }
  83. /* NUL-terminate the string */
  84. inputBuffer[recvResult] = '\0';
  85. /* Convert to int */
  86. data = atoi(inputBuffer);
  87. }
  88. while (0);
  89. if (connectSocket != INVALID_SOCKET)
  90. {
  91. CLOSE_SOCKET(connectSocket);
  92. }
  93. #ifdef _WIN32
  94. if (wsaDataInit)
  95. {
  96. WSACleanup();
  97. }
  98. #endif
  99. }
  100. {
  101. int buffer[10] = { 0 };
  102. /* POTENTIAL FLAW: Attempt to access a negative index of the array
  103. * This check does not check to see if the array index is negative */
  104. if (data < 10)
  105. {
  106. printIntLine(buffer[data]);
  107. }
  108. else
  109. {
  110. printLine("ERROR: Array index is too big.");
  111. }
  112. }
  113. }
  114. #endif /* OMITBAD */
  115. #ifndef OMITGOOD
  116. /* goodG2B uses the GoodSource with the BadSink */
  117. static void goodG2B()
  118. {
  119. int data;
  120. /* Initialize data */
  121. data = -1;
  122. /* FIX: Use a value greater than 0, but less than 10 to avoid attempting to
  123. * access an index of the array in the sink that is out-of-bounds */
  124. data = 7;
  125. {
  126. int buffer[10] = { 0 };
  127. /* POTENTIAL FLAW: Attempt to access a negative index of the array
  128. * This check does not check to see if the array index is negative */
  129. if (data < 10)
  130. {
  131. printIntLine(buffer[data]);
  132. }
  133. else
  134. {
  135. printLine("ERROR: Array index is too big.");
  136. }
  137. }
  138. }
  139. /* goodB2G uses the BadSource with the GoodSink */
  140. static void goodB2G()
  141. {
  142. int data;
  143. /* Initialize data */
  144. data = -1;
  145. {
  146. #ifdef _WIN32
  147. WSADATA wsaData;
  148. int wsaDataInit = 0;
  149. #endif
  150. int recvResult;
  151. struct sockaddr_in service;
  152. SOCKET connectSocket = INVALID_SOCKET;
  153. char inputBuffer[CHAR_ARRAY_SIZE];
  154. do
  155. {
  156. #ifdef _WIN32
  157. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  158. {
  159. break;
  160. }
  161. wsaDataInit = 1;
  162. #endif
  163. /* POTENTIAL FLAW: Read data using a connect socket */
  164. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  165. if (connectSocket == INVALID_SOCKET)
  166. {
  167. break;
  168. }
  169. memset(&service, 0, sizeof(service));
  170. service.sin_family = AF_INET;
  171. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  172. service.sin_port = htons(TCP_PORT);
  173. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  174. {
  175. break;
  176. }
  177. /* Abort on error or the connection was closed, make sure to recv one
  178. * less char than is in the recv_buf in order to append a terminator */
  179. recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);
  180. if (recvResult == SOCKET_ERROR || recvResult == 0)
  181. {
  182. break;
  183. }
  184. /* NUL-terminate the string */
  185. inputBuffer[recvResult] = '\0';
  186. /* Convert to int */
  187. data = atoi(inputBuffer);
  188. }
  189. while (0);
  190. if (connectSocket != INVALID_SOCKET)
  191. {
  192. CLOSE_SOCKET(connectSocket);
  193. }
  194. #ifdef _WIN32
  195. if (wsaDataInit)
  196. {
  197. WSACleanup();
  198. }
  199. #endif
  200. }
  201. {
  202. int buffer[10] = { 0 };
  203. /* FIX: Properly validate the array index and prevent a buffer underread */
  204. if (data >= 0 && data < (10))
  205. {
  206. printIntLine(buffer[data]);
  207. }
  208. else
  209. {
  210. printLine("ERROR: Array index is out-of-bounds");
  211. }
  212. }
  213. }
  214. void CWE127_Buffer_Underread__CWE839_connect_socket_01_good()
  215. {
  216. goodG2B();
  217. goodB2G();
  218. }
  219. #endif /* OMITGOOD */
  220. /* Below is the main(). It is only used when building this testcase on
  221. its own for testing or for building a binary to use in testing binary
  222. analysis tools. It is not used when compiling all the testcases as one
  223. application, which is how source code analysis tools are tested. */
  224. #ifdef INCLUDEMAIN
  225. int main(int argc, char * argv[])
  226. {
  227. /* seed randomness */
  228. srand( (unsigned)time(NULL) );
  229. #ifndef OMITGOOD
  230. printLine("Calling good()...");
  231. CWE127_Buffer_Underread__CWE839_connect_socket_01_good();
  232. printLine("Finished good()");
  233. #endif /* OMITGOOD */
  234. #ifndef OMITBAD
  235. printLine("Calling bad()...");
  236. CWE127_Buffer_Underread__CWE839_connect_socket_01_bad();
  237. printLine("Finished bad()");
  238. #endif /* OMITBAD */
  239. return 0;
  240. }
  241. #endif

No Description

Contributors (1)