The mulWadUp method is not rounding up correctly the (x * y) / WAD
result.
By incrementing the value of x when the result of (z+x)/y
is equal to 1, it introduces a new calculation ((x +1) * y) / WAD
.
This results for a specific range of numbers, have a final outcome completely wrong increased by 2 instead of 1 in the best case scenario or returning a huge difference of result.
For this POC we will use a simple division with small numbers to show demonstrate the issue, at the end of the POC we will show how far off this calculation could be.
The issue arises in the mulWadUp method as shown in the provided code snippet
The flaw lies in the conditional increment of x (if iszero(sub(div(add(z, x), y), 1)) { x := add(x, 1) }
).
This adjustment is meant to round up x, which is actually not necessary at all.
We can demonstrate the vulnerability with the following variable values:
x = 576684777797582847 and y = 288511851128422423
At this step we have the following equation ((x +1) * y) / WAD
The result will be ((x + 1) * y / WAD) + 1)
instead of (x * y / WAD) + 1)
Giving us the wrong result 166380392759963588
instead of 166380392759963587
If we use bigger numbers as the following one
x = 223108379911240396122621124461961929566
y = 137428086083331686137894327482105279507
The result will be 30661357640354615014511215001663630841895493678062493593460
instead of 30661357640354615014511215001663630841758065591979161907322
Which is off by 137428086083331686138
wei
This vulnerability can lead to inaccurate calculations in transactions relying on mulWadUp.
The impact could range from minor discrepancies in values to more significant financial inaccuracies, depending on the use case of the contract.
As this method is used in the library and the probability to happen is low to medium and the impact is medium to high i ll qualify it as a medium severity.
halmos
manual review
Remove the if iszero(sub(div(add(z, x), y), 1)) { x := add(x, 1) }
method that lead to this bug and doesn't add any solution to the method:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.