Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/spear bits audit5 #90

Merged
merged 12 commits into from
Feb 3, 2023
Merged

Feature/spear bits audit5 #90

merged 12 commits into from
Feb 3, 2023

Conversation

invocamanman
Copy link
Collaborator

@invocamanman invocamanman commented Jan 27, 2023

This PR solves the following spearbits audit issues:
https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/42
https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/43
https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/46
https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/22
https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/15
https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/38

Also this PR have the following changes:

  • Erase not used imports in PolygonZkEVM contract

  • Update a little bit the condition on DepositContract:

    • Before: depositCount < _MAX_DEPOSIT_COUNT
    • After: depositCount > _MAX_DEPOSIT_COUNT
      Note that now depositCount can be ==_MAX_DEPOSIT_COUNT, this should be safe and it's more consistent wit the use of MAX in the whole contract.
  • Update a little bit the logic of PolygonZkEVMGlobalExitRoot::updateExitRoot after using custom errors to optimize gas

  • The change from < to > in setVerifyBatchTimeTarget / newVerifyBatchTimeTarget but the difference is neglectable PolygonZkEVM.sol#L1224

@invocamanman invocamanman changed the base branch from main to develop January 30, 2023 16:29
@invocamanman invocamanman marked this pull request as ready for review February 2, 2023 00:11
newVerifyBatchTimeTarget < 1 days,
"PolygonZkEVM::setVerifyBatchTimeTarget: setVerifyBatchTimeTarget incorrect range"
);
if (newVerifyBatchTimeTarget > 1 days) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a conscious choice to save a little bit of gas by not doing newVerifyBatchTimeTarget >= 1 days?

Copy link

@0xleastwood 0xleastwood Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

difference here would be negligible as already discussed in https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/22

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! it is!

@0xleastwood
Copy link

lgtm!

(bool success, bytes memory data) = address(token).staticcall(
abi.encodeCall(IERC20MetadataUpgradeable.decimals, ())
);
return success && data.length == 32 ? uint8(abi.decode(data, (uint256))) : 18;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might make sense to use uint8 here and let it revert for tokens that use decimals greater than 255.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yee i will change it again! i agree

depositCount < _MAX_DEPOSIT_COUNT,
"DepositContract:_deposit: Merkle tree full"
);
if (depositCount > _MAX_DEPOSIT_COUNT) {
Copy link

@0xleastwood 0xleastwood Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding this as mentioned in https://github.com/spearbit-audits/review-polygon-zkevm-contracts/issues/22 , this would allow for _MAX_DEPOSIT_COUNT + 1 deposits which I think would overflow the merkle tree?

EDIT: This would not overflow the merkle tree but instead make use of the last empty leaf represented by 32 zero bits.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bit representation for _MAX_DEPOSIT_COUNT + 1 will be identical to the zero deposit when we look only at the right most 32 bits. But fortunately, we increment depositCount the first time this function is called, so we don't already use this leaf and hence, nothing should be overwritten as far as I can see.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was a little bit gas saving, the last deposit if i'm not mistaken will happen something like this:
depositCount = 2**32 --> the first 32 bits will be 0

  uint256 size = ++depositCount;
        for (
            uint256 height = 0;
            height < _DEPOSIT_CONTRACT_TREE_DEPTH;
            height++
        ) {
            if (((size >> height) & 1) == 1) {
                _branch[height] = node;
                return;
            }
            node = keccak256(abi.encodePacked(_branch[height], node));
        }

Here the condition if (((size >> height) & 1) == 1) will never be true, and therefore we will get into the assert(false)

Anyway this does not seem obvious so i will just put the equal in the revert condition

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye I agree!

error EtherTransferFailed();

/**
* @dev Thrown when the message transactoin on claimMessage fail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when the message transactoin on claimMessage fail
* @dev Thrown when the message transaction on claimMessage fail

error GlobalExitRootInvalid();

/**
* @dev Thrown when a the smt proof does not match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when a the smt proof does not match
* @dev Thrown when the smt proof does not match

error NotValidOwner();

/**
* @dev Thrown when the spender of permit does not this contract
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when the spender of permit does not this contract
* @dev Thrown when the spender of permit is not this contract

error OnlyTrustedAggregator();

/**
* @dev Thrown when try to sequence 0 batches
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when try to sequence 0 batches
* @dev Thrown when trying to sequence 0 batches

error SequenceZeroBatches();

/**
* @dev Thrown when an try to sequence or verify more batches than _MAX_VERIFY_BATCHES
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when an try to sequence or verify more batches than _MAX_VERIFY_BATCHES
* @dev Thrown when trying to sequence or verify more batches than _MAX_VERIFY_BATCHES

pragma solidity ^0.8.0;

/**
* @dev Implementation of the ERC20 with werid metadata.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Implementation of the ERC20 with werid metadata.
* @dev Implementation of the ERC20 with weird metadata.

(bool success, bytes memory data) = address(token).staticcall(
abi.encodeCall(IERC20MetadataUpgradeable.symbol, ())
);
return success ? _returnDataToString(data) : "???";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename ???

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update NatSpec after change

(bool success, bytes memory data) = address(token).staticcall(
abi.encodeCall(IERC20MetadataUpgradeable.name, ())
);
return success ? _returnDataToString(data) : "???";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename ???

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update NatSpec after change

}

// If the first one is 0, we do not handle the encoding
if (nonZeroBytes == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed to handle

Copy link
Collaborator Author

@invocamanman invocamanman Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally i will handle it, in my opinoin if the first byte returned is 0 means that the token was trying encode somehow a name, i think it's better this way that just an empty string

}
return string(bytesArray);
} else {
return "???";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename ???

error ForceBatchesOverflow();

/**
* @dev Thrown when are sequenced more force batches than actually were submitted
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when are sequenced more force batches than actually were submitted
* @dev Thrown when more force batches were sequenced than actually submitted

error TrustedAggregatorTimeoutNotExpired();

/**
* @dev Thrown when try to access a pending that does not exist
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when try to access a pending that does not exist
* @dev Thrown when trying to access a pending state that does not exist


/**
* @dev Thrown when try to sequence a force batch using sequenceForceBatches and the
* force timeout do not expired
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* force timeout do not expired
* force timeout did not expire

@@ -8,6 +8,11 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
* Based on the implementation of the deposit eth2.0 contract https://github.com/ethereum/consensus-specs/blob/dev/solidity_deposit_contract/deposit_contract.sol
*/
contract DepositContract is Initializable {
/**
* @dev Thrown when the merle tree is full
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when the merle tree is full
* @dev Thrown when the merkle tree is full

error MsgValueNotZero();

/**
* @dev Thrown when the ether transfer on claimAsset fail
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @dev Thrown when the ether transfer on claimAsset fail
* @dev Thrown when the ether transfer on claimAsset fails

(bool success, bytes memory data) = address(token).staticcall(
abi.encodeCall(IERC20MetadataUpgradeable.symbol, ())
);
return success ? _returnDataToString(data) : "???";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update NatSpec after change

(bool success, bytes memory data) = address(token).staticcall(
abi.encodeCall(IERC20MetadataUpgradeable.name, ())
);
return success ? _returnDataToString(data) : "???";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update NatSpec after change

@invocamanman invocamanman merged commit e242081 into develop Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants