Platform: Code4rena
Start Date: 29/07/2022
Pot Size: $50,000 USDC
Total HM: 6
Participants: 75
Period: 5 days
Judge: GalloDaSballo
Total Solo HM: 3
Id: 149
League: ETH
Rank: 13/75
Findings: 2
Award: $195.99
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: oyc_109
Also found by: 0x1f8b, 0x52, 0xNazgul, 0xSmartContract, 0xf15ers, 8olidity, Aymen0909, Bnke0x0, CertoraInc, Chom, CodingNameKiki, Deivitto, Dravee, ElKu, IllIllI, JC, Lambda, Noah3o6, NoamYakov, RedOneN, Respx, ReyAdmirado, Rohan16, Rolezn, Ruhum, Sm4rty, TomJ, Twpony, Waze, Yiko, __141345__, ajtra, apostle0x01, ashiq0x01, asutorufos, bardamu, benbaessler, berndartmueller, bharg4v, bulej93, c3phas, cccz, ch13fd357r0y3r, codexploder, cryptonue, cryptphi, defsec, djxploit, durianSausage, fatherOfBlocks, gogo, hansfriese, horsefacts, ignacio, kyteg, lucacez, mics, rbserver, robee, sashik_eth, simon135, sseefried, tofunmi, xiaoming90
136.7488 USDC - $136.75
The protocol is using low level calls with solidity version 0.8.x which can result in optimizer bug.
https://medium.com/certora/overly-optimistic-optimizer-certora-bug-disclosure-2101e3f7994d
https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/deposit-service/ReceiverImplementation.sol#L3
Code Review
Consider upgrading to solidity 0.8.15.
Some tokens (like USDT L199) do not work when changing the allowance from an existing non-zero allowance value. They must first be approved by zero and then the actual allowance must be approved.
https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/deposit-service/ReceiverImplementation.sol#L38
When trying to re-approve an already approved token, all transactions revert and the protocol cannot be used.
None
Approve with a zero amount first before setting the actual amount.
Owner role has absolute power across the contracts with several onlyOwner functions. There is no ability to change admin to a new address or renounce it which is helpful for lost/compromised admin keys or to delegate control to a different governance/DAO address in future.
The project does not use the widely used OpenZeppelin Ownable library which provides transfer/renounce functions to mitigate such compromised/accidental situations with admin keys. This makes admin role/key a single-point of failure.
Ensure admins are reasonably redundant/independent (3/7 or 5/9) multisigs and add transfer/renounce functionality for admin. Consider using OpenZeppelin’s Ownable library.
Missing checks for zero-addresses may lead to infunctional protocol, if the variable addresses are updated incorrectly.
https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/gas-service/AxelarGasService.sol#L12 https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/gas-service/AxelarGasService.sol#L110
Code Review
Consider adding zero-address checks in the discussed constructors: require(newAddr != address(0));.
Selfdestruct is a function in the Solidity smart contract used to delete contracts on the blockchain. When a contract executes a self-destruct operation, the remaining ether on the contract account will be sent to a specified target, and its storage and code are erased. The self-destruct function is a double-edged sword. However, using the self.destruct function can break the contract and leads to loss of functionality.
https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/DepositHandler.sol#L29
Code Review
Consider reviewing self.destruct functionality.
There is no impact to the funds but to align with best practices, it is always better to update internal state before any external function calls.
https://github.com/code-423n4/2022-07-axelar/blob/main/xc20/contracts/XC20Wrapper.sol#L76
Call external interaction before accounting. (Update the internal accounting before transferring the tokens out.)
In case a hack is occuring or an exploit is discovered, the team should be able to pause functionality until the necessary changes are made to the system.
To use a thorchain example again, the team behind thorchain noticed an attack was going to occur well before the system transferred funds to the hacker. However, they were not able to shut the system down fast enough. (According to the incidence report here: https://github.com/HalbornSecurity/PublicReports/blob/master/Incident%20Reports/Thorchain_Incident_Analysis_July_23_2021.pdf)
Code Review
Pause functionality on the contract would have helped secure the funds quickly.
The contracts inherit OpenZeppelin's Ownable contract which enables the onlyOwner role to transfer ownership to another address. It's possible that the onlyOwner role mistakenly transfers ownership to the wrong address, resulting in a loss of the onlyOwner role. The current ownership transfer process involves the current owner calling Unlock.transferOwnership(). This function checks the new owner is not the zero address and proceeds to write the new owner's address into the owner's state variable. If the nominated EOA account is not a valid account, it is entirely possible the owner may accidentally transfer ownership to an uncontrolled account, breaking all functions with the onlyOwner() modifier. Lack of two-step procedure for critical operations leaves them error-prone if the address is incorrect, the new address will take on the functionality of the new role immediately
for Ex : -Alice deploys a new version of the whitehack group address. When she invokes the whitehack group address setter to replace the address, she accidentally enters the wrong address. The new address now has access to the role immediately and is too late to revert
None
Implement zero address check and Consider implementing a two step process where the owner nominates an account and the nominated account needs to call an acceptOwnership() function for the transfer of ownership to fully succeed. This ensures the nominated EOA account is a valid and active account.
The following contract functions performs an ERC20.approve() call but does not check the success return value. Some tokens do not revert if the approval failed but return false instead.
https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/deposit-service/ReceiverImplementation.sol#L38
None
Its recommend to using OpenZeppelin’s SafeERC20 versions with the safeApprove function that handles the return value check as well as non-standard-compliant tokens.
A mixture of Failing to calling init the implementation contract after it is deployed can leads to self-destruct through proxy.
https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/gas-service/AxelarGasServiceProxy.sol https://github.com/code-423n4/2022-07-axelar/blob/main/contracts/util/Proxy.sol#L26
None
Ensure that init function called.
#0 - GalloDaSballo
2022-08-31T21:18:47Z
Great submission, however codebase is using 0.8.9 and the bug was introduced in 0.8.13 (per the article you linked) <img width="701" alt="Screenshot 2022-08-31 at 23 03 05" src="https://user-images.githubusercontent.com/13383782/187781574-c9e36c78-db4e-4bd4-8a00-ad635a7b1287.png">
Will give it NC
Disagree because the sendToken
will transferFrom
all tokens, setting allowance back to 0
Disputed in lack of a specific POC. Also disputed per the rulebook, usage of timelock cannot be proven from the code and as such while the comment is appreciated it is not falsifiable. See: https://github.com/code-423n4/org/issues/7
##Â C4-004 : Missing zero-address check in the setter functions in the constructor - Low Valid Low
I think NC is more appropriate given the usage in the codebase
Valid Low
##Â C4-007 : Contract should have pause/unpause functionality Will respectfully disagree
NC
Valid Low
Will dispute as proxy receives implementation in the constructor, as such there is no time in which the proxy can be maliciously destroyed.
If you do find such an instance, please raise severity (High may be in order, Med for sure) and submit with POC
Overall the report is definitely automated, but more hits than misses
3L 3NC
🌟 Selected for report: IllIllI
Also found by: 0x1f8b, 0xNazgul, 0xsam, 8olidity, Aymen0909, Bnke0x0, Chom, CodingNameKiki, Deivitto, Dravee, ElKu, Fitraldys, JC, Lambda, MiloTruck, Noah3o6, NoamYakov, RedOneN, Respx, ReyAdmirado, Rohan16, Rolezn, Ruhum, Sm4rty, TomJ, Tomio, Waze, __141345__, a12jmx, ajtra, ak1, apostle0x01, asutorufos, benbaessler, bharg4v, bulej93, c3phas, defsec, djxploit, durianSausage, erictee, fatherOfBlocks, gerdusx, gogo, kyteg, lucacez, medikko, mics, owenthurm, oyc_109, rbserver, robee, sashik_eth, simon135, tofunmi
59.2384 USDC - $59.24
[S]: Suggested optimation, save a decent amount of gas without compromising readability;
[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;
[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.
> 0
can be replaced with != 0
for gas optimizationFor the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
None
Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.
Unchecked{i++};
Since _amount can be 0. Checking if (_amount != 0) before the transfer can potentially save an external call and the unnecessary gas cost of a 0 token transfer.
https://github.com/jbx-protocol/juice-contracts-v2-code4rena/blob/828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol#L87
All Contracts
None
Consider checking amount != 0.
Uint is default initialized to 0. There is no need assign false to variable.
2022-07-axelar-main/contracts/AxelarGateway.sol::207 => for (uint256 i = 0; i < symbols.length; i++) { 2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::68 => uint256 totalWeight = 0; 2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::69 => for (uint256 i = 0; i < weightsLength; ++i) { 2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::94 => uint256 operatorIndex = 0; 2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::95 => uint256 weight = 0; 2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::98 => for (uint256 i = 0; i < signatures.length; ++i) {
Code Review
uint x = 0 costs more gas than uint x without having any different functionality.
Using double require instead of operator && can save more gas.
2022-07-axelar-main/contracts/gas-service/AxelarGasService.sol:159: bool transferred = success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); 2022-07-axelar-main/contracts/gas-service/AxelarGasService.sol:175: bool transferred = success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); 2022-07-axelar-main/contracts/deposit-service/DepositBase.sol:72: bool transferred = success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol:118: if (refundTokens[i] == gatewayToken && msg.sender != refundAddress) continue; 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol:165: if (addressForNativeDeposit(salt, refundAddress, destinationChain, destinationAddress).balance > 0 && msg.sender != refundAddress) 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol:208: if (refundTokens[i] == wrappedTokenAddress && msg.sender != refundAddress) continue; 2022-07-axelar-main/contracts/AxelarGateway.sol:388: if (!success || (returnData.length != uint256(0) && !abi.decode(returnData, (bool)))) revert BurnFailed(symbol); 2022-07-axelar-main/contracts/AxelarGateway.sol:462: return success && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); 2022-07-axelar-main/contracts/AxelarGateway.sol:613: if (limit > 0 && amount > limit) revert ExceedDailyMintLimit(symbol);
Code Review
Example
using &&: function check(uint x)public view{ require(x == 0 && x < 1 ); } // gas cost 21630 using double require: require(x == 0 ); require( x < 1); } } // gas cost 21622
Strict inequalities add a check of non equality which costs around 3 gas.
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
Code Review
Use >= or <= instead of > and < when possible.
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
None
Consider to cache array length.
In some cases, having function arguments in calldata instead of memory is more optimal.
Consider the following generic example:
contract C { function add(uint[] memory arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }
In the above example, the dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient. Consider the following snippet instead:
contract C { function add(uint[] calldata arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }
In the above snippet, instead of going via memory, the value is directly read from calldata using calldataload. That is, there are no intermediate memory operations that carries this value.
Gas savings: In the former example, the ABI decoding begins with copying value from calldata to memory in a for loop. Each iteration would cost at least 60 gas. In the latter example, this can be completely avoided. This will also reduce the number of instructions and therefore reduces the deploy time cost of the contract.
In short, use calldata instead of memory if the function argument is only read.
Note that in older Solidity versions, changing some function arguments from memory to calldata may cause "unimplemented feature error". This can be avoided by using a newer (0.8.*) Solidity compiler.
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol:56: (address[] memory newOperators, uint256[] memory newWeights, uint256 newThreshold) = abi.decode(
None
Some parameters in examples given above are later hashed. It may be beneficial for those parameters to be in memory rather than calldata.
++i is more gas efficient than i++ in loops forwarding.
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) { 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
Code Review
It is recommend to use unchecked{++i} and change i declaration to uint256.
> 0
can be replaced with != 0
for gas optimization!= 0
is a cheaper operation compared to > 0
, when dealing with uint.
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::165 => if (addressForNativeDeposit(salt, refundAddress, destinationChain, destinationAddress).balance > 0 && msg.sender != refundAddress) 2022-07-axelar-main/contracts/deposit-service/ReceiverImplementation.sol::23 => if (address(this).balance > 0) refundAddress.transfer(address(this).balance); 2022-07-axelar-main/contracts/deposit-service/ReceiverImplementation.sol::51 => if (address(this).balance > 0) refundAddress.transfer(address(this).balance); 2022-07-axelar-main/contracts/deposit-service/ReceiverImplementation.sol::71 => if (address(this).balance > 0) refundAddress.transfer(address(this).balance);
None
Consider to replace > 0
with != 0
for gas optimization.
The contracts assigns two constants to the result of a keccak operation, which results in gas waste since the expression is computed each time the constant is accessed.
See this issue for more context: ethereum/solidity#9232 (https://github.com/ethereum/solidity/issues/9232)
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::227 => keccak256( 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::233 => keccak256(abi.encodePacked(type(DepositReceiver).creationCode, abi.encode(delegateData))) 2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::242 => return keccak256('axelar-deposit-service'); 2022-07-axelar-main/contracts/deposit-service/AxelarDepositServiceProxy.sol::9 => return keccak256('axelar-deposit-service'); 2022-07-axelar-main/contracts/gas-service/AxelarGasService.sol::27 => keccak256(payload), 2022-07-axelar-main/contracts/gas-service/AxelarGasService.sol::52 => keccak256(payload), 2022-07-axelar-main/contracts/gas-service/AxelarGasService.sol::71 => emit NativeGasPaidForContractCall(sender, destinationChain, destinationAddress, keccak256(payload), msg.value, refundAddress); 2022-07-axelar-main/contracts/gas-service/AxelarGasService.sol::90 => keccak256(payload), 2022-07-axelar-main/contracts/gas-service/AxelarGasService.sol::181 => return keccak256('axelar-gas-service');
None
Replace the constant directive with immutable, or assign the already hashed value to the constants.
Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.
All Contracts
Solidity 0.8.13 has a useful change which reduced gas costs of external calls which expect a return value: https://blog.soliditylang.org/2021/11/09/solidity-0.8.10-release-announcement/
Solidity 0.8.13 has some improvements too but not well tested.
Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist
All Contracts
None
Consider to upgrade pragma to at least 0.8.13.
#0 - GalloDaSballo
2022-08-20T22:41:31Z
Less than 500 gas saved (calldata <- mem example doesn't apply from what I can tell)