Period Size not updated on creating new Pair
The factory's setPeriodSize function updates the period size only for existing pairs, not for pairs created afterwards. A newly created pair therefore keeps the default period size instead of the reduced value set by the admin.
Originally disclosed on Code4rena
Submitted by csanuragjain
The period size is not updated to current while creating a new pair. This means even if period size has been reduced from default value, this new pair will still point to the higher default value.
Proof of Concept
- Assume Pair P1,P2 exists in BaseV1Factory with default period size as 1800
- Admin decides to decrease the period size to 900 using setPeriodSize function
function setPeriodSize(uint newPeriod) external {
require(msg.sender == admin);
require(newPeriod <= MaxPeriod);
for (uint i; i < allPairs.length; ) {
BaseV1Pair(allPairs[i]).setPeriodSize(newPeriod);
unchecked {++i;}
}
}
- This changes period size of P1, P2 to 900
- Admin creates a new Pair P3 using createPair function
function createPair(address tokenA, address tokenB, bool stable) external returns (address pair) {
require(tokenA != tokenB, "IA"); // BaseV1: IDENTICAL_ADDRESSES
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), "ZA"); // BaseV1: ZERO_ADDRESS
require(getPair[token0][token1][stable] == address(0), "PE"); // BaseV1: PAIR_EXISTS - single check is sufficient
bytes32 salt = keccak256(abi.encodePacked(token0, token1, stable)); // notice salt includes stable as well, 3 parameters
(_temp0, _temp1, _temp) = (token0, token1, stable);
pair = address(new BaseV1Pair{salt:salt}());
getPair[token0][token1][stable] = pair;
getPair[token1][token0][stable] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
isPair[pair] = true;
emit PairCreated(token0, token1, stable, pair, allPairs.length);
}
- A new Pair is created but the period size is not updated which means P3’s period size will be 1800 instead of 900 which is incorrect
Recommended Mitigation Steps
Add a new variable which stores the updated period size. Once a pair is created, update its period size using this new variable:
uint periodSizeUpdated=1800;
function setPeriodSize(uint newPeriod) external {
...
periodSizeUpdated=newPeriod;
}
function createPair(address tokenA, address tokenB, bool stable) external returns (address pair) {
...
BaseV1Pair(pair).setPeriodSize(newPeriod);
}
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 factory stores the period size only inside each pair and never keeps a factory-level current value, so createPair has nothing to apply to new pairs.
Impact
New pairs could use a stale, higher period size than the admin intended, which may affect how those pairs behave.
What the attacker needed
Admin role on the factory; admin has changed the period size
The fix
The factory should store the current period size and apply it to each pair when createPair runs; the report notes the team confirmed the issue.
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 factory contract creates trading pairs and lets the admin change a setting called period size. The admin changed the setting for pairs that already existed, but the code that creates new pairs did not apply the new setting. So any pair made later used the old default value.
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.