fuzz.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef DROPBEAR_FUZZ_H
  2. #define DROPBEAR_FUZZ_H
  3. #include "config.h"
  4. #if DROPBEAR_FUZZ
  5. #include "includes.h"
  6. #include "buffer.h"
  7. #include "algo.h"
  8. #include "netio.h"
  9. #include "fuzz-wrapfd.h"
  10. // once per process
  11. void fuzz_common_setup(void);
  12. void fuzz_svr_setup(void);
  13. void fuzz_cli_setup(void);
  14. // constructor attribute so it runs before main(), including
  15. // in non-fuzzing mode.
  16. void fuzz_early_setup(void) __attribute__((constructor));
  17. // must be called once per fuzz iteration.
  18. // returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE
  19. int fuzz_set_input(const uint8_t *Data, size_t Size);
  20. int fuzz_run_server(const uint8_t *Data, size_t Size, int skip_kexmaths, int postauth);
  21. int fuzz_run_client(const uint8_t *Data, size_t Size, int skip_kexmaths);
  22. const void* fuzz_get_algo(const algo_type *algos, const char* name);
  23. // fuzzer functions that intrude into general code
  24. void fuzz_kex_fakealgos(void);
  25. int fuzz_checkpubkey_line(buffer* line, int line_num, char* filename,
  26. const char* algo, unsigned int algolen,
  27. const unsigned char* keyblob, unsigned int keybloblen);
  28. extern const char * const * fuzz_signkey_names;
  29. void fuzz_seed(const unsigned char* dat, unsigned int len);
  30. void fuzz_svr_hook_preloop(void);
  31. int fuzz_dropbear_listen(const char* address, const char* port,
  32. int *socks, unsigned int sockcount, char **errstring, int *maxfd);
  33. // helpers
  34. void fuzz_get_socket_address(int fd, char **local_host, char **local_port,
  35. char **remote_host, char **remote_port, int host_lookup);
  36. void fuzz_fake_send_kexdh_reply(void);
  37. int fuzz_spawn_command(int *ret_writefd, int *ret_readfd, int *ret_errfd, pid_t *ret_pid);
  38. void fuzz_dump(const unsigned char* data, size_t len);
  39. // fake IO wrappers
  40. #ifndef FUZZ_SKIP_WRAP
  41. #define select(nfds, readfds, writefds, exceptfds, timeout) \
  42. wrapfd_select(nfds, readfds, writefds, exceptfds, timeout)
  43. #define write(fd, buf, count) wrapfd_write(fd, buf, count)
  44. #define read(fd, buf, count) wrapfd_read(fd, buf, count)
  45. #define close(fd) wrapfd_close(fd)
  46. #define kill(pid, sig) fuzz_kill(pid, sig)
  47. #endif // FUZZ_SKIP_WRAP
  48. struct dropbear_fuzz_options {
  49. int fuzzing;
  50. // fuzzing input
  51. buffer *input;
  52. struct dropbear_cipher recv_cipher;
  53. struct dropbear_hash recv_mac;
  54. int wrapfds;
  55. // whether to skip slow bignum maths
  56. int skip_kexmaths;
  57. // whether is svr_postauth mode
  58. int svr_postauth;
  59. // dropbear_exit() jumps back
  60. int do_jmp;
  61. sigjmp_buf jmp;
  62. // write out decrypted session data to this FD if it is set
  63. // flag - this needs to be set manually in cli-main.c etc
  64. int dumping;
  65. // the file descriptor
  66. int recv_dumpfd;
  67. // avoid filling fuzzing logs, this points to /dev/null
  68. FILE *fake_stderr;
  69. };
  70. extern struct dropbear_fuzz_options fuzz;
  71. /* guard for when fuzz.h is included by fuzz-common.c */
  72. #ifndef FUZZ_NO_REPLACE_STDERR
  73. /* This is a bodge but seems to work.
  74. glibc stdio.h has the comment
  75. "C89/C99 say they're macros. Make them happy." */
  76. /* OS X has it as a macro */
  77. #ifdef stderr
  78. #undef stderr
  79. #endif
  80. #define stderr (fuzz.fake_stderr)
  81. #endif /* FUZZ_NO_REPLACE_STDERR */
  82. struct passwd* fuzz_getpwuid(uid_t uid);
  83. struct passwd* fuzz_getpwnam(const char *login);
  84. /* guard for when fuzz.h is included by fuzz-common.c */
  85. #ifndef FUZZ_NO_REPLACE_GETPW
  86. #define getpwnam(x) fuzz_getpwnam(x)
  87. #define getpwuid(x) fuzz_getpwuid(x)
  88. #endif // FUZZ_NO_REPLACE_GETPW
  89. #endif /* DROPBEAR_FUZZ */
  90. #endif /* DROPBEAR_FUZZ_H */