fix: keep wc_RsaSSL_Sign()'s result signed in SignHashRsa - #1131
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a signed/unsigned conversion bug in the agent RSA signing path that could convert negative wolfCrypt errors into a huge signature length, leading to a stack over-read during RSA verification when using oversized RSA keys (under WOLFSSH_AGENT). Adds an API test that exercises the hostile-agent message injection scenario and is intended to be run under sanitizers.
Changes:
- Fix
SignHashRsa()to keepwc_RsaSSL_Sign()’s return value signed and only assign*sigSzon success. - Expand the agent test harness response buffer and add a sanitizer-focused regression test for an oversized RSA modulus injection path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/agent.c |
Prevents negative RSA sign errors from being stored into an unsigned size and erroneously treated as success. |
tests/api.c |
Adds a regression test (and increases harness response capacity) to detect the pre-fix stack over-read under sanitizers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1131
Scan targets checked: wolfssh-bugs, wolfssh-src
No new issues found in the changed files. ✅
ejohnstown
approved these changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
SignHashRsa()storedwc_RsaSSL_Sign()'sintreturn directly intoword32* sigSz, then testedif (*sigSz <= 0). Against an unsigned destination that test is equivalent to== 0, so every negative wolfCrypt error became a ~4 GiB length and fell through to the success branch.An agent identity with an RSA key larger than 2048 bits makes
wc_RsaSSL_Sign()returnRSA_BUFFER_E, becausePostSignRequest()passes a fixed 256-byte output buffer.wolfSSH_RsaVerify()then clamps the bogus length to the key size and copies that many bytes out of the 256-byte stack buffer — a 128-byte stack over-read at 3072 bits, 256 at 4096. The verify fails afterwards, so nothing reaches the wire; the defect is memory safety only.Reachable through
wolfSSH_AGENT_SignRequest(), which feeds whatever the agent I/O callback returns intoDoMessage()— so a hostile or compromised agent socket can injectADD_IDENTITY+SIGN_REQUEST.DoAddIdentity()applies no key-size limit. Gated onWOLFSSH_AGENT. Closes f-7203.Fix (
src/agent.c)Keep the result signed and assign the length only on success, matching
SignHRsa()ininternal.c— the sibling call sites already did this correctly, and this was the only unsigned destination in the tree.Oversized keys now fail cleanly with
WS_RSA_E.Test harness (
tests/api.c)test_wolfSSH_agent_signrequest_oversize_rsa_key()reuses the existingWOLFSSH_AGENTharness, injecting two agent responses: anADD_IDENTITYcarrying a 3072-bit modulus, then aSIGN_REQUESTfor it.AgentTestCtx.responsegrew 128 → 512 to carry them. Guarded by#ifndef WOLFSSH_NO_RSA_SHA2_256, matching the feature gates on the code it drives.The fix changes no return code —
WS_RSA_Ebefore and after — so this test only distinguishes fixed from unfixed under a sanitizer.Verification
make check: 10 passed, 1 skipped, 0 failed.stack-buffer-overflow, READ of size 384atinternal.c:13902viawolfSSH_AGENT_SignRequest→wolfSSH_RsaVerify.-DWOLFSSH_NO_RSA_SHA2_256and-DWOLFSSH_NO_RSA; neutralizing the guard aborts in both, confirming it is load-bearing.-Werrorsweep: 6/6 configs clean.