fuzzer.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /* By Guido Vranken <guidovranken@gmail.com> --
  2. * https://guidovranken.wordpress.com/ */
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. #include <limits.h>
  8. #include "srtp.h"
  9. #include "srtp_priv.h"
  10. #include "fuzzer.h"
  11. #include "mt19937.h"
  12. #include "testmem.h"
  13. /* Global variables */
  14. static bool g_no_align = false; /* Can be enabled with --no_align */
  15. static bool g_post_init =
  16. false; /* Set to true once past initialization phase */
  17. static bool g_write_input = false;
  18. #ifdef FUZZ_32BIT
  19. #include <sys/mman.h>
  20. static bool g_no_mmap = false; /* Can be enabled with --no_mmap */
  21. static void *g_mmap_allocation =
  22. NULL; /* Keeps current mmap() allocation address */
  23. static size_t g_mmap_allocation_size =
  24. 0; /* Keeps current mmap() allocation size */
  25. #endif
  26. /* Custom allocator functions */
  27. static void *fuzz_alloc(const size_t size, const bool do_zero)
  28. {
  29. void *ret = NULL;
  30. #ifdef FUZZ_32BIT
  31. bool do_malloc = true;
  32. #endif
  33. bool do_mmap, mmap_high = true;
  34. if (size == 0) {
  35. size_t ret;
  36. /* Allocations of size 0 are not illegal, but are a bad practice, since
  37. * writing just a single byte to this region constitutes undefined
  38. * behavior per the C spec. glibc will return a small, valid memory
  39. * region
  40. * whereas OpenBSD will crash upon writing to it.
  41. * Intentionally return a pointer to an invalid page to detect
  42. * unsound code efficiently.
  43. * fuzz_free is aware of this pointer range and will not attempt
  44. * to free()/munmap() it.
  45. */
  46. ret = 0x01 + (fuzz_mt19937_get() % 1024);
  47. return (void *)ret;
  48. }
  49. /* Don't do mmap()-based allocations during initialization */
  50. if (g_post_init == true) {
  51. /* Even extract these values if --no_mmap is specified.
  52. * This keeps the PRNG output stream consistent across
  53. * fuzzer configurations.
  54. */
  55. do_mmap = (fuzz_mt19937_get() % 64) == 0 ? true : false;
  56. if (do_mmap == true) {
  57. mmap_high = (fuzz_mt19937_get() % 2) == 0 ? true : false;
  58. }
  59. } else {
  60. do_mmap = false;
  61. }
  62. #ifdef FUZZ_32BIT
  63. /* g_mmap_allocation must be NULL because we only support a single
  64. * concurrent mmap allocation at a time
  65. */
  66. if (g_mmap_allocation == NULL && g_no_mmap == false && do_mmap == true) {
  67. void *mmap_address;
  68. if (mmap_high == true) {
  69. mmap_address = (void *)0xFFFF0000;
  70. } else {
  71. mmap_address = (void *)0x00010000;
  72. }
  73. g_mmap_allocation_size = size;
  74. ret = mmap(mmap_address, g_mmap_allocation_size, PROT_READ | PROT_WRITE,
  75. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  76. if (ret == MAP_FAILED) {
  77. /* That's okay -- just return NULL to the caller */
  78. ret = NULL;
  79. /* Reset this for the sake of cleanliness */
  80. g_mmap_allocation_size = 0;
  81. }
  82. /* ret not being MAP_FAILED does not mean that ret is the requested
  83. * address (mmap_address). That's okay. We're not going to perform
  84. * a munmap() on it and call malloc() instead. It won't gain us
  85. * anything.
  86. */
  87. g_mmap_allocation = ret;
  88. do_malloc = false;
  89. }
  90. if (do_malloc == true)
  91. #endif
  92. {
  93. ret = malloc(size);
  94. }
  95. /* Mimic calloc() if so requested */
  96. if (ret != NULL && do_zero) {
  97. memset(ret, 0, size);
  98. }
  99. return ret;
  100. }
  101. /* Internal allocations by this fuzzer must on one hand (sometimes)
  102. * receive memory from mmap(), but on the other hand these requests for
  103. * memory may not fail. By calling this function, the allocation is
  104. * guaranteed to succeed; it first tries with fuzz_alloc(), which may
  105. * fail if it uses mmap(), and if that is the case, memory is allocated
  106. * via the libc allocator (malloc, calloc) which should always succeed */
  107. static void *fuzz_alloc_succeed(const size_t size, const bool do_zero)
  108. {
  109. void *ret = fuzz_alloc(size, do_zero);
  110. if (ret == NULL) {
  111. if (do_zero == false) {
  112. ret = malloc(size);
  113. } else {
  114. ret = calloc(1, size);
  115. }
  116. }
  117. return ret;
  118. }
  119. void *fuzz_calloc(const size_t nmemb, const size_t size)
  120. {
  121. /* We must be past srtp_init() to prevent that that function fails */
  122. if (g_post_init == true) {
  123. /* Fail 1 in 64 allocations on average to test whether the library
  124. * can deal with this properly.
  125. */
  126. if ((fuzz_mt19937_get() % 64) == 0) {
  127. return NULL;
  128. }
  129. }
  130. return fuzz_alloc(nmemb * size, true);
  131. }
  132. static bool fuzz_is_special_pointer(void *ptr)
  133. {
  134. /* Special, invalid pointers introduced when code attempted
  135. * to do size = 0 allocations.
  136. */
  137. if ((size_t)ptr >= 0x01 && (size_t)ptr < (0x01 + 1024)) {
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. void fuzz_free(void *ptr)
  144. {
  145. if (fuzz_is_special_pointer(ptr) == true) {
  146. return;
  147. }
  148. #ifdef FUZZ_32BIT
  149. if (g_post_init == true && ptr != NULL && ptr == g_mmap_allocation) {
  150. if (munmap(g_mmap_allocation, g_mmap_allocation_size) == -1) {
  151. /* Shouldn't happen */
  152. abort();
  153. }
  154. g_mmap_allocation = NULL;
  155. } else
  156. #endif
  157. {
  158. free(ptr);
  159. }
  160. }
  161. static srtp_err_status_t fuzz_srtp_protect(srtp_t srtp_sender,
  162. void *hdr,
  163. int *len,
  164. uint8_t use_mki,
  165. unsigned int mki)
  166. {
  167. return srtp_protect(srtp_sender, hdr, len);
  168. }
  169. static srtp_err_status_t fuzz_srtp_unprotect(srtp_t srtp_sender,
  170. void *hdr,
  171. int *len,
  172. uint8_t use_mki,
  173. unsigned int mki)
  174. {
  175. return srtp_unprotect(srtp_sender, hdr, len);
  176. }
  177. static srtp_err_status_t fuzz_srtp_protect_rtcp(srtp_t srtp_sender,
  178. void *hdr,
  179. int *len,
  180. uint8_t use_mki,
  181. unsigned int mki)
  182. {
  183. return srtp_protect_rtcp(srtp_sender, hdr, len);
  184. }
  185. static srtp_err_status_t fuzz_srtp_unprotect_rtcp(srtp_t srtp_sender,
  186. void *hdr,
  187. int *len,
  188. uint8_t use_mki,
  189. unsigned int mki)
  190. {
  191. return srtp_unprotect_rtcp(srtp_sender, hdr, len);
  192. }
  193. static srtp_err_status_t fuzz_srtp_protect_mki(srtp_t srtp_sender,
  194. void *hdr,
  195. int *len,
  196. uint8_t use_mki,
  197. unsigned int mki)
  198. {
  199. return srtp_protect_mki(srtp_sender, hdr, len, use_mki, mki);
  200. }
  201. static srtp_err_status_t fuzz_srtp_protect_rtcp_mki(srtp_t srtp_sender,
  202. void *hdr,
  203. int *len,
  204. uint8_t use_mki,
  205. unsigned int mki)
  206. {
  207. return srtp_protect_rtcp_mki(srtp_sender, hdr, len, use_mki, mki);
  208. }
  209. static srtp_err_status_t fuzz_srtp_unprotect_mki(srtp_t srtp_sender,
  210. void *hdr,
  211. int *len,
  212. uint8_t use_mki,
  213. unsigned int mki)
  214. {
  215. return srtp_unprotect_mki(srtp_sender, hdr, len, use_mki);
  216. }
  217. static srtp_err_status_t fuzz_srtp_unprotect_rtcp_mki(srtp_t srtp_sender,
  218. void *hdr,
  219. int *len,
  220. uint8_t use_mki,
  221. unsigned int mki)
  222. {
  223. return srtp_unprotect_rtcp_mki(srtp_sender, hdr, len, use_mki);
  224. }
  225. /* Get protect length functions */
  226. static srtp_err_status_t fuzz_srtp_get_protect_length(const srtp_t srtp_ctx,
  227. uint8_t use_mki,
  228. unsigned int mki,
  229. uint32_t *length)
  230. {
  231. return srtp_get_protect_trailer_length(srtp_ctx, 0, 0, length);
  232. }
  233. static srtp_err_status_t fuzz_srtp_get_protect_rtcp_length(
  234. const srtp_t srtp_ctx,
  235. uint8_t use_mki,
  236. unsigned int mki,
  237. uint32_t *length)
  238. {
  239. return srtp_get_protect_rtcp_trailer_length(srtp_ctx, 0, 0, length);
  240. }
  241. static srtp_err_status_t fuzz_srtp_get_protect_mki_length(const srtp_t srtp_ctx,
  242. uint8_t use_mki,
  243. unsigned int mki,
  244. uint32_t *length)
  245. {
  246. return srtp_get_protect_trailer_length(srtp_ctx, use_mki, mki, length);
  247. }
  248. static srtp_err_status_t fuzz_srtp_get_protect_rtcp_mki_length(
  249. const srtp_t srtp_ctx,
  250. uint8_t use_mki,
  251. unsigned int mki,
  252. uint32_t *length)
  253. {
  254. return srtp_get_protect_rtcp_trailer_length(srtp_ctx, use_mki, mki, length);
  255. }
  256. static uint8_t *extract_key(const uint8_t **data,
  257. size_t *size,
  258. const size_t key_size)
  259. {
  260. uint8_t *ret;
  261. if (*size < key_size) {
  262. return NULL;
  263. }
  264. ret = fuzz_alloc_succeed(key_size, false);
  265. EXTRACT(ret, *data, *size, key_size);
  266. return ret;
  267. }
  268. static srtp_master_key_t *extract_master_key(const uint8_t **data,
  269. size_t *size,
  270. const size_t key_size,
  271. bool simulate,
  272. bool *success)
  273. {
  274. srtp_master_key_t *ret = NULL;
  275. uint16_t mki_id_size;
  276. if (simulate == true) {
  277. *success = false;
  278. }
  279. EXTRACT_IF(&mki_id_size, *data, *size, sizeof(mki_id_size));
  280. if (*size < key_size + mki_id_size) {
  281. goto end;
  282. }
  283. if (simulate == true) {
  284. *data += key_size + mki_id_size;
  285. *size -= key_size + mki_id_size;
  286. *success = true;
  287. goto end;
  288. }
  289. ret = fuzz_alloc_succeed(sizeof(srtp_master_key_t), false);
  290. ret->key = fuzz_alloc_succeed(key_size, false);
  291. ret->mki_id = fuzz_alloc_succeed(mki_id_size, false);
  292. EXTRACT(ret->key, *data, *size, key_size);
  293. EXTRACT(ret->mki_id, *data, *size, mki_id_size);
  294. ret->mki_size = mki_id_size;
  295. end:
  296. return ret;
  297. }
  298. static srtp_master_key_t **extract_master_keys(const uint8_t **data,
  299. size_t *size,
  300. const size_t key_size,
  301. unsigned long *num_master_keys)
  302. {
  303. const uint8_t *data_orig = *data;
  304. size_t size_orig = *size;
  305. size_t i = 0;
  306. srtp_master_key_t **ret = NULL;
  307. *num_master_keys = 0;
  308. /* First pass -- dry run, determine how many keys we want and can extract */
  309. while (1) {
  310. uint8_t do_extract_master_key;
  311. bool success;
  312. if (*size < sizeof(do_extract_master_key)) {
  313. goto next;
  314. }
  315. EXTRACT(&do_extract_master_key, *data, *size,
  316. sizeof(do_extract_master_key));
  317. /* Decide whether to extract another key */
  318. if ((do_extract_master_key % 2) == 0) {
  319. break;
  320. }
  321. extract_master_key(data, size, key_size, true, &success);
  322. if (success == false) {
  323. break;
  324. }
  325. (*num_master_keys)++;
  326. }
  327. next:
  328. *data = data_orig;
  329. *size = size_orig;
  330. /* Allocate array of pointers */
  331. ret = fuzz_alloc_succeed(*num_master_keys * sizeof(srtp_master_key_t *),
  332. false);
  333. /* Second pass -- perform the actual extractions */
  334. for (i = 0; i < *num_master_keys; i++) {
  335. uint8_t do_extract_master_key;
  336. EXTRACT_IF(&do_extract_master_key, *data, *size,
  337. sizeof(do_extract_master_key));
  338. if ((do_extract_master_key % 2) == 0) {
  339. break;
  340. }
  341. ret[i] = extract_master_key(data, size, key_size, false, NULL);
  342. if (ret[i] == NULL) {
  343. /* Shouldn't happen */
  344. abort();
  345. }
  346. }
  347. end:
  348. return ret;
  349. }
  350. static srtp_policy_t *extract_policy(const uint8_t **data, size_t *size)
  351. {
  352. srtp_policy_t *policy = NULL;
  353. struct {
  354. uint8_t srtp_crypto_policy_func;
  355. uint64_t window_size;
  356. uint8_t allow_repeat_tx;
  357. uint8_t ssrc_type;
  358. uint32_t ssrc_value;
  359. uint8_t num_xtn_hdr;
  360. uint8_t do_extract_key;
  361. uint8_t do_extract_master_keys;
  362. } params;
  363. EXTRACT_IF(&params, *data, *size, sizeof(params));
  364. params.srtp_crypto_policy_func %= sizeof(fuzz_srtp_crypto_policies) /
  365. sizeof(fuzz_srtp_crypto_policies[0]);
  366. params.allow_repeat_tx %= 2;
  367. params.ssrc_type %=
  368. sizeof(fuzz_ssrc_type_map) / sizeof(fuzz_ssrc_type_map[0]);
  369. policy = fuzz_alloc_succeed(sizeof(*policy), true);
  370. fuzz_srtp_crypto_policies[params.srtp_crypto_policy_func]
  371. .crypto_policy_func(&policy->rtp);
  372. fuzz_srtp_crypto_policies[params.srtp_crypto_policy_func]
  373. .crypto_policy_func(&policy->rtcp);
  374. if (policy->rtp.cipher_key_len > MAX_KEY_LEN) {
  375. /* Shouldn't happen */
  376. abort();
  377. }
  378. policy->ssrc.type = fuzz_ssrc_type_map[params.ssrc_type].srtp_ssrc_type;
  379. policy->ssrc.value = params.ssrc_value;
  380. if ((params.do_extract_key % 2) == 0) {
  381. policy->key = extract_key(data, size, policy->rtp.cipher_key_len);
  382. if (policy->key == NULL) {
  383. fuzz_free(policy);
  384. return NULL;
  385. }
  386. }
  387. if (params.num_xtn_hdr != 0) {
  388. const size_t xtn_hdr_size = params.num_xtn_hdr * sizeof(int);
  389. if (*size < xtn_hdr_size) {
  390. fuzz_free(policy->key);
  391. fuzz_free(policy);
  392. return NULL;
  393. }
  394. policy->enc_xtn_hdr = fuzz_alloc_succeed(xtn_hdr_size, false);
  395. EXTRACT(policy->enc_xtn_hdr, *data, *size, xtn_hdr_size);
  396. policy->enc_xtn_hdr_count = params.num_xtn_hdr;
  397. }
  398. if ((params.do_extract_master_keys % 2) == 0) {
  399. policy->keys = extract_master_keys(
  400. data, size, policy->rtp.cipher_key_len, &policy->num_master_keys);
  401. if (policy->keys == NULL) {
  402. fuzz_free(policy->key);
  403. fuzz_free(policy->enc_xtn_hdr);
  404. fuzz_free(policy);
  405. return NULL;
  406. }
  407. }
  408. policy->window_size = params.window_size;
  409. policy->allow_repeat_tx = params.allow_repeat_tx;
  410. policy->next = NULL;
  411. end:
  412. return policy;
  413. }
  414. static srtp_policy_t *extract_policies(const uint8_t **data, size_t *size)
  415. {
  416. srtp_policy_t *curpolicy = NULL, *policy_chain = NULL;
  417. curpolicy = extract_policy(data, size);
  418. if (curpolicy == NULL) {
  419. return NULL;
  420. }
  421. policy_chain = curpolicy;
  422. while (1) {
  423. uint8_t do_extract_policy;
  424. EXTRACT_IF(&do_extract_policy, *data, *size, sizeof(do_extract_policy));
  425. /* Decide whether to extract another policy */
  426. if ((do_extract_policy % 2) == 0) {
  427. break;
  428. }
  429. curpolicy->next = extract_policy(data, size);
  430. if (curpolicy->next == NULL) {
  431. break;
  432. }
  433. curpolicy = curpolicy->next;
  434. }
  435. end:
  436. return policy_chain;
  437. }
  438. static uint32_t *extract_remove_stream_ssrc(const uint8_t **data,
  439. size_t *size,
  440. uint8_t *num_remove_stream)
  441. {
  442. uint32_t *ret = NULL;
  443. uint8_t _num_remove_stream;
  444. size_t total_size;
  445. *num_remove_stream = 0;
  446. EXTRACT_IF(&_num_remove_stream, *data, *size, sizeof(_num_remove_stream));
  447. if (_num_remove_stream == 0) {
  448. goto end;
  449. }
  450. total_size = _num_remove_stream * sizeof(uint32_t);
  451. if (*size < total_size) {
  452. goto end;
  453. }
  454. ret = fuzz_alloc_succeed(total_size, false);
  455. EXTRACT(ret, *data, *size, total_size);
  456. *num_remove_stream = _num_remove_stream;
  457. end:
  458. return ret;
  459. }
  460. static uint32_t *extract_set_roc(const uint8_t **data,
  461. size_t *size,
  462. uint8_t *num_set_roc)
  463. {
  464. uint32_t *ret = NULL;
  465. uint8_t _num_set_roc;
  466. size_t total_size;
  467. *num_set_roc = 0;
  468. EXTRACT_IF(&_num_set_roc, *data, *size, sizeof(_num_set_roc));
  469. if (_num_set_roc == 0) {
  470. goto end;
  471. }
  472. /* Tuples of 2 uint32_t's */
  473. total_size = _num_set_roc * sizeof(uint32_t) * 2;
  474. if (*size < total_size) {
  475. goto end;
  476. }
  477. ret = fuzz_alloc_succeed(total_size, false);
  478. EXTRACT(ret, *data, *size, total_size);
  479. *num_set_roc = _num_set_roc;
  480. end:
  481. return ret;
  482. }
  483. static void free_policies(srtp_policy_t *curpolicy)
  484. {
  485. size_t i;
  486. while (curpolicy) {
  487. srtp_policy_t *next = curpolicy->next;
  488. fuzz_free(curpolicy->key);
  489. for (i = 0; i < curpolicy->num_master_keys; i++) {
  490. fuzz_free(curpolicy->keys[i]->key);
  491. fuzz_free(curpolicy->keys[i]->mki_id);
  492. fuzz_free(curpolicy->keys[i]);
  493. }
  494. fuzz_free(curpolicy->keys);
  495. fuzz_free(curpolicy->enc_xtn_hdr);
  496. fuzz_free(curpolicy);
  497. curpolicy = next;
  498. }
  499. }
  500. static uint8_t *run_srtp_func(const srtp_t srtp_ctx,
  501. const uint8_t **data,
  502. size_t *size)
  503. {
  504. uint8_t *ret = NULL;
  505. uint8_t *copy = NULL, *copy_2 = NULL;
  506. struct {
  507. uint16_t size;
  508. uint8_t srtp_func;
  509. uint8_t use_mki;
  510. uint32_t mki;
  511. uint8_t stretch;
  512. } params_1;
  513. struct {
  514. uint8_t srtp_func;
  515. uint8_t use_mki;
  516. uint32_t mki;
  517. } params_2;
  518. int ret_size;
  519. EXTRACT_IF(&params_1, *data, *size, sizeof(params_1));
  520. params_1.srtp_func %= sizeof(srtp_funcs) / sizeof(srtp_funcs[0]);
  521. params_1.use_mki %= 2;
  522. if (*size < params_1.size) {
  523. goto end;
  524. }
  525. /* Enforce 4 byte alignment */
  526. if (g_no_align == false) {
  527. params_1.size -= params_1.size % 4;
  528. }
  529. if (params_1.size == 0) {
  530. goto end;
  531. }
  532. ret_size = params_1.size;
  533. if (srtp_funcs[params_1.srtp_func].protect == true) {
  534. /* Intentionally not initialized to trigger MemorySanitizer, if
  535. * applicable */
  536. uint32_t alloc_size;
  537. if (srtp_funcs[params_1.srtp_func].get_length(
  538. srtp_ctx, params_1.use_mki, params_1.mki, &alloc_size) !=
  539. srtp_err_status_ok) {
  540. goto end;
  541. }
  542. copy = fuzz_alloc_succeed(ret_size + alloc_size, false);
  543. } else {
  544. copy = fuzz_alloc_succeed(ret_size, false);
  545. }
  546. EXTRACT(copy, *data, *size, params_1.size);
  547. if (srtp_funcs[params_1.srtp_func].srtp_func(
  548. srtp_ctx, copy, &ret_size, params_1.use_mki, params_1.mki) !=
  549. srtp_err_status_ok) {
  550. fuzz_free(copy);
  551. goto end;
  552. }
  553. // fuzz_free(copy);
  554. fuzz_testmem(copy, ret_size);
  555. ret = copy;
  556. EXTRACT_IF(&params_2, *data, *size, sizeof(params_2));
  557. params_2.srtp_func %= sizeof(srtp_funcs) / sizeof(srtp_funcs[0]);
  558. params_2.use_mki %= 2;
  559. if (ret_size == 0) {
  560. goto end;
  561. }
  562. if (srtp_funcs[params_2.srtp_func].protect == true) {
  563. /* Intentionally not initialized to trigger MemorySanitizer, if
  564. * applicable */
  565. uint32_t alloc_size;
  566. if (srtp_funcs[params_2.srtp_func].get_length(
  567. srtp_ctx, params_2.use_mki, params_2.mki, &alloc_size) !=
  568. srtp_err_status_ok) {
  569. goto end;
  570. }
  571. copy_2 = fuzz_alloc_succeed(ret_size + alloc_size, false);
  572. } else {
  573. copy_2 = fuzz_alloc_succeed(ret_size, false);
  574. }
  575. memcpy(copy_2, copy, ret_size);
  576. fuzz_free(copy);
  577. copy = copy_2;
  578. if (srtp_funcs[params_2.srtp_func].srtp_func(
  579. srtp_ctx, copy, &ret_size, params_2.use_mki, params_2.mki) !=
  580. srtp_err_status_ok) {
  581. fuzz_free(copy);
  582. ret = NULL;
  583. goto end;
  584. }
  585. fuzz_testmem(copy, ret_size);
  586. ret = copy;
  587. end:
  588. return ret;
  589. }
  590. void fuzz_srtp_event_handler(srtp_event_data_t *data)
  591. {
  592. fuzz_testmem(data, sizeof(srtp_event_data_t));
  593. if (data->session != NULL) {
  594. fuzz_testmem(data->session, sizeof(*data->session));
  595. }
  596. }
  597. static void fuzz_write_input(const uint8_t *data, size_t size)
  598. {
  599. FILE *fp = fopen("input.bin", "wb");
  600. if (fp == NULL) {
  601. /* Shouldn't happen */
  602. abort();
  603. }
  604. if (size != 0 && fwrite(data, size, 1, fp) != 1) {
  605. printf("Cannot write\n");
  606. /* Shouldn't happen */
  607. abort();
  608. }
  609. fclose(fp);
  610. }
  611. int LLVMFuzzerInitialize(int *argc, char ***argv)
  612. {
  613. char **_argv = *argv;
  614. int i;
  615. bool no_custom_event_handler = false;
  616. if (srtp_init() != srtp_err_status_ok) {
  617. /* Shouldn't happen */
  618. abort();
  619. }
  620. for (i = 0; i < *argc; i++) {
  621. if (strcmp("--no_align", _argv[i]) == 0) {
  622. g_no_align = true;
  623. } else if (strcmp("--no_custom_event_handler", _argv[i]) == 0) {
  624. no_custom_event_handler = true;
  625. } else if (strcmp("--write_input", _argv[i]) == 0) {
  626. g_write_input = true;
  627. }
  628. #ifdef FUZZ_32BIT
  629. else if (strcmp("--no_mmap", _argv[i]) == 0) {
  630. g_no_mmap = true;
  631. }
  632. #endif
  633. else if (strncmp("--", _argv[i], 2) == 0) {
  634. printf("Invalid argument: %s\n", _argv[i]);
  635. exit(0);
  636. }
  637. }
  638. if (no_custom_event_handler == false) {
  639. if (srtp_install_event_handler(fuzz_srtp_event_handler) !=
  640. srtp_err_status_ok) {
  641. /* Shouldn't happen */
  642. abort();
  643. }
  644. }
  645. /* Fully initialized -- past this point, simulated allocation failures
  646. * are allowed to occur */
  647. g_post_init = true;
  648. return 0;
  649. }
  650. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
  651. {
  652. uint8_t num_remove_stream;
  653. uint32_t *remove_stream_ssrc = NULL;
  654. uint8_t num_set_roc;
  655. uint32_t *set_roc = NULL;
  656. srtp_t srtp_ctx = NULL;
  657. srtp_policy_t *policy_chain = NULL, *policy_chain_2 = NULL;
  658. uint32_t randseed;
  659. static bool firstrun = true;
  660. if (firstrun == true) {
  661. /* TODO version check etc and send it to MSAN */
  662. }
  663. #ifdef FUZZ_32BIT
  664. /* Free the mmap allocation made during the previous iteration, if
  665. * applicable */
  666. fuzz_free(g_mmap_allocation);
  667. #endif
  668. if (g_write_input == true) {
  669. fuzz_write_input(data, size);
  670. }
  671. EXTRACT_IF(&randseed, data, size, sizeof(randseed));
  672. fuzz_mt19937_init(randseed);
  673. srand(randseed);
  674. /* policy_chain is used to initialize the srtp context with */
  675. if ((policy_chain = extract_policies(&data, &size)) == NULL) {
  676. goto end;
  677. }
  678. /* policy_chain_2 is used as an argument to srtp_update later on */
  679. if ((policy_chain_2 = extract_policies(&data, &size)) == NULL) {
  680. goto end;
  681. }
  682. /* Create context */
  683. if (srtp_create(&srtp_ctx, policy_chain) != srtp_err_status_ok) {
  684. goto end;
  685. }
  686. // free_policies(policy_chain);
  687. // policy_chain = NULL;
  688. /* Don't check for NULL result -- no extractions is fine */
  689. remove_stream_ssrc =
  690. extract_remove_stream_ssrc(&data, &size, &num_remove_stream);
  691. /* Don't check for NULL result -- no extractions is fine */
  692. set_roc = extract_set_roc(&data, &size, &num_set_roc);
  693. {
  694. uint8_t *ret;
  695. int i = 0, j = 0;
  696. while ((ret = run_srtp_func(srtp_ctx, &data, &size)) != NULL) {
  697. fuzz_free(ret);
  698. /* Keep removing streams until the set of SSRCs extracted from the
  699. * fuzzer input is exhausted */
  700. if (i < num_remove_stream) {
  701. if (srtp_remove_stream(srtp_ctx, remove_stream_ssrc[i]) !=
  702. srtp_err_status_ok) {
  703. goto end;
  704. }
  705. i++;
  706. }
  707. /* Keep setting and getting ROCs until the set of SSRC/ROC tuples
  708. * extracted from the fuzzer input is exhausted */
  709. if (j < num_set_roc * 2) {
  710. uint32_t roc;
  711. if (srtp_set_stream_roc(srtp_ctx, set_roc[j], set_roc[j + 1]) !=
  712. srtp_err_status_ok) {
  713. goto end;
  714. }
  715. if (srtp_get_stream_roc(srtp_ctx, set_roc[j + 1], &roc) !=
  716. srtp_err_status_ok) {
  717. goto end;
  718. }
  719. j += 2;
  720. }
  721. if (policy_chain_2 != NULL) {
  722. /* TODO srtp_update(srtp_ctx, policy_chain_2); */
  723. /* Discard after using once */
  724. free_policies(policy_chain_2);
  725. policy_chain_2 = NULL;
  726. }
  727. }
  728. }
  729. end:
  730. free_policies(policy_chain);
  731. free_policies(policy_chain_2);
  732. fuzz_free(remove_stream_ssrc);
  733. fuzz_free(set_roc);
  734. if (srtp_ctx != NULL) {
  735. srtp_dealloc(srtp_ctx);
  736. }
  737. fuzz_mt19937_destroy();
  738. return 0;
  739. }