Skip to content

content-sqlite: verify database integrity during module load #4340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/modules/content-sqlite/content-sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,14 @@ static int content_sqlite_opendb (struct content_sqlite *ctx, bool truncate)
log_sqlite_error (ctx, "setting sqlite 'locking_mode' pragma");
goto error;
}
if (sqlite3_exec (ctx->db,
"PRAGMA quick_check",
NULL,
NULL,
NULL) != SQLITE_OK) {
log_sqlite_error (ctx, "setting sqlite 'quick_check' pragma");
goto error;
}
if (sqlite3_exec (ctx->db,
sql_create_table,
NULL,
Expand Down Expand Up @@ -832,16 +840,16 @@ int mod_main (flux_t *h, int argc, char **argv)
if (content_register_backing_store (h, "content-sqlite") < 0)
goto done;
if (content_register_service (h, "content-backing") < 0)
goto done;
goto done_unreg;
if (content_register_service (h, "kvs-checkpoint") < 0)
goto done;
goto done_unreg;
if (flux_reactor_run (flux_get_reactor (h), 0) < 0) {
flux_log_error (h, "flux_reactor_run");
goto done;
goto done_unreg;
}
if (content_unregister_backing_store (h) < 0)
goto done;
rc = 0;
done_unreg:
(void)content_unregister_backing_store (h);
done:
content_sqlite_closedb (ctx);
content_sqlite_destroy (ctx);
Expand Down
33 changes: 33 additions & 0 deletions t/t0012-content-sqlite.t
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,39 @@ test_expect_success 'run flux with statedir and verify modes' '
grep "journal_mode=WAL synchronous=NORMAL" logs4
'

# Will create in WAL mode since statedir is set
recreate_database()
{
flux start -o,-Sbroker.rc1_path=,-Sbroker.rc3_path= \
-o,-Sstatedir=$(pwd) bash -c \
"flux module load content-sqlite truncate; \
flux module remove content-sqlite"
}
load_module_xfail()
{
flux start -o,-Sbroker.rc1_path=,-Sbroker.rc3_path= \
-o,-Sstatedir=$(pwd) flux module load content-sqlite
}

# FWIW https://www.sqlite.org/fileformat.html
test_expect_success 'create database with bad header magic' '
recreate_database &&
echo "xxxxxxxxxxxxxxxx" | dd obs=1 count=16 seek=0 of=content.sqlite
'
test_expect_success 'module load fails with corrupt database' '
test_must_fail load_module_xfail
'
test_expect_success 'create database with bad schema format number' '
recreate_database &&
echo "\001\001\001\001" | dd obs=1 count=4 seek=44 of=content.sqlite
'
test_expect_success 'module load fails with corrupt database' '
test_must_fail load_module_xfail
'
test_expect_success 'full instance start fails corrupt database' '
test_must_fail flux start -o,-Sstatedir=$(pwd) /bin/true
'

test_expect_success 'remove content-sqlite module on rank 0' '
flux module remove content-sqlite
'
Expand Down