Fix static analysis issue
[platform/core/security/ode.git] / server / internal-encryption.cpp
index ee1c95d..58edac5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2015-2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2015-2023 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -225,7 +225,7 @@ void stopSystemdUnits()
                }
        }
 
-       for (auto unit : preprocessUnits) {
+       for (auto &unit : preprocessUnits) {
                stopUnit(unit);
        }
 
@@ -366,7 +366,7 @@ void killProcesses(std::vector<const char*> &args)
                while ((::getline(&line, &len, fp.get())) != -1) {
                        std::string log(line);
                        log.erase(std::find_if(log.rbegin(), log.rend(),
-                                               std::ptr_fun<int, int>(std::isgraph)).base(), log.end());
+                                               [](unsigned char c){ return std::isgraph(c); }).base(), log.end());
                        WARN(SINK, log);
                }
        } catch (std::exception &e) {
@@ -453,7 +453,7 @@ InternalEncryptionServer::InternalEncryptionServer(ServerContext& srv,
 
        std::string source = findDevPath();
 
-       if (getState() == State::Encrypted) {
+       if (getStateInternal() == State::Encrypted) {
                //"error_partially_encrypted"
                if (!FileFooter::exist(source) && !UpgradeSupport::checkUpgradeFlag()) {
                        // Trigger key migration process
@@ -461,13 +461,9 @@ InternalEncryptionServer::InternalEncryptionServer(ServerContext& srv,
                }
        }
 
-       engine.reset(new INTERNAL_ENGINE(
-               source, INTERNAL_PATH,
-               ProgressBar([](unsigned v) {
-                       ::vconf_set_str(VCONFKEY_ODE_ENCRYPT_PROGRESS,
-                                                       std::to_string(v).c_str());
-               })
-       ));
+       engine.reset(new INTERNAL_ENGINE(source,
+                                        INTERNAL_PATH,
+                                        ProgressBar(VCONFKEY_ODE_ENCRYPT_PROGRESS)));
 
        try {
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
@@ -497,6 +493,8 @@ int InternalEncryptionServer::migrateMasterKey(const std::string& dev, const std
 
 int InternalEncryptionServer::setMountPassword(const std::string& password)
 {
+       RequestLifetime rl(server);
+
        const std::string& dev = engine->getSource();
 
        // check if upgrade flag exists
@@ -514,6 +512,8 @@ int InternalEncryptionServer::setMountPassword(const std::string& password)
 
 int InternalEncryptionServer::mount(const std::vector<unsigned char> &mk, unsigned int options)
 {
+       RequestLifetime rl(server);
+
        if (mountKey.empty() && mk.empty()) {
                ERROR(SINK, "You need to set master key first.");
                return error::NoData;
@@ -522,7 +522,7 @@ int InternalEncryptionServer::mount(const std::vector<unsigned char> &mk, unsign
        BinaryData key = mk.empty() ? mountKey : mk;
        mountKey.clear();
 
-       if (getState() != State::Encrypted) {
+       if (getStateInternal() != State::Encrypted) {
                ERROR(SINK, "Cannot mount, SD partition's state incorrect.");
                return error::NoSuchDevice;
        }
@@ -550,6 +550,8 @@ int InternalEncryptionServer::mount(const std::vector<unsigned char> &mk, unsign
 
 int InternalEncryptionServer::isMounted()
 {
+       RequestLifetime rl(server);
+
        int ret = 0;
        try {
                ret = engine->isMounted() ? 1 : 0;
@@ -562,7 +564,9 @@ int InternalEncryptionServer::isMounted()
 
 int InternalEncryptionServer::umount()
 {
-       if (getState() != State::Encrypted) {
+       RequestLifetime rl(server);
+
+       if (getStateInternal() != State::Encrypted) {
                ERROR(SINK, "Cannot umount, partition's state incorrect.");
                return error::NoSuchDevice;
        }
@@ -588,7 +592,9 @@ int InternalEncryptionServer::umount()
 
 int InternalEncryptionServer::prepareEncryption(unsigned int options)
 {
-       if (getState() != State::Unencrypted) {
+       RequestLifetime rl(server);
+
+       if (getStateInternal() != State::Unencrypted) {
                ERROR(SINK, "Cannot encrypt, partition's state incorrect.");
                return error::NoSuchDevice;
        }
@@ -601,7 +607,7 @@ int InternalEncryptionServer::prepareEncryption(unsigned int options)
                return error::Unknown;
        }
 
-       setOptions(options & getSupportedOptions());
+       setOptions(options & engine->getSupportedOptions());
 
        ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "prepared_encryption");
        ::sync();
@@ -612,7 +618,9 @@ int InternalEncryptionServer::prepareEncryption(unsigned int options)
 
 int InternalEncryptionServer::prepareDecryption()
 {
-       if (getState() != State::Encrypted) {
+       RequestLifetime rl(server);
+
+       if (getStateInternal() != State::Encrypted) {
                ERROR(SINK, "Cannot decrypt, partition's state incorrect.");
                return error::NoSuchDevice;
        }
@@ -634,8 +642,10 @@ int InternalEncryptionServer::prepareDecryption()
 
 int InternalEncryptionServer::encrypt(const std::string& password, unsigned int options)
 {
-       if (getState() != State::Unencrypted
-                       && getState() != State::PreparedEncryption) {
+       RequestLifetime rl(server);
+
+       int state = getStateInternal();
+       if (state != State::Unencrypted && state != State::PreparedEncryption) {
                ERROR(SINK, "Cannot encrypt, partition's state incorrect.");
                return error::NoSuchDevice;
        }
@@ -645,7 +655,7 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
        if (ret != error::None)
                return ret;
 
-       auto encryptWorker = [masterKey, options, this]() {
+       auto encryptWorker = [masterKey, options, this](RequestLifetime&& rl) {
                try {
                        if (::device_power_request_lock(POWER_LOCK_DISPLAY, 0) != 0)
                                ERROR(SINK, "Failed to request to lock display");
@@ -654,7 +664,7 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
                        ::sleep(1);
 
                        runtime::File file("/opt/etc/.odeprogress");
-                       if (getState() == State::Unencrypted) {
+                       if (getStateInternal() == State::Unencrypted) {
                                /* For backward compatibility */
                                file.create(MODE_0640);
                                std::string source = engine->getSource();
@@ -667,7 +677,7 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
                                        INFO(SINK, "Unmounting internal storage.");
                                        unmountInternalStorage(source);
                                }
-                               setOptions(options & getSupportedOptions());
+                               setOptions(options & engine->getSupportedOptions());
                        }
 
                        INFO(SINK, "Encryption started.");
@@ -698,7 +708,7 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
                }
        };
 
-       std::thread asyncWork(encryptWorker);
+       std::thread asyncWork(encryptWorker, std::move(rl));
        asyncWork.detach();
 
        return error::None;
@@ -706,8 +716,10 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
 
 int InternalEncryptionServer::decrypt(const std::string& password)
 {
-       if (getState() != State::Encrypted
-                       && getState() != State::PreparedDecryption) {
+       RequestLifetime rl(server);
+
+       int state = getStateInternal();
+       if (state != State::Encrypted && state != State::PreparedDecryption) {
                ERROR(SINK, "Cannot decrypt, partition's state incorrect.");
                return error::NoSuchDevice;
        }
@@ -726,7 +738,7 @@ int InternalEncryptionServer::decrypt(const std::string& password)
        if (ret != error::None)
                return ret;
 
-       auto decryptWorker = [masterKey, this]() {
+       auto decryptWorker = [masterKey, this](RequestLifetime&& rl) {
                try {
                        if (::device_power_request_lock(POWER_LOCK_DISPLAY, 0) != 0)
                                ERROR(SINK, "Failed to request to lock display");
@@ -735,7 +747,7 @@ int InternalEncryptionServer::decrypt(const std::string& password)
                        ::sleep(1);
 
                        runtime::File file("/opt/etc/.odeprogress");
-                       if (getState() == State::Encrypted) {
+                       if (getStateInternal() == State::Encrypted) {
                                /* For backward compatibility */
                                file.create(MODE_0640);
 
@@ -776,7 +788,7 @@ int InternalEncryptionServer::decrypt(const std::string& password)
                }
        };
 
-       std::thread asyncWork(decryptWorker);
+       std::thread asyncWork(decryptWorker, std::move(rl));
        asyncWork.detach();
 
        return error::None;
@@ -784,7 +796,9 @@ int InternalEncryptionServer::decrypt(const std::string& password)
 
 int InternalEncryptionServer::recovery()
 {
-       int state = getState();
+       RequestLifetime rl(server);
+
+       int state = getStateInternal();
 
        if (state == State::Unencrypted)
                return error::NoSuchDevice;
@@ -844,6 +858,27 @@ int InternalEncryptionServer::verifyPassword(const std::string& password)
 
 int InternalEncryptionServer::getState()
 {
+       RequestLifetime rl(server);
+
+       return getStateInternal();
+}
+
+unsigned int InternalEncryptionServer::getSupportedOptions()
+{
+       RequestLifetime rl(server);
+
+       return engine->getSupportedOptions();
+}
+
+std::string InternalEncryptionServer::getDevicePath() const
+{
+       RequestLifetime rl(server);
+
+       return engine->getSource();
+}
+
+int InternalEncryptionServer::getStateInternal() const
+{
        char *value = ::vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
        if (value == NULL) {
                throw runtime::Exception("Failed to get vconf value.");
@@ -866,14 +901,4 @@ int InternalEncryptionServer::getState()
        return State::NotSupported;
 }
 
-unsigned int InternalEncryptionServer::getSupportedOptions()
-{
-       return engine->getSupportedOptions();
-}
-
-std::string InternalEncryptionServer::getDevicePath() const
-{
-       return engine->getSource();
-}
-
 } // namespace ode