From 41fa3d34e0be9e6f8cf47f4cbb5272fc6580f4fa Mon Sep 17 00:00:00 2001 From: Abhishek Vijay Date: Thu, 24 May 2018 21:23:21 +0530 Subject: [PATCH] [Non-ACR][Preventing SQL Injection Attack] Change-Id: I174d9983dc54cde93e32c87d04d600a0830b8f7e Signed-off-by: Abhishek Vijay --- include/private/AppHistoryTypes.h | 7 ++++++- src/server/usage-stats/InstallMonitor.cpp | 35 ++++++++++++++++++++++--------- src/server/usage-stats/InstallMonitor.h | 8 +++++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/include/private/AppHistoryTypes.h b/include/private/AppHistoryTypes.h index 95a64e6..b6615b2 100644 --- a/include/private/AppHistoryTypes.h +++ b/include/private/AppHistoryTypes.h @@ -131,7 +131,12 @@ #define VAL_ALERTING VAL_CONNECTING #define VAL_INCOMING VAL_CONNECTING - #define COLUMN_NAME_DELIMITER "," +#define APPHISTORY_SQL_LEN_MAX 256 +#define APPHISTORY_SNPRINTF(dest, size, format, arg...)\ + do {\ + snprintf(dest, size-1, format, ##arg);\ + } while (0) + #endif diff --git a/src/server/usage-stats/InstallMonitor.cpp b/src/server/usage-stats/InstallMonitor.cpp index eda93cf..10ef7b4 100644 --- a/src/server/usage-stats/InstallMonitor.cpp +++ b/src/server/usage-stats/InstallMonitor.cpp @@ -80,23 +80,38 @@ void ctx::AppInstallMonitor::__packageEventCb(const char *type, const char *pack bool ctx::AppInstallMonitor::__appInfoCb(package_info_app_component_type_e compType, const char *appId, void *userData) { Database* database = DatabaseManager::getInstance(); + int count = 1; if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL) { - std::stringstream query; - query << "INSERT INTO " APP_TABLE_REMOVABLE_APP " (" KEY_APP_ID ") VALUES ('" << appId << "')"; - database->execute(query.str(), NULL); + apphistory_stmt hstmt = NULL; + char query[APPHISTORY_SQL_LEN_MAX] = {0, }; + + APPHISTORY_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId) VALUES " "(?) ", APP_TABLE_REMOVABLE_APP); + + hstmt = database->prepare_query(query); + database->query_bind_text(hstmt, count, appId); + database->execute(hstmt); + } else if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL) { - database->execute(__createDeletionQuery(APP_TABLE_REMOVABLE_APP, appId), NULL); - database->execute(__createDeletionQuery(APP_TABLE_USAGE_LOG, appId), NULL); + database->execute(__createDeletionQuery(APP_TABLE_REMOVABLE_APP, appId)); + database->execute(__createDeletionQuery(APP_TABLE_USAGE_LOG, appId)); } return true; } -std::string ctx::AppInstallMonitor::__createDeletionQuery(const char* tableName, const char* appId) +apphistory_stmt ctx::AppInstallMonitor::__createDeletionQuery(const char* tableName, const char* appId) { - std::stringstream query; - query << "DELETE FROM " << tableName; - query << " WHERE " KEY_APP_ID " = '" << appId << "'"; - return query.str(); + Database *database = DatabaseManager::getInstance(); + int count = 1; + + apphistory_stmt hstmt = NULL; + char query[APPHISTORY_SQL_LEN_MAX] = {0, }; + + APPHISTORY_SNPRINTF(query, sizeof(query), "DELETE FROM %s(AppId) WHERE KEY_APP_ID = ? ", tableName); + + hstmt = database->prepare_query(query); + database->query_bind_text(hstmt, count, appId); + return hstmt; } + diff --git a/src/server/usage-stats/InstallMonitor.h b/src/server/usage-stats/InstallMonitor.h index 123868f..94fbac6 100644 --- a/src/server/usage-stats/InstallMonitor.h +++ b/src/server/usage-stats/InstallMonitor.h @@ -19,6 +19,9 @@ #include #include +#include + +typedef sqlite3_stmt* apphistory_stmt; namespace ctx { @@ -29,8 +32,9 @@ namespace ctx { bool __startMonitoring(); void __stopMonitoring(); - static std::string __createDeletionQuery(const char* tableName, const char* appId); - static void __packageEventCb(const char *type, const char *package, package_manager_event_type_e eventType, package_manager_event_state_e eventState, int progress, package_manager_error_e error, void *userData); + static apphistory_stmt __createDeletionQuery(const char* tableName, const char* appId); + static void __packageEventCb(const char *type, const char *package, package_manager_event_type_e eventType, + package_manager_event_state_e eventState, int progress, package_manager_error_e error, void *userData); static bool __appInfoCb(package_info_app_component_type_e compType, const char *appId, void *userData); public: -- 2.7.4