From: Seok Hong Date: Tue, 22 Nov 2016 01:34:13 +0000 (+0900) Subject: Add Ext4 Tool X-Git-Tag: submit/tizen/20170213.020148~38 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5b41eda9259fc8c5b9e637dd1230a2de77324927;p=platform%2Fcore%2Fsecurity%2Fode.git Add Ext4 Tool Change-Id: I23df2c8adbd31c100e0b28dcd23271f840af34d3 Signed-off-by: Seok Hong --- diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index d67cda9..94a688d 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -15,6 +15,7 @@ # SET(SERVER_SRCS main.cpp server.cpp + ext4-tool.cpp secure-erase.cpp internal-encryption.cpp external-encryption.cpp diff --git a/server/ext4-tool.cpp b/server/ext4-tool.cpp new file mode 100644 index 0000000..783c084 --- /dev/null +++ b/server/ext4-tool.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +#include + +#include +#include + +#include +#include +#include +#include + +#include "ext4-tool.h" + +namespace ode { + +#define ODE_SUPERBLOCK_OFFSET 1024 +#define ODE_SUPERBLOCK_SIZE 1024 +#define ODE_EXT2_MIN_DESC_SIZE 32 +#define ODE_EXT2_MIN_BLOCK_LOG_SIZE 10 /* 1024 */ +#define ODE_EXT2_MIN_BLOCK_SIZE (1 << ODE_EXT2_MIN_BLOCK_LOG_SIZE) + +static unsigned int divCeilSafely(unsigned int a, unsigned int b) +{ + if (!a) + return 0; + + return ((a - 1) / b) + 1; +} + +Ext4Tool::Ext4Tool(const std::string &src) : + source(src), totalBlockCount(0) +{ + unsigned int firstDataBlock = 0; + unsigned int blockSize = 0; + unsigned int blocksPerGroup = 0; + unsigned int clustersPerGroup = 0; + runtime::File device(source); + + if (device.exists() == false) + throw runtime::Exception("Source doesn't exist"); + + device.open(O_RDONLY); + + // read totalBlockCount + device.lseek(ODE_SUPERBLOCK_OFFSET + 4, SEEK_SET); + device.read(&totalBlockCount, 4); + + // read firstDataBlock + device.lseek(ODE_SUPERBLOCK_OFFSET + 20, SEEK_SET); + device.read(&firstDataBlock, 4); + + // read blockSize + device.lseek(ODE_SUPERBLOCK_OFFSET + 24, SEEK_SET); + device.read(&blockSize, 4); + blockSize = (ODE_EXT2_MIN_BLOCK_SIZE << blockSize); + + // read blocksPerGroup + device.lseek(ODE_SUPERBLOCK_OFFSET + 32, SEEK_SET); + device.read(&blocksPerGroup, 4); + + // read clustersPerGroup + device.lseek(ODE_SUPERBLOCK_OFFSET + 36, SEEK_SET); + device.read(&clustersPerGroup, 4); + + // read bitmap + bitmap.resize(totalBlockCount + 1); + + unsigned int groupDescCount = divCeilSafely(totalBlockCount - firstDataBlock, blocksPerGroup); + int blockNbytes = clustersPerGroup / 8; + + // read group_desc + unsigned int descPerBlock = blockSize / ODE_EXT2_MIN_DESC_SIZE; + unsigned int descBlockCount = divCeilSafely(groupDescCount, descPerBlock); + + // read first meta block + data group_desc(descBlockCount * blockSize); + device.lseek((firstDataBlock + 1) * blockSize, SEEK_SET); + device.read(group_desc.data(), (descBlockCount * blockSize)); + + unsigned int cnt = 0; + unsigned int blkItr = firstDataBlock; + + // this structure just is used for easy type-casting. + struct odeExtGroupDesc { + unsigned int blockBitmap; /* Blocks bitmap block */ + /* skip other member */ + }; + + for (unsigned int i = 0; i < groupDescCount; i++) { + data block_bitmap(blockSize); + + unsigned int blk = (((struct odeExtGroupDesc *)(((unsigned char *)(group_desc.data())) + i * ODE_EXT2_MIN_DESC_SIZE))->blockBitmap); + device.lseek(blk * blockSize, SEEK_SET); + device.read(block_bitmap.data(), blockSize); + + cnt = blockNbytes << 3; + memcpy(bitmap.data() + (blkItr >> 3), block_bitmap.data(), (cnt + 7) >> 3); + blkItr += blockNbytes << 3; + } + + device.close(); +} + +Ext4Tool::~Ext4Tool() +{ +} + +unsigned int Ext4Tool::getTotalBlockCount() +{ + return totalBlockCount; +} + +bool Ext4Tool::isUsedBlock(unsigned int blockIndex) +{ + unsigned char *addr = (bitmap.data() + (blockIndex >> 3)); + int mask = 1 << (blockIndex & 0x07); + + if (mask & *addr) + return true; + + return false; +} + +void Ext4Tool::forceCleanUp() +{ + std::vector args = { + "-f", + "-y", + source + }; + + runtime::Process proc("/sbin/e2fsck", args); + proc.execute(); + proc.waitForFinished(); +} + +} // namespace ode diff --git a/server/ext4-tool.h b/server/ext4-tool.h new file mode 100644 index 0000000..fb10912 --- /dev/null +++ b/server/ext4-tool.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#ifndef __EXT4_TOOL_H__ +#define __EXT4_TOOL_H__ + +#include +#include + +namespace ode { + +class Ext4Tool final { +public: + Ext4Tool(const std::string &src); + Ext4Tool(const Ext4Tool &) = delete; + Ext4Tool(Ext4Tool &&) = delete; + ~Ext4Tool(); + + Ext4Tool &operator=(const Ext4Tool &) = delete; + Ext4Tool &operator=(Ext4Tool &&) = delete; + + typedef std::vector data; + + unsigned int getTotalBlockCount(); + bool isUsedBlock(unsigned int blockIndex); + + void forceCleanUp(); + +private: + std::string source; + unsigned int totalBlockCount; + data bitmap; +}; + +} // namespace ode + +#endif // __EXT4_TOOL_H__