Skip to content

Commit e89ac7c

Browse files
committed
Fix #50 and #51
If some of the cipher schemes are excluded from compilation, executing cipher-related pragmas could lead to crashes due to wrong indexing based on the numeric cipher ids. Numeric cipher ids are now handled correctly. Note: The default configuration with all cipher schemes enabled is not affected.
1 parent 1351f06 commit e89ac7c

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/cipher_common.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ sqlite3mcCloneCodecParameterTable()
9696
{
9797
CipherParams* params = globalCodecParameterTable[j].m_params;
9898
cloneCodecParams[j].m_name = globalCodecParameterTable[j].m_name;
99+
cloneCodecParams[j].m_id = globalCodecParameterTable[j].m_id;
99100
cloneCodecParams[j].m_params = &cloneCipherParams[offset];
100101
for (n = 0; strlen(params[n].m_name) > 0; ++n);
101102
/* Copy all parameters of the current table (including sentinel) */
@@ -110,6 +111,7 @@ sqlite3mcCloneCodecParameterTable()
110111
offset += (n + 1);
111112
}
112113
cloneCodecParams[nTables].m_name = globalCodecParameterTable[nTables].m_name;
114+
cloneCodecParams[nTables].m_id = globalCodecParameterTable[nTables].m_id;
113115
cloneCodecParams[nTables].m_params = NULL;
114116
}
115117
else
@@ -193,8 +195,20 @@ sqlite3mcGetCipherType(sqlite3* db)
193195
SQLITE_PRIVATE CipherParams*
194196
sqlite3mcGetCipherParams(sqlite3* db, int cypherType)
195197
{
198+
int j = 0;
196199
CodecParameter* codecParams = (db != NULL) ? sqlite3mcGetCodecParams(db) : globalCodecParameterTable;
197-
CipherParams* cipherParamTable = (codecParams != NULL) ? codecParams[cypherType].m_params : globalCodecParameterTable[cypherType].m_params;
200+
if (codecParams == NULL)
201+
{
202+
codecParams = globalCodecParameterTable;
203+
}
204+
if (cypherType > 0)
205+
{
206+
for (j = 1; codecParams[j].m_id > 0; ++j)
207+
{
208+
if (cypherType == codecParams[j].m_id) break;
209+
}
210+
}
211+
CipherParams* cipherParamTable = codecParams[j].m_params;
198212
return cipherParamTable;
199213
}
200214

src/cipher_config.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,23 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
9797
value = (hasDefaultPrefix) ? param->m_default : (hasMinPrefix) ? param->m_minValue : (hasMaxPrefix) ? param->m_maxValue : param->m_value;
9898
if (!hasMinPrefix && !hasMaxPrefix && newValue >= 0 && newValue >= param->m_minValue && newValue <= param->m_maxValue)
9999
{
100-
/* Do not allow to change the default value for parameter "hmac_check" */
101-
if (hasDefaultPrefix && (sqlite3_stricmp(paramName, "hmac_check") != 0))
100+
int allowChange = 1;
101+
/* Allow cipher change only if new cipher is actually available */
102+
if (sqlite3_stricmp(paramName, "cipher") == 0)
102103
{
103-
param->m_default = newValue;
104+
allowChange = (codecDescriptorTable[newValue - 1] != &mcDummyDescriptor);
105+
}
106+
107+
if (allowChange)
108+
{
109+
/* Do not allow to change the default value for parameter "hmac_check" */
110+
if (hasDefaultPrefix && (sqlite3_stricmp(paramName, "hmac_check") != 0))
111+
{
112+
param->m_default = newValue;
113+
}
114+
param->m_value = newValue;
115+
value = newValue;
104116
}
105-
param->m_value = newValue;
106-
value = newValue;
107117
}
108118
if (db != NULL)
109119
{
@@ -758,27 +768,29 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
758768
pragmaValue = ((char**) pArg)[2];
759769
if (sqlite3StrICmp(pragmaName, "cipher") == 0)
760770
{
761-
int j = -1;
771+
int cipherId = -1;
762772
if (pragmaValue != NULL)
763773
{
774+
int j = 1;
764775
/* Try to locate the cipher name */
765776
for (j = 1; strlen(globalCodecParameterTable[j].m_name) > 0; ++j)
766777
{
767778
if (sqlite3_stricmp(pragmaValue, globalCodecParameterTable[j].m_name) == 0) break;
768779
}
780+
cipherId = (strlen(globalCodecParameterTable[j].m_name) > 0) ? globalCodecParameterTable[j].m_id : CODEC_TYPE_UNKNOWN;
769781
}
770782

771-
/* j is the index of the cipher name, if found */
772-
if ((j == -1) || (strlen(globalCodecParameterTable[j].m_name) > 0))
783+
/* cipherId is the numeric id of the cipher name, if found */
784+
if ((cipherId == -1) || (cipherId > 0 && cipherId <= CODEC_TYPE_MAX))
773785
{
774786
int value;
775787
if (configDefault)
776788
{
777-
value = sqlite3mc_config(db, "default:cipher", j);
789+
value = sqlite3mc_config(db, "default:cipher", cipherId);
778790
}
779791
else
780792
{
781-
value = sqlite3mc_config(db, "cipher", j);
793+
value = sqlite3mc_config(db, "cipher", cipherId);
782794
}
783795
rc = SQLITE_OK;
784796
((char**)pArg)[0] = sqlite3_mprintf("%s", codecDescriptorTable[value - 1]->m_name);

0 commit comments

Comments
 (0)