From ff93b2b23d4053259584800c601a923a3c8b01e8 Mon Sep 17 00:00:00 2001 From: Martin Chang Date: Thu, 11 Dec 2025 23:44:42 +0800 Subject: [PATCH 1/3] Fix fread check for reading randomness Fallback CSPRNG on generic UNIX will read `urandom` for randomness. The read has a bug where if `fread` reads say 10 bytes but the size is 32. The buffer is only partially updated and returned successful. This patch ensures the fallback read returns success only when the buffer is fully filled. --- trantor/utils/Utilities.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trantor/utils/Utilities.cc b/trantor/utils/Utilities.cc index 665253d2..99cfa0e3 100644 --- a/trantor/utils/Utilities.cc +++ b/trantor/utils/Utilities.cc @@ -441,7 +441,7 @@ static bool systemRandomBytes(void *ptr, size_t size) LOG_FATAL << "Failed to open /dev/urandom for randomness"; abort(); } - if (fread(ptr, 1, size, fptr.get()) != 0) + if (fread(ptr, 1, size, fptr.get()) != size) return true; #endif return false; From a1e0c313a54df04e3641e4d912af2a4c18e97d34 Mon Sep 17 00:00:00 2001 From: Martin Chang Date: Thu, 11 Dec 2025 23:45:18 +0800 Subject: [PATCH 2/3] Fix fread condition to check for equality --- trantor/utils/Utilities.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trantor/utils/Utilities.cc b/trantor/utils/Utilities.cc index 99cfa0e3..bf9193ee 100644 --- a/trantor/utils/Utilities.cc +++ b/trantor/utils/Utilities.cc @@ -441,7 +441,7 @@ static bool systemRandomBytes(void *ptr, size_t size) LOG_FATAL << "Failed to open /dev/urandom for randomness"; abort(); } - if (fread(ptr, 1, size, fptr.get()) != size) + if (fread(ptr, 1, size, fptr.get()) == size) return true; #endif return false; From 2d5dec9330c7409791c87d41759742cc6b70191a Mon Sep 17 00:00:00 2001 From: Martin Chang Date: Fri, 12 Dec 2025 00:06:07 +0800 Subject: [PATCH 3/3] Use microseconds --- trantor/utils/Utilities.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trantor/utils/Utilities.cc b/trantor/utils/Utilities.cc index bf9193ee..b70d21eb 100644 --- a/trantor/utils/Utilities.cc +++ b/trantor/utils/Utilities.cc @@ -533,9 +533,9 @@ bool secureRandomBytes(void *data, size_t len) auto now = chrono::steady_clock::now(); // the proposed algorithm uses the time in nanoseconds, but we don't have a // way to read it (yet) not C++ provided a standard way to do it. Falling - // back to milliseconds. This along with additional entropy is hopefully + // back to microseconds. This along with additional entropy is hopefully // good enough. - state.time = chrono::time_point_cast(now) + state.time = chrono::time_point_cast(now) .time_since_epoch() .count(); // `now` lives on the stack, so address in each call _may_ be different.