#1

[HIGH] CSPRNG Silently Accepts Entropy-Source Failure

OpenxlabMemberopened 3mo ago

Audited commit: bfa9d9f43c2fc0a6c008544ca6ea90ab2a4dbc57

Classification

  • Type: Security control failure
  • Severity: High
  • Confidence: Certain

Affected Locations

  • src/util.zig:73 — fillRandom()
  • src/util.zig:61-63 — fillCsprng() delegates to fillRandom()
  • src/util.zig:78-83 — secureRandom() exposes fillCsprng() as the CSPRNG fill function

Summary

fillRandom() returned normally when getentropy() failed. Because the API returns void, callers could not distinguish entropy failure from successful randomization. Any unfilled portion of the requested buffer retained its caller-provided contents and was presented as cryptographically random output.

Preconditions

  • A caller requests random bytes through secureRandom() or invokes fillRandom().
  • getentropy() returns nonzero while filling the supplied buffer.

Proof

secureRandom() installs fillCsprng() as its std.Random fill function. fillCsprng() delegates directly to fillRandom().

The original failure path was:

if (c.getentropy(buf[off..].ptr, n) != 0) return;

A bounded reproducer supplied a 300-byte buffer initialized to 0x41 and interposed getentropy() so that:

  1. The first 256-byte request succeeded.
  2. The second request failed.
  3. secureRandom().bytes() returned normally.
  4. The first 256 bytes were replaced.
  5. The remaining 44 bytes retained the caller-supplied 0x41 value.

No retry, abort, error propagation, or downstream validation occurred.

Why This Is A Real Bug

The CSPRNG contract implies that a completed fill produced random bytes. Instead, entropy-source failure caused normal completion with partially or entirely predictable output. This is a deterministic fail-open in a cryptographic security control.

The output is security-sensitive: the helper is also used to generate the server-link authentication nonce in generateNonce() at src/handlers.zig:209. Entropy failure could therefore propagate predictable bytes into an authentication challenge.

Fix Requirement

Entropy-source failure must not be reported as successful random generation. The implementation must either:

  • terminate immediately on getentropy() failure, or
  • expose a fallible API and require every caller to handle the error.

Patch Rationale

The patch replaces the silent return with std.process.abort(). This preserves the existing infallible void fill interface while enforcing fail-closed behavior: the process cannot continue using unchanged buffer contents as purported CSPRNG output.

Successful chunked fills remain unchanged.

Residual Risk

None

Patch suggestion

diff --git a/src/util.zig b/src/util.zig
index 2350080..de10f8a 100644
--- a/src/util.zig
+++ b/src/util.zig
@@ -70,7 +70,7 @@ pub fn fillRandom(buf: []u8) void {
     var off: usize = 0;
     while (off < buf.len) {
         const n = @min(256, buf.len - off);
-        if (c.getentropy(buf[off..].ptr, n) != 0) return;
+        if (c.getentropy(buf[off..].ptr, n) != 0) std.process.abort();
         off += n;
     }
 }

Activity

0

No activity yet.