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 419150 - g_rand_new() needlessly consumes entropy when seeding random number generator
g_rand_new() needlessly consumes entropy when seeding random number generator
Status: RESOLVED DUPLICATE of bug 593232
Product: glib
Classification: Platform
Component: general
2.12.x
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2007-03-16 22:38 UTC by Steve Tyler
Modified: 2009-09-08 19:44 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Steve Tyler 2007-03-16 22:38:35 UTC
Please describe the problem:
g_rand_new() uses fread() to seed a random number generator.
fread() buffers 4096 bytes from /dev/urandom when it only needs 16 bytes.
Since system entropy is a limited (though renewable) resource,
it should be consumed sparingly.

Here is a little more background on why wasting entropy is a problem.

"When accessed as /dev/urandom, as many bytes as are requested are
returned even when the entropy pool is exhausted."
http://www.linux.com/howtos/Secure-Programs-HOWTO/random-numbers.shtml

When the entropy pool is exhausted, the Linux RNG (accessed via
/dev/urandom) behaves like a pseudo-random number generator, which is not
acceptable for cryptographic applications such as password generators.

Analysis of the Linux Random Number Generator
http://www.pinkas.net/PAPERS/gpr06.pdf


Steps to reproduce:
1. run: watch -d -n 1 cat /proc/sys/kernel/random/entropy_avail
2. run several times any app that calls g_rand_new() (appended example will do)


Actual results:
entropy_avail plummets if the app is run several times
strace shows that 4096 bytes are read from /dev/urandom

open("/dev/urandom", O_RDONLY|O_LARGEFILE) = 3
...
read(3, "9\326I\242\277x9^\355\266/\6\24[lh\fP\310\275\30\207_7"..., 4096) = 4096


Expected results:
Only 16 bytes (sizeof(seed)) are read from /dev/urandom

Does this happen every time?
yes.
fread() is buffering 4096 bytes data.
The solution is to disable buffering after calling fopen() and before calling fread().
This can be done with: setvbuf(fp, NULL, _IONBF, 0);


Other information:
glib2-2.12.9-1.fc6
http://svn.gnome.org/viewcvs/glib/trunk/glib/grand.c?view=markup

Test prog:
#include <glib.h>
main()
{
    g_rand_new();
}

Prototype fix:
#include <stdio.h>
main()
{
        FILE* fp;
        size_t n;
        int x;

        fp = fopen("/dev/urandom", "rb");
        setvbuf(fp, NULL, _IONBF, 0); // set to unbuffered
        n = fread(&x, sizeof(x), 1, fp);
}
Comment 1 Steve Tyler 2007-03-17 00:37:56 UTC
The gnome-password-generator shows entropy depression during start-up.
Gnome Password Generator is a GUI based secure password generator.
http://sourceforge.net/projects/gnome-password/

Strace shows that during initialization gnome-password-generator opens and reads /dev/urandom 3 times, consuming 16 + 4096 + 4096 bytes of entropy.
The latter two are due to calls to g_rand_new().

In a terminal window run
watch -d -n 1 cat /proc/sys/kernel/random/entropy_avail
then run gnome-password-generator.

NB: gnome-password-generator (v. 1.4) uses a pseudo-random number generator to produce the actual passwords.  Using a pseudo-random number generator for a cryptographic application is undesirable, but beyond the scope of this bug.
Comment 2 Tim-Philipp Müller 2009-08-27 09:25:15 UTC
This is also a performance issue. Also see bug #593232.
Comment 3 Tim-Philipp Müller 2009-09-08 19:44:21 UTC

*** This bug has been marked as a duplicate of bug 593232 ***