Repository navigation
Improve VM rollback handling in AutoSeeded_RNG - #6054
Conversation
|
cc @KurathSec |
|
Thanks for the cc. We built this branch (1c9b8af) and its parent (82979f1) with the same minimized static build and ran In every pair the kernel's Each line also calls The runs were x86_64 only, where the timestamp is |
reneme
left a comment
There was a problem hiding this comment.
The choices of entropy and the mandatory high-res clock make a lot of sense. I was wondering whether or not to incorporate the PID regardless of the availability of the system RNG in a belt-and-suspenders fashion of "let's take all we can get". But then, apart from the obvious performance cost, if the system RNG is available it is likely producing much better entropy than the 4-byte PID. Not sure, frankly.
Comments below are really just C++-nerd nits, nothing critical at all.
| std::array<uint8_t, 22> additional_input = {0}; | ||
| const size_t written = rollback_unique_input(additional_input); | ||
| this->fill_bytes_with_input(output, std::span{additional_input}.first(written)); |
There was a problem hiding this comment.
| std::array<uint8_t, 22> additional_input = {0}; | |
| const size_t written = rollback_unique_input(additional_input); | |
| this->fill_bytes_with_input(output, std::span{additional_input}.first(written)); | |
| // TODO(C++26): let rollback_unique_input return std::inplace_vector | |
| std::array<uint8_t, 22> additional_input = {0}; | |
| const size_t written = rollback_unique_input(additional_input); | |
| this->fill_bytes_with_input(output, std::span{additional_input}.first(written)); |
... I'm looking forward to this occasionally. Perhaps its worth adding a private implementation for things like that.
There was a problem hiding this comment.
C++26 might be a while tbh given we're getting pushback on C++20 still :/ might as well add an appropriate util given that, basically a minimal clone that matches the expected std interface so cutting over is easy. Certainly this pattern comes up and is handled in various ad-hoc ways atm.
I did consider this but the significant issue here is relating to the system RNG being duplicated after a VM rollback and in that situation the PID won't have changed either. Also including both the timestamp and the pid cuts the system RNGs portion to just 10 bytes (could do 12 if we assume pids are 16 bit), which feels a little bit short (certainly not terrible, but an attacker willing and able to perform an 80 bit search is not completely out of the question) unless we accept a 3rd compression function during the HMAC_DRBG, which has a fairly dramatic affect on performance. |
To handle situations with VM rollback or undetected process space duplication, AutoSeeded_RNG::randomize invokes the underlying RNG's randomize_with_ts_input, which previously (if available) read from the system RNG and provided that as additional input to the underlying HMAC_DRBG, on the assumption that this randomness would always be fresh. However some unfortunate VM configurations are possible where the host kernel does not or can not notify the guest kernel that a rollback has occurred. In this situation, the guest /dev/random itself can return duplicate output. To some extent this is just a failure of the kernel RNG to satisfy its contract. Still, attempt to alleviate the situation by modifying randomize_with_ts_input to prefix the system RNG output with a 64-bit high resolution timestamp (the CPU cycle counter if available), on the theory that after a rollback it's still fairly likely for the clocks to diverge. Since here we're assuming the initial seeding state is secure, even a single bit difference in the additional input is sufficient to cause the duplicated states to diverge. Using processor RNG (RDRAND/DARN) instead of a timestamp would provide a stronger guarantee here, but its cost varies enormously across CPUs, and on many common machines it can take over 5000 cycles to complete. This would have a significant impact on performance, especially for small draws. The total additional input is 22 bytes, the most that fits in the final block of each HMAC within HMAC_DRBG(SHA-256)'s update without requiring an additional compression. If the timestamp is not available the system RNG fills all 22 bytes, and if the system RNG is not available the process ID follows the timestamp as before.
1c9b8af to
c4ddf51
Compare
Previously (in #6054) stateful RNGs would use the clock combined with the system RNG output to avoid any problems with VM rollback of the RNG state, because on some platforms the hardware RNG is excessively slow and using it drastically hurts RNG performance. Now CPUID runs a quick sequence of calls of the hardware RNG, and if they are fast sets an additional bit indicating the hardware RNG both exists and is fast. If this bit is set, AutoSeeded_RNG will use the hardware RNG in preference to the clock.
Previously (in #6054) stateful RNGs would use the clock combined with the system RNG output to avoid any problems with VM rollback of the RNG state, because on some platforms the hardware RNG is excessively slow and using it drastically hurts RNG performance. Now the RNG runs a quick sequence of calls of the hardware RNG, and if they are fast sets a flag indicating the hardware RNG both exists and is fast. Then AutoSeeded_RNG will include 64-bits of hardware RNG output along with the rest of the additional input.
Previously (in #6054) stateful RNGs would use the clock combined with the system RNG output to avoid any problems with VM rollback of the RNG state, because on some platforms the hardware RNG is excessively slow and using it drastically hurts RNG performance. Now the RNG runs a quick sequence of calls of the hardware RNG, and if they are fast sets a flag indicating the hardware RNG both exists and is fast. Then AutoSeeded_RNG will include 64-bits of hardware RNG output along with the rest of the additional input.
Previously (in #6054) stateful RNGs would use the clock combined with the system RNG output to avoid any problems with VM rollback of the RNG state, because on some platforms the hardware RNG is excessively slow and using it drastically hurts RNG performance. Now the RNG runs a quick sequence of calls of the hardware RNG, and if they are fast sets a flag indicating the hardware RNG both exists and is fast. Then AutoSeeded_RNG will include 64-bits of hardware RNG output along with the rest of the additional input.
To handle situations with VM rollback or undetected process space duplication, AutoSeeded_RNG::randomize invokes the underlying RNG's randomize_with_ts_input, which previously (if available) read from the system RNG, on the assumption that this randomness would always be fresh.
However some unfortunate VM configurations are possible where the host kernel does not or can not notify the guest kernel that a rollback has occurred. In this situation, the guest /dev/random itself can return duplicate output.
To some extent this is just a failure of the kernel RNG to satisfy its contract. Still, attempt to alleviate the situation by modifying randomize_with_ts_input to prefix the system RNG output with a 64-bit high resolution timestamp (the CPU cycle counter if available), on the theory that after a rollback it's still fairly likely for the clocks to diverge. Since here we're assuming the initial seeding state is secure, even a single bit difference in the additional input is sufficient to cause the duplicated states to diverge.
The processor RNG (RDRAND/DARN) would provide a stronger guarantee, but its cost varies enormously across CPUs; on Tiger Lake a single RDRAND takes over 13000 cycles and is serialized across all cores, which more than doubled the cost of small AutoSeeded_RNG requests.
The total additional input is 22 bytes, the most that fits in the final block of each HMAC within HMAC_DRBG(SHA-256)'s update without requiring an additional compression. If the timestamp is not available the system RNG fills all 22 bytes, and if the system RNG is not available the process ID follows the timestamp as before.