The createMarket transaction lack of expiration timestamp check
The createMarket function calculates market length and maxPayout using block.timestamp without a deadline check, so a transaction delayed in the mempool can create a market with a much shorter duration than intended. This leads to an unexpectedly large maxPayout, allowing a malicious user to purchase bonds and receive an excessive amount of payout tokens before the market is closed.
Source: https://github.com/sherlock-audit/2023-02-bond-judging/issues/60
Found by
whitehat
Summary
The createMarket transaction lack of expiration timestamp check
Vulnerability Detail
Let us look into the heavily forked Uniswap V2 contract addLiquidity function implementation
// **** ADD LIQUIDITY ****
function _addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin
) internal virtual returns (uint amountA, uint amountB) {
// create the pair if it doesn't exist yet
if (IUniswapV2Factory(factory).getPair(tokenA, tokenB) == address(0)) {
IUniswapV2Factory(factory).createPair(tokenA, tokenB);
}
(uint reserveA, uint reserveB) = UniswapV2Library.getReserves(factory, tokenA, tokenB);
if (reserveA == 0 && reserveB == 0) {
(amountA, amountB) = (amountADesired, amountBDesired);
} else {
uint amountBOptimal = UniswapV2Library.quote(amountADesired, reserveA, reserveB);
if (amountBOptimal <= amountBDesired) {
require(amountBOptimal >= amountBMin, 'UniswapV2Router: INSUFFICIENT_B_AMOUNT');
(amountA, amountB) = (amountADesired, amountBOptimal);
} else {
uint amountAOptimal = UniswapV2Library.quote(amountBDesired, reserveB, reserveA);
assert(amountAOptimal <= amountADesired);
require(amountAOptimal >= amountAMin, 'UniswapV2Router: INSUFFICIENT_A_AMOUNT');
(amountA, amountB) = (amountAOptimal, amountBDesired);
}
}
}
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
(amountA, amountB) = _addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin);
address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA);
TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB);
liquidity = IUniswapV2Pair(pair).mint(to);
}
the implementation has two point that worth noting,
the first point is the deadline check
modifier ensure(uint deadline) {
require(deadline >= block.timestamp, 'UniswapV2Router: EXPIRED');
_;
}
The transaction can be pending in mempool for a long time and can be executed in a long time after the user submit the transaction.
Problem is createMarket, which calculates the length and maxPayout by block.timestamp inside it.
// Calculate market length and check time bounds
uint48 length = uint48(params_.conclusion - block.timestamp); \
if (
length < minMarketDuration ||
params_.depositInterval < minDepositInterval ||
params_.depositInterval > length
) revert Auctioneer_InvalidParams();
// Calculate the maximum payout amount for this market, determined by deposit interval
uint256 capacity = params_.capacityInQuote
? params_.capacity.mulDiv(scale, price)
: params_.capacity;
market.maxPayout = capacity.mulDiv(uint256(params_.depositInterval), uint256(length));
After the market is created at wrong time, user can call purchase. At purchaseBond(),
// Payout for the deposit = amount / price
//
// where:
// payout = payout tokens out
// amount = quote tokens in
// price = quote tokens : payout token (i.e. 200 QUOTE : BASE), adjusted for scaling
payout = amount_.mulDiv(term.scale, price);
// Payout must be greater than user inputted minimum
if (payout < minAmountOut_) revert Auctioneer_AmountLessThanMinimum();
// Markets have a max payout amount, capping size because deposits
// do not experience slippage. max payout is recalculated upon tuning
if (payout > market.maxPayout) revert Auctioneer_MaxPayoutExceeded();
payout value is calculated by term.scale which the market owner has set assuming the market would be created at desired timestamp. Even, maxPayout is far bigger than expected, as it is calculated by very small length.
Impact
Even though the market owner close the market at any time, malicious user can attack the market before close and steal unexpectedly large amount of payout Tokens.
Code Snippet
Tool used
Manual Review
Recommendation
Use deadline, like uniswap
Discussion
Oighty
Agree with this finding. We have noticed some issues with shorter than expected durations for existing markets.
Our proposed fix is to have users specify a start timestamp and a duration, which will be used to calculate/set the conclusion. If block.timestamp is greater than the start, then the txn will revert. Therefore, users must create the market before the target start time. We may allow this to be bypassed by providing a start time of zero, which would then start the market at the block.timestamp for the provided duration.
hrishibhat
Given the pre-condition that the transaction needs to be in the mempool for a long time for it to have a significant impact, considering this issue as valid medium
Oighty
https://github.com/Bond-Protocol/bonds/pull/54
xiaoming9090
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
A malicious user could purchase bonds and receive an unexpectedly large amount of payout tokens before the market owner closes the market, though this requires the transaction to remain pending in the mempool for a long time.
What the attacker needed
Transaction delayed in mempool; market owner has not closed market
The fix
The fix was to have users specify a start timestamp and duration, reverting if block.timestamp is greater than start, as implemented in the referenced pull request.
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.
When you send a transaction on a blockchain, it can sit in a waiting area called the mempool before being processed. If the transaction doesn't include a deadline, it might be processed much later than you expect. Here, the market creation code used the current time to set how long the market lasts and the maximum payout, so a delayed transaction created a market with a very short duration and a huge payout limit, letting someone buy bonds cheaply and get too many tokens.
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.