Fix UAF in DH buffers - #11115
Conversation
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11115
Scan targets checked: wolfcrypt-rs-bugs, wolfssl-bugs, wolfssl-src
Findings: 10
10 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Findings are non-blocking.
|
0aa1ca3 to
2c81e99
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11115
Scan targets checked: wolfcrypt-rs-bugs, wolfssl-bugs, wolfssl-src
Findings: 6
5 finding(s) posted as inline comments (see file-level comments below)
Info (1)
Buffers struct comments still claim the CTX owns serverDH_P/serverDH_G
File: wolfssl/internal.h:5261
Function: Buffers
Category: Logic errors
CopySSL_CTX_DhParams makes the SSL object always own p/g when they come from the CTX, and the only remaining weOwnDH == 0 case (tls.c:5467) points at static DhParams tables that neither object owns. The "WOLFSSL_CTX owns, unless we own" comments now describe an ownership model that no longer exists, two lines above the new declaration.
Recommendation: Reword both comments to state that the SSL owns p/g when weOwnDH is set and otherwise points at static named-group parameters.
Referenced code: wolfssl/internal.h:5261-5262 (2 lines)
This review was generated automatically by Fenrir. Findings are non-blocking.
bea08dd to
f3375ba
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11115
Scan targets checked: wolfcrypt-rs-bugs, wolfssl-bugs, wolfssl-src
Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Findings are non-blocking.
f3375ba to
072d89c
Compare
f6e5f7a to
233309d
Compare
|
jenkins retest this please |
233309d to
bf4b4aa
Compare
There was a problem hiding this comment.
Pull request overview
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Fixes a use-after-free risk where WOLFSSL sessions could alias WOLFSSL_CTX DH parameter buffers (P/G) without ownership, then later observe dangling pointers when the context rotates/replaces DH parameters.
Changes:
- Introduce
CopySSL_CTX_DhParams()to deep-copy DH (P/G) buffers fromWOLFSSL_CTXintoWOLFSSLsessions. - Update
SetSSL_CTX()andwolfSSL_set_accept_state()to use the copy helper instead of aliasing context buffers. - Add/adjust unit tests to validate DH parameter copying, rotation safety, reuse behavior, and OOM handling.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
wolfssl/internal.h |
Documents DH buffer ownership semantics and declares the new DH copy helper. |
src/internal.c |
Implements DH parameter deep-copying and adjusts resource cleanup behavior. |
src/ssl_api_hs.c |
Uses the new DH copy helper during wolfSSL_set_accept_state(). |
tests/api/test_tls13.h |
Registers new TLS 1.3 DH-related tests. |
tests/api/test_tls13.c |
Adds TLS 1.3 tests for DH rotation and accept-state DH copying. |
tests/api/test_ssl_hs.h |
Registers new handshake tests for DH reuse and OOM behavior. |
tests/api/test_ssl_hs.c |
Updates existing DH expectations and adds new DH reuse/OOM coverage. |
Suppressed comments (4)
src/internal.c:1
- Returning success when the context has no DH buffers leaves any existing
ssl->buffers.serverDH_{P,G}state untouched. SinceSetSSL_CTX()now relies on this helper, switching an SSL from a context-with-DH to a context-without-DH can retain stale DH parameters that no longer match the new context. Consider treating missing ctx DH params as a 'clear' operation: ifssl->buffers.weOwnDHis set, free existing P/G buffers, set both pointers to NULL, set both lengths to 0, and resetweOwnDH(and ensure the caller keepsoptions.haveDHconsistent).
src/internal.c:1 - When freeing client-side DH parameters in
FreeHandshakeResources(), the code clears the pointers but leavesssl->buffers.weOwnDHand theserverDH_{P,G}.lengthfields unchanged. This can leave the object in an inconsistent state (e.g., 'owns DH' but buffers are NULL), which is error-prone for later cleanup paths and any logic that consults lengths/ownership. After freeing, also resetweOwnDHto 0 and set both DH lengths to 0 (and keepoptions.haveDHconsistent as applicable).
src/internal.c:1 CopySSL_CTX_DhParamsis declared asWOLFSSL_LOCALinwolfssl/internal.h, but the definition insrc/internal.comitsWOLFSSL_LOCAL. To ensure consistent symbol visibility/linkage (and avoid unintentionally exporting the symbol on some toolchains), annotate the definition withWOLFSSL_LOCALas well.
src/internal.c:1- Given the behavior change (sessions now copy DH params in
SetSSL_CTX()), it would be useful to add a unit test that switches an existingWOLFSSL*between two contexts where the second context has no DH parameters. The test should assert that the SSL object's DH buffers/lengths/ownership are cleared (andhaveDHis consistent), so it doesn't retain stale parameters from the previous context.
💡 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 #11115
Scan targets checked: wolfcrypt-rs-bugs, wolfssl-bugs, wolfssl-src
No new issues found in the changed files. ✅
Description
Fixes the case where
SetSSL_CTXaliases theserverDH_PandserverDH_Gbuffers withweOwnDHstill at 0. A subsequent call towolfSSL_CTX_SetTmpDH*would result in a dangling pointer.Related to #11109, but with DH buffers. Note that refcounting is not performed here since the API uses plain byte buffers instead of structs/objects.
weOwnDH == 0currently means either "CTX heap buffer" or "static named-group params" today. After this change, it only means the static case which removes ambiguity in the free paths.Testing
Added unit tests.
Checklist