Skip to content

Improve VM rollback handling in AutoSeeded_RNG - #6054

Merged
randombit merged 1 commit into
masterfrom
jack/rng-vm-rollback
Oct 24, 2026
Merged

randombit merged 1 commit into
masterfrom
jack/rng-vm-rollback

Conversation

@randombit

Copy link
Copy Markdown
Owner

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.

@randombit

Copy link
Copy Markdown
Owner Author

cc @KurathSec

@KurathSec

Copy link
Copy Markdown
Contributor

Thanks for the cc. We built this branch (1c9b8af) and its parent (82979f1) with the same minimized static build and ran AutoSeeded_RNG (botan_rng_init(&rng, "user"), one 32-byte botan_rng_get per line) in x86_64 KVM guests, each snapshot restored twice, in the three configurations where the guest kernel does not reseed on a restore: QEMU 10.2.2 without a vmgenid device, QEMU with the device but a guest kernel without the vmgenid driver, and Firecracker 1.17 with a guest kernel without the driver.

In every pair the kernel's getrandom output repeated for the first one or two lines after the restore. At the parent, AutoSeeded_RNG repeated for exactly those lines (15 of 15 pairs). With this PR it was distinct from the first line on (30 of 30 pairs, 10 per configuration). With the driver present, and across fork, clone, _Fork and a clone into a new pid namespace, it is distinct at both commits.

Each line also calls botan_rng_generate_with_input with a fixed input before botan_rng_get. Its block in the first line after the restore is identical in every pair at both commits, so the state the restore resumes from is still the snapshot's, as expected; with this PR the two states separate at the first botan_rng_get.

The runs were x86_64 only, where the timestamp is rdtsc. On aarch64 get_cpu_cycle_counter has no case, so the timestamp there comes from clock_gettime; we did not run that.

@randombit randombit added this to the Botan 3.14 milestone Oct 22, 2026

@reneme reneme left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/lib/rng/rng.cpp Resolved
Comment thread src/lib/rng/rng.cpp Resolved
Comment thread src/lib/rng/rng.cpp Resolved
Comment thread src/lib/rng/rng.cpp Outdated
Comment on lines +77 to +79
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));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@randombit

Copy link
Copy Markdown
Owner Author

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.

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.
@randombit
randombit force-pushed the jack/rng-vm-rollback branch from 1c9b8af to c4ddf51 Compare October 22, 2026 16:55
@randombit
randombit merged commit 7cdf0fd into master Oct 24, 2026
96 of 101 checks passed
@randombit
randombit deleted the jack/rng-vm-rollback branch October 24, 2026 14:50
randombit added a commit that referenced this pull request Nov 4, 2026
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.
randombit added a commit that referenced this pull request Nov 4, 2026
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.
randombit added a commit that referenced this pull request Nov 11, 2026
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.
randombit added a commit that referenced this pull request Nov 11, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants