DeFiFoundry
50,000 USDC
View results
Submission Details
Severity: low
Invalid

[M-05] Lack of Calldata Validation in `_doDexSwap` May Lead to Incorrect Swaps

Summary

The _doDexSwap function does not properly validate the callData before executing a swap. Since _doDexSwap is responsible for interacting with the Paraswap protocol through the ParaSwapUtils.swap function, unverified callData can lead to incorrect swaps. This vulnerability can result in unintended token swaps, incorrect balance calculations, or failed transactions due to incorrect token approvals.

Vulnerability Details

Function Involved

function _doDexSwap(bytes memory data, bool isCollateralToIndex) internal returns (uint256 outputAmount) {
(address to, uint256 amount, bytes memory callData) = abi.decode(data, (address, uint256, bytes));
IERC20 inputToken;
IERC20 outputToken;
if (isCollateralToIndex) {
inputToken = collateralToken;
outputToken = IERC20(indexToken);
} else {
inputToken = IERC20(indexToken);
outputToken = collateralToken;
}
uint256 balBefore = outputToken.balanceOf(address(this));
ParaSwapUtils.swap(to, callData); // Unchecked calldata being passed to external swap function
outputAmount = IERC20(outputToken).balanceOf(address(this)) - balBefore;
emit DexSwap(address(inputToken), amount, address(outputToken), outputAmount, isCollateralToIndex);
}

This function takes in the swap data and decodes it to retrieve:

  • to: the target contract address (Paraswap in this case)

  • amount: the amount being swapped

  • callData: the transaction data for the Paraswap call

However, there is no validation on callData before passing it to ParaSwapUtils.swap(), meaning:

  1. The fromToken in the decoded calldata may not match the expected inputToken.

  2. The calldata structure may be incorrect, leading to failed swaps or unintended approvals.

  3. The swap may execute an unintended token pair if the calldata is tampered with.

Flow of Execution

  • _runSwap calls _doDexSwap

  • _doDexSwap extracts callData and executes ParaSwapUtils.swap

  • ParaSwapUtils.swap extracts token addresses from callData and executes the swap

  • No validation ensures that fromToken matches inputToken

Example of a Potential Issue

Consider a scenario where the keeper provides an incorrect callData where:

  • The fromToken is not the expected inputToken

  • The swap is executed for a different token than intended

assembly {
fromToken := mload(add(callData, 68)) // Extracts fromToken from calldata
}

If the extracted fromToken does not match inputToken, the function may still proceed with execution, leading to an incorrect swap.

Impact

  • Incorrect Swaps: The function may swap the wrong tokens due to manipulated calldata.

  • Fund Mismanagement: If the wrong token is approved and swapped, user funds could be misallocated.

  • Failed Transactions: If an invalid callData is provided, transactions may revert, leading to inefficiencies.

  • Potential Fund Loss: If tokens are swapped incorrectly and sent to an unintended address, funds may become irrecoverable.

Tools Used

  • Manual Code Review

Recommendations

1. Validate the callData Before Execution

Modify _doDexSwap to extract fromToken from callData and ensure it matches inputToken.

(address to, uint256 amount, bytes memory callData) = abi.decode(data, (address, uint256, bytes));
// Extract fromToken from callData
address fromToken;
assembly {
fromToken := mload(add(callData, 68))
}
// Ensure the extracted token matches the expected input token
require(fromToken == address(inputToken), "Incorrect input token in calldata");

2. Implement Additional Safeguards in ParaSwapUtils.swap

Modify ParaSwapUtils.swap() to validate fromToken before executing the swap.

function swap(address to, bytes memory callData) external {
_validateCallData(to, callData);
address approvalAddress = IAugustusSwapper(to).getTokenTransferProxy();
address fromToken;
uint256 fromAmount;
assembly {
fromToken := mload(add(callData, 68))
fromAmount := mload(add(callData, 100))
}
// Ensure the fromToken is an allowed token
require(fromToken == expectedToken, "Invalid fromToken in calldata");
IERC20(fromToken).safeApprove(approvalAddress, fromAmount);
(bool success, ) = to.call(callData);
require(success, "Paraswap call reverted");
}

3. Add Logging for Debugging

Add an event to log decoded fromToken and expected inputToken before executing swaps.

event DebugSwap(address expectedToken, address fromToken, uint256 amount);
emit DebugSwap(address(inputToken), fromToken, amount);
Updates

Lead Judging Commences

n0kto Lead Judge 7 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Admin is trusted / Malicious keepers

Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelihood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point. Keepers are added by the admin, there is no "malicious keeper" and if there is a problem in those keepers, that's out of scope. ReadMe and known issues states: " * System relies heavily on keeper for executing trades * Single keeper point of failure if not properly distributed * Malicious keeper could potentially front-run or delay transactions * Assume that Keeper will always have enough gas to execute transactions. There is a pay execution fee function, but the assumption should be that there's more than enough gas to cover transaction failures, retries, etc * There are two spot swap functionalies: (1) using GMX swap and (2) using Paraswap. We can assume that any swap failure will be retried until success. " " * Heavy dependency on GMX protocol functioning correctly * Owner can update GMX-related addresses * Changes in GMX protocol could impact system operations * We can assume that the GMX keeper won't misbehave, delay, or go offline. " "Issues related to GMX Keepers being DOS'd or losing functionality would be considered invalid."

invalid_ParaswapUtils_swap_malicious_callData

This function call only reached via a function called by the keeper. So no malicious callData will be provided.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.