test for codec error corruption in WAL mode

This commit is contained in:
Stephen Lombardo 2020-11-10 16:57:44 -05:00
parent 5bdc671b0a
commit e0df9c6aa5
2 changed files with 152 additions and 0 deletions

View File

@ -39,6 +39,11 @@
#include "sqlcipher_ext.h"
#endif
#ifdef SQLCIPHER_TEST
static int cipher_fail_next_encrypt = 0;
static int cipher_fail_next_decrypt = 0;
#endif
/* Generate code to return a string value */
static void codec_vdbe_return_string(Parse *pParse, const char *zLabel, const char *value, int value_type){
Vdbe *v = sqlite3GetVdbe(pParse);
@ -110,6 +115,24 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
codec_vdbe_return_string(pParse, "cipher_license", license_result, P4_DYNAMIC);
}
} else
#endif
#ifdef SQLCIPHER_TEST
if( sqlite3StrICmp(zLeft,"cipher_fail_next_encrypt")==0 ){
if( zRight ) {
cipher_fail_next_encrypt = sqlite3GetBoolean(zRight,1);
} else {
char *fail = sqlite3_mprintf("%d", cipher_fail_next_encrypt);
codec_vdbe_return_string(pParse, "cipher_fail_next_encrypt", fail, P4_DYNAMIC);
}
}else
if( sqlite3StrICmp(zLeft,"cipher_fail_next_decrypt")==0 ){
if( zRight ) {
cipher_fail_next_decrypt = sqlite3GetBoolean(zRight,1);
} else {
char *fail = sqlite3_mprintf("%d", cipher_fail_next_decrypt);
codec_vdbe_return_string(pParse, "cipher_fail_next_decrypt", fail, P4_DYNAMIC);
}
}else
#endif
if( sqlite3StrICmp(zLeft, "cipher_fips_status")== 0 && !zRight ){
if(ctx) {
@ -685,6 +708,9 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
memcpy(buffer, plaintext_header_sz ? pData : (void *) SQLITE_FILE_HEADER, offset);
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_DECRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
#ifdef SQLCIPHER_TEST
if(cipher_fail_next_decrypt) rc = SQLITE_ERROR;
#endif
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
sqlcipher_memset((unsigned char*) buffer+offset, 0, page_sz-offset);
sqlcipher_codec_ctx_set_error(ctx, rc);
@ -707,6 +733,9 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
memcpy(buffer, plaintext_header_sz ? pData : kdf_salt, offset);
}
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_ENCRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
#ifdef SQLCIPHER_TEST
if(cipher_fail_next_encrypt) rc = SQLITE_ERROR;
#endif
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
sqlcipher_memset((unsigned char*)buffer+offset, 0, page_sz-offset);
sqlcipher_codec_ctx_set_error(ctx, rc);

View File

@ -0,0 +1,123 @@
# SQLCipher
# codec.test developed by Stephen Lombardo (Zetetic LLC)
# sjlombardo at zetetic dot net
# http://zetetic.net
#
# Copyright (c) 2018, ZETETIC LLC
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the ZETETIC LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# This file implements regression tests for SQLite library. The
# focus of this script is testing code cipher features.
#
# NOTE: tester.tcl has overridden the definition of sqlite3 to
# automatically pass in a key value. Thus tests in this file
# should explicitly close and open db with sqlite_orig in order
# to bypass default key assignment.
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/sqlcipher.tcl
do_test codec-error-journal-delete {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
CREATE table t1(a INTEGER PRIMARY KEY,b);
BEGIN;
}
for {set i 1} {$i<=10000} {incr i} {
execsql "INSERT INTO t1(a,b) VALUES($i,'value $i');"
}
execsql {
COMMIT;
}
db close
sqlite_orig db test.db
catchsql {
PRAGMA key = 'testkey';
PRAGMA cipher_fail_next_encrypt = 1;
UPDATE t1 SET b = 'fail' WHERE a = 5000;
}
db close
sqlite_orig db test.db
execsql {
PRAGMA cipher_fail_next_encrypt = 0;
PRAGMA key = 'testkey';
PRAGMA integrity_check;
PRAGMA cipher_integrity_check;
}
} {ok ok}
db close
file delete -force test.db
do_test codec-error-journal-wal {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA journal_mode = WAL;
CREATE table t1(a INTEGER PRIMARY KEY,b);
BEGIN;
}
for {set i 1} {$i<=10000} {incr i} {
execsql "INSERT INTO t1(a,b) VALUES($i,'value $i');"
}
execsql {
COMMIT;
}
db close
sqlite_orig db test.db
catchsql {
PRAGMA key = 'testkey';
PRAGMA cipher_fail_next_encrypt = 1;
UPDATE t1 SET b = 'fail' WHERE a = 5000;
}
db close
sqlite_orig db test.db
execsql {
PRAGMA cipher_fail_next_encrypt = 0;
PRAGMA key = 'testkey';
-- PRAGMA integrity_check;
PRAGMA cipher_integrity_check;
}
} {ok ok}
db close
file delete -force test.db
finish_test