urandom
是通过设备驱动程序提供的,内核与驱动程序做的第一件事情是致电init
调用。
如果你到这里看看:http://lxr.free-electrons.com/source/drivers/char/random.c#L1401
* Note that setup_arch() may call add_device_randomness()
* long before we get here. This allows seeding of the pools
* with some platform dependent data very early in the boot
* process. But it limits our options here. We must use
* statically allocated structures that already have all
* initializations complete at compile time. We should also
* take care not to overwrite the precious per platform data
* we were given.
*/
static int rand_initialize(void)
{
init_std_data(&input_pool);
init_std_data(&blocking_pool);
init_std_data(&nonblocking_pool);
return 0;
}
early_initcall(rand_initialize);
所以,init
功能该驱动程序是rand_initialize
。但请注意,评论说setup_arch
可能会在此设备初始化之前调用add_device randomness()
。然而,调用该功能并不会添加任何实际的熵(它提供像MAC地址的东西池,所以如果你有两个完全相同的虚拟机,你很好)。从评论:
* add_device_randomness() is for adding data to the random pool that
* is likely to differ between two devices (or possibly even per boot).
* This would be things like MAC addresses or serial numbers, or the
* read-out of the RTC. This does *not* add any actual entropy to the
* pool, but it initializes the pool to different values for devices
* that might otherwise be identical and have very little entropy
* available to them (particularly common in the embedded world).
另外,需要注意的是熵池存储在关机并通过初始化脚本恢复开机时间(在我的Ubuntu 14.04,它在/etc/init.d/urandom
),所以你可能需要从调用脚本脚本前
53 (
54 date +%s.%N
55
56 # Load and then save $POOLBYTES bytes,
57 # which is the size of the entropy pool
58 if [ -f "$SAVEDFILE" ]
59 then
60 cat "$SAVEDFILE"
61 fi
62 # Redirect output of subshell (not individual commands)
63 # to cope with a misfeature in the FreeBSD (not Linux)
64 # /dev/random, where every superuser write/close causes
65 # an explicit reseed of the yarrow.
66 ) >/dev/urandom
或类似的呼叫。
修改'urandom'设备驱动程序在启动时将其第一个值保存在某处,然后编写一个简单的'ioctl'来检索它。 – Barmar
嗨@Barmar,这听起来像个好主意,而且我还没有在我的搜索中遇到过这个想法,但是你能指点我到哪个地方去解释怎么去做?我和我的团队对内核开发相对不熟悉,虽然我们有足够的能力进行必要的修改,但我们只需要知道从哪里开始。内核是很容易理解的,但是源代码本身是一个搜索周围的巨大地方。 – user3703603
不知道,但我猜想整个'urandom'设备驱动程序只是一个源文件。 – Barmar