2015-10-15 44 views
0

我正在进行随机数生成领域的研究,我需要从知名的“P's和Q's”论文(here)中演示“启动时熵孔” 。我们将同时推迟两个相同的最小Linux虚拟机的副本,并且我们期望他们的/ dev/urandom值在引导过程的某个早期阶段是相同的。尽早读取/ dev/urandom

但是,我一直无法在启动过程中及早读取/ dev/urandom来发现问题。我们需要在启动过程的早些时候。

如何获取/ dev/urandom的最早可能值?我们可能需要修改内核,但我们在这方面的经验很少,需要一些指导。或者,如果有一个可以在不重新编译内核的情况下使用内核工具的话,那也会很棒。

提前致谢!

+0

修改'urandom'设备驱动程序在启动时将其第一个值保存在某处,然后编写一个简单的'ioctl'来检索它。 – Barmar

+0

嗨@Barmar,这听起来像个好主意,而且我还没有在我的搜索中遇到过这个想法,但是你能指点我到哪个地方去解释怎么去做?我和我的团队对内核开发相对不熟悉,虽然我们有足够的能力进行必要的修改,但我们只需要知道从哪里开始。内核是很容易理解的,但是源代码本身是一个搜索周围的巨大地方。 – user3703603

+0

不知道,但我猜想整个'urandom'设备驱动程序只是一个源文件。 – Barmar

回答

0

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 

或类似的呼叫。