1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-01-23 15:07:03 +01:00

gpg: Make the tofu DB check and initialization atomic.

* g10/tofu.c (initdb): Make the version check and the database
initialization atomic.

--
Signed-off-by: Neal H. Walfield <neal@g10code.com>
Co-authored-by: Andre Heinecke <aheinecke@intevation.de>
This commit is contained in:
Neal H. Walfield 2015-10-20 20:42:44 +02:00
parent 485e0a221d
commit 85bd7d9491

View File

@ -248,6 +248,15 @@ initdb (sqlite3 *db, enum db_type type)
unsigned long int count; unsigned long int count;
int version = -1; int version = -1;
rc = sqlite3_exec (db, "begin transaction;", NULL, NULL, &err);
if (rc)
{
log_error (_("error beginning transaction on TOFU database: %s\n"),
err);
sqlite3_free (err);
return 1;
}
/* If the DB has no tables, then assume this is a new DB that needs /* If the DB has no tables, then assume this is a new DB that needs
to be initialized. */ to be initialized. */
rc = sqlite3_exec (db, rc = sqlite3_exec (db,
@ -258,7 +267,7 @@ initdb (sqlite3 *db, enum db_type type)
log_error (_("error querying TOFU DB's available tables: %s\n"), log_error (_("error querying TOFU DB's available tables: %s\n"),
err); err);
sqlite3_free (err); sqlite3_free (err);
return 1; goto out;
} }
else if (count != 0) else if (count != 0)
/* Assume that the DB is already initialized. Make sure the /* Assume that the DB is already initialized. Make sure the
@ -270,21 +279,22 @@ initdb (sqlite3 *db, enum db_type type)
/* Happy, happy, joy, joy. */ /* Happy, happy, joy, joy. */
{ {
sqlite3_free (err); sqlite3_free (err);
return 0; rc = 0;
goto out;
} }
else if (rc == SQLITE_ABORT && version == -1) else if (rc == SQLITE_ABORT && version == -1)
/* Unsupported version. */ /* Unsupported version. */
{ {
/* An error message was already displayed. */ /* An error message was already displayed. */
sqlite3_free (err); sqlite3_free (err);
return 1; goto out;
} }
else if (rc) else if (rc)
/* Some error. */ /* Some error. */
{ {
log_error (_("error determining TOFU DB's version: %s\n"), err); log_error (_("error determining TOFU DB's version: %s\n"), err);
sqlite3_free (err); sqlite3_free (err);
return 1; goto out;
} }
else else
/* Unexpected success. This can only happen if there are no /* Unexpected success. This can only happen if there are no
@ -292,19 +302,11 @@ initdb (sqlite3 *db, enum db_type type)
{ {
log_error (_("error determining TOFU DB's version: %s\n"), log_error (_("error determining TOFU DB's version: %s\n"),
"select returned 0, but expected ABORT"); "select returned 0, but expected ABORT");
return 1; rc = 1;
goto out;
} }
} }
rc = sqlite3_exec (db, "begin transaction;", NULL, NULL, &err);
if (rc)
{
log_error (_("error beginning transaction on TOFU database: %s\n"),
err);
sqlite3_free (err);
return 1;
}
/* Create the version table. */ /* Create the version table. */
rc = sqlite3_exec (db, rc = sqlite3_exec (db,
"create table version (version INTEGER);", "create table version (version INTEGER);",