BaseBranchRouter._transferAndApproveToken may revert in some cases
Originally disclosed on Code4rena
Submitted by nobody2018
In the bot-report.md for (M-04) Return values of approve() not checked, it describes that “by not checking the return value, operations that should have marked as failed, may potentially go through without actually approving anything”, but the following contents of this submission is different.
If the token’s approve does not return a value, like USDT, then ERC20(_token).approve will revert. Because ERC20 comes from solmate/tokens/ERC20.sol, its approve is defined as function approve(address spender, uint256 amount) public virtual returns (bool) where the return value is bool type with a size of 1 byte. The bytecode compiled by the solidity compiler from ERC20(_token).approve will check whether the return size is 1 byte. If not, revert will occur.
This will cause all functions calling _transferAndApproveToken to revert. This includes callOutAndBridge and callOutAndBridgeMultiple.
Proof of Concept
File: src\BaseBranchRouter.sol
160: function _transferAndApproveToken(address _hToken, address _token, uint256 _amount, uint256 _deposit) internal {
......
173: if (_deposit > 0) {
174: _token.safeTransferFrom(msg.sender, address(this), _deposit);
175:-> ERC20(_token).approve(_localPortAddress, _deposit);
176: }
177: }
L175, the compiled code of ERC20(_token).approve(_localPortAddress, _deposit) is similar to the following:
(bool ret, bytes data) = _token.call(abi.encodeWithSignature("approve(address,uint256)", _localPortAddress, _deposit);
if (ret) {
if (data.length != 1) // since usdt.approve has no return value, so data.length = 0
{
revert;
}
return abi.decode(data, (bool));
} else {
revert;
}
Copy the coded POC below to one project from Foundry and run forge test -vvv to prove this issue:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
import "forge-std/Test.sol";
import "solmate/tokens/ERC20.sol";
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";
interface CheatCodes {
function prank(address) external;
function createSelectFork(string calldata,uint256) external returns(uint256);
}
interface IUSDT {
function approve(address, uint256) external;
}
contract ContractTest is DSTest{
address USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
function setUp() public {
cheats.createSelectFork("https://rpc.ankr.com/eth", 18283438);
}
function test() public {
address user = address(0x12399543949349);
cheats.prank(user);
ERC20(USDT).approve(address(0x1111111111), 1000e6);
}
function testOkByIUSDT() public {
address user = address(0x12399543949349);
cheats.prank(user);
IUSDT(USDT).approve(address(0x1111111111), 1000e6);
}
function testOkBySafeApprove() public {
address user = address(0x12399543949349);
cheats.prank(user);
SafeTransferLib.safeApprove(ERC20(USDT), address(0x1111111111), 1000e6);
}
}
/**output
Running 3 tests for src/test/usdtApprove.sol:ContractTest
[FAIL. Reason: EvmError: Revert] test() (gas: 37109)
Traces:
[37109] ContractTest::test()
├─ [0] VM::prank(0x0000000000000000000000000012399543949349)
│ └─ ← ()
├─ [26953] 0xdAC17F958D2ee523a2206206994597C13D831ec7::approve(0x0000000000000000000000000000001111111111, 1000000000)
│ ├─ emit Approval(owner: 0x0000000000000000000000000012399543949349, spender: 0x0000000000000000000000000000001111111111, value: 1000000000)
│ └─ ← ()
└─ ← "EvmError: Revert"
[PASS] testOkByIUSDT() (gas: 37113)
[PASS] testOkBySafeApprove() (gas: 36988)
Test result: FAILED. 2 passed; 1 failed; finished in 489.70ms
**/
Recommended Mitigation Steps
Use safeApprove from solady/utils/SafeTransferLib.sol.
File: src\BaseBranchRouter.sol
160: function _transferAndApproveToken(address _hToken, address _token, uint256 _amount, uint256 _deposit) internal {
......
173: if (_deposit > 0) {
174: _token.safeTransferFrom(msg.sender, address(this), _deposit);
175:- ERC20(_token).approve(_localPortAddress, _deposit);
175:+ ERC20(_token).safeApprove(_localPortAddress, _deposit);
176: }
177: }
Assessed type
DoS
Out of Scope - USDT reverts because it needs to approve to zero first, which is also in the bot report.
nobody2018 (warden) commented:
The issue described in this report is not about approving to zero first, but the difference between
usdt.approveandERC20.approvesignatures:
usdt.approveis defined as follows:function approve(address _spender, uint _value) public;ERC20.approveis defined as follows:function approve(address spender, uint256 amount) public virtual returns (bool);This will lead to different opcodes compiled by the compiler. When checking the length of the return data,
usdt.approverequires the length of the return data to be 0, whileERC20.approverequires the length of the return data to be 1. Therefore, tx always reverts.The POC of this report is to prove this issue, not approve to zero first.
Note that while this deviation from the standard only happens on the mainnet variant of USDT, and not on the Arbitrum one, it is likely that the protocol would be extended with branches to mainnet. Non-adherence to the ERC20 standard in the case of mainnet USDT can’t be considered a poisoned token, and therefore the Medium severity is sustained.
Addressed here.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity doS bug in Maia DAO - Ulysses. Code4rena made it public on 29 November 2023. Found in the 2023-09-maia audit contest (finding M-06).
About DoS
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.