From: Jaemin Ryu Date: Mon, 27 Nov 2017 05:55:11 +0000 (+0900) Subject: Add database integrity check X-Git-Tag: submit/tizen_4.0/20171129.062733~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6b715c299d8c333b4dfb2216fae02f76ff0996ce;p=platform%2Fcore%2Fsecurity%2Fklay.git Add database integrity check Change-Id: Ifa3a20b029327828a5ba53b46e7ba00305570a62 Signed-off-by: Jaemin Ryu --- diff --git a/include/klay/db/connection.h b/include/klay/db/connection.h index 641310b..a6569c2 100644 --- a/include/klay/db/connection.h +++ b/include/klay/db/connection.h @@ -32,7 +32,7 @@ public: ReadWrite = SQLITE_OPEN_READWRITE }; - Connection(const std::string& name, const int flags); + Connection(const std::string& name, const int flags, bool integrityCheck = true); ~Connection(); int exec(const std::string& query); diff --git a/src/db/connection.cpp b/src/db/connection.cpp index 0eac647..639970a 100644 --- a/src/db/connection.cpp +++ b/src/db/connection.cpp @@ -13,17 +13,40 @@ * See the License for the specific language governing permissions and * limitations under the License */ +#include + #include #include namespace database { -Connection::Connection(const std::string& name, const int flags) : +Connection::Connection(const std::string& name, const int flags, bool integrityCheck) : handle(nullptr), filename(name) { if (::sqlite3_open_v2(filename.c_str(), &handle, flags, NULL)) { throw runtime::Exception(getErrorMessage()); } + + if (integrityCheck) { + bool verified = false; + sqlite3_stmt *integrity = NULL; + if (::sqlite3_prepare_v2(handle, "PRAGMA integrity_check;", -1, &integrity, NULL) == SQLITE_OK ) { + while (::sqlite3_step(integrity) == SQLITE_ROW) { + const unsigned char *result = ::sqlite3_column_text(integrity, 0); + if (result && ::strcmp((const char *)result, (const char *)"ok") == 0) { + verified = true; + break; + } + } + + ::sqlite3_finalize(integrity); + } + + if (!verified) { + ::sqlite3_close(handle); + throw runtime::Exception("Malformed database"); + } + } } Connection::~Connection()