Return value of low level call not checked
The smart contract sends Ether using a low-level call but does not check whether the call succeeded. If the recipient contract rejects the transfer, the transaction still completes and the Ether remains stuck in the contract.
dd0x7e8 Follow with Udsen, SanketKogekar and 17 others
Originally disclosed on Code4rena
Submitted by dd0x7e8, also found by Udsen, SanketKogekar, pep7siup, hpsb, parsely, josephdara, Fulum, Sathish9098, shirochan, MatricksDeCoder, Kaysoft, ravikiranweb3, Bughunter101, j4ld1na, T1MOH, fatherOfBlocks, grearlake, debo, and piyushshukla
Impact
If the msg.sender is a contract and its receive() function has the potential to revert, the code payable(msg.sender).call{value:wad}("") could potentially return a false result, which is not being verified. As a result, the calling functions may exit without successfully returning ethers to senders.
Proof of Concept
PoC using Foundry:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
contract CallerContract {
VaultContract private vaultAddress;
constructor(VaultContract _vaultAddr) payable {
vaultAddress = _vaultAddr;
}
receive() payable external {
revert();
}
function claim(uint256 amount) public {
vaultAddress.claimEther(amount);
}
}
contract VaultContract {
CallerContract private callerContract;
constructor() payable {}
receive() payable external {
revert();
}
function claimEther(uint256 amount) public {
require(amount <= 3 ether, "Cannot claim more than 3 ether");
msg.sender.call{value:amount}("");
//(bool sent,) = msg.sender.call{value: amount}("");
//require(sent, "sent failed");
}
function sendEither() payable external {
callerContract.claim(3 ether);
}
function setCaller(CallerContract _caller) public {
callerContract = _caller;
}
}
contract CallReturnValueNotCheckedTest is Test {
address public Bob;
VaultContract public vaultContract;
CallerContract public callerContract;
function setUp() public {
Bob = makeAddr("Bob");
vaultContract = new VaultContract();
callerContract = new CallerContract(vaultContract);
vaultContract.setCaller(callerContract);
deal(Bob, 10 ether);
}
function test_callReturnNotChecked() public {
console2.log("Before calling: Bob balance is %d ether", Bob.balance / 1e18);
console2.log("Before calling: Vault balance is %d ether", address(vaultContract).balance / 1e18);
console2.log("Bob sends 3 ether");
vm.startPrank(Bob);
//vm.expectRevert();
vaultContract.sendEither{value: 3 ether}();
console2.log("After calling: Bob balance is %d ether", Bob.balance / 1e18);
console2.log("After calling: Vault balance is %d ether", address(vaultContract).balance / 1e18);
vm.stopPrank();
}
}
The output is:
Running 1 test for test/ReturnValueTest.t.sol:CallReturnValueNotCheckedTest
[PASS] test_callReturnNotChecked() (gas: 42207)
Logs:
Before calling: Bob balance is 10 ether
Before calling: Vault balance is 0 ether
Bob sends 3 ether
After calling: Bob balance is 7 ether
After calling: Vault balance is 3 ether
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 762.13µs
Ran 1 test suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
The caller contract fails to receive ether, but the whole transaction is not reverted. As a result, the ether will be locked in the Vault contract.
This issue can be prevented by adding logic to check the return value of call.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
contract CallerContract {
VaultContract private vaultAddress;
constructor(VaultContract _vaultAddr) payable {
vaultAddress = _vaultAddr;
}
receive() payable external {
revert();
}
function claim(uint256 amount) public {
vaultAddress.claimEther(amount);
}
}
contract VaultContract {
CallerContract private callerContract;
constructor() payable {}
receive() payable external {
revert();
}
function claimEther(uint256 amount) public {
require(amount <= 3 ether, "Cannot claim more than 3 ether");
//msg.sender.call{value:amount}("");
(bool sent,) = msg.sender.call{value: amount}("");
require(sent, "sent failed");
}
function sendEither() payable external {
callerContract.claim(3 ether);
}
function setCaller(CallerContract _caller) public {
callerContract = _caller;
}
}
contract CallReturnValueNotCheckedTest is Test {
address public Bob;
VaultContract public vaultContract;
CallerContract public callerContract;
function setUp() public {
Bob = makeAddr("Bob");
vaultContract = new VaultContract();
callerContract = new CallerContract(vaultContract);
vaultContract.setCaller(callerContract);
deal(Bob, 10 ether);
}
function test_callReturnNotChecked() public {
console2.log("Before calling: Bob balance is %d ether", Bob.balance / 1e18);
console2.log("Before calling: Vault balance is %d ether", address(vaultContract).balance / 1e18);
console2.log("Bob sends 3 ether");
vm.startPrank(Bob);
//vm.expectRevert();
vaultContract.sendEither{value: 3 ether}();
console2.log("After calling: Bob balance is %d ether", Bob.balance / 1e18);
console2.log("After calling: Vault balance is %d ether", address(vaultContract).balance / 1e18);
vm.stopPrank();
}
function test_callReturnChecked_revert() public {
console2.log("Before calling: Bob balance is %d ether", Bob.balance / 1e18);
console2.log("Before calling: Vault balance is %d ether", address(vaultContract).balance / 1e18);
console2.log("Bob sends 3 ether");
vm.startPrank(Bob);
vm.expectRevert();
vaultContract.sendEither{value: 3 ether}();
console2.log("After calling: Bob balance is %d ether", Bob.balance / 1e18);
console2.log("After calling: Vault balance is %d ether", address(vaultContract).balance / 1e18);
vm.stopPrank();
}
}
Output of modified code:
Running 1 test for test/ReturnValueTest.t.sol:CallReturnValueNotCheckedTest
[PASS] test_callReturnChecked_revert() (gas: 42788)
Logs:
Before calling: Bob balance is 10 ether
Before calling: Vault balance is 0 ether
Bob sends 3 ether
After calling: Bob balance is 10 ether
After calling: Vault balance is 0 ether
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 6.60ms
Ran 1 test suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
Now the ether is returned back if call is failed.
Recommended Mitigation Steps
It’s recommended to check the return value to be true or just use OpenZeppelin Address library sendValue() function for ether transfer. See https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.9.3/contracts/utils/Address.sol#L64 .
Keref (Good Entry) confirmed and commented:
See PR#3.
Status: Not fully mitigated. Full details in reports from kutugu and 3docSec, and also included in the Mitigation Review section below.
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 contract uses a low-level call to send Ether but ignores the boolean return value that indicates success or failure.
Impact
Ether sent to a contract that rejects the transfer could become permanently locked in the sending contract.
What the attacker needed
Victim contract rejects Ether; attacker can trigger the send
The fix
The contract should check the return value of the low-level call and revert if it is false, or use OpenZeppelin's `sendValue()` function.
How it was found
- Deploy a vault contract that sends Ether using an unchecked low-level call.
- Deploy a caller contract whose receive function reverts.
- Have the caller contract invoke the vault's claim function.
- Observe that the transaction does not revert and the Ether remains in the vault.
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.
When a smart contract sends Ether to another contract, the receiving contract can refuse the payment. The sending contract must check whether the payment succeeded; otherwise, the Ether can get stuck in the sending contract.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.