Skip to content

Commit

Permalink
Prevent crashes due to uninitialized cipher tables
Browse files Browse the repository at this point in the history
Using the cipher configuration API functions could lead to crashes, if the global cipher tables were not yet initialized.
  • Loading branch information
utelle committed Nov 9, 2023
1 parent 7b3ef7a commit a6f98ba
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/cipher_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
int hasMaxPrefix = 0;
CipherParams* param;

#ifndef SQLITE_OMIT_AUTOINIT
if (sqlite3_initialize()) return value;
#endif

if (paramName == NULL || (db == NULL && newValue >= 0))
{
return value;
Expand Down Expand Up @@ -134,14 +138,22 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
SQLITE_API int
sqlite3mc_cipher_count()
{
#ifndef SQLITE_OMIT_AUTOINIT
if (sqlite3_initialize()) return 0;
#endif
return sqlite3mcGetGlobalCipherCount();
}

SQLITE_API int
sqlite3mc_cipher_index(const char* cipherName)
{
int count = sqlite3mcGetGlobalCipherCount();
int j = 0;
int count;
int j;
#ifndef SQLITE_OMIT_AUTOINIT
if (sqlite3_initialize()) return -1;
#endif
count = sqlite3mcGetGlobalCipherCount();
j = 0;
for (j = 0; j < count && globalCodecDescriptorTable[j].m_name[0] != 0; ++j)
{
if (sqlite3_stricmp(cipherName, globalCodecDescriptorTable[j].m_name) == 0) break;
Expand All @@ -153,8 +165,13 @@ SQLITE_API const char*
sqlite3mc_cipher_name(int cipherIndex)
{
static char cipherName[CIPHER_NAME_MAXLEN] = "";
int count = sqlite3mcGetGlobalCipherCount();
int j = 0;
int count;
int j;
#ifndef SQLITE_OMIT_AUTOINIT
if( sqlite3_initialize() ) return cipherName;
#endif
count = sqlite3mcGetGlobalCipherCount();
j = 0;
cipherName[0] = '\0';
if (cipherIndex > 0 && cipherIndex <= count)
{
Expand All @@ -179,6 +196,10 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
CipherParams* cipherParamTable = NULL;
int j = 0;

#ifndef SQLITE_OMIT_AUTOINIT
if (sqlite3_initialize()) return value;
#endif

if (cipherName == NULL || paramName == NULL)
{
sqlite3_log(SQLITE_WARNING,
Expand Down Expand Up @@ -246,7 +267,7 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
{
sqlite3mcConfigureSQLCipherVersion(db, hasDefaultPrefix, newValue);
}
else
else if (newValue != -1)
{
sqlite3_log(SQLITE_WARNING,
"sqlite3mc_config_cipher: SQLCipher legacy version %d out of range [%d..%d]",
Expand Down Expand Up @@ -282,7 +303,7 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
param->m_value = newValue;
value = newValue;
}
else
else if (newValue != -1)
{
sqlite3_log(SQLITE_WARNING,
"sqlite3mc_config_cipher: Value %d for parameter '%s' of cipher '%s' out of range [%d..%d]",
Expand All @@ -306,6 +327,9 @@ SQLITE_API unsigned char*
sqlite3mc_codec_data(sqlite3* db, const char* zDbName, const char* paramName)
{
unsigned char* result = NULL;
#ifndef SQLITE_OMIT_AUTOINIT
if (sqlite3_initialize()) return NULL;
#endif
if (db != NULL && paramName != NULL)
{
int iDb = (zDbName != NULL) ? sqlite3FindDbName(db, zDbName) : 0;
Expand Down

0 comments on commit a6f98ba

Please sign in to comment.