Skip to content

Commit 45475c4

Browse files
committed
fix issue where memory PRAGMA cipher_memory_security would report OFF when it was actually ON
1 parent 7778787 commit 45475c4

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/crypto_impl.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ static volatile int default_page_size = 4096;
7474
static volatile int default_plaintext_header_sz = 0;
7575
static volatile int default_hmac_algorithm = SQLCIPHER_HMAC_SHA512;
7676
static volatile int default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512;
77-
static volatile int mem_security_on = 0;
78-
static volatile int mem_security_initialized = 0;
79-
static volatile int mem_security_activated = 0;
77+
static volatile int sqlcipher_mem_security_on = 0;
78+
static volatile int sqlcipher_mem_executed = 0;
79+
static volatile int sqlcipher_mem_initialized = 0;
8080
static volatile unsigned int sqlcipher_activate_count = 0;
8181
static volatile sqlite3_mem_methods default_mem_methods;
8282
static sqlcipher_provider *default_provider = NULL;
@@ -99,10 +99,10 @@ static void sqlcipher_mem_shutdown(void *pAppData) {
9999
}
100100
static void *sqlcipher_mem_malloc(int n) {
101101
void *ptr = default_mem_methods.xMalloc(n);
102-
if(mem_security_on) {
102+
if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1;
103+
if(sqlcipher_mem_security_on) {
103104
sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mem_malloc: calling sqlcipher_mlock(%p,%d)", ptr, n);
104105
sqlcipher_mlock(ptr, n);
105-
if(!mem_security_activated) mem_security_activated = 1;
106106
}
107107
return ptr;
108108
}
@@ -111,19 +111,19 @@ static int sqlcipher_mem_size(void *p) {
111111
}
112112
static void sqlcipher_mem_free(void *p) {
113113
int sz;
114-
if(mem_security_on) {
114+
if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1;
115+
if(sqlcipher_mem_security_on) {
115116
sz = sqlcipher_mem_size(p);
116117
sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mem_free: calling sqlcipher_memset(%p,0,%d) and sqlcipher_munlock(%p, %d)", p, sz, p, sz);
117118
sqlcipher_memset(p, 0, sz);
118119
sqlcipher_munlock(p, sz);
119-
if(!mem_security_activated) mem_security_activated = 1;
120120
}
121121
default_mem_methods.xFree(p);
122122
}
123123
static void *sqlcipher_mem_realloc(void *p, int n) {
124124
void *new = NULL;
125125
int orig_sz = 0;
126-
if(mem_security_on) {
126+
if(sqlcipher_mem_security_on) {
127127
orig_sz = sqlcipher_mem_size(p);
128128
if (n==0) {
129129
sqlcipher_mem_free(p);
@@ -161,12 +161,13 @@ static sqlite3_mem_methods sqlcipher_mem_methods = {
161161
};
162162

163163
void sqlcipher_init_memmethods() {
164-
if(mem_security_initialized) return;
164+
if(sqlcipher_mem_initialized) return;
165165
if(sqlite3_config(SQLITE_CONFIG_GETMALLOC, &default_mem_methods) != SQLITE_OK ||
166166
sqlite3_config(SQLITE_CONFIG_MALLOC, &sqlcipher_mem_methods) != SQLITE_OK) {
167-
mem_security_on = mem_security_activated = 0;
167+
sqlcipher_mem_security_on = sqlcipher_mem_executed = sqlcipher_mem_initialized = 0;
168+
} else {
169+
sqlcipher_mem_initialized = 1;
168170
}
169-
mem_security_initialized = 1;
170171
}
171172

172173
int sqlcipher_register_provider(sqlcipher_provider *p) {
@@ -870,13 +871,16 @@ int sqlcipher_get_default_pagesize() {
870871
void sqlcipher_set_mem_security(int on) {
871872
/* memory security can only be enabled, not disabled */
872873
if(on) {
873-
mem_security_on = on;
874-
mem_security_activated = 0;
874+
sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_set_mem_security: on");
875+
sqlcipher_mem_security_on = on;
875876
}
876877
}
877878

878879
int sqlcipher_get_mem_security() {
879-
return mem_security_on && mem_security_activated;
880+
/* only report that memory security is enabled if pragma cipher_memory_security is ON and
881+
SQLCipher's allocator/deallocator was run at least one timecurrently used */
882+
sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_get_mem_security: sqlcipher_mem_security_on = %d, sqlcipher_mem_executed = %d", sqlcipher_mem_security_on, sqlcipher_mem_executed);
883+
return sqlcipher_mem_security_on && sqlcipher_mem_executed;
880884
}
881885

882886

0 commit comments

Comments
 (0)