Skip to content

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

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

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?

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

2 participants