Skip to content

[Review] Merge bitcoin/bitcoin#36838: guix: backport oscrypto PBKDF2 timing fix - #10

Merged
jonatack merged 1 commit into
bitcoin-v:masterfrom
BTC4Peace:audit/pr-5c4e8374074f
Dec 1, 2026
Merged

jonatack merged 1 commit into
bitcoin-v:masterfrom
BTC4Peace:audit/pr-5c4e8374074f

Conversation

@auditbob

@auditbob auditbob Bot commented Dec 1, 2026

Copy link
Copy Markdown

Upstream commit: bitcoin/bitcoin@5c4e837

1575462b22b8584a49103eee7ad5a7ccf3289c4b guix: backport oscrypto PBKDF2 timing fix (Sjors Provoost)

Pull request description:

With substitutes disabled, guix-codesign can fail when building oscrypto 1.3.0. The test_dump_private test raises ZeroDivisionError.

The Guix time-machine bump in bitcoin/bitcoin#36233 (merged after the 32.x branch-off) pulls in OpenSSL 3.5.5, which can finish oscrypto's initial PBKDF2 benchmark in under a millisecond. oscrypto truncates this duration to zero and then divides by it. This was fixed upstream in wbond/oscrypto@144c121

This PR adds the fix to contrib/guix/patches. It can be dropped once a new oscrypto release includes the fix and a Guix time machine bump includes it.

ACKs for top commit:
fanquake:
ACK 1575462b22b8584a49103eee7ad5a7ccf3289c4b

Tree-SHA512: 6a9e5bccb3d239d1638cc4005445359d1674e7b3a5dfc037d9cac92757047068db154d5574b382e90ac2995d8ebdf8f15551021ad399683b8a5d92f1bd6965a2

1575462b22b8584a49103eee7ad5a7ccf3289c4b guix: backport oscrypto PBKDF2 timing fix (Sjors Provoost)

Pull request description:

  With substitutes disabled, `guix-codesign` can fail when building [oscrypto](https://pypi.org/project/oscrypto/) 1.3.0. The `test_dump_private` test raises `ZeroDivisionError`.

  The Guix time-machine bump in bitcoin#36233 (merged after the 32.x branch-off) pulls in OpenSSL 3.5.5, which can finish oscrypto's initial PBKDF2 benchmark in under a millisecond. oscrypto truncates this duration to zero and then divides by it. This was fixed upstream in wbond/oscrypto@144c121

  This PR adds the fix to `contrib/guix/patches`. It can be dropped once a new oscrypto release includes the fix and a Guix time machine bump includes it.

ACKs for top commit:
  fanquake:
    ACK 1575462b22b8584a49103eee7ad5a7ccf3289c4b

Tree-SHA512: 6a9e5bccb3d239d1638cc4005445359d1674e7b3a5dfc037d9cac92757047068db154d5574b382e90ac2995d8ebdf8f15551021ad399683b8a5d92f1bd6965a2

Cherry-picked-from: bitcoin/bitcoin@5c4e837
@auditbob

auditbob Bot commented Dec 1, 2026

Copy link
Copy Markdown
Author

1. Summary

This merge backports upstream wbond/oscrypto@144c1219 as contrib/guix/patches/oscrypto-pbkdf2-timing.patch and wires it into the codesigning manifest. contrib/guix/manifest_codesign.scm:6-8 now imports the upstream package under a renamed binding ((python-oscrypto . guix:python-oscrypto)), and contrib/guix/manifest_codesign.scm:18-21 re-defines python-oscrypto via (package-with-extra-patches guix:python-oscrypto (list (local-file "patches/oscrypto-pbkdf2-timing.patch"))). local-file is newly imported from (guix gexp) at line 13.

Effect scope, verified: the only manifest consumer is guix-codesign (contrib/guix/guix-codesign:326 passes --manifest="${PWD}/contrib/guix/manifest_codesign.scm"), and inside the manifest every reference to python-oscrypto (lines 49, 52, 82, 153) resolves to the locally defined, patched variant; guix:python-oscrypto itself is referenced only at line 20. A repo-wide search for oscrypto returns no hits outside contrib/guix/manifest_codesign.scm and the new patch, so contrib/guix/manifest.scm/manifest_build.scm (release builds) are untouched.

Motivation (from the PR description): after the time-machine bump in #36233 pulled in OpenSSL 3.5.5, oscrypto 1.3.0's PBKDF2 calibration can complete in under a millisecond; the duration was truncated to zero and subsequently used as a divisor, crashing test_dump_private and therefore the from-source python-oscrypto build when substitutes are disabled. The patch (a) makes _get_elapsed return fractional milliseconds and (b) clamps observed_ms to a minimum of 1 in _measure.

2. Consensus & Validation

None. No src/ files, chainstate/UTXO, script/block validation, mempool, or reorg code is touched; the change is Guix build tooling for the codesigning environment (win64 uses osslsigncode-2.14/nss-certs; darwin pulls python-signapple, contrib/guix/manifest_codesign.scm:203-208). No release artifact is derived from the patched package.

3. Security, P2P & DoS

None for Bitcoin Core runtime. I read all 33 lines of contrib/guix/patches/oscrypto-pbkdf2-timing.patch: it contains no imports, no I/O, no shell/exec, and no code other than the two arithmetic edits (contrib/guix/patches/oscrypto-pbkdf2-timing.patch:18-21,29-30) plus the third-party commit header; the hunk arithmetics are self-consistent (both hunks have matching old/new line counts), and the patch is a/-b/ prefixed for -p1 application.

  • [Nit] The patch turns garbage-in into a bounded value: _get_elapsed measures with datetime.now() (wall clock, not monotonic), and the clamp also absorbs negative measurements if the clock steps backwards. This is strictly safer than the previous crash-on-zero behaviour.
  • [Nit] Provenance is in-file only (the From 144c1219... / From: wbond header); there is no upstream URL trailer. I cannot verify byte-for-byte equality with the upstream commit from this repository, but the diff content itself is benign and the PR description names the upstream commit.
  • [Nit] A context mismatch against the oscrypto tarball supplied by the pinned Guix (commit 60f6956aeffa7f30285745bd0ea615e9acfc74f8, contrib/guix/libexec/prelude.bash:73-74) would surface as a failed codesign build, not silently unpatched code — i.e. fail-closed, but it breaks guix-codesign for all signers until updated.

4. Concurrency & Locking

None. No C++ code, mutexes, or lock annotations involved.

5. Code Quality & Test Coverage

  • The #:select rename pair at contrib/guix/manifest_codesign.scm:6-8 is mandatory, not cosmetic: without renaming the import, (define-public python-oscrypto ... (package-with-extra-patches python-oscrypto ...)) at lines 18-21 would self-reference. It is the only dotted rename in the tree (verified by search), but it is a documented Guile #:select form, and a failure here would abort manifest loading loudly.
  • [Nit] local-file relative resolution: "patches/oscrypto-pbkdf2-timing.patch" (line 21) matches the pattern already used at contrib/guix/toolchains.scm:97-98,118,122,259-260 for files in the same contrib/guix/patches/ directory (verified present), so the idiom is precedented in this tree.
  • [Nit] Local patched package means signers lose substitutes for the whole chain: package-with-extra-patches changes python-oscrypto's source/derivation output, so python-oscryptotests (inherits it, line 49), python-certvalidator (line 82) and python-signapple (line 153) must also be built locally even with substitutes enabled. All are small pure-Python builds; acceptable, but worth knowing for codesign turnaround.
  • [Nit] The comment at contrib/guix/manifest_codesign.scm:19 explains the symptom but records neither upstream commit 144c1219 nor the drop condition ("new oscrypto release + time-machine bump"). Other entries in contrib/guix/patches/ carry prose or commit headers, and the drop condition lives only in the PR description, so a future Guix bump is likely to leave this patch behind until it fails to apply.
  • [Nit] define-public python-oscrypto reuses the upstream name for a locally patched variant (lines 18-21); required for the existing (package (inherit python-oscrypto) ...)/propagated-inputs references, but guix show-style output will be ambiguous. Not worth churn.
  • Test coverage: no CI exercises this. Searching .github/workflows for guix returns nothing, and test/lint/lint-files.py has no patch-file handling — the new file satisfies ALLOWED_FILENAME_REGEXP at test/lint/lint-files.py:21 and is 100644, so lint will not object. Verification is therefore manual guix-codesign with substitutes disabled, exactly the environment in the PR. This gap is pre-existing, not introduced here.
  • Post-merge hygiene: the patch file is only named in the manifest; an automated reference check (e.g. a grep in lint) does not exist, so if the manifest line is ever reverted the patch file would silently become dead weight. Cosmetic.

6. Cross-Audit Reconciliation & Post-Merge Verdict

Confirmed by both auditors and re-verified here: the manifest wiring and all three manifest-level consumers (contrib/guix/manifest_codesign.scm:49,52,82,153); the patched source is inherited by python-oscryptotests; the patch is scoped to codesigning only; the local-file path and (guix gexp) import; the pinned time-machine commit (libexec/prelude.bash:74); and the "no CI coverage" observation.

Checked by one auditor and verified: package-with-extra-patches usage precedent (contrib/guix/toolchains.scm:96,117,121) and the --manifest load site (guix-codesign:326, which evaluates the manifest on the host against ${PWD}, so the relative local-file path is reachable); signapple apply in contrib/guix/libexec/codesign.sh:117,120.

Checked by one auditor and dropped/downgraded: the claimed [Risk] that package-with-extra-patches may not be exported by (guix packages) is unsupported — contrib/guix/toolchains.scm imports (guix packages) wholesale and uses that symbol, so it is available in the pinned Guix's (guix packages); likewise the "only dotted rename in the tree" concern is a Guile-supported form and would fail at load, not silently. The [Follow-up] about a provenance/removal comment is real but bookkeeping only; I record it as a [Nit] rather than a required fix. Audit_b's claim that oscrypto's own tests (including test_dump_private) run inside the Guix python-oscrypto build cannot be verified from this repository (the package definition lives in Guix, not here); the PR description is the only support, so treat it as plausible-but-unverified.

Could not verify from this repo (inherent): that the patch applies cleanly to the exact oscrypto 1.3.0 sdist used by the pinned Guix, and that the caller in kdf.py performs the division by the value returned from the _measure closure — the patch shows only return 1.0 / target_ms * observed_ms as context (.../oscrypto-pbkdf2-timing.patch:33). Both require an actual guix-codesign run. The int→float change in _get_elapsed is safe from inspection because that helper is nested inside pbkdf2_iteration_calculator (the hunk context at patch line 14 shows the enclosing def _get_start(): and 4-space-extra indentation), so its return type is local to the measurement code.

Overall risk: low — build-only change, benign arithmetic, fail-closed on patch mismatch. Key lines to re-check in review: contrib/guix/manifest_codesign.scm:6-8,13,18-21 and contrib/guix/patches/oscrypto-pbkdf2-timing.patch:14-33.

Triage: routine


📊 Token Usage & Review Stats
Stage Model Input Tokens Cache Hit Output Reasoning Total Rounds Latency
Audit A deepseek-flash 135,789 123,776 (91%) 21,801 18,169 157,590 8 140.4s
Audit B gemini-4-neon-high 182,727 132,339 (72%) 6,962 5,066 189,689 6 81.4s
Judge deepseek-flash 150,832 131,840 (87%) 14,491 10,746 165,323 8 91.9s
Total — 469,348 387,955 (83%) 43,254 33,981 512,602 22 313.7s

@auditbob auditbob Bot added audited Automated review completed Build system labels Dec 1, 2026

@jonatack jonatack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK e0e9235

@jonatack
jonatack merged commit 103f779 into bitcoin-v:master Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

audited Automated review completed Build system

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants