Ext4 engine: Add key initialization 62/113962/5
authoryeji01kim <yeji01.kim@samsung.com>
Thu, 9 Feb 2017 10:49:13 +0000 (19:49 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Mon, 13 Feb 2017 05:31:02 +0000 (14:31 +0900)
fix the file corruption after applying ext4 encryption.
initialize key array to prevent garbage value get in.

Change-Id: I83e0195793e0dc98b889e0fad747d39467da110b
Signed-off-by: yeji01kim <yeji01.kim@samsung.com>
server/engine/encryption/ext4-engine.cpp [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 0abd19c..212f396
@@ -139,8 +139,8 @@ void addKeyToKeyring(const Ext4Engine::data& key)
                return;
        }
        ext4Key.mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
-       ::memcpy(ext4Key.raw, key.data(), EXT4_MAX_KEY_SIZE);
-       std::string ext4KeyRaw = ext4Key.raw;
+       ::memset(ext4Key.raw, 0, EXT4_MAX_KEY_SIZE);
+       ::memcpy(ext4Key.raw, key.data(), key.size());
        ext4Key.size = EXT4_MAX_KEY_SIZE;
 
        ret = ::syscall(__NR_add_key, ext4KeyringType.c_str(), keyRefFull.c_str(),
@@ -183,7 +183,7 @@ int Ext4Engine::copy(std::string& src, std::string& dest)
        if (::chown(dest.c_str(), st.st_uid, st.st_gid) == -1)
                throw runtime::Exception(dest + runtime::GetSystemErrorMessage());
        if (::sendfile(writeFd, readFd, 0, st.st_size) == -1)
-               return 1;
+               throw runtime::Exception(runtime::GetSystemErrorMessage());
 
        /* progress bar update */
        curSize += st.st_size;
@@ -392,22 +392,22 @@ static bool prepareEncryptDir(std::string& sourceName, std::string& destName)
        return true;
 }
 
-static bool getPolicy(const std::string& dirName)
+static int getPolicy(const std::string& dirName)
 {
        struct ext4_encryption_policy policy;
        int fd, rc;
 
        fd = ::open(dirName.c_str(), O_DIRECTORY);
        if (fd == -1)
-               return false;
+               return 0;
 
        rc = ::ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &policy);
        close(fd);
        if (rc) {
                ERROR("ioctl error");
-               return false;
+               return 0;
        }
-       return true;
+       return 1;
 }
 
 Ext4Engine::Ext4Engine(const std::string& src, const std::string& dest, const ProgressBar &prgsBar) :
@@ -470,8 +470,8 @@ void Ext4Engine::decrypt(const Ext4Engine::data& key, unsigned int options)
 {
        std::string destDir = getDestination();
 
-       if (!getPolicy(bindMountPoint))
-               throw runtime::Exception("directory isn't encrypted");
+       if (getPolicy(bindMountPoint) != 1)
+               throw runtime::Exception("failed get policy");
        addKeyToKeyring(key);
 
        preScanDir(bindMountPoint);