Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
cleanup and use of smart pointers to manage memory.
  • Loading branch information
dr8co committed Jun 27, 2024
1 parent d93c4f7 commit 7814883
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
16 changes: 16 additions & 0 deletions src/mimallocSTL.cppm
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
// Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see https://www.gnu.org/licenses.

module;
#include <mimalloc.h>
#include <array>
Expand Down
2 changes: 0 additions & 2 deletions src/secureAllocator.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

module;

#include <new>
#include <limits>
#include <vector>
#include <sodium.h>
#include <string>
#include <mimalloc.h>

export module secureAllocator;

Expand Down
28 changes: 15 additions & 13 deletions src/utils/utils.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,21 @@ export {
/// \throws std::bad_alloc if memory allocation fails.
/// \throws std::runtime_error if memory locking/unlocking fails.
privacy::string getSensitiveInfo(const char *prompt = "") {
// Allocate a buffer for the password
auto *buffer = static_cast<char *>(sodium_malloc(MAX_PASSPHRASE_LEN));
if (buffer == nullptr)
// A lambda to free memory allocated by sodium_malloc
auto deleter = [](char *ptr) noexcept -> void {
sodium_free(ptr);
};

// Allocate memory for the passphrase
const std::unique_ptr<char, decltype(deleter)> buffer(static_cast<char *>(sodium_malloc(MAX_PASSPHRASE_LEN)),
deleter);

if (!buffer)
throw std::bad_alloc(); // Memory allocation failed

// Lock the memory to prevent swapping
if (sodium_mlock(buffer, MAX_PASSPHRASE_LEN) == -1) {
sodium_free(buffer);
if (sodium_mlock(buffer.get(), MAX_PASSPHRASE_LEN) == -1)
throw std::runtime_error("Failed to lock memory.");
}

// Turn off terminal echoing
termios oldSettings{}, newSettings{};
Expand All @@ -384,24 +389,21 @@ export {
} else {
// Check if buffer is not full
if (index < MAX_PASSPHRASE_LEN - 1) {
buffer[index++] = ch;
buffer.get()[index++] = ch;
}
}
}
buffer[index] = '\0'; // Null-terminate the string
buffer.get()[index] = '\0'; // Null-terminate the string

// Restore terminal settings
tcsetattr(STDIN_FILENO, TCSANOW, &oldSettings);

privacy::string passphrase{buffer};
privacy::string passphrase{buffer.get()};

// Unlock the memory
if (sodium_munlock(buffer, MAX_PASSPHRASE_LEN) == -1)
if (sodium_munlock(buffer.get(), MAX_PASSPHRASE_LEN) == -1)
throw std::runtime_error("Failed to unlock memory.");

// Free the buffer
sodium_free(buffer);

// Trim leading and trailing spaces
stripString(passphrase);

Expand Down

0 comments on commit 7814883

Please sign in to comment.