MantisBT - CMake
View Issue Details
0015976CMakeCMakepublic2016-02-16 20:262016-06-10 14:21
Cristian Rodríguez 
Brad King 
normalminoralways
closedfixed 
linuxlinuxany
CMake 3.4.3 
CMake 3.6CMake 3.6 
0015976: cmSystemTools::RandomSeed() requests excessive entropy per invocation
cmSystemTools::RandomSeed() opens a *buffered* ifstream, the final result is that each invocation requests _IO_BUFSIZ -1 of entropy to the operating system instead of just sizeof(seed)
mkdir test_cmake
echo "PROJECT(foo)" > CMakeLists.txt
cd test_cmake
strace -f -ocmake.trace cmake .

open cmake.trace

10768 26289 open("/dev/urandom", O_RDONLY) = 4
10769 26289 read(4, "h\304j\vx\314\26\0042\250g\275\0014,\256\251\257T&\304\212\310\347q\201\263\324\330\273z8"..., 8191) = 8191
10770 26289 close(4) = 0
10771 26289 write(3, "cmake_minimum_required(VERSION 3"..., 795) = 795
10772 26289 close(3) = 0
10773 26289 stat("/home/crrodriguez/test_cmake/CMakeFiles/CMakeTmp", {st_mode=S_IFDIR|0755, st_size=58, ...}) = 0
10774 26289 getcwd("/home/crrodriguez/test_cmake", 2048) = 29
git diff
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index 3ba7287..26bec34 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -2184,8 +2184,10 @@ unsigned int cmSystemTools::RandomSeed()
   } seed;
 
   // Try using a real random source.
- cmsys::ifstream fin("/dev/urandom");
- if(fin && fin.read(seed.bytes, sizeof(seed)) &&
+ cmsys::ifstream fin;
+ fin.rdbuf()->pubsetbuf(0, 0);
+ fin.open("/dev/urandom");
+ if(fin.good() && fin.read(seed.bytes, sizeof(seed)) &&
      fin.gcount() == sizeof(seed))
     {
     return seed.integer;
No tags attached.
Issue History
2016-02-16 20:26Cristian RodríguezNew Issue
2016-02-17 09:05Brad KingNote Added: 0040496
2016-02-17 09:05Brad KingAssigned To => Brad King
2016-02-17 09:05Brad KingStatusnew => resolved
2016-02-17 09:05Brad KingResolutionopen => fixed
2016-02-17 09:05Brad KingFixed in Version => CMake 3.6
2016-02-17 09:05Brad KingTarget Version => CMake 3.6
2016-02-19 09:45Brad KingNote Edited: 0040496bug_revision_view_page.php?bugnote_id=40496#r2029
2016-06-10 14:21Kitware RobotNote Added: 0041244
2016-06-10 14:21Kitware RobotStatusresolved => closed

Notes
(0040496)
Brad King   
2016-02-17 09:05   
(edited on: 2016-02-19 09:45)
Good catch, thanks! Applied:

 cmSystemTools: Avoid excess entropy consumption by RandomSeed
 https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=f23f18ab [^]

(0041244)
Kitware Robot   
2016-06-10 14:21   
This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.