How can I exhaust /dev/urandom for testing?

2019-07-01 15:15发布

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.

1条回答
欢心
2楼-- · 2019-07-01 16:16

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.

查看更多
登录 后发表回答