From 16c3457e3a9466fa8c48071624a5fac23c58e0eb Mon Sep 17 00:00:00 2001 From: yeji01kim Date: Thu, 9 Feb 2017 19:49:13 +0900 Subject: [PATCH] Ext4 engine: Add key initialization fix the file corruption after applying ext4 encryption. initialize key array to prevent garbage value get in. Change-Id: I83e0195793e0dc98b889e0fad747d39467da110b Signed-off-by: yeji01kim --- server/engine/encryption/ext4-engine.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) mode change 100644 => 100755 server/engine/encryption/ext4-engine.cpp diff --git a/server/engine/encryption/ext4-engine.cpp b/server/engine/encryption/ext4-engine.cpp old mode 100644 new mode 100755 index 0abd19c..212f396 --- a/server/engine/encryption/ext4-engine.cpp +++ b/server/engine/encryption/ext4-engine.cpp @@ -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); -- 2.7.4