Fraxlend pair deployment can be front-run by a custom pair deployment
The FraxlendPairDeployer contract uses a salt derived from a seed and config data to prevent duplicate pair deployments, but a whitelisted user can front-run a standard deployment by calling deployCustom with a name that produces the same salt, causing the honest deployment to revert. The attacker can also use the same name to trigger a revert in _deploySecond, blocking the deployment.
Originally disclosed on Code4rena
Submitted by berndartmueller, also found by IllIllI
Impact
The FraxlendPairDeployer contract used to deploy Fraxlend pairs prevents deploying pairs with the same salt and _configData. This makes it vulnerable to front-running pair deployments and prevents deploying honest Fraxlend pairs.
Proof of Concept
FraxlendPairDeployer._deployFirst
function _deployFirst(
bytes32 _saltSeed,
bytes memory _configData,
bytes memory _immutables,
uint256 _maxLTV,
uint256 _liquidationFee,
uint256 _maturityDate,
uint256 _penaltyRate,
bool _isBorrowerWhitelistActive,
bool _isLenderWhitelistActive
) private returns (address _pairAddress) {
{
// _saltSeed is the same for all public pairs so duplicates cannot be created
bytes32 salt = keccak256(abi.encodePacked(_saltSeed, _configData));
require(deployedPairsBySalt[salt] == address(0), "FraxlendPairDeployer: Pair already deployed"); // @audit-info Reverts if a pair with the same salt is already deployed
bytes memory _creationCode = BytesLib.concat(
SSTORE2.read(contractAddress1),
SSTORE2.read(contractAddress2)
);
bytes memory bytecode = abi.encodePacked(
_creationCode,
abi.encode(
_configData,
_immutables,
_maxLTV,
_liquidationFee,
_maturityDate,
_penaltyRate,
_isBorrowerWhitelistActive,
_isLenderWhitelistActive
)
);
assembly {
_pairAddress := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
require(_pairAddress != address(0), "FraxlendPairDeployer: create2 failed");
deployedPairsBySalt[salt] = _pairAddress;
}
return _pairAddress;
}
A deployed Fraxlend pair address is stored in the deployedPairsBySalt mapping using a salt as the key. This salt is generated based on a seed _saltSeed and _configData. Deploying a standard Fraxlend pair with FraxlendPairDeployer.deploy uses _saltSeed = keccak256(abi.encodePacked("public")) and user-defined _configData. The salt is the same for all standard deployments.
function deploy(bytes memory _configData) external returns (address _pairAddress) {
(address _asset, address _collateral, , , , address _rateContract, ) = abi.decode(
_configData,
(address, address, address, address, uint256, address, bytes)
);
string memory _name = string(
abi.encodePacked(
"FraxlendV1 - ",
IERC20(_collateral).safeName(),
"/",
IERC20(_asset).safeName(),
" - ",
IRateCalculator(_rateContract).name(),
" - ",
(deployedPairsArray.length + 1).toString()
)
);
_pairAddress = _deployFirst(
keccak256(abi.encodePacked("public")),
_configData,
abi.encode(CIRCUIT_BREAKER_ADDRESS, COMPTROLLER_ADDRESS, TIME_LOCK_ADDRESS, FRAXLEND_WHITELIST_ADDRESS),
DEFAULT_MAX_LTV,
DEFAULT_LIQ_FEE,
0,
0,
false,
false
);
_deploySecond(_name, _pairAddress, _configData, new address[](0), new address[](0));
_logDeploy(_name, _pairAddress, _configData, DEFAULT_MAX_LTV, DEFAULT_LIQ_FEE, 0);
}
A whitelisted (and potentially dishonest) user could watch the mempool for new Fraxlend pair deployments and front-run deployments with a custom pair deployment by calling FraxlendPairDeployer.deployCustom.
FraxlendPairDeployer.deployCustom
function deployCustom(
string memory _name,
bytes memory _configData,
uint256 _maxLTV,
uint256 _liquidationFee,
uint256 _maturityDate,
uint256 _penaltyRate,
address[] memory _approvedBorrowers,
address[] memory _approvedLenders
) external returns (address _pairAddress) {
require(_maxLTV <= GLOBAL_MAX_LTV, "FraxlendPairDeployer: _maxLTV is too large");
require(
IFraxlendWhitelist(FRAXLEND_WHITELIST_ADDRESS).fraxlendDeployerWhitelist(msg.sender),
"FraxlendPairDeployer: Only whitelisted addresses"
);
_pairAddress = _deployFirst(
keccak256(abi.encodePacked(_name)),
_configData,
abi.encode(CIRCUIT_BREAKER_ADDRESS, COMPTROLLER_ADDRESS, TIME_LOCK_ADDRESS, FRAXLEND_WHITELIST_ADDRESS),
_maxLTV,
_liquidationFee,
_maturityDate,
_penaltyRate,
_approvedBorrowers.length > 0,
_approvedLenders.length > 0
);
_deploySecond(_name, _pairAddress, _configData, _approvedBorrowers, _approvedLenders);
deployedPairCustomStatusByAddress[_pairAddress] = true;
_logDeploy(_name, _pairAddress, _configData, _maxLTV, _liquidationFee, _maturityDate);
}
Internally the FraxlendPairDeployer.deployCustom uses the same _deployFirst function as a standard pair deployment uses. However, the salt is user-definable by using any _name, for instance public. Then the salt is the same as _saltSeed = keccak256(abi.encodePacked("public")). The user then uses the same _configData as the to be deployed pair. But, contrary to a standard pair deployment, a whitelisted user is also able to provide additional configuration, _maxLTV, _liquidationFee and so on.
The whitelisted user is able to deploy a custom Fraxlend pair with the same salt and the same _configData but by using for instance a _maturityDate < block.timestamp, this deployed custom pool is unusable (due to the modifier isNotPastMaturity reverting. This modifier is used by FraxlendPairCore.deposit, FraxlendPairCore.mint, FraxlendPairCore.borrowAsset, FraxlendPairCore.addCollateral and FraxlendPairCore.leveragedPosition).
As FraxlendPairDeployer._deployFirst checks if a pair is already deployed with a given salt, the honest pair deployment that got front-run reverts. It is now not possible to deploy a pair with the same configuration.
NOTE: The same can be achieved by front-running a pair deployment transaction with a custom pair deployment and with the same name (FraxlendPairDeployer.deployCustom allows providing an arbitrary name . FraxlendPairDeployer._deploySecond checks the name of the pair and reverts if a pair with that name already exists:
FraxlendPairDeployer._deploySecond
function _deploySecond(
string memory _name,
address _pairAddress,
bytes memory _configData,
address[] memory _approvedBorrowers,
address[] memory _approvedLenders
) private {
(, , , , , , bytes memory _rateInitData) = abi.decode(
_configData,
(address, address, address, address, uint256, address, bytes)
);
require(deployedPairsByName[_name] == address(0), "FraxlendPairDeployer: Pair name must be unique"); // @audit-info reverts if a pair with the same name exists already
deployedPairsByName[_name] = _pairAddress;
deployedPairsArray.push(_name);
// Set additional values for FraxlendPair
IFraxlendPair _fraxlendPair = IFraxlendPair(_pairAddress);
_fraxlendPair.initialize(_name, _approvedBorrowers, _approvedLenders, _rateInitData);
// Transfer Ownership of FraxlendPair
_fraxlendPair.transferOwnership(COMPTROLLER_ADDRESS);
}
Recommended Mitigation Steps
Consider validating the _name parameter in the FraxlendPairDeployer.deployCustom function to not equal the string public, thus preventing the usage of the same salt as a standard pair deployment.
Additionally, consider prefixing the name of a custom pair deployment to also prevent front-running this way.
DrakeEvans (Frax) acknowledged
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 salt for custom deployments is derived from a user-supplied name, allowing a whitelisted user to choose a name that produces the same salt as a standard deployment, and the name uniqueness check can also be exploited.
Impact
A whitelisted attacker can block standard pair deployments, preventing honest users from deploying pairs with the same configuration; the deployed custom pair may be unusable if a past maturity date is set.
What the attacker needed
Attacker must be whitelisted; Attacker can monitor the mempool; Victim initiates a standard pair deployment
The fix
The mitigation is to validate that the _name parameter in deployCustom is not 'public' and to prefix custom pair names to prevent front-running.
How it was found
- A whitelisted user monitors the mempool for pending standard pair deployments.
- They see a deployment transaction with a specific config data.
- They call deployCustom with a name that yields the same salt (e.g., 'public') and the same config data, possibly with additional parameters like a past maturity date.
- The custom deployment executes first, storing the salt in deployedPairsBySalt.
- The original standard deployment then reverts because the salt is already used.
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 a new lending pair is created, the system uses a unique identifier (salt) to ensure no two pairs are identical. A whitelisted user can watch for pending pair creations and quickly create their own pair with the same identifier, which makes the original creation fail. This blocks legitimate users from creating their pairs.
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.