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.

CWE606_Unchecked_Loop_Condition__char_connect_socket_02.c 14 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE606_Unchecked_Loop_Condition__char_connect_socket_02.c
  3. Label Definition File: CWE606_Unchecked_Loop_Condition.label.xml
  4. Template File: sources-sinks-02.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 606 Unchecked Input For Loop Condition
  9. * BadSource: connect_socket Read data using a connect socket (client side)
  10. * GoodSource: Input a number less than MAX_LOOP
  11. * Sinks:
  12. * GoodSink: Use data as the for loop variant after checking to see if it is less than MAX_LOOP
  13. * BadSink : Use data as the for loop variant without checking its size
  14. * Flow Variant: 02 Control flow: if(1) and if(0)
  15. *
  16. * */
  17. #include "std_testcase.h"
  18. #define MAX_LOOP 10000
  19. #ifndef _WIN32
  20. #include <wchar.h>
  21. #endif
  22. #ifdef _WIN32
  23. #include <winsock2.h>
  24. #include <windows.h>
  25. #include <direct.h>
  26. #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  27. #define CLOSE_SOCKET closesocket
  28. #else /* NOT _WIN32 */
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <unistd.h>
  34. #define INVALID_SOCKET -1
  35. #define SOCKET_ERROR -1
  36. #define CLOSE_SOCKET close
  37. #define SOCKET int
  38. #endif
  39. #define TCP_PORT 27015
  40. #define IP_ADDRESS "127.0.0.1"
  41. #ifndef OMITBAD
  42. void CWE606_Unchecked_Loop_Condition__char_connect_socket_02_bad()
  43. {
  44. char * data;
  45. char dataBuffer[100] = "";
  46. data = dataBuffer;
  47. if(1)
  48. {
  49. {
  50. #ifdef _WIN32
  51. WSADATA wsaData;
  52. int wsaDataInit = 0;
  53. #endif
  54. int recvResult;
  55. struct sockaddr_in service;
  56. char *replace;
  57. SOCKET connectSocket = INVALID_SOCKET;
  58. size_t dataLen = strlen(data);
  59. do
  60. {
  61. #ifdef _WIN32
  62. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  63. {
  64. break;
  65. }
  66. wsaDataInit = 1;
  67. #endif
  68. /* POTENTIAL FLAW: Read data using a connect socket */
  69. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  70. if (connectSocket == INVALID_SOCKET)
  71. {
  72. break;
  73. }
  74. memset(&service, 0, sizeof(service));
  75. service.sin_family = AF_INET;
  76. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  77. service.sin_port = htons(TCP_PORT);
  78. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  79. {
  80. break;
  81. }
  82. /* Abort on error or the connection was closed, make sure to recv one
  83. * less char than is in the recv_buf in order to append a terminator */
  84. /* Abort on error or the connection was closed */
  85. recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);
  86. if (recvResult == SOCKET_ERROR || recvResult == 0)
  87. {
  88. break;
  89. }
  90. /* Append null terminator */
  91. data[dataLen + recvResult / sizeof(char)] = '\0';
  92. /* Eliminate CRLF */
  93. replace = strchr(data, '\r');
  94. if (replace)
  95. {
  96. *replace = '\0';
  97. }
  98. replace = strchr(data, '\n');
  99. if (replace)
  100. {
  101. *replace = '\0';
  102. }
  103. }
  104. while (0);
  105. if (connectSocket != INVALID_SOCKET)
  106. {
  107. CLOSE_SOCKET(connectSocket);
  108. }
  109. #ifdef _WIN32
  110. if (wsaDataInit)
  111. {
  112. WSACleanup();
  113. }
  114. #endif
  115. }
  116. }
  117. if(1)
  118. {
  119. {
  120. int i, n, intVariable;
  121. if (sscanf(data, "%d", &n) == 1)
  122. {
  123. /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
  124. intVariable = 0;
  125. for (i = 0; i < n; i++)
  126. {
  127. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  128. intVariable++; /* avoid a dead/empty code block issue */
  129. }
  130. printIntLine(intVariable);
  131. }
  132. }
  133. }
  134. }
  135. #endif /* OMITBAD */
  136. #ifndef OMITGOOD
  137. /* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */
  138. static void goodB2G1()
  139. {
  140. char * data;
  141. char dataBuffer[100] = "";
  142. data = dataBuffer;
  143. if(1)
  144. {
  145. {
  146. #ifdef _WIN32
  147. WSADATA wsaData;
  148. int wsaDataInit = 0;
  149. #endif
  150. int recvResult;
  151. struct sockaddr_in service;
  152. char *replace;
  153. SOCKET connectSocket = INVALID_SOCKET;
  154. size_t dataLen = strlen(data);
  155. do
  156. {
  157. #ifdef _WIN32
  158. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  159. {
  160. break;
  161. }
  162. wsaDataInit = 1;
  163. #endif
  164. /* POTENTIAL FLAW: Read data using a connect socket */
  165. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  166. if (connectSocket == INVALID_SOCKET)
  167. {
  168. break;
  169. }
  170. memset(&service, 0, sizeof(service));
  171. service.sin_family = AF_INET;
  172. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  173. service.sin_port = htons(TCP_PORT);
  174. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  175. {
  176. break;
  177. }
  178. /* Abort on error or the connection was closed, make sure to recv one
  179. * less char than is in the recv_buf in order to append a terminator */
  180. /* Abort on error or the connection was closed */
  181. recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);
  182. if (recvResult == SOCKET_ERROR || recvResult == 0)
  183. {
  184. break;
  185. }
  186. /* Append null terminator */
  187. data[dataLen + recvResult / sizeof(char)] = '\0';
  188. /* Eliminate CRLF */
  189. replace = strchr(data, '\r');
  190. if (replace)
  191. {
  192. *replace = '\0';
  193. }
  194. replace = strchr(data, '\n');
  195. if (replace)
  196. {
  197. *replace = '\0';
  198. }
  199. }
  200. while (0);
  201. if (connectSocket != INVALID_SOCKET)
  202. {
  203. CLOSE_SOCKET(connectSocket);
  204. }
  205. #ifdef _WIN32
  206. if (wsaDataInit)
  207. {
  208. WSACleanup();
  209. }
  210. #endif
  211. }
  212. }
  213. if(0)
  214. {
  215. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  216. printLine("Benign, fixed string");
  217. }
  218. else
  219. {
  220. {
  221. int i, n, intVariable;
  222. if (sscanf(data, "%d", &n) == 1)
  223. {
  224. /* FIX: limit loop iteration counts */
  225. if (n < MAX_LOOP)
  226. {
  227. intVariable = 0;
  228. for (i = 0; i < n; i++)
  229. {
  230. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  231. intVariable++; /* avoid a dead/empty code block issue */
  232. }
  233. printIntLine(intVariable);
  234. }
  235. }
  236. }
  237. }
  238. }
  239. /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  240. static void goodB2G2()
  241. {
  242. char * data;
  243. char dataBuffer[100] = "";
  244. data = dataBuffer;
  245. if(1)
  246. {
  247. {
  248. #ifdef _WIN32
  249. WSADATA wsaData;
  250. int wsaDataInit = 0;
  251. #endif
  252. int recvResult;
  253. struct sockaddr_in service;
  254. char *replace;
  255. SOCKET connectSocket = INVALID_SOCKET;
  256. size_t dataLen = strlen(data);
  257. do
  258. {
  259. #ifdef _WIN32
  260. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
  261. {
  262. break;
  263. }
  264. wsaDataInit = 1;
  265. #endif
  266. /* POTENTIAL FLAW: Read data using a connect socket */
  267. connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  268. if (connectSocket == INVALID_SOCKET)
  269. {
  270. break;
  271. }
  272. memset(&service, 0, sizeof(service));
  273. service.sin_family = AF_INET;
  274. service.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  275. service.sin_port = htons(TCP_PORT);
  276. if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)
  277. {
  278. break;
  279. }
  280. /* Abort on error or the connection was closed, make sure to recv one
  281. * less char than is in the recv_buf in order to append a terminator */
  282. /* Abort on error or the connection was closed */
  283. recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);
  284. if (recvResult == SOCKET_ERROR || recvResult == 0)
  285. {
  286. break;
  287. }
  288. /* Append null terminator */
  289. data[dataLen + recvResult / sizeof(char)] = '\0';
  290. /* Eliminate CRLF */
  291. replace = strchr(data, '\r');
  292. if (replace)
  293. {
  294. *replace = '\0';
  295. }
  296. replace = strchr(data, '\n');
  297. if (replace)
  298. {
  299. *replace = '\0';
  300. }
  301. }
  302. while (0);
  303. if (connectSocket != INVALID_SOCKET)
  304. {
  305. CLOSE_SOCKET(connectSocket);
  306. }
  307. #ifdef _WIN32
  308. if (wsaDataInit)
  309. {
  310. WSACleanup();
  311. }
  312. #endif
  313. }
  314. }
  315. if(1)
  316. {
  317. {
  318. int i, n, intVariable;
  319. if (sscanf(data, "%d", &n) == 1)
  320. {
  321. /* FIX: limit loop iteration counts */
  322. if (n < MAX_LOOP)
  323. {
  324. intVariable = 0;
  325. for (i = 0; i < n; i++)
  326. {
  327. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  328. intVariable++; /* avoid a dead/empty code block issue */
  329. }
  330. printIntLine(intVariable);
  331. }
  332. }
  333. }
  334. }
  335. }
  336. /* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */
  337. static void goodG2B1()
  338. {
  339. char * data;
  340. char dataBuffer[100] = "";
  341. data = dataBuffer;
  342. if(0)
  343. {
  344. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  345. printLine("Benign, fixed string");
  346. }
  347. else
  348. {
  349. /* FIX: Set data to a number less than MAX_LOOP */
  350. strcpy(data, "15");
  351. }
  352. if(1)
  353. {
  354. {
  355. int i, n, intVariable;
  356. if (sscanf(data, "%d", &n) == 1)
  357. {
  358. /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
  359. intVariable = 0;
  360. for (i = 0; i < n; i++)
  361. {
  362. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  363. intVariable++; /* avoid a dead/empty code block issue */
  364. }
  365. printIntLine(intVariable);
  366. }
  367. }
  368. }
  369. }
  370. /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  371. static void goodG2B2()
  372. {
  373. char * data;
  374. char dataBuffer[100] = "";
  375. data = dataBuffer;
  376. if(1)
  377. {
  378. /* FIX: Set data to a number less than MAX_LOOP */
  379. strcpy(data, "15");
  380. }
  381. if(1)
  382. {
  383. {
  384. int i, n, intVariable;
  385. if (sscanf(data, "%d", &n) == 1)
  386. {
  387. /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
  388. intVariable = 0;
  389. for (i = 0; i < n; i++)
  390. {
  391. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  392. intVariable++; /* avoid a dead/empty code block issue */
  393. }
  394. printIntLine(intVariable);
  395. }
  396. }
  397. }
  398. }
  399. void CWE606_Unchecked_Loop_Condition__char_connect_socket_02_good()
  400. {
  401. goodB2G1();
  402. goodB2G2();
  403. goodG2B1();
  404. goodG2B2();
  405. }
  406. #endif /* OMITGOOD */
  407. /* Below is the main(). It is only used when building this testcase on
  408. its own for testing or for building a binary to use in testing binary
  409. analysis tools. It is not used when compiling all the testcases as one
  410. application, which is how source code analysis tools are tested. */
  411. #ifdef INCLUDEMAIN
  412. int main(int argc, char * argv[])
  413. {
  414. /* seed randomness */
  415. srand( (unsigned)time(NULL) );
  416. #ifndef OMITGOOD
  417. printLine("Calling good()...");
  418. CWE606_Unchecked_Loop_Condition__char_connect_socket_02_good();
  419. printLine("Finished good()");
  420. #endif /* OMITGOOD */
  421. #ifndef OMITBAD
  422. printLine("Calling bad()...");
  423. CWE606_Unchecked_Loop_Condition__char_connect_socket_02_bad();
  424. printLine("Finished bad()");
  425. #endif /* OMITBAD */
  426. return 0;
  427. }
  428. #endif

No Description

Contributors (1)