Repository navigation
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
6af10ee to
de49bb5
Compare
| uint8_t rest[72]; | ||
| uint64_t vm_generation_counter; | ||
| }; | ||
| # define OSSL_VMCLOCK_MAGIC 0x4b4c4356u |
There was a problem hiding this comment.
Does this code need to take into account platforms using Big Endian?
There was a problem hiding this comment.
Yes. Every field of the page is little-endian (__le32 and __le64 in
include/uapi/linux/vmclock-abi.h), and the kernel reads them with le32_to_cpu() and
le64_to_cpu(). Today the driver only builds where that is the native order (depends on X86_TSC || ARM_ARCH_TIMER and ARCH_SUPPORTS_INT128, and big-endian arm64 depends on BROKEN
in 7.2), but the ABI says little-endian, so the new revision converts every field it reads, the
magic and version included, using DECLARE_IS_ENDIAN. On little-endian targets the conversion
compiles away. The test now writes the fake page in little-endian byte order, so on a big-endian
host it exercises the conversion.
| if (!RUN_ONCE(&vmclock_once, do_vmclock_init) || vmclock_page == NULL) | ||
| return 0; | ||
| do { | ||
| while (((seq = vmclock_page->seq_count) & 1) != 0) |
There was a problem hiding this comment.
is this busy loop a potential issue? (Does it need to timeout ?)
There was a problem hiding this comment.
Yes, it should be bounded. The kernel's own readers of the page give up after 100 ms
(VMCLOCK_MAX_WAIT in drivers/ptp/ptp_vmclock.c) and return -ETIMEDOUT. Here the read now
looks at the sequence count at most 1000 times, and a read that gives up counts as a change, so
the DRBG reseeds. A bound that turns out too short then costs an extra reseed, and a page that
stays odd can no longer keep a DRBG lock held. A new test leaves the page odd and checks that
RAND_bytes() returns and reseeds, and that the reseeding stops once the page is consistent
again.
|
I asked gpt about threading in this PR 1. Busy-waiting can block other threads indirectly The reader spins until it sees a stable sequence counter: It also retries indefinitely if the counter changes during the read. Although this happens before A stuck reader can therefore hold both locks indefinitely, blocking other threads needing the shared primary DRBG. This is a liveness problem rather than a demonstrated lock-order deadlock. Parent locking 2. Reading the epoch before locking permits observations to arrive out of order The code does this: For a shared DRBG, this sequence is possible:
Step | Thread A | Thread B
-- | -- | --
1 | Reads epoch 1, then pauses |
2 | | VMClock changes to epoch 2
3 | | Reads 2, locks DRBG, reseeds, records 2
4 | Resumes, locks DRBG, sees remembered 2 versus local 1 |
5 | Reseeds again and records the stale value 1 |
6 | Next caller observes 2 and reseeds again |
The write itself is protected by the lock, so this is a stale-observation race, not an unsynchronized memory write. It causes unnecessary reseeds and lets the remembered epoch move backwards. This matters particularly for the shared primary DRBG; normal public and private DRBGs are associated with individual threads. Reading the epoch after acquiring the DRBG lock would prevent this ordering problem. Read and lock placement 3. A restore can occur after the check If a snapshot captures a thread after it reads the old epoch, that thread can resume after restoration using its saved local value. If the remembered epoch matches, it can generate output without detecting the restore on that call. Moving the read inside the lock narrows this window but cannot eliminate snapshots occurring after the check. This is a limitation of the detection design, beyond ordinary thread synchronization. The initialization looks sound: I would prioritize bounded reader retries with explicit failure handling, moving the epoch read under the DRBG lock, and adding concurrency tests. The new test changes the fake page between calls, so it exercises neither concurrent page updates nor competing callers. 1. Busy-waiting can block other threads indirectly The reader spins until it sees a stable sequence counter: while (((seq = vmclock_page->seq_count) & 1) != 0) ; It also retries indefinitely if the counter changes during the read. Although this happens before ossl_prov_drbg_generate() acquires its own lock, a caller can already hold locks. During child reseeding, get_entropy() holds the child’s lock, acquires its parent’s lock, and calls the parent to generate seed material. That parent then reads VMClock. A stuck reader can therefore hold both locks indefinitely, blocking other threads needing the shared primary DRBG. This is a liveness problem rather than a demonstrated lock-order deadlock. [Parent locking](https://github.com/openssl/openssl/blob/de49bb5101421a13f02d18e61e9b841cafc98305/providers/implementations/rands/drbg.c#L219-L244) 2. Reading the epoch before locking permits observations to arrive out of order The code does this: vmclock_epoch = ossl_prov_vmclock_epoch(); /* ... */ CRYPTO_THREAD_write_lock(drbg->lock); /* ... */ drbg->vmclock_epoch = vmclock_epoch; For a shared DRBG, this sequence is possible: Step Thread A Thread B 1 Reads epoch 1, then pauses 2 VMClock changes to epoch 2 3 Reads 2, locks DRBG, reseeds, records 2 4 Resumes, locks DRBG, sees remembered 2 versus local 1 5 Reseeds again and records the stale value 1 6 Next caller observes 2 and reseeds againThe write itself is protected by the lock, so this is a stale-observation race, not an unsynchronized memory write. It causes unnecessary reseeds and lets the remembered epoch move backwards. |
🔒 Aisle Security Analysis✅ No new security issues found in this run. Aisle supplements but does not replace security review. Last updated on: 2026-10-21T03:24:51Z |
|
Thank you for the careful review. All three points hold, and the new commit addresses each. 1. Spinning with a lock held. The read is bounded, as in the inline reply. In master 2. Reading before the lock. The epoch is now read, compared and stored under 3. A snapshot after the check. Agreed: a check before generating cannot see a snapshot taken We measured this window on a reference consumer of the same shape under Firecracker, snapshotting What the second read cannot see is a snapshot taken after Two limits of the design.
The CHANGES entry names the default provider's DRBGs (the FIPS provider does not read the page) and Setup. Tests. Four VMClock tests:
Removing the second read fails the second and third tests; dropping prediction resistance from the Cost. Where the device exists, the second read adds one more read of the page per generate |
de57eee to
840d826
Compare
6462211 to
bf536a9
Compare
33f20bd to
600a474
Compare
|
Rebased onto master, which picks up the pkcs11-provider update (c4b5515) for the failing external test. @paulidale, your approval was dismissed by the push that addressed @esyr's comment on |
bukka
left a comment
There was a problem hiding this comment.
It looks reasonable, just few things
| */ | ||
| static const char *vmclock_path(void) | ||
| { | ||
| const char *path = ossl_safe_getenv("OPENSSL_VMCLOCK"); |
There was a problem hiding this comment.
shouldn't this be used on on debug build or under some macro like in other similar envs?
There was a problem hiding this comment.
Agreed, it is a test hook. It is now read only where NDEBUG is not defined, and a release build also accepts nothing but the device for the page, no regular file. drbgtest registers the VMClock tests in the same builds, so they run in the --debug jobs. If you would rather have a Configure option than NDEBUG for this, I will change it.
There was a problem hiding this comment.
I don't really like that NDEBUG as it kind of doesn't fit. The env is really just for test and I actually wonder if it's needed at all. What I was thinking is to separate the test to its own file (it's kind of separate from drbgtest.c anyway). This new test could then link it statically (using ../libcrypto.a) and could just pre-define OSSL_VMCLOCK_DEVICE. The source would just do ifndef precheck so it can be overwritten just with macro. That could be a bit more flexible and should still work.
There was a problem hiding this comment.
It is changed now. The reader takes the device path from OSSL_VMCLOCK_DEVICE, defined under #ifndef, and no longer reads an environment variable. The VMClock tests moved to test/vmclock_test.c, which defines OSSL_VMCLOCK_DEVICE as a file in its working directory, compiles its own copy of vmclock.c with it and links ../libcrypto.a, so the default provider's DRBGs use that copy; only such a copy accepts a regular file. The tests now run in release builds too. They do not cover the FIPS provider, whose copy of the reader a test cannot replace.
|
@bukka thank you for the review. Four commits, one per comment, with replies inline:
CHANGES.md and openssl-env(7) are updated to match. The commit messages now carry |
4becb19 to
a8bacfb
Compare
|
@bukka, when you have time to take another look, the commits are already in. |
|
@KurathSec It looks good. Just added comment to that env as I'm not sure it's needed. Otherwise I think it's good. |
A guest restored from a snapshot continues with the memory it had when the snapshot was taken, the DRBG states included, and its clocks resume from the snapshot too, so neither the pid check nor the time-based reseed sees the restore, and two guests loaded from one snapshot continue the same DRBG state until the next scheduled reseed. Linux exposes the hypervisor's VMClock page as /dev/vmclock0; its vm_generation_counter (Linux 7.0) changes every time the guest is loaded from saved state and its disruption_marker on a restore or a live migration. Firecracker attaches the device to every guest, QEMU with -device vmclock. Map the page once and read the two fields under its sequence lock on every generate call, and treat a change like a change of pid. Hosts without the device are unaffected. OPENSSL_VMCLOCK names another file for tests; the new drbgtest case bumps the counter on such a file and checks that the public DRBG and the primary reseed once. Assisted-by: Claude Code:claude-fable-5-1
Every field of the VMClock page is little-endian; convert the fields read, and check the version as well as the magic. Open the page with O_NONBLOCK and map only a character device or a regular file that holds the whole structure. Bound the wait on the page's sequence count and treat a read that gives up as a change, so that a page left in mid-update costs a reseed and cannot keep a DRBG lock held. Read, compare and store the epoch under the DRBG's lock, so that the remembered value cannot move backwards. After generating, read the epoch again, as the vDSO getrandom() does, and if it moved, reseed with the caller's prediction resistance and additional input and generate the output once more; clear the output if that fails. Use the page only where the compiler provides the fences. drbgtest gains a test with a seed source that moves the counter while a DRBG reseeds from it, one that leaves the page in mid-update, and one with four threads drawing while the counter moves; the stand-in file is removed at the end, and the VMClock tests are skipped if it cannot be created. CHANGES now names the default provider and states that the reseed is only as fresh as the guest kernel's generator. Assisted-by: Claude Code:claude-opus-5-5
The fake VMClock page is shared by every library context. Each VMClock test now runs in its own context and puts the generation counter back before it returns, so the default DRBGs' reseed counts, which test_rand_fork_safety and test_rand_reseed compare, do not depend on the test order (OPENSSL_TEST_RAND_ORDER). Use snprintf rather than BIO_snprintf, deprecated in 4.0, and OSSL_sleep rather than usleep, which a strict C99 build does not declare. Document OPENSSL_VMCLOCK in openssl-env(7). Assisted-by: Claude Code:claude-opus-5-5
Move the VMClock reader from providers/implementations/rands/seeding/, which holds the entropy sources, to providers/implementations/rands/ beside drbg.c, and build it into the FIPS provider as well as the default one. The DRBGs of both providers reseed when the generation changes. The mapping belongs to the library context, in the OSSL_LIB_CTX slot 7 that was unused: the first DRBG created in a context maps the page, and freeing the context unmaps it, so the FIPS provider keeps no global data outside a library context. The FIPS provider has no ossl_safe_getenv(), so it reads OPENSSL_VMCLOCK after making the check of OPENSSL_issetugid() itself. drbgtest takes an optional configuration file; given one, it runs only the VMClock tests, in library contexts that load it. test_rand runs it with fips-and-base.cnf when the FIPS provider is built; FIPS providers older than 4.2 are skipped. Drop the security-sensitive note on OPENSSL_VMCLOCK in openssl-env(7). Assisted-by: Claude Code:claude-opus-5-5
Move getenv.c and uid.c from libcrypto's own sources to UTIL_COMMON, so that the FIPS provider has ossl_safe_getenv() and OPENSSL_issetugid() too, and read OPENSSL_VMCLOCK through ossl_safe_getenv() in both providers. Assisted-by: Claude Code:claude-opus-5-5
Give ossl_prov_vmclock_ctx_new(), ossl_prov_vmclock_ctx_free(), ossl_prov_vmclock_page() and ossl_prov_vmclock_changed() the doxygen blocks that DOCUMENTATION.md asks of internal functions. Assisted-by: Claude Code:claude-opus-5-5
The page's little-endian fields are converted in code that is compiled only for Linux with GCC or a compatible compiler, so the three helpers can use __builtin_bswap16(), __builtin_bswap32() and __builtin_bswap64() in place of shifts. Assisted-by: Claude Code:claude-fable-5-2
OPENSSL_VMCLOCK lets a test name a file in place of /dev/vmclock0. It is now read only where NDEBUG is not defined, as is the acceptance of a regular file for the page; a release build opens the device and nothing else. drbgtest registers the VMClock tests in the same builds. Assisted-by: Claude Code:claude-fable-5-2
A read gives up when the page's sequence count stays odd for all of its looks. The first read that gives up counts as a change and the DRBG reseeds; it is then left alone until the page can be read again, which counts as a change once more, whatever the counter holds by then. While the page is unreadable a call looks at it once, so a page that does not settle costs a generate call two loads and no reseed, and is treated, after the one reseed, like a missing device. What a DRBG remembers between looks moves into PROV_VMCLOCK_STATE, which keeps the unreadable state apart from the generation value. Assisted-by: Claude Code:claude-fable-5-2
The reseed that a change causes draws on the kernel's generator, and the page can change before the guest's kernel has reseeded its own: the kernel does that on the VMGenID notification, shortly after the guest resumes, or else on its periodic reseed, at most 60 seconds apart. A DRBG called sooner takes seed that every copy of the snapshot may hold. After a change a DRBG therefore reseeds again on its first call at least one second, and at least 64 seconds, after the reseed that the change caused. The waits count from the completion of that reseed, which for a DRBG with a parent comes after the parent's, so a DRBG whose wait is over takes seed from a parent that has reseeded after the wait as well. They are counted on the monotonic clock. This bounds how long a DRBG keeps such seed. Output drawn before the kernel has reseeded can still repeat across the copies. Assisted-by: Claude Code:claude-fable-5-2
OSSL_VMCLOCK_DEVICE names the device, and a test may define it to name a regular file instead; only such a copy of the reader accepts a regular file. The reader no longer looks at an environment variable, so debug and release builds read the page the same way. The VMClock tests move from drbgtest to vmclock_test, which builds its own copy of the reader with a file in its working directory in place of the device and links libcrypto statically, so that the default provider's DRBGs use that copy. They run in every build on Linux. Assisted-by: Claude Code:claude-opus-5-5
The FIPS provider took getenv.c and uid.c from UTIL_COMMON for the VMClock reader's environment variable, which the reader no longer reads. Nothing in the provider calls ossl_safe_getenv() or OPENSSL_issetugid() now, so they go back to libcrypto's own sources. Assisted-by: Claude Code:claude-opus-5-5
43f5250 to
89a189b
Compare
A new feature, offered for a future release as discussed with the OpenSSL team by mail: the
provider DRBGs reseed when the VMClock generation counter changes, which is the signal Linux
gives a process that its virtual machine has been restored from a snapshot.
Since Linux 7.0 the VMClock device (drivers/ptp/ptp_vmclock.c, include/uapi/linux/vmclock-abi.h)
shares one read-only page with the guest; it carries a
vm_generation_counterthat thehypervisor bumps on every load from saved state, flagged by
VMCLOCK_FLAG_VM_GEN_COUNTER_PRESENT,beside a
disruption_markerthat also moves on migration. Firecracker attaches the device toevery guest; QEMU exposes it with
-device vmclock(as of 10.2.2 without the counter, with themarker). The DRBGs today reseed on a change of pid, on the request count and on the time
interval, and none of these changes across a restore, so every copy restored from one image
continues from the same state (RAND(7) documentation of that behaviour is in a separate pull
request).
The change:
providers/implementations/rands/vmclock.c, built into the default and FIPSproviders, maps the page of
/dev/vmclock0once per library context and reads, under the page'ssequence count, the generation counter (when the flags say it is present) and the disruption
marker, folded to one value. Each provider DRBG keeps the value it last saw in
PROV_DRBG, andossl_prov_drbg_generatetreats a change like a change of pid, so the public and private DRBGsreseed from the primary and the primary from its seed source; after generating it reads the value
again and, if it moved, reseeds and generates the output once more. That reseed draws on the
kernel's generator, which the guest's kernel may not have reseeded yet, so a DRBG reseeds again
on its first call at least 1 s, and at least 64 s, after it. A page that stays in mid-update for
a whole read counts as a change once, and once more when it can be read again. Where the device
does not exist the cost is one null compare per generate call; where it does, a few loads under a
sequence count before and after.
vmclock_testbuilds its own copy of the reader with a filestanding in for the page, moves the counter and checks the reseeds of the default provider's DRBGs.
Nothing else in the DRBGs changes: not the algorithms, the entropy sources or the fork handling.
Measured with the harness that found the gap (the same cells before and after): under
Firecracker with VMGenID and VMClock attached the restore cell goes from 32 of 32 identical
blocks to 0 of 32, under QEMU with the VMClock device from 32 of 32 to 0 of 32 (through the
marker), under QEMU without the device it stays 32 of 32 and under CRIU 14 of 14, and the fork
cells are unchanged. Per-call timing of RAND_bytes on 32-byte requests is within the spread
between benchmark batches of the unpatched build, with and without the page.
Checklist