Repository navigation
Expand file tree
/
Copy pathscript.c
More file actions
1191 lines (1118 loc) · 45 KB
/
Copy pathscript.c
File metadata and controls
1191 lines (1118 loc) · 45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Interprets fuzzer bytes as SQLCipher operations: keys, codec pragmas, writes, rekeys, exports and file damage. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "confidentiality.h"
#include "fault.h"
#include "known.h"
#include "libstate.h"
#include "memvfs.h"
#include "model.h"
#include "script.h"
#include "tamper.h"
#include "uniqueness.h"
#define MAX_OPS 64
#define MAX_RECIPE 16
#define MAX_BLOB 20000
#define MAX_RAW_SQL 2048
#define MAX_DUMP (1 << 20)
#define MAX_DUMP_TABLES 8
/* Progress callbacks, each every 100 VDBE ops, before a run or a read-back is interrupted. */
#define SCRIPT_BUDGET 20000
#define DUMP_BUDGET 20000
static const char *const files[SCRIPT_FILES] = {"main.db", "other.db", "third.db"};
/* Values are permanent so saved inputs keep their meaning. Append new ops, never renumber. */
/* A selector byte that masks to an unassigned value is a no-op. */
#define OP_MASK 0x1f
enum op {
OP_OPEN = 0,
OP_REOPEN = 1,
OP_CLOSE = 2,
OP_KEY = 3,
OP_REKEY = 4,
OP_SETTING = 5,
OP_STATEMENT = 6,
OP_PRAGMA = 7,
OP_ATTACH = 8,
OP_BACKUP = 9,
OP_DAMAGE = 10,
OP_READ = 11,
OP_CACHE_RACE = 12,
OP_RAW_SQL = 13,
OP_CANARY = 14,
OP_FAULT_TXN = 15,
OP_COUNT = 16,
};
enum step_kind { STEP_KEY, STEP_SETTING };
/* A step is the input slice its operation parsed, so replaying it rebuilds the same key or pragma. */
struct step {
enum step_kind kind;
struct input slice;
/* Nonzero for a rekey, which keeps the connection's kdf_iter while the key it replays as sets its own. */
int kdf_iter;
};
struct recipe {
struct step steps[MAX_RECIPE];
int count;
/* Set once a passphrase key is recorded, since PBKDF2 then runs at the file's kdf_iter. */
int passphrase;
/* Set once a step did not fit, so replay can no longer rebuild the connection that wrote the file. */
int truncated;
};
struct key {
unsigned char bytes[128];
int len;
int passphrase;
int kdf_iter;
/* Raw hex keys only. Runs cipher_migrate right after the key, before any page access. */
int migrate;
};
static const struct fuzz_sqlite *lib;
/* SQLCipher major version of lib, since 5.x drops and adds codec pragmas. */
static int major;
/* The major version inputs parse as, which a plain SQLite run borrows from the SQLCipher run it is compared with. */
static int parse_major;
static const struct fuzz_sqlite *parse_like;
static sqlite3 *db;
static int db_file = -1;
/* Set once the open connection has read or written its file, which ends the window for a first key. */
static int touched;
/* cipher_default_page_size of lib, which a new ATTACH keys its file with. */
static int default_page;
static int trusted;
static long budget;
static struct recipe recipes[SCRIPT_FILES];
/* Files whose content plain SQLite cannot reproduce, which differential_plain leaves uncompared. */
static int plain_exempt[SCRIPT_FILES];
/* Files raw damage hit, which can differ between the two runs since their memvfs slots differ. */
static int damaged[SCRIPT_FILES];
static unsigned u8(struct input *in) {
if (!in->size) return 0;
in->size--;
return *in->data++;
}
static unsigned u16(struct input *in) {
unsigned low = u8(in);
return low | u8(in) << 8;
}
static unsigned long u32(struct input *in) {
unsigned long low = u16(in);
return low | (unsigned long)u16(in) << 16;
}
static struct input take(struct input *in, size_t max) {
size_t len = u16(in) % (max + 1);
if (len > in->size) len = in->size;
struct input out = {in->data, len};
in->data += len;
in->size -= len;
return out;
}
static void dump_put(struct dump *out, const void *bytes, size_t len) {
if (!out) return;
fuzz_grow_append(&out->bytes, &out->len, &out->cap, MAX_DUMP, bytes, len);
}
static void dump_code(struct dump *out, int rc) {
unsigned char code = (unsigned char)rc;
dump_put(out, &code, 1);
}
void dump_free(struct dump *out) {
free(out->bytes);
memset(out, 0, sizeof *out);
}
/* Pragmas the input may not reach: PBKDF2 cost, disk output, and process-wide state no reset can undo. */
static int authorize(void *unused, int action, const char *name, const char *value, const char *schema,
const char *trigger) {
static const char *const denied[] = {
"key", "rekey", "hexkey", "hexrekey",
"textkey", "textrekey", "kdf_iter", "fast_kdf_iter",
"rekey_kdf_iter", "cipher_default_kdf_iter", "cipher_compatibility",
"cipher_default_compatibility", "cipher_migrate", "cipher_memory_security",
"cipher_hmac_salt_mask", "cipher_profile", "cipher_log", "cipher_log_level", "cipher_log_source",
};
(void)unused;
(void)schema;
(void)trigger;
if (trusted || !name) return SQLITE_OK;
/* L8: an attached plaintext-header file read with a page size other than the one its header states. */
if (action == SQLITE_ATTACH)
return known_header_page_mismatch(major, memvfs_header_page_size(name), default_page) ? SQLITE_DENY : SQLITE_OK;
if (action != SQLITE_PRAGMA) return SQLITE_OK;
for (size_t i = 0; i < sizeof denied / sizeof *denied; i++) {
if (strcasecmp(name, denied[i]) == 0) return SQLITE_DENY;
}
if (known_denied_pragma(major, name, value)) return SQLITE_DENY;
if (touched && value && known_late_setting(name)) return SQLITE_DENY; /* L6 */
return SQLITE_OK;
}
static int progress(void *unused) {
(void)unused;
return --budget < 0;
}
static sqlite3 *open_file(int file, int create) {
sqlite3 *handle = NULL;
int flags = SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0);
if (lib->open_v2(files[file], &handle, flags, NULL) != SQLITE_OK) {
lib->close_v2(handle);
return NULL;
}
lib->set_authorizer(handle, authorize, NULL);
lib->progress_handler(handle, 100, progress, NULL);
lib->limit(handle, SQLITE_LIMIT_LENGTH, 1 << 20);
lib->limit(handle, SQLITE_LIMIT_SQL_LENGTH, 1 << 16);
lib->limit(handle, SQLITE_LIMIT_VDBE_OP, 25000);
return handle;
}
static void close_db(void) {
if (db) {
uniqueness_check(lib, db, "main", db_file, files[db_file]);
lib->close_v2(db);
}
db = NULL;
db_file = -1;
touched = 0;
}
static void dump_row(sqlite3_stmt *stmt, struct dump *out) {
int columns = lib->column_count(stmt);
for (int i = 0; i < columns; i++) {
unsigned char type = (unsigned char)lib->column_type(stmt, i);
dump_put(out, &type, 1);
if (type == SQLITE_INTEGER) {
sqlite3_int64 value = lib->column_int64(stmt, i);
dump_put(out, &value, sizeof value);
} else if (type == SQLITE_FLOAT) {
double value = lib->column_double(stmt, i);
dump_put(out, &value, sizeof value);
} else if (type != SQLITE_NULL) {
const void *bytes = lib->column_blob(stmt, i);
int len = lib->column_bytes(stmt, i);
dump_put(out, &len, sizeof len);
if (bytes) dump_put(out, bytes, (size_t)len);
}
}
}
static int drain(sqlite3_stmt *stmt, struct dump *out) {
int rc;
while ((rc = lib->step(stmt)) == SQLITE_ROW) dump_row(stmt, out);
lib->finalize(stmt);
return rc == SQLITE_DONE ? SQLITE_OK : rc;
}
/* A read of schema's page 1, returning the step's result code, which fails when the key does not match the file. */
static int key_verified(sqlite3 *handle, const char *schema) {
char sql[64];
snprintf(sql, sizeof sql, "PRAGMA \"%s\".schema_version", schema);
sqlite3_stmt *stmt = NULL;
int rc = lib->prepare_v2(handle, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) return rc;
rc = lib->step(stmt);
lib->finalize(stmt);
return rc;
}
/* Whether file has a non-empty journal or WAL, which any first page access rolls into it whatever the key. */
static int has_pending_recovery(int file) {
char name[64];
size_t len = 0;
snprintf(name, sizeof name, "%s-journal", files[file]);
memvfs_peek(name, &len);
if (len) return 1;
snprintf(name, sizeof name, "%s-wal", files[file]);
memvfs_peek(name, &len);
return len != 0;
}
/* Reads through the key just applied on schema, checks a mismatch left file's bytes untouched, and marks a match. */
static void verify_key(sqlite3 *handle, const char *schema, int file, int key_len) {
unsigned long long before = tamper_hash_file(files[file]);
int recovery = has_pending_recovery(file);
int rc = key_verified(handle, schema);
if (!recovery) tamper_check_wrong_key(rc, before, files[file]);
if (key_len > 0 && (rc == SQLITE_ROW || rc == SQLITE_DONE)) confidentiality_mark_keyed(file);
/* The codec connection cannot read or write a file its key does not open, while plain SQLite can. */
if (rc != SQLITE_ROW && rc != SQLITE_DONE) plain_exempt[file] = 1;
}
/* L1: VACUUM INTO, and VACUUM with a file temp store, copy pages into a database keyed with the default page size. */
static int vacuum_blocked(sqlite3 *handle, int vacuum) {
if (!vacuum || (vacuum == 1 && lib_int(lib, handle, "PRAGMA temp_store") != 1)) return 0;
return known_backup_blocked(major, fuzz_codec_page_size(lib, handle), lib_int(lib, handle, "PRAGMA cipher_default_page_size"));
}
/* L8: the handle would read file's plaintext-header database with a different page size than the header states. */
static int header_mismatch(sqlite3 *handle, int file) {
int header_page = file < 0 ? 0 : memvfs_header_page_size(files[file]);
return header_page && known_header_page_mismatch(major, header_page, fuzz_codec_page_size(lib, handle));
}
/* The plain run never sees an L8 mismatch, so it keeps a connection open that the codec run closes. */
static int plain_diverged;
static void close_mismatched(void) {
close_db();
plain_diverged = 1;
}
/* Whether file holds pages, in the database or its WAL, that an earlier key and settings wrote. */
static int file_written(int file) {
char wal[32];
size_t len = 0;
if (memvfs_peek(files[file], &len) && len) return 1;
snprintf(wal, sizeof wal, "%s-wal", files[file]);
return memvfs_peek(wal, &len) && len;
}
/* Runs every statement in sql and returns the first failure. */
static int run(sqlite3 *handle, const char *sql, struct dump *out) {
int result = SQLITE_OK;
while (sql && *sql) {
sqlite3_stmt *stmt = NULL;
const char *tail = NULL;
int rc = lib->prepare_v2(handle, sql, -1, &stmt, &tail);
if (rc == SQLITE_OK && stmt && !trusted && vacuum_blocked(handle, known_vacuum(sql, (size_t)(tail - sql)))) {
lib->finalize(stmt);
stmt = NULL;
rc = SQLITE_AUTH;
}
if (rc == SQLITE_OK && stmt) rc = drain(stmt, out);
if (handle == db && !trusted) {
default_page = lib_int(lib, handle, "PRAGMA cipher_default_page_size");
touched = 1;
if (header_mismatch(db, db_file)) break;
}
dump_code(out, rc);
if (result == SQLITE_OK) result = rc;
if (rc != SQLITE_OK && !stmt) break;
sql = tail;
}
return result;
}
static int run_trusted(sqlite3 *handle, const char *sql, struct dump *out) {
trusted = 1;
int rc = run(handle, sql, out);
trusted = 0;
return rc;
}
/* read_key's length for its raw key with an explicit salt, x'<64 hex digits key><32 hex digits salt>'. */
#define RAW_KEY_SALT_LEN (3 + 2 * 48)
static void read_key(struct input *in, struct key *key) {
unsigned form = u8(in);
memset(key, 0, sizeof *key);
if (form & 1) {
unsigned char raw[48];
int len = form & 2 ? 48 : 32;
for (int i = 0; i < len; i++) raw[i] = (unsigned char)u8(in);
key->bytes[0] = 'x';
key->bytes[1] = '\'';
fuzz_hex((char *)key->bytes + 2, raw, len);
key->bytes[2 + 2 * len] = '\'';
key->len = 3 + 2 * len;
key->migrate = (form & 4) != 0;
} else {
struct input pass = take(in, 64);
memcpy(key->bytes, pass.data, pass.size);
key->len = (int)pass.size;
key->passphrase = key->len > 0;
}
key->kdf_iter = (int)(u8(in) % 4) + 1;
}
static int apply_key(sqlite3 *handle, const char *schema, const struct key *key, int rekey, struct dump *out) {
char sql[96];
if (rekey) {
if (!lib->rekey_v2) return SQLITE_OK; /* Plain SQLite has no codec to rekey. */
if (major < 5) {
snprintf(sql, sizeof sql, "PRAGMA \"%s\".rekey_kdf_iter = %d", schema, key->kdf_iter);
run_trusted(handle, sql, out);
}
int rc = lib->rekey_v2(handle, schema, key->bytes, key->len);
dump_code(out, rc);
return rc;
}
if (!lib->key_v2) return SQLITE_OK;
int rc = lib->key_v2(handle, schema, key->bytes, key->len);
dump_code(out, rc);
snprintf(sql, sizeof sql, "PRAGMA \"%s\".kdf_iter = %d", schema, key->kdf_iter);
run_trusted(handle, sql, out);
return rc;
}
static unsigned page_size(struct input *in) {
unsigned pick = u8(in);
return known_page_size(parse_major, pick % 16 < 8 ? 512u << (pick % 8) : u16(in));
}
static const char *pick(struct input *in, const char *const *choices, size_t count) {
return choices[u8(in) % count];
}
#define PICK(in, choices) pick(in, choices, sizeof choices / sizeof *choices)
enum setting {
SET_PAGE_SIZE,
SET_USE_HMAC,
SET_HMAC_ALGORITHM,
SET_KDF_ALGORITHM,
SET_HEADER_SIZE,
SET_SALT,
SET_COMPATIBILITY,
SET_KDF_ITER,
SET_HMAC_PGNO,
SET_FAST_KDF_ITER,
SET_CIPHER,
SET_AEAD,
SET_HMAC_FAST_KDF,
SET_ADD_RANDOM,
};
/* 5.x removes the deprecated pragmas and adds the AEAD switches. */
static const enum setting settings_4[] = {
SET_PAGE_SIZE, SET_USE_HMAC, SET_HMAC_ALGORITHM, SET_KDF_ALGORITHM, SET_HEADER_SIZE, SET_SALT,
SET_COMPATIBILITY, SET_KDF_ITER, SET_HMAC_PGNO, SET_FAST_KDF_ITER, SET_CIPHER, SET_ADD_RANDOM,
};
static const enum setting settings_5[] = {
SET_PAGE_SIZE, SET_USE_HMAC, SET_HMAC_ALGORITHM, SET_KDF_ALGORITHM, SET_HEADER_SIZE, SET_SALT,
SET_COMPATIBILITY, SET_KDF_ITER, SET_AEAD, SET_HMAC_FAST_KDF, SET_ADD_RANDOM,
};
/* One codec pragma on schema, followed by a cheap kdf_iter wherever the pragma would restore an expensive one. */
static enum setting format_setting(struct input *in, const char *schema, char *sql, size_t cap) {
static const char *const on_off[] = {"ON", "OFF"};
static const char *const hmacs[] = {"HMAC_SHA1", "HMAC_SHA256", "HMAC_SHA512", "HMAC_MD5"};
static const char *const kdfs[] = {"PBKDF2_HMAC_SHA1", "PBKDF2_HMAC_SHA256", "PBKDF2_HMAC_SHA512", "SCRYPT"};
static const char *const pgnos[] = {"le", "be", "native", "middle"};
static const char *const ciphers[] = {"aes-256-cbc", "aes-128-cbc", "chacha20"};
static const unsigned headers[] = {0, 16, 24, 32, 48, 4096};
unsigned char salt[16];
char salt_hex[33];
unsigned char random_data[24];
char random_hex[49];
unsigned choice = u8(in);
enum setting setting = parse_major < 5 ? settings_4[choice % (sizeof settings_4 / sizeof *settings_4)]
: settings_5[choice % (sizeof settings_5 / sizeof *settings_5)];
switch (setting) {
case SET_PAGE_SIZE:
snprintf(sql, cap, "PRAGMA \"%s\".cipher_page_size = %u", schema, page_size(in));
break;
case SET_USE_HMAC:
snprintf(sql, cap, "PRAGMA \"%s\".cipher_use_hmac = %s", schema, PICK(in, on_off));
break;
case SET_HMAC_ALGORITHM:
snprintf(sql, cap, "PRAGMA \"%s\".cipher_hmac_algorithm = %s", schema, PICK(in, hmacs));
break;
case SET_KDF_ALGORITHM:
snprintf(sql, cap, "PRAGMA \"%s\".cipher_kdf_algorithm = %s", schema, PICK(in, kdfs));
break;
case SET_HEADER_SIZE: {
unsigned index = u8(in);
unsigned size = index % 8 < 6 ? headers[index % 8] : u8(in);
snprintf(sql, cap, "PRAGMA \"%s\".cipher_plaintext_header_size = %u", schema, known_header_size(size));
break;
}
case SET_SALT:
for (int i = 0; i < 16; i++) salt[i] = (unsigned char)u8(in);
fuzz_hex(salt_hex, salt, 16);
snprintf(sql, cap, "PRAGMA \"%s\".cipher_salt = \"x'%s'\"", schema, salt_hex);
break;
case SET_COMPATIBILITY:
snprintf(sql, cap, "PRAGMA \"%s\".cipher_compatibility = %u; PRAGMA \"%s\".kdf_iter = 2", schema, u8(in) % 6,
schema);
break;
case SET_KDF_ITER:
snprintf(sql, cap, "PRAGMA \"%s\".kdf_iter = %u", schema, u8(in) % 4 + 1);
break;
case SET_HMAC_PGNO:
snprintf(sql, cap, "PRAGMA \"%s\".cipher_hmac_pgno = %s", schema, PICK(in, pgnos));
break;
case SET_FAST_KDF_ITER:
snprintf(sql, cap, "PRAGMA \"%s\".fast_kdf_iter = %u", schema, u8(in) % 4 + 1);
break;
case SET_CIPHER:
snprintf(sql, cap, "PRAGMA \"%s\".cipher = '%s'", schema, PICK(in, ciphers));
break;
case SET_AEAD:
snprintf(sql, cap, "PRAGMA \"%s\".cipher_aead = %s", schema, PICK(in, on_off));
break;
case SET_HMAC_FAST_KDF: {
const char *value = PICK(in, on_off);
/* L3: cipher_use_hmac redoes the reserve setup that cipher_hmac_fast_kdf skips. */
snprintf(sql, cap, "PRAGMA \"%s\".cipher_hmac_fast_kdf = %s%s%s%s", schema, value,
value == on_off[0] ? "; PRAGMA \"" : "", value == on_off[0] ? schema : "",
value == on_off[0] ? "\".cipher_use_hmac = ON" : "");
break;
}
case SET_ADD_RANDOM: {
/* Mutating raw SQL rarely yields the exact x'<hex>' value sqlcipher_codec_add_random accepts. */
unsigned len = u8(in) % (sizeof random_data + 1);
for (unsigned i = 0; i < len; i++) random_data[i] = (unsigned char)u8(in);
fuzz_hex(random_hex, random_data, len);
snprintf(sql, cap, "PRAGMA \"%s\".cipher_add_random = \"x'%s'\"", schema, random_hex);
break;
}
}
return setting;
}
static enum setting apply_setting(sqlite3 *handle, const char *schema, struct input *in, struct dump *out) {
char sql[160];
enum setting setting = format_setting(in, schema, sql, sizeof sql);
run_trusted(handle, sql, out);
return setting;
}
/* A key that carries its own salt sets that salt on purpose, so file's salt is no longer a draw. */
static void note_salted_key(int file, const struct key *key, int rc) {
if (rc == SQLITE_OK && key->len == RAW_KEY_SALT_LEN) uniqueness_note_explicit_salt(file);
}
static void record(int file, enum step_kind kind, const uint8_t *start, const uint8_t *end) {
struct recipe *recipe = &recipes[file];
if (recipe->count == MAX_RECIPE) {
recipe->truncated = 1;
return;
}
recipe->steps[recipe->count].kind = kind;
recipe->steps[recipe->count].slice.data = start;
recipe->steps[recipe->count].slice.size = (size_t)(end - start);
recipe->steps[recipe->count].kdf_iter = 0;
recipe->count++;
}
/* A failed rekey leaves the old key in place, while replaying it as a key would apply the new one. */
static void record_rekey(int file, const uint8_t *start, const uint8_t *end, int rc) {
if (rc != SQLITE_OK) return;
struct input slice = {start, (size_t)(end - start)};
struct key key;
read_key(&slice, &key);
if (key.len == 0) { /* 5.x writes the file back in plaintext, which opens with no key or setting at all. */
memset(&recipes[file], 0, sizeof recipes[file]);
return;
}
trusted = 1;
int kdf_iter = lib_int(lib, db, "PRAGMA main.kdf_iter");
trusted = 0;
record(file, STEP_KEY, start, end);
struct recipe *recipe = &recipes[file];
if (recipe->count > 0) recipe->steps[recipe->count - 1].kdf_iter = kdf_iter;
}
/* Whether the last key file's recipe applies is a nonempty one, assumed so once the recipe dropped a step. */
static int recipe_keyed(int file) {
if (recipes[file].truncated) return 1;
int keyed = 0;
for (int i = 0; i < recipes[file].count; i++) {
if (recipes[file].steps[i].kind != STEP_KEY) continue;
struct input slice = recipes[file].steps[i].slice;
struct key key;
read_key(&slice, &key);
keyed = key.len > 0;
}
return keyed;
}
/* Returns nonzero when replay cannot rebuild the writing connection: a dropped step, or settings that disagree with
the file's plaintext header (L8). */
static int replay(sqlite3 *handle, int file, struct dump *out) {
const struct recipe *recipe = &recipes[file];
if (recipe->truncated && lib->key_v2) return 1; /* Plain SQLite replays nothing a dropped step could change. */
for (int i = 0; i < recipe->count; i++) {
struct input slice = recipe->steps[i].slice;
if (recipe->steps[i].kind == STEP_KEY) {
struct key key;
read_key(&slice, &key);
if (recipe->steps[i].kdf_iter) key.kdf_iter = recipe->steps[i].kdf_iter;
apply_key(handle, "main", &key, 0, out);
} else {
apply_setting(handle, "main", &slice, out);
}
}
return header_mismatch(handle, file);
}
/* Every table's rows, plus the result codes only a codec connection has when codes is set. */
static void read_tables(sqlite3 *handle, struct dump *out, int codes) {
char names[MAX_DUMP_TABLES][128];
int count = 0;
sqlite3_stmt *stmt = NULL;
/* Canary names carry a counter that differs between the two runs of differential_plain, so it skips them. */
int rc = lib->prepare_v2(handle,
codes ? "SELECT name FROM sqlite_schema WHERE type = 'table' ORDER BY name"
: "SELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE "
"'canary\\_%' ESCAPE '\\' ORDER BY name",
-1, &stmt, NULL);
if (rc == SQLITE_OK) {
while ((rc = lib->step(stmt)) == SQLITE_ROW) {
dump_row(stmt, out);
const unsigned char *name = lib->column_blob(stmt, 0);
int len = lib->column_bytes(stmt, 0);
if (count < MAX_DUMP_TABLES && name && len < (int)sizeof names[0]) {
memcpy(names[count], name, (size_t)len);
names[count++][len] = 0;
}
}
lib->finalize(stmt);
}
if (codes) dump_code(out, rc);
/* A database without tables holds what a missing file holds, and harness pragmas create files on one side only. */
if (!codes && count == 0) return;
for (int i = 0; i < count; i++) {
char sql[300] = "SELECT * FROM \"";
size_t at = strlen(sql);
for (const char *c = names[i]; *c; c++) {
if (*c == '"') sql[at++] = '"';
sql[at++] = *c;
}
sql[at++] = '"';
sql[at] = 0;
run(handle, sql, out);
}
run_trusted(handle, codes ? "PRAGMA cipher_integrity_check; PRAGMA integrity_check" : "PRAGMA integrity_check", out);
}
static void read_all(sqlite3 *handle, struct dump *out) { read_tables(handle, out, 1); }
/* SQLite's PRNG also feeds journal and temp-file draws, which differ between a codec and a plain connection. */
static uint64_t blob_state;
static void fill_blob(unsigned char *out, unsigned len) {
for (unsigned i = 0; i < len; i++) {
uint64_t z = (blob_state += 0x9e3779b97f4a7c15ULL);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
out[i] = (unsigned char)(z ^ (z >> 31));
}
}
static void op_statement(struct input *in) {
static const char *const statements[] = {
"CREATE TABLE IF NOT EXISTS t(a INTEGER PRIMARY KEY, b BLOB, c TEXT)",
"INSERT INTO t(b, c) VALUES (?4, ?3)",
"INSERT INTO t(b, c) SELECT zeroblob(?2), c FROM t LIMIT 16",
"UPDATE t SET b = ?4 WHERE a % 3 = ?2 % 3",
"DELETE FROM t WHERE a % (?2 % 4 + 1) = 0",
"CREATE INDEX IF NOT EXISTS t_c ON t(c)",
"CREATE TABLE IF NOT EXISTS w(k TEXT PRIMARY KEY, v BLOB) WITHOUT ROWID",
"INSERT OR REPLACE INTO w VALUES (hex(substr(?5, 1, ?2 % 32 + 1)), ?4)",
"DROP TABLE IF EXISTS w",
"VACUUM",
"BEGIN",
"COMMIT",
"ROLLBACK",
"SAVEPOINT s",
"ROLLBACK TO s",
"RELEASE s",
"PRAGMA incremental_vacuum(4)",
"PRAGMA wal_checkpoint(TRUNCATE)",
"PRAGMA integrity_check",
"PRAGMA cipher_integrity_check",
"SELECT count(*), sum(length(b)) FROM t",
"CREATE TEMP TABLE IF NOT EXISTS tt AS SELECT * FROM t",
"PRAGMA cipher_migrate",
};
size_t index = u8(in) % (sizeof statements / sizeof *statements);
unsigned blob = u16(in) % (MAX_BLOB + 1);
unsigned small = u8(in);
struct input text = take(in, 256);
/* Drawn before any return, so the codec and plain runs see the same values for the same op. */
static unsigned char bytes[MAX_BLOB], key_bytes[32];
fill_blob(bytes, blob);
fill_blob(key_bytes, sizeof key_bytes);
if (!db) return;
int already_touched = touched;
touched = 1;
int migrate = strcmp(statements[index], "PRAGMA cipher_migrate") == 0;
/* Migration retries the passphrase at the legacy kdf_iter counts, far beyond a fuzz iteration's time. */
if (migrate && recipes[db_file].passphrase) return;
if (vacuum_blocked(db, known_vacuum(statements[index], strlen(statements[index])))) return;
sqlite3_stmt *stmt = NULL;
if (lib->prepare_v2(db, statements[index], -1, &stmt, NULL) != SQLITE_OK) return;
lib->bind_int64(stmt, 1, blob);
lib->bind_int64(stmt, 2, small);
lib->bind_text(stmt, 3, (const char *)text.data, (int)text.size, SQLITE_TRANSIENT);
lib->bind_blob(stmt, 4, bytes, (int)blob, SQLITE_STATIC);
lib->bind_blob(stmt, 5, key_bytes, sizeof key_bytes, SQLITE_STATIC);
int check = migrate && !known_migrate_poisons(already_touched);
/* L11: after a late cipher_migrate every read on the codec connection fails, while plain SQLite has no such pragma. */
if (migrate && known_migrate_poisons(already_touched)) plain_exempt[db_file] = 1;
if (check) model_snapshot(lib, db, "main", db_file);
trusted = 1;
int rc = drain(stmt, NULL);
trusted = 0;
if (check && rc == SQLITE_OK) model_check(lib, db, "main", db_file, files[db_file], "cipher_migrate");
else if (check) model_discard();
}
static void op_pragma(struct input *in) {
static const char *const pragmas[] = {"auto_vacuum", "journal_mode", "locking_mode", "cache_size",
"secure_delete", "temp_store", "synchronous", "page_size"};
static const char *const values[][6] = {
{"NONE", "FULL", "INCREMENTAL"},
{"DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF", "WAL"},
{"NORMAL", "EXCLUSIVE"},
{"1", "2", "10", "-2000"},
{"ON", "OFF", "FAST"},
{"DEFAULT", "FILE", "MEMORY"},
{"OFF", "NORMAL", "FULL"},
};
static const size_t counts[] = {3, 6, 2, 4, 3, 3, 3};
size_t index = u8(in) % (sizeof pragmas / sizeof *pragmas);
char sql[96];
if (index == 7) {
snprintf(sql, sizeof sql, "PRAGMA page_size = %u", page_size(in));
} else {
snprintf(sql, sizeof sql, "PRAGMA %s = %s", pragmas[index], values[index][u8(in) % counts[index]]);
}
/* L6: SQLCipher answers page_size as cipher_page_size, which after first access is the same misuse as a late key. */
if (!db || (index == 7 && touched)) return;
touched = 1;
/* page_size is cipher_page_size here, so it pins a written file as op_setting's settings do. */
if (index == 7 && file_written(db_file)) plain_exempt[db_file] = 1;
run_trusted(db, sql, NULL);
if (known_wal_without_shm(major) && strcmp(sql, "PRAGMA journal_mode = WAL") == 0) plain_exempt[db_file] = 1;
if (header_mismatch(db, db_file)) close_mismatched(); /* L8 */
}
static void op_key(struct input *in, int rekey) {
const uint8_t *start = in->data;
struct key key;
read_key(in, &key);
/* L6: a key applied after the connection touched the file is position D and corrupts memory. */
if (!db || (touched && !rekey) || !lib->key_v2) return;
if (rekey) model_snapshot(lib, db, "main", db_file);
if (rekey && known_rekey_commits_transaction(lib->get_autocommit(db))) plain_exempt[db_file] = 1;
int rc = apply_key(db, "main", &key, rekey, NULL);
if (rekey) touched = 1; /* A rekey reads and rewrites every page, so a later key comes after first access. */
if (rekey && rc == SQLITE_OK) model_check(lib, db, "main", db_file, files[db_file], "rekey");
else if (rekey) model_discard();
/* cipher_migrate needs the underived passphrase, which the first page access discards. */
if (!rekey && rc == SQLITE_OK && !key.passphrase && key.migrate) {
int check = !known_migrate_poisons(touched);
if (check) model_snapshot(lib, db, "main", db_file);
int migrate_rc = run_trusted(db, "PRAGMA main.cipher_migrate", NULL);
if (check && migrate_rc == SQLITE_OK) model_check(lib, db, "main", db_file, files[db_file], "cipher_migrate");
else if (check) model_discard();
touched = 1;
}
/* key_v2 never encrypts what the file already holds, so only a read that decodes proves the key, unlike rekey_v2. */
if (!rekey && rc == SQLITE_OK) {
verify_key(db, "main", db_file, key.len);
touched = 1;
} else if (rc == SQLITE_OK && key.len > 0) {
confidentiality_mark_keyed(db_file);
} else if (rc == SQLITE_OK) {
confidentiality_forget_file(db_file); /* A rekey to the empty key writes the file back in plaintext. */
}
note_salted_key(db_file, &key, rc);
if (rekey) record_rekey(db_file, start, in->data, rc);
else record(db_file, STEP_KEY, start, in->data);
}
static void op_setting(struct input *in) {
const uint8_t *start = in->data;
char sql[160];
enum setting setting = format_setting(in, "main", sql, sizeof sql);
if (!db || touched) return; /* L6 */
/* The codec reads a written file only under the settings that wrote it, while plain SQLite ignores them. */
if (file_written(db_file)) plain_exempt[db_file] = 1;
run_trusted(db, sql, NULL);
if (setting == SET_SALT) uniqueness_note_explicit_salt(db_file);
record(db_file, STEP_SETTING, start, in->data);
if (header_mismatch(db, db_file)) close_mismatched(); /* L8 */
}
static void op_attach(struct input *in) {
static const char *const moves[] = {
"SELECT sqlcipher_export('aux')",
"SELECT sqlcipher_export('main', 'aux')",
"SELECT count(*) FROM aux.sqlite_schema",
};
unsigned target = u8(in);
const uint8_t *start = in->data;
struct key key;
read_key(in, &key);
const uint8_t *key_end = in->data;
unsigned settings = u8(in) % 3;
/* Parsed before any return, since the codec and plain runs return at different points and must stay aligned. */
char setting_sql[2][160];
enum setting setting_kind[2];
struct input setting_slice[2];
for (unsigned i = 0; i < settings; i++) {
setting_slice[i].data = in->data;
setting_kind[i] = format_setting(in, "aux", setting_sql[i], sizeof setting_sql[i]);
setting_slice[i].size = (size_t)(in->data - setting_slice[i].data);
}
unsigned move = u8(in) % (sizeof moves / sizeof *moves);
if (!db) return;
touched = 1;
int file = (db_file + 1 + (int)(target % 2)) % SCRIPT_FILES;
confidentiality_forget_file(file);
if (known_header_page_mismatch(major, memvfs_header_page_size(files[file]), default_page)) return; /* L8 */
int written = file_written(file);
sqlite3_stmt *stmt = NULL;
/* Plain SQLite has no KEY clause, so it attaches the same file without one. */
int keyless = !lib->key_v2;
if (lib->prepare_v2(db, keyless ? "ATTACH ?1 AS aux" : "ATTACH ?1 AS aux KEY ?2", -1, &stmt, NULL) != SQLITE_OK)
return;
lib->bind_text(stmt, 1, files[file], -1, SQLITE_STATIC);
if (!keyless) lib->bind_blob(stmt, 2, key.bytes, key.len, SQLITE_TRANSIENT);
trusted = 1;
int rc = drain(stmt, NULL);
trusted = 0;
if (rc != SQLITE_OK) {
if (!keyless) plain_exempt[file] = 1; /* Plain SQLite attaches what the codec refused to. */
return;
}
memset(&recipes[file], 0, sizeof recipes[file]);
recipes[file].passphrase = key.passphrase;
record(file, STEP_KEY, start, key_end);
char sql[96];
snprintf(sql, sizeof sql, "PRAGMA aux.kdf_iter = %d", key.kdf_iter);
run_trusted(db, sql, NULL);
for (unsigned i = 0; i < settings; i++) {
if (written) plain_exempt[file] = 1; /* As in op_setting. */
run_trusted(db, setting_sql[i], NULL);
if (setting_kind[i] == SET_SALT) uniqueness_note_explicit_salt(file);
record(file, STEP_SETTING, setting_slice[i].data, setting_slice[i].data + setting_slice[i].size);
}
/* L6: the read follows the settings, since a setting after aux's first access is the same misuse as a late key. */
if (!keyless) {
verify_key(db, "aux", file, key.len);
note_salted_key(file, &key, SQLITE_OK);
}
/* Export only adds tables, so the target must still hold every source table unchanged. Attach alters nothing. */
/* Export into aux reads main, every other move reads aux. */
if (move == 0) model_snapshot(lib, db, "main", db_file);
else model_snapshot(lib, db, "aux", file);
/* sqlcipher_export does not exist in plain SQLite, so only the codec run fills the export's target. */
if (move == 0) plain_exempt[file] = 1;
else if (move == 1) plain_exempt[db_file] = 1;
if (run_trusted(db, moves[move], NULL) != SQLITE_OK) {
model_discard();
} else if (move == 0) {
model_check_contains(lib, db, "aux", file, files[file], "sqlcipher_export");
} else if (move == 1) {
model_check_contains(lib, db, "main", db_file, files[db_file], "sqlcipher_export");
} else {
model_check(lib, db, "aux", file, files[file], "attach");
}
uniqueness_check(lib, db, "aux", file, files[file]);
run_trusted(db, "DETACH aux", NULL);
}
struct backup_args {
unsigned target;
const uint8_t *start, *end;
struct key key;
};
static void read_backup(struct input *in, struct backup_args *args) {
args->target = u8(in);
args->start = in->data;
read_key(in, &args->key);
args->end = in->data;
}
static int backup_target(const struct backup_args *args) {
return (db_file + 1 + (int)(args->target % 2)) % SCRIPT_FILES;
}
static void run_backup(const struct backup_args *args) {
if (!db) return;
touched = 1;
int file = backup_target(args);
sqlite3 *dest = open_file(file, 1);
if (!dest) return;
confidentiality_forget_file(file);
memset(&recipes[file], 0, sizeof recipes[file]);
recipes[file].passphrase = args->key.passphrase;
int rc = apply_key(dest, "main", &args->key, 0, NULL);
if (rc == SQLITE_OK && lib->key_v2) verify_key(dest, "main", file, args->key.len);
note_salted_key(file, &args->key, rc);
record(file, STEP_KEY, args->start, args->end);
/* Plain SQLite copies whatever the codec refuses or fails to back up, plaintext into keyed included. */
int backed_up = 0;
if (known_backup_blocked(major, fuzz_codec_page_size(lib, db), fuzz_codec_page_size(lib, dest))) {
plain_exempt[file] = 1;
uniqueness_check(lib, dest, "main", file, files[file]);
lib->close_v2(dest);
return;
}
sqlite3_backup *backup = lib->backup_init(dest, "main", db, "main");
if (backup) {
model_snapshot(lib, db, "main", db_file);
int step_rc = lib->backup_step(backup, -1);
lib->backup_finish(backup);
if (step_rc == SQLITE_DONE) model_check(lib, dest, "main", file, files[file], "backup");
else model_discard();
backed_up = step_rc == SQLITE_DONE;
if (backed_up && plain_exempt[db_file]) plain_exempt[file] = 1; /* A copy holds what made its source differ. */
}
if (!backed_up && lib->key_v2) plain_exempt[file] = 1;
uniqueness_check(lib, dest, "main", file, files[file]);
lib->close_v2(dest);
}
static void op_backup(struct input *in) {
struct backup_args args;
read_backup(in, &args);
run_backup(&args);
}
/* The script file index memvfs slot index names, or -1 for a temp or super-journal file. */
static int file_of_memvfs(int index) {
const char *name = memvfs_name(index);
for (int i = 0; i < SCRIPT_FILES; i++) {
if (strcmp(files[i], name) == 0) return i;
}
return -1;
}
static void op_damage(struct input *in) {
unsigned target = u8(in);
unsigned kind = u8(in) % 3;
unsigned long offset = u32(in);
unsigned mask = u8(in) | 1;
struct input chunk = kind == 2 ? take(in, 4096) : (struct input){NULL, 0};
int count = memvfs_count();
if (!count) return;
int index = (int)(target % (unsigned)count);
long long size = memvfs_size(index);
long long at = kind == 0 && size ? (long long)(offset % (unsigned long long)size)
: (long long)(offset % (unsigned long long)(size + 1));
long long len = kind == 0 ? 1 : kind == 2 ? (long long)chunk.size : 0;
if (known_header_edit(major, memvfs_starts_with(index, "SQLite format 3", 16), at, len)) return;
int file = file_of_memvfs(index);
tamper_prune(file, memvfs_name(index));
/* The same bytes are ciphertext on one side and plaintext on the other. */
if (file >= 0) plain_exempt[file] = damaged[file] = 1;
/* Raw damage writes input bytes, which mutators copy around, so an IV or salt there is no draw. */
if (kind != 1) uniqueness_note_damage(file);
model_note_damage(file);
if (kind == 0) {
if (size) {
memvfs_flip(index, at, (unsigned char)mask);
tamper_flip(file, memvfs_name(index), at, (unsigned char)mask);
}
} else if (kind == 1) {
memvfs_truncate(index, at);
} else {
memvfs_overwrite(index, at, chunk.data, chunk.size);
if (chunk.size) tamper_note(file, memvfs_name(index), at, len);
}
}
/* Tampers a page of the open file, cached or not, then copies pages with a rekey, a backup or VACUUM. */
static void op_cache_race(struct input *in) {
unsigned warm = u8(in) & 1;
unsigned target = u8(in);
unsigned kind = u8(in) % 3;
unsigned char mask = u8(in) | 1;
unsigned page_offset = u16(in);
unsigned page_draw = u32(in);
/* Parsed before any return, since plain SQLite has no codec page and returns below while the codec run goes on. */
const uint8_t *key_start = in->data;
struct key key;
struct backup_args backup;
if (kind == 0) read_key(in, &key);
else if (kind == 1) read_backup(in, &backup);
const uint8_t *key_end = in->data;
if (!db) return;
int count = memvfs_count();
if (!count) return;
int index = (int)(target % (unsigned)count);
if (file_of_memvfs(index) != db_file) return;
long long size = memvfs_size(index);
int page_size = fuzz_codec_page_size(lib, db);
if (!size || page_size <= 0) return;
long long pgno = size / page_size ? (long long)(page_draw % (unsigned long long)(size / page_size)) + 1 : 1;
long long at = (pgno - 1) * page_size + (long long)(page_offset % (unsigned)page_size);
if (known_header_edit(major, memvfs_starts_with(index, "SQLite format 3", 16), at, 1)) return;
touched = 1;
if (warm) {
sqlite3_stmt *stmt = NULL;
if (lib->prepare_v2(db, "SELECT data FROM sqlite_dbpage WHERE pgno = ?1", -1, &stmt, NULL) == SQLITE_OK) {
lib->bind_int64(stmt, 1, pgno);
lib->step(stmt);
lib->finalize(stmt);
}
}
tamper_prune(db_file, memvfs_name(index));
/* The plain run returned above, having no codec page to flip and so no copy of one to make. */
plain_exempt[db_file] = 1;
if (kind == 1) plain_exempt[backup_target(&backup)] = 1;
memvfs_flip(index, at, mask);
tamper_flip(db_file, memvfs_name(index), at, mask);
uniqueness_note_damage(db_file);
model_note_damage(db_file);
if (kind == 0) {
int rc = apply_key(db, "main", &key, 1, NULL);
if (rc == SQLITE_OK && lib->rekey_v2 && key.len > 0) confidentiality_mark_keyed(db_file);
else if (rc == SQLITE_OK && lib->rekey_v2) confidentiality_forget_file(db_file);
note_salted_key(db_file, &key, rc);
record_rekey(db_file, key_start, key_end, rc);
if (key.passphrase) recipes[db_file].passphrase = 1;
} else if (kind == 1) {
run_backup(&backup);
} else if (!vacuum_blocked(db, known_vacuum("VACUUM", 6))) {
run(db, "VACUUM", NULL);
}
}
/* Arms one fault, commits a write, then reopens, which must show exactly the state the connection saw afterwards. */
static void op_fault_txn(struct input *in) {
static const enum fault_kind kinds[] = {FAULT_SHORT_READ, FAULT_SHORT_WRITE, FAULT_ENOSPC, FAULT_FSYNC,
FAULT_ALLOC};
enum fault_kind kind = kinds[u8(in) % (sizeof kinds / sizeof *kinds)];
unsigned countdown = u8(in);
unsigned blob = u16(in) % (MAX_BLOB + 1);
if (!db || !lib->get_autocommit(db)) return;
touched = 1;
/* The same countdown lands on different I/O in a codec connection, which reads and writes more per statement. */
plain_exempt[db_file] = 1;
/* SQLite takes a short read for the end of the file and keeps the zero-filled rest, which only a page HMAC rejects. */
int authenticated = lib_int(lib, db, "PRAGMA main.cipher_use_hmac") == 1;
fault_arm(kind, countdown);
int rc = run_trusted(db, "BEGIN IMMEDIATE", NULL);
if (rc == SQLITE_OK) rc = run_trusted(db, "CREATE TABLE IF NOT EXISTS t(a INTEGER PRIMARY KEY, b BLOB, c TEXT)", NULL);