Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack of Slippage Controls in retrieveTokensForWithdraw Function #1490

Open
c4-bot-2 opened this issue May 17, 2024 · 6 comments
Open

Lack of Slippage Controls in retrieveTokensForWithdraw Function #1490

c4-bot-2 opened this issue May 17, 2024 · 6 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working insufficient quality report This report is not of sufficient quality M-08 primary issue Highest quality submission among a set of duplicates 🤖_primary AI based primary recommendation 🤖_139_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards selected for report This submission will be included/highlighted in the audit report sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@c4-bot-2
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2024-04-noya/blob/main/contracts/accountingManager/AccountingManager.sol#L548

Vulnerability details

Impact

Without slippage controls in the retrieveTokensForWithdraw function, there is a risk of financial loss for users during token retrieval from connectors. This function handles sensitive operations where base tokens are pulled from external liquidity pools or other decentralized financial instruments to fulfill withdrawal requests. In volatile market conditions, the absence of slippage controls can lead to a significant discrepancy between the expected and actual amounts of tokens retrieved, potentially resulting in users receiving less value than their withdrawal entitlements.

Proof of Concept

The function interacts with connectors to retrieve tokens without ensuring the retrieval amounts are within acceptable slippage ranges

https://github.com/code-423n4/2024-04-noya/blob/main/contracts/accountingManager/AccountingManager.sol#L548

   function retrieveTokensForWithdraw(RetrieveData[] calldata retrieveData) public onlyManager nonReentrant {
       uint256 amountAskedForWithdraw_temp = 0;
       uint256 neededAssets = neededAssetsForWithdraw();
       for (uint256 i = 0; i < retrieveData.length; i++) {
           if (!registry.isAnActiveConnector(vaultId, retrieveData[i].connectorAddress)) {
               continue;
           }
           uint256 balanceBefore = baseToken.balanceOf(address(this));
           uint256 amount = IConnector(retrieveData[i].connectorAddress).sendTokensToTrustedAddress(
               address(baseToken), retrieveData[i].withdrawAmount, address(this), retrieveData[i].data
           );
           uint256 balanceAfter = baseToken.balanceOf(address(this));
           if (balanceBefore + amount > balanceAfter) revert NoyaAccounting_banalceAfterIsNotEnough();
           amountAskedForWithdraw_temp += retrieveData[i].withdrawAmount;
           emit RetrieveTokensForWithdraw(
               retrieveData[i].withdrawAmount,
               retrieveData[i].connectorAddress,
               amount,
               amountAskedForWithdraw + amountAskedForWithdraw_temp
           );
       }
       amountAskedForWithdraw += amountAskedForWithdraw_temp;
       if (amountAskedForWithdraw_temp > neededAssets) {
           revert NoyaAccounting_INVALID_AMOUNT();
       }
   }

This code lacks mechanisms to check the price impact of retrieving large amounts from connectors, which could lead to unfavorable token retrieval rates.

Tools Used

Manual

Recommended Mitigation Steps

Integrating slippage control into the retrieveTokensForWithdraw function can be done by adding a check to ensure the amount of tokens retrieved is within an acceptable range of the expected amount. Here is a suggested implementation:

// Constants defining the maximum allowable slippage percentage (e.g., 1% slippage)
uint256 private constant MAX_SLIPPAGE = 100; // Representing 1%

function retrieveTokensForWithdraw(RetrieveData[] calldata retrieveData) public onlyManager nonReentrant {
    uint256 amountAskedForWithdraw_temp = 0;
    uint256 neededAssets = neededAssetsForWithdraw();
    for (uint256 i = 0; i < retrieveData.length; i++) {
        if (!registry.isAnActiveConnector(vaultId, retrieveData[i].connectorAddress)) {
            continue;
        }
        uint256 balanceBefore = baseToken.balanceOf(address(this));
        uint256 amount = IConnector(retrieveData[i].connectorAddress).sendTokensToTrustedAddress(
            address(baseToken), retrieveData[i].withdrawAmount, address(this), retrieveData[i].data
        );
        uint256 balanceAfter = baseToken.balanceOf(address(this));
        if (balanceBefore + amount > balanceAfter) revert NoyaAccounting_banalceAfterIsNotEnough();
        
        // Check for slippage
        uint256 expectedAmount = retrieveData[i].withdrawAmount;
        uint256 actualAmount = balanceAfter - balanceBefore;
        uint256 maxSlippageAmount = (expectedAmount * (10000 + MAX_SLIPPAGE)) / 10000;
        if (actualAmount < expectedAmount || actualAmount > maxSlippageAmount) {
            revert NoyaAccounting_SlippageTooHigh();
        }
        
        amountAskedForWithdraw_temp += actualAmount;
        emit RetrieveTokensForWithdraw(
            actualAmount,
            retrieveData[i].connectorAddress,
            amount,
            amountAskedForWithdraw + amountAskedForWithdraw_temp
            

Assessed type

Invalid Validation

@c4-bot-2 c4-bot-2 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels May 17, 2024
@c4-bot-13 c4-bot-13 added 🤖_139_group AI based duplicate group recommendation 🤖_primary AI based primary recommendation labels May 17, 2024
@c4-pre-sort
Copy link

DadeKuma marked the issue as primary issue

@c4-pre-sort c4-pre-sort added the primary issue Highest quality submission among a set of duplicates label May 18, 2024
@DadeKuma
Copy link

QA as these are deposits/withdrawals and not swaps

@c4-pre-sort
Copy link

DadeKuma marked the issue as insufficient quality report

@c4-pre-sort c4-pre-sort added the insufficient quality report This report is not of sufficient quality label May 21, 2024
@c4-judge
Copy link
Contributor

c4-judge commented Jun 2, 2024

gzeon-c4 marked the issue as satisfactory

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jun 2, 2024
@c4-judge
Copy link
Contributor

c4-judge commented Jun 2, 2024

gzeon-c4 marked the issue as selected for report

@gzeon-c4
Copy link

gzeon-c4 commented Jun 2, 2024

Deposit into a vault is implicitly a swap (deposit token to vault share).

@HadiEsna HadiEsna added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working insufficient quality report This report is not of sufficient quality M-08 primary issue Highest quality submission among a set of duplicates 🤖_primary AI based primary recommendation 🤖_139_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards selected for report This submission will be included/highlighted in the audit report sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

8 participants