Missing scaling factor in recordKeyPurchase ?
The recordKeyPurchase function computes maxTokens using integer division that likely truncates a term to zero, so the intended asymptotic curve is not applied. An attacker could receive more token rewards than intended.
Submitted by cmichel
The Unlock.recordKeyPurchase function computes the maxTokens as:
maxTokens = IMintableERC20(udt).balanceOf(address(this)) * valueInETH / (2 + 2 * valueInETH / grossNetworkProduct) / grossNetworkProduct;
Note that grossNetworkProduct was already increased by valueInETH in the code before. Meaning, the (2 + 2 * valueInETH / grossNetworkProduct) part of the computation will almost always be 2 as usually grossNetworkProduct > 2 * valueInETH, and thus the 2 * valueInETH / grossNetworkProduct is zero by integer division.
Impact
The maxTokens curve might not be computed as intended and lead to being able to receive more token rewards than intended.
Recommended Mitigation Steps
The comment “we distribute tokens using asymptotic curve between 0 and 0.5” should be more clear to indicate how exactly the curve looks like. It could be that a floating-point number was desired instead of the integer division in 2 * valueInETH / grossNetworkProduct. In that case, consider adding a scaling factor to this term and divide by it at the end of the computation again.
julien51 (Unlock Protocol) commented:
I am not fully sure I understand what the problem is here?
0xleastwood (judge) commented:
I think the warden is raising an issue where
2 * valueInEth / grossNetworkProductwill more than likely truncate and return0. I think this is a valid finding.
julien51 (Unlock Protocol) commented:
Hum, we did some tests and could not reproduce here.
0xleastwood (judge) commented:
I’m not sure how
2 * valueInETH / grossNetworkProductdoes not always lead to some truncation.grossNetworkProductis equal tovalueInETHin the first call but always greater thanvalueInETHin any subsequent calls.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Impact
The maxTokens curve might not be computed as intended and could allow receiving more token rewards than intended.
What the attacker needed
None
The fix
Consider adding a scaling factor to the term and dividing by it at the end of the computation.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
In smart contracts, numbers are often integers, so division drops remainders. Here, a formula meant to create a curve ends up ignoring a small term, making the reward calculation wrong. This could let someone get more tokens than they should.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.