Skip to content

Free only the KEX keys that were initialized - #1362

Open
ejohnstown wants to merge 1 commit into
wolfSSL:masterfrom
ejohnstown:kex-key-cleanup
Open

ejohnstown wants to merge 1 commit into
wolfSSL:masterfrom
ejohnstown:kex-key-cleanup

Conversation

@ejohnstown

Copy link
Copy Markdown
Contributor

SendKexDhReply() and the ECDH and Curve25519 KeyAgree functions free
only the keys they initialized, so a missing host key or a failed
small-stack allocation no longer frees uninitialized storage.

  • per-key init flags, including keyInit in sigKeyBlockFull
  • regress and unit tests for both paths

Issue: F-11675, F-11939

SendKexDhReply and the ECDH and Curve25519 KeyAgree functions now
track which keys were initialized and free only those. A host-key
format with no loaded key, or a failed small-stack allocation, no
longer hands never-initialized storage to the wolfCrypt free.

- sigKeyBlockFull gains keyInit, set by SendKexGetSigningKey
- per-key Inited flags in the client and server KeyAgree functions
- regress: no matching host key frees nothing (WOLF_CRYPTO_CB_FREE)
- unit: server KeyAgree survives an allocation failure (small stack)

Issue: F-11675, F-11939
Copilot AI balanced review requested due to automatic review settings November 2, 2026 18:56

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.

🟢 Approval recommended

No active review issues are recorded for the changes guarding key-exchange cleanup against freeing uninitialized keys.

No open issues 🎉

What changed in this PR

This PR hardens key-exchange cleanup so wolfSSH frees only keys that were successfully initialized, avoiding invalid cleanup when a host key is missing or a small-stack allocation fails.

Changes:

  • Tracks initialization of signing keys and temporary ECDH and Curve25519 keys before freeing them.
  • Adds regression coverage for a missing matching host key and allocation-failure tests for server key agreement.
File Description
wolfssh/​internal.h Declares the Curve25519 server key-agreement test hook.
tests/​unit.c Adds fault-injected allocation tests that detect cleanup of uninitialized keys.
tests/​regress.c Tests missing-host-key failure without freeing an uninitialized RSA key.
src/​internal.c Guards key cleanup with initialization flags and exposes the new test hook.

Review effort: Balanced

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

Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 2 of 4 in-scope changed file(s) opened by the reviewer; not opened: tests/unit.c, wolfssh/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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants