From 426fc99fb55317a87641b625ac41ed21d8b429eb Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 28 Aug 2019 09:38:11 +0000 Subject: locked pages, making the tag id possibly configurable. --- src/lib/utils/os_utils.cpp | 37 +++++++++++++++++++++++++++++-------- src/lib/utils/os_utils.h | 2 ++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp index daa5bb73f..85da33382 100644 --- a/src/lib/utils/os_utils.cpp +++ b/src/lib/utils/os_utils.cpp @@ -55,6 +55,10 @@ extern "C" char **environ; #endif +#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) + #include +#endif + namespace Botan { // Not defined in OS namespace for historical reasons @@ -401,6 +405,30 @@ size_t OS::read_env_variable_sz(const std::string& name, size_t def) return def; } +int OS::get_locked_fd() + { +#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) +// On Darwin, tagging anonymous pages allows vmmap to track these. +// Allowed from 240 to 255 for userland applications, taken an hardcoded +// value for now even though it can possibly intersect. + static constexpr int default_locked_fd = VM_MAKE_TAG(255); + int locked_fd = default_locked_fd; + + if (const char *locked_env = read_env_variable("BOTAN_LOCKED_FD")) + { + long locked_fdl = std::strtol(locked_env, nullptr, 10); + if (locked_fdl < 100 || locked_fdl > 255) + { + locked_fdl = default_locked_fd; + } + locked_fd = static_cast(locked_fdl); + } + return VM_MAKE_TAG(locked_fd); +#else + return -1; +#endif + } + std::vector OS::allocate_locked_pages(size_t count) { std::vector result; @@ -430,14 +458,7 @@ std::vector OS::allocate_locked_pages(size_t count) #define PROT_MAX(p) 0 #endif const int pflags = PROT_READ | PROT_WRITE; -#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) -// On Darwin, tagging anonymous pages allows vmmap to track these. -// Allowed from 240 to 255 for userland applications, taken an hardcoded -// value for now even though it can possibly intersect. - const int locked_fd = (255<<24); -#else - const int locked_fd = -1; -#endif + const int locked_fd = get_locked_fd(); ptr = ::mmap(nullptr, 2*page_size, pflags | PROT_MAX(pflags), diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h index 5b8c49f99..9604777fe 100644 --- a/src/lib/utils/os_utils.h +++ b/src/lib/utils/os_utils.h @@ -111,6 +111,8 @@ const char* read_env_variable(const std::string& var_name); */ size_t read_env_variable_sz(const std::string& var_name, size_t def_value = 0); +int get_locked_fd(); + /** * Request count pages of RAM which are locked into memory using mlock, * VirtualLock, or some similar OS specific API. Free it with free_locked_pages. -- cgit v1.2.3 From 8d6a9e2cfe2a65ccd84adc46c39313662dfd1291 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 28 Aug 2019 10:51:15 +0000 Subject: Fixes and tweaks from feedback --- src/lib/utils/os_utils.cpp | 50 +++++++++++++++++++++++----------------------- src/lib/utils/os_utils.h | 2 -- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp index 85da33382..843e68638 100644 --- a/src/lib/utils/os_utils.cpp +++ b/src/lib/utils/os_utils.cpp @@ -60,6 +60,30 @@ #endif namespace Botan { +namespace { +int get_locked_fd() + { +#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) +// On Darwin, tagging anonymous pages allows vmmap to track these. +// Allowed from 240 to 255 for userland applications, taken an hardcoded +// value for now even though it can possibly intersect. + static constexpr int default_locked_fd = 255; + int locked_fd = default_locked_fd; + + if (size_t locked_fdl = OS::read_env_variable_sz("BOTAN_LOCKED_FD", default_locked_fd)) + { + if (locked_fdl < 240 || locked_fdl > 255) + { + locked_fdl = default_locked_fd; + } + locked_fd = static_cast(locked_fdl); + } + return VM_MAKE_TAG(locked_fd); +#else + return -1; +#endif + } +} // Not defined in OS namespace for historical reasons void secure_scrub_memory(void* ptr, size_t n) @@ -405,30 +429,6 @@ size_t OS::read_env_variable_sz(const std::string& name, size_t def) return def; } -int OS::get_locked_fd() - { -#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS) -// On Darwin, tagging anonymous pages allows vmmap to track these. -// Allowed from 240 to 255 for userland applications, taken an hardcoded -// value for now even though it can possibly intersect. - static constexpr int default_locked_fd = VM_MAKE_TAG(255); - int locked_fd = default_locked_fd; - - if (const char *locked_env = read_env_variable("BOTAN_LOCKED_FD")) - { - long locked_fdl = std::strtol(locked_env, nullptr, 10); - if (locked_fdl < 100 || locked_fdl > 255) - { - locked_fdl = default_locked_fd; - } - locked_fd = static_cast(locked_fdl); - } - return VM_MAKE_TAG(locked_fd); -#else - return -1; -#endif - } - std::vector OS::allocate_locked_pages(size_t count) { std::vector result; @@ -458,7 +458,7 @@ std::vector OS::allocate_locked_pages(size_t count) #define PROT_MAX(p) 0 #endif const int pflags = PROT_READ | PROT_WRITE; - const int locked_fd = get_locked_fd(); + static const int locked_fd = get_locked_fd(); ptr = ::mmap(nullptr, 2*page_size, pflags | PROT_MAX(pflags), diff --git a/src/lib/utils/os_utils.h b/src/lib/utils/os_utils.h index 9604777fe..5b8c49f99 100644 --- a/src/lib/utils/os_utils.h +++ b/src/lib/utils/os_utils.h @@ -111,8 +111,6 @@ const char* read_env_variable(const std::string& var_name); */ size_t read_env_variable_sz(const std::string& var_name, size_t def_value = 0); -int get_locked_fd(); - /** * Request count pages of RAM which are locked into memory using mlock, * VirtualLock, or some similar OS specific API. Free it with free_locked_pages. -- cgit v1.2.3