Skip to content

ML-DSA: return BAD_FUNC_ARG for a NULL len in the key length getters - #11642

Draft
Arpan0995 wants to merge 1 commit into
wolfSSL:masterfrom
Arpan0995:mldsa-getlen-null-len
Draft

Arpan0995 wants to merge 1 commit into
wolfSSL:masterfrom
Arpan0995:mldsa-getlen-null-len

Conversation

@Arpan0995

Copy link
Copy Markdown

Description

wc_MlDsaKey_GetPrivLen(), wc_MlDsaKey_GetPubLen() and wc_MlDsaKey_GetSigLen() write through len without checking it, so a NULL len is dereferenced. Their doxygen says they return BAD_FUNC_ARG when key or len is NULL, and the LMS and XMSS length getters already check len.

This adds the check to the three ML-DSA getters and updates their source comments. Nothing changes when len is a valid pointer, including the current behavior of storing the error code in *len when no parameter set is selected.

Testing

./configure --enable-mldsa --disable-shared \
    CFLAGS="-O1 -g -fsanitize=address,undefined -fno-sanitize-recover=undefined" \
    LDFLAGS="-fsanitize=address,undefined"
make
./wolfcrypt/test/testwolfcrypt
./tests/unit.test --api --group mldsa

test_wc_MldsaDecisionCoverage() in tests/api/test_mldsa.c now calls each getter with a valid key and a NULL len, next to the existing NULL key cases. Without the wc_mldsa.c change, UBSan stops the test with a store to a null pointer in wc_MlDsaKey_GetPubLen(). With it, testwolfcrypt and the mldsa API group pass in this build and with --enable-mldsa=verify-only and the same flags. Tested on macOS (arm64) with Apple clang.

Checklist

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

wc_MlDsaKey_GetPrivLen(), wc_MlDsaKey_GetPubLen() and
wc_MlDsaKey_GetSigLen() wrote through len without checking it. Return
BAD_FUNC_ARG for a NULL len, as documented and as the LMS and XMSS
getters do, and add NULL len cases to test_wc_MldsaDecisionCoverage().
Copilot AI balanced review requested due to automatic review settings October 3, 2026 14:21
@wolfSSL-Bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

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.

Copilot review overview

🟢 Approval recommended

The implementation safely addresses the NULL dereference and includes focused regression tests.

Review effort: Balanced
Findings: None

What changed in this PR

Adds NULL-output-pointer validation to ML-DSA length getters, preventing invalid memory writes while honoring the documented API contract.

Changes:

  • Return BAD_FUNC_ARG when len is NULL.
  • Add regression coverage for all three getters.
File Description
wolfcrypt/​src/​wc_mldsa.c Validates len before writing and updates comments.
tests/​api/​test_mldsa.c Tests NULL len handling.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

3 participants