Understanding and Preventing Short Address Attacks in Solidity Smart Contracts
What is a Short Address Attack?
A Short Address Attack occurs when an attacker manipulates the data sent in an Ethereum transaction, tricking the smart contract into reading more data than was sent. This exploit leverages the way Solidity pads shorter arguments to 32 bytes.
Ethereum addresses are typically 20 bytes long, but if an attacker uses an address shorter than 20 bytes, the Solidity contract will automatically pad the difference on the right side with zeros to ensure that the address is appropriate. Suppose a function expects two arguments (e.g., an address and a token amount). In that case, the contract might interpret part of the second argument (the token amount) as part of the first (the address) due to the automatic padding, effectively allowing the attacker to send more tokens than intended.
An Example of a Short Address Attack
Consider a Solidity function transfer(address _to, uint256 _value)
that is used to transfer tokens from the sender's account to another account. Here's a simplified version:
function transfer(address _to, uint256 _value) public {
require(balances[msg.sender] >= _value);
balances[msg.sender] -= _value;
balances[_to] +=…