Unsafe casting within _purchase function can result in overflow
The _purchase function in FPAM.sol casts the result of a payout calculation to uint96, which can overflow when the quote token has fewer decimals than the base token and the amount is large. An attacker could receive fewer base tokens than expected, causing a loss of funds.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-axis-finance-judging/issues/204
Found by
FindEverythingX
Summary
Unsafe casting within _purchase function can result in overflow
Vulnerability Detail
Contract: FPAM.sol
The _purchase function is invoked whenever a user wants to buy some tokens from an FPAM auction.
Note how the amount_ parameter is from type uint96:
The payout is then calculated as follows:
amount * 10^baseTokenDecimals / price
The crux: The quote token can be with 6 decimals and the base token with 18 decimals.
This would then potentially result in an overflow and the payout is falsified.
Consider the following PoC:
amount = 1_000_000_000e6 (fees can be deducted or not, this does not matter for this PoC)
baseTokenDecimals = 18
price = 1e4
This price basically means, a user will receive 1e18 BASE tokens for 1e4 (0.01) QUOTE tokens, respectively a user must provide 1e4 (0.01) QUOTE tokens to receive 1e18 BASE tokens
The calculation would be as follows:
1_000_000_000e6 * 1e18 / 1e4 = 1e29
while uint96.max = 7.922….e28
Therefore, the result will be casted to uint96 and overflow, it would effectively manipulate the auction outcome, which can result in a loss of funds for the buyer, because he will receive less BASE tokens than expected (due to the overflow).
It is clear that this calculation example can work on multiple different scenarios (even though only very limited because of the high bidding [amount] size) . However, using BASE token with 18 decimals and QUOTE token with 6 decimals will more often result in such an issue.
This issue is only rated as medium severity because the buyer can determine a minAmountOut parameter. The problem is however the auction is a fixed price auction and the buyer already knows the price and the amount he provides, which gives him exactly the fixed output amount. Therefore, there is usually absolutely no slippage necessity to be set by the buyer and lazy buyers might just set this to zero.
Impact
IMPACT:
a) Loss of funds for buyer
Code Snippet
https://github.com/sherlock-audit/2024-03-axis-finance/blob/main/moonraker/src/modules/auctions/FPAM.sol#L128 https://github.com/sherlock-audit/2024-03-axis-finance/blob/main/moonraker/src/modules/auctions/FPAM.sol#L135
Tool used
Manual Review
Recommendation
Consider simply switching to a uint256 approach, this should be adapted in the overall architecture. The only important thing (as far as I have observed) is to make sure the heap mechanism does not overflow when calculating the relative values:
Discussion
0xJem
I would rate this low priority.
It is possible, but highly unlikely as it requires all of these conditions to be met:
- The lot capacity would need to be close to the maximum (uint96 max)
- The max payout needs to be 100%
- The quote token decimals need to be low
- The price needs to be low
Oighty
I do think this is valid. I'll leave it up to the judge to determine severity. The fact that the buyer can receive much fewer tokens than expected, even in an outlandish scenario, shouldn't be possible.
sherlock-admin4
The protocol team fixed this issue in the following PRs/commits: https://github.com/Axis-Fi/moonraker/pull/130
10xhash
The protocol team fixed this issue in the following PRs/commits: Axis-Fi/moonraker#130
Fixed uint256 is now used, avoiding the unsafe casting
sherlock-admin4
The Lead Senior Watson signed off on the fix.
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
Root cause
The payout calculation result is cast to uint96 without checking for overflow, and the calculation can exceed uint96.max when token decimals differ.
Impact
The buyer could receive fewer base tokens than expected, resulting in a loss of funds; the text notes this is mitigated if the buyer sets a non-zero minAmountOut.
What the attacker needed
Large purchase amount near uint96 max; quote token with low decimals (e.g., 6); base token with high decimals (e.g., 18); low price
The fix
The issue was fixed by switching to uint256 to avoid the unsafe casting.
How it was found
- Call the _purchase function with a large amount parameter (e.g., 1_000_000_000e6).
- Use a base token with 18 decimals and a quote token with 6 decimals.
- Set a low price (e.g., 1e4) so the payout calculation yields a value above uint96.max.
- The result is cast to uint96 and overflows, reducing the payout.
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.
A smart contract calculates how many tokens a buyer should receive, but stores the result in a small number type that can only hold values up to about 7.9e28. If the calculation produces a larger number, it wraps around to a small value, so the buyer gets far fewer tokens than they paid for.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.