linux - 尽早阅读/dev/urandom

标签 linux random kernel boot entropy

我正在进行随机数生成领域的研究,我需要演示著名的“P 和 Q”论文 (here) 中的“启动时熵洞”。我们将同时假脱机处理同一个最小 Linux 虚拟机的两个副本,我们期望它们的/dev/urandom 值在启动过程的某个早期点是相同的。

但是,我无法在启动过程中尽早读取/dev/urandom 以发现问题。我们需要在引导过程的早期。

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

提前致谢!

最佳答案

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

或者类似的调用。

关于linux - 尽早阅读/dev/urandom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153010/

相关文章:

linux - 操作系统如何实际测量 CPU 功率?

c - 没有gdt的idt使用grub

linux - Linux 上的 Nancy 使用 100% CPU

linux - 在分布式 jmeter 架构中,我停留在 'Waiting for possible shutdown message on port 4445'

python - 为什么在传递递减值列表 "random.choices"时 "cum_weights"总是返回相同的元素?

python - 使用 Python 中的 Keras 和 TensorFlow 无法重现结果

linux - iptables中表的意义

linux - printf 使用 in/bin/bash

python - 如何将 python urandom 转换为字符串?

c - 使用 set_current_state 在内核中休眠