fix: signed-shift UB in SLH-DSA base_2b digest accumulator#10917
Open
MarkAtwood wants to merge 1 commit into
Open
fix: signed-shift UB in SLH-DSA base_2b digest accumulator#10917MarkAtwood wants to merge 1 commit into
MarkAtwood wants to merge 1 commit into
Conversation
This was referenced Jul 15, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes undefined behavior in the SLH-DSA base_2b() digest-to-indices conversion by ensuring the digest accumulator is unsigned, eliminating signed left-shift overflow on common inputs while preserving output correctness.
Changes:
- Change the
totalaccumulator inslhdsakey_base_2b()frominttoword32to avoid signed-shift UB during byte accumulation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The FORS digest accumulator 'total' was a signed int and is never masked, so 'total << 8' overflows INT32_MAX from the 4th consumed byte onward (C11 6.5.7p4 undefined behavior). Declare it word32. Output is unchanged: overflowed high bits were already discarded by the mask. Reproduced with -fsanitize=shift during the standard SLH-DSA KAT test; clean after this change.
MarkAtwood
force-pushed
the
fix/slhdsa-base2b-shift-ub
branch
from
July 15, 2026 19:42
91d1db9 to
c428218
Compare
Contributor
|
Jenkins retest this please |
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.
slhdsakey_base_2b()accumulates the FORS message digest into a signedintthat is never masked down; from the fourth consumed byte onwardtotal << 8exceedsINT32_MAX, which is undefined behavior (CWE-190, C11 6.5.7p4). It sits on the sign and verify path of every SLH-DSA parameter set.Fix: declare
totalasword32. Output is identical: the overflowed high bits were always discarded by the mask. SLH-DSA KATs pass unchanged.Verified with
-fsanitize=shift(with-fwrapvremoved from CFLAGS): pre-fix,wc_slhdsa.c:1717: left shift of 609481888 by 8 places cannot be represented in type 'int'fires during the standard KAT test; post-fix the run is clean.Exposure note: autotools builds get
-fwrapvfrom the hardening macros, which gives this defined wrapping semantics, but embedded users compiling wolfCrypt sources with their own flags have no such guarantee.-fwrapvalso suppresses the sanitizer's shift-base check, which is why nightly sanitizer runs never caught it.Credit: found by Dominik Blain of Qreative Lab (qreativelab.io) using their Z3-based formal analysis engine, reported with a machine-checked witness. Same bug class as the earlier fix in this file from PR #10104, which addressed a different site.
Related: #10918 (ML-DSA gamma1_19 encoder), #10919 (ML-DSA slow-multiply reductions) — same bug class, found in the same report.