1 /* 2 ** 2018-10-26 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This program is designed for fuzz-testing SQLite database files using 14 ** the -fsanitize=fuzzer option of clang. 15 ** 16 ** The -fsanitize=fuzzer option causes a main() to be inserted automatically. 17 ** That main() invokes LLVMFuzzerTestOneInput(D,S) to be invoked repeatedly. 18 ** Each D is a fuzzed database file. The code in this file runs various 19 ** SQL statements against that database, trying to provoke a failure. 20 ** 21 ** For best results the seed database files should have these tables: 22 ** 23 ** Table "t1" with columns "a" and "b" 24 ** Tables "t2" and "t3 with the same number of compatible columns 25 ** "t3" should have a column names "x" 26 ** Table "t4" with a column "x" that is compatible with t3.x. 27 ** 28 ** Any of these tables can be virtual tables, for example FTS or RTree tables. 29 ** 30 ** To run this test: 31 ** 32 ** mkdir dir 33 ** cp dbfuzz2-seed*.db dir 34 ** clang-6.0 -I. -g -O1 -fsanitize=fuzzer \ 35 ** -DTHREADSAFE=0 -DSQLITE_ENABLE_DESERIALIZE \ 36 ** -DSQLITE_ENABLE_DBSTAT_VTAB dbfuzz2.c sqlite3.c -ldl 37 ** ./a.out dir 38 */ 39 #include <assert.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <stdarg.h> 44 #include <ctype.h> 45 #include <stdint.h> 46 #include <sys/time.h> 47 #include <sys/resource.h> 48 #include "sqlite3.h" 49 50 /* 51 ** This is the is the SQL that is run against the database. 52 */ 53 static const char *azSql[] = { 54 "PRAGMA integrity_check;", 55 "SELECT * FROM sqlite_master;", 56 "SELECT sum(length(name)) FROM dbstat;", 57 "UPDATE t1 SET b=a, a=b WHERE a<b;", 58 "ALTER TABLE t1 RENAME TO alkjalkjdfiiiwuer987lkjwer82mx97sf98788s9789s;", 59 "INSERT INTO t3 SELECT * FROM t2;", 60 "DELETE FROM t3 WHERE x IN (SELECT x FROM t4);", 61 "REINDEX;", 62 "DROP TABLE t3;", 63 "VACUUM;", 64 }; 65 66 /* Output verbosity level. 0 means complete silence */ 67 int eVerbosity = 0; 68 69 /* True to activate PRAGMA vdbe_debug=on */ 70 static int bVdbeDebug = 0; 71 72 /* Maximum size of the in-memory database file */ 73 static sqlite3_int64 szMax = 104857600; 74 75 /* libFuzzer invokes this routine with fuzzed database files (in aData). 76 ** This routine run SQLite against the malformed database to see if it 77 ** can provoke a failure or malfunction. 78 */ 79 int LLVMFuzzerTestOneInput(const uint8_t *aData, size_t nByte){ 80 unsigned char *a; 81 sqlite3 *db; 82 int rc; 83 int i; 84 sqlite3_int64 x; 85 char *zErr = 0; 86 87 if( eVerbosity>=1 ){ 88 printf("************** nByte=%d ***************\n", (int)nByte); 89 fflush(stdout); 90 } 91 if( sqlite3_initialize() ) return 0; 92 rc = sqlite3_open(0, &db); 93 if( rc ) return 1; 94 a = sqlite3_malloc64(nByte+1); 95 if( a==0 ) return 1; 96 memcpy(a, aData, nByte); 97 sqlite3_deserialize(db, "main", a, nByte, nByte, 98 SQLITE_DESERIALIZE_RESIZEABLE | 99 SQLITE_DESERIALIZE_FREEONCLOSE); 100 x = szMax; 101 sqlite3_file_control(db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x); 102 if( bVdbeDebug ){ 103 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); 104 } 105 for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){ 106 if( eVerbosity>=1 ){ 107 printf("%s\n", azSql[i]); 108 fflush(stdout); 109 } 110 zErr = 0; 111 rc = sqlite3_exec(db, azSql[i], 0, 0, &zErr); 112 if( rc && eVerbosity>=1 ){ 113 printf("-- rc=%d zErr=%s\n", rc, zErr); 114 } 115 sqlite3_free(zErr); 116 } 117 rc = sqlite3_close(db); 118 if( rc!=SQLITE_OK ){ 119 fprintf(stdout, "sqlite3_close() returns %d\n", rc); 120 } 121 if( sqlite3_memory_used()!=0 ){ 122 int nAlloc = 0; 123 int nNotUsed = 0; 124 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); 125 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", 126 sqlite3_memory_used(), nAlloc); 127 exit(1); 128 } 129 return 0; 130 } 131 132 /* 133 ** Return the number of "v" characters in a string. Return 0 if there 134 ** are any characters in the string other than "v". 135 */ 136 static int numberOfVChar(const char *z){ 137 int N = 0; 138 while( z[0] && z[0]=='v' ){ 139 z++; 140 N++; 141 } 142 return z[0]==0 ? N : 0; 143 } 144 145 /* libFuzzer invokes this routine once when the executable starts, to 146 ** process the command-line arguments. 147 */ 148 int LLVMFuzzerInitialize(int *pArgc, char ***pArgv){ 149 int i, j, n; 150 int argc = *pArgc; 151 char **argv = *pArgv; 152 for(i=j=1; i<argc; i++){ 153 char *z = argv[i]; 154 if( z[0]=='-' ){ 155 z++; 156 if( z[0]=='-' ) z++; 157 if( z[0]=='v' && (n = numberOfVChar(z))>0 ){ 158 eVerbosity += n; 159 continue; 160 } 161 if( strcmp(z,"vdbe-debug")==0 ){ 162 bVdbeDebug = 1; 163 continue; 164 } 165 if( strcmp(z,"max-db-size")==0 ){ 166 if( i+1==argc ){ 167 fprintf(stderr, "missing argument to %s\n", argv[i]); 168 exit(1); 169 } 170 szMax = strtol(argv[++i], 0, 0); 171 continue; 172 } 173 if( strcmp(z,"max-stack")==0 174 || strcmp(z,"max-data")==0 175 || strcmp(z,"max-as")==0 176 ){ 177 struct rlimit x,y; 178 int resource = RLIMIT_STACK; 179 char *zType = "RLIMIT_STACK"; 180 if( i+1==argc ){ 181 fprintf(stderr, "missing argument to %s\n", argv[i]); 182 exit(1); 183 } 184 if( z[4]=='d' ){ 185 resource = RLIMIT_DATA; 186 zType = "RLIMIT_DATA"; 187 } 188 if( z[4]=='a' ){ 189 resource = RLIMIT_AS; 190 zType = "RLIMIT_AS"; 191 } 192 memset(&x,0,sizeof(x)); 193 getrlimit(resource, &x); 194 y.rlim_cur = atoi(argv[++i]); 195 y.rlim_max = x.rlim_cur; 196 setrlimit(resource, &y); 197 memset(&y,0,sizeof(y)); 198 getrlimit(resource, &y); 199 printf("%s changed from %d to %d\n", 200 zType, (int)x.rlim_cur, (int)y.rlim_cur); 201 continue; 202 } 203 } 204 argv[j++] = argv[i]; 205 } 206 argv[j] = 0; 207 *pArgc = j; 208 return 0; 209 } 210 211 #ifdef STANDALONE 212 /* 213 ** Read an entire file into memory. Space to hold the file comes 214 ** from malloc(). 215 */ 216 static unsigned char *readFile(const char *zName, int *pnByte){ 217 FILE *in = fopen(zName, "rb"); 218 long nIn; 219 size_t nRead; 220 unsigned char *pBuf; 221 if( in==0 ) return 0; 222 fseek(in, 0, SEEK_END); 223 nIn = ftell(in); 224 rewind(in); 225 pBuf = malloc( nIn+1 ); 226 if( pBuf==0 ){ fclose(in); return 0; } 227 nRead = fread(pBuf, nIn, 1, in); 228 fclose(in); 229 if( nRead!=1 ){ 230 free(pBuf); 231 return 0; 232 } 233 pBuf[nIn] = 0; 234 if( pnByte ) *pnByte = nIn; 235 return pBuf; 236 } 237 #endif /* STANDALONE */ 238 239 #ifdef STANDALONE 240 int main(int argc, char **argv){ 241 int i; 242 LLVMFuzzerInitialize(&argc, &argv); 243 for(i=1; i<argc; i++){ 244 unsigned char *pIn; 245 int nIn; 246 pIn = readFile(argv[i], &nIn); 247 if( pIn ){ 248 LLVMFuzzerTestOneInput((const uint8_t*)pIn, (size_t)nIn); 249 free(pIn); 250 } 251 } 252 if( eVerbosity>0 ){ 253 struct rusage x; 254 printf("SQLite %s\n", sqlite3_sourceid()); 255 memset(&x, 0, sizeof(x)); 256 if( getrusage(RUSAGE_SELF, &x)==0 ){ 257 printf("Maximum RSS = %ld KB\n", x.ru_maxrss); 258 } 259 } 260 return 0; 261 } 262 #endif /*STANDALONE*/ 263