【问题标题】:Reading /dev/urandom as early as possible尽早阅读 /dev/urandom
【发布时间】:2015-10-15 15:48:47
【问题描述】:

我正在进行随机数生成领域的研究,我需要证明著名的“P's and Q's”论文 (here) 中的“启动时熵空洞”。我们将同时假脱机处理相同最小 Linux 虚拟机的两个副本,并且我们希望它们的 /dev/urandom 值在启动过程的早期阶段是相同的。

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

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

提前致谢!

【问题讨论】:

  • 修改urandom设备驱动程序以在它启动时将其第一个值保存在某处,并编写一个简单的ioctl来检索它。
  • 嗨@Barmar,这听起来是个好主意,我在搜索中还没有遇到这个主意,但是你能指出我在某个地方可以准确解释如何去做吗?我自己和我的团队对内核开发相对不熟悉,虽然我们觉得自己有足够的能力进行必要的修改,但我们只需要知道从哪里开始。内核很容易理解,但源代码本身是一个巨大的搜索空间。
  • 不知道,但我猜整个urandom 设备驱动程序只是某处的一个源文件。
  • 谷歌“/dev/urandom的源代码”
  • 谢谢,我有整个内核的源代码,我一直在研究它以了解从哪里开始。所以我有了来源,只需找到它的安装位置并在之后立即阅读。但我不熟悉,所以这成了一个问题。

标签: linux random kernel boot entropy


【解决方案1】:

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 地址等内容,因此如果您有两个完全相同的 VM,那么您就可以了)。来自评论:

  * 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

或类似的调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-13
    • 2019-06-02
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多