1 //#include <stdint.h>
2 //#include <stdlib.h>
3 //#include <stdio.h>
4 //#include <string>
5 //#include <iostream>
6 //#include <mysql.h>
7 //#include <mysql/client_plugin.h>
8 //#include <mysqld_error.h>
9 #include "sql/sql_class.h"
10 #include "sql/conn_handler/channel_info.h"
11 #include "sql/conn_handler/connection_handler.h"
12 #include "sql/conn_handler/connection_handler_manager.h"
13 #include "sql/conn_handler/init_net_server_extension.h"
14 #include "sql/conn_handler/connection_handler_impl.h"
15 #include "sql/mysqld.h"
16 #include "sql/set_var.h"
17 #include "sql/rpl_handler.h"
18 #include "sql/log.h"
19 #include "sql/opt_costconstantcache.h"
20 #include "sql/sql_plugin.h"
21 #include "sql/sql_thd_internal_api.h"
22 #include "sql/mysqld_thd_manager.h"
23 #include "sql/bootstrap.h"
24 #include "mysql/psi/mysql_socket.h"
25 #include "mysql/psi/mysql_file.h"
26 #include "violite.h"
27 #include "util_fuzz.h"
28 #include <stdlib.h>
29 #include <libgen.h>
30 
31 using namespace std;
32 FILE *logfile = NULL;
33 extern int mysqld_main(int argc, char **argv);
34 char *filepath = NULL;
35 
LLVMFuzzerInitialize(const int * argc,char *** argv)36 extern "C" int LLVMFuzzerInitialize(const int* argc, char*** argv) {
37     filepath = dirname(strdup((*argv)[0]));
38     return 0;
39 }
40 
bufferToFile(const char * name,const uint8_t * Data,size_t Size)41 static int bufferToFile(const char * name, const uint8_t *Data, size_t Size) {
42     FILE * fd;
43     if (remove(name) != 0) {
44         if (errno != ENOENT) {
45             printf("failed remove, errno=%d\n", errno);
46             return -1;
47         }
48     }
49     fd = fopen(name, "wb");
50     if (fd == NULL) {
51         printf("failed open, errno=%d\n", errno);
52         return -2;
53     }
54     if (fwrite (Data, 1, Size, fd) != Size) {
55         fclose(fd);
56         return -3;
57     }
58     fclose(fd);
59     return 0;
60 }
61 
62 #define MAX_SIZE 256
63 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
65     if (Size < 1) {
66         return 0;
67     }
68     if (logfile == NULL) {
69         my_progname = "fuzz_initfile";
70         /* first init was run with
71          * mysqld --user=root --initialize-insecure --log-error-verbosity=5 --datadir=/out/mysql/data/ --basedir=/out/mysql/
72          */
73         utilfuzz_rmrf("/tmp/mysql_initfile");
74         char command[MAX_SIZE];
75         char argbase[MAX_SIZE];
76         char arginitfile[MAX_SIZE];
77 
78         snprintf(command, MAX_SIZE-1, "%s/mysql/data", filepath);
79         utilfuzz_cpr(command, "/tmp/mysql_initfile");
80 
81         snprintf(argbase, MAX_SIZE-1, "--basedir=%s/mysql/", filepath);
82         snprintf(arginitfile, MAX_SIZE-1, "--init-file=%s/initnopw.sql", filepath);
83 
84         char *fakeargv[] = {const_cast<char *>("fuzz_initfile"),
85             const_cast<char *>("--user=root"),
86             const_cast<char *>("--secure-file-priv=NULL"),
87             const_cast<char *>("--log-error-verbosity=5"),
88             const_cast<char *>("--explicit_defaults_for_timestamp"),
89             //we should adapt vio_fuzz to give a socket to openssl in order to support ssl
90             const_cast<char *>("--skip-ssl"),
91             const_cast<char *>("--mysqlx=0"),
92             const_cast<char *>("--event-scheduler=DISABLED"),
93             const_cast<char *>("--performance_schema=OFF"),
94             const_cast<char *>("--thread_stack=1048576"),
95             const_cast<char *>("--datadir=/tmp/mysql_initfile/"),
96             const_cast<char *>("--port=3302"),
97             const_cast<char *>("--socket=/tmp/initfile.sock"),
98             const_cast<char *>(argbase),
99             const_cast<char *>(arginitfile),
100             0};
101         int fakeargc = 15;
102         mysqld_main(fakeargc, fakeargv);
103         //terminate_compress_gtid_table_thread();
104 
105         logfile = fopen("/dev/null", "w");
106     }
107 
108     bufferToFile("/tmp/initfuzz.sql", Data, Size);
109     MYSQL_FILE *file;
110     if (!(file =
111           mysql_file_fopen(key_file_init, "/tmp/initfuzz.sql", O_RDONLY, MYF(MY_WME)))) {
112         abort();
113     }
114     (void)bootstrap::run_bootstrap_thread("/tmp/initfuzz.sql", file, NULL, SYSTEM_THREAD_INIT_FILE);
115     mysql_file_fclose(file, MYF(MY_WME));
116 
117     return 0;
118 }
119