Skip to content

F-16307: Session Cache ex_data consistent ownership rules internal cache - #12220

Open
aidankeefe2022 wants to merge 9 commits into
wolfSSL:masterfrom
aidankeefe2022:session_cache_ownership
Open

aidankeefe2022 wants to merge 9 commits into
wolfSSL:masterfrom
aidankeefe2022:session_cache_ownership

Conversation

@aidankeefe2022

@aidankeefe2022 aidankeefe2022 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Zendesk Ticket: https://wolfssl.zendesk.com/agent/tickets/22776
Includes reproduction steps ^

Description

Previously, a session handle after loading the cache could own the ex_data that was also stored in the cache. This meant that the session handle operated on its personal ex_data field, while other threads resuming a session from the cache would get the cache result.

An example: (Suggested pattern in OpenSSL docs)

lock -> get app data -> set to something -> unlock -> free app data

thread 1 would get the copy from its handle ex_data field

thread 2 would then get the same pointer from the cache, free it again, and a double free would occur

fix:
If app data is in the cache, it is only in the cache, and handles need to search the cache to get it. ie. the cache is the sole owner of the ex_data once it is loaded.

There are a widespread set of changes here to make this bookkeeping happen, but with the ex_data one-location rule, the reasoning about the cache is much clearer!

Helpful diagram I had Mr. Claude make

ex_data Leg D Before After.html

Testing

Added 4 new tests to ensure that ex_data exists in the cache and is retrieved via the cache when applicable. And that internal/external cache behave as expected

Gaps

This PR is only focused on the internal cache. There are preexisting ownership issues and bugs in the external cache paths, but these would increase the scope of this PR to be much larger than it already is. I made sure that no new regressions hit the external cache path. The reason for the internal cache fix is that they are the path reported on by the Zendesk vuln ticket. A follow-up PR is the most reasonable path forward due to inter-cache ownership rules needing to be decided on, and it is a bigger undertaking than what this is a fix for.

Checklist

  • added tests

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

A critical race can make session-duplication callbacks access freed application data, while unresolved cache ownership and cleanup defects can cause data loss, leaks, and nondeterministic test failures.

5 open issues
What changed in this PR

This PR hardens wolfSSL session-cache ownership of application ex_data to prevent stale handle copies from causing double frees.

Changes:

  • Makes cached sessions the sole owners of ex_data, with handles resolving data through the cache.
  • Updates session duplication and ticket reissue handling to retrieve or transfer cached data.
  • Adds TLS 1.3 tests for duplication callbacks and data preservation across ticket reissues.
File Description
wolfssl/​internal.h Declares the helper that clears handle-local ex_data.
tests/​api/​test_tls13.h Registers two new session-data tests.
tests/​api/​test_tls13.c Adds ownership tests and shares callback test helpers.
src/​ssl_sess.c Centralizes cache lookup and updates ownership and duplication logic.
src/​internal.c Clears local data copies when restoring cached sessions.

Review effort: Balanced

Comment thread src/ssl_sess.c Resolved
Comment thread src/ssl_sess.c Resolved
Comment thread src/ssl_sess.c Resolved
Comment thread src/ssl_sess.c Resolved
Comment thread tests/api/test_tls13.c Resolved

@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 #12220

Scan targets checked: wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_tls13.h
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer

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: Max

Comment thread tests/api/test_tls13.c Resolved

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.

Comment thread src/ssl_sess.c Resolved
Comment thread src/ssl_sess.c Resolved
Comment thread tests/api/test_session.c Resolved

@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 #12220

Scan targets checked: wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_session.h, tests/api/test_tls13.h
Coverage: 2 of 3 in-scope changed file(s) opened by the reviewer; not opened: tests/api/test_session.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

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review November 20, 2026 20:17

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

@aidankeefe2022
aidankeefe2022 requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot November 20, 2026 23:09
@aidankeefe2022

Copy link
Copy Markdown
Member Author

@claude review this

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

Active findings identify ex_data allocation and reference leaks during failed session duplication and stateful TLS 1.3 resumption from a non-owning external-cache session.

2 open issues
3 resolved since last review

Review effort: Balanced

Comment thread src/internal.c Resolved
Comment thread src/ssl_sess.c Resolved

@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 #12220

Scan targets checked: wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_session.h, tests/api/test_tls13.h
Coverage: 3 of 4 in-scope changed file(s) opened by the reviewer; not opened: tests/api/test_session.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

Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/ssl_sess.c Resolved
Comment thread src/ssl_sess.c Resolved
@aidankeefe2022 aidankeefe2022 changed the title Session Cache ex_data Ownership Hardening Session Cache ex_data consistent ownership rules Nov 20, 2026
@aidankeefe2022
aidankeefe2022 requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot November 21, 2026 00:09
@aidankeefe2022

Copy link
Copy Markdown
Member Author

@claude

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

Owning external sessions lose ex_data when internal session storage is disabled, causing subsequent session duplication to pass NULL instead of the external session’s data to duplication callbacks.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread src/ssl_sess.c Resolved

@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 #12220

Scan targets checked: wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_session.h, tests/api/test_tls13.h
Coverage: 3 of 5 in-scope changed file(s) opened by the reviewer; not opened: src/ssl.c, wolfssl/internal.h

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

Comment thread src/ssl_sess.c Resolved
@aidankeefe2022
aidankeefe2022 marked this pull request as ready for review November 23, 2026 19:04
@github-actions

Copy link
Copy Markdown

retest this please

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text -124 B (-0.2%, 72,692 B / 262,144 B, total: 28% used)

gcc-arm-cortex-m3

  • FLASH: .text +748 B (+0.6%, 135,266 B / 262,144 B, total: 52% used)

gcc-arm-cortex-m4

  • FLASH: .rodata.CSWTCH.2 +8 B, .rodata.str1.1 +99 B, .text +704 B (+0.4%, 214,296 B / 262,144 B, total: 82% used)

gcc-arm-cortex-m4-baremetal

  • FLASH: .text -128 B (-0.2%, 75,444 B / 262,144 B, total: 29% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .rodata.CSWTCH.2 +8 B, .rodata.str1.1 +99 B, .text -576 B (-0.3%, 185,019 B / 262,144 B, total: 71% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +1,024 B (+0.5%, 201,700 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-min-ecc

  • FLASH: .text -128 B (-0.2%, 69,910 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .rodata +160 B, .text +1,984 B (+0.3%, 807,720 B / 1,048,576 B, total: 77% used)
  • RAM: .data +4 B (+0.0%, 121,764 B / 262,144 B, total: 46% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .rodata.CSWTCH.2 +8 B, .rodata.str1.1 +99 B, .text -192 B (-0.0%, 227,388 B / 262,144 B, total: 87% used)

gcc-arm-cortex-m4-pq

  • FLASH: .rodata +112 B, .text +576 B (+0.2%, 320,288 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .rodata +112 B, .text +896 B (+0.3%, 349,976 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-sp-math

  • FLASH: .text -128 B (-0.2%, 69,910 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +768 B (+0.6%, 136,014 B / 262,144 B, total: 52% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .rodata.CSWTCH.2 +8 B, .rodata.str1.1 +99 B, .text +768 B (+0.3%, 256,754 B / 262,144 B, total: 98% used)

gcc-arm-cortex-m7

  • FLASH: .rodata.CSWTCH.2 +8 B, .rodata.str1.1 +99 B, .text +704 B (+0.4%, 214,296 B / 262,144 B, total: 82% used)

gcc-arm-cortex-m7-pq

  • FLASH: .rodata +112 B, .text +512 B (+0.2%, 321,120 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .rodata.CSWTCH.2 +8 B, .rodata.str1.1 +99 B, .text +704 B (+0.3%, 256,754 B / 262,144 B, total: 98% used)

stm32-sim-stm32h753

  • FLASH: .text +11,724 B (+5.6%, 221,880 B / 2,097,152 B, total: 11% used)

zephyr-frdm_rw612

  • FLASH: rodata +32 B, text +16 B (+0.0%, 297,188 B / 67,108,864 B, total: 0% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

@aidankeefe2022
aidankeefe2022 force-pushed the session_cache_ownership branch from 4c44718 to 6b0e89b Compare November 23, 2026 19:09
@aidankeefe2022
aidankeefe2022 force-pushed the session_cache_ownership branch from 135ae2e to 5565de3 Compare December 2, 2026 00:06
Previously a session handle after loading the cache could own the ex_data
what was also stored in the cache. This ment that the session handle operated
on its personal ex_data field while other threads resuming a session
from the cache would get the cache result.

An example: (Suggested pattern in OpenSSL docs)

lock -> get app data -> set to NULL -> unlock -> free app data

thread 1 would get the copy from its handle and free the app data
through a void*

thread 2 would then get the same pointer from the cache free it again
and a double free would occur

fix:
If app data is in the Cache it is only in the cache and handles need to
search the cache to get it. ie. the cache is the sole owner of the
ex_data once it is loaded.
@aidankeefe2022
aidankeefe2022 force-pushed the session_cache_ownership branch from 5565de3 to cb90c9b Compare December 2, 2026 00:06
@julek-wolfssl
julek-wolfssl self-requested a review December 2, 2026 14:08
@aidangarske aidangarske added the zd For prs that fix zendesk issues label Dec 3, 2026
@aidangarske

Copy link
Copy Markdown
Member

This also fixes F-16307. I checked it with a regression test: on master the session ex_data free callback runs twice, on this branch it runs once. Could you add F-16307 to the PR title so Fenrir links it and closes it on merge? I can send you the test if you want it in this PR.

@aidankeefe2022 aidankeefe2022 changed the title Session Cache ex_data consistent ownership rules internal cache F-16307: Session Cache ex_data consistent ownership rules internal cache Dec 3, 2026
@aidankeefe2022 aidankeefe2022 added the fenrir For fenrir fix prs label Dec 3, 2026
@aidankeefe2022

aidankeefe2022 commented Dec 3, 2026 •

Copy link
Copy Markdown
Member Author

This also fixes F-16307. I checked it with a regression test: on master the session ex_data free callback runs twice, on this branch it runs once. Could you add F-16307 to the PR title so Fenrir links it and closes it on merge? I can send you the test if you want it in this PR.

@aidangarske Thanks for finding that! I added it to the title; please send the test!

@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.

  • SendTls13NewSessionTicket: why does every ticket now get a new ID when HAVE_EX_DATA is defined? That covers most compat builds, even ones that never use session ex_data, and costs a cache entry per ticket. The ex_data also follows only the newest ticket, so resuming from an older one finds none. Why isn't re-adding under the same ID enough?
  • SessionExDataCopy with shareRest = 1: a slot with a free callback but no dup callback is shared, and both sessions own it, so both run the free callback. Shouldn't the copy skip those slots too?

Comment thread wolfssl/internal.h
Comment on lines 5597 to +5599
WOLFSSL_LOCAL int wolfSSL_DupSession(const WOLFSSL_SESSION* input,
WOLFSSL_SESSION* output, int avoidSysCalls);
WOLFSSL_SESSION* output, int avoidSysCalls, int transferExData,
byte* ticketNonceBuf, byte* ticketNonceLen, byte* preallocUsed);

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.

If you are modifying wolfSSL_DupSession anyway then I don't see the point of using it and then immediately clearing the ex_data. Add a parameter to do it in wolfSSL_DupSession.

@aidankeefe2022 aidankeefe2022 Dec 4, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added a behavior flag arg that determines the clearing behavior in place of transferExData. No more clear logic after the function

reverted the session ticket copy.
now all plain session tickets get the ex_data and is safe!

added a behvior mode flag arg to the DupSession function
Puts the clearing logic into the dup function
@aidankeefe2022

Copy link
Copy Markdown
Member Author

SessionExDataCopy with shareRest = 1: a slot with a free callback but no dup callback is shared, and both sessions own it, so both run the free callback. Shouldn't the copy skip those slots too?

The share rest scenario is matching OpenSSL and this case only runs when the ex_data is not referenced in our internal cache. We keep this to make sure people don't lose data, and we trust them to manage the lifetimes. This can be revisited in the future, but as far as I can tell, it would need a full redesign of the session cache system, i.e., the cache holds references to sessions, not the sessions themselves or something similar. This also impacts the external cache, which is breaking for anyone that has an external cache as of now.

SendTls13NewSessionTicket: why does every ticket now get a new ID when HAVE_EX_DATA is defined? That covers most compat builds, even ones that never use session ex_data, and costs a cache entry per ticket. The ex_data also follows only the newest ticket, so resuming from an older one finds none. Why isn't re-adding under the same ID enough?

After looking again, you are right: we can just reuse the ID. There was a test that was misleading by applying a rule for 0-RTT to all tickets on master. This rule is not needed anymore for plain tickets with the new ownership rules... I knew I had gotten that wrong; thank you for the extra attention there!

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 test can double-free a session when an assertion skips the second assignment to dup, causing a crash instead of reporting the regression; reset dup to NULL after the first free.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread tests/api/test_tls13.c Resolved

@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 #12220

Scan targets checked: wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_session.h, tests/api/test_tls13.h
Coverage: 3 of 6 in-scope changed file(s) opened by the reviewer; not opened: src/tls13.c, tests/api/test_session.c, wolfssl/internal.h

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

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

Labels

fenrir For fenrir fix prs zd For prs that fix zendesk issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants