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.

CWE126_Buffer_Overread__CWE129_connect_socket_02.c 12 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE126_Buffer_Overread__CWE129_connect_socket_02.c
  3. Label Definition File: CWE126_Buffer_Overread__CWE129.label.xml
  4. Template File: sources-sinks-02.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 126 Buffer Overread
  9. * BadSource: connect_socket Read data using a connect socket (client side)
  10. * GoodSource: Larger than zero but less than 10
  11. * Sinks:
  12. * GoodSink: Ensure the array index is valid
  13. * BadSink : Improperly check the array index by not checking the upper bound
  14. * Flow Variant: 02 Control flow: if(1) and if(0)
  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 CWE126_Buffer_Overread__CWE129_connect_socket_02_bad()
  40. {
  41. int data;
  42. /* Initialize data */
  43. data = -1;
  44. if(1)
  45. {
  46. {
  47. #ifdef _WIN32
  48. WSADATA wsaData;
  49. int wsaDataInit = 0;
  50. #endif
  51. int recvResult;
  52. struct sockaddr_in service;
  53. SOCKET connectSocket = INVALID_SOCKET;
  54. char inputBuffer[CHAR_ARRAY_SIZE];
  55. do
  56. {
  57. #ifdef _WIN32
  58. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  59. {
  60. break;
  61. }
  62. wsaDataInit = 1;
  63. #endif
  64. /* POTENTIAL FLAW: Read data using a connect socket */
  65. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  66. if (connectSocket == INVALID_SOCKET)
  67. {
  68. break;
  69. }
  70. memset(&service, 0, sizeof(service));
  71. service.sin_family = AF_INET;
  72. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  73. service.sin_port = htons(TCP_PORT);
  74. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  75. {
  76. break;
  77. }
  78. /* Abort on error or the connection was closed, make sure to recv one
  79. * less char than is in the recv_buf in order to append a terminator */
  80. recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);
  81. if (recvResult == SOCKET_ERROR || recvResult == 0)
  82. {
  83. break;
  84. }
  85. /* NUL-terminate the string */
  86. inputBuffer[recvResult] = '\0';
  87. /* Convert to int */
  88. data = atoi(inputBuffer);
  89. }
  90. while (0);
  91. if (connectSocket != INVALID_SOCKET)
  92. {
  93. CLOSE_SOCKET(connectSocket);
  94. }
  95. #ifdef _WIN32
  96. if (wsaDataInit)
  97. {
  98. WSACleanup();
  99. }
  100. #endif
  101. }
  102. }
  103. if(1)
  104. {
  105. {
  106. int buffer[10] = { 0 };
  107. /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound
  108. * This check does not check the upper bounds of the array index */
  109. if (data >= 0)
  110. {
  111. printIntLine(buffer[data]);
  112. }
  113. else
  114. {
  115. printLine("ERROR: Array index is negative");
  116. }
  117. }
  118. }
  119. }
  120. #endif /* OMITBAD */
  121. #ifndef OMITGOOD
  122. /* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */
  123. static void goodB2G1()
  124. {
  125. int data;
  126. /* Initialize data */
  127. data = -1;
  128. if(1)
  129. {
  130. {
  131. #ifdef _WIN32
  132. WSADATA wsaData;
  133. int wsaDataInit = 0;
  134. #endif
  135. int recvResult;
  136. struct sockaddr_in service;
  137. SOCKET connectSocket = INVALID_SOCKET;
  138. char inputBuffer[CHAR_ARRAY_SIZE];
  139. do
  140. {
  141. #ifdef _WIN32
  142. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  143. {
  144. break;
  145. }
  146. wsaDataInit = 1;
  147. #endif
  148. /* POTENTIAL FLAW: Read data using a connect socket */
  149. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  150. if (connectSocket == INVALID_SOCKET)
  151. {
  152. break;
  153. }
  154. memset(&service, 0, sizeof(service));
  155. service.sin_family = AF_INET;
  156. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  157. service.sin_port = htons(TCP_PORT);
  158. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  159. {
  160. break;
  161. }
  162. /* Abort on error or the connection was closed, make sure to recv one
  163. * less char than is in the recv_buf in order to append a terminator */
  164. recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);
  165. if (recvResult == SOCKET_ERROR || recvResult == 0)
  166. {
  167. break;
  168. }
  169. /* NUL-terminate the string */
  170. inputBuffer[recvResult] = '\0';
  171. /* Convert to int */
  172. data = atoi(inputBuffer);
  173. }
  174. while (0);
  175. if (connectSocket != INVALID_SOCKET)
  176. {
  177. CLOSE_SOCKET(connectSocket);
  178. }
  179. #ifdef _WIN32
  180. if (wsaDataInit)
  181. {
  182. WSACleanup();
  183. }
  184. #endif
  185. }
  186. }
  187. if(0)
  188. {
  189. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  190. printLine("Benign, fixed string");
  191. }
  192. else
  193. {
  194. {
  195. int buffer[10] = { 0 };
  196. /* FIX: Properly validate the array index and prevent a buffer overread */
  197. if (data >= 0 && data < (10))
  198. {
  199. printIntLine(buffer[data]);
  200. }
  201. else
  202. {
  203. printLine("ERROR: Array index is out-of-bounds");
  204. }
  205. }
  206. }
  207. }
  208. /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  209. static void goodB2G2()
  210. {
  211. int data;
  212. /* Initialize data */
  213. data = -1;
  214. if(1)
  215. {
  216. {
  217. #ifdef _WIN32
  218. WSADATA wsaData;
  219. int wsaDataInit = 0;
  220. #endif
  221. int recvResult;
  222. struct sockaddr_in service;
  223. SOCKET connectSocket = INVALID_SOCKET;
  224. char inputBuffer[CHAR_ARRAY_SIZE];
  225. do
  226. {
  227. #ifdef _WIN32
  228. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  229. {
  230. break;
  231. }
  232. wsaDataInit = 1;
  233. #endif
  234. /* POTENTIAL FLAW: Read data using a connect socket */
  235. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  236. if (connectSocket == INVALID_SOCKET)
  237. {
  238. break;
  239. }
  240. memset(&service, 0, sizeof(service));
  241. service.sin_family = AF_INET;
  242. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  243. service.sin_port = htons(TCP_PORT);
  244. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  245. {
  246. break;
  247. }
  248. /* Abort on error or the connection was closed, make sure to recv one
  249. * less char than is in the recv_buf in order to append a terminator */
  250. recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);
  251. if (recvResult == SOCKET_ERROR || recvResult == 0)
  252. {
  253. break;
  254. }
  255. /* NUL-terminate the string */
  256. inputBuffer[recvResult] = '\0';
  257. /* Convert to int */
  258. data = atoi(inputBuffer);
  259. }
  260. while (0);
  261. if (connectSocket != INVALID_SOCKET)
  262. {
  263. CLOSE_SOCKET(connectSocket);
  264. }
  265. #ifdef _WIN32
  266. if (wsaDataInit)
  267. {
  268. WSACleanup();
  269. }
  270. #endif
  271. }
  272. }
  273. if(1)
  274. {
  275. {
  276. int buffer[10] = { 0 };
  277. /* FIX: Properly validate the array index and prevent a buffer overread */
  278. if (data >= 0 && data < (10))
  279. {
  280. printIntLine(buffer[data]);
  281. }
  282. else
  283. {
  284. printLine("ERROR: Array index is out-of-bounds");
  285. }
  286. }
  287. }
  288. }
  289. /* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */
  290. static void goodG2B1()
  291. {
  292. int data;
  293. /* Initialize data */
  294. data = -1;
  295. if(0)
  296. {
  297. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  298. printLine("Benign, fixed string");
  299. }
  300. else
  301. {
  302. /* FIX: Use a value greater than 0, but less than 10 to avoid attempting to
  303. * access an index of the array in the sink that is out-of-bounds */
  304. data = 7;
  305. }
  306. if(1)
  307. {
  308. {
  309. int buffer[10] = { 0 };
  310. /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound
  311. * This check does not check the upper bounds of the array index */
  312. if (data >= 0)
  313. {
  314. printIntLine(buffer[data]);
  315. }
  316. else
  317. {
  318. printLine("ERROR: Array index is negative");
  319. }
  320. }
  321. }
  322. }
  323. /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  324. static void goodG2B2()
  325. {
  326. int data;
  327. /* Initialize data */
  328. data = -1;
  329. if(1)
  330. {
  331. /* FIX: Use a value greater than 0, but less than 10 to avoid attempting to
  332. * access an index of the array in the sink that is out-of-bounds */
  333. data = 7;
  334. }
  335. if(1)
  336. {
  337. {
  338. int buffer[10] = { 0 };
  339. /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound
  340. * This check does not check the upper bounds of the array index */
  341. if (data >= 0)
  342. {
  343. printIntLine(buffer[data]);
  344. }
  345. else
  346. {
  347. printLine("ERROR: Array index is negative");
  348. }
  349. }
  350. }
  351. }
  352. void CWE126_Buffer_Overread__CWE129_connect_socket_02_good()
  353. {
  354. goodB2G1();
  355. goodB2G2();
  356. goodG2B1();
  357. goodG2B2();
  358. }
  359. #endif /* OMITGOOD */
  360. /* Below is the main(). It is only used when building this testcase on
  361. its own for testing or for building a binary to use in testing binary
  362. analysis tools. It is not used when compiling all the testcases as one
  363. application, which is how source code analysis tools are tested. */
  364. #ifdef INCLUDEMAIN
  365. int main(int argc, char * argv[])
  366. {
  367. /* seed randomness */
  368. srand( (unsigned)time(NULL) );
  369. #ifndef OMITGOOD
  370. printLine("Calling good()...");
  371. CWE126_Buffer_Overread__CWE129_connect_socket_02_good();
  372. printLine("Finished good()");
  373. #endif /* OMITGOOD */
  374. #ifndef OMITBAD
  375. printLine("Calling bad()...");
  376. CWE126_Buffer_Overread__CWE129_connect_socket_02_bad();
  377. printLine("Finished bad()");
  378. #endif /* OMITBAD */
  379. return 0;
  380. }
  381. #endif

No Description

Contributors (1)