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 /* libFuzzer invokes this routine with fuzzed database files (in aData). 73 ** This routine run SQLite against the malformed database to see if it 74 ** can provoke a failure or malfunction. 75 */ 76 int LLVMFuzzerTestOneInput(const uint8_t *aData, size_t nByte){ 77 unsigned char *a; 78 sqlite3 *db; 79 int rc; 80 int i; 81 82 if( eVerbosity>=1 ){ 83 printf("************** nByte=%d ***************\n", (int)nByte); 84 fflush(stdout); 85 } 86 if( sqlite3_initialize() ) return 0; 87 rc = sqlite3_open(0, &db); 88 if( rc ) return 1; 89 a = sqlite3_malloc64(nByte+1); 90 if( a==0 ) return 1; 91 memcpy(a, aData, nByte); 92 sqlite3_deserialize(db, "main", a, nByte, nByte, 93 SQLITE_DESERIALIZE_RESIZEABLE | 94 SQLITE_DESERIALIZE_FREEONCLOSE); 95 if( bVdbeDebug ){ 96 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); 97 } 98 for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){ 99 if( eVerbosity>=1 ){ 100 printf("%s\n", azSql[i]); 101 fflush(stdout); 102 } 103 sqlite3_exec(db, azSql[i], 0, 0, 0); 104 } 105 rc = sqlite3_close(db); 106 if( rc!=SQLITE_OK ){ 107 fprintf(stdout, "sqlite3_close() returns %d\n", rc); 108 } 109 if( sqlite3_memory_used()!=0 ){ 110 int nAlloc = 0; 111 int nNotUsed = 0; 112 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); 113 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", 114 sqlite3_memory_used(), nAlloc); 115 exit(1); 116 } 117 return 0; 118 } 119 120 /* 121 ** Return the number of "v" characters in a string. Return 0 if there 122 ** are any characters in the string other than "v". 123 */ 124 static int numberOfVChar(const char *z){ 125 int N = 0; 126 while( z[0] && z[0]=='v' ){ 127 z++; 128 N++; 129 } 130 return z[0]==0 ? N : 0; 131 } 132 133 /* libFuzzer invokes this routine once when the executable starts, to 134 ** process the command-line arguments. 135 */ 136 int LLVMFuzzerInitialize(int *pArgc, char ***pArgv){ 137 int i, j, n; 138 int argc = *pArgc; 139 char **argv = *pArgv; 140 for(i=j=1; i<argc; i++){ 141 char *z = argv[i]; 142 if( z[0]=='-' ){ 143 z++; 144 if( z[0]=='-' ) z++; 145 if( z[0]=='v' && (n = numberOfVChar(z))>0 ){ 146 eVerbosity += n; 147 continue; 148 } 149 if( strcmp(z,"vdbe-debug")==0 ){ 150 bVdbeDebug = 1; 151 continue; 152 } 153 if( strcmp(z,"max-stack")==0 154 || strcmp(z,"max-data")==0 155 || strcmp(z,"max-as")==0 156 ){ 157 struct rlimit x,y; 158 int resource = RLIMIT_STACK; 159 char *zType = "RLIMIT_STACK"; 160 if( i+1==argc ){ 161 fprintf(stderr, "missing argument to %s\n", argv[i]); 162 exit(1); 163 } 164 if( z[4]=='d' ){ 165 resource = RLIMIT_DATA; 166 zType = "RLIMIT_DATA"; 167 } 168 if( z[4]=='a' ){ 169 resource = RLIMIT_AS; 170 zType = "RLIMIT_AS"; 171 } 172 memset(&x,0,sizeof(x)); 173 getrlimit(resource, &x); 174 y.rlim_cur = atoi(argv[++i]); 175 y.rlim_max = x.rlim_cur; 176 setrlimit(resource, &y); 177 memset(&y,0,sizeof(y)); 178 getrlimit(resource, &y); 179 printf("%s changed from %d to %d\n", 180 zType, (int)x.rlim_cur, (int)y.rlim_cur); 181 continue; 182 } 183 } 184 argv[j++] = argv[i]; 185 } 186 argv[j] = 0; 187 *pArgc = j; 188 return 0; 189 } 190 191 #ifdef STANDALONE 192 /* 193 ** Read an entire file into memory. Space to hold the file comes 194 ** from malloc(). 195 */ 196 static unsigned char *readFile(const char *zName, int *pnByte){ 197 FILE *in = fopen(zName, "rb"); 198 long nIn; 199 size_t nRead; 200 unsigned char *pBuf; 201 if( in==0 ) return 0; 202 fseek(in, 0, SEEK_END); 203 nIn = ftell(in); 204 rewind(in); 205 pBuf = malloc( nIn+1 ); 206 if( pBuf==0 ){ fclose(in); return 0; } 207 nRead = fread(pBuf, nIn, 1, in); 208 fclose(in); 209 if( nRead!=1 ){ 210 free(pBuf); 211 return 0; 212 } 213 pBuf[nIn] = 0; 214 if( pnByte ) *pnByte = nIn; 215 return pBuf; 216 } 217 #endif /* STANDALONE */ 218 219 #ifdef STANDALONE 220 int main(int argc, char **argv){ 221 int i; 222 LLVMFuzzerInitialize(&argc, &argv); 223 for(i=1; i<argc; i++){ 224 unsigned char *pIn; 225 int nIn; 226 pIn = readFile(argv[i], &nIn); 227 if( pIn ){ 228 LLVMFuzzerTestOneInput((const uint8_t*)pIn, (size_t)nIn); 229 free(pIn); 230 } 231 } 232 if( eVerbosity>0 ){ 233 struct rusage x; 234 printf("SQLite %s\n", sqlite3_sourceid()); 235 memset(&x, 0, sizeof(x)); 236 if( getrusage(RUSAGE_SELF, &x)==0 ){ 237 printf("Maximum RSS = %ld KB\n", x.ru_maxrss); 238 } 239 } 240 return 0; 241 } 242 #endif /*STANDALONE*/ 243