loolwsd/LOOLKit.cpp | 5 +++-- loolwsd/Util.cpp | 17 ++++++++++++++++- loolwsd/Util.hpp | 3 ++- 3 files changed, 21 insertions(+), 4 deletions(-)
New commits: commit 019aed134e17b69e615a5990eab8c471184973e9 Author: Ashod Nakashian <ashod.nakash...@collabora.co.uk> Date: Sat Mar 12 10:15:45 2016 -0500 loolwsd: using random jailId Change-Id: Ie9501e4346a124994270ca1fac61869ed04b9b72 Reviewed-on: https://gerrit.libreoffice.org/23213 Reviewed-by: Ashod Nakashian <ashnak...@gmail.com> Tested-by: Ashod Nakashian <ashnak...@gmail.com> diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp index 5414a26..4fb6141 100644 --- a/loolwsd/LOOLKit.cpp +++ b/loolwsd/LOOLKit.cpp @@ -414,7 +414,7 @@ public: [this](const std::string& id, const std::string& uri, const std::string& docPassword, bool isDocPasswordProvided) { return onLoad(id, uri, docPassword, isDocPasswordProvided); }, [this](const std::string& id) { onUnload(id); }); // child -> 0, sessionId -> 1, PID -> 2 - const std::string hello("child " + sessionId + " " + std::to_string(Process::id())); + const std::string hello("child " + sessionId + " " + _jailId); session->sendTextFrame(hello); auto thread = std::make_shared<Connection>(session, ws); @@ -764,6 +764,7 @@ void lokit_main(const std::string& childRoot, #ifdef LOOLKIT_NO_MAIN // Reinitialize logging when forked. Log::initialize("kit"); + Util::rng::reseed(); #endif struct pollfd pollPipeBroker; @@ -782,7 +783,7 @@ void lokit_main(const std::string& childRoot, std::map<std::string, std::shared_ptr<Document>> _documents; - static const std::string jailId = std::to_string(Process::id()); + static const std::string jailId = Util::encodeId(Util::rng::getNext()); static const std::string process_name = "loolkit"; if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(process_name.c_str()), 0, 0, 0) != 0) diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp index 7485a9e..5fc9a2a 100644 --- a/loolwsd/Util.cpp +++ b/loolwsd/Util.cpp @@ -65,7 +65,22 @@ namespace rng { static std::random_device _rd; static std::mutex _rngMutex; - static std::mt19937_64 _rng = std::mt19937_64(_rd()); + + // Create the prng with a random-device for seed. + // If we don't have a hardware random-device, we will get the same seed. + // In that case we are better off with an arbitrary, but changing, seed. + static std::mt19937_64 _rng = std::mt19937_64(_rd.entropy() + ? _rd() + : (clock() + getpid())); + + // A new seed is used to shuffle the sequence. + // N.B. Always reseed after getting forked! + void reseed() + { + _rng.seed(_rd.entropy() ? _rd() : (clock() + getpid())); + } + + // Returns a new random number. unsigned getNext() { std::unique_lock<std::mutex> lock(_rngMutex); diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp index 1e6e515..2e83ea8 100644 --- a/loolwsd/Util.hpp +++ b/loolwsd/Util.hpp @@ -50,7 +50,8 @@ namespace Util { namespace rng { - unsigned getNext(); + void reseed(); + unsigned getNext(); } /// Encode an integral ID into a string, with padding support. _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits