Skip to content

Fenrir 15782: Allocate session memory from the CTX heap, not the SSL heap - #11899

Open
aidankeefe2022 wants to merge 1 commit into
wolfSSL:masterfrom
aidankeefe2022:ssl-sess-fenrir
Open

aidankeefe2022 wants to merge 1 commit into
wolfSSL:masterfrom
aidankeefe2022:ssl-sess-fenrir

Conversation

@aidankeefe2022

Copy link
Copy Markdown
Member

Description

With WOLFSSL_STATIC_MEMORY, ssl->heap is a per-connection heap hint that wolfSSL_free() releases. A WOLFSSL_SESSION can live longer than its SSL (wolfSSL_get1_session(), the session cache, resumption on a new SSL). Memory tied to the session was being taken from or returned to ssl->heap, so a retained session could point at memory from a pool that was already gone.

  • InitSSL / wolfSSL_clear: create the session with ctx->heap.
  • SetTicket, DoFinished, AddPSKtoPreMasterSecret: allocate and free session tickets and ticket nonces with ssl->session->heap. One of these frees passed NULL as the heap.
  • PushPeerCertToChain: build the chain certs, including the leaf the server keeps as session->peer, from ssl->session->heap. Copy the leaf with wolfSSL_X509_d2i_ex() rather than wolfSSL_X509_dup(), because the dup inherits peerCert's heap, which is ssl->heap.

Testing

Add tests for get1_session/clear/SESSION_dup, for static and dynamic session tickets across resumption, and for the server's peer cert after wolfSSL_free(). Each test checks the session heap pointers, and where stats are tracked, that curAlloc returns to its baseline.

Copilot AI 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.

🟡 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

Comment thread tests/api.c Outdated
Comment thread tests/api.c Outdated

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

With WOLFSSL_STATIC_MEMORY, ssl->heap is a per-connection heap hint
that wolfSSL_free() releases. A WOLFSSL_SESSION can live longer than
its SSL (wolfSSL_get1_session(), the session cache, resumption on a
new SSL). Memory tied to the session was being taken from or returned
to ssl->heap, so a retained session could point at memory from a pool
that was already gone.

- InitSSL / wolfSSL_clear: create the session with ctx->heap.
- SetTicket, DoFinished, AddPSKtoPreMasterSecret: allocate and free
  session tickets and ticket nonces with ssl->session->heap. One of
  these frees passed NULL as the heap.
- PushPeerCertToChain: build the chain certs, including the leaf the
  server keeps as session->peer, from ssl->session->heap. Copy the
  leaf with wolfSSL_X509_d2i_ex() rather than wolfSSL_X509_dup(),
  because the dup inherits peerCert's heap, which is ssl->heap.

Add tests for get1_session/clear/SESSION_dup, for static and dynamic
session tickets across resumption, and for the server's peer cert
after wolfSSL_free(). Each test checks the session heap pointers, and
where stats are tracked, that curAlloc returns to its baseline.
@aidankeefe2022
aidankeefe2022 marked this pull request as ready for review October 26, 2026 20:51
@github-actions

Copy link
Copy Markdown

retest this please

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

@danielinux danielinux changed the title Fenrir 15782 (HIGH): Allocate session memory from the CTX heap, not the SSL heap Fenrir 15782: Allocate session memory from the CTX heap, not the SSL heap Oct 27, 2026

@julek-wolfssl julek-wolfssl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

pr11899-heap-hint-refcount.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants