2015-10-15 23 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 

或類似的呼叫。