After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 710738 - GRand has lame fallback for Windows
GRand has lame fallback for Windows
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2013-10-23 17:16 UTC by Colin Walters
Modified: 2014-07-23 17:09 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
win32: use real random data for seed on win32 (2.78 KB, patch)
2013-10-25 16:28 UTC, Allison Karlitskaya (desrt)
accepted-commit_now Details | Review
win32: use real random data for seed on win32 (1.92 KB, patch)
2013-10-25 16:42 UTC, Allison Karlitskaya (desrt)
committed Details | Review

Description Colin Walters 2013-10-23 17:16:21 UTC
On Unix we seed from /dev/unrandom, but on Windows we just fall back to getpid() and time.  Windows has CryptGenRandom().
Comment 1 Allison Karlitskaya (desrt) 2013-10-25 16:28:19 UTC
Created attachment 258135 [details] [review]
win32: use real random data for seed on win32

We use RtlGenRandom() to do this instead of bringing up an entire crypto
context and using CryptGenRandom().  Several web sources suggest that
this is what CryptGenRandom() is doing internally anyway, and MSDN
documents this as being legit.
Comment 2 Allison Karlitskaya (desrt) 2013-10-25 16:31:21 UTC
http://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx make it look like 'rand_s' is an easier way to get this working, but I couldn't make it work with mingw (and the docs on MSDN say that it's just doing RtlGenRandom() internally anyway).  The above patch does work on mingw32/fedora + wine.
Comment 3 Colin Walters 2013-10-25 16:33:40 UTC
Review of attachment 258135 [details] [review]:

Nice!  Code looks correct structurally, and the content also looks good to me from what I can tell online.
Comment 4 Allison Karlitskaya (desrt) 2013-10-25 16:42:59 UTC
Created attachment 258136 [details] [review]
win32: use real random data for seed on win32

We can get cryptographically secure data from rand_s().


I figured it out.  I had a #ifdef G_OS_WIN32 too early (ie: before it
was set) which caused me to fail to include the proper header.
Comment 5 Colin Walters 2013-10-25 17:02:15 UTC
Review of attachment 258136 [details] [review]:

Oh that is *far* simpler.  Just one little bit:

::: glib/grand.c
@@ +267,3 @@
+#else /* G_OS_WIN32 */
+  gint i;
+

Won't this declaration-after-statement trip up C89 mode?  Or do we not care for MSVC/mingw?
Comment 6 Allison Karlitskaya (desrt) 2013-10-25 22:19:48 UTC
Attachment 258136 [details] pushed as 0e1924a - win32: use real random data for seed on win32


The variable is only declared inside of the #else branch and there is no
statement above the #ifdef.  The only thing up there: 'guint32 seed[4];'

Pushed.
Comment 7 Morten Welinder 2013-12-25 22:07:26 UTC
This breaks the build under mingw:

Creating library file: .libs/libglib-2.0.dll.a
.libs/libglib_2_0_la-grand.o:grand.c:(.text+0x804): undefined reference to `_rand_s'
.libs/libglib_2_0_la-grand.o:grand.c:(.text+0x817): undefined reference to `_rand_s'
collect2: ld returned 1 exit status
Comment 8 Morten Welinder 2013-12-25 23:00:49 UTC
The original patch appears to work.

Btw., if you are going to claim "cryptographically secure data" then you
should really check the return value.
Comment 9 Allison Karlitskaya (desrt) 2013-12-26 00:10:52 UTC
This is a known mingw32 shortcoming.  Someone filed a bug upstream about it already;

http://sourceforge.net/p/mingw/bugs/2122/
Comment 10 Ignacio Casal Quinteiro (nacho) 2014-07-14 13:25:43 UTC
I am reopening this since this patch broke the windows build. I needed to revert it in my builder to be able to build glib on windows.

See: https://git.gnome.org/browse/gtk-builder-win/tree/win32/libs/12_glib/revert_rand_s.patch

It is applied with -R btw.
Comment 11 Ignacio Casal Quinteiro (nacho) 2014-07-23 17:09:59 UTC
Nevermind it seems it was due to using the old mingw.