Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exhaust /dev/urandom for testing?

I recently had a bug where I didn't properly handle when the entropy on my linux server got too low and a read of /dev/urandom returned less than the number of bytes expected.

How can I recreate this with a test? Is there a way to lower the entropy on a system or to reliably empty /dev/urandom?

I'd like to be able to have a regression test that will verify my fix. I'm using Ubuntu 12.04.

like image 356
Tom Avatar asked Nov 26 '25 14:11

Tom


1 Answers

According to random(4) man page,

read from the /dev/urandom device will not block

You should read a lot of bytes from /dev/random (without any u) if you want it to block. (How many is hardware and system dependent).

So you cannot "exaust" /dev/urandom, since

 A read from the /dev/urandom device will not block waiting for 
 more entropy. As a result, if there is not sufficient entropy in 
 the entropy pool, the returned values are theoretically vulnerable 
 to a cryptographic attack on the algorithms used by the driver.

I believe you should use /dev/random which indeed can be exhausted, by blocking. But you should not read more than about 256 bits from it.

like image 98
Basile Starynkevitch Avatar answered Dec 01 '25 06:12

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!