fuzzer-kexdh.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 struct key_context* keep_newkeys = NULL;
  9. #define NUM_PARAMS 80
  10. static struct kex_dh_param *dh_params[NUM_PARAMS];
  11. static void setup() __attribute__((constructor));
  12. // Perform initial setup here to avoid hitting timeouts on first run
  13. static void setup() {
  14. fuzz_common_setup();
  15. fuzz_svr_setup();
  16. keep_newkeys = (struct key_context*)m_malloc(sizeof(struct key_context));
  17. keep_newkeys->algo_kex = fuzz_get_algo(sshkex, "diffie-hellman-group14-sha256");
  18. keep_newkeys->algo_hostkey = DROPBEAR_SIGNKEY_ECDSA_NISTP256;
  19. ses.newkeys = keep_newkeys;
  20. /* Pre-generate parameters */
  21. int i;
  22. for (i = 0; i < NUM_PARAMS; i++) {
  23. dh_params[i] = gen_kexdh_param();
  24. }
  25. }
  26. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  27. if (fuzz_set_input(Data, Size) == DROPBEAR_FAILURE) {
  28. return 0;
  29. }
  30. m_malloc_set_epoch(1);
  31. if (setjmp(fuzz.jmp) == 0) {
  32. /* Based on recv_msg_kexdh_init()/send_msg_kexdh_reply()
  33. with DROPBEAR_KEX_NORMAL_DH */
  34. ses.newkeys = keep_newkeys;
  35. /* Choose from the collection of ecdh params */
  36. unsigned int e = buf_getint(fuzz.input);
  37. struct kex_dh_param * dh_param = dh_params[e % NUM_PARAMS];
  38. DEF_MP_INT(dh_e);
  39. m_mp_init(&dh_e);
  40. if (buf_getmpint(fuzz.input, &dh_e) != DROPBEAR_SUCCESS) {
  41. dropbear_exit("Bad kex value");
  42. }
  43. ses.kexhashbuf = buf_new(KEXHASHBUF_MAX_INTS);
  44. kexdh_comb_key(dh_param, &dh_e, svr_opts.hostkey);
  45. mp_clear(ses.dh_K);
  46. m_free(ses.dh_K);
  47. mp_clear(&dh_e);
  48. buf_free(ses.hash);
  49. buf_free(ses.session_id);
  50. /* kexhashbuf is freed in kexdh_comb_key */
  51. m_malloc_free_epoch(1, 0);
  52. } else {
  53. m_malloc_free_epoch(1, 1);
  54. TRACE(("dropbear_exit longjmped"))
  55. /* dropbear_exit jumped here */
  56. }
  57. return 0;
  58. }