Overflow in CollateralTracker allows minting shares for free
The mint function in CollateralTracker calculates required assets in an unchecked block, so a very large shares value overflows the multiplication and yields a tiny asset cost. An attacker can mint a huge number of shares for almost nothing and then withdraw all collateral.
Submitted by 0xLogos
Impact
Malicious actors can mint huge amounts of shares for free and then withdraw all collateral.
Proof of Concept
In the mint function user-controlled shares parameter goes right away to the previewMint function which then calculates required assets in unchecked block. If the shares value is high enough, overflow in shares * DECIMALS will occur, and assets will be very low.
function previewMint(uint shares) public view returns (uint assets) {
unchecked {
assets = Math.mulDivRoundingUp(
shares * DECIMALS, totalAssets(), totalSupply * (DECIMALS - COMMISSION_FEE)
);
}
}
function mint(uint shares, address receiver) external returns (uint assets) {
assets = previewMint(shares);
if (assets > type(uint104).max) revert Errors.DepositTooLarge();
...
}
Insert the following snippet to ColalteralTracker.t.sol for coded PoC:
function test_poc1(uint256 x) public {
_initWorld(x);
_grantTokens(Bob);
vm.startPrank(Bob);
uint shares = type(uint).max / 10000 + 1;
IERC20Partial(token0).approve(address(collateralToken0), type(uint256).max);
uint256 returnedAssets0 = collateralToken0.mint(shares, Bob);
assertEq(shares, collateralToken0.balanceOf(Bob));
assertEq(returnedAssets0, 1);
}
Recommended Mitigation Steps
Remove unchecked block.
function maxMint(address) external view returns (uint maxShares) {
return (convertToShares(type(uint104).max) * DECIMALS) / (DECIMALS + COMMISSION_FEE);
}
Assessed type
Under/Overflow
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 multiplication in previewMint is performed inside an unchecked block, so an oversized shares value overflows and produces a very small asset requirement.
Impact
An attacker could mint huge amounts of shares for free and then withdraw all collateral.
What the attacker needed
Ability to call mint on the contract
The fix
The unchecked block should be removed so the multiplication is checked for overflow.
How it was found
- Set up the test world and grant tokens to Bob.
- Bob approves the collateral token contract to spend his tokens.
- Bob calls mint with a shares value just above the maximum uint divided by 10000.
- The call succeeds, Bob's share balance equals the requested shares, and only one asset unit is returned.
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 lets people deposit collateral and receive shares in return. The code that works out how much collateral a given number of shares costs did the maths without checking for overflow, so a huge number wrapped around to a tiny value. That meant someone could get a massive amount of shares for almost no collateral and then take everyone else's funds.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.