Skip to content

Fix memory leak in Dilithium signing when WC_DILITHIUM_CACHE_MATRIX_A is enabled#10384

Closed
zulfff wants to merge 1 commit intowolfSSL:masterfrom
zulfff:fix/dilithium-memory-leak
Closed

Fix memory leak in Dilithium signing when WC_DILITHIUM_CACHE_MATRIX_A is enabled#10384
zulfff wants to merge 1 commit intowolfSSL:masterfrom
zulfff:fix/dilithium-memory-leak

Conversation

@zulfff
Copy link
Copy Markdown

@zulfff zulfff commented May 2, 2026

fix #10383

Description

Fixes memory leak in Dilithium (ML-DSA) signing when WC_DILITHIUM_CACHE_MATRIX_A compile option is enabled and WC_DILITHIUM_FIXED_ARRAY is disabled.

In the signing function around line 8321 of wolfcrypt/src/dilithium.c, memory was allocated into the local variable a instead of storing it in key->a. Then on line 8328, a gets overwritten with key->a (which is still NULL at this point), causing the allocated memory to be lost and never freed.

Changed line 8321 from:

a = (sword32*)XMALLOC(params->aSz, key->heap, DYNAMIC_TYPE_DILITHIUM);

To:

key->a = (sword32*)XMALLOC(params->aSz, key->heap, DYNAMIC_TYPE_DILITHIUM);

This matches the correct pattern used elsewhere in the same file (e.g., line 7765-7766 in dilithium_make_key).

Testing

  • Built wolfSSL with --enable-mldsa --enable-debug
  • Ran ML-DSA unit tests: ./tests/unit.test --group mldsa
  • All tests passed (16 passed, 5 skipped, 0 failed)
  • No memory leaks detected during signing operations

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

… is enabled

Changed line 8321 from allocating to local variable 'a' to 'key->a'
to prevent memory leak when the matrix A is dynamically allocated.
@wolfSSL-Bot
Copy link
Copy Markdown

Can one of the admins verify this patch?

@embhorn embhorn self-assigned this May 4, 2026
@embhorn
Copy link
Copy Markdown
Member

embhorn commented May 4, 2026

Hello @zulfff

Thanks for sharing this PR. We do not have you on file as an approved contributor. We also do not typically accept small fixes from new contributors. If will be opening more PR's and you'd like to become a contributor, please email support@wolfssl.com to request a CLA form.

If you'd rather not, we can close this PR and review the issue you opened.

Thanks,
@embhorn -wolfSSL Support

@embhorn
Copy link
Copy Markdown
Member

embhorn commented May 5, 2026

Closing in favor of #10400

@embhorn embhorn closed this May 5, 2026
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.

[Bug]: Memory leak in Dilithium signing when WC_DILITHIUM_CACHE_MATRIX_A is enabled

3 participants