collateralizeWLp can be bypassed even when collateralization is paused
The collateralizeWLp function checks whether collateralization is paused, but the collateral credit for wrapped LP tokens is calculated from the live LP token balance of the tokenId, which anyone can increase by donating tokens directly. An attacker could therefore add collateral to a position even when the admin has paused collateralization.
Submitted by said
Admin can pause collateralization for a specific mode to prevent users from providing more collateral either via collateralize or collateralizeWLp. However, due to not properly using internal accounting when tracking wLP collateral, users can still provide more collateral by directly donating tokens to a specific LP tokenId.
Proof of Concept
It can be seen that when canCollateralize of certain mode is paused, collateralizeWLp should be paused.
https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/core/InitCore.sol#L243-L261
/// @inheritdoc IInitCore
function collateralizeWLp(uint _posId, address _wLp, uint _tokenId)
public
virtual
onlyAuthorized(_posId)
nonReentrant
{
IConfig _config = IConfig(config);
uint16 mode = _getPosMode(_posId);
// check mode status
>>> _require(_config.getModeStatus(mode).canCollateralize, Errors.COLLATERALIZE_PAUSED);
// check if the wLp is whitelisted
_require(_config.whitelistedWLps(_wLp), Errors.TOKEN_NOT_WHITELISTED);
// check if the position mode supports _wLp
_require(_config.isAllowedForCollateral(mode, IBaseWrapLp(_wLp).lp(_tokenId)), Errors.INVALID_MODE);
// update collateral on the position
uint amtColl = IPosManager(POS_MANAGER).addCollateralWLp(_posId, _wLp, _tokenId);
emit CollateralizeWLp(_wLp, _tokenId, _posId, amtColl);
}
However, when calculating collateral credit, it will calculate based on balance of LP of specific token inside wLP contract.
https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/core/InitCore.sol#L456
function getCollateralCreditCurrent_e36(uint _posId) public virtual returns (uint collCredit_e36) {
address _oracle = oracle;
IConfig _config = IConfig(config);
uint16 mode = _getPosMode(_posId);
// get position collateral
>>> (address[] memory pools, uint[] memory shares, address[] memory wLps, uint[][] memory ids, uint[][] memory amts)
= IPosManager(POS_MANAGER).getPosCollInfo(_posId);
// calculate collateralCredit
uint collCredit_e54;
for (uint i; i < pools.length; i = i.uinc()) {
address token = ILendingPool(pools[i]).underlyingToken();
uint tokenPrice_e36 = IInitOracle(_oracle).getPrice_e36(token);
uint tokenValue_e36 = ILendingPool(pools[i]).toAmtCurrent(shares[i]) * tokenPrice_e36;
TokenFactors memory factors = _config.getTokenFactors(mode, pools[i]);
collCredit_e54 += tokenValue_e36 * factors.collFactor_e18;
}
for (uint i; i < wLps.length; i = i.uinc()) {
for (uint j; j < ids[i].length; j = j.uinc()) {
uint wLpPrice_e36 = IBaseWrapLp(wLps[i]).calculatePrice_e36(ids[i][j], _oracle);
uint wLpValue_e36 = amts[i][j] * wLpPrice_e36;
TokenFactors memory factors = _config.getTokenFactors(mode, IBaseWrapLp(wLps[i]).lp(ids[i][j]));
collCredit_e54 += wLpValue_e36 * factors.collFactor_e18;
}
}
collCredit_e36 = collCredit_e54 / ONE_E18;
}
https://github.com/code-423n4/2023-12-initcapital/blob/main/contracts/core/PosManager.sol#L125
function getPosCollInfo(uint _posId)
external
view
returns (
address[] memory pools,
uint[] memory amts,
address[] memory wLps,
uint[][] memory ids,
uint[][] memory wLpAmts
)
{
PosCollInfo storage posCollInfo = __posCollInfos[_posId];
pools = posCollInfo.collTokens.values();
amts = new uint[](pools.length);
for (uint i; i < pools.length; i = i.uinc()) {
amts[i] = posCollInfo.collAmts[pools[i]];
}
wLps = posCollInfo.wLps.values();
ids = new uint[][](wLps.length);
wLpAmts = new uint[][](wLps.length);
for (uint i; i < wLps.length; i = i.uinc()) {
ids[i] = posCollInfo.ids[wLps[i]].values();
wLpAmts[i] = new uint[](ids[i].length);
for (uint j; j < ids[i].length; j = j.uinc()) {
>>> wLpAmts[i][j] = IBaseWrapLp(wLps[i]).balanceOfLp(ids[i][j]);
}
}
}
It should be noted that most DEXs (e.g., Uniswap) allow any user to provide liquidity to any other users position. In practice, this bypasses the collateralization paused functionality.
Recommended Mitigation Steps
Implement internal accounting for wLP inside the PosManager.
fez-init (INIT) acknowledged and commented:
Internal accounting shall be ensured in wLp.
Medium is appropriate as the admin’s action can be bypassed.
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 contract uses the live token balance (balanceOfLp) instead of internal accounting to track wLP collateral, so direct token donations are counted as collateral.
Impact
The admin's pause on collateralization can be bypassed, allowing users to add collateral when they should not be able to.
What the attacker needed
A position with a wLP tokenId; ability to transfer LP tokens directly
The fix
Implement internal accounting for wLP inside the PosManager so that only explicitly added collateral is counted.
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 users add collateral to a position, and an admin can pause that action. However, the contract works out how much collateral a position has by looking at the current balance of a token, and anyone can send extra tokens to that token address. So a user can still increase the collateral by sending tokens directly, bypassing the pause.
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.