From 627bd655347cb3aaf04b352e4f0a0207d64c8cb1 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 16 May 2018 11:00:10 +0200 Subject: Fix memory leak (mismatch free/delete/delete[]), fix invalid memory access --- include/odhtdb/Database.hpp | 20 ++++++++++---------- include/odhtdb/DatabaseNode.hpp | 6 +++--- include/odhtdb/DatabaseStorage.hpp | 16 ++++++++-------- include/odhtdb/FileUtils.hpp | 2 +- include/odhtdb/OwnedMemory.hpp | 28 ++++++++++++++++++++++++++++ include/odhtdb/PasswordHash.hpp | 2 +- 6 files changed, 51 insertions(+), 23 deletions(-) (limited to 'include') diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index 04c3b62..a2914ac 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -113,16 +113,16 @@ namespace odhtdb class DatabaseCreateResponse { public: - DatabaseCreateResponse(std::shared_ptr nodeAdminKeyPair, std::shared_ptr nodeAdminGroupId, std::shared_ptr key, std::shared_ptr hash); + DatabaseCreateResponse(std::shared_ptr nodeAdminKeyPair, std::shared_ptr nodeAdminGroupId, std::shared_ptr key, std::shared_ptr hash); const std::shared_ptr getNodeAdminKeyPair() const; - const std::shared_ptr getNodeAdminGroupId() const; - const std::shared_ptr getNodeEncryptionKey() const; + const std::shared_ptr getNodeAdminGroupId() const; + const std::shared_ptr getNodeEncryptionKey() const; const std::shared_ptr getRequestHash() const; private: std::shared_ptr nodeAdminKeyPair; - std::shared_ptr nodeAdminGroupId; - std::shared_ptr key; + std::shared_ptr nodeAdminGroupId; + std::shared_ptr key; std::shared_ptr hash; }; @@ -194,7 +194,7 @@ namespace odhtdb // Throws DatabaseStorageWrongPassword if password for the stored user is wrong. MapHash getStoredNodeUserInfoDecrypted(const std::string &username, const std::string &password) const; - std::vector getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; + std::vector getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; void receiveCustomMessage(const dht::InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc); @@ -203,10 +203,10 @@ namespace odhtdb static dht::InfoHash getInfoHash(const void *data, usize size); private: - void deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); - void deserializeAddRequest(const std::shared_ptr &value, const Hash &requestDataHash, const std::shared_ptr &nodeHash, const std::shared_ptr encryptionKey); - bool listenCreateData(std::shared_ptr value, const Hash &hash, const std::shared_ptr encryptionKey); - bool listenAddData(std::shared_ptr value, const Hash &requestDataHash, const std::shared_ptr nodeHash, const std::shared_ptr encryptionKey); + void deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); + void deserializeAddRequest(const std::shared_ptr &value, const Hash &requestDataHash, const std::shared_ptr &nodeHash, const std::shared_ptr encryptionKey); + bool listenCreateData(std::shared_ptr value, const Hash &hash, const std::shared_ptr encryptionKey); + bool listenAddData(std::shared_ptr value, const Hash &requestDataHash, const std::shared_ptr nodeHash, const std::shared_ptr encryptionKey); private: dht::DhtRunner node; DatabaseStorage databaseStorage; diff --git a/include/odhtdb/DatabaseNode.hpp b/include/odhtdb/DatabaseNode.hpp index 620cd40..c707af9 100644 --- a/include/odhtdb/DatabaseNode.hpp +++ b/include/odhtdb/DatabaseNode.hpp @@ -11,14 +11,14 @@ namespace odhtdb public: DatabaseNode() {} - DatabaseNode(const std::shared_ptr &_encryptionKey, const std::shared_ptr &_nodeHash) : + DatabaseNode(const std::shared_ptr &_encryptionKey, const std::shared_ptr &_nodeHash) : encryptionKey(_encryptionKey), nodeHash(_nodeHash) { } - const std::shared_ptr getNodeEncryptionKey() const + const std::shared_ptr getNodeEncryptionKey() const { return encryptionKey; } @@ -28,7 +28,7 @@ namespace odhtdb return nodeHash; } private: - std::shared_ptr encryptionKey; + std::shared_ptr encryptionKey; std::shared_ptr nodeHash; }; } diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index a618420..9f93994 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -75,7 +75,7 @@ namespace odhtdb struct StoredNodeInfo { - std::shared_ptr nodeEncryptionKey; + std::shared_ptr nodeEncryptionKey; std::shared_ptr userKeyPair; }; @@ -135,14 +135,14 @@ namespace odhtdb MapHash getStoredNodeUserInfoDecrypted(const std::string &username, const std::string &password) const; // Returns true and node decryption key if node exists and we have the decryption key, - // otherwise return false and OwnedMemory with data set to nullptr - std::pair> getNodeDecryptionKey(const Hash &nodeHash); + // otherwise return false and OwnedByteArray with data set to nullptr + std::pair> getNodeDecryptionKey(const Hash &nodeHash); void setNodeDecryptionKey(const Hash &nodeHash, const DataView &decryptionKey); const std::vector& getRemoteNodes() const; void setRemoteNodes(const std::vector &remoteNodes); - std::vector getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; + std::vector getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; const dht::crypto::Identity& getIdentity() const; @@ -155,10 +155,10 @@ namespace odhtdb void bindCheckError(int sqliteBindResult); void loadMetadataFromFile(); void loadRemoteNodesFromFile(); - bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr decryptionKey); - bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr decryptionKey, const Signature::PublicKey *creatorPublicKey, const DataView &adminGroupId, u64 timestamp); - bool decryptNodeAddData(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const DataView &encryptedData, const std::shared_ptr decryptionKey); - bool decryptNodeAddUser(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const Signature::PublicKey *userToAddPublicKey, const DataView &groupToAddUserTo, const std::shared_ptr decryptionKey); + bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr decryptionKey); + bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr decryptionKey, const Signature::PublicKey *creatorPublicKey, const DataView &adminGroupId, u64 timestamp); + bool decryptNodeAddData(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const DataView &encryptedData, const std::shared_ptr decryptionKey); + bool decryptNodeAddUser(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const Signature::PublicKey *userToAddPublicKey, const DataView &groupToAddUserTo, const std::shared_ptr decryptionKey); i64 getNodeRowId(const Hash &nodeHash); i64 getNodeAddDataRowId(const Hash &requestHash); diff --git a/include/odhtdb/FileUtils.hpp b/include/odhtdb/FileUtils.hpp index 88721ed..155f53a 100644 --- a/include/odhtdb/FileUtils.hpp +++ b/include/odhtdb/FileUtils.hpp @@ -14,7 +14,7 @@ namespace odhtdb }; // Throws FileException on error - OwnedMemory fileGetContent(const boost::filesystem::path &filepath); + OwnedByteArray fileGetContent(const boost::filesystem::path &filepath); // Creates file if it doesn't exist. // Throws FileException on error diff --git a/include/odhtdb/OwnedMemory.hpp b/include/odhtdb/OwnedMemory.hpp index 67afe06..4c6df1c 100644 --- a/include/odhtdb/OwnedMemory.hpp +++ b/include/odhtdb/OwnedMemory.hpp @@ -22,4 +22,32 @@ namespace odhtdb void *data; usize size; }; + + class OwnedByteArray + { + public: + OwnedByteArray() : data(nullptr), size(0) {} + OwnedByteArray(u8 *_data, usize _size) : data(_data), size(_size) {} + OwnedByteArray(OwnedByteArray &&other) + { + data = other.data; + size = other.size; + + other.data = nullptr; + other.size = 0; + } + ~OwnedByteArray() + { + delete[] data; + } + + // Do not allow copy of this struct, forcing move when returning a OwnedByteArray in a function + OwnedByteArray(OwnedByteArray&) = delete; + OwnedByteArray& operator = (OwnedByteArray&) = delete; + + const DataView getView() const { return DataView(data, size); } + + u8 *data; + usize size; + }; } diff --git a/include/odhtdb/PasswordHash.hpp b/include/odhtdb/PasswordHash.hpp index bc02c53..6d23358 100644 --- a/include/odhtdb/PasswordHash.hpp +++ b/include/odhtdb/PasswordHash.hpp @@ -7,5 +7,5 @@ namespace odhtdb { const int HASH_PASSWORD_LENGTH = 32; - OwnedMemory hashPassword(const DataView &plainPassword, const DataView &salt); + OwnedByteArray hashPassword(const DataView &plainPassword, const DataView &salt); } -- cgit v1.2.3