Repository navigation
Fenrir 15782: wolfSSL Static blocks are ref counted and freed by last ref - #11899
aidankeefe2022 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new regression tests need configuration fixes for builds without PEM parsing or TLS 1.2 client authentication.
2 open issues
What changed in this PR
This PR fixes session memory ownership so retained sessions do not depend on a connection’s freed static-memory heap hint.
Changes:
- Create sessions using the context heap during initialization and reset.
- Use the session heap for tickets, ticket nonces, and retained peer certificates.
- Add regression tests for session retention, duplication, resumption, and cleanup.
| File | Description |
|---|---|
tests/api.c |
Adds static-memory session lifetime and cleanup tests. |
src/ssl.c |
Uses the context heap when wolfSSL_clear() replaces a session. |
src/ssl_api_cert.c |
Allocates peer-chain certificates from the session heap. |
src/internal.c |
Corrects session creation and ticket-related heap usage. |
Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11899
Scan targets checked: wolfssl-src, wolfssl-bugs
Coverage: 2 of 4 in-scope changed file(s) opened by the reviewer; not opened: src/internal.c, tests/api.c
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
329a888 to
e508cc1
Compare
|
retest this please |
|
julek-wolfssl
left a comment
There was a problem hiding this comment.
This is not a maintainable solution. Caller sites should not have knowledge about which hint to use. I would prefer to store a reference counter in WOLFSSL_HEAP_HINT and let the allocator figure out when it can be released. Attached is a detailed description of what I envision.
With WOLFSSL_STATIC_MEMORY, each SSL gets its own heap hint, and wolfSSL_free() releases it. Some objects allocated from that hint can live longer than the SSL: a session kept with wolfSSL_get1_session(), the server's peer cert stored in the session, or an X509 returned by wolfSSL_get_peer_certificate(). After wolfSSL_free(), these objects point at a hint that no longer exists. Update the static heap session, ticket and peer cert chain tests to check the hint's refCount as the SSL, sessions and X509s are freed. Add a case where an X509 is the last holder. Check that curAlloc returns to its baseline (except with WOLFSSL_STATIC_MEMORY_LEAN). Fix test_wolfSSL_CTX_add_session_ext without WOLFSSL_TICKET_HAVE_ID: a TLS 1.2 ticket session has no session ID, so it can't be added to the cache, and the remove callback isn't called on resumption.
e508cc1 to
09fb499
Compare
| wc_Memory* inBuf; | ||
| byte haFlag; /* flag used for checking handshake count */ | ||
| #endif | ||
| word32 refCount; /* SSL ref + other refs */ |
There was a problem hiding this comment.
This needs to be wolfSSL_Ref and use the accompanying functions for using this type.
| #ifdef WOLFSSL_STATIC_MEMORY | ||
| void* heap = x509->heap; | ||
| #endif | ||
| FreeX509(x509); | ||
| XFREE(x509, x509->heap, DYNAMIC_TYPE_X509); | ||
| #ifdef WOLFSSL_STATIC_MEMORY | ||
| /* after the XFREE: it may free the hint x509 came from */ | ||
| wc_HeapHint_Unref(heap); | ||
| #endif | ||
| } |
There was a problem hiding this comment.
My idea was that FreeSSL_StaticMemory would mark the heap as ready for release and then the last XFREE call would de-allocate the heap hint. This way wc_HeapHint_Unref doesn't need to be called by any caller and everything happens under the hood.
There was a problem hiding this comment.
Ohh yeah that is better! Fixing now
|
retest this please |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11899
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Coverage: 5 of 6 in-scope changed file(s) opened by the reviewer; not opened: tests/api.c
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
There was a problem hiding this comment.
🟡 Changes recommended
The API tests still call XREALLOC when WOLFSSL_STATIC_MEMORY and WOLFSSL_NO_REALLOC are enabled, causing link failure because wolfSSL_Realloc() is compiled out.
1 open issue
2 resolved since last review
Review effort: Balanced
Static blocks now keep a ref count and only free when all holders are done holding.
fce7f12 to
a02fbcd
Compare
|
added some guards to tests this just passed CI before that change assigning to wolfSSL_Bot |


Description
With
WOLFSSL_STATIC_MEMORY, each SSL gets its own heap hint, andwolfSSL_free()used to release it. Some objects allocated from that hint can outlive the SSL: a session fromwolfSSL_get1_session(), the server's peer cert stored in the session, or an X509 fromwolfSSL_get_peer_certificate(). AfterwolfSSL_free(), freeing or using these objects touched a heap hint that had already been freed.wolfSSL_Ref). The SSL holds one reference, and so does every block allocated from the hint.wc_Memory.next, a field that sits unused while the block is allocated.wolfSSL_Free()drops that reference, so the hint goes back to the pool when its last block is freed.wolfSSL_free()drops the SSL's reference throughwc_HeapHint_Release().WOLFMEM_IO_POOL_FIXED) belong to the SSL and hold no reference. IO pool buffers do hold one.Testing
test_wolfSSL_static_heap_io_refs(IO pool and fixed IO) andtest_wolfSSL_static_heap_realloc_refstests.curAllocreturns to its baseline (skipped withWOLFSSL_STATIC_MEMORY_LEAN).test_wolfSSL_CTX_add_session_extwithoutWOLFSSL_TICKET_HAVE_ID. A TLS 1.2 ticket session has no session ID, so it can't be added to the cache, and the remove callback isn't called on resumption.