HardhatDeFi
15,000 USDC
View results
Submission Details
Severity: medium
Invalid

abi.encodePacked() should not be used with dynamic types when passing the result to a hash function such as keccak256()

Vulnerability Report: Use of abi.encodePacked() with Dynamic Types

Summary

The use of abi.encodePacked() with dynamic types in Solidity can lead to hash collisions when the resulting byte array is passed to a hash function such as keccak256(). This vulnerability arises because abi.encodePacked() concatenates values without padding them to 32 bytes, which can result in different inputs producing the same output. This report outlines the details of the vulnerability, its impact, tools used for testing, and recommendations for mitigation.

Vulnerability Details

  • Vulnerability Type: Hash Collision

  • Affected Contract: AaveDIVAWrapperCore.sol

  • Line Number: 93

  • Description: The line of code in question is:

    string(abi.encodePacked("w", _collateralTokenContract.symbol()))

When using abi.encodePacked() with dynamic types (e.g., strings), the resulting byte array may lead to hash collisions. For example, abi.encodePacked("1", "23456") and abi.encodePacked("123", "456") can produce the same hash output, which can be exploited in scenarios where unique identifiers are critical.

Impact

The impact of this vulnerability can be significant, especially in contracts that rely on unique hashes for critical operations such as:

  • Identifying unique assets or tokens.

  • Validating user inputs or transactions.

  • Ensuring the integrity of data stored on-chain.

If an attacker can generate a hash collision, they may be able to manipulate contract behavior, leading to unauthorized access, loss of funds, or other unintended consequences.

Tools Used

  • Solidity: The programming language used to write the smart contract.

  • Hardhat: A development environment for Ethereum that allows for testing and deploying smart contracts.

  • Mocha & Chai: JavaScript testing frameworks used to write and execute tests for the smart contract.

Recommendations

  1. Use abi.encode(): Replace instances of abi.encodePacked() with abi.encode() when dealing with dynamic types to ensure proper padding and avoid hash collisions.

    • Example: Change abi.encodePacked("prefix", input) to abi.encode("prefix", input).

  2. Conduct Code Reviews: Regularly review smart contract code for potential vulnerabilities, especially when dealing with hashing and encoding functions.

  3. Implement Unit Tests: Create comprehensive unit tests to verify the behavior of hashing functions and ensure that they produce expected outputs for a variety of inputs.

  4. Stay Updated: Keep abreast of best practices and updates in the Solidity language and Ethereum development community to mitigate emerging vulnerabilities.

  5. Consider Alternative Approaches: If the use of dynamic types is unavoidable, consider using other methods for generating unique identifiers that do not rely on hashing.

Proof of Concept (PoC)

To demonstrate the issue, the following Solidity contract can be used:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract HashTest {
function hashPacked(string memory input) public pure returns (bytes32) {
return keccak256(abi.encodePacked("prefix", input));
}
function hashEncoded(string memory input) public pure returns (bytes32) {
return keccak256(abi.encode("prefix", input));
}
}
Updates

Lead Judging Commences

bube Lead Judge 4 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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