20260907-RNG-extras - #11435
20260907-RNG-extras#11435
Conversation
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11435
Scan targets checked: none
Failed targets: linuxkm-src, linuxkm-bugs, wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
|
Note the kernel module build failures are expected, and will resolve once #11382 merges. |
|
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11435
Scan targets checked: linuxkm-src, linuxkm-bugs, wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Findings: 17
17 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
71f1f6b to
97bdcee
Compare
97bdcee to
0c4cec3
Compare
0c4cec3 to
4893d1c
Compare
* add NO_DEFAULT_FOUND_E "No default object registered for request type". * add missing #include <wolfssl/wolfcrypt/logging.h> in WOLFSSL_DEBUG_TRACE_ERROR_CODES path. * add __func__ to __GNUC__ WC_ERR_TRACE(). wolfcrypt/src/rng_bank.c, wolfssl/wolfcrypt/rng_bank.h, wolfcrypt/test/test.c: * add wc_rng_bank_inst.bank for parent access from the children. * add wc_rng_bank_inst_checkin(). * improve thread safety, error checking, and default bank support in wc_rng_bank_checkout(), wc_local_rng_bank_checkout_for_bankref(), wc_rng_bank_inst_reinit(), wc_rng_bank_seed(), wc_rng_bank_reseed(), and wc_InitRng_BankRef_local(). * orthogonalize common code in wc_InitRng_BankRef() and wc_rng_new_bankref() into wc_InitRng_BankRef_local(). wolfcrypt/test/test.c: * fix typo in PRINT_HEAP_CHECKPOINT(). * add wc_rng_bank_inst_checkin() tests to random_bank_test(), update expected failure codes, and remove obsolete test clauses. * add missing !HAVE_FIPS gates in cryptocb_test(). wolfcrypt/src/random.c: * in WC_RNG_BANK_SUPPORT variant of wc_RNG_GenerateBlock(), use the new wc_rng_bank_inst_checkin(), not wc_rng_bank_checkin(). * tweaks to WOLFSSL_DEBUG_TRACE_ERROR_CODES code wrappers to mollify clang-tidy and -Wconversion. * fix a spelling error in _InitRng(). linuxkm/lkcapi_sha_glue.c: * in wc_linuxkm_drbg_generate(), opportunistically reseed once half way to WC_RESEED_INTERVAL and wc_linuxkm_can_block(). Also properly inhibit the recovery call to wc_rng_bank_inst_reinit() if ! wc_linuxkm_can_block(). * refactor wc_mix_pool_bytes() to use wc_RNG_DRBG_Reseed(), and only on the CPU-local RNG. wolfcrypt/src/asn.c, wolfcrypt/src/curve25519.c, wolfcrypt/src/evp.c, wolfcrypt/src/pkcs7.c, wolfcrypt/src/pkcs12.c, wolfcrypt/src/srp.c: at each existing wc_InitRng(), attempt wc_InitRng_BankRef() if WC_RNG_BANK_DEFAULT_SUPPORT && WC_HAVE_RNG_BANKREF.
…mproved dynamics.
… wolfcrypt/src/pkcs7.c, wolfcrypt/src/pkcs12.c, wolfcrypt/src/srp.c: revert wc_InitRng_BankRef() airdrops.
* supply WC_DRBG_OK for HAVE_FIPS with FIPS_VERSION3 < 5.2.4 or == 6.0.0, for the DRBG health tests in wc_rng_bank_checkout(), wc_rng_bank_seed(), and wc_rng_bank_reseed(); * in rng_inst_matches_bank(), compare integer addresses (wc_ptr_t), and compute the mid-instance alignment check on the integer difference -- the negative tests deliberately supply fabricated pointers, for which pointer relationals and subtraction are undefined (C11 6.5.8p5 / 6.5.6p9), while integer comparisons are defined for any value.
wc_rng_bank checkin and seed edge cases from the 20260820 review batch: duplicate (stale-copy) checkin through both wc_rng_bank_checkin() and wc_rng_bank_inst_checkin(), asserting BAD_STATE_E without bank mutation; seedSz == 0 no-op success for an explicit inited bank and for the default form while a default is set; and seedSz == 0 with no default bank set, asserting NO_DEFAULT_FOUND_E.
blockability into can_wait before the DRBG checkout, and gate the opportunistic reseed and the RNG_FAILURE_E reinit recovery on it -- wc_linuxkm_can_block() is false whenever the affinity lock is held (the lock callback is SAVE_VECTOR_REGISTERS_MAYBE_INHIBIT()), so the previous live-sampled guards were unsatisfiable in every reachable state and both features were dead. For each of the two blocking operations, bracket it to restore preemptibility while keeping the instance checked out: migrate_disable() (CONFIG_SMP && >= 5.11.0 -- on earlier kernels migrate_disable() is absent or maps to preempt_disable(), so migration during the operation is simply tolerated there), then RESTORE_VECTOR_REGISTERS_MAYBE_INHIBITED() keyed on the lock's _AFFINITY_LOCKED/_VEC_OPS_INH bits, then the blocking operation preemptibly (wc_rng_bank_inst_reinit()'s WC_RELAX_LONG_LOOP now sees can_block() true and actually yields), then re-establish the vector state flavor-matched (SAVE_VECTOR_REGISTERS2() for the affinity hold, DISABLE_VECTOR_REGISTERS() for the inhibit hold), then migrate_enable(). If re-establishment fails, clear the corresponding lock bits so checkin doesn't double-restore; the cleared bits also make a failed bracket a valid entry state for the subsequent one. Plain (non-atomic) accesses to drbg->lock are sound across the span: WC_RNG_BANK_INST_LOCK_HELD is invariantly held, and it is the only bit considered by contending threads. (root defect identified in 20260820 review batch; see PR discussion for why the review's proposed fix -- pre-captured blockability alone, without releasing the holds -- would have converted the dead code into sleep-in-atomic / bh-off stalls)
…r message on failed wc_rng_bank_default_checkout(), and rework the error message when wc_linuxkm_drbg_generate() fails.
…ialize seed and lenp to NULL to work around false positive from CI.
….c: fix RNG-extras support for HAVE_INTEL_RDRAND.
kaleb-himes
left a comment
There was a problem hiding this comment.
Approved as is for functionality, there are still some open compliance items we need to discuss with the CSTL but we can address them after we split the v7 branch from the master branch.
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical and moderate issues affect RNG correctness, compatibility, and kernel integration.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR expands wolfSSL’s RNG and RNG-bank facilities with lifecycle, recovery, locking, pooling, and Linux kernel integration support.
Changes:
- Adds expanded RNG and RNG-bank APIs, configuration, and lifecycle features.
- Updates kernel integration, tests, documentation, and supporting cryptographic code.
- Adds configuration macros and workflow updates.
File summaries
| File | Change |
|---|---|
wolfssl/wolfcrypt/wc_port.h |
Adds CAS retry and timing abstractions. |
wolfssl/wolfcrypt/types.h |
Updates constructor and no-malloc configuration behavior. |
wolfssl/wolfcrypt/settings.h |
Updates RNG defaults and debug-stat configuration. |
wolfssl/wolfcrypt/rsa.h |
Declares RSA RNG-detachment support. |
wolfssl/wolfcrypt/rng_bank.h |
Defines expanded RNG-bank flags, state, and APIs. |
wolfssl/wolfcrypt/random.h |
Defines expanded RNG features and APIs. |
wolfssl/wolfcrypt/error-crypt.h |
Supports related cryptographic error handling. |
wolfssl/wolfcrypt/ecc.h |
Updates ECC declarations for the RNG expansion. |
wolfssl/wolfcrypt/curve25519.h |
Updates Curve25519 declarations for the RNG expansion. |
wolfcrypt/test/test.h |
Extends test declarations for RNG coverage. |
wolfcrypt/src/rsa.c |
Implements RSA RNG detachment. |
wolfcrypt/src/error.c |
Updates related error handling. |
wolfcrypt/src/ecc.c |
Integrates ECC implementation changes. |
wolfcrypt/src/curve25519.c |
Integrates Curve25519 implementation changes. |
tests/unit-mcdc/test_random_whitebox.c |
Adds white-box RNG coverage. |
tests/unit-mcdc/test_random_fault_whitebox.c |
Adds RNG fault-path coverage. |
linuxkm/patches/7.3/WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-7v3.patch |
Updates kernel random callback integration. |
linuxkm/lkcapi_glue.c |
Updates kernel RNG glue, controls, and statistics. |
linuxkm/linuxkm_wc_port.h |
Adds Linux-kernel portability support. |
linuxkm/include.am |
Updates kernel build integration. |
doc/dox_comments/header_files/rng_bank.h |
Documents expanded RNG-bank APIs. |
doc/dox_comments/header_files/random.h |
Documents expanded RNG APIs. |
configure.ac |
Adds RNG-extras configuration. |
.wolfssl_known_macro_extras |
Registers RNG configuration macros. |
.github/workflows/codespell.yml |
Updates spelling-check workflow configuration. |
Review details
Suppressed comments (6)
doc/dox_comments/header_files/random.h:923
wc_RNG_DRBG_Reseed_Now()returns 0 when no DRBG is present (for example, RDRAND-backed RNGs); the implementation explicitly treats that as nothing to reseed. This return-value documentation incorrectly promisesWRONG_TYPE_OBJECT_Efor that case.
\return WRONG_TYPE_OBJECT_E rng has no DRBG (RDRAND et al.).
doc/dox_comments/header_files/random.h:1619
- The public header explicitly says a null
free_hookunregisters an existing hook, but this Doxygen return description says nullfree_hookis invalid. Correct the documentation so API consumers are not told that the documented unregister operation is an error.
\return BAD_FUNC_ARG rng or free_hook is null.
doc/dox_comments/header_files/rng_bank.h:695
- The parameter list documents
nonceandnonceSzbeforefirst_inst/last_inst, but the declaration immediately below takes the instance range first and the nonce afterward. Generated API documentation therefore presents the arguments in the wrong order.
\param bank The bank to reseed.
\param nonce Optional per-instance nonce material (may be null).
\param nonceSz Length of nonce in bytes.
\param first_inst The first instance offset.
doc/dox_comments/header_files/rng_bank.h:697
- This says the wait budget is per instance, but
wc_rng_bank_reseed_range()initializes its timer once before the whole range and reuses it for every instance. A slow earlier instance can therefore consume the budget and cause later instances to time out early. Either reset the timer for each instance or document this as a walk-wide budget.
\param timeout_secs Wait budget per instance.
linuxkm/patches/7.3/WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-7v3.patch:470
- When callback ownership is live but the callback reports not-ready, this fallback still returns readable whenever the native CRNG is ready.
random_read_iter()now gates nonblocking reads oncrng_ready_by_cb()and can return-EAGAIN, so poll/select may wake readers that immediately fail. Make poll use callback readiness whenever callbacks are live, and consult native readiness only when they are not.
return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM;
wolfssl/wolfcrypt/rng_bank.h:65
WC_16BIT_CPUbuilds defineword32asunsigned longwhileunsigned intcan remain 16 bits. Consequently,1U << 16is not a valid 32-bit flag expression on those supported targets; use a 32-bit operand for this new bit.
#define WC_RNG_BANK_FLAG_AUTO_RECOVER_AND_PROMOTE (1U << 16)
- Files reviewed: 25/29 changed files
- Comments generated: 9
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11435
Scan targets checked: linuxkm-src, linuxkm-bugs, wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Findings: 18
18 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
wolfssl/wolfcrypt/settings.h: fixes for a batch of static-analysis findings. * complete the WC_CAS_WITH_RETRY post-loop dispositions in rng_bank.c and the lock_put()s: aborted releases percolate before the lock word (and, in wc_RNG_lock_put(), the mutex) is reported free, keeping ownership with the caller for retry; the inst_lock_*_extra() setters return the CAS result; wc_rng_bank_inst_invalidate_entropy() latches or condemns, mirroring wc_RNG_invalidate_entropy(). * wc_RNG_invalidate_entropy(): tolerate WRONG_TYPE_OBJECT_E from wc_RNG_DRBG_ScheduleReseed() -- a no-DRBG (direct-RDRAND) instance has nothing to schedule and nothing whose staleness the latch would mark; don't condemn it. * wc_rng_bank_reseed_range(): fix undeclared identifier (inst -> drbg) in the HAVE_FIPS && FIPS_VERSION3_LT(7,0,0) && WC_RNG_HAVE_RBGC branch. * _InitRng(): free and clear the full mutex when wc_LockMutex() fails after successful wc_InitMutex() (WC_RNG_INIT_FLAG_LOCK_INITIALLY). * settings.h: WC_RESEED_INTERVAL (1UL << 48UL) -> (W64LIT(1) << 48) -- unsigned long is 32 bits on LLP64 targets (64-bit MinGW), making the shift undefined. * test.c: silence unused i in random_bank_test() when the reseed-interval check is configured out.
…bank.c, wolfcrypt/test/test.c, wolfssl/wolfcrypt/random.h: fixes for a second batch of static-analysis findings. * vmgenid fallback poller: search the ACPI IDs the kernel vmgenid driver matches -- "VMGENCTR" and "VM_GEN_COUNTER" -- rather than the spec's mixed-case spellings. ACPICA upcases _HID/_CID strings when building the namespace and acpi_get_devices() matches by strcmp, so the old walk found QEMU only by its _HID and missed CID-only devices (Hyper-V, VMware). * wc_linuxkm_drbg_generate(): in non-vector builds, drop local_bh_disable() around the blockable CAN_WAIT reseed and reinit operations and retake it after, mirroring the vector arm's release/reacquire bracket; the checkout's migrate_disable() persists across the sleep, preserving CPU pinning preemptibly. * entropy daemon and sysfs stats dumps: gate the next-stir format fragments and arguments on WC_RNG_HAVE_NEXT_SEED. * new WC_LKM_BANK_RBGC_FLAG (WC_RNG_BANK_FLAG_RBGC when WC_RNG_HAVE_RBGC, else WC_RNG_BANK_FLAG_NONE): keep LKCAPI functional without RBGC support compiled in. * wc_rng_bank_init_nonce(): use (size_t)(-1) rather than SIZE_MAX in the allocation overflow check (C90 and NO_STDINT_H builds). * wc_rng_bank_fini(): pre-scan all instances for WC_RNG_LOCK_HELD before any mutation, returning BUSY_E (retryable) with refcount untouched, free hook unfired, and root intact. * random.h: #error on WC_RNG_DEBUG_STATS without the RNG lock facility. * wc_RNG_DRBG_Stir_Nonce(): reject nonce == NULL with nonzero nonceSz (BAD_FUNC_ARG), matching the other nonce-bearing APIs. * random_bank_test(): hoist held_inst to function scope and check it in from the shared teardown path, closing a leak on failing assertions. * wc_RNG_DRBG_NextSeedGenerate_local(): pointer declaration style cleanup.
Expansion of core RNG facility to add kernel-facing semantics.
The SP 800-90 compliance report is the best introduction to the expansion:
rng-extras-compliance-report (2).pdf
Also expands rng_bank facility with corresponding functionalities, and refactors corresponding portions of
linuxkm/lkcapi_sha_glue.cto follow suit.Tested with full run
wolfssl-multi-test.sh(530 scenarios).Relevant new scenarios:
Note, tested atop #11382, and must merge after #11382 merges.