-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Optional Rigorous Range Check to Prevent Potential Overflow Vulnerability in LessThan(8) Usage #83
base: main
Are you sure you want to change the base?
Conversation
Thanks again for your support! I think a quick fix could be adding an optional argument to the However, I worry that users might forget to enable or disable this option, leading to unintended behavior. Defining the Let me know what you think! |
Hi @Divide-By-0 @SoraSuegami, this PR is now ready for review!! |
Hi!
Thank you for this fantastic project! While exploring the code, I noticed that
LessThan(8)
is used in several templates to validate the range of the input msg. However, this function does not check the bit-length of msg, which can result in potential security vulnerabilities.Issue Overview:
The
LessThan(N)
function in Circomlib has a known overflow vulnerability. When handling values exceedingN
bits, it can produce unexpected results. For instance, in this project, the intended behavior seems to be ensuringLessThan(8)
outputsout = 0
if msg[i] exceeds 255.However, for an input like:
msg[i] = 21888242871839275222246405745257275088548364400416034343698204186575808495616
(which I discovered using a fuzzing tool), the
LessThan(8)
function incorrectly outputsout = 1
, allowing a valid proof to be generated.example
zk-regex/packages/circom/circuits/common/body_hash_regex.circom
Line 15 in b7bb363
I confirmed that:
{"msg": ["256"]}
.{"msg": ["21888242871839275222246405745257275088548364400416034343698204186575808495616"]}
, despite this input clearly exceeding the intended range.Affected Templates
The issue also impacts the following templates:
BodyHashRegex
EmailAddrRegex
EmailAddrWithNameRegex
EmailDomainRegex
FromAllRegex
MessageIdRegex
ReversedBracketRegex
SubjectAllRegex
TimestampRegex
ToAddrRegex
ToAllRegex
Users of these templates should exercise caution. Although using these templates to the standard utf-8 body is safe, replace
LessThan
withSemiSafeLessThan
If stricter input validation is needed.Changes in This PR:
To address this issue, I have introduced a new option
-i --is_safe <true/false>
option to thezk-regex
CLI. When set totrue
, the generated Circom template usesSemiSafeLessThan
for range check, which callsNum2Bits
and validates the bit-length of the input. This ensures that inputs are constrained correctly and prevents unintended overflow behavior.Example
Additional Context:
For further details on this type of vulnerability, see:
https://github.com/BlakeMScurr/comparator-overflow
Acknolwdgement
Special thanks to @Divide-By-0 and @SoraSuegami for confirming this potential vulnerability!