Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ The sources are generated from signed SQLCipher and libtomcrypt releases by `upg

The same checks run on SQLCipher's newest signed pre-release, and daily on the heads of its `prerelease` and `beta` branches. Nothing from those lines ships, so they report under the SQLCipher previews badge and never hold back the CI badge or a release.

[ClusterFuzzLite](https://google.github.io/clusterfuzzlite/) fuzzes SQLCipher as `sqlite-wasm-rs` compiles it, under `AddressSanitizer`, `UndefinedBehaviorSanitizer` and `MemorySanitizer`, on the shipped release, SQLCipher's newest signed pre-release and its `prerelease` branch. A script target drives keys, rekeys, codec settings, attach, backup, export, page damage and injected I/O and allocation faults. It checks that a tampered page never reads back as valid, that no plaintext reaches a keyed file, that IVs and salts never repeat, that operations meant to keep content keep it, and that the rows match what plain SQLite and the OpenSSL provider produce. Two file-image targets hand SQLCipher hostile database files, one without the key and one with it.
[ClusterFuzzLite](https://google.github.io/clusterfuzzlite/) fuzzes SQLCipher as `sqlite-wasm-rs` compiles it, under `AddressSanitizer`, `UndefinedBehaviorSanitizer` and `MemorySanitizer`, on the shipped release, SQLCipher's newest signed pre-release and its `prerelease` branch. A script target drives keys, rekeys, codec settings, VFS chunk sizes, attach, backup, export, page damage and injected I/O and allocation faults. It checks that a tampered page never reads back as valid, that no plaintext reaches a keyed file, that IVs and salts never repeat and no salt is all zero, that operations meant to keep content keep it, and that the rows match what plain SQLite and the OpenSSL provider produce. Two file-image targets hand SQLCipher hostile database files, one without the key and one with it.

The crate is MIT, SQLCipher is BSD-3-Clause, SQLite is public domain, and libtomcrypt is public domain or WTFPL.
1 change: 1 addition & 0 deletions fuzz/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct fuzz_sqlite {
int (*backup_step)(sqlite3_backup *, int);
int (*backup_finish)(sqlite3_backup *);
int (*config)(int, ...);
int (*file_control)(sqlite3 *, const char *, int, void *);
};

#endif
4 changes: 4 additions & 0 deletions fuzz/known.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ int known_wal_without_shm(int major) {
return major >= 5; /* sqlcipher#623 */
}

int known_zero_salt_after_size_hint(const char *library) {
return strncmp(library, "fuzz_sqlite_prerelease_", 23) != 0; /* sqlcipher#633 */
}

/* L7: sqlcipherCodecGetKey copies a zero-length pass that its callers never free. */
const char *__lsan_default_suppressions(void);
const char *__lsan_default_suppressions(void) {
Expand Down
4 changes: 4 additions & 0 deletions fuzz/known.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ int known_rekey_commits_transaction(int autocommit);
/* sqlcipher#623: 5.0 advertises shared memory memvfs lacks, so after WAL every write without exclusive locking fails. */
int known_wal_without_shm(int major);

/* sqlcipher#633: a file grown on SQLITE_FCNTL_SIZE_HINT before page 1 is written gets an all-zero salt. Only the
prerelease line may carry the fix, so the other lines never set a chunk size. */
int known_zero_salt_after_size_hint(const char *library);

#endif
1 change: 1 addition & 0 deletions fuzz/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ const struct fuzz_sqlite FUZZ_LIBRARY = {
.backup_step = sqlite3_backup_step,
.backup_finish = sqlite3_backup_finish,
.config = sqlite3_config,
.file_control = sqlite3_file_control,
};
23 changes: 19 additions & 4 deletions fuzz/memvfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ struct mem_data {
struct mem_file {
sqlite3_file base;
struct mem_data *data;
/* SQLITE_FCNTL_CHUNK_SIZE, kept per handle as the unix VFS keeps it. */
int chunk;
};

static long long round_to_chunk(const struct mem_file *handle, long long size) {
return handle->chunk > 0 ? (size + handle->chunk - 1) / handle->chunk * handle->chunk : size;
}

static struct mem_data files[MAX_FILES];

static struct mem_data *lookup(const char *name) {
Expand Down Expand Up @@ -106,6 +112,7 @@ static int mem_write(sqlite3_file *file, const void *in, int amount, sqlite3_int

static int mem_truncate(sqlite3_file *file, sqlite3_int64 size) {
struct mem_data *data = ((struct mem_file *)file)->data;
size = round_to_chunk((struct mem_file *)file, size);
if (size < data->size) data->size = size;
return SQLITE_OK;
}
Expand Down Expand Up @@ -134,11 +141,18 @@ static int mem_check_reserved_lock(sqlite3_file *file, int *out) {
return SQLITE_OK;
}

/* The unix VFS's chunk handling, so a file grows on SQLITE_FCNTL_SIZE_HINT before SQLite writes its first page. */
static int mem_file_control(sqlite3_file *file, int op, void *arg) {
(void)file;
(void)op;
(void)arg;
return SQLITE_NOTFOUND;
struct mem_file *handle = (struct mem_file *)file;
switch (op) {
case SQLITE_FCNTL_CHUNK_SIZE:
handle->chunk = *(int *)arg;
return SQLITE_OK;
case SQLITE_FCNTL_SIZE_HINT:
return handle->chunk > 0 ? grow(handle->data, round_to_chunk(handle, *(sqlite3_int64 *)arg)) : SQLITE_OK;
default:
return SQLITE_NOTFOUND;
}
}

static int mem_sector_size(sqlite3_file *file) {
Expand Down Expand Up @@ -185,6 +199,7 @@ static int mem_open(sqlite3_vfs *vfs, const char *name, sqlite3_file *file, int
}
data->refs++;
handle->data = data;
handle->chunk = 0;
handle->base.pMethods = &mem_methods;
if (out_flags) *out_flags = flags;
return SQLITE_OK;
Expand Down
13 changes: 12 additions & 1 deletion fuzz/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ enum op {
OP_RAW_SQL = 13,
OP_CANARY = 14,
OP_FAULT_TXN = 15,
OP_COUNT = 16,
OP_CHUNK_SIZE = 16,
OP_COUNT = 17,
};

enum step_kind { STEP_KEY, STEP_SETTING };
Expand Down Expand Up @@ -1043,6 +1044,13 @@ static void op_raw_sql(struct input *in) {
if (header_mismatch(db, db_file)) close_mismatched(); /* L8 */
}

static void op_chunk_size(struct input *in) {
static const int sizes[] = {0, 512, 4096, 65536};
int chunk = sizes[u8(in) % (sizeof sizes / sizeof *sizes)];
if (!db || known_zero_salt_after_size_hint((parse_like ? parse_like : lib)->name)) return;
lib->file_control(db, "main", SQLITE_FCNTL_CHUNK_SIZE, &chunk);
}

void script_parse_like(const struct fuzz_sqlite *api) { parse_like = api; }

static void use(const struct fuzz_sqlite *api) {
Expand Down Expand Up @@ -1153,6 +1161,9 @@ void script_run(const struct fuzz_sqlite *api, struct input *in, enum script_mod
case OP_FAULT_TXN:
op_fault_txn(in);
break;
case OP_CHUNK_SIZE:
op_chunk_size(in);
break;
case OP_COUNT:
abort();
}
Expand Down
14 changes: 12 additions & 2 deletions fuzz/uniqueness.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,20 @@ static void check_ivs(int file, const char *file_name, int page_size, int reserv
}
}

static void check_salt(int file, const char *file_name) {
static void check_salt(int file, const char *file_name, int page_size) {
size_t have = 0;
const unsigned char *bytes = memvfs_peek(file_name, &have);
if (!bytes || have < IV_LEN) return;
size_t page_end = have < (size_t)page_size ? have : (size_t)page_size;
size_t written = IV_LEN;
while (written < page_end && bytes[written] == 0) written++;
if (written == page_end) return; /* A chunk the VFS preallocated, with page 1 never written through the codec. */
int zero = 1;
for (int b = 0; b < IV_LEN && zero; b++) zero = bytes[b] == 0;
if (zero) {
fprintf(stderr, "oracle=uniqueness file=%s reason=database salt is all zero\n", file_name);
abort();
}
unsigned long long hash = fuzz_fnv1a(bytes, IV_LEN);
struct file_state *st = &files_state[file];
if (st->has_salt && st->last_salt == hash) return;
Expand Down Expand Up @@ -178,5 +188,5 @@ void uniqueness_check(const struct fuzz_sqlite *api, sqlite3 *db, const char *sc
} /* chacha20's reserve padding is not modeled here, so its files get the salt check below but not the IV one. */

if (reserve_sz > 0 && reserve_sz < page_size) check_ivs(file, file_name, page_size, reserve_sz, unit_len);
if (header == 0 && !files_state[file].explicit_salt) check_salt(file, file_name);
if (header == 0 && !files_state[file].explicit_salt) check_salt(file, file_name, page_size);
}
Loading