Use call() rather than transfer() on address payable
The smart contract uses `.transfer()` to send ether, which forwards only 2300 gas units. An attacker could cause payments to fail if the recipient is a contract requiring more gas, or if gas costs rise, breaking the protocol.
Originally disclosed on Code4rena
Submitted by cloudjunky, also found by 141345, _Adam, 0x1f8b, 0x4non, 0x52, 0xDjango, 0xf15ers, 0xHarry, 0xNazgul, 0xNineDec, 0xsanson, 0xsolstars, 8olidity, arcoun, asutorufos, bardamu, bearonbike, bin2chen, Bnke0x0, brgltd, bulej93, c3phas, carlitox477, cccz, CertoraInc, Chom, codexploder, cRat1st0s, cryptonue, cryptphi, cthulhu_cult, Deivitto, dharma09, dipp, djxploit, Dravee, durianSausage, ellahi, GalloDaSballo, GimelSec, giovannidisiena, hansfriese, horsefacts, hyh, IllIllI, immeas, indijanc, jayjonah8, jayphbee, Jmaxmanblue, joestakey, JohnSmith, Jujic, Kenshin, kenzo, Krow10, kyteg, ladboy233, Lambda, MEP, minhquanym, navinavu, Noah3o6, obront, oyc_109, peritoflores, rbserver, reassor, RedOneN, rokinot, rotcivegaf, Ruhum, saian, scaraven, shenwilly, simon135, sseefried, StErMi, StyxRave, teddav, TomJ, Treasure-Seeker, TrungOre, and zzzitron
L154 in GolomTrader.sol uses .transfer() to send ether to other addresses. There are a number of issues with using .transfer(), as it can fail for a number of reasons (specified in the Proof of Concept).
Proof of Concept
- The destination is a smart contract that doesn’t implement a
payablefunction or it implements apayablefunction but that function uses more than 2300 gas units. - The destination is a smart contract that doesn’t implement a
payablefallbackfunction or it implements apayablefallbackfunction but that function uses more than 2300 gas units. - The destination is a smart contract but that smart contract is called via an intermediate proxy contract increasing the case requirements to more than 2300 gas units. A further example of unknown destination complexity is that of a multisig wallet that as part of its operation uses more than 2300 gas units.
- Future changes or forks in Ethereum result in higher gas fees than transfer provides. The
.transfer()creates a hard dependency on 2300 gas units being appropriate now and into the future.
Tools Used
Vim
Recommended Remediation Steps
Instead use the .call() function to transfer ether and avoid some of the limitations of .transfer(). This would be accomplished by changing payEther() to something like;
(bool success, ) = payable(payAddress).call{value: payAmt}(""); // royalty transfer to royaltyaddress
require(success, "Transfer failed.");
Gas units can also be passed to the .call() function as a variable to accomodate any uses edge cases. Gas could be a mutable state variable that can be set by the contract owner.
0xsaruman (Golom) resolved and commented:
Given how many upgrades I’m making on this, I figured a comment on my reasoning was in order. In many contests, this would be considered low risk. While unlikely to occur without warning, it is well-documented and so very well might occur at some point in the foreseeable future. With Golom’s implementation, the entire functionality of the protocol would break if the gas price were to rise, resulting in a need to relaunch/redeploy. The extreme nature of this disruption offsets the other factors normally considered and is why I consider it to be a medium risk in this contest.
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
Likely the use of `.transfer()` instead of `.call()` for sending ether, which imposes a hard 2300 gas limit.
Impact
Payments could fail if the recipient requires more than 2300 gas or if gas costs rise, potentially breaking the entire protocol.
What the attacker needed
Recipient is a contract needing >2300 gas; or gas costs rise
The fix
The issue was fixed by replacing `.transfer()` with `.call()` and checking the success of the call.
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, it can use `.transfer()`, which gives the recipient a fixed small amount of gas (2300 units) to run code. If the recipient needs more gas, the payment fails. This report says that could break the whole protocol.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.