Skip to content

Commit

Permalink
data_path file permissions hardening (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
argakiig authored and rkeene committed Sep 11, 2018
1 parent 4996a42 commit 4366cbf
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions rai/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ TEST (block_store, DISABLED_already_open) // File can be shared
{
auto path (rai::unique_path ());
boost::filesystem::create_directories (path.parent_path ());
boost::filesystem::permissions (path.parent_path (), boost::filesystem::owner_all);
std::ofstream file;
file.open (path.string ().c_str ());
ASSERT_TRUE (file.is_open ());
Expand Down
8 changes: 4 additions & 4 deletions rai/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set (platform_sources plat/default/priority.cpp)
set (platform_sources plat/default/priority.cpp plat/posix/perms.cpp)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set (platform_sources plat/windows/priority.cpp)
set (platform_sources plat/windows/priority.cpp plat/windows/perms.cpp)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set (platform_sources plat/linux/priority.cpp)
set (platform_sources plat/linux/priority.cpp plat/posix/perms.cpp)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set (platform_sources plat/default/priority.cpp)
set (platform_sources plat/default/priority.cpp plat/posix/perms.cpp)
else ()
error ("Unknown platform: ${CMAKE_SYSTEM_NAME}")
endif ()
Expand Down
9 changes: 9 additions & 0 deletions rai/lib/plat/posix/perms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <rai/lib/utility.hpp>

#include <sys/stat.h>
#include <sys/types.h>

void rai::set_umask ()
{
umask (077);
}
14 changes: 14 additions & 0 deletions rai/lib/plat/windows/perms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <assert.h>
#include <rai/lib/utility.hpp>

#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>

void rai::set_umask ()
{
int oldMode;

auto result (_umask_s (_S_IWRITE | _S_IREAD, &oldMode));
assert (result == 0);
}
1 change: 1 addition & 0 deletions rai/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace rai
{
// Lower priority of calling work generating thread
void work_thread_reprioritize ();
void set_umask ();
template <typename... T>
class observer_set
{
Expand Down
1 change: 1 addition & 0 deletions rai/node/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rai::mdb_env::mdb_env (bool & error_a, boost::filesystem::path const & path_a, i
if (path_a.has_parent_path ())
{
boost::filesystem::create_directories (path_a.parent_path (), error);
boost::filesystem::permissions (path_a.parent_path (), boost::filesystem::owner_all);
if (!error)
{
auto status1 (mdb_env_create (&environment));
Expand Down
2 changes: 2 additions & 0 deletions rai/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2436,6 +2436,7 @@ void rai::node::backup_wallet ()
{
auto backup_path (application_path / "backup");
boost::filesystem::create_directories (backup_path);
boost::filesystem::permissions (backup_path, boost::filesystem::owner_all);
i->second->store.write_backup (transaction, backup_path / (i->first.to_string () + ".json"));
}
auto this_l (shared ());
Expand Down Expand Up @@ -4109,6 +4110,7 @@ alarm (*service),
work (1, nullptr)
{
boost::filesystem::create_directories (path);
boost::filesystem::permissions (path, boost::filesystem::owner_all);
logging.max_size = std::numeric_limits<std::uintmax_t>::max ();
logging.init (path);
node = std::make_shared<rai::node> (init, *service, 24000, path, alarm, logging, work);
Expand Down
1 change: 1 addition & 0 deletions rai/rai_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ bool rai_daemon::daemon_config::upgrade_json (unsigned version_a, boost::propert
void rai_daemon::daemon::run (boost::filesystem::path const & data_path)
{
boost::filesystem::create_directories (data_path);
boost::filesystem::permissions (data_path, boost::filesystem::owner_all);
rai_daemon::daemon_config config (data_path);
auto config_path ((data_path / "config.json"));
std::fstream config_file;
Expand Down
3 changes: 3 additions & 0 deletions rai/rai_node/entry.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <rai/lib/utility.hpp>
#include <rai/node/cli.hpp>
#include <rai/node/node.hpp>
#include <rai/node/testing.hpp>
Expand All @@ -10,6 +11,8 @@

int main (int argc, char * const * argv)
{
rai::set_umask ();

boost::program_options::options_description description ("Command line options");
rai::add_node_options (description);

Expand Down
3 changes: 3 additions & 0 deletions rai/rai_wallet/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost
{
rai_qt::eventloop_processor processor;
boost::filesystem::create_directories (data_path);
boost::filesystem::permissions (data_path, boost::filesystem::owner_all);
QPixmap pixmap (":/logo.png");
QSplashScreen * splash = new QSplashScreen (pixmap);
splash->show ();
Expand Down Expand Up @@ -285,6 +286,8 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost

int main (int argc, char * const * argv)
{
rai::set_umask ();

try
{
QApplication application (argc, const_cast<char **> (argv));
Expand Down

0 comments on commit 4366cbf

Please sign in to comment.