fuzzer-kexecdh.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "fuzz.h"
  2. #include "session.h"
  3. #include "fuzz-wrapfd.h"
  4. #include "debug.h"
  5. #include "runopts.h"
  6. #include "algo.h"
  7. #include "bignum.h"
  8. static const struct dropbear_kex *ecdh[3]; /* 256, 384, 521 */
  9. static struct key_context* keep_newkeys = NULL;
  10. /* number of generated parameters. An arbitrary limit, but will delay startup */
  11. #define NUM_PARAMS 80
  12. static struct kex_ecdh_param *ecdh_params[NUM_PARAMS];
  13. static void setup() __attribute__((constructor));
  14. // Perform initial setup here to avoid hitting timeouts on first run
  15. static void setup() {
  16. fuzz_common_setup();
  17. fuzz_svr_setup();
  18. /* ses gets zeroed by fuzz_set_input */
  19. keep_newkeys = (struct key_context*)m_malloc(sizeof(struct key_context));
  20. ecdh[0] = fuzz_get_algo(sshkex, "ecdh-sha2-nistp256");
  21. ecdh[1] = fuzz_get_algo(sshkex, "ecdh-sha2-nistp384");
  22. ecdh[2] = fuzz_get_algo(sshkex, "ecdh-sha2-nistp521");
  23. assert(ecdh[0]);
  24. assert(ecdh[1]);
  25. assert(ecdh[2]);
  26. keep_newkeys->algo_hostkey = DROPBEAR_SIGNKEY_ECDSA_NISTP256;
  27. ses.newkeys = keep_newkeys;
  28. /* Pre-generate parameters */
  29. int i;
  30. for (i = 0; i < NUM_PARAMS; i++) {
  31. ses.newkeys->algo_kex = ecdh[i % 3];
  32. ecdh_params[i] = gen_kexecdh_param();
  33. }
  34. }
  35. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  36. if (fuzz_set_input(Data, Size) == DROPBEAR_FAILURE) {
  37. return 0;
  38. }
  39. m_malloc_set_epoch(1);
  40. if (setjmp(fuzz.jmp) == 0) {
  41. /* Based on recv_msg_kexdh_init()/send_msg_kexdh_reply()
  42. with DROPBEAR_KEX_ECDH */
  43. ses.newkeys = keep_newkeys;
  44. /* random choice of ecdh 256, 384, 521 */
  45. unsigned char b = buf_getbyte(fuzz.input);
  46. ses.newkeys->algo_kex = ecdh[b % 3];
  47. /* Choose from the collection of ecdh params */
  48. unsigned int e = buf_getint(fuzz.input);
  49. struct kex_ecdh_param *ecdh_param = ecdh_params[e % NUM_PARAMS];
  50. buffer * ecdh_qs = buf_getstringbuf(fuzz.input);
  51. ses.kexhashbuf = buf_new(KEXHASHBUF_MAX_INTS);
  52. kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
  53. mp_clear(ses.dh_K);
  54. m_free(ses.dh_K);
  55. buf_free(ecdh_qs);
  56. buf_free(ses.hash);
  57. buf_free(ses.session_id);
  58. /* kexhashbuf is freed in kexdh_comb_key */
  59. m_malloc_free_epoch(1, 0);
  60. } else {
  61. m_malloc_free_epoch(1, 1);
  62. TRACE(("dropbear_exit longjmped"))
  63. /* dropbear_exit jumped here */
  64. }
  65. return 0;
  66. }