🔏Lack of Code Size Check in Token Transfer Functions in Solmate

A notable vulnerability exists within the Solmate library's implementations of token transfer functions, such as safetransfer and safetransferfrom, which do not verify the code size of the token address. This oversight can lead to significant risks, including potential loss of funds.

Vulnerability Details

Impact: The primary consequence of this vulnerability is the possibility of miscalculation in transaction records, resulting in a loss of funds. When safetransfer and safetransferfrom are called on a token address that is not a valid contract (i.e., it does not contain executable code), these functions will return success without executing an actual transfer of tokens.

Proof of Concept: In scenarios where these functions are invoked without verifying that the target address has a valid contract code size, they will incorrectly assume that the operation was successful. This creates a significant problem: if no actual transfer occurs, the contract's state is updated based on the erroneous assumption that tokens have been moved. Consequently, the protocol misrecords the transaction as successful, which can lead to discrepancies in fund accounting and management.

Risk Summary: This vulnerability highlights the critical importance of implementing thorough checks when interacting with external addresses. The failure to verify the existence and validity of a contract at a given address can result in financial losses and undermine the trustworthiness of smart contracts utilizing the Solmate library.

Mitigation Strategies

To mitigate this vulnerability, developers using Solmate should incorporate the following best practices:

  1. Code Size Check: Before executing any token transfers, implement checks to ensure that the target address has a non-zero code size. This can be achieved using the extcodesize opcode in Solidity.

  2. Fallback Mechanism: Integrate fallback mechanisms to address situations where a transfer to an invalid address is attempted, thereby safeguarding against unintended fund losses.

  3. Comprehensive Testing: Conduct rigorous testing and audits of the codebase to identify and rectify instances where Solmate's token transfer functions are used without appropriate checks.

By addressing this vulnerability in the Solmate library, developers can enhance the security and robustness of their smart contracts, ultimately protecting users from potential financial losses.

Last updated