fuzz-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #define FUZZ_NO_REPLACE_STDERR
  2. #define FUZZ_NO_REPLACE_GETPW
  3. #include "includes.h"
  4. #include "includes.h"
  5. #include "dbutil.h"
  6. #include "runopts.h"
  7. #include "crypto_desc.h"
  8. #include "session.h"
  9. #include "dbrandom.h"
  10. #include "bignum.h"
  11. #include "atomicio.h"
  12. #include "fuzz-wrapfd.h"
  13. #include "fuzz.h"
  14. struct dropbear_fuzz_options fuzz;
  15. static void fuzz_dropbear_log(int UNUSED(priority), const char* format, va_list param);
  16. static void load_fixed_hostkeys(void);
  17. static void load_fixed_client_key(void);
  18. // This runs automatically before main, due to contructor attribute in fuzz.h
  19. void fuzz_early_setup(void) {
  20. /* Set stderr to point to normal stderr by default */
  21. fuzz.fake_stderr = stderr;
  22. }
  23. void fuzz_common_setup(void) {
  24. disallow_core();
  25. fuzz.fuzzing = 1;
  26. fuzz.wrapfds = 1;
  27. fuzz.do_jmp = 1;
  28. fuzz.input = m_malloc(sizeof(buffer));
  29. _dropbear_log = fuzz_dropbear_log;
  30. crypto_init();
  31. fuzz_seed("start", 5);
  32. /* let any messages get flushed */
  33. setlinebuf(stdout);
  34. #if DEBUG_TRACE
  35. if (debug_trace)
  36. {
  37. fprintf(stderr, "Dropbear fuzzer: -v specified, not disabling stderr output\n");
  38. }
  39. else
  40. #endif
  41. if (getenv("DROPBEAR_KEEP_STDERR")) {
  42. fprintf(stderr, "Dropbear fuzzer: DROPBEAR_KEEP_STDERR, not disabling stderr output\n");
  43. }
  44. else
  45. {
  46. fprintf(stderr, "Dropbear fuzzer: Disabling stderr output\n");
  47. fuzz.fake_stderr = fopen("/dev/null", "w");
  48. assert(fuzz.fake_stderr);
  49. }
  50. }
  51. int fuzz_set_input(const uint8_t *Data, size_t Size) {
  52. fuzz.input->data = (unsigned char*)Data;
  53. fuzz.input->size = Size;
  54. fuzz.input->len = Size;
  55. fuzz.input->pos = 0;
  56. memset(&ses, 0x0, sizeof(ses));
  57. memset(&svr_ses, 0x0, sizeof(svr_ses));
  58. memset(&cli_ses, 0x0, sizeof(cli_ses));
  59. wrapfd_setup(fuzz.input);
  60. // printhex("input", fuzz.input->data, fuzz.input->len);
  61. fuzz_seed(fuzz.input->data, MIN(fuzz.input->len, 16));
  62. return DROPBEAR_SUCCESS;
  63. }
  64. #if DEBUG_TRACE
  65. static void fuzz_dropbear_log(int UNUSED(priority), const char* format, va_list param) {
  66. if (debug_trace) {
  67. char printbuf[1024];
  68. vsnprintf(printbuf, sizeof(printbuf), format, param);
  69. fprintf(stderr, "%s\n", printbuf);
  70. }
  71. }
  72. #else
  73. static void fuzz_dropbear_log(int UNUSED(priority), const char* UNUSED(format), va_list UNUSED(param)) {
  74. /* No print */
  75. }
  76. #endif /* DEBUG_TRACE */
  77. void fuzz_svr_setup(void) {
  78. fuzz_common_setup();
  79. _dropbear_exit = svr_dropbear_exit;
  80. char *argv[] = {
  81. "dropbear",
  82. "-E",
  83. };
  84. int argc = sizeof(argv) / sizeof(*argv);
  85. svr_getopts(argc, argv);
  86. load_fixed_hostkeys();
  87. }
  88. void fuzz_svr_hook_preloop() {
  89. if (fuzz.svr_postauth) {
  90. ses.authstate.authdone = 1;
  91. fill_passwd("root");
  92. }
  93. }
  94. void fuzz_cli_setup(void) {
  95. fuzz_common_setup();
  96. _dropbear_exit = cli_dropbear_exit;
  97. _dropbear_log = cli_dropbear_log;
  98. char *argv[] = {
  99. "dbclient",
  100. "-y",
  101. "localhost",
  102. "uptime"
  103. };
  104. int argc = sizeof(argv) / sizeof(*argv);
  105. cli_getopts(argc, argv);
  106. load_fixed_client_key();
  107. /* Avoid password prompt */
  108. setenv(DROPBEAR_PASSWORD_ENV, "password", 1);
  109. }
  110. #include "fuzz-hostkeys.c"
  111. static void load_fixed_client_key(void) {
  112. buffer *b = buf_new(3000);
  113. sign_key *key;
  114. enum signkey_type keytype;
  115. key = new_sign_key();
  116. keytype = DROPBEAR_SIGNKEY_ANY;
  117. buf_putbytes(b, keyed25519, keyed25519_len);
  118. buf_setpos(b, 0);
  119. if (buf_get_priv_key(b, key, &keytype) == DROPBEAR_FAILURE) {
  120. dropbear_exit("failed fixed ed25519 hostkey");
  121. }
  122. list_append(cli_opts.privkeys, key);
  123. buf_free(b);
  124. }
  125. static void load_fixed_hostkeys(void) {
  126. buffer *b = buf_new(3000);
  127. enum signkey_type type;
  128. TRACE(("load fixed hostkeys"))
  129. svr_opts.hostkey = new_sign_key();
  130. buf_setlen(b, 0);
  131. buf_putbytes(b, keyr, keyr_len);
  132. buf_setpos(b, 0);
  133. type = DROPBEAR_SIGNKEY_RSA;
  134. if (buf_get_priv_key(b, svr_opts.hostkey, &type) == DROPBEAR_FAILURE) {
  135. dropbear_exit("failed fixed rsa hostkey");
  136. }
  137. buf_setlen(b, 0);
  138. buf_putbytes(b, keyd, keyd_len);
  139. buf_setpos(b, 0);
  140. type = DROPBEAR_SIGNKEY_DSS;
  141. if (buf_get_priv_key(b, svr_opts.hostkey, &type) == DROPBEAR_FAILURE) {
  142. dropbear_exit("failed fixed dss hostkey");
  143. }
  144. buf_setlen(b, 0);
  145. buf_putbytes(b, keye, keye_len);
  146. buf_setpos(b, 0);
  147. type = DROPBEAR_SIGNKEY_ECDSA_NISTP256;
  148. if (buf_get_priv_key(b, svr_opts.hostkey, &type) == DROPBEAR_FAILURE) {
  149. dropbear_exit("failed fixed ecdsa hostkey");
  150. }
  151. buf_setlen(b, 0);
  152. buf_putbytes(b, keyed25519, keyed25519_len);
  153. buf_setpos(b, 0);
  154. type = DROPBEAR_SIGNKEY_ED25519;
  155. if (buf_get_priv_key(b, svr_opts.hostkey, &type) == DROPBEAR_FAILURE) {
  156. dropbear_exit("failed fixed ed25519 hostkey");
  157. }
  158. buf_free(b);
  159. }
  160. void fuzz_kex_fakealgos(void) {
  161. ses.newkeys->recv.crypt_mode = &dropbear_mode_none;
  162. ses.newkeys->recv.algo_mac = &dropbear_nohash;
  163. }
  164. void fuzz_get_socket_address(int UNUSED(fd), char **local_host, char **local_port,
  165. char **remote_host, char **remote_port, int UNUSED(host_lookup)) {
  166. if (local_host) {
  167. *local_host = m_strdup("fuzzlocalhost");
  168. }
  169. if (local_port) {
  170. *local_port = m_strdup("1234");
  171. }
  172. if (remote_host) {
  173. *remote_host = m_strdup("fuzzremotehost");
  174. }
  175. if (remote_port) {
  176. *remote_port = m_strdup("9876");
  177. }
  178. }
  179. /* cut down version of svr_send_msg_kexdh_reply() that skips slow maths. Still populates structures */
  180. void fuzz_fake_send_kexdh_reply(void) {
  181. assert(!ses.dh_K);
  182. m_mp_alloc_init_multi(&ses.dh_K, NULL);
  183. mp_set_ul(ses.dh_K, 12345678uL);
  184. finish_kexhashbuf();
  185. }
  186. /* fake version of spawn_command() */
  187. int fuzz_spawn_command(int *ret_writefd, int *ret_readfd, int *ret_errfd, pid_t *ret_pid) {
  188. *ret_writefd = wrapfd_new_dummy();
  189. *ret_readfd = wrapfd_new_dummy();
  190. if (ret_errfd) {
  191. *ret_errfd = wrapfd_new_dummy();
  192. }
  193. if (*ret_writefd == -1 || *ret_readfd == -1 || (ret_errfd && *ret_errfd == -1)) {
  194. m_close(*ret_writefd);
  195. m_close(*ret_readfd);
  196. if (ret_errfd) {
  197. m_close(*ret_errfd);
  198. }
  199. return DROPBEAR_FAILURE;
  200. } else {
  201. *ret_pid = 999;
  202. return DROPBEAR_SUCCESS;
  203. }
  204. }
  205. /* Fake dropbear_listen, always returns failure for now.
  206. TODO make it sometimes return success with wrapfd_new_dummy() sockets.
  207. Making the listeners fake a new incoming connection will be harder. */
  208. /* Listen on address:port.
  209. * Special cases are address of "" listening on everything,
  210. * and address of NULL listening on localhost only.
  211. * Returns the number of sockets bound on success, or -1 on failure. On
  212. * failure, if errstring wasn't NULL, it'll be a newly malloced error
  213. * string.*/
  214. int fuzz_dropbear_listen(const char* UNUSED(address), const char* UNUSED(port),
  215. int *UNUSED(socks), unsigned int UNUSED(sockcount), char **errstring, int *UNUSED(maxfd)) {
  216. if (errstring) {
  217. *errstring = m_strdup("fuzzing can't listen (yet)");
  218. }
  219. return -1;
  220. }
  221. int fuzz_run_server(const uint8_t *Data, size_t Size, int skip_kexmaths, int postauth) {
  222. static int once = 0;
  223. if (!once) {
  224. fuzz_svr_setup();
  225. fuzz.skip_kexmaths = skip_kexmaths;
  226. once = 1;
  227. }
  228. fuzz.svr_postauth = postauth;
  229. if (fuzz_set_input(Data, Size) == DROPBEAR_FAILURE) {
  230. return 0;
  231. }
  232. uint32_t wrapseed;
  233. genrandom((void*)&wrapseed, sizeof(wrapseed));
  234. wrapfd_setseed(wrapseed);
  235. int fakesock = wrapfd_new_fuzzinput();
  236. m_malloc_set_epoch(1);
  237. fuzz.do_jmp = 1;
  238. if (setjmp(fuzz.jmp) == 0) {
  239. svr_session(fakesock, fakesock);
  240. m_malloc_free_epoch(1, 0);
  241. } else {
  242. fuzz.do_jmp = 0;
  243. m_malloc_free_epoch(1, 1);
  244. TRACE(("dropbear_exit longjmped"))
  245. /* dropbear_exit jumped here */
  246. }
  247. return 0;
  248. }
  249. int fuzz_run_client(const uint8_t *Data, size_t Size, int skip_kexmaths) {
  250. static int once = 0;
  251. if (!once) {
  252. fuzz_cli_setup();
  253. fuzz.skip_kexmaths = skip_kexmaths;
  254. once = 1;
  255. }
  256. if (fuzz_set_input(Data, Size) == DROPBEAR_FAILURE) {
  257. return 0;
  258. }
  259. // Allow to proceed sooner
  260. ses.kexstate.donefirstkex = 1;
  261. uint32_t wrapseed;
  262. genrandom((void*)&wrapseed, sizeof(wrapseed));
  263. wrapfd_setseed(wrapseed);
  264. int fakesock = wrapfd_new_fuzzinput();
  265. m_malloc_set_epoch(1);
  266. fuzz.do_jmp = 1;
  267. if (setjmp(fuzz.jmp) == 0) {
  268. cli_session(fakesock, fakesock, NULL, 0);
  269. m_malloc_free_epoch(1, 0);
  270. } else {
  271. fuzz.do_jmp = 0;
  272. m_malloc_free_epoch(1, 1);
  273. TRACE(("dropbear_exit longjmped"))
  274. /* dropbear_exit jumped here */
  275. }
  276. return 0;
  277. }
  278. const void* fuzz_get_algo(const algo_type *algos, const char* name) {
  279. const algo_type *t;
  280. for (t = algos; t->name; t++) {
  281. if (strcmp(t->name, name) == 0) {
  282. return t->data;
  283. }
  284. }
  285. assert(0);
  286. }
  287. void fuzz_dump(const unsigned char* data, size_t len) {
  288. if (fuzz.dumping) {
  289. TRACE(("dump %zu", len))
  290. assert(atomicio(vwrite, fuzz.recv_dumpfd, (void*)data, len) == len);
  291. }
  292. }
  293. static struct passwd pwd_root = {
  294. .pw_name = "root",
  295. .pw_passwd = "!",
  296. .pw_uid = 0,
  297. .pw_gid = 0,
  298. .pw_dir = "/root",
  299. .pw_shell = "/bin/sh",
  300. };
  301. static struct passwd pwd_other = {
  302. .pw_name = "other",
  303. .pw_passwd = "!",
  304. .pw_uid = 100,
  305. .pw_gid = 100,
  306. .pw_dir = "/home/other",
  307. .pw_shell = "/bin/sh",
  308. };
  309. /* oss-fuzz runs fuzzers under minijail, without /etc/passwd.
  310. We provide sufficient values for the fuzzers to run */
  311. struct passwd* fuzz_getpwnam(const char *login) {
  312. if (!fuzz.fuzzing) {
  313. return getpwnam(login);
  314. }
  315. if (strcmp(login, pwd_other.pw_name) == 0) {
  316. return &pwd_other;
  317. }
  318. if (strcmp(login, pwd_root.pw_name) == 0) {
  319. return &pwd_root;
  320. }
  321. return NULL;
  322. }
  323. struct passwd* fuzz_getpwuid(uid_t uid) {
  324. if (!fuzz.fuzzing) {
  325. return getpwuid(uid);
  326. }
  327. if (uid == pwd_other.pw_uid) {
  328. return &pwd_other;
  329. }
  330. if (uid == pwd_root.pw_uid) {
  331. return &pwd_root;
  332. }
  333. return NULL;
  334. }