Skip to content

SQLCipher segfaults on sqlite3_key(NULL, NULL, 0) with v4.7.0+, but did not segfault on v4.6.1 #578

@hosiet

Description

@hosiet

On Debian systems, for the following minimal program:

#include <stdio.h>
#include "/usr/include/sqlcipher/sqlite3.h"

//extern int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);

int main(void)
{
    printf("Calling sqlite3_key(NULL, NULL, 0)...\n");

    int rc = sqlite3_key(NULL, NULL, 0);

    printf("Return code: %d\n", rc);
    return 0;
}

We build it from source using gcc test_key_null.c -o test_key_null $(pkg-config --cflags --libs sqlcipher) -I/usr/include -DSQLITE_HAS_CODEC:

When building with sqlcipher 4.6.1:

-> % ./test_key_null
Calling sqlite3_key(NULL, NULL, 0)...
Return code: 1

When building with sqlcipher 4.11.0:

-> % ./test_key_null
Calling sqlite3_key(NULL, NULL, 0)...
[1]    2190 segmentation fault  ./test_key_null

Looks like it was caused by 291dc9b . I wonder whether this behaviour is a regression or intentional.

You may argue that this usage is completely wrong, but it exists in real projects. For example, in KDE's KMyMoney v5.2.1 release:

Maybe it will be better to add a guard check in sqlite3_key_v2() function to fail early when db is NULL, instead of plain segfaults directly in sqlcipher_find_db_index(). For example, something like:

if (db == NULL) {
    return SQLITE_MISUSE;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions